Class: OnChain
- Inherits:
-
Object
- Object
- OnChain
- Defined in:
- lib/onchain/block_chain.rb,
lib/onchain/sweeper.rb,
lib/onchain/payments.rb
Overview
We support a number of blockchain API providers, if one goes down we automatically switch over to another.
Each provider has to support the following methods
get_balance(address) get_all_balances([]) send_tx(tx_hex) get_transactions(address)
Defined Under Namespace
Classes: BlockChain
Constant Summary collapse
- FEE =
50000
Class Method Summary collapse
-
.create_payment_tx(redemption_script, payments) ⇒ Object
With a bunch of HD wallet paths, build a transaction That pays all the coins to a certain address.
- .get_address_from_redemption_script(redemption_script) ⇒ Object
- .hex_to_script(hex) ⇒ Object
-
.sweep(paths, mpk, destination_address) ⇒ Object
With a bunch of HD wallet paths, build a transaction That pays all the coins to a certain address.
Class Method Details
.create_payment_tx(redemption_script, payments) ⇒ Object
With a bunch of HD wallet paths, build a transaction That pays all the coins to a certain address
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/onchain/payments.rb', line 22 def create_payment_tx(redemption_script, payments) begin fund_address = get_address_from_redemption_script(redemption_script) tx = Bitcoin::Protocol::Tx.new total_amount = FEE payments.each do |payment| if payment[1].is_a?(String) payment[1] = payment[1].to_i end total_amount = total_amount + payment[1] end total_in_fund = OnChain::BlockChain.get_balance_satoshi(fund_address) # Do we have enough in the fund. if(total_amount > total_in_fund) return 'Balance is not enough to cover payment' end # OK, let's get some inputs amount_so_far = 0 unspent = OnChain::BlockChain.get_unspent_outs(fund_address) unspent.each do |spent| txin = Bitcoin::Protocol::TxIn.new txin.prev_out = spent[0].scan(/../).map { |x| x.hex }.pack('c*').reverse txin.prev_out_index = spent[1] txin.script = hex_to_script(redemption_script).to_payload tx.add_in(txin) amount_so_far = amount_so_far + spent[3].to_i if amount_so_far >= total_amount next end end change = amount_so_far - total_amount payments.each do |payment| txout = Bitcoin::Protocol::TxOut.new(payment[1], Bitcoin::Script.to_address_script(payment[0])) tx.add_out(txout) end # Send the chnage back. if total_in_fund > total_amount txout = Bitcoin::Protocol::TxOut.new(total_in_fund - total_amount, Bitcoin::Script.to_address_script(fund_address)) tx.add_out(txout) end return tx rescue Exception => e return 'Unable to parse payment :: ' + e.to_s end end |
.get_address_from_redemption_script(redemption_script) ⇒ Object
6 7 8 9 10 11 12 13 |
# File 'lib/onchain/payments.rb', line 6 def get_address_from_redemption_script(redemption_script) sbin = redemption_script.scan(/../).map { |x| x.hex }.pack('c*') hex = sbin.unpack("H*")[0] fund_address = Bitcoin.hash160_to_p2sh_address(Bitcoin.hash160(hex)) return fund_address end |
.hex_to_script(hex) ⇒ Object
15 16 17 18 |
# File 'lib/onchain/payments.rb', line 15 def hex_to_script(hex) sbin = hex.scan(/../).map { |x| x.hex }.pack('c*') return Bitcoin::Script.new(sbin) end |
.sweep(paths, mpk, destination_address) ⇒ Object
With a bunch of HD wallet paths, build a transaction That pays all the coins to a certain address
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/onchain/sweeper.rb', line 7 def sweep(paths, mpk, destination_address) master = MoneyTree::Node.from_serialized_address(mpk) tx = Bitcoin::Protocol::Tx.new amount = 0 paths.each do |path| address = master.node_for_path(path).to_address unspent = OnChain::BlockChain.get_unspent_outs(address) unspent.each do |spent| txin = Bitcoin::Protocol::TxIn.new txin.prev_out = spent[0] txin.prev_out_index = spent[1] amount += spent[3].to_i tx.add_in(txin) end end txout = Bitcoin::Protocol::TxOut.new(amount, Bitcoin::Script.from_string( "OP_DUP OP_HASH160 #{destination_address} " + "OP_EQUALVERIFY OP_CHECKSIG").to_payload) tx.add_out(txout) end |