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.
11 // ErrorCode identifies a kind of script error.
14 // These constants are used to identify a specific Error.
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
21 // ---------------------------------------
22 // Failures related to improper API usage.
23 // ---------------------------------------
25 // ErrInvalidFlags is returned when the passed flags to NewEngine
26 // contain an invalid combination.
29 // ErrInvalidIndex is returned when an out-of-bounds index is passed to
33 // ErrUnsupportedAddress is returned when a concrete type that
34 // implements a btcutil.Address is not a supported type.
37 // ErrNotMultisigScript is returned from CalcMultiSigStats when the
38 // provided script is not a multisig script.
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
46 // ErrTooMuchNullData is returned from NullDataScript when the length of
47 // the provided data exceeds MaxDataCarrierSize.
50 // ------------------------------------------
51 // Failures related to final execution state.
52 // ------------------------------------------
54 // ErrEarlyReturn is returned when OP_RETURN is executed in the script.
57 // ErrEmptyStack is returned when the script evaluated without error,
58 // but terminated with an empty top stack element.
61 // ErrEvalFalse is returned when the script evaluated without error but
62 // terminated with a false top stack element.
65 // ErrScriptUnfinished is returned when CheckErrorCondition is called on
66 // a script that has not finished executing.
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
75 // -----------------------------------------------------
76 // Failures related to exceeding maximum allowed limits.
77 // -----------------------------------------------------
79 // ErrScriptTooBig is returned if a script is larger than MaxScriptSize.
82 // ErrElementTooBig is returned if the size of an element to be pushed
83 // to the stack is over MaxScriptElementSize.
86 // ErrTooManyOperations is returned if a script has more than
87 // MaxOpsPerScript opcodes that do not push data.
90 // ErrStackOverflow is returned when stack and altstack combined depth
94 // ErrInvalidPubKeyCount is returned when the number of public keys
95 // specified for a multsig is either negative or greater than
96 // MaxPubKeysPerMultiSig.
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
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.
112 // --------------------------------------------
113 // Failures related to verification operations.
114 // --------------------------------------------
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.
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.
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
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
134 // ErrCheckSigVerify is returned when OP_CHECKMULTISIGVERIFY is
135 // encountered in a script and the top item on the data stack does not
137 ErrCheckMultiSigVerify
139 // --------------------------------------------
140 // Failures related to improper use of opcodes.
141 // --------------------------------------------
143 // ErrDisabledOpcode is returned when a disabled opcode is encountered
147 // ErrReservedOpcode is returned when an opcode marked as reserved
148 // is encountered in a script.
151 // ErrMalformedPush is returned when a data push opcode tries to push
152 // more bytes than are left in the script.
155 // ErrInvalidStackOperation is returned when a stack operation is
156 // attempted with a number that is invalid for the current stack size.
157 ErrInvalidStackOperation
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
165 // ---------------------------------
166 // Failures related to malleability.
167 // ---------------------------------
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.
174 // ErrInvalidSigHashType is returned when a signature hash type is not
175 // one of the supported types.
176 ErrInvalidSigHashType
178 // ErrSigDER is returned when a signature is not a canonically-encoded
182 // ErrSigHighS is returned when the ScriptVerifyLowS flag is set and the
183 // script contains any signatures whose S values are higher than the
187 // ErrNotPushOnly is returned when a script that is required to only
188 // push data to the stack performs other operations. A couple of cases
189 // where this applies is for a pay-to-script-hash signature script when
190 // bip16 is active and when the ScriptVerifySigPushOnly flag is set.
193 // ErrSigNullDummy is returned when the ScriptStrictMultiSig flag is set
194 // and a multisig script has anything other than 0 for the extra dummy
198 // ErrPubKeyType is returned when the ScriptVerifyStrictEncoding
199 // flag is set and the script contains invalid public keys.
202 // ErrCleanStack is returned when the ScriptVerifyCleanStack flag
203 // is set, and after evalution, the stack does not contain only a
207 // ErrNullFail is returned when the ScriptVerifyNullFail flag is
208 // set and signatures are not empty on failed checksig or checkmultisig
212 // ErrWitnessMalleated is returned if ScriptVerifyWitness is set and a
213 // native p2wsh program is encountered which has a non-empty sigScript.
216 // ErrWitnessMalleatedP2SH is returned if ScriptVerifyWitness if set
217 // and the validation logic for nested p2sh encounters a sigScript
218 // which isn't *exactyl* a datapush of the witness program.
219 ErrWitnessMalleatedP2SH
221 // -------------------------------
222 // Failures related to soft forks.
223 // -------------------------------
225 // ErrDiscourageUpgradableNOPs is returned when the
226 // ScriptDiscourageUpgradableNops flag is set and a NOP opcode is
227 // encountered in a script.
228 ErrDiscourageUpgradableNOPs
230 // ErrNegativeLockTime is returned when a script contains an opcode that
231 // interprets a negative lock time.
234 // ErrUnsatisfiedLockTime is returned when a script contains an opcode
235 // that involves a lock time and the required lock time has not been
237 ErrUnsatisfiedLockTime
239 // ErrMinimalIf is returned if ScriptVerifyWitness is set and the
240 // operand of an OP_IF/OP_NOF_IF are not either an empty vector or
244 // ErrDiscourageUpgradableWitnessProgram is returned if
245 // ScriptVerifyWitness is set and the versino of an executing witness
246 // program is outside the set of currently defined witness program
248 ErrDiscourageUpgradableWitnessProgram
250 // ----------------------------------------
251 // Failures related to segregated witness.
252 // ----------------------------------------
254 // ErrWitnessProgramEmpty is returned if ScriptVerifyWitness is set and
255 // the witness stack itself is empty.
256 ErrWitnessProgramEmpty
258 // ErrWitnessProgramMismatch is returned if ScriptVerifyWitness is set
259 // and the witness itself for a p2wkh witness program isn't *exactly* 2
260 // items or if the witness for a p2wsh isn't the sha255 of the witness
262 ErrWitnessProgramMismatch
264 // ErrWitnessProgramWrongLength is returned if ScriptVerifyWitness is
265 // set and the length of the witness program violates the length as
266 // dictated by the current witness version.
267 ErrWitnessProgramWrongLength
269 // ErrWitnessUnexpected is returned if ScriptVerifyWitness is set and a
270 // transaction includes witness data but doesn't spend an which is a
271 // witness program (nested or native).
274 // ErrWitnessPubKeyType is returned if ScriptVerifyWitness is set and
275 // the public key used in either a check-sig or check-multi-sig isn't
276 // serialized in a compressed format.
279 // numErrorCodes is the maximum error code number used in tests. This
280 // entry MUST be the last entry in the enum.
284 // Map of ErrorCode values back to their constant names for pretty printing.
285 var errorCodeStrings = map[ErrorCode]string{
286 ErrInternal: "ErrInternal",
287 ErrInvalidFlags: "ErrInvalidFlags",
288 ErrInvalidIndex: "ErrInvalidIndex",
289 ErrUnsupportedAddress: "ErrUnsupportedAddress",
290 ErrNotMultisigScript: "ErrNotMultisigScript",
291 ErrTooManyRequiredSigs: "ErrTooManyRequiredSigs",
292 ErrTooMuchNullData: "ErrTooMuchNullData",
293 ErrEarlyReturn: "ErrEarlyReturn",
294 ErrEmptyStack: "ErrEmptyStack",
295 ErrEvalFalse: "ErrEvalFalse",
296 ErrScriptUnfinished: "ErrScriptUnfinished",
297 ErrInvalidProgramCounter: "ErrInvalidProgramCounter",
298 ErrScriptTooBig: "ErrScriptTooBig",
299 ErrElementTooBig: "ErrElementTooBig",
300 ErrTooManyOperations: "ErrTooManyOperations",
301 ErrStackOverflow: "ErrStackOverflow",
302 ErrInvalidPubKeyCount: "ErrInvalidPubKeyCount",
303 ErrInvalidSignatureCount: "ErrInvalidSignatureCount",
304 ErrNumberTooBig: "ErrNumberTooBig",
305 ErrVerify: "ErrVerify",
306 ErrEqualVerify: "ErrEqualVerify",
307 ErrNumEqualVerify: "ErrNumEqualVerify",
308 ErrCheckSigVerify: "ErrCheckSigVerify",
309 ErrCheckMultiSigVerify: "ErrCheckMultiSigVerify",
310 ErrDisabledOpcode: "ErrDisabledOpcode",
311 ErrReservedOpcode: "ErrReservedOpcode",
312 ErrMalformedPush: "ErrMalformedPush",
313 ErrInvalidStackOperation: "ErrInvalidStackOperation",
314 ErrUnbalancedConditional: "ErrUnbalancedConditional",
315 ErrMinimalData: "ErrMinimalData",
316 ErrInvalidSigHashType: "ErrInvalidSigHashType",
317 ErrSigDER: "ErrSigDER",
318 ErrSigHighS: "ErrSigHighS",
319 ErrNotPushOnly: "ErrNotPushOnly",
320 ErrSigNullDummy: "ErrSigNullDummy",
321 ErrPubKeyType: "ErrPubKeyType",
322 ErrCleanStack: "ErrCleanStack",
323 ErrNullFail: "ErrNullFail",
324 ErrDiscourageUpgradableNOPs: "ErrDiscourageUpgradableNOPs",
325 ErrNegativeLockTime: "ErrNegativeLockTime",
326 ErrUnsatisfiedLockTime: "ErrUnsatisfiedLockTime",
327 ErrWitnessProgramEmpty: "ErrWitnessProgramEmpty",
328 ErrWitnessProgramMismatch: "ErrWitnessProgramMismatch",
329 ErrWitnessProgramWrongLength: "ErrWitnessProgramWrongLength",
330 ErrWitnessMalleated: "ErrWitnessMalleated",
331 ErrWitnessMalleatedP2SH: "ErrWitnessMalleatedP2SH",
332 ErrWitnessUnexpected: "ErrWitnessUnexpected",
333 ErrMinimalIf: "ErrMinimalIf",
334 ErrWitnessPubKeyType: "ErrWitnessPubKeyType",
335 ErrDiscourageUpgradableWitnessProgram: "ErrDiscourageUpgradableWitnessProgram",
338 // String returns the ErrorCode as a human-readable name.
339 func (e ErrorCode) String() string {
340 if s := errorCodeStrings[e]; s != "" {
343 return fmt.Sprintf("Unknown ErrorCode (%d)", int(e))
346 // Error identifies a script-related error. It is used to indicate three
347 // classes of errors:
348 // 1) Script execution failures due to violating one of the many requirements
349 // imposed by the script engine or evaluating to false
350 // 2) Improper API usage by callers
351 // 3) Internal consistency check failures
353 // The caller can use type assertions on the returned errors to access the
354 // ErrorCode field to ascertain the specific reason for the error. As an
355 // additional convenience, the caller may make use of the IsErrorCode function
356 // to check for a specific error code.
362 // Error satisfies the error interface and prints human-readable errors.
363 func (e Error) Error() string {
367 // scriptError creates an Error given a set of arguments.
368 func scriptError(c ErrorCode, desc string) Error {
369 return Error{ErrorCode: c, Description: desc}
372 // IsErrorCode returns whether or not the provided error is a script error with
373 // the provided error code.
374 func IsErrorCode(err error, c ErrorCode) bool {
375 serr, ok := err.(Error)
376 return ok && serr.ErrorCode == c