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.



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

def chain_code
  @chain_code
end

#depthObject

Returns the value of attribute depth.



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

def depth
  @depth
end

#numberObject

Returns the value of attribute number.



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

def number
  @number
end

#parent_fingerprintObject

Returns the value of attribute parent_fingerprint.



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

def parent_fingerprint
  @parent_fingerprint
end

#pubkeyObject

hex format



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

def pubkey
  @pubkey
end

#verObject

Returns the value of attribute ver.



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

def ver
  @ver
end

Class Method Details

.from_base58(address) ⇒ Object

import pub key from Base58 private key address



312
313
314
# File 'lib/bitcoin/ext_key.rb', line 312

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

.parse_from_payload(payload) ⇒ Object



297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/bitcoin/ext_key.rb', line 297

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)


330
331
332
333
# File 'lib/bitcoin/ext_key.rb', line 330

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.



317
318
319
320
321
322
323
324
325
326
327
# File 'lib/bitcoin/ext_key.rb', line 317

def self.version_from_purpose(purpose)
  v = purpose - Bitcoin::HARDENED_THRESHOLD
  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

#==(other) ⇒ Object



293
294
295
# File 'lib/bitcoin/ext_key.rb', line 293

def ==(other)
  to_payload == other.to_payload
end

#addrObject

get address



216
217
218
219
220
221
222
223
224
225
# File 'lib/bitcoin/ext_key.rb', line 216

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



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/bitcoin/ext_key.rb', line 256

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 > (Bitcoin::HARDENED_THRESHOLD - 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



239
240
241
# File 'lib/bitcoin/ext_key.rb', line 239

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

#hardened?Boolean

whether hardened key.

Returns:

  • (Boolean)


251
252
253
# File 'lib/bitcoin/ext_key.rb', line 251

def hardened?
  number >= Bitcoin::HARDENED_THRESHOLD
end

#hash160Object



211
212
213
# File 'lib/bitcoin/ext_key.rb', line 211

def hash160
  Bitcoin.hash160(pub)
end

#identifierObject

get key identifier



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

def identifier
  Bitcoin.hash160(pub)
end

#keyBitcoin::Key

get key object

Returns:



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

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

#key_typeObject

get key type defined by BIP-178 using version.



281
282
283
284
285
286
287
288
289
290
291
# File 'lib/bitcoin/ext_key.rb', line 281

def key_type
  v = version
  case v
  when Bitcoin.chain_params.bip49_pubkey_p2wpkh_p2sh_version
    Bitcoin::Key::TYPES[:p2wpkh_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



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

def pub
  pubkey
end

#to_base58Object

Base58 encoded extended pubkey



244
245
246
247
248
# File 'lib/bitcoin/ext_key.rb', line 244

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

#to_payloadObject

serialize extended pubkey



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

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



275
276
277
278
# File 'lib/bitcoin/ext_key.rb', line 275

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