Commit 32d4cc8627d666f70ec5c392d5307665dd034872

Authored by Marius Hanne

Merge remote-tracking branch 'brian/master' into wallet_gui

Showing 2 changed files Side-by-side Diff

lib/bitcoin/protocol/parser.rb
... ... @@ -2,10 +2,14 @@
2 2 module Protocol
3 3  
4 4 class Parser
  5 +
5 6 def initialize(handler=nil)
6 7 @h = handler || Handler.new
7 8 @buf = ""
  9 + @log = Bitcoin::Logger.create("parser")
8 10 end
  11 +
  12 + def log; @log; end
9 13  
10 14 # handles inv/getdata packets
11 15 #
12 16  
... ... @@ -118,9 +122,9 @@
118 122 def handle_error(type, msg)
119 123 case type
120 124 when :close
121   - puts "closing packet stream (#{msg})"
  125 + log.debug {"closing packet stream (#{msg})"}
122 126 else
123   - p [type, msg]
  127 + log.debug { [type, msg] }
124 128 end
125 129 end
126 130 end # Parser
lib/bitcoin/protocol/tx.rb
... ... @@ -101,6 +101,11 @@
101 101 def signature_hash_for_input(input_idx, outpoint_tx, script_pubkey=nil, hash_type=nil, drop_sigs=nil, script=nil)
102 102 # https://github.com/bitcoin/bitcoin/blob/e071a3f6c06f41068ad17134189a4ac3073ef76b/script.cpp#L834
103 103 # http://code.google.com/p/bitcoinj/source/browse/trunk/src/com/google/bitcoin/core/Script.java#318
  104 + # https://en.bitcoin.it/wiki/OP_CHECKSIG#How_it_works
  105 + # https://github.com/bitcoin/bitcoin/blob/c2e8c8acd8ae0c94c70b59f55169841ad195bb99/src/script.cpp#L1058
  106 +
  107 + hash_type ||= 1 # 1: ALL, 2: NONE, 3: SINGLE
  108 +
104 109 pin = @in.map.with_index{|i,idx|
105 110 if idx == input_idx
106 111 script_pubkey ||= outpoint_tx.out[ i.prev_out_index ].pk_script
107 112  
... ... @@ -109,16 +114,25 @@
109 114 length = script_pubkey.bytesize
110 115 [ i.prev_out, i.prev_out_index, length, script_pubkey, i.sequence || "\xff\xff\xff\xff" ].pack("a32ICa#{length}a4")
111 116 else
112   - [ i.prev_out, i.prev_out_index, 0, i.sequence || "\xff\xff\xff\xff" ].pack("a32ICa4")
  117 + case hash_type
  118 + when 2
  119 + [ i.prev_out, i.prev_out_index, 0, "\x00\x00\x00\x00" ].pack("a32ICa4")
  120 + else
  121 + [ i.prev_out, i.prev_out_index, 0, i.sequence || "\xff\xff\xff\xff" ].pack("a32ICa4")
  122 + end
113 123 end
114 124 }.join
115 125 pout = @out.map{|o|
116 126 [ o.value, o.pk_script_length, o.pk_script ].pack("QCa#{o.pk_script_length}")
117 127 }.join
118 128  
119   - hash_type ||= 1 # 1: ALL, 2: NONE, 3: SINGLE
120   -
121   - in_size, out_size = Protocol.pack_var_int(@in.size), Protocol.pack_var_int(@out.size)
  129 + case hash_type
  130 + when 2
  131 + pout = ""
  132 + in_size, out_size = Protocol.pack_var_int(@in.size), Protocol.pack_var_int(0)
  133 + else
  134 + in_size, out_size = Protocol.pack_var_int(@in.size), Protocol.pack_var_int(@out.size)
  135 + end
122 136 buf = [[@ver].pack("I"), in_size, pin, out_size, pout, [@lock_time].pack("I")].join + [hash_type].pack("I")
123 137 Digest::SHA256.digest( Digest::SHA256.digest( buf ) )
124 138 end