Class: MoneyTree::PublicKey
- Inherits:
-
Key
- Object
- Key
- MoneyTree::PublicKey
show all
- Defined in:
- lib/money-tree/key.rb
Constant Summary
Constants inherited
from Key
Key::GROUP_NAME, Key::ORDER
Constants included
from Support
Support::BASE58_CHARS, Support::INT32_MAX, Support::INT64_MAX
Instance Attribute Summary collapse
Attributes inherited from Key
#ec_key, #key, #options, #raw_key
Instance Method Summary
collapse
Methods inherited from Key
#valid?
Methods included from Support
#base58_to_int, #bytes_to_hex, #bytes_to_int, #decode_base58, #decode_base64, #digestify, #encode_base58, #encode_base64, #from_serialized_base58, #hex_to_bytes, #hex_to_int, #hmac_sha512, #hmac_sha512_hex, #int_to_base58, #int_to_bytes, #int_to_hex, #ripemd160, #sha256, #to_serialized_base58
Constructor Details
#initialize(p_key, opts = {}) ⇒ PublicKey
Returns a new instance of PublicKey.
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/money-tree/key.rb', line 169
def initialize(p_key, opts = {})
@options = opts
@options[:compressed] = true if @options[:compressed].nil?
if p_key.is_a?(PrivateKey)
@private_key = p_key
@point = @private_key.calculate_public_key(@options)
@group = @point.group
@key = @raw_key = to_hex
else
@raw_key = p_key
@group = PKey::EC::Group.new GROUP_NAME
@key = parse_raw_key
end
raise ArgumentError, "Must initialize with a MoneyTree::PrivateKey or a public key value" if @key.nil?
end
|
Instance Attribute Details
#group ⇒ Object
Returns the value of attribute group.
167
168
169
|
# File 'lib/money-tree/key.rb', line 167
def group
@group
end
|
#key_int ⇒ Object
Returns the value of attribute key_int.
167
168
169
|
# File 'lib/money-tree/key.rb', line 167
def key_int
@key_int
end
|
#point ⇒ Object
Returns the value of attribute point.
167
168
169
|
# File 'lib/money-tree/key.rb', line 167
def point
@point
end
|
#private_key ⇒ Object
Returns the value of attribute private_key.
167
168
169
|
# File 'lib/money-tree/key.rb', line 167
def private_key
@private_key
end
|
Instance Method Details
#compressed ⇒ Object
194
195
196
197
198
|
# File 'lib/money-tree/key.rb', line 194
def compressed
compressed_key = self.class.new raw_key, options compressed_key.set_point to_i, compressed: true
compressed_key
end
|
232
233
234
|
# File 'lib/money-tree/key.rb', line 232
def compressed_hex_format?
raw_key.length == 66 && !raw_key[/\H/]
end
|
#compression ⇒ Object
186
187
188
|
# File 'lib/money-tree/key.rb', line 186
def compression
@group.point_conversion_form
end
|
#compression=(compression_type = :compressed) ⇒ Object
190
191
192
|
# File 'lib/money-tree/key.rb', line 190
def compression=(compression_type = :compressed)
@group.point_conversion_form = compression_type
end
|
228
229
230
|
# File 'lib/money-tree/key.rb', line 228
def hex_format?
raw_key.length == 130 && !raw_key[/\H/]
end
|
#parse_raw_key ⇒ Object
215
216
217
218
219
220
221
222
223
224
225
226
|
# File 'lib/money-tree/key.rb', line 215
def parse_raw_key
result = if raw_key.is_a?(Integer)
set_point raw_key
elsif hex_format?
set_point hex_to_int(raw_key), compressed: false
elsif compressed_hex_format?
set_point hex_to_int(raw_key), compressed: true
else
raise KeyFormatNotFound
end
to_hex
end
|
#set_point(int = to_i, opts = {}) ⇒ Object
206
207
208
209
210
211
212
213
|
# File 'lib/money-tree/key.rb', line 206
def set_point(int = to_i, opts = {})
opts = options.merge(opts)
opts[:compressed] = true if opts[:compressed].nil?
self.compression = opts[:compressed] ? :compressed : :uncompressed
bn = BN.new int_to_hex(int), 16
@point = PKey::EC::Point.new group, bn
raise KeyInvalid, 'point is not on the curve' unless @point.on_curve?
end
|
#to_address(network: :bitcoin) ⇒ Object
Also known as:
to_s
249
250
251
252
253
|
# File 'lib/money-tree/key.rb', line 249
def to_address(network: :bitcoin)
hash = to_ripemd160
address = NETWORKS[network][:address_version] + hash
to_serialized_base58 address
end
|
#to_bytes ⇒ Object
261
262
263
|
# File 'lib/money-tree/key.rb', line 261
def to_bytes
int_to_bytes to_i
end
|
#to_fingerprint ⇒ Object
256
257
258
259
|
# File 'lib/money-tree/key.rb', line 256
def to_fingerprint
hash = to_ripemd160
hash.slice(0..7)
end
|
#to_hex ⇒ Object
236
237
238
|
# File 'lib/money-tree/key.rb', line 236
def to_hex
int_to_hex to_i, 66
end
|
#to_i ⇒ Object
240
241
242
|
# File 'lib/money-tree/key.rb', line 240
def to_i
point.to_bn.to_i
end
|
#to_ripemd160 ⇒ Object
244
245
246
247
|
# File 'lib/money-tree/key.rb', line 244
def to_ripemd160
hash = sha256 to_hex
ripemd160 hash
end
|
#uncompressed ⇒ Object
200
201
202
203
204
|
# File 'lib/money-tree/key.rb', line 200
def uncompressed
uncompressed_key = self.class.new raw_key, options uncompressed_key.set_point to_i, compressed: false
uncompressed_key
end
|