Class: Tapyrus::BIP175

Inherits:
Object
  • Object
show all
Defined in:
lib/tapyrus/bip175.rb

Overview

Key generation based on BIP-175

master = Tapyrus::ExtKey.from_base58(‘xprv9s21ZrQH143K2JF8RafpqtKiTbsbaxEeUaMnNHsm5o6wCW3z8ySyH4UxFVSfZ8n7ESu7fgir8imbZKLYVBxFPND1pniTZ81vKfd45EHKX73’) bip175 = Tapyrus::BIP175.from_private_key(master) bip175 << “foo” bip175 << “bar” bip175.addr > 1C7f322izqMqLzZzfzkPAjxBzprxDi47Yf

Constant Summary collapse

PURPOSE_TYPE =
175

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBIP175

Returns a new instance of BIP175.



19
20
21
# File 'lib/tapyrus/bip175.rb', line 19

def initialize
  @contracts = []
end

Instance Attribute Details

#master_ext_keyObject

Returns the value of attribute master_ext_key.



17
18
19
# File 'lib/tapyrus/bip175.rb', line 17

def master_ext_key
  @master_ext_key
end

#payment_baseObject

Returns the value of attribute payment_base.



17
18
19
# File 'lib/tapyrus/bip175.rb', line 17

def payment_base
  @payment_base
end

Class Method Details

.from_ext_key(key) ⇒ Object

Parameters:

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
# File 'lib/tapyrus/bip175.rb', line 24

def self.from_ext_key(key)
  raise ArgumentError, "key should be Tapyrus::ExtKey" unless key.is_a?(Tapyrus::ExtKey)
  raise ArgumentError, "key should be master private extended key" unless key.master?
  new.tap do |bip175|
    bip175.master_ext_key = key
    bip175.payment_base =
      key.derive(PURPOSE_TYPE, true).derive(Tapyrus.chain_params.bip44_coin_type, true).ext_pubkey
  end
end

.from_ext_pubkey(key) ⇒ Object

Parameters:

Raises:

  • (ArgumentError)


35
36
37
38
# File 'lib/tapyrus/bip175.rb', line 35

def self.from_ext_pubkey(key)
  raise ArgumentError, "key should be Tapyrus::ExtPubkey" unless key.is_a?(Tapyrus::ExtPubkey)
  new.tap { |bip175| bip175.payment_base = key }
end

Instance Method Details

#add(contract) ⇒ Object Also known as: <<

Add value of hashed contract

Parameters:

  • contract (String)

    contract information



42
43
44
45
# File 'lib/tapyrus/bip175.rb', line 42

def add(contract)
  @contracts << Tapyrus.sha256(contract)
  self
end

#addrObject



74
75
76
# File 'lib/tapyrus/bip175.rb', line 74

def addr
  pubkey.addr
end

#combined_hashString

Return combined hash consist of payment_base and contract hashes

Returns:



50
51
52
53
54
# File 'lib/tapyrus/bip175.rb', line 50

def combined_hash
  hashes = @contracts.map { |c| c.bth }.sort
  concatenated_hash = [payment_base.to_base58].concat(hashes).join
  Tapyrus.sha256(concatenated_hash)
end

#priv_keyTapyrus::ExtKey

Return pay-to-contract extended private key

Returns:



58
59
60
61
62
63
64
# File 'lib/tapyrus/bip175.rb', line 58

def priv_key
  key = master_ext_key.derive(PURPOSE_TYPE, true).derive(Tapyrus.chain_params.bip44_coin_type, true)

  # Split every 2 bytes
  paths = combined_hash.unpack("S>*")
  paths.inject(key) { |key, p| key.derive(p) }
end

#pubkeyTapyrus::ExtPubkey

Return pay-to-contract extended public key

Returns:



68
69
70
71
72
# File 'lib/tapyrus/bip175.rb', line 68

def pubkey
  # Split every 2 bytes
  paths = combined_hash.unpack("S>*")
  paths.inject(payment_base) { |key, p| key.derive(p) }
end