Class: Tapyrus::ExtKey
- Inherits:
-
Object
- Object
- Tapyrus::ExtKey
- Includes:
- HexConverter
- Defined in:
- lib/tapyrus/ext_key.rb
Overview
BIP32 Extended private key
Constant Summary collapse
- MAX_DEPTH =
255
- MASTER_FINGERPRINT =
"00000000"
Instance Attribute Summary collapse
-
#chain_code ⇒ Object
Returns the value of attribute chain_code.
-
#depth ⇒ Object
Returns the value of attribute depth.
-
#key ⇒ Object
Tapyrus::Key.
-
#number ⇒ Object
Returns the value of attribute number.
-
#parent_fingerprint ⇒ Object
Returns the value of attribute parent_fingerprint.
-
#ver ⇒ Object
Returns the value of attribute ver.
Class Method Summary collapse
-
.from_base58(address) ⇒ Object
import private key from Base58 private key address.
-
.generate_master(seed) ⇒ Object
generate master key from seed.
- .parse_from_payload(payload) ⇒ Object
-
.support_version?(version) ⇒ Boolean
check whether
version
is supported version bytes. -
.version_from_purpose(purpose) ⇒ Object
get version bytes from purpose’ value.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#addr ⇒ Object
get address.
-
#derive(number, harden = false) ⇒ Tapyrus::ExtKey
derive new key.
-
#ext_pubkey ⇒ Object
get ExtPubkey from priv_key.
-
#fingerprint ⇒ Object
get fingerprint.
-
#hardened? ⇒ Boolean
whether hardened key.
- #hash160 ⇒ Object
-
#identifier ⇒ Object
get key identifier.
-
#key_type ⇒ Object
get key type defined by BIP-178 using version.
- #master? ⇒ Boolean
-
#priv ⇒ Object
get private key(hex).
-
#priv_ver_to_pub_ver ⇒ Object
convert privkey version to pubkey version.
-
#pub ⇒ Object
get public key(hex).
-
#to_base58 ⇒ Object
Base58 encoded extended private key.
-
#to_payload ⇒ Object
serialize extended private key.
-
#version ⇒ Object
get version bytes using serialization format.
Methods included from HexConverter
Instance Attribute Details
#chain_code ⇒ Object
Returns the value of attribute chain_code.
15 16 17 |
# File 'lib/tapyrus/ext_key.rb', line 15 def chain_code @chain_code end |
#depth ⇒ Object
Returns the value of attribute depth.
13 14 15 |
# File 'lib/tapyrus/ext_key.rb', line 13 def depth @depth end |
#key ⇒ Object
Tapyrus::Key
16 17 18 |
# File 'lib/tapyrus/ext_key.rb', line 16 def key @key end |
#number ⇒ Object
Returns the value of attribute number.
14 15 16 |
# File 'lib/tapyrus/ext_key.rb', line 14 def number @number end |
#parent_fingerprint ⇒ Object
Returns the value of attribute parent_fingerprint.
17 18 19 |
# File 'lib/tapyrus/ext_key.rb', line 17 def parent_fingerprint @parent_fingerprint end |
#ver ⇒ Object
Returns the value of attribute ver.
12 13 14 |
# File 'lib/tapyrus/ext_key.rb', line 12 def ver @ver end |
Class Method Details
.from_base58(address) ⇒ Object
import private key from Base58 private key address
165 166 167 |
# File 'lib/tapyrus/ext_key.rb', line 165 def self.from_base58(address) ExtKey.parse_from_payload(ExtPubkey.validate_checksum(address)) end |
.generate_master(seed) ⇒ Object
generate master key from seed.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/tapyrus/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 = "00000000" l = Tapyrus.hmac_sha512("Tapyrus seed", seed.htb) left = l[0..31].bth.to_i(16) raise "invalid key" if left >= CURVE_ORDER || left == 0 l_priv = ECDSA::Format::IntegerOctetString.encode(left, 32) ext_key.key = Tapyrus::Key.new(priv_key: l_priv.bth, key_type: Tapyrus::Key::TYPES[:compressed]) ext_key.chain_code = l[32..-1] ext_key end |
.parse_from_payload(payload) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/tapyrus/ext_key.rb', line 141 def self.parse_from_payload(payload) buf = StringIO.new(payload) ext_key = ExtKey.new ext_key.ver = buf.read(4).bth # version raise ArgumentError, Errors::Messages::INVALID_BIP32_VERSION unless ExtKey.support_version?(ext_key.ver) ext_key.depth = buf.read(1).unpack("C").first ext_key.parent_fingerprint = buf.read(4).bth ext_key.number = buf.read(4).unpack("N").first if ext_key.depth == 0 unless ext_key.parent_fingerprint == ExtKey::MASTER_FINGERPRINT raise ArgumentError, Errors::Messages::INVALID_BIP32_FINGERPRINT end raise ArgumentError, Errors::Messages::INVALID_BIP32_ZERO_INDEX if ext_key.number > 0 end if ext_key.parent_fingerprint == ExtKey::MASTER_FINGERPRINT && ext_key.depth > 0 raise ArgumentError, Errors::Messages::INVALID_BIP32_ZERO_DEPTH end ext_key.chain_code = buf.read(32) raise ArgumentError, Errors::Messages::INVALID_BIP32_PRIV_PREFIX unless buf.read(1).bth == "00" # 0x00 ext_key.key = Tapyrus::Key.new(priv_key: buf.read(32).bth, key_type: Tapyrus::Key::TYPES[:compressed]) ext_key end |
.support_version?(version) ⇒ Boolean
check whether version
is supported version bytes.
183 184 185 186 187 188 |
# File 'lib/tapyrus/ext_key.rb', line 183 def self.support_version?(version) p = Tapyrus.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.
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/tapyrus/ext_key.rb', line 170 def self.version_from_purpose(purpose) v = purpose - Tapyrus::HARDENED_THRESHOLD case v when 49 Tapyrus.chain_params.bip49_privkey_p2wpkh_p2sh_version when 84 Tapyrus.chain_params.bip84_privkey_p2wpkh_version else Tapyrus.chain_params.extended_privkey_version end end |
Instance Method Details
#==(other) ⇒ Object
137 138 139 |
# File 'lib/tapyrus/ext_key.rb', line 137 def ==(other) to_payload == other.to_payload end |
#addr ⇒ Object
get address
72 73 74 |
# File 'lib/tapyrus/ext_key.rb', line 72 def addr ext_pubkey.addr end |
#derive(number, harden = false) ⇒ Tapyrus::ExtKey
derive new key
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/tapyrus/ext_key.rb', line 95 def derive(number, harden = false) number += Tapyrus::HARDENED_THRESHOLD if harden new_key = ExtKey.new new_key.depth = depth + 1 new_key.number = number new_key.parent_fingerprint = fingerprint if number > (Tapyrus::HARDENED_THRESHOLD - 1) data = [0x00].pack("C") << key.priv_key.htb << [number].pack("N") else data = key.pubkey.htb << [number].pack("N") end l = Tapyrus.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 child_priv = ECDSA::Format::IntegerOctetString.encode(child_priv, 32) new_key.key = Tapyrus::Key.new(priv_key: child_priv.bth, key_type: key_type) new_key.chain_code = l[32..-1] new_key.ver = version new_key end |
#ext_pubkey ⇒ Object
get ExtPubkey from priv_key
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/tapyrus/ext_key.rb', line 35 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 |
#fingerprint ⇒ Object
get fingerprint
82 83 84 |
# File 'lib/tapyrus/ext_key.rb', line 82 def fingerprint identifier.slice(0..7) end |
#hardened? ⇒ Boolean
whether hardened key.
87 88 89 |
# File 'lib/tapyrus/ext_key.rb', line 87 def hardened? number >= Tapyrus::HARDENED_THRESHOLD end |
#hash160 ⇒ Object
67 68 69 |
# File 'lib/tapyrus/ext_key.rb', line 67 def hash160 Tapyrus.hash160(pub) end |
#identifier ⇒ Object
get key identifier
77 78 79 |
# File 'lib/tapyrus/ext_key.rb', line 77 def identifier Tapyrus.hash160(key.pubkey) end |
#key_type ⇒ Object
get key type defined by BIP-178 using version.
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/tapyrus/ext_key.rb', line 125 def key_type v = version case v when Tapyrus.chain_params.bip49_privkey_p2wpkh_p2sh_version Tapyrus::Key::TYPES[:p2wpkh_p2sh] when Tapyrus.chain_params.bip84_privkey_p2wpkh_version Tapyrus::Key::TYPES[:p2wpkh] when Tapyrus.chain_params.extended_privkey_version Tapyrus::Key::TYPES[:compressed] end end |
#master? ⇒ Boolean
202 203 204 |
# File 'lib/tapyrus/ext_key.rb', line 202 def master? depth == 0 && number == 0 && parent_fingerprint == "00000000" end |
#priv ⇒ Object
get private key(hex)
58 59 60 |
# File 'lib/tapyrus/ext_key.rb', line 58 def priv key.priv_key end |
#priv_ver_to_pub_ver ⇒ Object
convert privkey version to pubkey version
191 192 193 194 195 196 197 198 199 200 |
# File 'lib/tapyrus/ext_key.rb', line 191 def priv_ver_to_pub_ver case version when Tapyrus.chain_params.bip49_privkey_p2wpkh_p2sh_version Tapyrus.chain_params.bip49_pubkey_p2wpkh_p2sh_version when Tapyrus.chain_params.bip84_privkey_p2wpkh_version Tapyrus.chain_params.bip84_pubkey_p2wpkh_version else Tapyrus.chain_params.extended_pubkey_version end end |
#pub ⇒ Object
get public key(hex)
63 64 65 |
# File 'lib/tapyrus/ext_key.rb', line 63 def pub key.pubkey end |
#to_base58 ⇒ Object
Base58 encoded extended private key
53 54 55 |
# File 'lib/tapyrus/ext_key.rb', line 53 def to_base58 ExtPubkey.encode_base58(to_hex) end |
#to_payload ⇒ Object
serialize extended private key
47 48 49 50 |
# File 'lib/tapyrus/ext_key.rb', line 47 def to_payload version.htb << [depth].pack("C") << parent_fingerprint.htb << [number].pack("N") << chain_code << [0x00].pack("C") << key.priv_key.htb end |
#version ⇒ Object
get version bytes using serialization format
119 120 121 122 |
# File 'lib/tapyrus/ext_key.rb', line 119 def version return ExtKey.version_from_purpose(number) if depth == 1 ver ? ver : Tapyrus.chain_params.extended_privkey_version end |