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.



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

def chain_code
  @chain_code
end

#depthObject

Returns the value of attribute depth.



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

def depth
  @depth
end

#numberObject

Returns the value of attribute number.



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

def number
  @number
end

#parent_fingerprintObject

Returns the value of attribute parent_fingerprint.



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

def parent_fingerprint
  @parent_fingerprint
end

#pubkeyObject

hex format



205
206
207
# File 'lib/bitcoin/ext_key.rb', line 205

def pubkey
  @pubkey
end

#verObject

Returns the value of attribute ver.



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

def ver
  @ver
end

Class Method Details

.from_base58(address) ⇒ Object

import pub key from Base58 private key address



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

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

.parse_from_payload(payload) ⇒ Object



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

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
  if ext_pubkey.depth == 0
    raise ArgumentError, 'Invalid parent fingerprint.' unless ext_pubkey.parent_fingerprint == ExtKey::MASTER_FINGERPRINT
  end
  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)


341
342
343
344
# File 'lib/bitcoin/ext_key.rb', line 341

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.



328
329
330
331
332
333
334
335
336
337
338
# File 'lib/bitcoin/ext_key.rb', line 328

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



301
302
303
# File 'lib/bitcoin/ext_key.rb', line 301

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

#addrObject

get address



223
224
225
226
227
228
229
230
231
232
# File 'lib/bitcoin/ext_key.rb', line 223

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

Raises:

  • (IndexError)


263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/bitcoin/ext_key.rb', line 263

def derive(number)
  new_key = ExtPubkey.new
  new_key.depth = depth + 1
  raise IndexError, 'Depth over 255.' if new_key.depth > Bitcoin::ExtKey::MAX_DEPTH
  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



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

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

#hardened?Boolean

whether hardened key.

Returns:

  • (Boolean)


258
259
260
# File 'lib/bitcoin/ext_key.rb', line 258

def hardened?
  number >= Bitcoin::HARDENED_THRESHOLD
end

#hash160Object



218
219
220
# File 'lib/bitcoin/ext_key.rb', line 218

def hash160
  Bitcoin.hash160(pub)
end

#identifierObject

get key identifier



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

def identifier
  Bitcoin.hash160(pub)
end

#keyBitcoin::Key

get key object

Returns:



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

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

#key_typeObject

get key type defined by BIP-178 using version.



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

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



214
215
216
# File 'lib/bitcoin/ext_key.rb', line 214

def pub
  pubkey
end

#to_base58Object

Base58 encoded extended pubkey



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

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

#to_payloadObject

serialize extended pubkey



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

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



283
284
285
286
# File 'lib/bitcoin/ext_key.rb', line 283

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