class Bitcoin::ExtPubkey
BIP-32 Extended public key
Attributes
chain_code[RW]
depth[RW]
number[RW]
parent_fingerprint[RW]
pub_key[RW]
Public Class Methods
from_base58(address)
click to toggle source
import private key from Base58 private key address
# File lib/hdkey/ext_key.rb, line 193 def self.from_base58(address) data = StringIO.new(Bitcoin.decode_base58(address).htb) key = ExtPubkey.new data.read(4).bth # version key.depth = data.read(1).unpack('C').first key.parent_fingerprint = data.read(4).bth key.number = data.read(4).unpack('N').first key.chain_code = data.read(32) key.pub_key = OpenSSL::PKey::EC::Point.from_hex(Bitcoin.bitcoin_elliptic_curve.group, data.read(33).bth) key end
Public Instance Methods
addr()
click to toggle source
get address
# File lib/hdkey/ext_key.rb, line 145 def addr Bitcoin.hash160_to_address(Bitcoin.hash160(pub)) end
derive(number)
click to toggle source
derive child key
# File lib/hdkey/ext_key.rb, line 177 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 > (2**31 -1) data = pub.htb << [number].pack('N') l = Bitcoin.hmac_sha512(chain_code, data) left = OpenSSL::BN.from_hex(l[0..31].bth) raise 'invalid key' if left.to_i >= CURVE_ORDER new_key.pub_key = Bitcoin.bitcoin_elliptic_curve.group.generator.mul(left).ec_add(pub_key) new_key.chain_code = l[32..-1] new_key end
fingerprint()
click to toggle source
get fingerprint
# File lib/hdkey/ext_key.rb, line 165 def fingerprint identifier.slice(0..7) end
identifier()
click to toggle source
get key identifier
# File lib/hdkey/ext_key.rb, line 160 def identifier Bitcoin.hash160(pub) end
pub()
click to toggle source
get public key(hex)
# File lib/hdkey/ext_key.rb, line 139 def pub pub_key.group.point_conversion_form = :compressed pub_key.to_hex.rjust(66, '0') end
segwit_addr()
click to toggle source
get segwit p2wpkh address
# File lib/hdkey/ext_key.rb, line 150 def segwit_addr hash160 = Bitcoin.hash160(pub) p2wpkh = [ ["00", "14", hash160].join ].pack("H*").bth segwit_addr = Bech32::SegwitAddr.new segwit_addr.hrp = Bitcoin.network[:address_version] == '00' ? 'bc' : 'tb' segwit_addr.script_pubkey = p2wpkh segwit_addr.addr end
to_base58()
click to toggle source
Base58 encoded extended pubkey
# File lib/hdkey/ext_key.rb, line 170 def to_base58 h = to_payload.bth hex = h + Bitcoin.checksum(h) Bitcoin.encode_base58(hex) end
to_payload()
click to toggle source
serialize extended pubkey
# File lib/hdkey/ext_key.rb, line 134 def to_payload Bitcoin.network[:extended_pubkey_version].htb << [depth].pack('C') << parent_fingerprint.htb << [number].pack('N') << chain_code << pub.htb end