Class: Bitcoin::ExtPubkey
- Inherits:
-
Object
- Object
- Bitcoin::ExtPubkey
- Defined in:
- lib/bitcoin/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).
-
#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.
124 125 126 |
# File 'lib/bitcoin/ext_key.rb', line 124 def chain_code @chain_code end |
#depth ⇒ Object
Returns the value of attribute depth.
122 123 124 |
# File 'lib/bitcoin/ext_key.rb', line 122 def depth @depth end |
#number ⇒ Object
Returns the value of attribute number.
123 124 125 |
# File 'lib/bitcoin/ext_key.rb', line 123 def number @number end |
#parent_fingerprint ⇒ Object
Returns the value of attribute parent_fingerprint.
126 127 128 |
# File 'lib/bitcoin/ext_key.rb', line 126 def parent_fingerprint @parent_fingerprint end |
#pub_key ⇒ Object
Returns the value of attribute pub_key.
125 126 127 |
# File 'lib/bitcoin/ext_key.rb', line 125 def pub_key @pub_key end |
Class Method Details
.from_base58(address) ⇒ Object
import private key from Base58 private key address
178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/bitcoin/ext_key.rb', line 178 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
140 141 142 |
# File 'lib/bitcoin/ext_key.rb', line 140 def addr Bitcoin.hash160_to_address(Bitcoin.hash160(pub)) end |
#derive(number) ⇒ Object
derive child key
162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/bitcoin/ext_key.rb', line 162 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
150 151 152 |
# File 'lib/bitcoin/ext_key.rb', line 150 def fingerprint identifier.slice(0..7) end |
#identifier ⇒ Object
get key identifier
145 146 147 |
# File 'lib/bitcoin/ext_key.rb', line 145 def identifier Bitcoin.hash160(pub) end |
#pub ⇒ Object
get public key(hex)
134 135 136 137 |
# File 'lib/bitcoin/ext_key.rb', line 134 def pub pub_key.group.point_conversion_form = :compressed pub_key.to_hex.rjust(66, '0') end |
#to_base58 ⇒ Object
Base58 encoded extended pubkey
155 156 157 158 159 |
# File 'lib/bitcoin/ext_key.rb', line 155 def to_base58 h = to_payload.bth hex = h + Bitcoin.checksum(h) Bitcoin.encode_base58(hex) end |