module Tapyrus::KeyPath

Public Instance Methods

parse_key_path(path_string) click to toggle source

key path convert an array of derive number @param [String] path_string @return [Array] key path numbers.

# File lib/tapyrus/key_path.rb, line 6
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 + Tapyrus::HARDENED_THRESHOLD : p.to_i)
    end[
    1..-1
  ]
end
to_key_path(numbers) click to toggle source

key path numbers convert to path string. @param [Array] key path numbers. @return [String] path string.

# File lib/tapyrus/key_path.rb, line 25
def to_key_path(numbers)
  "m/#{numbers.map { |p| p >= Tapyrus::HARDENED_THRESHOLD ? "#{p - Tapyrus::HARDENED_THRESHOLD}'" : p.to_s }.join('/')}"
end