Random "previous output ScriptPubKey mismatch" Error

+3 votes
After creating a multisig transaction containing only metadata (no asset data), sometimes I randomly get a "previous output ScriptPubKey mismatch" error when I try to sign the transaction. Most of the time it works fine, but sometimes I get that error. Posted below is a screenshot of the error. decoded info is from decoderawtransaction. When signing, I am passing in the extra parameters (txid, vout, redeemScript, and scriptPubKey) along with the raw tx object from createrawsendfrom and the private keys. However, I am not sure if I am obtaining the extra parameters properly.

The error:
"Previous output scriptPubKey mismatch:

OP_HASH160 0e2acee2307892bcf2ea66e3f321b34979e24bf8 OP_EQUAL 73706b65adfb3830d91629f70b8b29cf1fc36fbd OP_DROP 73706b700800000000000000ffffffff0b507a59 OP_DROP

vs:

OP_HASH160 0e2acee2307892bcf2ea66e3f321b34979e24bf8 OP_EQUAL

500"
asked Jul 28, 2017 by veronica
edited Jul 28, 2017 by veronica
Could you please post a full transcript of what you are doing? I'm afraid this description doesn't contain enough information for us to assess.
1. addmultisigaddress with n=2 and 2 users (creator and one other user on a different node)
2. importaddress to creator's node
3. importaddress to other user's node
4. creator creates transaction using createrawsendfrom with only meta data, no asset data (which returns raw transaction hex if I understand correctly)
5. getting extra parameters for signrawtransaction:
    listaddresses(multisig_address, True)[0]['hex'] for redeem script
    decoderawtransaction(rawtxhex)['vin'][0]['txid'] for txid
    getrawtransaction(txid, 1)['vout'][-1]['scriptPubKey']['hex'] for scripPubKey
    decoderawtransaction(rawtxhex)['vout'][-1]['n'] for vout
6. creator signrawtransaction(rawtxhex, extra_parameters_dict, private_keys_array)

step 6 works most of the time but randomly returns Previous output scriptPubKey mismatch error sometimes. I can't seem to figure out what is causing the issue because it seems completely random. I tried creating transactions the same way from the same address and it would alternate between breaking and working. Let me know if you need more details. Thanks!

1 Answer

+1 vote

Based on the extra details you provided: It looks like the problem is that you're fixing the index of the output being spent from the previous transaction (assuming -1 means the last) but you cannot do this. You need to take the previous tx output index (vout) from the same vin array element as you're extracting the previous txid.

answered Jul 31, 2017 by MultiChain
What is the previous txid? Isn't there only one txid if I just created the transaction or am I using the wrong txid? If I get the vout from the same vin array element as txid (decoderawtransaction(rawtxhex)['vin'][0]['vout']) it gives me the scriptPubKey mismatch error.

OP_HASH160 b6baaf39df057d1b2e727ada2698d0ad0a3592a0 OP_EQUAL 73706b652225fba45a535de1478bdae2bf305bbe OP_DROP 73706b700800000000000000ffffffffa8907b59 OP_DROP
vs:
OP_DUP OP_HASH160 dbfc4aeefcd6c5c1f576b52322899b0e8f2636cb OP_EQUALVERIFY OP_CHECKSIG
500
This error means that scriptPubKey you are passing to signrawtransaction doesn't match that stored in the blockchain.

I see at least one error in what you sent - for <vout>: is should be

decoderawtransaction(rawtxhex)['vin'][0]['vout']

not

decoderawtransaction(rawtxhex)['vout'][-1]['n']'


If you have rawtxhex with only one element in decoderawtransaction(rawtxhex)['vin'], the parameters for signrawtransaction should be (in your notation)

<txid>: decoderawtransaction(rawtxhex)['vin'][0]['txid']
<vout>: decoderawtransaction(rawtxhex)['vin'][0]['vout']
<scriptPubKey>: getrawtransaction(<txid>,1)['vout'][<vout>]['scriptPubKey']['hex']
   or (probably better)
   gettxout(<txid>,<vout>)['scriptPubKey']['hex']
<redeemScript>: listaddresses(multisig_address, True)[0]['hex']

If you still get this error, please post:
1. Output of decoderawtransaction for unsigned transaction (rawtxhex)
2. Output of "getrawtransaction 1" for all transactions appearing in the inputs (vin) of unsigned transaction
3. Exact list of parameters for signrawtransaction
Seems to be working now. Thank you!
Great - thanks for the update.
...