github.com/palcoin-project/palcd@v1.0.0/txscript/error.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 "fmt" 9 ) 10 11 // ErrorCode identifies a kind of script error. 12 type ErrorCode int 13 14 // These constants are used to identify a specific Error. 15 const ( 16 // ErrInternal is returned if internal consistency checks fail. In 17 // practice this error should never be seen as it would mean there is an 18 // error in the engine logic. 19 ErrInternal ErrorCode = iota 20 21 // --------------------------------------- 22 // Failures related to improper API usage. 23 // --------------------------------------- 24 25 // ErrInvalidFlags is returned when the passed flags to NewEngine 26 // contain an invalid combination. 27 ErrInvalidFlags 28 29 // ErrInvalidIndex is returned when an out-of-bounds index is passed to 30 // a function. 31 ErrInvalidIndex 32 33 // ErrUnsupportedAddress is returned when a concrete type that 34 // implements a palcutil.Address is not a supported type. 35 ErrUnsupportedAddress 36 37 // ErrNotMultisigScript is returned from CalcMultiSigStats when the 38 // provided script is not a multisig script. 39 ErrNotMultisigScript 40 41 // ErrTooManyRequiredSigs is returned from MultiSigScript when the 42 // specified number of required signatures is larger than the number of 43 // provided public keys. 44 ErrTooManyRequiredSigs 45 46 // ErrTooMuchNullData is returned from NullDataScript when the length of 47 // the provided data exceeds MaxDataCarrierSize. 48 ErrTooMuchNullData 49 50 // ------------------------------------------ 51 // Failures related to final execution state. 52 // ------------------------------------------ 53 54 // ErrEarlyReturn is returned when OP_RETURN is executed in the script. 55 ErrEarlyReturn 56 57 // ErrEmptyStack is returned when the script evaluated without error, 58 // but terminated with an empty top stack element. 59 ErrEmptyStack 60 61 // ErrEvalFalse is returned when the script evaluated without error but 62 // terminated with a false top stack element. 63 ErrEvalFalse 64 65 // ErrScriptUnfinished is returned when CheckErrorCondition is called on 66 // a script that has not finished executing. 67 ErrScriptUnfinished 68 69 // ErrScriptDone is returned when an attempt to execute an opcode is 70 // made once all of them have already been executed. This can happen 71 // due to things such as a second call to Execute or calling Step after 72 // all opcodes have already been executed. 73 ErrInvalidProgramCounter 74 75 // ----------------------------------------------------- 76 // Failures related to exceeding maximum allowed limits. 77 // ----------------------------------------------------- 78 79 // ErrScriptTooBig is returned if a script is larger than MaxScriptSize. 80 ErrScriptTooBig 81 82 // ErrElementTooBig is returned if the size of an element to be pushed 83 // to the stack is over MaxScriptElementSize. 84 ErrElementTooBig 85 86 // ErrTooManyOperations is returned if a script has more than 87 // MaxOpsPerScript opcodes that do not push data. 88 ErrTooManyOperations 89 90 // ErrStackOverflow is returned when stack and altstack combined depth 91 // is over the limit. 92 ErrStackOverflow 93 94 // ErrInvalidPubKeyCount is returned when the number of public keys 95 // specified for a multsig is either negative or greater than 96 // MaxPubKeysPerMultiSig. 97 ErrInvalidPubKeyCount 98 99 // ErrInvalidSignatureCount is returned when the number of signatures 100 // specified for a multisig is either negative or greater than the 101 // number of public keys. 102 ErrInvalidSignatureCount 103 104 // ErrNumberTooBig is returned when the argument for an opcode that 105 // expects numeric input is larger than the expected maximum number of 106 // bytes. For the most part, opcodes that deal with stack manipulation 107 // via offsets, arithmetic, numeric comparison, and boolean logic are 108 // those that this applies to. However, any opcode that expects numeric 109 // input may fail with this code. 110 ErrNumberTooBig 111 112 // -------------------------------------------- 113 // Failures related to verification operations. 114 // -------------------------------------------- 115 116 // ErrVerify is returned when OP_VERIFY is encountered in a script and 117 // the top item on the data stack does not evaluate to true. 118 ErrVerify 119 120 // ErrEqualVerify is returned when OP_EQUALVERIFY is encountered in a 121 // script and the top item on the data stack does not evaluate to true. 122 ErrEqualVerify 123 124 // ErrNumEqualVerify is returned when OP_NUMEQUALVERIFY is encountered 125 // in a script and the top item on the data stack does not evaluate to 126 // true. 127 ErrNumEqualVerify 128 129 // ErrCheckSigVerify is returned when OP_CHECKSIGVERIFY is encountered 130 // in a script and the top item on the data stack does not evaluate to 131 // true. 132 ErrCheckSigVerify 133 134 // ErrCheckSigVerify is returned when OP_CHECKMULTISIGVERIFY is 135 // encountered in a script and the top item on the data stack does not 136 // evaluate to true. 137 ErrCheckMultiSigVerify 138 139 // -------------------------------------------- 140 // Failures related to improper use of opcodes. 141 // -------------------------------------------- 142 143 // ErrDisabledOpcode is returned when a disabled opcode is encountered 144 // in a script. 145 ErrDisabledOpcode 146 147 // ErrReservedOpcode is returned when an opcode marked as reserved 148 // is encountered in a script. 149 ErrReservedOpcode 150 151 // ErrMalformedPush is returned when a data push opcode tries to push 152 // more bytes than are left in the script. 153 ErrMalformedPush 154 155 // ErrInvalidStackOperation is returned when a stack operation is 156 // attempted with a number that is invalid for the current stack size. 157 ErrInvalidStackOperation 158 159 // ErrUnbalancedConditional is returned when an OP_ELSE or OP_ENDIF is 160 // encountered in a script without first having an OP_IF or OP_NOTIF or 161 // the end of script is reached without encountering an OP_ENDIF when 162 // an OP_IF or OP_NOTIF was previously encountered. 163 ErrUnbalancedConditional 164 165 // --------------------------------- 166 // Failures related to malleability. 167 // --------------------------------- 168 169 // ErrMinimalData is returned when the ScriptVerifyMinimalData flag 170 // is set and the script contains push operations that do not use 171 // the minimal opcode required. 172 ErrMinimalData 173 174 // ErrInvalidSigHashType is returned when a signature hash type is not 175 // one of the supported types. 176 ErrInvalidSigHashType 177 178 // ErrSigTooShort is returned when a signature that should be a 179 // canonically-encoded DER signature is too short. 180 ErrSigTooShort 181 182 // ErrSigTooLong is returned when a signature that should be a 183 // canonically-encoded DER signature is too long. 184 ErrSigTooLong 185 186 // ErrSigInvalidSeqID is returned when a signature that should be a 187 // canonically-encoded DER signature does not have the expected ASN.1 188 // sequence ID. 189 ErrSigInvalidSeqID 190 191 // ErrSigInvalidDataLen is returned a signature that should be a 192 // canonically-encoded DER signature does not specify the correct number 193 // of remaining bytes for the R and S portions. 194 ErrSigInvalidDataLen 195 196 // ErrSigMissingSTypeID is returned a signature that should be a 197 // canonically-encoded DER signature does not provide the ASN.1 type ID 198 // for S. 199 ErrSigMissingSTypeID 200 201 // ErrSigMissingSLen is returned when a signature that should be a 202 // canonically-encoded DER signature does not provide the length of S. 203 ErrSigMissingSLen 204 205 // ErrSigInvalidSLen is returned a signature that should be a 206 // canonically-encoded DER signature does not specify the correct number 207 // of bytes for the S portion. 208 ErrSigInvalidSLen 209 210 // ErrSigInvalidRIntID is returned when a signature that should be a 211 // canonically-encoded DER signature does not have the expected ASN.1 212 // integer ID for R. 213 ErrSigInvalidRIntID 214 215 // ErrSigZeroRLen is returned when a signature that should be a 216 // canonically-encoded DER signature has an R length of zero. 217 ErrSigZeroRLen 218 219 // ErrSigNegativeR is returned when a signature that should be a 220 // canonically-encoded DER signature has a negative value for R. 221 ErrSigNegativeR 222 223 // ErrSigTooMuchRPadding is returned when a signature that should be a 224 // canonically-encoded DER signature has too much padding for R. 225 ErrSigTooMuchRPadding 226 227 // ErrSigInvalidSIntID is returned when a signature that should be a 228 // canonically-encoded DER signature does not have the expected ASN.1 229 // integer ID for S. 230 ErrSigInvalidSIntID 231 232 // ErrSigZeroSLen is returned when a signature that should be a 233 // canonically-encoded DER signature has an S length of zero. 234 ErrSigZeroSLen 235 236 // ErrSigNegativeS is returned when a signature that should be a 237 // canonically-encoded DER signature has a negative value for S. 238 ErrSigNegativeS 239 240 // ErrSigTooMuchSPadding is returned when a signature that should be a 241 // canonically-encoded DER signature has too much padding for S. 242 ErrSigTooMuchSPadding 243 244 // ErrSigHighS is returned when the ScriptVerifyLowS flag is set and the 245 // script contains any signatures whose S values are higher than the 246 // half order. 247 ErrSigHighS 248 249 // ErrNotPushOnly is returned when a script that is required to only 250 // push data to the stack performs other operations. A couple of cases 251 // where this applies is for a pay-to-script-hash signature script when 252 // bip16 is active and when the ScriptVerifySigPushOnly flag is set. 253 ErrNotPushOnly 254 255 // ErrSigNullDummy is returned when the ScriptStrictMultiSig flag is set 256 // and a multisig script has anything other than 0 for the extra dummy 257 // argument. 258 ErrSigNullDummy 259 260 // ErrPubKeyType is returned when the ScriptVerifyStrictEncoding 261 // flag is set and the script contains invalid public keys. 262 ErrPubKeyType 263 264 // ErrCleanStack is returned when the ScriptVerifyCleanStack flag 265 // is set, and after evalution, the stack does not contain only a 266 // single element. 267 ErrCleanStack 268 269 // ErrNullFail is returned when the ScriptVerifyNullFail flag is 270 // set and signatures are not empty on failed checksig or checkmultisig 271 // operations. 272 ErrNullFail 273 274 // ErrWitnessMalleated is returned if ScriptVerifyWitness is set and a 275 // native p2wsh program is encountered which has a non-empty sigScript. 276 ErrWitnessMalleated 277 278 // ErrWitnessMalleatedP2SH is returned if ScriptVerifyWitness if set 279 // and the validation logic for nested p2sh encounters a sigScript 280 // which isn't *exactyl* a datapush of the witness program. 281 ErrWitnessMalleatedP2SH 282 283 // ------------------------------- 284 // Failures related to soft forks. 285 // ------------------------------- 286 287 // ErrDiscourageUpgradableNOPs is returned when the 288 // ScriptDiscourageUpgradableNops flag is set and a NOP opcode is 289 // encountered in a script. 290 ErrDiscourageUpgradableNOPs 291 292 // ErrNegativeLockTime is returned when a script contains an opcode that 293 // interprets a negative lock time. 294 ErrNegativeLockTime 295 296 // ErrUnsatisfiedLockTime is returned when a script contains an opcode 297 // that involves a lock time and the required lock time has not been 298 // reached. 299 ErrUnsatisfiedLockTime 300 301 // ErrMinimalIf is returned if ScriptVerifyWitness is set and the 302 // operand of an OP_IF/OP_NOF_IF are not either an empty vector or 303 // [0x01]. 304 ErrMinimalIf 305 306 // ErrDiscourageUpgradableWitnessProgram is returned if 307 // ScriptVerifyWitness is set and the versino of an executing witness 308 // program is outside the set of currently defined witness program 309 // vesions. 310 ErrDiscourageUpgradableWitnessProgram 311 312 // ---------------------------------------- 313 // Failures related to segregated witness. 314 // ---------------------------------------- 315 316 // ErrWitnessProgramEmpty is returned if ScriptVerifyWitness is set and 317 // the witness stack itself is empty. 318 ErrWitnessProgramEmpty 319 320 // ErrWitnessProgramMismatch is returned if ScriptVerifyWitness is set 321 // and the witness itself for a p2wkh witness program isn't *exactly* 2 322 // items or if the witness for a p2wsh isn't the sha255 of the witness 323 // script. 324 ErrWitnessProgramMismatch 325 326 // ErrWitnessProgramWrongLength is returned if ScriptVerifyWitness is 327 // set and the length of the witness program violates the length as 328 // dictated by the current witness version. 329 ErrWitnessProgramWrongLength 330 331 // ErrWitnessUnexpected is returned if ScriptVerifyWitness is set and a 332 // transaction includes witness data but doesn't spend an which is a 333 // witness program (nested or native). 334 ErrWitnessUnexpected 335 336 // ErrWitnessPubKeyType is returned if ScriptVerifyWitness is set and 337 // the public key used in either a check-sig or check-multi-sig isn't 338 // serialized in a compressed format. 339 ErrWitnessPubKeyType 340 341 // numErrorCodes is the maximum error code number used in tests. This 342 // entry MUST be the last entry in the enum. 343 numErrorCodes 344 ) 345 346 // Map of ErrorCode values back to their constant names for pretty printing. 347 var errorCodeStrings = map[ErrorCode]string{ 348 ErrInternal: "ErrInternal", 349 ErrInvalidFlags: "ErrInvalidFlags", 350 ErrInvalidIndex: "ErrInvalidIndex", 351 ErrUnsupportedAddress: "ErrUnsupportedAddress", 352 ErrNotMultisigScript: "ErrNotMultisigScript", 353 ErrTooManyRequiredSigs: "ErrTooManyRequiredSigs", 354 ErrTooMuchNullData: "ErrTooMuchNullData", 355 ErrEarlyReturn: "ErrEarlyReturn", 356 ErrEmptyStack: "ErrEmptyStack", 357 ErrEvalFalse: "ErrEvalFalse", 358 ErrScriptUnfinished: "ErrScriptUnfinished", 359 ErrInvalidProgramCounter: "ErrInvalidProgramCounter", 360 ErrScriptTooBig: "ErrScriptTooBig", 361 ErrElementTooBig: "ErrElementTooBig", 362 ErrTooManyOperations: "ErrTooManyOperations", 363 ErrStackOverflow: "ErrStackOverflow", 364 ErrInvalidPubKeyCount: "ErrInvalidPubKeyCount", 365 ErrInvalidSignatureCount: "ErrInvalidSignatureCount", 366 ErrNumberTooBig: "ErrNumberTooBig", 367 ErrVerify: "ErrVerify", 368 ErrEqualVerify: "ErrEqualVerify", 369 ErrNumEqualVerify: "ErrNumEqualVerify", 370 ErrCheckSigVerify: "ErrCheckSigVerify", 371 ErrCheckMultiSigVerify: "ErrCheckMultiSigVerify", 372 ErrDisabledOpcode: "ErrDisabledOpcode", 373 ErrReservedOpcode: "ErrReservedOpcode", 374 ErrMalformedPush: "ErrMalformedPush", 375 ErrInvalidStackOperation: "ErrInvalidStackOperation", 376 ErrUnbalancedConditional: "ErrUnbalancedConditional", 377 ErrMinimalData: "ErrMinimalData", 378 ErrInvalidSigHashType: "ErrInvalidSigHashType", 379 ErrSigTooShort: "ErrSigTooShort", 380 ErrSigTooLong: "ErrSigTooLong", 381 ErrSigInvalidSeqID: "ErrSigInvalidSeqID", 382 ErrSigInvalidDataLen: "ErrSigInvalidDataLen", 383 ErrSigMissingSTypeID: "ErrSigMissingSTypeID", 384 ErrSigMissingSLen: "ErrSigMissingSLen", 385 ErrSigInvalidSLen: "ErrSigInvalidSLen", 386 ErrSigInvalidRIntID: "ErrSigInvalidRIntID", 387 ErrSigZeroRLen: "ErrSigZeroRLen", 388 ErrSigNegativeR: "ErrSigNegativeR", 389 ErrSigTooMuchRPadding: "ErrSigTooMuchRPadding", 390 ErrSigInvalidSIntID: "ErrSigInvalidSIntID", 391 ErrSigZeroSLen: "ErrSigZeroSLen", 392 ErrSigNegativeS: "ErrSigNegativeS", 393 ErrSigTooMuchSPadding: "ErrSigTooMuchSPadding", 394 ErrSigHighS: "ErrSigHighS", 395 ErrNotPushOnly: "ErrNotPushOnly", 396 ErrSigNullDummy: "ErrSigNullDummy", 397 ErrPubKeyType: "ErrPubKeyType", 398 ErrCleanStack: "ErrCleanStack", 399 ErrNullFail: "ErrNullFail", 400 ErrDiscourageUpgradableNOPs: "ErrDiscourageUpgradableNOPs", 401 ErrNegativeLockTime: "ErrNegativeLockTime", 402 ErrUnsatisfiedLockTime: "ErrUnsatisfiedLockTime", 403 ErrWitnessProgramEmpty: "ErrWitnessProgramEmpty", 404 ErrWitnessProgramMismatch: "ErrWitnessProgramMismatch", 405 ErrWitnessProgramWrongLength: "ErrWitnessProgramWrongLength", 406 ErrWitnessMalleated: "ErrWitnessMalleated", 407 ErrWitnessMalleatedP2SH: "ErrWitnessMalleatedP2SH", 408 ErrWitnessUnexpected: "ErrWitnessUnexpected", 409 ErrMinimalIf: "ErrMinimalIf", 410 ErrWitnessPubKeyType: "ErrWitnessPubKeyType", 411 ErrDiscourageUpgradableWitnessProgram: "ErrDiscourageUpgradableWitnessProgram", 412 } 413 414 // String returns the ErrorCode as a human-readable name. 415 func (e ErrorCode) String() string { 416 if s := errorCodeStrings[e]; s != "" { 417 return s 418 } 419 return fmt.Sprintf("Unknown ErrorCode (%d)", int(e)) 420 } 421 422 // Error identifies a script-related error. It is used to indicate three 423 // classes of errors: 424 // 1) Script execution failures due to violating one of the many requirements 425 // imposed by the script engine or evaluating to false 426 // 2) Improper API usage by callers 427 // 3) Internal consistency check failures 428 // 429 // The caller can use type assertions on the returned errors to access the 430 // ErrorCode field to ascertain the specific reason for the error. As an 431 // additional convenience, the caller may make use of the IsErrorCode function 432 // to check for a specific error code. 433 type Error struct { 434 ErrorCode ErrorCode 435 Description string 436 } 437 438 // Error satisfies the error interface and prints human-readable errors. 439 func (e Error) Error() string { 440 return e.Description 441 } 442 443 // scriptError creates an Error given a set of arguments. 444 func scriptError(c ErrorCode, desc string) Error { 445 return Error{ErrorCode: c, Description: desc} 446 } 447 448 // IsErrorCode returns whether or not the provided error is a script error with 449 // the provided error code. 450 func IsErrorCode(err error, c ErrorCode) bool { 451 serr, ok := err.(Error) 452 return ok && serr.ErrorCode == c 453 }