Class: BTC::ScriptChunk
- Inherits:
-
Object
- Object
- BTC::ScriptChunk
- Includes:
- Opcodes
- Defined in:
- lib/btcruby/script/script_chunk.rb
Overview
ScriptChunk represents either an opcode or a pushdata command.
Constant Summary
Constants included from Opcodes
Opcodes::OPCODE_NAME_TO_VALUE, Opcodes::OPCODE_VALUE_TO_NAME, 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_CHECKLOCKTIMEVERIFY, 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_FALSE, 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_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_TRUE, 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
-
#opcode ⇒ Object
readonly
Opcode for this chunk (first byte of the raw_data).
-
#pushdata ⇒ Object
readonly
If opcode is OP_PUSHDATA*, contains pure data being pushed.
-
#raw_data ⇒ Object
readonly
Raw data for this chunk.
-
#size ⇒ Object
readonly
Length of raw_data in bytes.
Class Method Summary collapse
-
.with_data(data, offset: 0) ⇒ Object
Parses the chunk with binary data.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#canonical? ⇒ Boolean
Returns true if this chunk is in canonical form (the most compact one).
- #data_only? ⇒ Boolean
-
#initialize(raw_data) ⇒ ScriptChunk
constructor
A new instance of ScriptChunk.
-
#interpreted_data ⇒ Object
Returns corresponding data for data_only opcodes: pushdata or OP_N-encoded numbers.
-
#length ⇒ Object
Length of raw_data in bytes.
-
#opcode? ⇒ Boolean
Returns true if this is a non-pushdata (also not OP_0) opcode.
-
#pushdata? ⇒ Boolean
Returns true if this is a pushdata chunk (or OP_0 opcode).
- #to_s ⇒ Object
Constructor Details
#initialize(raw_data) ⇒ ScriptChunk
199 200 201 |
# File 'lib/btcruby/script/script_chunk.rb', line 199 def initialize(raw_data) @raw_data = raw_data end |
Instance Attribute Details
#opcode ⇒ Object (readonly)
Opcode for this chunk (first byte of the raw_data).
12 13 14 |
# File 'lib/btcruby/script/script_chunk.rb', line 12 def opcode @opcode end |
#pushdata ⇒ Object (readonly)
If opcode is OP_PUSHDATA*, contains pure data being pushed. If opcode is OP_0, returns an empty string. If opcode is not pushdata, returns nil.
17 18 19 |
# File 'lib/btcruby/script/script_chunk.rb', line 17 def pushdata @pushdata end |
#raw_data ⇒ Object (readonly)
Raw data for this chunk. 1 byte for regular opcode, 1 or more bytes for pushdata command. We do not call it ‘data’ to avoid confusion with pushdata (see below). The encoding is guaranteed to be binary.
9 10 11 |
# File 'lib/btcruby/script/script_chunk.rb', line 9 def raw_data @raw_data end |
#size ⇒ Object (readonly)
Length of raw_data in bytes.
20 21 22 |
# File 'lib/btcruby/script/script_chunk.rb', line 20 def size @size end |
Class Method Details
.with_data(data, offset: 0) ⇒ Object
Parses the chunk with binary data. Assumes the encoding is binary.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/btcruby/script/script_chunk.rb', line 143 def self.with_data(data, offset: 0) raise ArgumentError, "Data is missing" if !data opcode, _ = BTC::WireFormat.read_uint8(data: data, offset: offset) raise ArgumentError, "Failed to read opcode of the script chunk" if !opcode # push data opcode if opcode <= OP_PUSHDATA4 length = data.bytesize if opcode < OP_PUSHDATA1 pushdata_length = opcode chunk_length = 1 + pushdata_length if offset + chunk_length > length raise ArgumentError, "PUSHDATA is longer than we have bytes available" end return self.new(data[offset, chunk_length]) elsif opcode == OP_PUSHDATA1 pushdata_length, _ = BTC::WireFormat.read_uint8(data: data, offset: offset + 1) if !pushdata_length raise ArgumentError, "Failed to read length for PUSHDATA1" end chunk_length = 1 + 1 + pushdata_length if offset + chunk_length > length raise ArgumentError, "PUSHDATA1 is longer than we have bytes available" end return self.new(data[offset, chunk_length]) elsif (opcode == OP_PUSHDATA2) pushdata_length, _ = BTC::WireFormat.read_uint16le(data: data, offset: offset + 1) if !pushdata_length raise ArgumentError, "Failed to read length for PUSHDATA2" end chunk_length = 1 + 2 + pushdata_length if offset + chunk_length > length raise ArgumentError, "PUSHDATA2 is longer than we have bytes available" end return self.new(data[offset, chunk_length]) elsif (opcode == OP_PUSHDATA4) pushdata_length, _ = BTC::WireFormat.read_uint32le(data: data, offset: offset + 1) if !pushdata_length raise ArgumentError, "Failed to read length for PUSHDATA4" end chunk_length = 1 + 4 + pushdata_length if offset + chunk_length > length raise ArgumentError, "PUSHDATA4 is longer than we have bytes available" end return self.new(data[offset, chunk_length]) end else # simple opcode - 1 byte return self.new(data[offset, 1]) end end |
Instance Method Details
#==(other) ⇒ Object
203 204 205 |
# File 'lib/btcruby/script/script_chunk.rb', line 203 def ==(other) @raw_data == other.raw_data end |
#canonical? ⇒ Boolean
Returns true if this chunk is in canonical form (the most compact one). Returns false if it contains pushdata with too big length prefix. Example of non-canonical chunk: 75 bytes pushed with OP_PUSHDATA1 instead of simple 0x4b prefix. Note: this is not as strict as check_minimal_push in ScriptInterpreter.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/btcruby/script/script_chunk.rb', line 44 def canonical? opcode = self.opcode if opcode < OP_PUSHDATA1 return true # most compact pushdata is always canonical. elsif opcode == OP_PUSHDATA1 return (self.raw_data.bytesize - (1+1)) >= OP_PUSHDATA1 elsif opcode == OP_PUSHDATA2 return (self.raw_data.bytesize - (1+2)) > 0xff elsif opcode == OP_PUSHDATA4 return (self.raw_data.bytesize - (1+4)) > 0xffff else return true # all other opcodes are canonical (just 1 byte code) end end |
#data_only? ⇒ Boolean
35 36 37 |
# File 'lib/btcruby/script/script_chunk.rb', line 35 def data_only? opcode <= OP_16 end |
#interpreted_data ⇒ Object
Returns corresponding data for data_only opcodes: pushdata or OP_N-encoded numbers. For all other opcodes returns nil.
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/btcruby/script/script_chunk.rb', line 82 def interpreted_data if d = pushdata return d end opcode = self.opcode if opcode == OP_1NEGATE || (opcode >= OP_1 && opcode <= OP_16) ScriptNumber.new(integer: opcode - OP_1 + 1).data else nil end end |
#length ⇒ Object
Length of raw_data in bytes.
21 22 23 |
# File 'lib/btcruby/script/script_chunk.rb', line 21 def size @size end |
#opcode? ⇒ Boolean
Returns true if this is a non-pushdata (also not OP_0) opcode.
24 25 26 |
# File 'lib/btcruby/script/script_chunk.rb', line 24 def opcode? !pushdata? end |
#pushdata? ⇒ Boolean
Returns true if this is a pushdata chunk (or OP_0 opcode).
29 30 31 32 33 |
# File 'lib/btcruby/script/script_chunk.rb', line 29 def pushdata? # Compact pushdata opcodes are "virtual", just length prefixes. # Attention: OP_0 is also "pushdata" code that pushes empty data. opcode <= OP_PUSHDATA4 end |
#to_s ⇒ Object
98 99 100 101 102 103 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 |
# File 'lib/btcruby/script/script_chunk.rb', line 98 def to_s opcode = self.opcode if self.opcode? return "OP_0" if opcode == OP_0 return "OP_1NEGATE" if opcode == OP_1NEGATE if opcode >= OP_1 && opcode <= OP_16 return "OP_#{opcode + 1 - OP_1}" end return Opcode.name_for_opcode(opcode) end pushdata = self.pushdata return "OP_0" if pushdata.bytesize == 0 # Empty data is encoded as OP_0. string = "" # If it's some weird readable string, show it as a readable string. if data_is_ascii_printable?(pushdata) && (pushdata.bytesize < 20 || pushdata.bytesize > 65) string = pushdata.encode(Encoding::ASCII) # Escape escapes & single quote characters. string.gsub!("\\", "\\\\") string.gsub!("'", "\\'") # Wrap in single quotes. Why not double? Because they are already used in JSON and we don't want to multiply the mess. string = "'#{string}'" else string = BTC.to_hex(pushdata) # Shorter than 128-bit chunks are wrapped in square brackets to avoid ambiguity with big all-decimal numbers. if (pushdata.bytesize < 16) string = "[#{string}]" end end # Pushdata with non-compact encoding will have explicit length prefix (1 for OP_PUSHDATA1, 2 for OP_PUSHDATA2 and 4 for OP_PUSHDATA4). if !canonical? prefix = 1 prefix = 2 if opcode == OP_PUSHDATA2 prefix = 4 if opcode == OP_PUSHDATA4 string = "#{prefix}:#{string}" end return string end |