Method: Bitcoin::Descriptor::Expression#derive_path

Defined in:
lib/bitcoin/descriptor/expression.rb

#derive_path(key, paths) ⇒ Bitcoin::Key

Derive key using paths.

Parameters:

Returns:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bitcoin/descriptor/expression.rb', line 97

def derive_path(key, paths)
  is_private = key.is_a?(Bitcoin::ExtKey)
  paths.each do |path|
    raise ArgumentError, 'xpub can not derive hardened key.' if !is_private && path.end_with?("'")
    if is_private
      hardened = path.end_with?("'")
      path = hardened ? path[0..-2] : path
      raise ArgumentError, 'Key path value is not a valid value.' unless path =~ /^[0-9]+$/
      raise ArgumentError, 'Key path value is out of range.' if !hardened && path.to_i >= Bitcoin::HARDENED_THRESHOLD
      key = key.derive(path.to_i, hardened)
    else
      raise ArgumentError, 'Key path value is not a valid value.' unless path =~ /^[0-9]+$/
      raise ArgumentError, 'Key path value is out of range.' if path.to_i >= Bitcoin::HARDENED_THRESHOLD
      key = key.derive(path.to_i)
    end
  end
  key
end