Module: Bitcoin::KeyPath

Included in:
PSBT::KeyOriginInfo, Wallet::MasterKey
Defined in:
lib/bitcoin/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.



7
8
9
10
11
12
13
14
15
16
# File 'lib/bitcoin/key_path.rb', line 7

def parse_key_path(path_string)
  path_string.split('/').map.with_index do|p, index|
    if index == 0
      raise ArgumentError.new("#{path_string} is invalid format.") unless p == 'm'
      next
    end
    raise ArgumentError.new("#{path_string} is invalid format.") unless p.delete("'") =~ /^[0-9]+$/
    (p[-1] == "'" ? p.delete("'").to_i + Bitcoin::HARDENED_THRESHOLD : p.to_i)
  end[1..-1]
end

#to_key_path(numbers) ⇒ String

key path numbers convert to path string.

Parameters:

  • key (Array[Integer])

    path numbers.

Returns:



21
22
23
# File 'lib/bitcoin/key_path.rb', line 21

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