class Bitcoin::PaymentCode

BIP47 payment code

Constants

SUPPORT_SIGNS
SUPPORT_VERSIONS
VERSION_BYTE

Attributes

sign[RW]
x_value[RW]

Public Class Methods

from_base58(base58_payment_code) click to toggle source

decode base58 encoded payment code @params [String] base58_payment_code base58 encoded payment code

# File lib/bitcoin/payment_code.rb, line 54
def self.from_base58(base58_payment_code)
  hex = Bitcoin::Base58.decode(base58_payment_code)
  version = hex[2..3]
  sign = hex[6..7]
  public_key = hex[8..71]
  payment_code = hex[0...-8]

  raise ArgumentError, 'invalid version byte' unless hex[0..1] == VERSION_BYTE
  raise ArgumentError, 'invalid version' unless PaymentCode.support_version?(version)
  raise ArgumentError, 'invalid sign' unless PaymentCode.support_sign?(sign)
  raise ArgumentError, Errors::Messages::INVALID_PUBLIC_KEY unless Bitcoin::Key.new(priv_key: nil, pubkey: sign + public_key).fully_valid_pubkey?
  raise ArgumentError, Errors::Messages::INVALID_CHECKSUM unless Bitcoin.calc_checksum(payment_code) == hex[-8..-1]

  x_value = payment_code[8..71]
  chain_code_hex = payment_code[72..135]

  payment_code_pubkey = PaymentCode.new
  payment_code_pubkey.depth = 3
  payment_code_pubkey.sign = sign
  payment_code_pubkey.x_value = x_value
  payment_code_pubkey.chain_code = [chain_code_hex].pack('H*')

  payment_code_pubkey.to_payload
end
generate_master(seed) click to toggle source

generate master key from seed. @params [String] seed a seed data with hex format.

Calls superclass method Bitcoin::ExtKey::generate_master
# File lib/bitcoin/payment_code.rb, line 23
def self.generate_master(seed)
  master_ext_key = super.derive(47, harden=true).derive(0, harden=true).derive(0, harden=true)
  compressed_pubkey = master_ext_key.pub

  payment_code = PaymentCode.new
  payment_code.depth = master_ext_key.depth
  payment_code.key = master_ext_key.key
  payment_code.sign = compressed_pubkey[0..1]
  payment_code.x_value = compressed_pubkey[2..-1]
  payment_code.chain_code = master_ext_key.chain_code
  payment_code
end
new() click to toggle source
# File lib/bitcoin/payment_code.rb, line 15
def initialize
  @version = '01'
  @features_bits = '00'
  @reserve_field = '0' * 26
end
support_sign?(sign) click to toggle source

check whether sign is supported version bytes.

# File lib/bitcoin/payment_code.rb, line 85
def self.support_sign?(sign)
  SUPPORT_SIGNS.include?(sign)
end
support_version?(version) click to toggle source

check whether version is supported version bytes.

# File lib/bitcoin/payment_code.rb, line 80
def self.support_version?(version)
  SUPPORT_VERSIONS.include?(version)
end

Public Instance Methods

notification_address() click to toggle source

get notification address

# File lib/bitcoin/payment_code.rb, line 48
def notification_address
  ext_pubkey.derive(0).addr
end
to_base58() click to toggle source

Base58 encoded payment code

# File lib/bitcoin/payment_code.rb, line 37
def to_base58
  payment_code_with_version_byte = VERSION_BYTE + to_hex
  Bitcoin::Base58.encode(payment_code_with_version_byte + Bitcoin.calc_checksum(payment_code_with_version_byte))
end
to_payload() click to toggle source

serialize payment code

# File lib/bitcoin/payment_code.rb, line 43
def to_payload
  @version.htb << @features_bits.htb << @sign.htb << @x_value.htb << @chain_code << @reserve_field.htb
end