Class: Tapyrus::ExtPubkey
- Inherits:
-
Object
- Object
- Tapyrus::ExtPubkey
- Includes:
- HexConverter
- Defined in:
- lib/tapyrus/ext_key.rb
Overview
BIP-32 Extended public key
Instance Attribute Summary collapse
-
#chain_code ⇒ Object
Returns the value of attribute chain_code.
-
#depth ⇒ Object
Returns the value of attribute depth.
-
#number ⇒ Object
Returns the value of attribute number.
-
#parent_fingerprint ⇒ Object
Returns the value of attribute parent_fingerprint.
-
#pubkey ⇒ Object
hex format.
-
#ver ⇒ Object
Returns the value of attribute ver.
Class Method Summary collapse
-
.encode_base58(hex) ⇒ String
Generate Base58 encoded key from BIP32 payload with hex format.
-
.from_base58(address) ⇒ Object
import pub key from Base58 private key address.
- .parse_from_payload(payload) ⇒ Object
-
.support_version?(version) ⇒ Boolean
check whether
versionis supported version bytes. -
.validate_checksum(base58) ⇒ String
Validate address checksum and return payload.
-
.version_from_purpose(purpose) ⇒ Object
get version bytes from purpose' value.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#addr ⇒ Object
get address.
-
#derive(number) ⇒ Object
derive child key.
-
#fingerprint ⇒ Object
get fingerprint.
-
#hardened? ⇒ Boolean
whether hardened key.
- #hash160 ⇒ Object
-
#identifier ⇒ Object
get key identifier.
-
#key ⇒ Tapyrus::Key
get key object.
-
#key_type ⇒ Object
get key type defined by BIP-178 using version.
- #master? ⇒ Boolean
- #pub ⇒ Object
-
#to_base58 ⇒ Object
Base58 encoded extended pubkey.
-
#to_payload ⇒ Object
serialize extended pubkey.
-
#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.
216 217 218 |
# File 'lib/tapyrus/ext_key.rb', line 216 def chain_code @chain_code end |
#depth ⇒ Object
Returns the value of attribute depth.
214 215 216 |
# File 'lib/tapyrus/ext_key.rb', line 214 def depth @depth end |
#number ⇒ Object
Returns the value of attribute number.
215 216 217 |
# File 'lib/tapyrus/ext_key.rb', line 215 def number @number end |
#parent_fingerprint ⇒ Object
Returns the value of attribute parent_fingerprint.
218 219 220 |
# File 'lib/tapyrus/ext_key.rb', line 218 def parent_fingerprint @parent_fingerprint end |
#pubkey ⇒ Object
hex format
217 218 219 |
# File 'lib/tapyrus/ext_key.rb', line 217 def pubkey @pubkey end |
#ver ⇒ Object
Returns the value of attribute ver.
213 214 215 |
# File 'lib/tapyrus/ext_key.rb', line 213 def ver @ver end |
Class Method Details
.encode_base58(hex) ⇒ String
Generate Base58 encoded key from BIP32 payload with hex format.
262 263 264 |
# File 'lib/tapyrus/ext_key.rb', line 262 def self.encode_base58(hex) Base58.encode(hex + Tapyrus.calc_checksum(hex)) end |
.from_base58(address) ⇒ Object
import pub key from Base58 private key address
339 340 341 |
# File 'lib/tapyrus/ext_key.rb', line 339 def self.from_base58(address) ExtPubkey.parse_from_payload(ExtPubkey.validate_checksum(address)) end |
.parse_from_payload(payload) ⇒ Object
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/tapyrus/ext_key.rb', line 316 def self.parse_from_payload(payload) buf = StringIO.new(payload) ext_pubkey = ExtPubkey.new ext_pubkey.ver = buf.read(4).bth # version raise ArgumentError, Errors::Messages::INVALID_BIP32_VERSION unless ExtPubkey.support_version?(ext_pubkey.ver) ext_pubkey.depth = buf.read(1).unpack("C").first ext_pubkey.parent_fingerprint = buf.read(4).bth ext_pubkey.number = buf.read(4).unpack("N").first if ext_pubkey.depth == 0 unless ext_pubkey.parent_fingerprint == ExtKey::MASTER_FINGERPRINT raise ArgumentError, Errors::Messages::INVALID_BIP32_FINGERPRINT end raise ArgumentError, Errors::Messages::INVALID_BIP32_ZERO_INDEX if ext_pubkey.number > 0 end if ext_pubkey.parent_fingerprint == ExtKey::MASTER_FINGERPRINT && ext_pubkey.depth > 0 raise ArgumentError, Errors::Messages::INVALID_BIP32_ZERO_DEPTH end ext_pubkey.chain_code = buf.read(32) ext_pubkey.pubkey = Tapyrus::Key.new(pubkey: buf.read(33).bth).pubkey ext_pubkey end |
.support_version?(version) ⇒ Boolean
check whether version is supported version bytes.
366 367 368 369 |
# File 'lib/tapyrus/ext_key.rb', line 366 def self.support_version?(version) p = Tapyrus.chain_params [p.bip49_pubkey_p2wpkh_p2sh_version, p.bip84_pubkey_p2wpkh_version, p.extended_pubkey_version].include?(version) end |
.validate_checksum(base58) ⇒ String
Validate address checksum and return payload.
346 347 348 349 350 |
# File 'lib/tapyrus/ext_key.rb', line 346 def self.validate_checksum(base58) raw = Base58.decode(base58) raise ArgumentError, Errors::Messages::INVALID_CHECKSUM unless Tapyrus.calc_checksum(raw[0...-8]) == raw[-8..-1] raw[0...-8].htb end |
.version_from_purpose(purpose) ⇒ Object
get version bytes from purpose' value.
353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/tapyrus/ext_key.rb', line 353 def self.version_from_purpose(purpose) v = purpose - Tapyrus::HARDENED_THRESHOLD case v when 49 Tapyrus.chain_params.bip49_pubkey_p2wpkh_p2sh_version when 84 Tapyrus.chain_params.bip84_pubkey_p2wpkh_version else Tapyrus.chain_params.extended_pubkey_version end end |
Instance Method Details
#==(other) ⇒ Object
312 313 314 |
# File 'lib/tapyrus/ext_key.rb', line 312 def ==(other) to_payload == other.to_payload end |
#addr ⇒ Object
get address
234 235 236 |
# File 'lib/tapyrus/ext_key.rb', line 234 def addr key.to_p2pkh end |
#derive(number) ⇒ Object
derive child key
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/tapyrus/ext_key.rb', line 272 def derive(number) new_key = ExtPubkey.new new_key.depth = depth + 1 new_key.number = number new_key.parent_fingerprint = fingerprint raise "hardened key is not support" if number > (Tapyrus::HARDENED_THRESHOLD - 1) data = pub.htb << [number].pack("N") l = Tapyrus.hmac_sha512(chain_code, data) left = l[0..31].bth.to_i(16) raise "invalid key" if left >= CURVE_ORDER l_priv = ECDSA::Format::IntegerOctetString.encode(left, 32) p1 = Tapyrus::Key.new(priv_key: l_priv.bth, key_type: Tapyrus::Key::TYPES[:uncompressed]).to_point p2 = Tapyrus::Key.new(pubkey: pubkey, key_type: key_type).to_point new_key.pubkey = (p1 + p2).to_hex new_key.chain_code = l[32..-1] # A depth 1 key derives its version bytes from the purpose, see #version. new_key.ver = new_key.depth == 1 ? nil : version new_key end |
#fingerprint ⇒ Object
get fingerprint
250 251 252 |
# File 'lib/tapyrus/ext_key.rb', line 250 def fingerprint identifier.slice(0..7) end |
#hardened? ⇒ Boolean
whether hardened key.
267 268 269 |
# File 'lib/tapyrus/ext_key.rb', line 267 def hardened? number >= Tapyrus::HARDENED_THRESHOLD end |
#hash160 ⇒ Object
229 230 231 |
# File 'lib/tapyrus/ext_key.rb', line 229 def hash160 Tapyrus.hash160(pub) end |
#identifier ⇒ Object
get key identifier
245 246 247 |
# File 'lib/tapyrus/ext_key.rb', line 245 def identifier Tapyrus.hash160(pub) end |
#key ⇒ Tapyrus::Key
get key object
240 241 242 |
# File 'lib/tapyrus/ext_key.rb', line 240 def key Tapyrus::Key.new(pubkey: pubkey, key_type: key_type) end |
#key_type ⇒ Object
get key type defined by BIP-178 using version.
300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/tapyrus/ext_key.rb', line 300 def key_type v = version case v when Tapyrus.chain_params.bip49_pubkey_p2wpkh_p2sh_version Tapyrus::Key::TYPES[:p2wpkh_p2sh] when Tapyrus.chain_params.bip84_pubkey_p2wpkh_version Tapyrus::Key::TYPES[:p2wpkh] when Tapyrus.chain_params.extended_pubkey_version Tapyrus::Key::TYPES[:compressed] end end |
#master? ⇒ Boolean
371 372 373 |
# File 'lib/tapyrus/ext_key.rb', line 371 def master? depth == 0 && number == 0 && parent_fingerprint == "00000000" end |
#pub ⇒ Object
225 226 227 |
# File 'lib/tapyrus/ext_key.rb', line 225 def pub pubkey end |
#to_base58 ⇒ Object
Base58 encoded extended pubkey
255 256 257 |
# File 'lib/tapyrus/ext_key.rb', line 255 def to_base58 ExtPubkey.encode_base58(to_hex) end |
#to_payload ⇒ Object
serialize extended pubkey
221 222 223 |
# File 'lib/tapyrus/ext_key.rb', line 221 def to_payload version.htb << [depth].pack("C") << parent_fingerprint.htb << [number].pack("N") << chain_code << pub.htb end |
#version ⇒ Object
get version bytes using serialization format
293 294 295 296 297 |
# File 'lib/tapyrus/ext_key.rb', line 293 def version return ver if ver return ExtPubkey.version_from_purpose(number) if depth == 1 Tapyrus.chain_params.extended_pubkey_version end |