Class: MoneyTree::PublicKey

Inherits:
Key
  • Object
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, #convert_p2wpkh_p2sh, #custom_hash_160, #decode_base58, #decode_base64, #digestify, #encode_base58, #encode_base64, #encode_p2wpkh_p2sh, #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, #to_serialized_bech32

Constructor Details

#initialize(p_key, opts = {}) ⇒ PublicKey

Returns a new instance of PublicKey.

Raises:

  • (ArgumentError)


171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/money-tree/key.rb', line 171

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

#groupObject (readonly)

Returns the value of attribute group.



169
170
171
# File 'lib/money-tree/key.rb', line 169

def group
  @group
end

#key_intObject (readonly)

Returns the value of attribute key_int.



169
170
171
# File 'lib/money-tree/key.rb', line 169

def key_int
  @key_int
end

#pointObject (readonly)

Returns the value of attribute point.



169
170
171
# File 'lib/money-tree/key.rb', line 169

def point
  @point
end

#private_keyObject (readonly)

Returns the value of attribute private_key.



169
170
171
# File 'lib/money-tree/key.rb', line 169

def private_key
  @private_key
end

Instance Method Details

#compressedObject



196
197
198
199
200
# File 'lib/money-tree/key.rb', line 196

def compressed
  compressed_key = self.class.new raw_key, options # deep clone
  compressed_key.set_point to_i, compressed: true
  compressed_key
end

#compressed_hex_format?Boolean

Returns:

  • (Boolean)


234
235
236
# File 'lib/money-tree/key.rb', line 234

def compressed_hex_format?
  raw_key.length == 66 && !raw_key[/\H/]
end

#compressionObject



188
189
190
# File 'lib/money-tree/key.rb', line 188

def compression
  @group.point_conversion_form
end

#compression=(compression_type = :compressed) ⇒ Object



192
193
194
# File 'lib/money-tree/key.rb', line 192

def compression=(compression_type = :compressed)
  @group.point_conversion_form = compression_type
end

#hex_format?Boolean

Returns:

  • (Boolean)


230
231
232
# File 'lib/money-tree/key.rb', line 230

def hex_format?
  raw_key.length == 130 && !raw_key[/\H/]
end

#parse_raw_keyObject



217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/money-tree/key.rb', line 217

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

Raises:



208
209
210
211
212
213
214
215
# File 'lib/money-tree/key.rb', line 208

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



251
252
253
254
255
# File 'lib/money-tree/key.rb', line 251

def to_address(network: :bitcoin)
  hash = to_ripemd160
  address = NETWORKS[network][:address_version] + hash
  to_serialized_base58 address
end

#to_bech32_address(network: :bitcoin) ⇒ Object



264
265
266
267
268
# File 'lib/money-tree/key.rb', line 264

def to_bech32_address(network: :bitcoin)
  hrp = NETWORKS[network][:human_readable_part]
  witprog = to_ripemd160
  to_serialized_bech32(hrp, witprog)
end

#to_bytesObject



275
276
277
# File 'lib/money-tree/key.rb', line 275

def to_bytes
  int_to_bytes to_i
end

#to_fingerprintObject



270
271
272
273
# File 'lib/money-tree/key.rb', line 270

def to_fingerprint
  hash = to_ripemd160
  hash.slice(0..7)
end

#to_hexObject



238
239
240
# File 'lib/money-tree/key.rb', line 238

def to_hex
  int_to_hex to_i, 66
end

#to_iObject



242
243
244
# File 'lib/money-tree/key.rb', line 242

def to_i
  point.to_bn.to_i
end

#to_p2wpkh_p2sh(network: :bitcoin) ⇒ Object



259
260
261
262
# File 'lib/money-tree/key.rb', line 259

def to_p2wpkh_p2sh(network: :bitcoin)
  prefix = [NETWORKS[network][:p2sh_version]].pack("H*")
  convert_p2wpkh_p2sh(to_hex, prefix)
end

#to_ripemd160Object



246
247
248
249
# File 'lib/money-tree/key.rb', line 246

def to_ripemd160
  hash = sha256 to_hex
  ripemd160 hash
end

#uncompressedObject



202
203
204
205
206
# File 'lib/money-tree/key.rb', line 202

def uncompressed
  uncompressed_key = self.class.new raw_key, options # deep clone
  uncompressed_key.set_point to_i, compressed: false
  uncompressed_key
end