Class: OnChain::Transaction

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

Class Method Summary collapse

Class Method Details

.add_fee_to_tx(fee, fee_addr, tx, network = :bitcoin) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/onchain/transaction.rb', line 97

def add_fee_to_tx(fee, fee_addr, tx, network = :bitcoin)
  
  # Add wallet fee
  if fee > 0 and (fee - 10000) > 0
    
    # Take the miners fee from the wallet fees
    fee = fee - 10000
    
    # Check for affiliate
    if fee_addr.kind_of?(Array)
      affil_fee = fee / 2
      txout1 = Bitcoin::Protocol::TxOut.new(affil_fee, to_address_script(fee_addr[0], network))
      txout2 = Bitcoin::Protocol::TxOut.new(affil_fee, to_address_script(fee_addr[1], network))
      tx.add_out(txout1)
      tx.add_out(txout2)
    else
      txout = Bitcoin::Protocol::TxOut.new(fee, to_address_script(fee_addr, network))
      tx.add_out(txout)
    end
  end
  
end

.calculate_fee(amount, fee_percent, min_fee_satoshi) ⇒ Object



180
181
182
183
184
185
186
187
188
189
# File 'lib/onchain/transaction.rb', line 180

def calculate_fee(amount, fee_percent, min_fee_satoshi)
  
  fee = (amount * (fee_percent / 100.0)).to_i
  
  if fee < min_fee_satoshi
    return min_fee_satoshi
  end
  
  return fee
end

.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

.create_single_address_transaction(orig_addr, dest_addr, amount, fee_percent, fee_addr, min_fee_satoshi, network = :bitcoin) ⇒ Object



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
# File 'lib/onchain/transaction.rb', line 44

def create_single_address_transaction(orig_addr, dest_addr, amount, 
  fee_percent, fee_addr, min_fee_satoshi, network = :bitcoin)

  tx = Bitcoin::Protocol::Tx.new
  
  fee = calculate_fee(amount, fee_percent, min_fee_satoshi)
  
  total_amount = amount + fee
  
  unspents, indexes, change = OnChain::BlockChain.get_unspent_for_amount(
    [orig_addr], total_amount, network)
  indexes = nil

  
  # Process the unpsent outs.
  unspents.each_with_index do |spent, index|

    txin = Bitcoin::Protocol::TxIn.new([ spent[0] ].pack('H*').reverse, spent[1])
    txin.script_sig = OnChain.hex_to_bin(spent[2])
    tx.add_in(txin)
  end
  txout = Bitcoin::Protocol::TxOut.new(amount, to_address_script(dest_addr, network))
  
  tx.add_out(txout)
  
  # Add wallet fee
  add_fee_to_tx(fee, fee_addr, tx, network)

  # Send the change back.
  if change > 0
    
    txout = Bitcoin::Protocol::TxOut.new(change, to_address_script(orig_addr, network))
  
    tx.add_out(txout)
  end

  inputs_to_sign = get_inputs_to_sign(tx)
  
  return OnChain::bin_to_hex(tx.to_payload), inputs_to_sign
end

.create_transaction(redemption_scripts, address, amount_in_satoshi, miners_fee, network = :bitcoin) ⇒ Object

Given a send address and an amount produce a transaction and a list of hashes that need to be signed.

The transaction will be in hex format.

The list of hashes that need to be signed will be in this format

[input index]=> { :hash => hash }

i.e.

0][:hash => ‘345435345…’
0][:hash => ‘122133445.…’


238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/onchain/transaction.rb', line 238

def create_transaction(redemption_scripts, address, amount_in_satoshi, miners_fee, network = :bitcoin)

  total_amount = miners_fee
  
  total_amount = total_amount + amount_in_satoshi
  
  addresses = redemption_scripts.map { |rs| 
    generate_address_of_redemption_script(rs, network)
  }
  
  unspents, indexes, change = OnChain::BlockChain.get_unspent_for_amount(addresses, total_amount, network)
  
  # OK, let's build a transaction.
  tx = Bitcoin::Protocol::Tx.new
  
  # Process the unpsent outs.
  unspents.each_with_index do |spent, index|

    script = redemption_scripts[indexes[index]]
    
    txin = Bitcoin::Protocol::TxIn.new([ spent[0] ].pack('H*').reverse, spent[1])
    txin.script_sig = OnChain::hex_to_bin(script)
    tx.add_in(txin)
  end
  
  # Do we have enough in the fund.
  #if(total_amount > btc_balance)
  #  raise 'Balance is not enough to cover payment'
  #end

  txout = Bitcoin::Protocol::TxOut.new(amount_in_satoshi, 
      to_address_script(address, network))
  
  tx.add_out(txout)
  
  change_address = addresses[0]

  # Send the change back.
  if change > 0
  
    txout = Bitcoin::Protocol::TxOut.new(change, 
      to_address_script(change_address, network))
  
    tx.add_out(txout)
  end

  inputs_to_sign = get_inputs_to_sign tx

  return OnChain::bin_to_hex(tx.to_payload), inputs_to_sign
end

.create_transaction_with_fee(redemption_scripts, address, amount, fee_percent, fee_addr) ⇒ Object

Like create_single_address_transaction but for multi sig wallets.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/onchain/transaction.rb', line 132

def create_transaction_with_fee(redemption_scripts, address, amount, fee_percent, fee_addr)

  fee = calculate_fee(amount, fee_percent, 10000)
  
  total_amount = amount + fee
  
  addresses = redemption_scripts.map { |rs| 
    generate_address_of_redemption_script(rs)
  }
  
  unspents, indexes, change = OnChain::BlockChain.get_unspent_for_amount(addresses, total_amount)
  
  # OK, let's build a transaction.
  tx = Bitcoin::Protocol::Tx.new
  
  # Process the unpsent outs.
  unspents.each_with_index do |spent, index|

    script = redemption_scripts[indexes[index]]
    
    txin = Bitcoin::Protocol::TxIn.new([ spent[0] ].pack('H*').reverse, spent[1])
    txin.script_sig = OnChain::hex_to_bin(script)
    tx.add_in(txin)
  end
  
  # Add wallet fee
  add_fee_to_tx(fee, fee_addr, tx)

  txout = Bitcoin::Protocol::TxOut.new(amount, Bitcoin::Script.to_address_script(address))
  
  tx.add_out(txout)
  
  change_address = addresses[0]

  # Send the change back.
  if change > 0
  
    txout = Bitcoin::Protocol::TxOut.new(change, 
      Bitcoin::Script.to_address_script(change_address))
  
    tx.add_out(txout)
  end

  inputs_to_sign = get_inputs_to_sign tx

  return OnChain::bin_to_hex(tx.to_payload), inputs_to_sign
end

.do_we_have_all_the_signatures(sig_list) ⇒ Object

Run through the signature list and check it is all signed.



345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/onchain/transaction.rb', line 345

def do_we_have_all_the_signatures(sig_list)
  
  sig_list.each do |input|
    input.each_key do |public_key|
      if input[public_key]['hash'] == nil or input[public_key]['sig'] == nil
        return false
      end
    end
  end
  
  return true
end

.generate_address_of_redemption_script(redemption_script, network = :bitcoin) ⇒ Object



125
126
127
128
129
# File 'lib/onchain/transaction.rb', line 125

def generate_address_of_redemption_script(redemption_script)
  hash160 = Bitcoin.hash160(redemption_script)

  return Bitcoin.hash160_to_p2sh_address(hash160)
end

.generate_redemption_script(minimum_sigs, addresses) ⇒ Object



120
121
122
123
# File 'lib/onchain/transaction.rb', line 120

def generate_redemption_script(minimum_sigs, addresses)
  address, redeem_script = Bitcoin.pubkeys_to_p2sh_multisig_address(minimum_sigs, *addresses)
  return redeem_script.hth
end

.get_inputs_to_sign(tx) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/onchain/transaction.rb', line 205

def get_inputs_to_sign(tx)
  inputs_to_sign = []
  tx.in.each_with_index do |txin, index|
    hash = tx.signature_hash_for_input(index, txin.script, 1)
    
    script = Bitcoin::Script.new txin.script
    
    pubkeys = get_public_keys_from_script(script)
    pubkeys.each do |key|
      
      if inputs_to_sign[index] == nil
        inputs_to_sign[index] = {}
      end
      inputs_to_sign[index][key] = {'hash' => OnChain::bin_to_hex(hash)}
    end
  end
  return inputs_to_sign
end

.get_public_keys_from_script(script) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/onchain/transaction.rb', line 191

def get_public_keys_from_script(script)

  if script.is_hash160?
    return [Bitcoin.hash160_to_address(script.get_hash160)]
  end
  
  pubs = []
  script.get_multisig_pubkeys.each do |pub|
    pub_hex = OnChain.bin_to_hex(pub)
    pubs << Bitcoin.hash160_to_address(Bitcoin.hash160(pub_hex))
  end
  return pubs
end

.sign_transaction(transaction_hex, sig_list, pubkey = nil) ⇒ Object

Given a transaction in hex string format, apply the given signature list to it.

Signatures should be in the format

[0]=> {’hash’ => ‘345435345.…’, ‘sig’ => ‘435fgdf4553…’} [0]=> {’hash’ => ‘122133445.…’, ‘sig’ => ‘435fgdf4553…’}



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/onchain/transaction.rb', line 307

def sign_transaction(transaction_hex, sig_list, pubkey = nil)
  
  tx = Bitcoin::Protocol::Tx.new OnChain::hex_to_bin(transaction_hex)
  
  tx.in.each_with_index do |txin, index|
    
    sigs = []
    
    rscript = Bitcoin::Script.new txin.script
    
    pub_keys = get_public_keys_from_script(rscript)
    pub_keys.each do |hkey|
      
      if sig_list[index][hkey] != nil and sig_list[index][hkey]['sig'] != nil
        
        # Add the signature to the list.
        sigs << OnChain.hex_to_bin(sig_list[index][hkey]['sig'])
        
      end
    end
    
    if sigs.count > 0
      in_script = Bitcoin::Script.new txin.script
      if in_script.is_hash160?
        sig = sigs[0]
        txin.script = Bitcoin::Script.to_pubkey_script_sig(sig, OnChain.hex_to_bin(pubkey))
      else
        txin.script = Bitcoin::Script.to_p2sh_multisig_script_sig(rscript.to_payload, sigs)
      end
    end
  
    #raise "Signature error " + index.to_s  if ! tx.verify_input_signature(index, in_script.to_payload)
  end
  
  return OnChain::bin_to_hex(tx.to_payload)
end

.to_address_script(orig_addr, network = :bitcoin) ⇒ Object

The bitcoin ruby methods are not thread safe when switching between testnet and the bitcoin network.



87
88
89
90
91
92
93
94
95
# File 'lib/onchain/transaction.rb', line 87

def to_address_script(orig_addr, network = :bitcoin)
  
  # TODO add thread safety.
  Bitcoin.network = network
  address = Bitcoin::Script.to_address_script(orig_addr)
  Bitcoin.network = :bitcoin
  
  return address
end