Class: Bitcoin::PSBT::HDKeyPath

Inherits:
Object
  • Object
show all
Defined in:
lib/bitcoin/psbt/hd_key_path.rb

Overview

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pubkey, fingerprint: nil, path: []) ⇒ HDKeyPath

Returns a new instance of HDKeyPath.



12
13
14
15
16
# File 'lib/bitcoin/psbt/hd_key_path.rb', line 12

def initialize(pubkey, fingerprint: nil, path: [])
  @pubkey = pubkey
  @fingerprint = fingerprint
  @path = path
end

Instance Attribute Details

#fingerprintObject

Returns the value of attribute fingerprint.



9
10
11
# File 'lib/bitcoin/psbt/hd_key_path.rb', line 9

def fingerprint
  @fingerprint
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/bitcoin/psbt/hd_key_path.rb', line 10

def path
  @path
end

#pubkeyObject (readonly)

Returns the value of attribute pubkey.



8
9
10
# File 'lib/bitcoin/psbt/hd_key_path.rb', line 8

def pubkey
  @pubkey
end

Class Method Details

.parse_from_payload(pubkey, payload) ⇒ Bitcoin::PSBT::HDKeyPath

parse hd key path from payload.

Parameters:

  • pubkey (String)

    a public key with binary format.

  • payload (String)

    hd key path value with binary format.

Returns:

Raises:

  • (ArgumentError)


22
23
24
25
26
27
# File 'lib/bitcoin/psbt/hd_key_path.rb', line 22

def self.parse_from_payload(pubkey, payload)
  raise ArgumentError, 'Size of key was not the expected size for the type BIP32 keypath.' unless [Bitcoin::Key::PUBLIC_KEY_SIZE, Bitcoin::Key::COMPRESSED_PUBLIC_KEY_SIZE].include?(pubkey.bytesize)
  pubkey = Bitcoin::Key.new(pubkey: pubkey.bth)
  raise ArgumentError, 'Invalid pubkey' unless pubkey.fully_valid_pubkey?
  self.new(pubkey.pubkey, fingerprint: payload[0...4].bth, path: payload[4..-1].unpack('I*'))
end

Instance Method Details

#to_payload(type = ) ⇒ String

generate payload which consist of pubkey and fingerprint, hd key path payload.

Returns:



31
32
33
# File 'lib/bitcoin/psbt/hd_key_path.rb', line 31

def to_payload(type = PSBT_IN_TYPES[:bip32_derivation])
  PSBT.serialize_to_vector(type, key: pubkey.htb, value: fingerprint.htb + path.pack('I*'))
end