Class: Bitcoin::Script
- Includes:
- Opcodes
- Defined in:
- lib/bitcoin/script/script.rb
Overview
bitcoin script
Constant Summary
Constants included from Opcodes
Opcodes::DUPLICATE_KEY, Opcodes::NAME_MAP, Opcodes::OPCODES_MAP, Opcodes::OP_0, Opcodes::OP_0NOTEQUAL, Opcodes::OP_1, Opcodes::OP_10, Opcodes::OP_11, Opcodes::OP_12, Opcodes::OP_13, Opcodes::OP_14, Opcodes::OP_15, Opcodes::OP_16, Opcodes::OP_1ADD, Opcodes::OP_1NEGATE, Opcodes::OP_1SUB, Opcodes::OP_2, Opcodes::OP_2DIV, Opcodes::OP_2DROP, Opcodes::OP_2DUP, Opcodes::OP_2MUL, Opcodes::OP_2OVER, Opcodes::OP_2ROT, Opcodes::OP_2SWAP, Opcodes::OP_3, Opcodes::OP_3DUP, Opcodes::OP_4, Opcodes::OP_5, Opcodes::OP_6, Opcodes::OP_7, Opcodes::OP_8, Opcodes::OP_9, Opcodes::OP_ABS, Opcodes::OP_ADD, Opcodes::OP_AND, Opcodes::OP_BOOLAND, Opcodes::OP_BOOLOR, Opcodes::OP_CAT, Opcodes::OP_CHECKMULTISIG, Opcodes::OP_CHECKMULTISIGVERIFY, Opcodes::OP_CHECKSIG, Opcodes::OP_CHECKSIGVERIFY, Opcodes::OP_CODESEPARATOR, Opcodes::OP_DEPTH, Opcodes::OP_DIV, Opcodes::OP_DROP, Opcodes::OP_DUP, Opcodes::OP_ELSE, Opcodes::OP_ENDIF, Opcodes::OP_EQUAL, Opcodes::OP_EQUALVERIFY, Opcodes::OP_FROMALTSTACK, Opcodes::OP_GREATERTHAN, Opcodes::OP_GREATERTHANOREQUAL, Opcodes::OP_HASH160, Opcodes::OP_HASH256, Opcodes::OP_IF, Opcodes::OP_IFDUP, Opcodes::OP_INVALIDOPCODE, Opcodes::OP_INVERT, Opcodes::OP_LEFT, Opcodes::OP_LESSTHAN, Opcodes::OP_LESSTHANOREQUAL, Opcodes::OP_LSHIFT, Opcodes::OP_MAX, Opcodes::OP_MIN, Opcodes::OP_MOD, Opcodes::OP_MUL, Opcodes::OP_NEGATE, Opcodes::OP_NIP, Opcodes::OP_NOP, Opcodes::OP_NOP1, Opcodes::OP_NOP10, Opcodes::OP_NOP2, Opcodes::OP_NOP3, Opcodes::OP_NOP4, Opcodes::OP_NOP5, Opcodes::OP_NOP6, Opcodes::OP_NOP7, Opcodes::OP_NOP8, Opcodes::OP_NOP9, Opcodes::OP_NOT, Opcodes::OP_NOTIF, Opcodes::OP_NUMEQUAL, Opcodes::OP_NUMEQUALVERIFY, Opcodes::OP_NUMNOTEQUAL, Opcodes::OP_OR, Opcodes::OP_OVER, Opcodes::OP_PICK, Opcodes::OP_PUBKEY, Opcodes::OP_PUBKEYHASH, Opcodes::OP_PUSHDATA1, Opcodes::OP_PUSHDATA2, Opcodes::OP_PUSHDATA4, Opcodes::OP_RESERVED, Opcodes::OP_RESERVED1, Opcodes::OP_RESERVED2, Opcodes::OP_RETURN, Opcodes::OP_RIGHT, Opcodes::OP_RIPEMD160, Opcodes::OP_ROLL, Opcodes::OP_ROT, Opcodes::OP_RSHIFT, Opcodes::OP_SHA1, Opcodes::OP_SHA256, Opcodes::OP_SIZE, Opcodes::OP_SUB, Opcodes::OP_SUBSTR, Opcodes::OP_SWAP, Opcodes::OP_TOALTSTACK, Opcodes::OP_TUCK, Opcodes::OP_VER, Opcodes::OP_VERIF, Opcodes::OP_VERIFY, Opcodes::OP_VERNOTIF, Opcodes::OP_WITHIN, Opcodes::OP_XOR
Instance Attribute Summary collapse
-
#chunks ⇒ Object
Returns the value of attribute chunks.
Class Method Summary collapse
-
.decode_number(s) ⇒ Object
decode script number hex to int value.
-
.encode_number(i) ⇒ Object
encode int value to script number hex.
-
.from_string(string) ⇒ Object
generate script from string.
-
.pack_pushdata(data) ⇒ Object
binary
dataconvert pushdata which contains data length and append PUSHDATA opcode if necessary. -
.parse_from_addr(addr) ⇒ Bitcoin::Script
generate script from addr.
- .parse_from_payload(payload) ⇒ Object
-
.to_multisig_script(m, pubkeys) ⇒ Script
generate m of n multisig script.
-
.to_p2pkh(pubkey_hash) ⇒ Object
generate P2PKH script.
-
.to_p2sh(script_hash) ⇒ Script
generate p2sh script.
-
.to_p2sh_multisig_script(m, pubkeys) ⇒ Script
generate m of n multisig p2sh script.
-
.to_p2wpkh(pubkey_hash) ⇒ Object
generate P2WPKH script.
-
.to_p2wsh(redeem_script) ⇒ Object
generate p2wsh script for
redeem_script.
Instance Method Summary collapse
-
#<<(obj) ⇒ Object
append object to payload.
- #==(other) ⇒ Object
- #addresses ⇒ Object
-
#append_data(data) ⇒ Script
append data to payload with pushdata opcode.
-
#append_opcode(opcode) ⇒ Script
append opcode to payload.
-
#delete_opcode(opcode) ⇒ Object
remove all occurences of opcode.
- #empty? ⇒ Boolean
-
#find_and_delete(subscript) ⇒ Object
removes chunks matching subscript byte-for-byte and returns as a new object.
- #get_multisig_pubkeys ⇒ Object
-
#include?(item) ⇒ Boolean
Check the item is in the chunk of the script.
-
#initialize ⇒ Script
constructor
A new instance of Script.
- #multisig? ⇒ Boolean
- #op_return? ⇒ Boolean
- #op_return_data ⇒ Object
-
#p2pkh? ⇒ Boolean
whether this script is a P2PKH format script.
- #p2sh? ⇒ Boolean
-
#p2wpkh? ⇒ Boolean
whether this script is a P2WPKH format script.
- #p2wsh? ⇒ Boolean
-
#push_int(n) ⇒ Object
push integer to stack.
-
#push_only? ⇒ Boolean
whether data push only script which dose not include other opcode.
-
#run ⇒ Object
execute script interpreter using this script for development.
-
#size ⇒ Object
script size.
-
#standard? ⇒ Boolean
check whether standard script.
- #standard_op_return? ⇒ Boolean
-
#subscript(*args) ⇒ Object
subscript this script to the specified range.
-
#subscript_codeseparator(separator_index) ⇒ Object
Returns a script that deleted the script before the index specified by separator_index.
- #to_h ⇒ Object
-
#to_hash160 ⇒ Object
generate hash160 hash for payload.
- #to_hex ⇒ Object
-
#to_p2sh ⇒ Script
generate p2sh script with this as a redeem script.
- #to_payload ⇒ Object
- #to_s ⇒ Object
-
#to_script_code(skip_separator_index = 0) ⇒ Object
If this script is witness program, return its script code, otherwise returns the self payload.
-
#to_sha256 ⇒ Object
generate sha-256 hash for payload.
- #type ⇒ Object
-
#witness_commitment ⇒ Object
get witness commitment.
-
#witness_data ⇒ Object
get witness version and witness program.
-
#witness_program? ⇒ Boolean
A witness program is any valid Script that consists of a 1-byte push opcode followed by a data push between 2 and 40 bytes.
Methods included from Opcodes
defined?, name_to_opcode, opcode_to_name, opcode_to_small_int, small_int_to_opcode
Constructor Details
#initialize ⇒ Script
Returns a new instance of Script.
12 13 14 |
# File 'lib/bitcoin/script/script.rb', line 12 def initialize @chunks = [] end |
Instance Attribute Details
#chunks ⇒ Object
Returns the value of attribute chunks.
10 11 12 |
# File 'lib/bitcoin/script/script.rb', line 10 def chunks @chunks end |
Class Method Details
.decode_number(s) ⇒ Object
decode script number hex to int value
387 388 389 390 391 392 393 394 395 |
# File 'lib/bitcoin/script/script.rb', line 387 def self.decode_number(s) v = s.htb.reverse return 0 if v.length.zero? mbs = v[0].unpack('C').first v[0] = [mbs - 0x80].pack('C') unless (mbs & 0x80) == 0 result = v.bth.to_i(16) result = -result unless (mbs & 0x80) == 0 result end |
.encode_number(i) ⇒ Object
encode int value to script number hex. The stacks hold byte vectors. When used as numbers, byte vectors are interpreted as little-endian variable-length integers with the most significant bit determining the sign of the integer. Thus 0x81 represents -1. 0x80 is another representation of zero (so called negative 0). Positive 0 is represented by a null-length vector. Byte vectors are interpreted as Booleans where False is represented by any representation of zero, and True is represented by any representation of non-zero.
373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/bitcoin/script/script.rb', line 373 def self.encode_number(i) return '' if i == 0 negative = i < 0 hex = i.abs.to_even_length_hex hex = '0' + hex unless (hex.length % 2).zero? v = hex.htb.reverse # change endian v = v << (negative ? 0x80 : 0x00) unless (v[-1].unpack('C').first & 0x80) == 0 v[-1] = [v[-1].unpack('C').first | 0x80].pack('C') if negative v.bth end |
.from_string(string) ⇒ Object
generate script from string.
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/bitcoin/script/script.rb', line 70 def self.from_string(string) script = new string.split(' ').each do |v| opcode = Opcodes.name_to_opcode(v) if opcode script << (v =~ /^\d/ && Opcodes.small_int_to_opcode(v.ord) ? v.ord : opcode) else script << (v =~ /^[0-9]+$/ ? v.to_i : v) end end script end |
.pack_pushdata(data) ⇒ Object
binary data convert pushdata which contains data length and append PUSHDATA opcode if necessary.
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/bitcoin/script/script.rb', line 398 def self.pack_pushdata(data) size = data.bytesize header = if size < OP_PUSHDATA1 [size].pack('C') elsif size < 0xff [OP_PUSHDATA1, size].pack('CC') elsif size < 0xffff [OP_PUSHDATA2, size].pack('Cv') elsif size < 0xffffffff [OP_PUSHDATA4, size].pack('CV') else raise ArgumentError, 'data size is too big.' end header + data end |
.parse_from_addr(addr) ⇒ Bitcoin::Script
generate script from addr.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/bitcoin/script/script.rb', line 86 def self.parse_from_addr(addr) begin segwit_addr = Bech32::SegwitAddr.new(addr) raise 'Invalid hrp.' unless Bitcoin.chain_params.bech32_hrp == segwit_addr.hrp Bitcoin::Script.parse_from_payload(segwit_addr.to_script_pubkey.htb) rescue Exception => e hex, addr_version = Bitcoin.decode_base58_address(addr) case addr_version when Bitcoin.chain_params.address_version Bitcoin::Script.to_p2pkh(hex) when Bitcoin.chain_params.p2sh_version Bitcoin::Script.to_p2sh(hex) else throw e end end end |
.parse_from_payload(payload) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/bitcoin/script/script.rb', line 104 def self.parse_from_payload(payload) s = new buf = StringIO.new(payload) until buf.eof? opcode = buf.read(1) if opcode.pushdata? pushcode = opcode.ord packed_size = nil len = case pushcode when OP_PUSHDATA1 packed_size = buf.read(1) packed_size.unpack('C').first when OP_PUSHDATA2 packed_size = buf.read(2) packed_size.unpack('v').first when OP_PUSHDATA4 packed_size = buf.read(4) packed_size.unpack('V').first else pushcode if pushcode < OP_PUSHDATA1 end if len s.chunks << [len].pack('C') if buf.eof? unless buf.eof? chunk = (packed_size ? (opcode + packed_size) : (opcode)) + buf.read(len) s.chunks << chunk end end else if Opcodes.defined?(opcode.ord) s << opcode.ord else s.chunks << (opcode + buf.read) # If opcode is invalid, put all remaining data in last chunk. end end end s end |
.to_multisig_script(m, pubkeys) ⇒ Script
generate m of n multisig script
57 58 59 |
# File 'lib/bitcoin/script/script.rb', line 57 def self.to_multisig_script(m, pubkeys) new << m << pubkeys << pubkeys.size << OP_CHECKMULTISIG end |
.to_p2pkh(pubkey_hash) ⇒ Object
generate P2PKH script
17 18 19 |
# File 'lib/bitcoin/script/script.rb', line 17 def self.to_p2pkh(pubkey_hash) new << OP_DUP << OP_HASH160 << pubkey_hash << OP_EQUALVERIFY << OP_CHECKSIG end |
.to_p2sh(script_hash) ⇒ Script
generate p2sh script.
38 39 40 |
# File 'lib/bitcoin/script/script.rb', line 38 def self.to_p2sh(script_hash) Script.new << OP_HASH160 << script_hash << OP_EQUAL end |
.to_p2sh_multisig_script(m, pubkeys) ⇒ Script
generate m of n multisig p2sh script
30 31 32 33 |
# File 'lib/bitcoin/script/script.rb', line 30 def self.to_p2sh_multisig_script(m, pubkeys) redeem_script = to_multisig_script(m, pubkeys) [redeem_script.to_p2sh, redeem_script] end |
.to_p2wpkh(pubkey_hash) ⇒ Object
generate P2WPKH script
22 23 24 |
# File 'lib/bitcoin/script/script.rb', line 22 def self.to_p2wpkh(pubkey_hash) new << WITNESS_VERSION << pubkey_hash end |
.to_p2wsh(redeem_script) ⇒ Object
generate p2wsh script for redeem_script
65 66 67 |
# File 'lib/bitcoin/script/script.rb', line 65 def self.to_p2wsh(redeem_script) new << WITNESS_VERSION << redeem_script.to_sha256 end |
Instance Method Details
#<<(obj) ⇒ Object
append object to payload
267 268 269 270 271 272 273 274 275 276 |
# File 'lib/bitcoin/script/script.rb', line 267 def <<(obj) if obj.is_a?(Integer) push_int(obj) elsif obj.is_a?(String) append_data(obj) elsif obj.is_a?(Array) obj.each { |o| self.<< o} self end end |
#==(other) ⇒ Object
474 475 476 477 |
# File 'lib/bitcoin/script/script.rb', line 474 def ==(other) return false unless other chunks == other.chunks end |
#addresses ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/bitcoin/script/script.rb', line 155 def addresses return [p2pkh_addr] if p2pkh? return [p2sh_addr] if p2sh? return [bech32_addr] if witness_program? return get_multisig_pubkeys.map{|pubkey| Bitcoin::Key.new(pubkey: pubkey.bth).to_p2pkh} if multisig? [] end |
#append_data(data) ⇒ Script
append data to payload with pushdata opcode
301 302 303 304 305 |
# File 'lib/bitcoin/script/script.rb', line 301 def append_data(data) data = Encoding::ASCII_8BIT == data.encoding ? data : data.htb chunks << Bitcoin::Script.pack_pushdata(data) self end |
#append_opcode(opcode) ⇒ Script
append opcode to payload
291 292 293 294 295 296 |
# File 'lib/bitcoin/script/script.rb', line 291 def append_opcode(opcode) opcode = Opcodes.small_int_to_opcode(opcode) if -1 <= opcode && opcode <= 16 raise ArgumentError, "specified invalid opcode #{opcode}." unless Opcodes.defined?(opcode) chunks << opcode.chr self end |
#delete_opcode(opcode) ⇒ Object
remove all occurences of opcode. Typically it’s OP_CODESEPARATOR.
456 457 458 459 |
# File 'lib/bitcoin/script/script.rb', line 456 def delete_opcode(opcode) @chunks = chunks.select{|chunk| chunk.ord != opcode} self end |
#empty? ⇒ Boolean
151 152 153 |
# File 'lib/bitcoin/script/script.rb', line 151 def empty? chunks.size == 0 end |
#find_and_delete(subscript) ⇒ Object
removes chunks matching subscript byte-for-byte and returns as a new object.
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/bitcoin/script/script.rb', line 422 def find_and_delete(subscript) raise ArgumentError, 'subscript must be Bitcoin::Script' unless subscript.is_a?(Script) return self if subscript.chunks.empty? buf = [] i = 0 result = Script.new chunks.each do |chunk| sub_chunk = subscript.chunks[i] if chunk.start_with?(sub_chunk) if chunk == sub_chunk buf << chunk i += 1 (i = 0; buf.clear) if i == subscript.chunks.size # matched the whole subscript else # matched the part of head i = 0 tmp = chunk.dup tmp.slice!(sub_chunk) result.chunks << tmp end else result.chunks << buf.join unless buf.empty? if buf.first == chunk i = 1 buf = [chunk] else i = 0 result.chunks << chunk end end end result end |
#get_multisig_pubkeys ⇒ Object
48 49 50 51 |
# File 'lib/bitcoin/script/script.rb', line 48 def get_multisig_pubkeys num = Bitcoin::Opcodes.opcode_to_small_int(chunks[-2].bth.to_i(16)) (1..num).map{ |i| chunks[i].pushed_data } end |
#include?(item) ⇒ Boolean
Check the item is in the chunk of the script.
308 309 310 311 312 313 314 315 316 317 |
# File 'lib/bitcoin/script/script.rb', line 308 def include?(item) chunk_item = if item.is_a?(Integer) item.chr elsif item.is_a?(String) data = Encoding::ASCII_8BIT == item.encoding ? item : item.htb Bitcoin::Script.pack_pushdata(data) end return false unless chunk_item chunks.include?(chunk_item) end |
#multisig? ⇒ Boolean
191 192 193 194 195 196 197 |
# File 'lib/bitcoin/script/script.rb', line 191 def multisig? return false if chunks.size < 4 || chunks.last.ord != OP_CHECKMULTISIG pubkey_count = Opcodes.opcode_to_small_int(chunks[-2].opcode) sig_count = Opcodes.opcode_to_small_int(chunks[0].opcode) return false unless pubkey_count || sig_count sig_count <= pubkey_count end |
#op_return? ⇒ Boolean
199 200 201 |
# File 'lib/bitcoin/script/script.rb', line 199 def op_return? chunks.size >= 1 && chunks[0].ord == OP_RETURN end |
#op_return_data ⇒ Object
208 209 210 211 212 |
# File 'lib/bitcoin/script/script.rb', line 208 def op_return_data return nil unless op_return? return nil if chunks.size == 1 chunks[1].pushed_data end |
#p2pkh? ⇒ Boolean
whether this script is a P2PKH format script.
169 170 171 172 173 |
# File 'lib/bitcoin/script/script.rb', line 169 def p2pkh? return false unless chunks.size == 5 [OP_DUP, OP_HASH160, OP_EQUALVERIFY, OP_CHECKSIG] == (chunks[0..1]+ chunks[3..4]).map(&:ord) && chunks[2].bytesize == 21 end |
#p2sh? ⇒ Boolean
186 187 188 189 |
# File 'lib/bitcoin/script/script.rb', line 186 def p2sh? return false unless chunks.size == 3 OP_HASH160 == chunks[0].ord && OP_EQUAL == chunks[2].ord && chunks[1].bytesize == 21 end |
#p2wpkh? ⇒ Boolean
whether this script is a P2WPKH format script.
176 177 178 179 |
# File 'lib/bitcoin/script/script.rb', line 176 def p2wpkh? return false unless chunks.size == 2 chunks[0].ord == WITNESS_VERSION && chunks[1].bytesize == 21 end |
#p2wsh? ⇒ Boolean
181 182 183 184 |
# File 'lib/bitcoin/script/script.rb', line 181 def p2wsh? return false unless chunks.size == 2 chunks[0].ord == WITNESS_VERSION && chunks[1].bytesize == 33 end |
#push_int(n) ⇒ Object
push integer to stack.
279 280 281 282 283 284 285 286 |
# File 'lib/bitcoin/script/script.rb', line 279 def push_int(n) begin append_opcode(n) rescue ArgumentError append_data(Script.encode_number(n)) end self end |
#push_only? ⇒ Boolean
whether data push only script which dose not include other opcode
215 216 217 218 219 220 |
# File 'lib/bitcoin/script/script.rb', line 215 def push_only? chunks.each do |c| return false if !c.opcode.nil? && c.opcode > OP_16 end true end |
#run ⇒ Object
execute script interpreter using this script for development.
361 362 363 |
# File 'lib/bitcoin/script/script.rb', line 361 def run Bitcoin::ScriptInterpreter.eval(Bitcoin::Script.new, self.dup) end |
#size ⇒ Object
script size
356 357 358 |
# File 'lib/bitcoin/script/script.rb', line 356 def size to_payload.bytesize end |
#standard? ⇒ Boolean
check whether standard script.
164 165 166 |
# File 'lib/bitcoin/script/script.rb', line 164 def standard? p2pkh? | p2sh? | p2wpkh? | p2wsh? | multisig? | standard_op_return? end |
#standard_op_return? ⇒ Boolean
203 204 205 206 |
# File 'lib/bitcoin/script/script.rb', line 203 def standard_op_return? op_return? && size <= MAX_OP_RETURN_RELAY && (chunks.size == 1 || chunks[1].opcode <= OP_16) end |
#subscript(*args) ⇒ Object
subscript this script to the specified range.
415 416 417 418 419 |
# File 'lib/bitcoin/script/script.rb', line 415 def subscript(*args) s = self.class.new s.chunks = chunks[*args] s end |
#subscript_codeseparator(separator_index) ⇒ Object
Returns a script that deleted the script before the index specified by separator_index.
462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/bitcoin/script/script.rb', line 462 def subscript_codeseparator(separator_index) buf = [] process_separator_index = 0 chunks.each{|chunk| buf << chunk if process_separator_index == separator_index if chunk.ord == OP_CODESEPARATOR && process_separator_index < separator_index process_separator_index += 1 end } buf.join end |
#to_h ⇒ Object
488 489 490 491 492 493 494 495 496 |
# File 'lib/bitcoin/script/script.rb', line 488 def to_h h = {asm: to_s, hex: to_payload.bth, type: type} addrs = addresses unless addrs.empty? h[:req_sigs] = multisig? ? Bitcoin::Opcodes.opcode_to_small_int(chunks[0].bth.to_i(16)) :addrs.size h[:addresses] = addrs end h end |
#to_hash160 ⇒ Object
generate hash160 hash for payload
351 352 353 |
# File 'lib/bitcoin/script/script.rb', line 351 def to_hash160 Bitcoin.hash160(to_payload.bth) end |
#to_hex ⇒ Object
147 148 149 |
# File 'lib/bitcoin/script/script.rb', line 147 def to_hex to_payload.bth end |
#to_p2sh ⇒ Script
generate p2sh script with this as a redeem script
44 45 46 |
# File 'lib/bitcoin/script/script.rb', line 44 def to_p2sh Script.to_p2sh(to_hash160) end |
#to_payload ⇒ Object
143 144 145 |
# File 'lib/bitcoin/script/script.rb', line 143 def to_payload chunks.join end |
#to_s ⇒ Object
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/bitcoin/script/script.rb', line 319 def to_s chunks.map { |c| case c when Integer opcode_to_name(c) when String if c.pushdata? v = Opcodes.opcode_to_small_int(c.ord) if v v else data = c.pushed_data if data.bytesize <= 4 Script.decode_number(data.bth) # for scriptnum else data.bth end end else opcode = Opcodes.opcode_to_name(c.ord) opcode ? opcode : 'OP_UNKNOWN [error]' end end }.join(' ') end |
#to_script_code(skip_separator_index = 0) ⇒ Object
If this script is witness program, return its script code, otherwise returns the self payload. ScriptInterpreter does not use this.
249 250 251 252 253 254 255 256 257 |
# File 'lib/bitcoin/script/script.rb', line 249 def to_script_code(skip_separator_index = 0) payload = to_payload if p2wpkh? payload = Script.to_p2pkh(chunks[1].pushed_data.bth).to_payload elsif skip_separator_index > 0 payload = subscript_codeseparator(skip_separator_index) end Bitcoin.pack_var_string(payload) end |
#to_sha256 ⇒ Object
generate sha-256 hash for payload
346 347 348 |
# File 'lib/bitcoin/script/script.rb', line 346 def to_sha256 Bitcoin.sha256(to_payload).bth end |
#type ⇒ Object
479 480 481 482 483 484 485 486 |
# File 'lib/bitcoin/script/script.rb', line 479 def type return 'pubkeyhash' if p2pkh? return 'scripthash' if p2sh? return 'multisig' if multisig? return 'witness_v0_keyhash' if p2wpkh? return 'witness_v0_scripthash' if p2wsh? 'nonstandard' end |
#witness_commitment ⇒ Object
get witness commitment
240 241 242 243 244 245 |
# File 'lib/bitcoin/script/script.rb', line 240 def witness_commitment return nil if !op_return? || op_return_data.bytesize < 36 buf = StringIO.new(op_return_data) return nil unless buf.read(4).bth == WITNESS_COMMITMENT_HEADER buf.read(32).bth end |
#witness_data ⇒ Object
get witness version and witness program
260 261 262 263 264 |
# File 'lib/bitcoin/script/script.rb', line 260 def witness_data version = opcode_to_small_int(chunks[0].opcode) program = chunks[1].pushed_data [version, program] end |
#witness_program? ⇒ Boolean
A witness program is any valid Script that consists of a 1-byte push opcode followed by a data push between 2 and 40 bytes.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/bitcoin/script/script.rb', line 223 def witness_program? return false if size < 4 || size > 42 || chunks.size < 2 opcode = chunks[0].opcode return false if opcode != OP_0 && (opcode < OP_1 || opcode > OP_16) return false unless chunks[1].pushdata? if size == (chunks[1][0].unpack('C').first + 2) program_size = chunks[1].pushed_data.bytesize return program_size >= 2 && program_size <= 40 end false end |