Commit 2d3ffbe2aa4716bd6c452d6404e9ef5edaab8010

Authored by Marius Hanne
1 parent cfa20c8eb5
Exists in op_eval

op_eval

Showing 2 changed files with 29 additions and 0 deletions Side-by-side Diff

lib/bitcoin/script.rb
... ... @@ -19,6 +19,7 @@
19 19 OP_CHECKSIGVERIFY = 173
20 20 OP_CHECKMULTISIG = 174
21 21 OP_CHECKMULTISIGVERIFY = 175
  22 + OP_EVAL = 176
22 23 OP_TOALTSTACK = 107
23 24 OP_FROMALTSTACK = 108
24 25 OP_TUCK = 125
... ... @@ -285,6 +286,10 @@
285 286 @debug << "OP_CHECKMULTISIG"
286 287 op_checkmultisig(check_callback)
287 288  
  289 + when OP_EVAL
  290 + @debug << "OP_EVAL"
  291 + op_eval(check_callback)
  292 +
288 293 else
289 294 name = OPCODES[chunk] || chunk
290 295 raise "opcode #{name} unkown or not implemented"
... ... @@ -353,6 +358,21 @@
353 358 end
354 359  
355 360 @stack << 1 if valid_sigs >= n_sigs
  361 + end
  362 +
  363 + # op_eval: https://en.bitcoin.it/wiki/BIP_0012
  364 + #
  365 + # <sig> {<pub> OP_CHECKSIG} | OP_DUP OP_HASH160 <script hash> OP_EQUALVERIFY OP_EVAL
  366 + def op_eval(check_callback)
  367 + script = @stack.pop
  368 +
  369 + # TODO: better way to encode stack data for new script?
  370 + # TODO: use all stack items
  371 + sig = Script.from_string(@stack.pop.unpack("H*")[0]).raw
  372 +
  373 + script = Script.new(sig + script)
  374 +
  375 + @stack << 1 if script.run(&check_callback)
356 376 end
357 377  
358 378 def is_standard? # TODO: add
spec/bitcoin/script_spec.rb
... ... @@ -372,6 +372,15 @@
372 372 # run_script(script, "foobar").should == false
373 373 end
374 374  
  375 + it "should do OP_EVAL" do
  376 + k1 = Bitcoin::Key.new; k1.generate
  377 + sig = (k1.sign("foobar") + "\x01").unpack("H*")[0]
  378 + inner_script = Bitcoin::Script.from_string("#{k1.pub} OP_CHECKSIG").raw.unpack("H*")[0]
  379 + script_hash = Bitcoin.hash160(inner_script)
  380 + script = "#{sig} #{inner_script} OP_DUP OP_HASH160 #{script_hash} OP_EQUALVERIFY OP_EVAL"
  381 + run_script(script, "foobar").should == true
  382 + end
  383 +
375 384 end
376 385  
377 386 end