github.com/lbryio/lbcd@v0.22.119/txscript/opcode.go (about) 1 // Copyright (c) 2013-2017 The btcsuite developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package txscript 6 7 import ( 8 "bytes" 9 "crypto/sha1" 10 "crypto/sha256" 11 "encoding/hex" 12 "fmt" 13 "hash" 14 "strings" 15 16 "golang.org/x/crypto/ripemd160" 17 18 "github.com/lbryio/lbcd/btcec" 19 "github.com/lbryio/lbcd/chaincfg/chainhash" 20 "github.com/lbryio/lbcd/wire" 21 ) 22 23 // An opcode defines the information related to a txscript opcode. opfunc, if 24 // present, is the function to call to perform the opcode on the script. The 25 // current script is passed in as a slice with the first member being the opcode 26 // itself. 27 type opcode struct { 28 value byte 29 name string 30 length int 31 opfunc func(*opcode, []byte, *Engine) error 32 } 33 34 // These constants are the values of the official opcodes used on the btc wiki, 35 // in bitcoin core and in most if not all other references and software related 36 // to handling BTC scripts. 37 const ( 38 OP_0 = 0x00 // 0 39 OP_FALSE = 0x00 // 0 - AKA OP_0 40 OP_DATA_1 = 0x01 // 1 41 OP_DATA_2 = 0x02 // 2 42 OP_DATA_3 = 0x03 // 3 43 OP_DATA_4 = 0x04 // 4 44 OP_DATA_5 = 0x05 // 5 45 OP_DATA_6 = 0x06 // 6 46 OP_DATA_7 = 0x07 // 7 47 OP_DATA_8 = 0x08 // 8 48 OP_DATA_9 = 0x09 // 9 49 OP_DATA_10 = 0x0a // 10 50 OP_DATA_11 = 0x0b // 11 51 OP_DATA_12 = 0x0c // 12 52 OP_DATA_13 = 0x0d // 13 53 OP_DATA_14 = 0x0e // 14 54 OP_DATA_15 = 0x0f // 15 55 OP_DATA_16 = 0x10 // 16 56 OP_DATA_17 = 0x11 // 17 57 OP_DATA_18 = 0x12 // 18 58 OP_DATA_19 = 0x13 // 19 59 OP_DATA_20 = 0x14 // 20 60 OP_DATA_21 = 0x15 // 21 61 OP_DATA_22 = 0x16 // 22 62 OP_DATA_23 = 0x17 // 23 63 OP_DATA_24 = 0x18 // 24 64 OP_DATA_25 = 0x19 // 25 65 OP_DATA_26 = 0x1a // 26 66 OP_DATA_27 = 0x1b // 27 67 OP_DATA_28 = 0x1c // 28 68 OP_DATA_29 = 0x1d // 29 69 OP_DATA_30 = 0x1e // 30 70 OP_DATA_31 = 0x1f // 31 71 OP_DATA_32 = 0x20 // 32 72 OP_DATA_33 = 0x21 // 33 73 OP_DATA_34 = 0x22 // 34 74 OP_DATA_35 = 0x23 // 35 75 OP_DATA_36 = 0x24 // 36 76 OP_DATA_37 = 0x25 // 37 77 OP_DATA_38 = 0x26 // 38 78 OP_DATA_39 = 0x27 // 39 79 OP_DATA_40 = 0x28 // 40 80 OP_DATA_41 = 0x29 // 41 81 OP_DATA_42 = 0x2a // 42 82 OP_DATA_43 = 0x2b // 43 83 OP_DATA_44 = 0x2c // 44 84 OP_DATA_45 = 0x2d // 45 85 OP_DATA_46 = 0x2e // 46 86 OP_DATA_47 = 0x2f // 47 87 OP_DATA_48 = 0x30 // 48 88 OP_DATA_49 = 0x31 // 49 89 OP_DATA_50 = 0x32 // 50 90 OP_DATA_51 = 0x33 // 51 91 OP_DATA_52 = 0x34 // 52 92 OP_DATA_53 = 0x35 // 53 93 OP_DATA_54 = 0x36 // 54 94 OP_DATA_55 = 0x37 // 55 95 OP_DATA_56 = 0x38 // 56 96 OP_DATA_57 = 0x39 // 57 97 OP_DATA_58 = 0x3a // 58 98 OP_DATA_59 = 0x3b // 59 99 OP_DATA_60 = 0x3c // 60 100 OP_DATA_61 = 0x3d // 61 101 OP_DATA_62 = 0x3e // 62 102 OP_DATA_63 = 0x3f // 63 103 OP_DATA_64 = 0x40 // 64 104 OP_DATA_65 = 0x41 // 65 105 OP_DATA_66 = 0x42 // 66 106 OP_DATA_67 = 0x43 // 67 107 OP_DATA_68 = 0x44 // 68 108 OP_DATA_69 = 0x45 // 69 109 OP_DATA_70 = 0x46 // 70 110 OP_DATA_71 = 0x47 // 71 111 OP_DATA_72 = 0x48 // 72 112 OP_DATA_73 = 0x49 // 73 113 OP_DATA_74 = 0x4a // 74 114 OP_DATA_75 = 0x4b // 75 115 OP_PUSHDATA1 = 0x4c // 76 116 OP_PUSHDATA2 = 0x4d // 77 117 OP_PUSHDATA4 = 0x4e // 78 118 OP_1NEGATE = 0x4f // 79 119 OP_RESERVED = 0x50 // 80 120 OP_1 = 0x51 // 81 - AKA OP_TRUE 121 OP_TRUE = 0x51 // 81 122 OP_2 = 0x52 // 82 123 OP_3 = 0x53 // 83 124 OP_4 = 0x54 // 84 125 OP_5 = 0x55 // 85 126 OP_6 = 0x56 // 86 127 OP_7 = 0x57 // 87 128 OP_8 = 0x58 // 88 129 OP_9 = 0x59 // 89 130 OP_10 = 0x5a // 90 131 OP_11 = 0x5b // 91 132 OP_12 = 0x5c // 92 133 OP_13 = 0x5d // 93 134 OP_14 = 0x5e // 94 135 OP_15 = 0x5f // 95 136 OP_16 = 0x60 // 96 137 OP_NOP = 0x61 // 97 138 OP_VER = 0x62 // 98 139 OP_IF = 0x63 // 99 140 OP_NOTIF = 0x64 // 100 141 OP_VERIF = 0x65 // 101 142 OP_VERNOTIF = 0x66 // 102 143 OP_ELSE = 0x67 // 103 144 OP_ENDIF = 0x68 // 104 145 OP_VERIFY = 0x69 // 105 146 OP_RETURN = 0x6a // 106 147 OP_TOALTSTACK = 0x6b // 107 148 OP_FROMALTSTACK = 0x6c // 108 149 OP_2DROP = 0x6d // 109 150 OP_2DUP = 0x6e // 110 151 OP_3DUP = 0x6f // 111 152 OP_2OVER = 0x70 // 112 153 OP_2ROT = 0x71 // 113 154 OP_2SWAP = 0x72 // 114 155 OP_IFDUP = 0x73 // 115 156 OP_DEPTH = 0x74 // 116 157 OP_DROP = 0x75 // 117 158 OP_DUP = 0x76 // 118 159 OP_NIP = 0x77 // 119 160 OP_OVER = 0x78 // 120 161 OP_PICK = 0x79 // 121 162 OP_ROLL = 0x7a // 122 163 OP_ROT = 0x7b // 123 164 OP_SWAP = 0x7c // 124 165 OP_TUCK = 0x7d // 125 166 OP_CAT = 0x7e // 126 167 OP_SUBSTR = 0x7f // 127 168 OP_LEFT = 0x80 // 128 169 OP_RIGHT = 0x81 // 129 170 OP_SIZE = 0x82 // 130 171 OP_INVERT = 0x83 // 131 172 OP_AND = 0x84 // 132 173 OP_OR = 0x85 // 133 174 OP_XOR = 0x86 // 134 175 OP_EQUAL = 0x87 // 135 176 OP_EQUALVERIFY = 0x88 // 136 177 OP_RESERVED1 = 0x89 // 137 178 OP_RESERVED2 = 0x8a // 138 179 OP_1ADD = 0x8b // 139 180 OP_1SUB = 0x8c // 140 181 OP_2MUL = 0x8d // 141 182 OP_2DIV = 0x8e // 142 183 OP_NEGATE = 0x8f // 143 184 OP_ABS = 0x90 // 144 185 OP_NOT = 0x91 // 145 186 OP_0NOTEQUAL = 0x92 // 146 187 OP_ADD = 0x93 // 147 188 OP_SUB = 0x94 // 148 189 OP_MUL = 0x95 // 149 190 OP_DIV = 0x96 // 150 191 OP_MOD = 0x97 // 151 192 OP_LSHIFT = 0x98 // 152 193 OP_RSHIFT = 0x99 // 153 194 OP_BOOLAND = 0x9a // 154 195 OP_BOOLOR = 0x9b // 155 196 OP_NUMEQUAL = 0x9c // 156 197 OP_NUMEQUALVERIFY = 0x9d // 157 198 OP_NUMNOTEQUAL = 0x9e // 158 199 OP_LESSTHAN = 0x9f // 159 200 OP_GREATERTHAN = 0xa0 // 160 201 OP_LESSTHANOREQUAL = 0xa1 // 161 202 OP_GREATERTHANOREQUAL = 0xa2 // 162 203 OP_MIN = 0xa3 // 163 204 OP_MAX = 0xa4 // 164 205 OP_WITHIN = 0xa5 // 165 206 OP_RIPEMD160 = 0xa6 // 166 207 OP_SHA1 = 0xa7 // 167 208 OP_SHA256 = 0xa8 // 168 209 OP_HASH160 = 0xa9 // 169 210 OP_HASH256 = 0xaa // 170 211 OP_CODESEPARATOR = 0xab // 171 212 OP_CHECKSIG = 0xac // 172 213 OP_CHECKSIGVERIFY = 0xad // 173 214 OP_CHECKMULTISIG = 0xae // 174 215 OP_CHECKMULTISIGVERIFY = 0xaf // 175 216 OP_NOP1 = 0xb0 // 176 217 OP_NOP2 = 0xb1 // 177 218 OP_CHECKLOCKTIMEVERIFY = 0xb1 // 177 - AKA OP_NOP2 219 OP_NOP3 = 0xb2 // 178 220 OP_CHECKSEQUENCEVERIFY = 0xb2 // 178 - AKA OP_NOP3 221 OP_NOP4 = 0xb3 // 179 222 OP_NOP5 = 0xb4 // 180 223 OP_CLAIMNAME = 0xb5 // 181 - AKA OP_NOP6 224 OP_SUPPORTCLAIM = 0xb6 // 182 - AKA OP_NOP7 225 OP_UPDATECLAIM = 0xb7 // 183 - AKA OP_NOP8 226 OP_NOP9 = 0xb8 // 184 227 OP_NOP10 = 0xb9 // 185 228 OP_UNKNOWN186 = 0xba // 186 229 OP_UNKNOWN187 = 0xbb // 187 230 OP_UNKNOWN188 = 0xbc // 188 231 OP_UNKNOWN189 = 0xbd // 189 232 OP_UNKNOWN190 = 0xbe // 190 233 OP_UNKNOWN191 = 0xbf // 191 234 OP_UNKNOWN192 = 0xc0 // 192 235 OP_UNKNOWN193 = 0xc1 // 193 236 OP_UNKNOWN194 = 0xc2 // 194 237 OP_UNKNOWN195 = 0xc3 // 195 238 OP_UNKNOWN196 = 0xc4 // 196 239 OP_UNKNOWN197 = 0xc5 // 197 240 OP_UNKNOWN198 = 0xc6 // 198 241 OP_UNKNOWN199 = 0xc7 // 199 242 OP_UNKNOWN200 = 0xc8 // 200 243 OP_UNKNOWN201 = 0xc9 // 201 244 OP_UNKNOWN202 = 0xca // 202 245 OP_UNKNOWN203 = 0xcb // 203 246 OP_UNKNOWN204 = 0xcc // 204 247 OP_UNKNOWN205 = 0xcd // 205 248 OP_UNKNOWN206 = 0xce // 206 249 OP_UNKNOWN207 = 0xcf // 207 250 OP_UNKNOWN208 = 0xd0 // 208 251 OP_UNKNOWN209 = 0xd1 // 209 252 OP_UNKNOWN210 = 0xd2 // 210 253 OP_UNKNOWN211 = 0xd3 // 211 254 OP_UNKNOWN212 = 0xd4 // 212 255 OP_UNKNOWN213 = 0xd5 // 213 256 OP_UNKNOWN214 = 0xd6 // 214 257 OP_UNKNOWN215 = 0xd7 // 215 258 OP_UNKNOWN216 = 0xd8 // 216 259 OP_UNKNOWN217 = 0xd9 // 217 260 OP_UNKNOWN218 = 0xda // 218 261 OP_UNKNOWN219 = 0xdb // 219 262 OP_UNKNOWN220 = 0xdc // 220 263 OP_UNKNOWN221 = 0xdd // 221 264 OP_UNKNOWN222 = 0xde // 222 265 OP_UNKNOWN223 = 0xdf // 223 266 OP_UNKNOWN224 = 0xe0 // 224 267 OP_UNKNOWN225 = 0xe1 // 225 268 OP_UNKNOWN226 = 0xe2 // 226 269 OP_UNKNOWN227 = 0xe3 // 227 270 OP_UNKNOWN228 = 0xe4 // 228 271 OP_UNKNOWN229 = 0xe5 // 229 272 OP_UNKNOWN230 = 0xe6 // 230 273 OP_UNKNOWN231 = 0xe7 // 231 274 OP_UNKNOWN232 = 0xe8 // 232 275 OP_UNKNOWN233 = 0xe9 // 233 276 OP_UNKNOWN234 = 0xea // 234 277 OP_UNKNOWN235 = 0xeb // 235 278 OP_UNKNOWN236 = 0xec // 236 279 OP_UNKNOWN237 = 0xed // 237 280 OP_UNKNOWN238 = 0xee // 238 281 OP_UNKNOWN239 = 0xef // 239 282 OP_UNKNOWN240 = 0xf0 // 240 283 OP_UNKNOWN241 = 0xf1 // 241 284 OP_UNKNOWN242 = 0xf2 // 242 285 OP_UNKNOWN243 = 0xf3 // 243 286 OP_UNKNOWN244 = 0xf4 // 244 287 OP_UNKNOWN245 = 0xf5 // 245 288 OP_UNKNOWN246 = 0xf6 // 246 289 OP_UNKNOWN247 = 0xf7 // 247 290 OP_UNKNOWN248 = 0xf8 // 248 291 OP_UNKNOWN249 = 0xf9 // 249 292 OP_SMALLINTEGER = 0xfa // 250 - bitcoin core internal 293 OP_PUBKEYS = 0xfb // 251 - bitcoin core internal 294 OP_UNKNOWN252 = 0xfc // 252 295 OP_PUBKEYHASH = 0xfd // 253 - bitcoin core internal 296 OP_PUBKEY = 0xfe // 254 - bitcoin core internal 297 OP_INVALIDOPCODE = 0xff // 255 - bitcoin core internal 298 ) 299 300 // Conditional execution constants. 301 const ( 302 OpCondFalse = 0 303 OpCondTrue = 1 304 OpCondSkip = 2 305 ) 306 307 // opcodeArray holds details about all possible opcodes such as how many bytes 308 // the opcode and any associated data should take, its human-readable name, and 309 // the handler function. 310 var opcodeArray = [256]opcode{ 311 // Data push opcodes. 312 OP_FALSE: {OP_FALSE, "OP_0", 1, opcodeFalse}, 313 OP_DATA_1: {OP_DATA_1, "OP_DATA_1", 2, opcodePushData}, 314 OP_DATA_2: {OP_DATA_2, "OP_DATA_2", 3, opcodePushData}, 315 OP_DATA_3: {OP_DATA_3, "OP_DATA_3", 4, opcodePushData}, 316 OP_DATA_4: {OP_DATA_4, "OP_DATA_4", 5, opcodePushData}, 317 OP_DATA_5: {OP_DATA_5, "OP_DATA_5", 6, opcodePushData}, 318 OP_DATA_6: {OP_DATA_6, "OP_DATA_6", 7, opcodePushData}, 319 OP_DATA_7: {OP_DATA_7, "OP_DATA_7", 8, opcodePushData}, 320 OP_DATA_8: {OP_DATA_8, "OP_DATA_8", 9, opcodePushData}, 321 OP_DATA_9: {OP_DATA_9, "OP_DATA_9", 10, opcodePushData}, 322 OP_DATA_10: {OP_DATA_10, "OP_DATA_10", 11, opcodePushData}, 323 OP_DATA_11: {OP_DATA_11, "OP_DATA_11", 12, opcodePushData}, 324 OP_DATA_12: {OP_DATA_12, "OP_DATA_12", 13, opcodePushData}, 325 OP_DATA_13: {OP_DATA_13, "OP_DATA_13", 14, opcodePushData}, 326 OP_DATA_14: {OP_DATA_14, "OP_DATA_14", 15, opcodePushData}, 327 OP_DATA_15: {OP_DATA_15, "OP_DATA_15", 16, opcodePushData}, 328 OP_DATA_16: {OP_DATA_16, "OP_DATA_16", 17, opcodePushData}, 329 OP_DATA_17: {OP_DATA_17, "OP_DATA_17", 18, opcodePushData}, 330 OP_DATA_18: {OP_DATA_18, "OP_DATA_18", 19, opcodePushData}, 331 OP_DATA_19: {OP_DATA_19, "OP_DATA_19", 20, opcodePushData}, 332 OP_DATA_20: {OP_DATA_20, "OP_DATA_20", 21, opcodePushData}, 333 OP_DATA_21: {OP_DATA_21, "OP_DATA_21", 22, opcodePushData}, 334 OP_DATA_22: {OP_DATA_22, "OP_DATA_22", 23, opcodePushData}, 335 OP_DATA_23: {OP_DATA_23, "OP_DATA_23", 24, opcodePushData}, 336 OP_DATA_24: {OP_DATA_24, "OP_DATA_24", 25, opcodePushData}, 337 OP_DATA_25: {OP_DATA_25, "OP_DATA_25", 26, opcodePushData}, 338 OP_DATA_26: {OP_DATA_26, "OP_DATA_26", 27, opcodePushData}, 339 OP_DATA_27: {OP_DATA_27, "OP_DATA_27", 28, opcodePushData}, 340 OP_DATA_28: {OP_DATA_28, "OP_DATA_28", 29, opcodePushData}, 341 OP_DATA_29: {OP_DATA_29, "OP_DATA_29", 30, opcodePushData}, 342 OP_DATA_30: {OP_DATA_30, "OP_DATA_30", 31, opcodePushData}, 343 OP_DATA_31: {OP_DATA_31, "OP_DATA_31", 32, opcodePushData}, 344 OP_DATA_32: {OP_DATA_32, "OP_DATA_32", 33, opcodePushData}, 345 OP_DATA_33: {OP_DATA_33, "OP_DATA_33", 34, opcodePushData}, 346 OP_DATA_34: {OP_DATA_34, "OP_DATA_34", 35, opcodePushData}, 347 OP_DATA_35: {OP_DATA_35, "OP_DATA_35", 36, opcodePushData}, 348 OP_DATA_36: {OP_DATA_36, "OP_DATA_36", 37, opcodePushData}, 349 OP_DATA_37: {OP_DATA_37, "OP_DATA_37", 38, opcodePushData}, 350 OP_DATA_38: {OP_DATA_38, "OP_DATA_38", 39, opcodePushData}, 351 OP_DATA_39: {OP_DATA_39, "OP_DATA_39", 40, opcodePushData}, 352 OP_DATA_40: {OP_DATA_40, "OP_DATA_40", 41, opcodePushData}, 353 OP_DATA_41: {OP_DATA_41, "OP_DATA_41", 42, opcodePushData}, 354 OP_DATA_42: {OP_DATA_42, "OP_DATA_42", 43, opcodePushData}, 355 OP_DATA_43: {OP_DATA_43, "OP_DATA_43", 44, opcodePushData}, 356 OP_DATA_44: {OP_DATA_44, "OP_DATA_44", 45, opcodePushData}, 357 OP_DATA_45: {OP_DATA_45, "OP_DATA_45", 46, opcodePushData}, 358 OP_DATA_46: {OP_DATA_46, "OP_DATA_46", 47, opcodePushData}, 359 OP_DATA_47: {OP_DATA_47, "OP_DATA_47", 48, opcodePushData}, 360 OP_DATA_48: {OP_DATA_48, "OP_DATA_48", 49, opcodePushData}, 361 OP_DATA_49: {OP_DATA_49, "OP_DATA_49", 50, opcodePushData}, 362 OP_DATA_50: {OP_DATA_50, "OP_DATA_50", 51, opcodePushData}, 363 OP_DATA_51: {OP_DATA_51, "OP_DATA_51", 52, opcodePushData}, 364 OP_DATA_52: {OP_DATA_52, "OP_DATA_52", 53, opcodePushData}, 365 OP_DATA_53: {OP_DATA_53, "OP_DATA_53", 54, opcodePushData}, 366 OP_DATA_54: {OP_DATA_54, "OP_DATA_54", 55, opcodePushData}, 367 OP_DATA_55: {OP_DATA_55, "OP_DATA_55", 56, opcodePushData}, 368 OP_DATA_56: {OP_DATA_56, "OP_DATA_56", 57, opcodePushData}, 369 OP_DATA_57: {OP_DATA_57, "OP_DATA_57", 58, opcodePushData}, 370 OP_DATA_58: {OP_DATA_58, "OP_DATA_58", 59, opcodePushData}, 371 OP_DATA_59: {OP_DATA_59, "OP_DATA_59", 60, opcodePushData}, 372 OP_DATA_60: {OP_DATA_60, "OP_DATA_60", 61, opcodePushData}, 373 OP_DATA_61: {OP_DATA_61, "OP_DATA_61", 62, opcodePushData}, 374 OP_DATA_62: {OP_DATA_62, "OP_DATA_62", 63, opcodePushData}, 375 OP_DATA_63: {OP_DATA_63, "OP_DATA_63", 64, opcodePushData}, 376 OP_DATA_64: {OP_DATA_64, "OP_DATA_64", 65, opcodePushData}, 377 OP_DATA_65: {OP_DATA_65, "OP_DATA_65", 66, opcodePushData}, 378 OP_DATA_66: {OP_DATA_66, "OP_DATA_66", 67, opcodePushData}, 379 OP_DATA_67: {OP_DATA_67, "OP_DATA_67", 68, opcodePushData}, 380 OP_DATA_68: {OP_DATA_68, "OP_DATA_68", 69, opcodePushData}, 381 OP_DATA_69: {OP_DATA_69, "OP_DATA_69", 70, opcodePushData}, 382 OP_DATA_70: {OP_DATA_70, "OP_DATA_70", 71, opcodePushData}, 383 OP_DATA_71: {OP_DATA_71, "OP_DATA_71", 72, opcodePushData}, 384 OP_DATA_72: {OP_DATA_72, "OP_DATA_72", 73, opcodePushData}, 385 OP_DATA_73: {OP_DATA_73, "OP_DATA_73", 74, opcodePushData}, 386 OP_DATA_74: {OP_DATA_74, "OP_DATA_74", 75, opcodePushData}, 387 OP_DATA_75: {OP_DATA_75, "OP_DATA_75", 76, opcodePushData}, 388 OP_PUSHDATA1: {OP_PUSHDATA1, "OP_PUSHDATA1", -1, opcodePushData}, 389 OP_PUSHDATA2: {OP_PUSHDATA2, "OP_PUSHDATA2", -2, opcodePushData}, 390 OP_PUSHDATA4: {OP_PUSHDATA4, "OP_PUSHDATA4", -4, opcodePushData}, 391 OP_1NEGATE: {OP_1NEGATE, "OP_1NEGATE", 1, opcode1Negate}, 392 OP_RESERVED: {OP_RESERVED, "OP_RESERVED", 1, opcodeReserved}, 393 OP_TRUE: {OP_TRUE, "OP_1", 1, opcodeN}, 394 OP_2: {OP_2, "OP_2", 1, opcodeN}, 395 OP_3: {OP_3, "OP_3", 1, opcodeN}, 396 OP_4: {OP_4, "OP_4", 1, opcodeN}, 397 OP_5: {OP_5, "OP_5", 1, opcodeN}, 398 OP_6: {OP_6, "OP_6", 1, opcodeN}, 399 OP_7: {OP_7, "OP_7", 1, opcodeN}, 400 OP_8: {OP_8, "OP_8", 1, opcodeN}, 401 OP_9: {OP_9, "OP_9", 1, opcodeN}, 402 OP_10: {OP_10, "OP_10", 1, opcodeN}, 403 OP_11: {OP_11, "OP_11", 1, opcodeN}, 404 OP_12: {OP_12, "OP_12", 1, opcodeN}, 405 OP_13: {OP_13, "OP_13", 1, opcodeN}, 406 OP_14: {OP_14, "OP_14", 1, opcodeN}, 407 OP_15: {OP_15, "OP_15", 1, opcodeN}, 408 OP_16: {OP_16, "OP_16", 1, opcodeN}, 409 410 // Control opcodes. 411 OP_NOP: {OP_NOP, "OP_NOP", 1, opcodeNop}, 412 OP_VER: {OP_VER, "OP_VER", 1, opcodeReserved}, 413 OP_IF: {OP_IF, "OP_IF", 1, opcodeIf}, 414 OP_NOTIF: {OP_NOTIF, "OP_NOTIF", 1, opcodeNotIf}, 415 OP_VERIF: {OP_VERIF, "OP_VERIF", 1, opcodeReserved}, 416 OP_VERNOTIF: {OP_VERNOTIF, "OP_VERNOTIF", 1, opcodeReserved}, 417 OP_ELSE: {OP_ELSE, "OP_ELSE", 1, opcodeElse}, 418 OP_ENDIF: {OP_ENDIF, "OP_ENDIF", 1, opcodeEndif}, 419 OP_VERIFY: {OP_VERIFY, "OP_VERIFY", 1, opcodeVerify}, 420 OP_RETURN: {OP_RETURN, "OP_RETURN", 1, opcodeReturn}, 421 OP_CHECKLOCKTIMEVERIFY: {OP_CHECKLOCKTIMEVERIFY, "OP_CHECKLOCKTIMEVERIFY", 1, opcodeCheckLockTimeVerify}, 422 OP_CHECKSEQUENCEVERIFY: {OP_CHECKSEQUENCEVERIFY, "OP_CHECKSEQUENCEVERIFY", 1, opcodeCheckSequenceVerify}, 423 424 // Stack opcodes. 425 OP_TOALTSTACK: {OP_TOALTSTACK, "OP_TOALTSTACK", 1, opcodeToAltStack}, 426 OP_FROMALTSTACK: {OP_FROMALTSTACK, "OP_FROMALTSTACK", 1, opcodeFromAltStack}, 427 OP_2DROP: {OP_2DROP, "OP_2DROP", 1, opcode2Drop}, 428 OP_2DUP: {OP_2DUP, "OP_2DUP", 1, opcode2Dup}, 429 OP_3DUP: {OP_3DUP, "OP_3DUP", 1, opcode3Dup}, 430 OP_2OVER: {OP_2OVER, "OP_2OVER", 1, opcode2Over}, 431 OP_2ROT: {OP_2ROT, "OP_2ROT", 1, opcode2Rot}, 432 OP_2SWAP: {OP_2SWAP, "OP_2SWAP", 1, opcode2Swap}, 433 OP_IFDUP: {OP_IFDUP, "OP_IFDUP", 1, opcodeIfDup}, 434 OP_DEPTH: {OP_DEPTH, "OP_DEPTH", 1, opcodeDepth}, 435 OP_DROP: {OP_DROP, "OP_DROP", 1, opcodeDrop}, 436 OP_DUP: {OP_DUP, "OP_DUP", 1, opcodeDup}, 437 OP_NIP: {OP_NIP, "OP_NIP", 1, opcodeNip}, 438 OP_OVER: {OP_OVER, "OP_OVER", 1, opcodeOver}, 439 OP_PICK: {OP_PICK, "OP_PICK", 1, opcodePick}, 440 OP_ROLL: {OP_ROLL, "OP_ROLL", 1, opcodeRoll}, 441 OP_ROT: {OP_ROT, "OP_ROT", 1, opcodeRot}, 442 OP_SWAP: {OP_SWAP, "OP_SWAP", 1, opcodeSwap}, 443 OP_TUCK: {OP_TUCK, "OP_TUCK", 1, opcodeTuck}, 444 445 // Splice opcodes. 446 OP_CAT: {OP_CAT, "OP_CAT", 1, opcodeDisabled}, 447 OP_SUBSTR: {OP_SUBSTR, "OP_SUBSTR", 1, opcodeDisabled}, 448 OP_LEFT: {OP_LEFT, "OP_LEFT", 1, opcodeDisabled}, 449 OP_RIGHT: {OP_RIGHT, "OP_RIGHT", 1, opcodeDisabled}, 450 OP_SIZE: {OP_SIZE, "OP_SIZE", 1, opcodeSize}, 451 452 // Bitwise logic opcodes. 453 OP_INVERT: {OP_INVERT, "OP_INVERT", 1, opcodeDisabled}, 454 OP_AND: {OP_AND, "OP_AND", 1, opcodeDisabled}, 455 OP_OR: {OP_OR, "OP_OR", 1, opcodeDisabled}, 456 OP_XOR: {OP_XOR, "OP_XOR", 1, opcodeDisabled}, 457 OP_EQUAL: {OP_EQUAL, "OP_EQUAL", 1, opcodeEqual}, 458 OP_EQUALVERIFY: {OP_EQUALVERIFY, "OP_EQUALVERIFY", 1, opcodeEqualVerify}, 459 OP_RESERVED1: {OP_RESERVED1, "OP_RESERVED1", 1, opcodeReserved}, 460 OP_RESERVED2: {OP_RESERVED2, "OP_RESERVED2", 1, opcodeReserved}, 461 462 // Numeric related opcodes. 463 OP_1ADD: {OP_1ADD, "OP_1ADD", 1, opcode1Add}, 464 OP_1SUB: {OP_1SUB, "OP_1SUB", 1, opcode1Sub}, 465 OP_2MUL: {OP_2MUL, "OP_2MUL", 1, opcodeDisabled}, 466 OP_2DIV: {OP_2DIV, "OP_2DIV", 1, opcodeDisabled}, 467 OP_NEGATE: {OP_NEGATE, "OP_NEGATE", 1, opcodeNegate}, 468 OP_ABS: {OP_ABS, "OP_ABS", 1, opcodeAbs}, 469 OP_NOT: {OP_NOT, "OP_NOT", 1, opcodeNot}, 470 OP_0NOTEQUAL: {OP_0NOTEQUAL, "OP_0NOTEQUAL", 1, opcode0NotEqual}, 471 OP_ADD: {OP_ADD, "OP_ADD", 1, opcodeAdd}, 472 OP_SUB: {OP_SUB, "OP_SUB", 1, opcodeSub}, 473 OP_MUL: {OP_MUL, "OP_MUL", 1, opcodeDisabled}, 474 OP_DIV: {OP_DIV, "OP_DIV", 1, opcodeDisabled}, 475 OP_MOD: {OP_MOD, "OP_MOD", 1, opcodeDisabled}, 476 OP_LSHIFT: {OP_LSHIFT, "OP_LSHIFT", 1, opcodeDisabled}, 477 OP_RSHIFT: {OP_RSHIFT, "OP_RSHIFT", 1, opcodeDisabled}, 478 OP_BOOLAND: {OP_BOOLAND, "OP_BOOLAND", 1, opcodeBoolAnd}, 479 OP_BOOLOR: {OP_BOOLOR, "OP_BOOLOR", 1, opcodeBoolOr}, 480 OP_NUMEQUAL: {OP_NUMEQUAL, "OP_NUMEQUAL", 1, opcodeNumEqual}, 481 OP_NUMEQUALVERIFY: {OP_NUMEQUALVERIFY, "OP_NUMEQUALVERIFY", 1, opcodeNumEqualVerify}, 482 OP_NUMNOTEQUAL: {OP_NUMNOTEQUAL, "OP_NUMNOTEQUAL", 1, opcodeNumNotEqual}, 483 OP_LESSTHAN: {OP_LESSTHAN, "OP_LESSTHAN", 1, opcodeLessThan}, 484 OP_GREATERTHAN: {OP_GREATERTHAN, "OP_GREATERTHAN", 1, opcodeGreaterThan}, 485 OP_LESSTHANOREQUAL: {OP_LESSTHANOREQUAL, "OP_LESSTHANOREQUAL", 1, opcodeLessThanOrEqual}, 486 OP_GREATERTHANOREQUAL: {OP_GREATERTHANOREQUAL, "OP_GREATERTHANOREQUAL", 1, opcodeGreaterThanOrEqual}, 487 OP_MIN: {OP_MIN, "OP_MIN", 1, opcodeMin}, 488 OP_MAX: {OP_MAX, "OP_MAX", 1, opcodeMax}, 489 OP_WITHIN: {OP_WITHIN, "OP_WITHIN", 1, opcodeWithin}, 490 491 // Crypto opcodes. 492 OP_RIPEMD160: {OP_RIPEMD160, "OP_RIPEMD160", 1, opcodeRipemd160}, 493 OP_SHA1: {OP_SHA1, "OP_SHA1", 1, opcodeSha1}, 494 OP_SHA256: {OP_SHA256, "OP_SHA256", 1, opcodeSha256}, 495 OP_HASH160: {OP_HASH160, "OP_HASH160", 1, opcodeHash160}, 496 OP_HASH256: {OP_HASH256, "OP_HASH256", 1, opcodeHash256}, 497 OP_CODESEPARATOR: {OP_CODESEPARATOR, "OP_CODESEPARATOR", 1, opcodeCodeSeparator}, 498 OP_CHECKSIG: {OP_CHECKSIG, "OP_CHECKSIG", 1, opcodeCheckSig}, 499 OP_CHECKSIGVERIFY: {OP_CHECKSIGVERIFY, "OP_CHECKSIGVERIFY", 1, opcodeCheckSigVerify}, 500 OP_CHECKMULTISIG: {OP_CHECKMULTISIG, "OP_CHECKMULTISIG", 1, opcodeCheckMultiSig}, 501 OP_CHECKMULTISIGVERIFY: {OP_CHECKMULTISIGVERIFY, "OP_CHECKMULTISIGVERIFY", 1, opcodeCheckMultiSigVerify}, 502 503 // Reserved opcodes. 504 OP_NOP1: {OP_NOP1, "OP_NOP1", 1, opcodeNop}, 505 OP_NOP4: {OP_NOP4, "OP_NOP4", 1, opcodeNop}, 506 OP_NOP5: {OP_NOP5, "OP_NOP5", 1, opcodeNop}, 507 OP_CLAIMNAME: {OP_CLAIMNAME, "OP_CLAIMNAME", 1, opcodeClaimScript}, 508 OP_SUPPORTCLAIM: {OP_SUPPORTCLAIM, "OP_SUPPORTCLAIM", 1, opcodeClaimScript}, 509 OP_UPDATECLAIM: {OP_UPDATECLAIM, "OP_UPDATECLAIM", 1, opcodeClaimScript}, 510 OP_NOP9: {OP_NOP9, "OP_NOP9", 1, opcodeNop}, 511 OP_NOP10: {OP_NOP10, "OP_NOP10", 1, opcodeNop}, 512 513 // Undefined opcodes. 514 OP_UNKNOWN186: {OP_UNKNOWN186, "OP_UNKNOWN186", 1, opcodeInvalid}, 515 OP_UNKNOWN187: {OP_UNKNOWN187, "OP_UNKNOWN187", 1, opcodeInvalid}, 516 OP_UNKNOWN188: {OP_UNKNOWN188, "OP_UNKNOWN188", 1, opcodeInvalid}, 517 OP_UNKNOWN189: {OP_UNKNOWN189, "OP_UNKNOWN189", 1, opcodeInvalid}, 518 OP_UNKNOWN190: {OP_UNKNOWN190, "OP_UNKNOWN190", 1, opcodeInvalid}, 519 OP_UNKNOWN191: {OP_UNKNOWN191, "OP_UNKNOWN191", 1, opcodeInvalid}, 520 OP_UNKNOWN192: {OP_UNKNOWN192, "OP_UNKNOWN192", 1, opcodeInvalid}, 521 OP_UNKNOWN193: {OP_UNKNOWN193, "OP_UNKNOWN193", 1, opcodeInvalid}, 522 OP_UNKNOWN194: {OP_UNKNOWN194, "OP_UNKNOWN194", 1, opcodeInvalid}, 523 OP_UNKNOWN195: {OP_UNKNOWN195, "OP_UNKNOWN195", 1, opcodeInvalid}, 524 OP_UNKNOWN196: {OP_UNKNOWN196, "OP_UNKNOWN196", 1, opcodeInvalid}, 525 OP_UNKNOWN197: {OP_UNKNOWN197, "OP_UNKNOWN197", 1, opcodeInvalid}, 526 OP_UNKNOWN198: {OP_UNKNOWN198, "OP_UNKNOWN198", 1, opcodeInvalid}, 527 OP_UNKNOWN199: {OP_UNKNOWN199, "OP_UNKNOWN199", 1, opcodeInvalid}, 528 OP_UNKNOWN200: {OP_UNKNOWN200, "OP_UNKNOWN200", 1, opcodeInvalid}, 529 OP_UNKNOWN201: {OP_UNKNOWN201, "OP_UNKNOWN201", 1, opcodeInvalid}, 530 OP_UNKNOWN202: {OP_UNKNOWN202, "OP_UNKNOWN202", 1, opcodeInvalid}, 531 OP_UNKNOWN203: {OP_UNKNOWN203, "OP_UNKNOWN203", 1, opcodeInvalid}, 532 OP_UNKNOWN204: {OP_UNKNOWN204, "OP_UNKNOWN204", 1, opcodeInvalid}, 533 OP_UNKNOWN205: {OP_UNKNOWN205, "OP_UNKNOWN205", 1, opcodeInvalid}, 534 OP_UNKNOWN206: {OP_UNKNOWN206, "OP_UNKNOWN206", 1, opcodeInvalid}, 535 OP_UNKNOWN207: {OP_UNKNOWN207, "OP_UNKNOWN207", 1, opcodeInvalid}, 536 OP_UNKNOWN208: {OP_UNKNOWN208, "OP_UNKNOWN208", 1, opcodeInvalid}, 537 OP_UNKNOWN209: {OP_UNKNOWN209, "OP_UNKNOWN209", 1, opcodeInvalid}, 538 OP_UNKNOWN210: {OP_UNKNOWN210, "OP_UNKNOWN210", 1, opcodeInvalid}, 539 OP_UNKNOWN211: {OP_UNKNOWN211, "OP_UNKNOWN211", 1, opcodeInvalid}, 540 OP_UNKNOWN212: {OP_UNKNOWN212, "OP_UNKNOWN212", 1, opcodeInvalid}, 541 OP_UNKNOWN213: {OP_UNKNOWN213, "OP_UNKNOWN213", 1, opcodeInvalid}, 542 OP_UNKNOWN214: {OP_UNKNOWN214, "OP_UNKNOWN214", 1, opcodeInvalid}, 543 OP_UNKNOWN215: {OP_UNKNOWN215, "OP_UNKNOWN215", 1, opcodeInvalid}, 544 OP_UNKNOWN216: {OP_UNKNOWN216, "OP_UNKNOWN216", 1, opcodeInvalid}, 545 OP_UNKNOWN217: {OP_UNKNOWN217, "OP_UNKNOWN217", 1, opcodeInvalid}, 546 OP_UNKNOWN218: {OP_UNKNOWN218, "OP_UNKNOWN218", 1, opcodeInvalid}, 547 OP_UNKNOWN219: {OP_UNKNOWN219, "OP_UNKNOWN219", 1, opcodeInvalid}, 548 OP_UNKNOWN220: {OP_UNKNOWN220, "OP_UNKNOWN220", 1, opcodeInvalid}, 549 OP_UNKNOWN221: {OP_UNKNOWN221, "OP_UNKNOWN221", 1, opcodeInvalid}, 550 OP_UNKNOWN222: {OP_UNKNOWN222, "OP_UNKNOWN222", 1, opcodeInvalid}, 551 OP_UNKNOWN223: {OP_UNKNOWN223, "OP_UNKNOWN223", 1, opcodeInvalid}, 552 OP_UNKNOWN224: {OP_UNKNOWN224, "OP_UNKNOWN224", 1, opcodeInvalid}, 553 OP_UNKNOWN225: {OP_UNKNOWN225, "OP_UNKNOWN225", 1, opcodeInvalid}, 554 OP_UNKNOWN226: {OP_UNKNOWN226, "OP_UNKNOWN226", 1, opcodeInvalid}, 555 OP_UNKNOWN227: {OP_UNKNOWN227, "OP_UNKNOWN227", 1, opcodeInvalid}, 556 OP_UNKNOWN228: {OP_UNKNOWN228, "OP_UNKNOWN228", 1, opcodeInvalid}, 557 OP_UNKNOWN229: {OP_UNKNOWN229, "OP_UNKNOWN229", 1, opcodeInvalid}, 558 OP_UNKNOWN230: {OP_UNKNOWN230, "OP_UNKNOWN230", 1, opcodeInvalid}, 559 OP_UNKNOWN231: {OP_UNKNOWN231, "OP_UNKNOWN231", 1, opcodeInvalid}, 560 OP_UNKNOWN232: {OP_UNKNOWN232, "OP_UNKNOWN232", 1, opcodeInvalid}, 561 OP_UNKNOWN233: {OP_UNKNOWN233, "OP_UNKNOWN233", 1, opcodeInvalid}, 562 OP_UNKNOWN234: {OP_UNKNOWN234, "OP_UNKNOWN234", 1, opcodeInvalid}, 563 OP_UNKNOWN235: {OP_UNKNOWN235, "OP_UNKNOWN235", 1, opcodeInvalid}, 564 OP_UNKNOWN236: {OP_UNKNOWN236, "OP_UNKNOWN236", 1, opcodeInvalid}, 565 OP_UNKNOWN237: {OP_UNKNOWN237, "OP_UNKNOWN237", 1, opcodeInvalid}, 566 OP_UNKNOWN238: {OP_UNKNOWN238, "OP_UNKNOWN238", 1, opcodeInvalid}, 567 OP_UNKNOWN239: {OP_UNKNOWN239, "OP_UNKNOWN239", 1, opcodeInvalid}, 568 OP_UNKNOWN240: {OP_UNKNOWN240, "OP_UNKNOWN240", 1, opcodeInvalid}, 569 OP_UNKNOWN241: {OP_UNKNOWN241, "OP_UNKNOWN241", 1, opcodeInvalid}, 570 OP_UNKNOWN242: {OP_UNKNOWN242, "OP_UNKNOWN242", 1, opcodeInvalid}, 571 OP_UNKNOWN243: {OP_UNKNOWN243, "OP_UNKNOWN243", 1, opcodeInvalid}, 572 OP_UNKNOWN244: {OP_UNKNOWN244, "OP_UNKNOWN244", 1, opcodeInvalid}, 573 OP_UNKNOWN245: {OP_UNKNOWN245, "OP_UNKNOWN245", 1, opcodeInvalid}, 574 OP_UNKNOWN246: {OP_UNKNOWN246, "OP_UNKNOWN246", 1, opcodeInvalid}, 575 OP_UNKNOWN247: {OP_UNKNOWN247, "OP_UNKNOWN247", 1, opcodeInvalid}, 576 OP_UNKNOWN248: {OP_UNKNOWN248, "OP_UNKNOWN248", 1, opcodeInvalid}, 577 OP_UNKNOWN249: {OP_UNKNOWN249, "OP_UNKNOWN249", 1, opcodeInvalid}, 578 579 // Bitcoin Core internal use opcode. Defined here for completeness. 580 OP_SMALLINTEGER: {OP_SMALLINTEGER, "OP_SMALLINTEGER", 1, opcodeInvalid}, 581 OP_PUBKEYS: {OP_PUBKEYS, "OP_PUBKEYS", 1, opcodeInvalid}, 582 OP_UNKNOWN252: {OP_UNKNOWN252, "OP_UNKNOWN252", 1, opcodeInvalid}, 583 OP_PUBKEYHASH: {OP_PUBKEYHASH, "OP_PUBKEYHASH", 1, opcodeInvalid}, 584 OP_PUBKEY: {OP_PUBKEY, "OP_PUBKEY", 1, opcodeInvalid}, 585 586 OP_INVALIDOPCODE: {OP_INVALIDOPCODE, "OP_INVALIDOPCODE", 1, opcodeInvalid}, 587 } 588 589 // opcodeOnelineRepls defines opcode names which are replaced when doing a 590 // one-line disassembly. This is done to match the output of the reference 591 // implementation while not changing the opcode names in the nicer full 592 // disassembly. 593 var opcodeOnelineRepls = map[string]string{ 594 "OP_1NEGATE": "-1", 595 "OP_0": "0", 596 "OP_1": "1", 597 "OP_2": "2", 598 "OP_3": "3", 599 "OP_4": "4", 600 "OP_5": "5", 601 "OP_6": "6", 602 "OP_7": "7", 603 "OP_8": "8", 604 "OP_9": "9", 605 "OP_10": "10", 606 "OP_11": "11", 607 "OP_12": "12", 608 "OP_13": "13", 609 "OP_14": "14", 610 "OP_15": "15", 611 "OP_16": "16", 612 } 613 614 // disasmOpcode writes a human-readable disassembly of the provided opcode and 615 // data into the provided buffer. The compact flag indicates the disassembly 616 // should print a more compact representation of data-carrying and small integer 617 // opcodes. For example, OP_0 through OP_16 are replaced with the numeric value 618 // and data pushes are printed as only the hex representation of the data as 619 // opposed to including the opcode that specifies the amount of data to push as 620 // well. 621 func disasmOpcode(buf *strings.Builder, op *opcode, data []byte, compact bool) { 622 // Replace opcode which represent values (e.g. OP_0 through OP_16 and 623 // OP_1NEGATE) with the raw value when performing a compact disassembly. 624 opcodeName := op.name 625 if compact { 626 if replName, ok := opcodeOnelineRepls[opcodeName]; ok { 627 opcodeName = replName 628 } 629 630 // Either write the human-readable opcode or the parsed data in hex for 631 // data-carrying opcodes. 632 switch { 633 case op.length == 1: 634 buf.WriteString(opcodeName) 635 636 default: 637 buf.WriteString(hex.EncodeToString(data)) 638 } 639 640 return 641 } 642 643 buf.WriteString(opcodeName) 644 645 switch op.length { 646 // Only write the opcode name for non-data push opcodes. 647 case 1: 648 return 649 650 // Add length for the OP_PUSHDATA# opcodes. 651 case -1: 652 buf.WriteString(fmt.Sprintf(" 0x%02x", len(data))) 653 case -2: 654 buf.WriteString(fmt.Sprintf(" 0x%04x", len(data))) 655 case -4: 656 buf.WriteString(fmt.Sprintf(" 0x%08x", len(data))) 657 } 658 659 buf.WriteString(fmt.Sprintf(" 0x%02x", data)) 660 } 661 662 // ******************************************* 663 // Opcode implementation functions start here. 664 // ******************************************* 665 666 // opcodeDisabled is a common handler for disabled opcodes. It returns an 667 // appropriate error indicating the opcode is disabled. While it would 668 // ordinarily make more sense to detect if the script contains any disabled 669 // opcodes before executing in an initial parse step, the consensus rules 670 // dictate the script doesn't fail until the program counter passes over a 671 // disabled opcode (even when they appear in a branch that is not executed). 672 func opcodeDisabled(op *opcode, data []byte, vm *Engine) error { 673 str := fmt.Sprintf("attempt to execute disabled opcode %s", op.name) 674 return scriptError(ErrDisabledOpcode, str) 675 } 676 677 // opcodeReserved is a common handler for all reserved opcodes. It returns an 678 // appropriate error indicating the opcode is reserved. 679 func opcodeReserved(op *opcode, data []byte, vm *Engine) error { 680 str := fmt.Sprintf("attempt to execute reserved opcode %s", op.name) 681 return scriptError(ErrReservedOpcode, str) 682 } 683 684 // opcodeInvalid is a common handler for all invalid opcodes. It returns an 685 // appropriate error indicating the opcode is invalid. 686 func opcodeInvalid(op *opcode, data []byte, vm *Engine) error { 687 str := fmt.Sprintf("attempt to execute invalid opcode %s", op.name) 688 return scriptError(ErrReservedOpcode, str) 689 } 690 691 // opcodeFalse pushes an empty array to the data stack to represent false. Note 692 // that 0, when encoded as a number according to the numeric encoding consensus 693 // rules, is an empty array. 694 func opcodeFalse(op *opcode, data []byte, vm *Engine) error { 695 vm.dstack.PushByteArray(nil) 696 return nil 697 } 698 699 // opcodePushData is a common handler for the vast majority of opcodes that push 700 // raw data (bytes) to the data stack. 701 func opcodePushData(op *opcode, data []byte, vm *Engine) error { 702 vm.dstack.PushByteArray(data) 703 return nil 704 } 705 706 // opcode1Negate pushes -1, encoded as a number, to the data stack. 707 func opcode1Negate(op *opcode, data []byte, vm *Engine) error { 708 vm.dstack.PushInt(scriptNum(-1)) 709 return nil 710 } 711 712 // opcodeN is a common handler for the small integer data push opcodes. It 713 // pushes the numeric value the opcode represents (which will be from 1 to 16) 714 // onto the data stack. 715 func opcodeN(op *opcode, data []byte, vm *Engine) error { 716 // The opcodes are all defined consecutively, so the numeric value is 717 // the difference. 718 vm.dstack.PushInt(scriptNum((op.value - (OP_1 - 1)))) 719 return nil 720 } 721 722 // opcodeNop is a common handler for the NOP family of opcodes. As the name 723 // implies it generally does nothing, however, it will return an error when 724 // the flag to discourage use of NOPs is set for select opcodes. 725 func opcodeNop(op *opcode, data []byte, vm *Engine) error { 726 switch op.value { 727 case OP_NOP1, OP_NOP4, OP_NOP5, 728 OP_NOP9, OP_NOP10: 729 if vm.hasFlag(ScriptDiscourageUpgradableNops) { 730 str := fmt.Sprintf("%v reserved for soft-fork "+ 731 "upgrades", op.name) 732 return scriptError(ErrDiscourageUpgradableNOPs, str) 733 } 734 } 735 return nil 736 } 737 738 func opcodeClaimScript(op *opcode, data []byte, vm *Engine) error { 739 vm.dstack.PushByteArray([]byte{0}) 740 return nil 741 } 742 743 // popIfBool enforces the "minimal if" policy during script execution if the 744 // particular flag is set. If so, in order to eliminate an additional source 745 // of nuisance malleability, post-segwit for version 0 witness programs, we now 746 // require the following: for OP_IF and OP_NOT_IF, the top stack item MUST 747 // either be an empty byte slice, or [0x01]. Otherwise, the item at the top of 748 // the stack will be popped and interpreted as a boolean. 749 func popIfBool(vm *Engine) (bool, error) { 750 // When not in witness execution mode, not executing a v0 witness 751 // program, or the minimal if flag isn't set pop the top stack item as 752 // a normal bool. 753 if !vm.isWitnessVersionActive(0) || !vm.hasFlag(ScriptVerifyMinimalIf) { 754 return vm.dstack.PopBool() 755 } 756 757 // At this point, a v0 witness program is being executed and the minimal 758 // if flag is set, so enforce additional constraints on the top stack 759 // item. 760 so, err := vm.dstack.PopByteArray() 761 if err != nil { 762 return false, err 763 } 764 765 // The top element MUST have a length of at least one. 766 if len(so) > 1 { 767 str := fmt.Sprintf("minimal if is active, top element MUST "+ 768 "have a length of at least, instead length is %v", 769 len(so)) 770 return false, scriptError(ErrMinimalIf, str) 771 } 772 773 // Additionally, if the length is one, then the value MUST be 0x01. 774 if len(so) == 1 && so[0] != 0x01 { 775 str := fmt.Sprintf("minimal if is active, top stack item MUST "+ 776 "be an empty byte array or 0x01, is instead: %v", 777 so[0]) 778 return false, scriptError(ErrMinimalIf, str) 779 } 780 781 return asBool(so), nil 782 } 783 784 // opcodeIf treats the top item on the data stack as a boolean and removes it. 785 // 786 // An appropriate entry is added to the conditional stack depending on whether 787 // the boolean is true and whether this if is on an executing branch in order 788 // to allow proper execution of further opcodes depending on the conditional 789 // logic. When the boolean is true, the first branch will be executed (unless 790 // this opcode is nested in a non-executed branch). 791 // 792 // <expression> if [statements] [else [statements]] endif 793 // 794 // Note that, unlike for all non-conditional opcodes, this is executed even when 795 // it is on a non-executing branch so proper nesting is maintained. 796 // 797 // Data stack transformation: [... bool] -> [...] 798 // Conditional stack transformation: [...] -> [... OpCondValue] 799 func opcodeIf(op *opcode, data []byte, vm *Engine) error { 800 condVal := OpCondFalse 801 if vm.isBranchExecuting() { 802 ok, err := popIfBool(vm) 803 if err != nil { 804 return err 805 } 806 807 if ok { 808 condVal = OpCondTrue 809 } 810 } else { 811 condVal = OpCondSkip 812 } 813 vm.condStack = append(vm.condStack, condVal) 814 return nil 815 } 816 817 // opcodeNotIf treats the top item on the data stack as a boolean and removes 818 // it. 819 // 820 // An appropriate entry is added to the conditional stack depending on whether 821 // the boolean is true and whether this if is on an executing branch in order 822 // to allow proper execution of further opcodes depending on the conditional 823 // logic. When the boolean is false, the first branch will be executed (unless 824 // this opcode is nested in a non-executed branch). 825 // 826 // <expression> notif [statements] [else [statements]] endif 827 // 828 // Note that, unlike for all non-conditional opcodes, this is executed even when 829 // it is on a non-executing branch so proper nesting is maintained. 830 // 831 // Data stack transformation: [... bool] -> [...] 832 // Conditional stack transformation: [...] -> [... OpCondValue] 833 func opcodeNotIf(op *opcode, data []byte, vm *Engine) error { 834 condVal := OpCondFalse 835 if vm.isBranchExecuting() { 836 ok, err := popIfBool(vm) 837 if err != nil { 838 return err 839 } 840 841 if !ok { 842 condVal = OpCondTrue 843 } 844 } else { 845 condVal = OpCondSkip 846 } 847 vm.condStack = append(vm.condStack, condVal) 848 return nil 849 } 850 851 // opcodeElse inverts conditional execution for other half of if/else/endif. 852 // 853 // An error is returned if there has not already been a matching OP_IF. 854 // 855 // Conditional stack transformation: [... OpCondValue] -> [... !OpCondValue] 856 func opcodeElse(op *opcode, data []byte, vm *Engine) error { 857 if len(vm.condStack) == 0 { 858 str := fmt.Sprintf("encountered opcode %s with no matching "+ 859 "opcode to begin conditional execution", op.name) 860 return scriptError(ErrUnbalancedConditional, str) 861 } 862 863 conditionalIdx := len(vm.condStack) - 1 864 switch vm.condStack[conditionalIdx] { 865 case OpCondTrue: 866 vm.condStack[conditionalIdx] = OpCondFalse 867 case OpCondFalse: 868 vm.condStack[conditionalIdx] = OpCondTrue 869 case OpCondSkip: 870 // Value doesn't change in skip since it indicates this opcode 871 // is nested in a non-executed branch. 872 } 873 return nil 874 } 875 876 // opcodeEndif terminates a conditional block, removing the value from the 877 // conditional execution stack. 878 // 879 // An error is returned if there has not already been a matching OP_IF. 880 // 881 // Conditional stack transformation: [... OpCondValue] -> [...] 882 func opcodeEndif(op *opcode, data []byte, vm *Engine) error { 883 if len(vm.condStack) == 0 { 884 str := fmt.Sprintf("encountered opcode %s with no matching "+ 885 "opcode to begin conditional execution", op.name) 886 return scriptError(ErrUnbalancedConditional, str) 887 } 888 889 vm.condStack = vm.condStack[:len(vm.condStack)-1] 890 return nil 891 } 892 893 // abstractVerify examines the top item on the data stack as a boolean value and 894 // verifies it evaluates to true. An error is returned either when there is no 895 // item on the stack or when that item evaluates to false. In the latter case 896 // where the verification fails specifically due to the top item evaluating 897 // to false, the returned error will use the passed error code. 898 func abstractVerify(op *opcode, vm *Engine, c ErrorCode) error { 899 verified, err := vm.dstack.PopBool() 900 if err != nil { 901 return err 902 } 903 904 if !verified { 905 str := fmt.Sprintf("%s failed", op.name) 906 return scriptError(c, str) 907 } 908 return nil 909 } 910 911 // opcodeVerify examines the top item on the data stack as a boolean value and 912 // verifies it evaluates to true. An error is returned if it does not. 913 func opcodeVerify(op *opcode, data []byte, vm *Engine) error { 914 return abstractVerify(op, vm, ErrVerify) 915 } 916 917 // opcodeReturn returns an appropriate error since it is always an error to 918 // return early from a script. 919 func opcodeReturn(op *opcode, data []byte, vm *Engine) error { 920 return scriptError(ErrEarlyReturn, "script returned early") 921 } 922 923 // verifyLockTime is a helper function used to validate locktimes. 924 func verifyLockTime(txLockTime, threshold, lockTime int64) error { 925 // The lockTimes in both the script and transaction must be of the same 926 // type. 927 if !((txLockTime < threshold && lockTime < threshold) || 928 (txLockTime >= threshold && lockTime >= threshold)) { 929 str := fmt.Sprintf("mismatched locktime types -- tx locktime "+ 930 "%d, stack locktime %d", txLockTime, lockTime) 931 return scriptError(ErrUnsatisfiedLockTime, str) 932 } 933 934 if lockTime > txLockTime { 935 str := fmt.Sprintf("locktime requirement not satisfied -- "+ 936 "locktime is greater than the transaction locktime: "+ 937 "%d > %d", lockTime, txLockTime) 938 return scriptError(ErrUnsatisfiedLockTime, str) 939 } 940 941 return nil 942 } 943 944 // opcodeCheckLockTimeVerify compares the top item on the data stack to the 945 // LockTime field of the transaction containing the script signature 946 // validating if the transaction outputs are spendable yet. If flag 947 // ScriptVerifyCheckLockTimeVerify is not set, the code continues as if OP_NOP2 948 // were executed. 949 func opcodeCheckLockTimeVerify(op *opcode, data []byte, vm *Engine) error { 950 // If the ScriptVerifyCheckLockTimeVerify script flag is not set, treat 951 // opcode as OP_NOP2 instead. 952 if !vm.hasFlag(ScriptVerifyCheckLockTimeVerify) { 953 if vm.hasFlag(ScriptDiscourageUpgradableNops) { 954 return scriptError(ErrDiscourageUpgradableNOPs, 955 "OP_NOP2 reserved for soft-fork upgrades") 956 } 957 return nil 958 } 959 960 // The current transaction locktime is a uint32 resulting in a maximum 961 // locktime of 2^32-1 (the year 2106). However, scriptNums are signed 962 // and therefore a standard 4-byte scriptNum would only support up to a 963 // maximum of 2^31-1 (the year 2038). Thus, a 5-byte scriptNum is used 964 // here since it will support up to 2^39-1 which allows dates beyond the 965 // current locktime limit. 966 // 967 // PeekByteArray is used here instead of PeekInt because we do not want 968 // to be limited to a 4-byte integer for reasons specified above. 969 so, err := vm.dstack.PeekByteArray(0) 970 if err != nil { 971 return err 972 } 973 lockTime, err := makeScriptNum(so, vm.dstack.verifyMinimalData, 5) 974 if err != nil { 975 return err 976 } 977 978 // In the rare event that the argument needs to be < 0 due to some 979 // arithmetic being done first, you can always use 980 // 0 OP_MAX OP_CHECKLOCKTIMEVERIFY. 981 if lockTime < 0 { 982 str := fmt.Sprintf("negative lock time: %d", lockTime) 983 return scriptError(ErrNegativeLockTime, str) 984 } 985 986 // The lock time field of a transaction is either a block height at 987 // which the transaction is finalized or a timestamp depending on if the 988 // value is before the txscript.LockTimeThreshold. When it is under the 989 // threshold it is a block height. 990 err = verifyLockTime(int64(vm.tx.LockTime), LockTimeThreshold, 991 int64(lockTime)) 992 if err != nil { 993 return err 994 } 995 996 // The lock time feature can also be disabled, thereby bypassing 997 // OP_CHECKLOCKTIMEVERIFY, if every transaction input has been finalized by 998 // setting its sequence to the maximum value (wire.MaxTxInSequenceNum). This 999 // condition would result in the transaction being allowed into the blockchain 1000 // making the opcode ineffective. 1001 // 1002 // This condition is prevented by enforcing that the input being used by 1003 // the opcode is unlocked (its sequence number is less than the max 1004 // value). This is sufficient to prove correctness without having to 1005 // check every input. 1006 // 1007 // NOTE: This implies that even if the transaction is not finalized due to 1008 // another input being unlocked, the opcode execution will still fail when the 1009 // input being used by the opcode is locked. 1010 if vm.tx.TxIn[vm.txIdx].Sequence == wire.MaxTxInSequenceNum { 1011 return scriptError(ErrUnsatisfiedLockTime, 1012 "transaction input is finalized") 1013 } 1014 1015 return nil 1016 } 1017 1018 // opcodeCheckSequenceVerify compares the top item on the data stack to the 1019 // LockTime field of the transaction containing the script signature 1020 // validating if the transaction outputs are spendable yet. If flag 1021 // ScriptVerifyCheckSequenceVerify is not set, the code continues as if OP_NOP3 1022 // were executed. 1023 func opcodeCheckSequenceVerify(op *opcode, data []byte, vm *Engine) error { 1024 // If the ScriptVerifyCheckSequenceVerify script flag is not set, treat 1025 // opcode as OP_NOP3 instead. 1026 if !vm.hasFlag(ScriptVerifyCheckSequenceVerify) { 1027 if vm.hasFlag(ScriptDiscourageUpgradableNops) { 1028 return scriptError(ErrDiscourageUpgradableNOPs, 1029 "OP_NOP3 reserved for soft-fork upgrades") 1030 } 1031 return nil 1032 } 1033 1034 // The current transaction sequence is a uint32 resulting in a maximum 1035 // sequence of 2^32-1. However, scriptNums are signed and therefore a 1036 // standard 4-byte scriptNum would only support up to a maximum of 1037 // 2^31-1. Thus, a 5-byte scriptNum is used here since it will support 1038 // up to 2^39-1 which allows sequences beyond the current sequence 1039 // limit. 1040 // 1041 // PeekByteArray is used here instead of PeekInt because we do not want 1042 // to be limited to a 4-byte integer for reasons specified above. 1043 so, err := vm.dstack.PeekByteArray(0) 1044 if err != nil { 1045 return err 1046 } 1047 stackSequence, err := makeScriptNum(so, vm.dstack.verifyMinimalData, 5) 1048 if err != nil { 1049 return err 1050 } 1051 1052 // In the rare event that the argument needs to be < 0 due to some 1053 // arithmetic being done first, you can always use 1054 // 0 OP_MAX OP_CHECKSEQUENCEVERIFY. 1055 if stackSequence < 0 { 1056 str := fmt.Sprintf("negative sequence: %d", stackSequence) 1057 return scriptError(ErrNegativeLockTime, str) 1058 } 1059 1060 sequence := int64(stackSequence) 1061 1062 // To provide for future soft-fork extensibility, if the 1063 // operand has the disabled lock-time flag set, 1064 // CHECKSEQUENCEVERIFY behaves as a NOP. 1065 if sequence&int64(wire.SequenceLockTimeDisabled) != 0 { 1066 return nil 1067 } 1068 1069 // Transaction version numbers not high enough to trigger CSV rules must 1070 // fail. 1071 if vm.tx.Version < 2 { 1072 str := fmt.Sprintf("invalid transaction version: %d", 1073 vm.tx.Version) 1074 return scriptError(ErrUnsatisfiedLockTime, str) 1075 } 1076 1077 // Sequence numbers with their most significant bit set are not 1078 // consensus constrained. Testing that the transaction's sequence 1079 // number does not have this bit set prevents using this property 1080 // to get around a CHECKSEQUENCEVERIFY check. 1081 txSequence := int64(vm.tx.TxIn[vm.txIdx].Sequence) 1082 if txSequence&int64(wire.SequenceLockTimeDisabled) != 0 { 1083 str := fmt.Sprintf("transaction sequence has sequence "+ 1084 "locktime disabled bit set: 0x%x", txSequence) 1085 return scriptError(ErrUnsatisfiedLockTime, str) 1086 } 1087 1088 // Mask off non-consensus bits before doing comparisons. 1089 lockTimeMask := int64(wire.SequenceLockTimeIsSeconds | 1090 wire.SequenceLockTimeMask) 1091 return verifyLockTime(txSequence&lockTimeMask, 1092 wire.SequenceLockTimeIsSeconds, sequence&lockTimeMask) 1093 } 1094 1095 // opcodeToAltStack removes the top item from the main data stack and pushes it 1096 // onto the alternate data stack. 1097 // 1098 // Main data stack transformation: [... x1 x2 x3] -> [... x1 x2] 1099 // Alt data stack transformation: [... y1 y2 y3] -> [... y1 y2 y3 x3] 1100 func opcodeToAltStack(op *opcode, data []byte, vm *Engine) error { 1101 so, err := vm.dstack.PopByteArray() 1102 if err != nil { 1103 return err 1104 } 1105 vm.astack.PushByteArray(so) 1106 1107 return nil 1108 } 1109 1110 // opcodeFromAltStack removes the top item from the alternate data stack and 1111 // pushes it onto the main data stack. 1112 // 1113 // Main data stack transformation: [... x1 x2 x3] -> [... x1 x2 x3 y3] 1114 // Alt data stack transformation: [... y1 y2 y3] -> [... y1 y2] 1115 func opcodeFromAltStack(op *opcode, data []byte, vm *Engine) error { 1116 so, err := vm.astack.PopByteArray() 1117 if err != nil { 1118 return err 1119 } 1120 vm.dstack.PushByteArray(so) 1121 1122 return nil 1123 } 1124 1125 // opcode2Drop removes the top 2 items from the data stack. 1126 // 1127 // Stack transformation: [... x1 x2 x3] -> [... x1] 1128 func opcode2Drop(op *opcode, data []byte, vm *Engine) error { 1129 return vm.dstack.DropN(2) 1130 } 1131 1132 // opcode2Dup duplicates the top 2 items on the data stack. 1133 // 1134 // Stack transformation: [... x1 x2 x3] -> [... x1 x2 x3 x2 x3] 1135 func opcode2Dup(op *opcode, data []byte, vm *Engine) error { 1136 return vm.dstack.DupN(2) 1137 } 1138 1139 // opcode3Dup duplicates the top 3 items on the data stack. 1140 // 1141 // Stack transformation: [... x1 x2 x3] -> [... x1 x2 x3 x1 x2 x3] 1142 func opcode3Dup(op *opcode, data []byte, vm *Engine) error { 1143 return vm.dstack.DupN(3) 1144 } 1145 1146 // opcode2Over duplicates the 2 items before the top 2 items on the data stack. 1147 // 1148 // Stack transformation: [... x1 x2 x3 x4] -> [... x1 x2 x3 x4 x1 x2] 1149 func opcode2Over(op *opcode, data []byte, vm *Engine) error { 1150 return vm.dstack.OverN(2) 1151 } 1152 1153 // opcode2Rot rotates the top 6 items on the data stack to the left twice. 1154 // 1155 // Stack transformation: [... x1 x2 x3 x4 x5 x6] -> [... x3 x4 x5 x6 x1 x2] 1156 func opcode2Rot(op *opcode, data []byte, vm *Engine) error { 1157 return vm.dstack.RotN(2) 1158 } 1159 1160 // opcode2Swap swaps the top 2 items on the data stack with the 2 that come 1161 // before them. 1162 // 1163 // Stack transformation: [... x1 x2 x3 x4] -> [... x3 x4 x1 x2] 1164 func opcode2Swap(op *opcode, data []byte, vm *Engine) error { 1165 return vm.dstack.SwapN(2) 1166 } 1167 1168 // opcodeIfDup duplicates the top item of the stack if it is not zero. 1169 // 1170 // Stack transformation (x1==0): [... x1] -> [... x1] 1171 // Stack transformation (x1!=0): [... x1] -> [... x1 x1] 1172 func opcodeIfDup(op *opcode, data []byte, vm *Engine) error { 1173 so, err := vm.dstack.PeekByteArray(0) 1174 if err != nil { 1175 return err 1176 } 1177 1178 // Push copy of data iff it isn't zero 1179 if asBool(so) { 1180 vm.dstack.PushByteArray(so) 1181 } 1182 1183 return nil 1184 } 1185 1186 // opcodeDepth pushes the depth of the data stack prior to executing this 1187 // opcode, encoded as a number, onto the data stack. 1188 // 1189 // Stack transformation: [...] -> [... <num of items on the stack>] 1190 // Example with 2 items: [x1 x2] -> [x1 x2 2] 1191 // Example with 3 items: [x1 x2 x3] -> [x1 x2 x3 3] 1192 func opcodeDepth(op *opcode, data []byte, vm *Engine) error { 1193 vm.dstack.PushInt(scriptNum(vm.dstack.Depth())) 1194 return nil 1195 } 1196 1197 // opcodeDrop removes the top item from the data stack. 1198 // 1199 // Stack transformation: [... x1 x2 x3] -> [... x1 x2] 1200 func opcodeDrop(op *opcode, data []byte, vm *Engine) error { 1201 return vm.dstack.DropN(1) 1202 } 1203 1204 // opcodeDup duplicates the top item on the data stack. 1205 // 1206 // Stack transformation: [... x1 x2 x3] -> [... x1 x2 x3 x3] 1207 func opcodeDup(op *opcode, data []byte, vm *Engine) error { 1208 return vm.dstack.DupN(1) 1209 } 1210 1211 // opcodeNip removes the item before the top item on the data stack. 1212 // 1213 // Stack transformation: [... x1 x2 x3] -> [... x1 x3] 1214 func opcodeNip(op *opcode, data []byte, vm *Engine) error { 1215 return vm.dstack.NipN(1) 1216 } 1217 1218 // opcodeOver duplicates the item before the top item on the data stack. 1219 // 1220 // Stack transformation: [... x1 x2 x3] -> [... x1 x2 x3 x2] 1221 func opcodeOver(op *opcode, data []byte, vm *Engine) error { 1222 return vm.dstack.OverN(1) 1223 } 1224 1225 // opcodePick treats the top item on the data stack as an integer and duplicates 1226 // the item on the stack that number of items back to the top. 1227 // 1228 // Stack transformation: [xn ... x2 x1 x0 n] -> [xn ... x2 x1 x0 xn] 1229 // Example with n=1: [x2 x1 x0 1] -> [x2 x1 x0 x1] 1230 // Example with n=2: [x2 x1 x0 2] -> [x2 x1 x0 x2] 1231 func opcodePick(op *opcode, data []byte, vm *Engine) error { 1232 val, err := vm.dstack.PopInt() 1233 if err != nil { 1234 return err 1235 } 1236 1237 return vm.dstack.PickN(val.Int32()) 1238 } 1239 1240 // opcodeRoll treats the top item on the data stack as an integer and moves 1241 // the item on the stack that number of items back to the top. 1242 // 1243 // Stack transformation: [xn ... x2 x1 x0 n] -> [... x2 x1 x0 xn] 1244 // Example with n=1: [x2 x1 x0 1] -> [x2 x0 x1] 1245 // Example with n=2: [x2 x1 x0 2] -> [x1 x0 x2] 1246 func opcodeRoll(op *opcode, data []byte, vm *Engine) error { 1247 val, err := vm.dstack.PopInt() 1248 if err != nil { 1249 return err 1250 } 1251 1252 return vm.dstack.RollN(val.Int32()) 1253 } 1254 1255 // opcodeRot rotates the top 3 items on the data stack to the left. 1256 // 1257 // Stack transformation: [... x1 x2 x3] -> [... x2 x3 x1] 1258 func opcodeRot(op *opcode, data []byte, vm *Engine) error { 1259 return vm.dstack.RotN(1) 1260 } 1261 1262 // opcodeSwap swaps the top two items on the stack. 1263 // 1264 // Stack transformation: [... x1 x2] -> [... x2 x1] 1265 func opcodeSwap(op *opcode, data []byte, vm *Engine) error { 1266 return vm.dstack.SwapN(1) 1267 } 1268 1269 // opcodeTuck inserts a duplicate of the top item of the data stack before the 1270 // second-to-top item. 1271 // 1272 // Stack transformation: [... x1 x2] -> [... x2 x1 x2] 1273 func opcodeTuck(op *opcode, data []byte, vm *Engine) error { 1274 return vm.dstack.Tuck() 1275 } 1276 1277 // opcodeSize pushes the size of the top item of the data stack onto the data 1278 // stack. 1279 // 1280 // Stack transformation: [... x1] -> [... x1 len(x1)] 1281 func opcodeSize(op *opcode, data []byte, vm *Engine) error { 1282 so, err := vm.dstack.PeekByteArray(0) 1283 if err != nil { 1284 return err 1285 } 1286 1287 vm.dstack.PushInt(scriptNum(len(so))) 1288 return nil 1289 } 1290 1291 // opcodeEqual removes the top 2 items of the data stack, compares them as raw 1292 // bytes, and pushes the result, encoded as a boolean, back to the stack. 1293 // 1294 // Stack transformation: [... x1 x2] -> [... bool] 1295 func opcodeEqual(op *opcode, data []byte, vm *Engine) error { 1296 a, err := vm.dstack.PopByteArray() 1297 if err != nil { 1298 return err 1299 } 1300 b, err := vm.dstack.PopByteArray() 1301 if err != nil { 1302 return err 1303 } 1304 1305 vm.dstack.PushBool(bytes.Equal(a, b)) 1306 return nil 1307 } 1308 1309 // opcodeEqualVerify is a combination of opcodeEqual and opcodeVerify. 1310 // Specifically, it removes the top 2 items of the data stack, compares them, 1311 // and pushes the result, encoded as a boolean, back to the stack. Then, it 1312 // examines the top item on the data stack as a boolean value and verifies it 1313 // evaluates to true. An error is returned if it does not. 1314 // 1315 // Stack transformation: [... x1 x2] -> [... bool] -> [...] 1316 func opcodeEqualVerify(op *opcode, data []byte, vm *Engine) error { 1317 err := opcodeEqual(op, data, vm) 1318 if err == nil { 1319 err = abstractVerify(op, vm, ErrEqualVerify) 1320 } 1321 return err 1322 } 1323 1324 // opcode1Add treats the top item on the data stack as an integer and replaces 1325 // it with its incremented value (plus 1). 1326 // 1327 // Stack transformation: [... x1 x2] -> [... x1 x2+1] 1328 func opcode1Add(op *opcode, data []byte, vm *Engine) error { 1329 m, err := vm.dstack.PopInt() 1330 if err != nil { 1331 return err 1332 } 1333 1334 vm.dstack.PushInt(m + 1) 1335 return nil 1336 } 1337 1338 // opcode1Sub treats the top item on the data stack as an integer and replaces 1339 // it with its decremented value (minus 1). 1340 // 1341 // Stack transformation: [... x1 x2] -> [... x1 x2-1] 1342 func opcode1Sub(op *opcode, data []byte, vm *Engine) error { 1343 m, err := vm.dstack.PopInt() 1344 if err != nil { 1345 return err 1346 } 1347 vm.dstack.PushInt(m - 1) 1348 1349 return nil 1350 } 1351 1352 // opcodeNegate treats the top item on the data stack as an integer and replaces 1353 // it with its negation. 1354 // 1355 // Stack transformation: [... x1 x2] -> [... x1 -x2] 1356 func opcodeNegate(op *opcode, data []byte, vm *Engine) error { 1357 m, err := vm.dstack.PopInt() 1358 if err != nil { 1359 return err 1360 } 1361 1362 vm.dstack.PushInt(-m) 1363 return nil 1364 } 1365 1366 // opcodeAbs treats the top item on the data stack as an integer and replaces it 1367 // it with its absolute value. 1368 // 1369 // Stack transformation: [... x1 x2] -> [... x1 abs(x2)] 1370 func opcodeAbs(op *opcode, data []byte, vm *Engine) error { 1371 m, err := vm.dstack.PopInt() 1372 if err != nil { 1373 return err 1374 } 1375 1376 if m < 0 { 1377 m = -m 1378 } 1379 vm.dstack.PushInt(m) 1380 return nil 1381 } 1382 1383 // opcodeNot treats the top item on the data stack as an integer and replaces 1384 // it with its "inverted" value (0 becomes 1, non-zero becomes 0). 1385 // 1386 // NOTE: While it would probably make more sense to treat the top item as a 1387 // boolean, and push the opposite, which is really what the intention of this 1388 // opcode is, it is extremely important that is not done because integers are 1389 // interpreted differently than booleans and the consensus rules for this opcode 1390 // dictate the item is interpreted as an integer. 1391 // 1392 // Stack transformation (x2==0): [... x1 0] -> [... x1 1] 1393 // Stack transformation (x2!=0): [... x1 1] -> [... x1 0] 1394 // Stack transformation (x2!=0): [... x1 17] -> [... x1 0] 1395 func opcodeNot(op *opcode, data []byte, vm *Engine) error { 1396 m, err := vm.dstack.PopInt() 1397 if err != nil { 1398 return err 1399 } 1400 1401 if m == 0 { 1402 vm.dstack.PushInt(scriptNum(1)) 1403 } else { 1404 vm.dstack.PushInt(scriptNum(0)) 1405 } 1406 return nil 1407 } 1408 1409 // opcode0NotEqual treats the top item on the data stack as an integer and 1410 // replaces it with either a 0 if it is zero, or a 1 if it is not zero. 1411 // 1412 // Stack transformation (x2==0): [... x1 0] -> [... x1 0] 1413 // Stack transformation (x2!=0): [... x1 1] -> [... x1 1] 1414 // Stack transformation (x2!=0): [... x1 17] -> [... x1 1] 1415 func opcode0NotEqual(op *opcode, data []byte, vm *Engine) error { 1416 m, err := vm.dstack.PopInt() 1417 if err != nil { 1418 return err 1419 } 1420 1421 if m != 0 { 1422 m = 1 1423 } 1424 vm.dstack.PushInt(m) 1425 return nil 1426 } 1427 1428 // opcodeAdd treats the top two items on the data stack as integers and replaces 1429 // them with their sum. 1430 // 1431 // Stack transformation: [... x1 x2] -> [... x1+x2] 1432 func opcodeAdd(op *opcode, data []byte, vm *Engine) error { 1433 v0, err := vm.dstack.PopInt() 1434 if err != nil { 1435 return err 1436 } 1437 1438 v1, err := vm.dstack.PopInt() 1439 if err != nil { 1440 return err 1441 } 1442 1443 vm.dstack.PushInt(v0 + v1) 1444 return nil 1445 } 1446 1447 // opcodeSub treats the top two items on the data stack as integers and replaces 1448 // them with the result of subtracting the top entry from the second-to-top 1449 // entry. 1450 // 1451 // Stack transformation: [... x1 x2] -> [... x1-x2] 1452 func opcodeSub(op *opcode, data []byte, vm *Engine) error { 1453 v0, err := vm.dstack.PopInt() 1454 if err != nil { 1455 return err 1456 } 1457 1458 v1, err := vm.dstack.PopInt() 1459 if err != nil { 1460 return err 1461 } 1462 1463 vm.dstack.PushInt(v1 - v0) 1464 return nil 1465 } 1466 1467 // opcodeBoolAnd treats the top two items on the data stack as integers. When 1468 // both of them are not zero, they are replaced with a 1, otherwise a 0. 1469 // 1470 // Stack transformation (x1==0, x2==0): [... 0 0] -> [... 0] 1471 // Stack transformation (x1!=0, x2==0): [... 5 0] -> [... 0] 1472 // Stack transformation (x1==0, x2!=0): [... 0 7] -> [... 0] 1473 // Stack transformation (x1!=0, x2!=0): [... 4 8] -> [... 1] 1474 func opcodeBoolAnd(op *opcode, data []byte, vm *Engine) error { 1475 v0, err := vm.dstack.PopInt() 1476 if err != nil { 1477 return err 1478 } 1479 1480 v1, err := vm.dstack.PopInt() 1481 if err != nil { 1482 return err 1483 } 1484 1485 if v0 != 0 && v1 != 0 { 1486 vm.dstack.PushInt(scriptNum(1)) 1487 } else { 1488 vm.dstack.PushInt(scriptNum(0)) 1489 } 1490 1491 return nil 1492 } 1493 1494 // opcodeBoolOr treats the top two items on the data stack as integers. When 1495 // either of them are not zero, they are replaced with a 1, otherwise a 0. 1496 // 1497 // Stack transformation (x1==0, x2==0): [... 0 0] -> [... 0] 1498 // Stack transformation (x1!=0, x2==0): [... 5 0] -> [... 1] 1499 // Stack transformation (x1==0, x2!=0): [... 0 7] -> [... 1] 1500 // Stack transformation (x1!=0, x2!=0): [... 4 8] -> [... 1] 1501 func opcodeBoolOr(op *opcode, data []byte, vm *Engine) error { 1502 v0, err := vm.dstack.PopInt() 1503 if err != nil { 1504 return err 1505 } 1506 1507 v1, err := vm.dstack.PopInt() 1508 if err != nil { 1509 return err 1510 } 1511 1512 if v0 != 0 || v1 != 0 { 1513 vm.dstack.PushInt(scriptNum(1)) 1514 } else { 1515 vm.dstack.PushInt(scriptNum(0)) 1516 } 1517 1518 return nil 1519 } 1520 1521 // opcodeNumEqual treats the top two items on the data stack as integers. When 1522 // they are equal, they are replaced with a 1, otherwise a 0. 1523 // 1524 // Stack transformation (x1==x2): [... 5 5] -> [... 1] 1525 // Stack transformation (x1!=x2): [... 5 7] -> [... 0] 1526 func opcodeNumEqual(op *opcode, data []byte, vm *Engine) error { 1527 v0, err := vm.dstack.PopInt() 1528 if err != nil { 1529 return err 1530 } 1531 1532 v1, err := vm.dstack.PopInt() 1533 if err != nil { 1534 return err 1535 } 1536 1537 if v0 == v1 { 1538 vm.dstack.PushInt(scriptNum(1)) 1539 } else { 1540 vm.dstack.PushInt(scriptNum(0)) 1541 } 1542 1543 return nil 1544 } 1545 1546 // opcodeNumEqualVerify is a combination of opcodeNumEqual and opcodeVerify. 1547 // 1548 // Specifically, treats the top two items on the data stack as integers. When 1549 // they are equal, they are replaced with a 1, otherwise a 0. Then, it examines 1550 // the top item on the data stack as a boolean value and verifies it evaluates 1551 // to true. An error is returned if it does not. 1552 // 1553 // Stack transformation: [... x1 x2] -> [... bool] -> [...] 1554 func opcodeNumEqualVerify(op *opcode, data []byte, vm *Engine) error { 1555 err := opcodeNumEqual(op, data, vm) 1556 if err == nil { 1557 err = abstractVerify(op, vm, ErrNumEqualVerify) 1558 } 1559 return err 1560 } 1561 1562 // opcodeNumNotEqual treats the top two items on the data stack as integers. 1563 // When they are NOT equal, they are replaced with a 1, otherwise a 0. 1564 // 1565 // Stack transformation (x1==x2): [... 5 5] -> [... 0] 1566 // Stack transformation (x1!=x2): [... 5 7] -> [... 1] 1567 func opcodeNumNotEqual(op *opcode, data []byte, vm *Engine) error { 1568 v0, err := vm.dstack.PopInt() 1569 if err != nil { 1570 return err 1571 } 1572 1573 v1, err := vm.dstack.PopInt() 1574 if err != nil { 1575 return err 1576 } 1577 1578 if v0 != v1 { 1579 vm.dstack.PushInt(scriptNum(1)) 1580 } else { 1581 vm.dstack.PushInt(scriptNum(0)) 1582 } 1583 1584 return nil 1585 } 1586 1587 // opcodeLessThan treats the top two items on the data stack as integers. When 1588 // the second-to-top item is less than the top item, they are replaced with a 1, 1589 // otherwise a 0. 1590 // 1591 // Stack transformation: [... x1 x2] -> [... bool] 1592 func opcodeLessThan(op *opcode, data []byte, vm *Engine) error { 1593 v0, err := vm.dstack.PopInt() 1594 if err != nil { 1595 return err 1596 } 1597 1598 v1, err := vm.dstack.PopInt() 1599 if err != nil { 1600 return err 1601 } 1602 1603 if v1 < v0 { 1604 vm.dstack.PushInt(scriptNum(1)) 1605 } else { 1606 vm.dstack.PushInt(scriptNum(0)) 1607 } 1608 1609 return nil 1610 } 1611 1612 // opcodeGreaterThan treats the top two items on the data stack as integers. 1613 // When the second-to-top item is greater than the top item, they are replaced 1614 // with a 1, otherwise a 0. 1615 // 1616 // Stack transformation: [... x1 x2] -> [... bool] 1617 func opcodeGreaterThan(op *opcode, data []byte, vm *Engine) error { 1618 v0, err := vm.dstack.PopInt() 1619 if err != nil { 1620 return err 1621 } 1622 1623 v1, err := vm.dstack.PopInt() 1624 if err != nil { 1625 return err 1626 } 1627 1628 if v1 > v0 { 1629 vm.dstack.PushInt(scriptNum(1)) 1630 } else { 1631 vm.dstack.PushInt(scriptNum(0)) 1632 } 1633 return nil 1634 } 1635 1636 // opcodeLessThanOrEqual treats the top two items on the data stack as integers. 1637 // When the second-to-top item is less than or equal to the top item, they are 1638 // replaced with a 1, otherwise a 0. 1639 // 1640 // Stack transformation: [... x1 x2] -> [... bool] 1641 func opcodeLessThanOrEqual(op *opcode, data []byte, vm *Engine) error { 1642 v0, err := vm.dstack.PopInt() 1643 if err != nil { 1644 return err 1645 } 1646 1647 v1, err := vm.dstack.PopInt() 1648 if err != nil { 1649 return err 1650 } 1651 1652 if v1 <= v0 { 1653 vm.dstack.PushInt(scriptNum(1)) 1654 } else { 1655 vm.dstack.PushInt(scriptNum(0)) 1656 } 1657 return nil 1658 } 1659 1660 // opcodeGreaterThanOrEqual treats the top two items on the data stack as 1661 // integers. When the second-to-top item is greater than or equal to the top 1662 // item, they are replaced with a 1, otherwise a 0. 1663 // 1664 // Stack transformation: [... x1 x2] -> [... bool] 1665 func opcodeGreaterThanOrEqual(op *opcode, data []byte, vm *Engine) error { 1666 v0, err := vm.dstack.PopInt() 1667 if err != nil { 1668 return err 1669 } 1670 1671 v1, err := vm.dstack.PopInt() 1672 if err != nil { 1673 return err 1674 } 1675 1676 if v1 >= v0 { 1677 vm.dstack.PushInt(scriptNum(1)) 1678 } else { 1679 vm.dstack.PushInt(scriptNum(0)) 1680 } 1681 1682 return nil 1683 } 1684 1685 // opcodeMin treats the top two items on the data stack as integers and replaces 1686 // them with the minimum of the two. 1687 // 1688 // Stack transformation: [... x1 x2] -> [... min(x1, x2)] 1689 func opcodeMin(op *opcode, data []byte, vm *Engine) error { 1690 v0, err := vm.dstack.PopInt() 1691 if err != nil { 1692 return err 1693 } 1694 1695 v1, err := vm.dstack.PopInt() 1696 if err != nil { 1697 return err 1698 } 1699 1700 if v1 < v0 { 1701 vm.dstack.PushInt(v1) 1702 } else { 1703 vm.dstack.PushInt(v0) 1704 } 1705 1706 return nil 1707 } 1708 1709 // opcodeMax treats the top two items on the data stack as integers and replaces 1710 // them with the maximum of the two. 1711 // 1712 // Stack transformation: [... x1 x2] -> [... max(x1, x2)] 1713 func opcodeMax(op *opcode, data []byte, vm *Engine) error { 1714 v0, err := vm.dstack.PopInt() 1715 if err != nil { 1716 return err 1717 } 1718 1719 v1, err := vm.dstack.PopInt() 1720 if err != nil { 1721 return err 1722 } 1723 1724 if v1 > v0 { 1725 vm.dstack.PushInt(v1) 1726 } else { 1727 vm.dstack.PushInt(v0) 1728 } 1729 1730 return nil 1731 } 1732 1733 // opcodeWithin treats the top 3 items on the data stack as integers. When the 1734 // value to test is within the specified range (left inclusive), they are 1735 // replaced with a 1, otherwise a 0. 1736 // 1737 // The top item is the max value, the second-top-item is the minimum value, and 1738 // the third-to-top item is the value to test. 1739 // 1740 // Stack transformation: [... x1 min max] -> [... bool] 1741 func opcodeWithin(op *opcode, data []byte, vm *Engine) error { 1742 maxVal, err := vm.dstack.PopInt() 1743 if err != nil { 1744 return err 1745 } 1746 1747 minVal, err := vm.dstack.PopInt() 1748 if err != nil { 1749 return err 1750 } 1751 1752 x, err := vm.dstack.PopInt() 1753 if err != nil { 1754 return err 1755 } 1756 1757 if x >= minVal && x < maxVal { 1758 vm.dstack.PushInt(scriptNum(1)) 1759 } else { 1760 vm.dstack.PushInt(scriptNum(0)) 1761 } 1762 return nil 1763 } 1764 1765 // calcHash calculates the hash of hasher over buf. 1766 func calcHash(buf []byte, hasher hash.Hash) []byte { 1767 hasher.Write(buf) 1768 return hasher.Sum(nil) 1769 } 1770 1771 // opcodeRipemd160 treats the top item of the data stack as raw bytes and 1772 // replaces it with ripemd160(data). 1773 // 1774 // Stack transformation: [... x1] -> [... ripemd160(x1)] 1775 func opcodeRipemd160(op *opcode, data []byte, vm *Engine) error { 1776 buf, err := vm.dstack.PopByteArray() 1777 if err != nil { 1778 return err 1779 } 1780 1781 vm.dstack.PushByteArray(calcHash(buf, ripemd160.New())) 1782 return nil 1783 } 1784 1785 // opcodeSha1 treats the top item of the data stack as raw bytes and replaces it 1786 // with sha1(data). 1787 // 1788 // Stack transformation: [... x1] -> [... sha1(x1)] 1789 func opcodeSha1(op *opcode, data []byte, vm *Engine) error { 1790 buf, err := vm.dstack.PopByteArray() 1791 if err != nil { 1792 return err 1793 } 1794 1795 hash := sha1.Sum(buf) 1796 vm.dstack.PushByteArray(hash[:]) 1797 return nil 1798 } 1799 1800 // opcodeSha256 treats the top item of the data stack as raw bytes and replaces 1801 // it with sha256(data). 1802 // 1803 // Stack transformation: [... x1] -> [... sha256(x1)] 1804 func opcodeSha256(op *opcode, data []byte, vm *Engine) error { 1805 buf, err := vm.dstack.PopByteArray() 1806 if err != nil { 1807 return err 1808 } 1809 1810 hash := sha256.Sum256(buf) 1811 vm.dstack.PushByteArray(hash[:]) 1812 return nil 1813 } 1814 1815 // opcodeHash160 treats the top item of the data stack as raw bytes and replaces 1816 // it with ripemd160(sha256(data)). 1817 // 1818 // Stack transformation: [... x1] -> [... ripemd160(sha256(x1))] 1819 func opcodeHash160(op *opcode, data []byte, vm *Engine) error { 1820 buf, err := vm.dstack.PopByteArray() 1821 if err != nil { 1822 return err 1823 } 1824 1825 hash := sha256.Sum256(buf) 1826 vm.dstack.PushByteArray(calcHash(hash[:], ripemd160.New())) 1827 return nil 1828 } 1829 1830 // opcodeHash256 treats the top item of the data stack as raw bytes and replaces 1831 // it with sha256(sha256(data)). 1832 // 1833 // Stack transformation: [... x1] -> [... sha256(sha256(x1))] 1834 func opcodeHash256(op *opcode, data []byte, vm *Engine) error { 1835 buf, err := vm.dstack.PopByteArray() 1836 if err != nil { 1837 return err 1838 } 1839 1840 vm.dstack.PushByteArray(chainhash.DoubleHashB(buf)) 1841 return nil 1842 } 1843 1844 // opcodeCodeSeparator stores the current script offset as the most recently 1845 // seen OP_CODESEPARATOR which is used during signature checking. 1846 // 1847 // This opcode does not change the contents of the data stack. 1848 func opcodeCodeSeparator(op *opcode, data []byte, vm *Engine) error { 1849 vm.lastCodeSep = int(vm.tokenizer.ByteIndex()) 1850 return nil 1851 } 1852 1853 // opcodeCheckSig treats the top 2 items on the stack as a public key and a 1854 // signature and replaces them with a bool which indicates if the signature was 1855 // successfully verified. 1856 // 1857 // The process of verifying a signature requires calculating a signature hash in 1858 // the same way the transaction signer did. It involves hashing portions of the 1859 // transaction based on the hash type byte (which is the final byte of the 1860 // signature) and the portion of the script starting from the most recent 1861 // OP_CODESEPARATOR (or the beginning of the script if there are none) to the 1862 // end of the script (with any other OP_CODESEPARATORs removed). Once this 1863 // "script hash" is calculated, the signature is checked using standard 1864 // cryptographic methods against the provided public key. 1865 // 1866 // Stack transformation: [... signature pubkey] -> [... bool] 1867 func opcodeCheckSig(op *opcode, data []byte, vm *Engine) error { 1868 pkBytes, err := vm.dstack.PopByteArray() 1869 if err != nil { 1870 return err 1871 } 1872 1873 fullSigBytes, err := vm.dstack.PopByteArray() 1874 if err != nil { 1875 return err 1876 } 1877 1878 // The signature actually needs needs to be longer than this, but at 1879 // least 1 byte is needed for the hash type below. The full length is 1880 // checked depending on the script flags and upon parsing the signature. 1881 if len(fullSigBytes) < 1 { 1882 vm.dstack.PushBool(false) 1883 return nil 1884 } 1885 1886 // Trim off hashtype from the signature string and check if the 1887 // signature and pubkey conform to the strict encoding requirements 1888 // depending on the flags. 1889 // 1890 // NOTE: When the strict encoding flags are set, any errors in the 1891 // signature or public encoding here result in an immediate script error 1892 // (and thus no result bool is pushed to the data stack). This differs 1893 // from the logic below where any errors in parsing the signature is 1894 // treated as the signature failure resulting in false being pushed to 1895 // the data stack. This is required because the more general script 1896 // validation consensus rules do not have the new strict encoding 1897 // requirements enabled by the flags. 1898 hashType := SigHashType(fullSigBytes[len(fullSigBytes)-1]) 1899 sigBytes := fullSigBytes[:len(fullSigBytes)-1] 1900 if err := vm.checkHashTypeEncoding(hashType); err != nil { 1901 return err 1902 } 1903 if err := vm.checkSignatureEncoding(sigBytes); err != nil { 1904 return err 1905 } 1906 if err := vm.checkPubKeyEncoding(pkBytes); err != nil { 1907 return err 1908 } 1909 1910 // Get script starting from the most recent OP_CODESEPARATOR. 1911 subScript := vm.subScript() 1912 1913 // Generate the signature hash based on the signature hash type. 1914 var hash []byte 1915 if vm.isWitnessVersionActive(0) { 1916 var sigHashes *TxSigHashes 1917 if vm.hashCache != nil { 1918 sigHashes = vm.hashCache 1919 } else { 1920 sigHashes = NewTxSigHashes(&vm.tx) 1921 } 1922 1923 hash, err = calcWitnessSignatureHashRaw(subScript, sigHashes, hashType, 1924 &vm.tx, vm.txIdx, vm.inputAmount) 1925 if err != nil { 1926 return err 1927 } 1928 } else { 1929 // Remove the signature since there is no way for a signature 1930 // to sign itself. 1931 subScript = removeOpcodeByData(subScript, fullSigBytes) 1932 1933 hash = calcSignatureHash(subScript, hashType, &vm.tx, vm.txIdx) 1934 } 1935 1936 pubKey, err := btcec.ParsePubKey(pkBytes, btcec.S256()) 1937 if err != nil { 1938 vm.dstack.PushBool(false) 1939 return nil 1940 } 1941 1942 var signature *btcec.Signature 1943 if vm.hasFlag(ScriptVerifyStrictEncoding) || 1944 vm.hasFlag(ScriptVerifyDERSignatures) { 1945 1946 signature, err = btcec.ParseDERSignature(sigBytes, btcec.S256()) 1947 } else { 1948 signature, err = btcec.ParseSignature(sigBytes, btcec.S256()) 1949 } 1950 if err != nil { 1951 vm.dstack.PushBool(false) 1952 return nil 1953 } 1954 1955 var valid bool 1956 if vm.sigCache != nil { 1957 var sigHash chainhash.Hash 1958 copy(sigHash[:], hash) 1959 1960 valid = vm.sigCache.Exists(sigHash, signature, pubKey) 1961 if !valid && signature.Verify(hash, pubKey) { 1962 vm.sigCache.Add(sigHash, signature, pubKey) 1963 valid = true 1964 } 1965 } else { 1966 valid = signature.Verify(hash, pubKey) 1967 } 1968 1969 if !valid && vm.hasFlag(ScriptVerifyNullFail) && len(sigBytes) > 0 { 1970 str := "signature not empty on failed checksig" 1971 return scriptError(ErrNullFail, str) 1972 } 1973 1974 vm.dstack.PushBool(valid) 1975 return nil 1976 } 1977 1978 // opcodeCheckSigVerify is a combination of opcodeCheckSig and opcodeVerify. 1979 // The opcodeCheckSig function is invoked followed by opcodeVerify. See the 1980 // documentation for each of those opcodes for more details. 1981 // 1982 // Stack transformation: [... signature pubkey] -> [... bool] -> [...] 1983 func opcodeCheckSigVerify(op *opcode, data []byte, vm *Engine) error { 1984 err := opcodeCheckSig(op, data, vm) 1985 if err == nil { 1986 err = abstractVerify(op, vm, ErrCheckSigVerify) 1987 } 1988 return err 1989 } 1990 1991 // parsedSigInfo houses a raw signature along with its parsed form and a flag 1992 // for whether or not it has already been parsed. It is used to prevent parsing 1993 // the same signature multiple times when verifying a multisig. 1994 type parsedSigInfo struct { 1995 signature []byte 1996 parsedSignature *btcec.Signature 1997 parsed bool 1998 } 1999 2000 // opcodeCheckMultiSig treats the top item on the stack as an integer number of 2001 // public keys, followed by that many entries as raw data representing the public 2002 // keys, followed by the integer number of signatures, followed by that many 2003 // entries as raw data representing the signatures. 2004 // 2005 // Due to a bug in the original Satoshi client implementation, an additional 2006 // dummy argument is also required by the consensus rules, although it is not 2007 // used. The dummy value SHOULD be an OP_0, although that is not required by 2008 // the consensus rules. When the ScriptStrictMultiSig flag is set, it must be 2009 // OP_0. 2010 // 2011 // All of the aforementioned stack items are replaced with a bool which 2012 // indicates if the requisite number of signatures were successfully verified. 2013 // 2014 // See the opcodeCheckSigVerify documentation for more details about the process 2015 // for verifying each signature. 2016 // 2017 // Stack transformation: 2018 // [... dummy [sig ...] numsigs [pubkey ...] numpubkeys] -> [... bool] 2019 func opcodeCheckMultiSig(op *opcode, data []byte, vm *Engine) error { 2020 numKeys, err := vm.dstack.PopInt() 2021 if err != nil { 2022 return err 2023 } 2024 2025 numPubKeys := int(numKeys.Int32()) 2026 if numPubKeys < 0 { 2027 str := fmt.Sprintf("number of pubkeys %d is negative", 2028 numPubKeys) 2029 return scriptError(ErrInvalidPubKeyCount, str) 2030 } 2031 if numPubKeys > MaxPubKeysPerMultiSig { 2032 str := fmt.Sprintf("too many pubkeys: %d > %d", 2033 numPubKeys, MaxPubKeysPerMultiSig) 2034 return scriptError(ErrInvalidPubKeyCount, str) 2035 } 2036 vm.numOps += numPubKeys 2037 if vm.numOps > MaxOpsPerScript { 2038 str := fmt.Sprintf("exceeded max operation limit of %d", 2039 MaxOpsPerScript) 2040 return scriptError(ErrTooManyOperations, str) 2041 } 2042 2043 pubKeys := make([][]byte, 0, numPubKeys) 2044 for i := 0; i < numPubKeys; i++ { 2045 pubKey, err := vm.dstack.PopByteArray() 2046 if err != nil { 2047 return err 2048 } 2049 pubKeys = append(pubKeys, pubKey) 2050 } 2051 2052 numSigs, err := vm.dstack.PopInt() 2053 if err != nil { 2054 return err 2055 } 2056 numSignatures := int(numSigs.Int32()) 2057 if numSignatures < 0 { 2058 str := fmt.Sprintf("number of signatures %d is negative", 2059 numSignatures) 2060 return scriptError(ErrInvalidSignatureCount, str) 2061 2062 } 2063 if numSignatures > numPubKeys { 2064 str := fmt.Sprintf("more signatures than pubkeys: %d > %d", 2065 numSignatures, numPubKeys) 2066 return scriptError(ErrInvalidSignatureCount, str) 2067 } 2068 2069 signatures := make([]*parsedSigInfo, 0, numSignatures) 2070 for i := 0; i < numSignatures; i++ { 2071 signature, err := vm.dstack.PopByteArray() 2072 if err != nil { 2073 return err 2074 } 2075 sigInfo := &parsedSigInfo{signature: signature} 2076 signatures = append(signatures, sigInfo) 2077 } 2078 2079 // A bug in the original Satoshi client implementation means one more 2080 // stack value than should be used must be popped. Unfortunately, this 2081 // buggy behavior is now part of the consensus and a hard fork would be 2082 // required to fix it. 2083 dummy, err := vm.dstack.PopByteArray() 2084 if err != nil { 2085 return err 2086 } 2087 2088 // Since the dummy argument is otherwise not checked, it could be any 2089 // value which unfortunately provides a source of malleability. Thus, 2090 // there is a script flag to force an error when the value is NOT 0. 2091 if vm.hasFlag(ScriptStrictMultiSig) && len(dummy) != 0 { 2092 str := fmt.Sprintf("multisig dummy argument has length %d "+ 2093 "instead of 0", len(dummy)) 2094 return scriptError(ErrSigNullDummy, str) 2095 } 2096 2097 // Get script starting from the most recent OP_CODESEPARATOR. 2098 script := vm.subScript() 2099 2100 // Remove the signature in pre version 0 segwit scripts since there is 2101 // no way for a signature to sign itself. 2102 if !vm.isWitnessVersionActive(0) { 2103 for _, sigInfo := range signatures { 2104 script = removeOpcodeByData(script, sigInfo.signature) 2105 } 2106 } 2107 2108 success := true 2109 numPubKeys++ 2110 pubKeyIdx := -1 2111 signatureIdx := 0 2112 for numSignatures > 0 { 2113 // When there are more signatures than public keys remaining, 2114 // there is no way to succeed since too many signatures are 2115 // invalid, so exit early. 2116 pubKeyIdx++ 2117 numPubKeys-- 2118 if numSignatures > numPubKeys { 2119 success = false 2120 break 2121 } 2122 2123 sigInfo := signatures[signatureIdx] 2124 pubKey := pubKeys[pubKeyIdx] 2125 2126 // The order of the signature and public key evaluation is 2127 // important here since it can be distinguished by an 2128 // OP_CHECKMULTISIG NOT when the strict encoding flag is set. 2129 2130 rawSig := sigInfo.signature 2131 if len(rawSig) == 0 { 2132 // Skip to the next pubkey if signature is empty. 2133 continue 2134 } 2135 2136 // Split the signature into hash type and signature components. 2137 hashType := SigHashType(rawSig[len(rawSig)-1]) 2138 signature := rawSig[:len(rawSig)-1] 2139 2140 // Only parse and check the signature encoding once. 2141 var parsedSig *btcec.Signature 2142 if !sigInfo.parsed { 2143 if err := vm.checkHashTypeEncoding(hashType); err != nil { 2144 return err 2145 } 2146 if err := vm.checkSignatureEncoding(signature); err != nil { 2147 return err 2148 } 2149 2150 // Parse the signature. 2151 var err error 2152 if vm.hasFlag(ScriptVerifyStrictEncoding) || 2153 vm.hasFlag(ScriptVerifyDERSignatures) { 2154 2155 parsedSig, err = btcec.ParseDERSignature(signature, 2156 btcec.S256()) 2157 } else { 2158 parsedSig, err = btcec.ParseSignature(signature, 2159 btcec.S256()) 2160 } 2161 sigInfo.parsed = true 2162 if err != nil { 2163 continue 2164 } 2165 sigInfo.parsedSignature = parsedSig 2166 } else { 2167 // Skip to the next pubkey if the signature is invalid. 2168 if sigInfo.parsedSignature == nil { 2169 continue 2170 } 2171 2172 // Use the already parsed signature. 2173 parsedSig = sigInfo.parsedSignature 2174 } 2175 2176 if err := vm.checkPubKeyEncoding(pubKey); err != nil { 2177 return err 2178 } 2179 2180 // Parse the pubkey. 2181 parsedPubKey, err := btcec.ParsePubKey(pubKey, btcec.S256()) 2182 if err != nil { 2183 continue 2184 } 2185 2186 // Generate the signature hash based on the signature hash type. 2187 var hash []byte 2188 if vm.isWitnessVersionActive(0) { 2189 var sigHashes *TxSigHashes 2190 if vm.hashCache != nil { 2191 sigHashes = vm.hashCache 2192 } else { 2193 sigHashes = NewTxSigHashes(&vm.tx) 2194 } 2195 2196 hash, err = calcWitnessSignatureHashRaw(script, sigHashes, hashType, 2197 &vm.tx, vm.txIdx, vm.inputAmount) 2198 if err != nil { 2199 return err 2200 } 2201 } else { 2202 hash = calcSignatureHash(script, hashType, &vm.tx, vm.txIdx) 2203 } 2204 2205 var valid bool 2206 if vm.sigCache != nil { 2207 var sigHash chainhash.Hash 2208 copy(sigHash[:], hash) 2209 2210 valid = vm.sigCache.Exists(sigHash, parsedSig, parsedPubKey) 2211 if !valid && parsedSig.Verify(hash, parsedPubKey) { 2212 vm.sigCache.Add(sigHash, parsedSig, parsedPubKey) 2213 valid = true 2214 } 2215 } else { 2216 valid = parsedSig.Verify(hash, parsedPubKey) 2217 } 2218 2219 if valid { 2220 // PubKey verified, move on to the next signature. 2221 signatureIdx++ 2222 numSignatures-- 2223 } 2224 } 2225 2226 if !success && vm.hasFlag(ScriptVerifyNullFail) { 2227 for _, sig := range signatures { 2228 if len(sig.signature) > 0 { 2229 str := "not all signatures empty on failed checkmultisig" 2230 return scriptError(ErrNullFail, str) 2231 } 2232 } 2233 } 2234 2235 vm.dstack.PushBool(success) 2236 return nil 2237 } 2238 2239 // opcodeCheckMultiSigVerify is a combination of opcodeCheckMultiSig and 2240 // opcodeVerify. The opcodeCheckMultiSig is invoked followed by opcodeVerify. 2241 // See the documentation for each of those opcodes for more details. 2242 // 2243 // Stack transformation: 2244 // [... dummy [sig ...] numsigs [pubkey ...] numpubkeys] -> [... bool] -> [...] 2245 func opcodeCheckMultiSigVerify(op *opcode, data []byte, vm *Engine) error { 2246 err := opcodeCheckMultiSig(op, data, vm) 2247 if err == nil { 2248 err = abstractVerify(op, vm, ErrCheckMultiSigVerify) 2249 } 2250 return err 2251 } 2252 2253 // OpcodeByName is a map that can be used to lookup an opcode by its 2254 // human-readable name (OP_CHECKMULTISIG, OP_CHECKSIG, etc). 2255 var OpcodeByName = make(map[string]byte) 2256 2257 func init() { 2258 // Initialize the opcode name to value map using the contents of the 2259 // opcode array. Also add entries for "OP_FALSE", "OP_TRUE", and 2260 // "OP_NOP2" since they are aliases for "OP_0", "OP_1", 2261 // and "OP_CHECKLOCKTIMEVERIFY" respectively. 2262 for _, op := range opcodeArray { 2263 OpcodeByName[op.name] = op.value 2264 } 2265 OpcodeByName["OP_FALSE"] = OP_FALSE 2266 OpcodeByName["OP_TRUE"] = OP_TRUE 2267 OpcodeByName["OP_NOP2"] = OP_CHECKLOCKTIMEVERIFY 2268 OpcodeByName["OP_NOP3"] = OP_CHECKSEQUENCEVERIFY 2269 }