bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.67k stars 2.1k forks source link

Error : 16: mandatory-script-verify-flag-failed (Script evaluated without error but finished with a false/empty top stack element) #1162

Closed bellaj closed 6 years ago

bellaj commented 6 years ago

I am trying to run the following code snippet:

var keyPair = bitcoin.ECPair.fromWIF('cQRu7yq9HPwm5NhLvYuAEc7iRzEBP5x35Q6UBW6skjfZUn2Z68xZ', bitcoin.networks.testnet)
     const txb = new bitcoin.TransactionBuilder(bitcoin.networks.testnet)
      txb.addInput('0ebd063bfe6d272567a6099ad8e1ab6991d8a002dda011f43bda7b54b073e86d', 0)
      txb.addOutput('n3CKupfRCJ6Bnmr78mw9eyeszUSkfyHcPy', 55000000-5000)
      txb.sign(0, keyPair)
console.log(txb.build().toHex()); 

If i use the generated raw transaction using bitcoin core cliet i keep getting the error: 16: mandatory-script-verify-flag-failed (Script evaluated without error but finished with a false/empty top stack element)

bellaj commented 6 years ago

i haven't noticed that the input i am trying to consume is a p2sh not a p2pkh.

ShangXoL commented 6 years ago

@bellaj hi, How did you solve this problem?

junderw commented 6 years ago

@ShangXoL Here's the type of transaction he made.

https://github.com/bitcoinjs/bitcoinjs-lib/blob/20551fd38070cf68d078c0644c7586a2607f57fa/test/integration/transactions.js#L137-L164

ShangXoL commented 6 years ago

@junderw thank you very much

ShangXoL commented 6 years ago

@junderw can you help me ? If 'txb.addInput(unspent.txId, unspent.vout)' had more than one , What changes will “ txb.sign(0, keyPair, p2sh.redeem.output, null, unspent.value) ” take?

dcousens commented 6 years ago

What do you mean by more than one? More than one input or more than one signature required?

ShangXoL commented 6 years ago

@dcousens More than one input

ShangXoL commented 6 years ago

@dcousens if more than one input , How to deal with parameter "unspent.value" of function "txb.sign()"?

dcousens commented 6 years ago

@ShangXoL to have more than one input, is identical to having more than one unspent. If you have multiple inputs, you should have multiple unspents. Use each unspent's .value respectively.

ShangXoL commented 6 years ago

@dcousens var bitcoin = require("bitcoinjs-lib"); const net = bitcoin.networks.testnet; const keyPair = bitcoin.ECPair.fromWIF("cTDMViKByraUgTryu2bUrv4nj9KF6tjhb9gj1vy9SEQeFQH6hcjL", net); const p2wpkh = bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey, network: net }) const p2sh = bitcoin.payments.p2sh({ redeem: p2wpkh, network: net })

var tx = new bitcoin.TransactionBuilder(net); tx.setVersion(2); tx.addInput("58aab89435bad892ff0817f5296d48faa564dfc369b12121c2e5521ec5f7db0b", 0);//99990000 tx.addInput("31f4d3025a026738ab45b62078c64924f26eee5c7bd1ca5b8f84a46be6e963c9", 1);//70000000 tx.addOutput("2NGVjpAzsd9JPnm1jKHeJtNk3e1FBsCHXZU", 169980000); tx.sign(0, keyPair, p2sh.redeem.output, null, 169990000);//<-------------------------- console.log(tx.build().toHex());

tx.sign(0, keyPair, p2sh.redeem.output, null, 169990000);//<--------------------------

is that so?

junderw commented 6 years ago

no

99990000 and 70000000

junderw commented 6 years ago

tx.sign(0, keyPair, p2sh.redeem.output, null, 99990000) tx.sign(1, keyPair, p2sh.redeem.output, null, 70000000)

ShangXoL commented 6 years ago

@junderw @dcousens Thank you so much, i get it