Class: Tapyrus::ExtPubkey

Inherits:
Object show all
Includes:
HexConverter
Defined in:
lib/tapyrus/ext_key.rb

Overview

BIP-32 Extended public key

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HexConverter

#to_hex

Instance Attribute Details

#chain_codeObject

Returns the value of attribute chain_code.



205
206
207
# File 'lib/tapyrus/ext_key.rb', line 205

def chain_code
  @chain_code
end

#depthObject

Returns the value of attribute depth.



203
204
205
# File 'lib/tapyrus/ext_key.rb', line 203

def depth
  @depth
end

#numberObject

Returns the value of attribute number.



204
205
206
# File 'lib/tapyrus/ext_key.rb', line 204

def number
  @number
end

#parent_fingerprintObject

Returns the value of attribute parent_fingerprint.



207
208
209
# File 'lib/tapyrus/ext_key.rb', line 207

def parent_fingerprint
  @parent_fingerprint
end

#pubkeyObject

hex format



206
207
208
# File 'lib/tapyrus/ext_key.rb', line 206

def pubkey
  @pubkey
end

#verObject

Returns the value of attribute ver.



202
203
204
# File 'lib/tapyrus/ext_key.rb', line 202

def ver
  @ver
end

Class Method Details

.encode_base58(hex) ⇒ String

Generate Base58 encoded key from BIP32 payload with hex format.

Parameters:

  • hex (String)

    BIP32 payload with hex format.

Returns:

  • (String)

    Base58 encoded extended key.



252
253
254
# File 'lib/tapyrus/ext_key.rb', line 252

def self.encode_base58(hex)
  Base58.encode(hex + Tapyrus.calc_checksum(hex))
end

.from_base58(address) ⇒ Object

import pub key from Base58 private key address



322
323
324
# File 'lib/tapyrus/ext_key.rb', line 322

def self.from_base58(address)
  ExtPubkey.parse_from_payload(ExtPubkey.validate_checksum(address))
end

.parse_from_payload(payload) ⇒ Object

Raises:

  • (ArgumentError)


303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/tapyrus/ext_key.rb', line 303

def self.parse_from_payload(payload)
  buf = StringIO.new(payload)
  ext_pubkey = ExtPubkey.new
  ext_pubkey.ver = buf.read(4).bth # version
  raise ArgumentError, Errors::Messages::INVALID_BIP32_VERSION unless ExtPubkey.support_version?(ext_pubkey.ver)
  ext_pubkey.depth = buf.read(1).unpack('C').first
  ext_pubkey.parent_fingerprint = buf.read(4).bth
  ext_pubkey.number = buf.read(4).unpack('N').first
  if ext_pubkey.depth == 0
    raise ArgumentError, Errors::Messages::INVALID_BIP32_FINGERPRINT unless ext_pubkey.parent_fingerprint == ExtKey::MASTER_FINGERPRINT
    raise ArgumentError, Errors::Messages::INVALID_BIP32_ZERO_INDEX if ext_pubkey.number > 0
  end
  raise ArgumentError, Errors::Messages::INVALID_BIP32_ZERO_DEPTH if ext_pubkey.parent_fingerprint == ExtKey::MASTER_FINGERPRINT && ext_pubkey.depth > 0
  ext_pubkey.chain_code = buf.read(32)
  ext_pubkey.pubkey = Tapyrus::Key.new(pubkey: buf.read(33).bth).pubkey
  ext_pubkey
end

.support_version?(version) ⇒ Boolean

check whether version is supported version bytes.

Returns:

  • (Boolean)


349
350
351
352
# File 'lib/tapyrus/ext_key.rb', line 349

def self.support_version?(version)
  p = Tapyrus.chain_params
  [p.bip49_pubkey_p2wpkh_p2sh_version, p.bip84_pubkey_p2wpkh_version, p.extended_pubkey_version].include?(version)
end

.validate_checksum(base58) ⇒ String

Validate address checksum and return payload.

Parameters:

  • BIP32 (String)

    Base58 address

Returns:

  • (String)

    BIP32 payload with binary format

Raises:

  • (ArgumentError)


329
330
331
332
333
# File 'lib/tapyrus/ext_key.rb', line 329

def self.validate_checksum(base58)
  raw = Base58.decode(base58)
  raise ArgumentError, Errors::Messages::INVALID_CHECKSUM unless Tapyrus.calc_checksum(raw[0...-8]) == raw[-8..-1]
  raw[0...-8].htb
end

.version_from_purpose(purpose) ⇒ Object

get version bytes from purpose’ value.



336
337
338
339
340
341
342
343
344
345
346
# File 'lib/tapyrus/ext_key.rb', line 336

def self.version_from_purpose(purpose)
  v = purpose - Tapyrus::HARDENED_THRESHOLD
  case v
    when 49
      Tapyrus.chain_params.bip49_pubkey_p2wpkh_p2sh_version
    when 84
      Tapyrus.chain_params.bip84_pubkey_p2wpkh_version
    else
      Tapyrus.chain_params.extended_pubkey_version
  end
end

Instance Method Details

#==(other) ⇒ Object



299
300
301
# File 'lib/tapyrus/ext_key.rb', line 299

def ==(other)
  to_payload == other.to_payload
end

#addrObject

get address



224
225
226
# File 'lib/tapyrus/ext_key.rb', line 224

def addr
  key.to_p2pkh
end

#derive(number) ⇒ Object

derive child key



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/tapyrus/ext_key.rb', line 262

def derive(number)
  new_key = ExtPubkey.new
  new_key.depth = depth + 1
  new_key.number = number
  new_key.parent_fingerprint = fingerprint
  raise 'hardened key is not support' if number > (Tapyrus::HARDENED_THRESHOLD - 1)
  data = pub.htb << [number].pack('N')
  l = Tapyrus.hmac_sha512(chain_code, data)
  left = l[0..31].bth.to_i(16)
  raise 'invalid key' if left >= CURVE_ORDER
  p1 = Tapyrus::Secp256k1::GROUP.generator.multiply_by_scalar(left)
  p2 = Tapyrus::Key.new(pubkey: pubkey, key_type: key_type).to_point
  new_key.pubkey = (p1 + p2).to_hex
  new_key.chain_code = l[32..-1]
  new_key.ver = version
  new_key
end

#fingerprintObject

get fingerprint



240
241
242
# File 'lib/tapyrus/ext_key.rb', line 240

def fingerprint
  identifier.slice(0..7)
end

#hardened?Boolean

whether hardened key.

Returns:

  • (Boolean)


257
258
259
# File 'lib/tapyrus/ext_key.rb', line 257

def hardened?
  number >= Tapyrus::HARDENED_THRESHOLD
end

#hash160Object



219
220
221
# File 'lib/tapyrus/ext_key.rb', line 219

def hash160
  Tapyrus.hash160(pub)
end

#identifierObject

get key identifier



235
236
237
# File 'lib/tapyrus/ext_key.rb', line 235

def identifier
  Tapyrus.hash160(pub)
end

#keyTapyrus::Key

get key object

Returns:



230
231
232
# File 'lib/tapyrus/ext_key.rb', line 230

def key
  Tapyrus::Key.new(pubkey: pubkey, key_type: key_type)
end

#key_typeObject

get key type defined by BIP-178 using version.



287
288
289
290
291
292
293
294
295
296
297
# File 'lib/tapyrus/ext_key.rb', line 287

def key_type
  v = version
  case v
  when Tapyrus.chain_params.bip49_pubkey_p2wpkh_p2sh_version
    Tapyrus::Key::TYPES[:p2wpkh_p2sh]
  when Tapyrus.chain_params.bip84_pubkey_p2wpkh_version
    Tapyrus::Key::TYPES[:p2wpkh]
  when Tapyrus.chain_params.extended_pubkey_version
    Tapyrus::Key::TYPES[:compressed]
  end
end

#pubObject



215
216
217
# File 'lib/tapyrus/ext_key.rb', line 215

def pub
  pubkey
end

#to_base58Object

Base58 encoded extended pubkey



245
246
247
# File 'lib/tapyrus/ext_key.rb', line 245

def to_base58
  ExtPubkey.encode_base58(to_hex)
end

#to_payloadObject

serialize extended pubkey



210
211
212
213
# File 'lib/tapyrus/ext_key.rb', line 210

def to_payload
  version.htb << [depth].pack('C') <<
      parent_fingerprint.htb << [number].pack('N') << chain_code << pub.htb
end

#versionObject

get version bytes using serialization format



281
282
283
284
# File 'lib/tapyrus/ext_key.rb', line 281

def version
  return ExtPubkey.version_from_purpose(number) if depth == 1
  ver ? ver : Tapyrus.chain_params.extended_pubkey_version
end