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.



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

def chain_code
  @chain_code
end

#depthObject

Returns the value of attribute depth.



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

def depth
  @depth
end

#numberObject

Returns the value of attribute number.



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

def number
  @number
end

#parent_fingerprintObject

Returns the value of attribute parent_fingerprint.



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

def parent_fingerprint
  @parent_fingerprint
end

#pubkeyObject

hex format



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

def pubkey
  @pubkey
end

#verObject

Returns the value of attribute ver.



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

def ver
  @ver
end

Class Method Details

.from_base58(address) ⇒ Object

import pub key from Base58 private key address



305
306
307
# File 'lib/bitcoin/ext_key.rb', line 305

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

.parse_from_payload(payload) ⇒ Object



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

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)


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

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.



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

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

#addrObject

get address



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

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



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

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 > (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



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

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

#hardened?Boolean

whether hardened key.

Returns:

  • (Boolean)


249
250
251
# File 'lib/bitcoin/ext_key.rb', line 249

def hardened?
  number >= HARDENED_THRESHOLD
end

#hash160Object



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

def hash160
  Bitcoin.hash160(pub)
end

#identifierObject

get key identifier



232
233
234
# File 'lib/bitcoin/ext_key.rb', line 232

def identifier
  Bitcoin.hash160(pub)
end

#keyBitcoin::Key

get key object

Returns:



227
228
229
# File 'lib/bitcoin/ext_key.rb', line 227

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

#key_typeObject

get key type defined by BIP-178 using version.



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

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



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

def pub
  pubkey
end

#to_base58Object

Base58 encoded extended pubkey



242
243
244
245
246
# File 'lib/bitcoin/ext_key.rb', line 242

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

#to_payloadObject

serialize extended pubkey



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

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



273
274
275
276
# File 'lib/bitcoin/ext_key.rb', line 273

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