Method: OnChain::Transaction.check_integrity

Defined in:
lib/onchain/transaction.rb

.check_integrity(txhex, amount, orig_addresses, dest_addr, tolerence) ⇒ Object

Check a transactions inputs only spend enough to cover fees and amount Basically if onchain creates an incorrect transaction the client can identify it here.



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
38
39
40
41
42
# File 'lib/onchain/transaction.rb', line 7

def check_integrity(txhex, amount, orig_addresses, dest_addr, tolerence)
  
  tx = Bitcoin::Protocol::Tx.new OnChain::hex_to_bin(txhex)
  
  input_amount = 0
  # Let's add up the value of all the inputs.
  tx.in.each_with_index do |txin, index|
  
    prev_hash = txin.to_hash['prev_out']['hash']
    prev_index = txin.to_hash['prev_out']['n']
    
    # Get the amount for the previous output
    prevhex = OnChain::BlockChain.get_transaction(prev_hash)
    prev_tx = Bitcoin::Protocol::Tx.new OnChain::hex_to_bin(prevhex)
    
    input_amount += prev_tx.out[prev_index].value
    
    if ! orig_addresses.include? prev_tx.out[prev_index].parsed_script.get_hash160_address
      raise "One of the inputs is not from from our list of valid originating addresses"
    end
  end
  
  # subtract the the chnage amounts
  tx.out.each do |txout|
    if orig_addresses.include? txout.parsed_script.get_address
      input_amount = input_amount - txout.value
    end
  end
  
  tolerence = (amount * (1 + tolerence)) 
  if input_amount > tolerence
    raise "Transaction has more input value (#{input_amount}) than the tolerence #{tolerence}"
  end
  
  return true
end