Class: Bitcoin::ExtKey

Inherits:
Object show all
Defined in:
lib/bitcoin/ext_key.rb

Overview

BIP32 Extended private key

Constant Summary collapse

MAX_DEPTH =
255
MASTER_FINGERPRINT =
'00000000'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#chain_codeObject

Returns the value of attribute chain_code.



15
16
17
# File 'lib/bitcoin/ext_key.rb', line 15

def chain_code
  @chain_code
end

#depthObject

Returns the value of attribute depth.



13
14
15
# File 'lib/bitcoin/ext_key.rb', line 13

def depth
  @depth
end

#keyObject

Bitcoin::Key



16
17
18
# File 'lib/bitcoin/ext_key.rb', line 16

def key
  @key
end

#numberObject

Returns the value of attribute number.



14
15
16
# File 'lib/bitcoin/ext_key.rb', line 14

def number
  @number
end

#parent_fingerprintObject

Returns the value of attribute parent_fingerprint.



17
18
19
# File 'lib/bitcoin/ext_key.rb', line 17

def parent_fingerprint
  @parent_fingerprint
end

#verObject

Returns the value of attribute ver.



12
13
14
# File 'lib/bitcoin/ext_key.rb', line 12

def ver
  @ver
end

Class Method Details

.from_base58(address) ⇒ Object

import private key from Base58 private key address



161
162
163
# File 'lib/bitcoin/ext_key.rb', line 161

def self.from_base58(address)
  ExtKey.parse_from_payload(Base58.decode(address).htb)
end

.generate_master(seed) ⇒ Object

generate master key from seed.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bitcoin/ext_key.rb', line 21

def self.generate_master(seed)
  ext_key = ExtKey.new
  ext_key.depth = ext_key.number = 0
  ext_key.parent_fingerprint = MASTER_FINGERPRINT
  l = Bitcoin.hmac_sha512('Bitcoin seed', seed.htb)
  left = l[0..31].bth.to_i(16)
  raise 'invalid key' if left >= CURVE_ORDER || left == 0
  ext_key.key = Bitcoin::Key.new(priv_key: l[0..31].bth, key_type: Bitcoin::Key::TYPES[:compressed])
  ext_key.chain_code = l[32..-1]
  ext_key
end

.parse_from_payload(payload) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/bitcoin/ext_key.rb', line 143

def self.parse_from_payload(payload)
  buf = StringIO.new(payload)
  ext_key = ExtKey.new
  ext_key.ver = buf.read(4).bth # version
  raise 'An unsupported version byte was specified.' unless ExtKey.support_version?(ext_key.ver)
  ext_key.depth = buf.read(1).unpack('C').first
  ext_key.parent_fingerprint = buf.read(4).bth
  if ext_key.depth == 0
    raise ArgumentError, 'Invalid parent fingerprint.' unless ext_key.parent_fingerprint == MASTER_FINGERPRINT
  end
  ext_key.number = buf.read(4).unpack('N').first
  ext_key.chain_code = buf.read(32)
  buf.read(1) # 0x00
  ext_key.key = Bitcoin::Key.new(priv_key: buf.read(32).bth, key_type: Bitcoin::Key::TYPES[:compressed])
  ext_key
end

.support_version?(version) ⇒ Boolean

check whether version is supported version bytes.



179
180
181
182
# File 'lib/bitcoin/ext_key.rb', line 179

def self.support_version?(version)
  p = Bitcoin.chain_params
  [p.bip49_privkey_p2wpkh_p2sh_version, p.bip84_privkey_p2wpkh_version, p.extended_privkey_version].include?(version)
end

.version_from_purpose(purpose) ⇒ Object

get version bytes from purpose’ value.



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/bitcoin/ext_key.rb', line 166

def self.version_from_purpose(purpose)
  v = purpose - Bitcoin::HARDENED_THRESHOLD
  case v
    when 49
      Bitcoin.chain_params.bip49_privkey_p2wpkh_p2sh_version
    when 84
      Bitcoin.chain_params.bip84_privkey_p2wpkh_version
    else
      Bitcoin.chain_params.extended_privkey_version
  end
end

Instance Method Details

#==(other) ⇒ Object



139
140
141
# File 'lib/bitcoin/ext_key.rb', line 139

def ==(other)
  to_payload == other.to_payload
end

#addrObject

get address



73
74
75
# File 'lib/bitcoin/ext_key.rb', line 73

def addr
  ext_pubkey.addr
end

#derive(number, harden = false) ⇒ Bitcoin::ExtKey

derive new key

Raises:

  • (IndexError)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/bitcoin/ext_key.rb', line 96

def derive(number, harden = false)
  number += Bitcoin::HARDENED_THRESHOLD if harden
  new_key = ExtKey.new
  new_key.depth = depth + 1
  raise IndexError, 'Depth over 255.' if new_key.depth > MAX_DEPTH
  new_key.number = number
  new_key.parent_fingerprint = fingerprint
  if number > (Bitcoin::HARDENED_THRESHOLD - 1)
    data = [0x00].pack('C') << key.priv_key.htb << [number].pack('N')
  else
    data = key.pubkey.htb << [number].pack('N')
  end
  l = Bitcoin.hmac_sha512(chain_code, data)
  left = l[0..31].bth.to_i(16)
  raise 'invalid key' if left >= CURVE_ORDER
  child_priv = (left + key.priv_key.to_i(16)) % CURVE_ORDER
  raise 'invalid key ' if child_priv >= CURVE_ORDER
  new_key.key = Bitcoin::Key.new(
      priv_key: child_priv.to_even_length_hex.rjust(64, '0'), key_type: key_type)
  new_key.chain_code = l[32..-1]
  new_key.ver = version
  new_key
end

#ext_pubkeyObject

get ExtPubkey from priv_key



34
35
36
37
38
39
40
41
42
43
# File 'lib/bitcoin/ext_key.rb', line 34

def ext_pubkey
  k = ExtPubkey.new
  k.depth = depth
  k.number = number
  k.parent_fingerprint = parent_fingerprint
  k.chain_code = chain_code
  k.pubkey = key.pubkey
  k.ver = priv_ver_to_pub_ver
  k
end

#fingerprintObject

get fingerprint



83
84
85
# File 'lib/bitcoin/ext_key.rb', line 83

def fingerprint
  identifier.slice(0..7)
end

#hardened?Boolean

whether hardened key.



88
89
90
# File 'lib/bitcoin/ext_key.rb', line 88

def hardened?
  number >= Bitcoin::HARDENED_THRESHOLD
end

#hash160Object



68
69
70
# File 'lib/bitcoin/ext_key.rb', line 68

def hash160
  Bitcoin.hash160(pub)
end

#identifierObject

get key identifier



78
79
80
# File 'lib/bitcoin/ext_key.rb', line 78

def identifier
  Bitcoin.hash160(key.pubkey)
end

#key_typeObject

get key type defined by BIP-178 using version.



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/bitcoin/ext_key.rb', line 127

def key_type
  v = version
  case v
  when Bitcoin.chain_params.bip49_privkey_p2wpkh_p2sh_version
    Bitcoin::Key::TYPES[:p2wpkh_p2sh]
  when Bitcoin.chain_params.bip84_privkey_p2wpkh_version
    Bitcoin::Key::TYPES[:p2wpkh]
  when Bitcoin.chain_params.extended_privkey_version
    Bitcoin::Key::TYPES[:compressed]
  end
end

#privObject

get private key(hex)



59
60
61
# File 'lib/bitcoin/ext_key.rb', line 59

def priv
  key.priv_key
end

#priv_ver_to_pub_verObject

convert privkey version to pubkey version



185
186
187
188
189
190
191
192
193
194
# File 'lib/bitcoin/ext_key.rb', line 185

def priv_ver_to_pub_ver
  case version
    when Bitcoin.chain_params.bip49_privkey_p2wpkh_p2sh_version
      Bitcoin.chain_params.bip49_pubkey_p2wpkh_p2sh_version
    when Bitcoin.chain_params.bip84_privkey_p2wpkh_version
      Bitcoin.chain_params.bip84_pubkey_p2wpkh_version
    else
      Bitcoin.chain_params.extended_pubkey_version
  end
end

#pubObject

get public key(hex)



64
65
66
# File 'lib/bitcoin/ext_key.rb', line 64

def pub
  key.pubkey
end

#to_base58Object

Base58 encoded extended private key



52
53
54
55
56
# File 'lib/bitcoin/ext_key.rb', line 52

def to_base58
  h = to_payload.bth
  hex = h + Bitcoin.calc_checksum(h)
  Base58.encode(hex)
end

#to_payloadObject

serialize extended private key



46
47
48
49
# File 'lib/bitcoin/ext_key.rb', line 46

def to_payload
  version.htb << [depth].pack('C') << parent_fingerprint.htb <<
      [number].pack('N') << chain_code << [0x00].pack('C') << key.priv_key.htb
end

#versionObject

get version bytes using serialization format



121
122
123
124
# File 'lib/bitcoin/ext_key.rb', line 121

def version
  return ExtKey.version_from_purpose(number) if depth == 1
  ver ? ver : Bitcoin.chain_params.extended_privkey_version
end