Class: Bitcoin::ExtPubkey

Inherits:
Object show all
Defined in:
lib/bitcoin/ext_key.rb

Overview

BIP-32 Extended public key

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#chain_codeObject

Returns the value of attribute chain_code.



193
194
195
# File 'lib/bitcoin/ext_key.rb', line 193

def chain_code
  @chain_code
end

#depthObject

Returns the value of attribute depth.



191
192
193
# File 'lib/bitcoin/ext_key.rb', line 191

def depth
  @depth
end

#numberObject

Returns the value of attribute number.



192
193
194
# File 'lib/bitcoin/ext_key.rb', line 192

def number
  @number
end

#parent_fingerprintObject

Returns the value of attribute parent_fingerprint.



195
196
197
# File 'lib/bitcoin/ext_key.rb', line 195

def parent_fingerprint
  @parent_fingerprint
end

#pubkeyObject

hex format



194
195
196
# File 'lib/bitcoin/ext_key.rb', line 194

def pubkey
  @pubkey
end

#verObject

Returns the value of attribute ver.



190
191
192
# File 'lib/bitcoin/ext_key.rb', line 190

def ver
  @ver
end

Class Method Details

.from_base58(address) ⇒ Object

import pub key from Base58 private key address



303
304
305
# File 'lib/bitcoin/ext_key.rb', line 303

def self.from_base58(address)
  ExtPubkey.parse_from_payload(Base58.decode(address).htb)
end

.parse_from_payload(payload) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/bitcoin/ext_key.rb', line 289

def self.parse_from_payload(payload)
  buf = StringIO.new(payload)
  ext_pubkey = ExtPubkey.new
  ext_pubkey.ver = buf.read(4).bth # version
  raise 'An unsupported version byte was specified.' 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
  ext_pubkey.chain_code = buf.read(32)
  ext_pubkey.pubkey = buf.read(33).bth
  ext_pubkey
end

.support_version?(version) ⇒ Boolean

check whether version is supported version bytes.

Returns:

  • (Boolean)


321
322
323
324
# File 'lib/bitcoin/ext_key.rb', line 321

def self.support_version?(version)
  p = Bitcoin.chain_params
  [p.bip49_pubkey_p2wpkh_p2sh_version, p.bip84_pubkey_p2wpkh_version, p.extended_pubkey_version].include?(version)
end

.version_from_purpose(purpose) ⇒ Object

get version bytes from purpose’ value.



308
309
310
311
312
313
314
315
316
317
318
# File 'lib/bitcoin/ext_key.rb', line 308

def self.version_from_purpose(purpose)
  v = purpose - 2**31
  case v
    when 49
      Bitcoin.chain_params.bip49_pubkey_p2wpkh_p2sh_version
    when 84
      Bitcoin.chain_params.bip84_pubkey_p2wpkh_version
    else
      Bitcoin.chain_params.extended_pubkey_version
  end
end

Instance Method Details

#addrObject

get address



212
213
214
215
216
217
218
219
220
221
# File 'lib/bitcoin/ext_key.rb', line 212

def addr
  case version
    when Bitcoin.chain_params.bip49_pubkey_p2wpkh_p2sh_version
      key.to_nested_p2wpkh
    when Bitcoin.chain_params.bip84_pubkey_p2wpkh_version
      key.to_p2wpkh
    else
      key.to_p2pkh
  end
end

#derive(number) ⇒ Object

derive child key



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/bitcoin/ext_key.rb', line 252

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 = l[0..31].bth.to_i(16)
  raise 'invalid key' if left >= CURVE_ORDER
  p1 = Bitcoin::Secp256k1::GROUP.generator.multiply_by_scalar(left)
  p2 = Bitcoin::Key.new(pubkey: pubkey, key_type: key_type).to_point
  new_key.pubkey = ECDSA::Format::PointOctetString.encode(p1 + p2, compression: true).bth
  new_key.chain_code = l[32..-1]
  new_key.ver = version
  new_key
end

#fingerprintObject

get fingerprint



235
236
237
# File 'lib/bitcoin/ext_key.rb', line 235

def fingerprint
  identifier.slice(0..7)
end

#hardened?Boolean

whether hardened key.

Returns:

  • (Boolean)


247
248
249
# File 'lib/bitcoin/ext_key.rb', line 247

def hardened?
  number >= 2**31
end

#hash160Object



207
208
209
# File 'lib/bitcoin/ext_key.rb', line 207

def hash160
  Bitcoin.hash160(pub)
end

#identifierObject

get key identifier



230
231
232
# File 'lib/bitcoin/ext_key.rb', line 230

def identifier
  Bitcoin.hash160(pub)
end

#keyBitcoin::Key

get key object

Returns:



225
226
227
# File 'lib/bitcoin/ext_key.rb', line 225

def key
  Bitcoin::Key.new(pubkey: pubkey, key_type: key_type)
end

#key_typeObject

get key type defined by BIP-178 using version.



277
278
279
280
281
282
283
284
285
286
287
# File 'lib/bitcoin/ext_key.rb', line 277

def key_type
  v = version
  case v
  when Bitcoin.chain_params.bip49_pubkey_p2wpkh_p2sh_version
    Bitcoin::Key::TYPES[:pw2pkh_p2sh]
  when Bitcoin.chain_params.bip84_pubkey_p2wpkh_version
    Bitcoin::Key::TYPES[:p2wpkh]
  when Bitcoin.chain_params.extended_pubkey_version
    Bitcoin::Key::TYPES[:compressed]
  end
end

#pubObject



203
204
205
# File 'lib/bitcoin/ext_key.rb', line 203

def pub
  pubkey
end

#to_base58Object

Base58 encoded extended pubkey



240
241
242
243
244
# File 'lib/bitcoin/ext_key.rb', line 240

def to_base58
  h = to_payload.bth
  hex = h + Bitcoin.calc_checksum(h)
  Base58.encode(hex)
end

#to_payloadObject

serialize extended pubkey



198
199
200
201
# File 'lib/bitcoin/ext_key.rb', line 198

def to_payload
  version.htb << [depth].pack('C') <<
      parent_fingerprint.htb << [number].pack('N') << chain_code << pub.htb
end

#versionObject

get version bytes using serialization format



271
272
273
274
# File 'lib/bitcoin/ext_key.rb', line 271

def version
  return ExtPubkey.version_from_purpose(number) if depth == 1
  ver ? ver : Bitcoin.chain_params.extended_pubkey_version
end