Class: Tapyrus::BIP175
- Inherits:
-
Object
- Object
- Tapyrus::BIP175
- 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
-
#master_ext_key ⇒ Object
Returns the value of attribute master_ext_key.
-
#payment_base ⇒ Object
Returns the value of attribute payment_base.
Class Method Summary collapse
Instance Method Summary collapse
-
#add(contract) ⇒ Object
(also: #<<)
Add value of hashed contract.
- #addr ⇒ Object
-
#combined_hash ⇒ String
Return combined hash consist of payment_base and contract hashes.
-
#initialize ⇒ BIP175
constructor
A new instance of BIP175.
-
#priv_key ⇒ Tapyrus::ExtKey
Return pay-to-contract extended private key.
-
#pubkey ⇒ Tapyrus::ExtPubkey
Return pay-to-contract extended public key.
Constructor Details
#initialize ⇒ BIP175
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_key ⇒ Object
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_base ⇒ Object
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
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 |
Instance Method Details
#add(contract) ⇒ Object Also known as: <<
Add value of hashed contract
42 43 44 45 |
# File 'lib/tapyrus/bip175.rb', line 42 def add(contract) @contracts << Tapyrus.sha256(contract) self end |
#addr ⇒ Object
74 75 76 |
# File 'lib/tapyrus/bip175.rb', line 74 def addr pubkey.addr end |
#combined_hash ⇒ String
Return combined hash consist of payment_base and contract hashes
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_key ⇒ Tapyrus::ExtKey
Return pay-to-contract extended private key
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 |
#pubkey ⇒ Tapyrus::ExtPubkey
Return pay-to-contract extended public key
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 |