Commit 71fa093cd9d773971e1d9703151581e9cb7b31c7

Authored by Julian Langschaedel
1 parent d9971b7447

fix Script#get_op_return_data helper

todo: port is standard template matching from bitcoind

Showing 2 changed files with 9 additions and 6 deletions Side-by-side Diff

lib/bitcoin/script.rb
... ... @@ -486,7 +486,7 @@
486 486 # is this a pubkey script
487 487 def is_pubkey?
488 488 return false if @chunks.size != 2
489   - (@chunks[1] == OP_CHECKSIG) && @chunks[0].is_a?(String) && @chunks[0].size > 1
  489 + (@chunks[1] == OP_CHECKSIG) && @chunks[0] && @chunks[0] != OP_RETURN
490 490 end
491 491 alias :is_send_to_ip? :is_pubkey?
492 492  
... ... @@ -506,7 +506,7 @@
506 506  
507 507 # is this an op_return script
508 508 def is_op_return?
509   - @chunks[0] == OP_RETURN && (!@chunks[1] || @chunks[1].is_a?(String)) &&@chunks.size <= 2
  509 + @chunks[0] == OP_RETURN && @chunks.size <= 2
510 510 end
511 511  
512 512 # get type of this tx
... ... @@ -565,7 +565,7 @@
565 565 # get the data possibly included in an OP_RETURN script
566 566 def get_op_return_data
567 567 return nil unless is_op_return?
568   - @chunks[1].unpack("H*")[0] if @chunks[1]
  568 + cast_to_string(@chunks[1]).unpack("H*")[0] if @chunks[1]
569 569 end
570 570  
571 571 # get all addresses this script corresponds to (if possible)
spec/bitcoin/script/script_spec.rb
... ... @@ -206,7 +206,8 @@
206 206 Script.new(SCRIPT[1]).get_op_return_data.should == nil
207 207 Script.from_string("OP_RETURN").get_op_return_data.should == nil
208 208 Script.from_string("OP_RETURN dead beef").get_op_return_data.should == nil
209   - Script.from_string("OP_RETURN OP_CHECKSIG").get_op_return_data.should == nil
  209 + Script.from_string("OP_RETURN deadbeef").get_op_return_data.should == "deadbeef"
  210 + Script.from_string("OP_RETURN OP_CHECKSIG").get_op_return_data.should == "00ac"
210 211 end
211 212  
212 213 end
... ... @@ -273,7 +274,8 @@
273 274 Script.new(SCRIPT[5]).is_op_return?.should == false
274 275 Script.new(SCRIPT[6]).is_op_return?.should == true
275 276 Script.from_string("OP_RETURN dead beef").is_op_return?.should == false
276   - Script.from_string("OP_RETURN OP_CHECKSIG").is_op_return?.should == false
  277 + Script.from_string("OP_RETURN deadbeef").is_op_return?.should == true
  278 + Script.from_string("OP_RETURN OP_CHECKSIG").is_op_return?.should == true
277 279 end
278 280  
279 281 it "#type" do
... ... @@ -284,7 +286,8 @@
284 286 Script.new(SCRIPT[4]).type.should == :multisig
285 287 Script.new(SCRIPT[5]).type.should == :p2sh
286 288 Script.new(SCRIPT[6]).type.should == :op_return
287   - Script.from_string("OP_RETURN OP_CHECKSIG").type.should == :unknown
  289 + Script.from_string("OP_RETURN OP_CHECKSIG").type.should == :op_return
  290 + Script.from_string("OP_RETURN dead beef").type.should == :unknown
288 291 end
289 292  
290 293 end