lib/btcruby/script/script.rb in btcruby-1.6 vs lib/btcruby/script/script.rb in btcruby-1.7

- old
+ new

@@ -87,10 +87,18 @@ script_hash_script? || standard_multisig_script? || public_key_script? || standard_op_return_script? end + + # Returns true if this is an output script wrapped in a versioned pushdata for segwit softfork. + def versioned_script? + return chunks.size == 1 && + chunks[0].pushdata? && + chunks[0].canonical? && + chunks[0].pushdata.bytesize > 2 + end # Returns true if the script is a pay-to-pubkey script: # "<pubkey> OP_CHECKSIG" def public_key_script? return false if @chunks.size != 2 @@ -246,11 +254,11 @@ # If script starts with `<pushdata> OP_DROP`, these two opcodes are removed # and a new script instance is returned. def without_dropped_prefix_data if dropped_prefix_data - return self.class.new << @chunks[2..-1] + return Script.new << @chunks[2..-1] end self end # Returns true if the script consists of push data operations only @@ -286,11 +294,11 @@ @chunks.map{|c| c.pushdata? ? c.pushdata : c.opcode } end # Complete copy of a script. def dup - self.class.new(data: self.data) + Script.new(data: self.data) end def ==(other) return false if other == nil self.data == other.data @@ -322,11 +330,11 @@ end # Wraps the recipient into an output P2SH script # (OP_HASH160 <20-byte hash of the recipient> OP_EQUAL). def p2sh_script - self.class.new << OP_HASH160 << BTC.hash160(self.data) << OP_EQUAL + Script.new << OP_HASH160 << BTC.hash160(self.data) << OP_EQUAL end # Returns a dummy script matching this script on the input with # the same size as an intended signature script. @@ -334,22 +342,22 @@ # Set `strict` to false to allow imprecise guess for P2SH script. # Returns nil if could not determine a matching script. def simulated_signature_script(strict: true) if public_key_hash_script? # assuming non-compressed pubkeys to be conservative - return self.class.new << Script.simulated_signature(hashtype: SIGHASH_ALL) << Script.simulated_uncompressed_pubkey + return Script.new << Script.simulated_signature(hashtype: SIGHASH_ALL) << Script.simulated_uncompressed_pubkey elsif public_key_script? - return self.class.new << Script.simulated_signature(hashtype: SIGHASH_ALL) + return Script.new << Script.simulated_signature(hashtype: SIGHASH_ALL) elsif script_hash_script? && !strict # This is a wild approximation, but works well if most p2sh scripts are 2-of-3 multisig scripts. # If you have a very particular smart contract scheme you should not use TransactionBuilder which estimates fees this way. - return self.class.new << OP_0 << [Script.simulated_signature(hashtype: SIGHASH_ALL)]*2 << Script.simulated_multisig_script(2,3).data + return Script.new << OP_0 << [Script.simulated_signature(hashtype: SIGHASH_ALL)]*2 << Script.simulated_multisig_script(2,3).data elsif multisig_script? - return self.class.new << OP_0 << [Script.simulated_signature(hashtype: SIGHASH_ALL)]*self.multisig_signatures_required + return Script.new << OP_0 << [Script.simulated_signature(hashtype: SIGHASH_ALL)]*self.multisig_signatures_required else return nil end end @@ -461,11 +469,11 @@ self.dup << other end # Same arguments as with Array#[]. def subscript(*args) - self.class.new << chunks[*args] + Script.new << chunks[*args] end alias_method :[], :subscript # Removes chunks matching subscript byte-for-byte and returns a new script instance with subscript removed. # So if pushdata items are encoded differently, they won't match. @@ -476,10 +484,10 @@ # Script: a b b a b a b a c # Result: a b b b a c subscriptsize = subscript.chunks.size buf = [] i = 0 - result = self.class.new + result = Script.new chunks.each do |chunk| if chunk == subscript.chunks[i] buf << chunk i+=1 if i == subscriptsize # matched the whole subscript