bitpay / bitcore-lib

A pure and powerful JavaScript Bitcoin library
https://bitcore.io/
Other
613 stars 1.03k forks source link

Bitcoin Cash, invalid multisig transaction (mandatory-script-verify-flag-failed) #162

Open shayanb opened 7 years ago

shayanb commented 7 years ago

I'm trying to spend Bitcoin cash from a 2 of 3 P2SH address using bitcoin-cash-lib.

It seems to work and generates the transaction. Here is the raw signed transaction:

0100000001e441583a6ca72f9488470f6f442bf4cbaee4108d3e659448e3be6c672ef5c02201000000fc0047304402204000624ff204bdb2c886c47adc85873338e13df708d4b36f4cccc76ef127937302201080b407ebc772bdddf9e9c7c6f86b0543cd623d6ac2e76f661c6e674c0133a34147304402207d445a15df4432e840c57092834c746967a25a64c07c436906c3a65cba8ce0a80220110f0c28f5f3af9d1783f93f62ec59b27c4e99f0f0d1257d57c915286fd98736414c69522102e4fd60801a5bbd0e88ee30044f0466d041906743bd58c4cd2a2c904a0ae0a5a3210301c0162eec8e3defcf252ffa7464e742e22be0bb5f341b95e127142f5c23a13321038b9b8ae0c191fbce037d51c1c741d13655fa3eff5cd3fd5f0c580ef7ad6c7b1c53aeffffffff0131b41400000000001976a914afc90494988b4087316eb8ca5399f2412561918688ac00000000
{
   "lock_time":0,
   "size":340,
   "inputs":[
      {
         "prev_out":{
            "index":1,
            "hash":"22c0f52e676cbee34894653e8d10e4aecbf42b446f0f4788942fa76c3a5841e4"
         },
         "script":"0047304402204000624ff204bdb2c886c47adc85873338e13df708d4b36f4cccc76ef127937302201080b407ebc772bdddf9e9c7c6f86b0543cd623d6ac2e76f661c6e674c0133a341483045022100a002661e7e99423f4bf469e5a3a86a46a52ec336a978b2e21276a1edecffe02d02201ac15fff5142c6fad82d0258102133809e03a9d5592d8ec428a0a1b3b5514ef1414c69522102e4fd60801a5bbd0e88ee30044f0466d041906743bd58c4cd2a2c904a0ae0a5a3210301c0162eec8e3defcf252ffa7464e742e22be0bb5f341b95e127142f5c23a13321038b9b8ae0c191fbce037d51c1c741d13655fa3eff5cd3fd5f0c580ef7ad6c7b1c53ae"
      }
   ],
   "version":1,
   "vin_sz":1,
   "hash":"b837964489e0f210d5ebe59196e85da4d49a32c852921b9a4045bcb96306ff9a",
   "vout_sz":1,
   "out":[
      {
         "script_string":"OP_DUP OP_HASH160 afc90494988b4087316eb8ca5399f24125619186 OP_EQUALVERIFY OP_CHECKSIG",
         "address":"1H2U4YDzSCMcpBwqKWnT3JkNPXbkhWPFGT",
         "value":1356849,
         "script":"76a914afc90494988b4087316eb8ca5399f2412561918688ac"
      }
   ]
}

However on broadcasting it, using insight I get the following error:

An error occured:
16: mandatory-script-verify-flag-failed (Script evaluated without error but finished with a false/empty top stack element). Code:-26

this seems like a common error in some other implementations as well (e.g electrum: https://github.com/fyookball/electrum/issues/6)

dizda commented 6 years ago

Hey,

Could you please share the script you've made? I'm also encountering some issues, but different ones "Invalid state: sigtype must be a number" for a 2-of-3 P2SH.

dizda commented 6 years ago

Ok I got it. I had the exact same error as yours today.

It was because it was not in the right order, althought the order MATTER!

The right order is:

  1. add all inputs
  2. to
  3. fee
  4. signing all inputs
  5. then finally serialize

It didn't work because the signing step was right after adding the inputs (the first step).

Then I could broadcast the transaction successfully.