1

Is a script spendable if the top item left on the stack evaluates to "TRUE", and that is the only item left in the stack?

In other words, is it okay for multiple items to be left on the stack?


For example,

A scriptPubKey + scriptSig is spenable if an OP_1 (or anything other than OP_0 or nothing) is left on top of the stack after the script has finished executing.

scriptPubKey: OP_DUP OP_HASH160 <pubkeyhash> OP_EQUALVERIFY OP_CHECKSIG
scriptSig:    <signature> <pubkey>

If the <pubkey> hashes to the <pubkeyhash>, and the <signature> corresponds the <pubkey>, then OP_CHECKSIG will end up leaving the following on the stack:

stack: OP_1

However, if you had the following scriptPubKey + scriptSig:

scriptPubKey: OP_2 OP_3
scriptSig:    OP_1

This would leave the following stack:

stack: OP_1 OP_2 OP_3

Would this evaluate to TRUE and mean the script is spendable?

I'm getting a non-mandatory-script-verify-flag (Extra items left on stack after execution) (code 64) error, and I'm wondering if this is the problem.

2

Yes, this is the problem.

Pre-segwit there is a standardness rule in Bitcoin Core that requires a single element only on the stack after execution.

In P2WSH, this is part of the consensus rules.

The reason for this is that without such a rule it is trivial to malleate any transaction: just prefix the scriptSig with additional garbage elements, which would never be inspected by the locking script, and thus just passed through.

| improve this answer | |
  • Thank you, makes perfect sense. I take it that my OP_2 OP_3 scriptPubKey is unspendable unless I mine it myself? – inersha Dec 1 '19 at 0:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.