Class: Bitcoin::ExtPubkey
- Inherits:
-
Object
- Object
- Bitcoin::ExtPubkey
- Defined in:
- lib/hdkey/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.
-
#pub_key ⇒ Object
Returns the value of attribute pub_key.
Class Method Summary collapse
-
.from_base58(address) ⇒ Object
import private key from Base58 private key address.
Instance Method Summary collapse
-
#addr ⇒ Object
get address.
-
#derive(number) ⇒ Object
derive child key.
-
#fingerprint ⇒ Object
get fingerprint.
-
#identifier ⇒ Object
get key identifier.
-
#pub ⇒ Object
get public key(hex).
-
#segwit_addr ⇒ Object
get segwit p2wpkh address.
-
#to_base58 ⇒ Object
Base58 encoded extended pubkey.
-
#to_payload ⇒ Object
serialize extended pubkey.
Instance Attribute Details
#chain_code ⇒ Object
Returns the value of attribute chain_code.
129 130 131 |
# File 'lib/hdkey/ext_key.rb', line 129 def chain_code @chain_code end |
#depth ⇒ Object
Returns the value of attribute depth.
127 128 129 |
# File 'lib/hdkey/ext_key.rb', line 127 def depth @depth end |
#number ⇒ Object
Returns the value of attribute number.
128 129 130 |
# File 'lib/hdkey/ext_key.rb', line 128 def number @number end |
#parent_fingerprint ⇒ Object
Returns the value of attribute parent_fingerprint.
131 132 133 |
# File 'lib/hdkey/ext_key.rb', line 131 def parent_fingerprint @parent_fingerprint end |
#pub_key ⇒ Object
Returns the value of attribute pub_key.
130 131 132 |
# File 'lib/hdkey/ext_key.rb', line 130 def pub_key @pub_key end |
Class Method Details
.from_base58(address) ⇒ Object
import private key from Base58 private key address
193 194 195 196 197 198 199 200 201 202 203 |
# 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 |
Instance Method Details
#addr ⇒ Object
get address
145 146 147 |
# File 'lib/hdkey/ext_key.rb', line 145 def addr Bitcoin.hash160_to_address(Bitcoin.hash160(pub)) end |
#derive(number) ⇒ Object
derive child key
177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# 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 ⇒ Object
get fingerprint
165 166 167 |
# File 'lib/hdkey/ext_key.rb', line 165 def fingerprint identifier.slice(0..7) end |
#identifier ⇒ Object
get key identifier
160 161 162 |
# File 'lib/hdkey/ext_key.rb', line 160 def identifier Bitcoin.hash160(pub) end |
#pub ⇒ Object
get public key(hex)
139 140 141 142 |
# 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 ⇒ Object
get segwit p2wpkh address
150 151 152 153 154 155 156 157 |
# 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 ⇒ Object
Base58 encoded extended pubkey
170 171 172 173 174 |
# 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 ⇒ Object
serialize extended pubkey
134 135 136 |
# 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 |