Bitcoin BIP-45 implementation on Ruby
ruby bin/demo.rb
bundle exec rdoc
NETWORK = :thebestcoin_testnet
REQUIRED_SIGNS = 2
cs_1_master = MultisigMoneyTree::Master.seed(0, network: NETWORK)
cs_2_master = MultisigMoneyTree::Master.seed(1, network: NETWORK)
cs_1_keys = {
private: cs_1_master.to_bip32(:private, network: NETWORK),
public: cs_1_master.to_bip32(:public, network: NETWORK)
}
cs_2_keys = {
private: cs_1_master.to_bip32(:private, network: NETWORK),
public: cs_1_master.to_bip32(:public, network: NETWORK)
}
# Init Master Node for bip32 key
cs_1_public_master = MultisigMoneyTree::Master.from_bip32(0, cs_1_keys[:public])
# Get deposit node for 1-th address
cs_1_deposit_node = cs_1_public_master.node_for(0, 1)
cs_1_public_node = {
pubkey: cs_1_deposit_node.to_bip32(:public, network: NETWORK),
address: cs_1_deposit_node.to_address(network: NETWORK)
}
In the same way, we initialize the node for the cosigner # 2
# Pack public nodes bip32 keys cosigners to hash with cosigner index
keys = {
0 => cs_0_public_node[:pubkey],
1 => cs_1_public_node[:pubkey]
}
# Init BIP45 (multisig) node
multisig_node = MultisigMoneyTree::BIP45Node.new({
required_signs: REQUIRED_SIGNS,
public_keys: keys,
network: NETWORK
})
# Get the address and other elements of multisig addresses
multisig = {
address: node.to_address,
redeem_script: node.redeem_script.hth,
public_key: node.to_bip45(network: NETWORK),
}
With the help of a public_key, we can in the future restore the multisig node by using the method MultisigMoneyTree::Master.from_bip45(pubkey)