Module: Tapyrus::KeyPath

Included in:
PSTT::KeyOriginInfo, PSTT::KeyOriginInfo, Wallet::MasterKey
Defined in:
lib/tapyrus/key_path.rb

Instance Method Summary collapse

Instance Method Details

#parse_key_path(path_string) ⇒ Array[Integer]

key path convert an array of derive number

Parameters:

Returns:

  • (Array[Integer])

    key path numbers.

Raises:

  • (ArgumentError)

    if the path is not a sequence of decimal indexes, each optionally followed by the hardened marker and each below Tapyrus::HARDENED_THRESHOLD.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/tapyrus/key_path.rb', line 8

def parse_key_path(path_string)
  elements = path_string.split("/")
  raise ArgumentError.new("#{path_string} is invalid format.") unless elements.first == "m"
  elements[1..-1].map do |element|
    hardened = element.end_with?("'")
    # The index is validated before it is converted, since String#to_i stops at the first
    # character which is not a digit and returns what it read so far. An element such as
    # "1'2" would otherwise be taken for the index 1.
    digits = hardened ? element[0..-2] : element
    raise ArgumentError.new("#{path_string} is invalid format.") unless digits =~ /\A[0-9]+\z/
    index = digits.to_i
    # An index of Tapyrus::HARDENED_THRESHOLD or above is the hardened child of a smaller
    # index, so it has no representation of its own in this notation.
    raise ArgumentError.new("#{path_string} is invalid format.") if index >= Tapyrus::HARDENED_THRESHOLD
    hardened ? index + Tapyrus::HARDENED_THRESHOLD : index
  end
end

#to_key_path(numbers) ⇒ String

key path numbers convert to path string.

Parameters:

  • key (Array[Integer])

    path numbers.

Returns:



29
30
31
# File 'lib/tapyrus/key_path.rb', line 29

def to_key_path(numbers)
  "m/#{numbers.map { |p| p >= Tapyrus::HARDENED_THRESHOLD ? "#{p - Tapyrus::HARDENED_THRESHOLD}'" : p.to_s }.join("/")}"
end