I have been following BIP16 in creating a P2SH transaction. I want to create a simple (one signature) P2SH. I am getting an error when trying to broadcast the tx. Error is 16: mandatory-script-verify-flag-failed (Signature must be zero for failed CHECK(MULTI)SIG operation)
To be clear, I am not trying multisig. What I am doing can be best described as P2SH-P2PK
, though I have not seen this term.
This is the only example given in BIP16, so this should work.
Here is what I have tried (testnet):
private key: BB2AC60BC518C0E239D5AF9D8D051A6BDFD0D931268DCA70C59E5992
public key: 039f53e45f8f18b8ed294378bda342eff69b2053debf27fbede7d2d6bd84be6235
(compressed)
redeemScript: [{pubKeySize,pubKey}] [OP_CHECKSIG]
21039F53E45F8F18B8ED294378BDA342EFF69B2053DEBF27FBEDE7D2D6BD84BE6235AC
redeemScriptHash: ripeMD160RIPEMD160(sha256SHA256(redeemScript))
323D0E8A083E98010299109337850D05DD6157F7
p2shAddress: Base58Check(0xC4 + redeemScriptHash) (C4 for testnet)
2MwprvB9tUMtX4vK8zJK8K329fNu79CJgR7
The above address has two UTOXs on testnet:
UTXO1: e434a13cac79dc3d26e7279bff05c0f071a2df03e2ba6ca13c88f0e82dca9998:0
UTXO2: 9b4943e7ab5f4512e42c94254eb6aab4c6823ce06d4ff816b7ce4fda155a2571:0
I now want to spent both UTXOs and keep following outputs:
Out1: 2MwprvB9tUMtX4vK8zJK8K329fNu79CJgR7 34000000 Satoshis
Out2: 2N8hwP1WmJrFF5QWABn38y63uYLhnJYJYTF 100000000 Satoshis
I created the tx as follows:
Create unsigned transactions with all scriptSigs empty
For each input to sign, set its scriptSig to redeemScript: 21039F53E45F8F18B8ED294378BDA342EFF69B2053DEBF27FBEDE7D2D6BD84BE6235AC
, keeping other scriptSigs to empty.
Serialize the new tx and append SIGHASHALL bytes (0x01) in little endian 4 bytes
Double Sha256 above and sign resulting value. Add one byte 0x01 to signature to indicate SIGHASH_ALL. Let sig
be the resulting value.
Set the scriptSig as:
[0x00] [{sigSize, sig}] [{pubKeySize, pubKey}] [OP_CHECKSIG]
I am assuming that the following scriptPubKey will be used:
[OP_HASH160] [{scriptHashSize, scriptHash}] [OP_EQUAL]
The following are the k
, r
and s
values for each input's signature:
Input 1:
k = 98790447509501799195296257240616657470656053786701275200434341714298778299820
r = 96398386359095408146340664941016369169423137684113382189227162443480418477689
s = 62903510511574365450545635776206168644738316078298063260649088246548574249129
Input 2:
k = 109372172176680138721552873719725202562296645126925021083510279924852033069204
r = 105162394984132461723584277789901247831150698039237112243693144757926439529504
s = 62903510511574365450545635776206168644738316078298063260649088246548574249129
After this, I am getting the following error in bitcoind for sending the tx:
error code: -26
error message:
16: mandatory-script-verify-flag-failed
(Signature must be zero for failed CHECK(MULTI)SIG operation)
What am I doing wrong?
Below isEDIT: Thanks to the taw transactionanswer by arubi, I found there were two issues with the above steps. First thing, I was doing wrong was not encoding the redeemScript
as stack data. Second thing, I was doing wrong was putting [0x00]
in scriptSig
. After fixing that, Step 5 should be:
01000000029899CA2DE8F0883CA16CBAE203DFA271F0C005FF9B27E7263DDC79AC3CA134E4000000006D00483045022100D51F8D9BFEB7BC6108FACAAE131DCF875493A1768A55310FCFAEC7848A37E679022074EDDFEF1B3B64348A7D4436E0BDFCFF639C5051701758D5DEA6172675C620980121039F53E45F8F18B8ED294378BDA342EFF69B2053DEBF27FBEDE7D2D6BD84BE6235ACFFFFFFFF71255A15DA4FCEB716F84F6DE03C82C6B4AAB64E25942CE412455FABE743499B000000006D00483045022100F1CE74A984923A8C0187F37BFF5E4E20EAAEDF347C2C4CE6919B4BC81D54209402201780319909D1DB6037DB6415FC40D9113EC6A93CC0C420382CD69DA834C895210121039F53E45F8F18B8ED294378BDA342EFF69B2053DEBF27FBEDE7D2D6BD84BE6235ACFFFFFFFF0280CC06020000000017A914323D0E8A083E98010299109337850D05DD6157F78700E1F5050000000017A914A9974100AEEE974A20CDA9A2F545704A0AB54FDC8700000000
Set the redeemScript
as [{pubKeySize, pubKey}] [OP_CHECKSIG]
Set the scriptSig
as [{sigSize, sig}] [{redeemScriptSize, redeemScript}]