Class: OnChain::Payments

Inherits:
Object
  • Object
show all
Defined in:
lib/onchain/payments.rb

Constant Summary collapse

FEE =
50000

Class Method Summary collapse

Class Method Details

.create_payment_tx(redemption_script, payments) ⇒ Object



19
20
21
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
# File 'lib/onchain/payments.rb', line 19

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 = OnChain.hex_to_bin(redemption_script)
  hex = OnChain.bin_to_hex(sbin)
  fund_address = Bitcoin.hash160_to_p2sh_address(Bitcoin.hash160(hex))
  
  return fund_address
end

.hex_to_script(hex) ⇒ Object



15
16
17
# File 'lib/onchain/payments.rb', line 15

def hex_to_script(hex)
  return Bitcoin::Script.new(OnChain::hex_to_bin(hex))
end