Commit d48c3498359d5b18552c18b400ada979543d68bf

Authored by Brian Armstrong

Merge branch 'master' of git://github.com/lian/bitcoin-ruby

Showing 1 changed file Side-by-side Diff

lib/bitcoin/protocol/tx.rb
... ... @@ -96,6 +96,11 @@
96 96 def signature_hash_for_input(input_idx, outpoint_tx, script_pubkey=nil, hash_type=nil, drop_sigs=nil, script=nil)
97 97 # https://github.com/bitcoin/bitcoin/blob/e071a3f6c06f41068ad17134189a4ac3073ef76b/script.cpp#L834
98 98 # http://code.google.com/p/bitcoinj/source/browse/trunk/src/com/google/bitcoin/core/Script.java#318
  99 + # https://en.bitcoin.it/wiki/OP_CHECKSIG#How_it_works
  100 + # https://github.com/bitcoin/bitcoin/blob/c2e8c8acd8ae0c94c70b59f55169841ad195bb99/src/script.cpp#L1058
  101 +
  102 + hash_type ||= 1 # 1: ALL, 2: NONE, 3: SINGLE
  103 +
99 104 pin = @in.map.with_index{|i,idx|
100 105 if idx == input_idx
101 106 script_pubkey ||= outpoint_tx.out[ i.prev_out_index ].pk_script
102 107  
... ... @@ -104,16 +109,25 @@
104 109 length = script_pubkey.bytesize
105 110 [ i.prev_out, i.prev_out_index, length, script_pubkey, i.sequence || "\xff\xff\xff\xff" ].pack("a32ICa#{length}a4")
106 111 else
107   - [ i.prev_out, i.prev_out_index, 0, i.sequence || "\xff\xff\xff\xff" ].pack("a32ICa4")
  112 + case hash_type
  113 + when 2
  114 + [ i.prev_out, i.prev_out_index, 0, "\x00\x00\x00\x00" ].pack("a32ICa4")
  115 + else
  116 + [ i.prev_out, i.prev_out_index, 0, i.sequence || "\xff\xff\xff\xff" ].pack("a32ICa4")
  117 + end
108 118 end
109 119 }.join
110 120 pout = @out.map{|o|
111 121 [ o.value, o.pk_script_length, o.pk_script ].pack("QCa#{o.pk_script_length}")
112 122 }.join
113 123  
114   - hash_type ||= 1 # 1: ALL, 2: NONE, 3: SINGLE
115   -
116   - in_size, out_size = Protocol.pack_var_int(@in.size), Protocol.pack_var_int(@out.size)
  124 + case hash_type
  125 + when 2
  126 + pout = ""
  127 + in_size, out_size = Protocol.pack_var_int(@in.size), Protocol.pack_var_int(0)
  128 + else
  129 + in_size, out_size = Protocol.pack_var_int(@in.size), Protocol.pack_var_int(@out.size)
  130 + end
117 131 buf = [[@ver].pack("I"), in_size, pin, out_size, pout, [@lock_time].pack("I")].join + [hash_type].pack("I")
118 132 Digest::SHA256.digest( Digest::SHA256.digest( buf ) )
119 133 end