Module: Bitcoin::KeyPath

Included in:
BIP85Entropy, 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:

Raises:

  • (ArgumentError)

    if invalid path_string.



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

def parse_key_path(path_string)
  paths = path_string.split('/')
  raise ArgumentError, "Invalid path." if path_string.include?(" ")
  raise ArgumentError, "Invalid path." unless path_string.count("/") <= paths.size
  paths.map.with_index do|p, index|
    if index == 0
      next if p == 'm'
      raise ArgumentError, "Invalid path." unless p == 'm'
    end
    raise ArgumentError, "Invalid path." if p.count("'") > 1 || (p.count("'") == 1 && p[-1] != "'")
    raise ArgumentError, "Invalid path." unless p.delete("'") =~ /^[0-9]+$/
    value = (p[-1] == "'" ? p.delete("'").to_i + Bitcoin::HARDENED_THRESHOLD : p.to_i)
    raise ArgumentError, "Invalid path." if value > 4294967295 # 4294967295 = 0xFFFFFFFF (uint32 max)
    value
  end[1..-1]
end

#to_key_path(numbers) ⇒ String

key path numbers convert to path string.

Parameters:

Returns:



28
29
30
# File 'lib/bitcoin/key_path.rb', line 28

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