Class: Tapyrus::PSTT::KeyOriginInfo

Inherits:
Object
  • Object
show all
Extended by:
KeyPath
Includes:
KeyPath
Defined in:
lib/tapyrus/pstt/key_origin_info.rb

Overview

The value of a BIP 32 derivation record: a master key fingerprint followed by a derivation path.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from KeyPath

parse_key_path, to_key_path

Constructor Details

#initialize(fingerprint:, key_paths: []) ⇒ KeyOriginInfo

Returns a new instance of KeyOriginInfo.

Parameters:

  • fingerprint (String)

    the master key fingerprint with hex format.

  • key_paths (Array[Integer]) (defaults to: [])

    the derivation path.

Raises:



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/tapyrus/pstt/key_origin_info.rb', line 18

def initialize(fingerprint:, key_paths: [])
  raise Error, "fingerprint must be 4 bytes." unless fingerprint.htb.bytesize == 4
  # Each element occupies 4 bytes of the record. Array#pack truncates a larger value to its
  # lowest 32 bits without raising, which would turn a path this object was built with into
  # a different one once it is serialized.
  unless key_paths.all? { |index| index.is_a?(Integer) && index >= 0 && index <= 0xffffffff }
    raise Error, "Each derivation path element must be a 32-bit unsigned integer."
  end
  @fingerprint = fingerprint
  @key_paths = key_paths
end

Instance Attribute Details

#fingerprintObject (readonly)

Returns the value of attribute fingerprint.



10
11
12
# File 'lib/tapyrus/pstt/key_origin_info.rb', line 10

def fingerprint
  @fingerprint
end

#key_pathsObject (readonly)

Returns the value of attribute key_paths.



14
15
16
# File 'lib/tapyrus/pstt/key_origin_info.rb', line 14

def key_paths
  @key_paths
end

Class Method Details

.from_path(fingerprint, path) ⇒ Tapyrus::PSTT::KeyOriginInfo

Build from a derivation path string such as "m/44'/2377'/0'/0/0".

Parameters:

  • fingerprint (String)

    the master key fingerprint with hex format.

  • path (String)

    the derivation path string.

Returns:



45
46
47
# File 'lib/tapyrus/pstt/key_origin_info.rb', line 45

def self.from_path(fingerprint, path)
  new(fingerprint: fingerprint, key_paths: parse_key_path(path))
end

.parse_from_payload(payload) ⇒ Tapyrus::PSTT::KeyOriginInfo

Parse the value of a BIP 32 derivation record.

Parameters:

  • payload (String)

    the value with binary format.

Returns:



33
34
35
36
37
38
39
# File 'lib/tapyrus/pstt/key_origin_info.rb', line 33

def self.parse_from_payload(payload)
  if payload.bytesize < 4 || ((payload.bytesize - 4) % 4) != 0
    raise Error, "BIP 32 derivation value must be a 4-byte fingerprint followed by 32-bit path elements."
  end
  fingerprint, *key_paths = payload.unpack("H8V*")
  new(fingerprint: fingerprint, key_paths: key_paths)
end

Instance Method Details

#==(other) ⇒ Object



58
59
60
# File 'lib/tapyrus/pstt/key_origin_info.rb', line 58

def ==(other)
  other.is_a?(KeyOriginInfo) && to_payload == other.to_payload
end

#pathString

Returns the derivation path string.

Returns:

  • (String)

    the derivation path string.



54
55
56
# File 'lib/tapyrus/pstt/key_origin_info.rb', line 54

def path
  to_key_path(key_paths)
end

#to_payloadObject



49
50
51
# File 'lib/tapyrus/pstt/key_origin_info.rb', line 49

def to_payload
  fingerprint.htb + key_paths.pack("V*")
end