How do I fix my check-multisig if-else output script?

2

I'm trying to create a custom script with if/else and a multisig operation in one branch. Currently, my script looks thus:

OP_IF
    <pub1>
    OP_CHECKSIG
OP_ELSE
    OP_2
    <pub2>
    <pub3>
    <pub4>
    OP_3
    OP_CHECKMULTISIG
OP_ENDIF

I have calculated a testnet address (2NF3yXy2MambEnJuY7eStLNcX92WSxytLvC) from this example script and sent testcoin there. Then, I tried spending the money with the following sig script:

OP_0 <sig2> <sig3> <redeemScript>

However, when trying to broadcast the transaction, I get an error:

PUSH TRANSACTION ERROR: 16: MANDATORY-SCRIPT-VERIFY-FLAG-FAILED (SIGNATURE MUST BE ZERO FOR FAILED CHECK(MULTI)SIG OPERATION)

For reference, here's my raw transaction:

0100000001a492c575e44450a9be9ec4520ea365c53ea6f2018130d682ebf97bdf1b1a15ca01000000fd230100483045022100f502ecfd2b7bbb3e7092b320c568abe0d68dd7e613ab3efcb9e479b2641a73a502202b416bb86bce3d242084f8e24cbed76e5a2c5a0795a5bbd7f3366bf6d086ae4f0147304402201022c7548b55f3fbe3ddf427ad2ae5c6a776e2a63ee25376dca123d53d0f940f022041aa7967208ee8aee1811f02d6c7db0ad2872304133d46dbdd117e9bc09d8c97014c8f632102bc050e51811c2c454bf3487129b19d60b87f7a51fb434e6073def19dc55cf4b3ac67522102a906b96ea2df77aff1f84cc9949550f8d6b8f3db5acd625d12d70b6da4b94b1e2102046e854e69a49dd210dbf554f1e420893935b47123667a1834def9fe5d44358a210325db22e2aef6f143026500522431d4f348f911562316350852e6382da6a048a753ae68ffffffff01403fcb000000000017a91429b5c822f40f80cce593460927095ea6d8d720bc8700000000

Its sig script is this:

00483045022100f502ecfd2b7bbb3e7092b320c568abe0d68dd7e613ab3efcb9e479b2641a73a502202b416bb86bce3d242084f8e24cbed76e5a2c5a0795a5bbd7f3366bf6d086ae4f0147304402201022c7548b55f3fbe3ddf427ad2ae5c6a776e2a63ee25376dca123d53d0f940f022041aa7967208ee8aee1811f02d6c7db0ad2872304133d46dbdd117e9bc09d8c97014c8f632102bc050e51811c2c454bf3487129b19d60b87f7a51fb434e6073def19dc55cf4b3ac67522102a906b96ea2df77aff1f84cc9949550f8d6b8f3db5acd625d12d70b6da4b94b1e2102046e854e69a49dd210dbf554f1e420893935b47123667a1834def9fe5d44358a210325db22e2aef6f143026500522431d4f348f911562316350852e6382da6a048a753ae68

Which decodes to

0 3045022100f502ecfd2b7bbb3e7092b320c568abe0d68dd7e613ab3efcb9e479b2641a73a502202b416bb86bce3d242084f8e24cbed76e5a2c5a0795a5bbd7f3366bf6d086ae4f[ALL] 304402201022c7548b55f3fbe3ddf427ad2ae5c6a776e2a63ee25376dca123d53d0f940f022041aa7967208ee8aee1811f02d6c7db0ad2872304133d46dbdd117e9bc09d8c97[ALL] 632102bc050e51811c2c454bf3487129b19d60b87f7a51fb434e6073def19dc55cf4b3ac67522102a906b96ea2df77aff1f84cc9949550f8d6b8f3db5acd625d12d70b6da4b94b1e2102046e854e69a49dd210dbf554f1e420893935b47123667a1834def9fe5d44358a210325db22e2aef6f143026500522431d4f348f911562316350852e6382da6a048a753ae68

The last line is clearly visible as the redeem script, which in turn decodes to this:

OP_IF 02bc050e51811c2c454bf3487129b19d60b87f7a51fb434e6073def19dc55cf4b3 OP_CHECKSIG OP_ELSE 2 02a906b96ea2df77aff1f84cc9949550f8d6b8f3db5acd625d12d70b6da4b94b1e 02046e854e69a49dd210dbf554f1e420893935b47123667a1834def9fe5d44358a 0325db22e2aef6f143026500522431d4f348f911562316350852e6382da6a048a7 3 OP_CHECKMULTISIG OP_ENDIF

Is my sig script incorrect? Is the output script invalid? How can I fix this and make it work?

Thank you very much in advance!

EDIT: After taking into consideration Pieter's comment but placing the extra OP_0 right after the two signatures in the sig script, it worked. Here's the new decoded sig script:

0 3045022100f502ecfd2b7bbb3e7092b320c568abe0d68dd7e613ab3efcb9e479b2641a73a502202b416bb86bce3d242084f8e24cbed76e5a2c5a0795a5bbd7f3366bf6d086ae4f[ALL] 304402201022c7548b55f3fbe3ddf427ad2ae5c6a776e2a63ee25376dca123d53d0f940f022041aa7967208ee8aee1811f02d6c7db0ad2872304133d46dbdd117e9bc09d8c97[ALL] 0 632102bc050e51811c2c454bf3487129b19d60b87f7a51fb434e6073def19dc55cf4b3ac67522102a906b96ea2df77aff1f84cc9949550f8d6b8f3db5acd625d12d70b6da4b94b1e2102046e854e69a49dd210dbf554f1e420893935b47123667a1834def9fe5d44358a210325db22e2aef6f143026500522431d4f348f911562316350852e6382da6a048a753ae68

And here's the link to the transaction: https://testnet.smartbit.com.au/tx/1894cb91fde60432e59d60f30de503e8b6fbe5d1756bbf0ef7302b57bf31d195

arik

Posted 2018-03-25T02:10:58.770

Reputation: 135

2I believe you may need two OP_0's at the start: one to be consumed by OP_IF, and one which is popped off by OP_CHECKMULTISIG. I'll turn this into a full answer once I've identified the issue.Pieter Wuille 2018-03-25T03:17:48.667

Thanks for your response! I just tried it, and I'm still getting the same error: PUSH TRANSACTION ERROR: 16: MANDATORY-SCRIPT-VERIFY-FLAG-FAILED (SIGNATURE MUST BE ZERO FOR FAILED CHECK(MULTI)SIG OPERATION)arik 2018-03-25T03:33:35.827

~30min in the Video Andreas explains the „IF“ handling of script. There must be a value before the IF statement. Either 1 for the If-branch or a 0 for the else branch. https://m.youtube.com/watch?v=yU3Sr07Qnxg

pebwindkraft 2018-03-25T08:33:54.423

@pebwindkraft: that should be an answer. ;)Murch 2018-03-25T18:45:45.423

@murch: not chasing for credits anymore :-) Pieter and you have provided the answers, and this is just "a little bit" additional explanation, which might help the users to better understand the previous comments.pebwindkraft 2018-03-26T08:19:29.383

@pebwindkraft: Fair enough. However, it's not only about the reputation, it's also that comments shouldn't be used to answer questions. ;)Murch 2018-03-26T16:34:49.937

Answers

5

OP_IF consumes a number from the top of the stack.
The OP_IF will evaluate to true if the number is not 0, and to false if the number is 0.

H/T for this answer by David Schwartz, and to Pieter Wuille for getting me on the right track.

Murch

Posted 2018-03-25T02:10:58.770

Reputation: 41 609