bitpay / bitcore-lib

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

MultiSigScriptHashInput: Provided public keys don't hash the provided output #186

Open lucasm-iRonin opened 6 years ago

lucasm-iRonin commented 6 years ago

Hi! I have a problem with transaction from multi-sig address that I created through coinb on testnet.

Here is the multi-sig address: https://live.blockcypher.com/btc-testnet/address/2NAayqTXNN9Qs6VDjyLm89sfDjCRFhJdqLs/

Now when I try to create a new transaction like this:

var Insight = require("bitcore-explorers").Insight;
delete global._bitcore;
var bitcore = require("bitcore-lib");
var insight = new Insight("testnet");

var homerAddress = 'ms7mTfzZy4Ch1hAaqNtk7bGhYkF1qxSGQE';
var bartAddress = 'mx4u4WPuEPQfcD1eaVB6S4HcXMqaLaJ8QA';
var escrowAddress = '2NAayqTXNN9Qs6VDjyLm89sfDjCRFhJdqLs';
var bartPubKey = '0272a5b4bbeefc1f8ba35d721af67823cd89b8bcde41cad0103a2fb774af4d425f';
var homerPubKey = '024454556f982f4c0878d38b8aeb6e962e09cbd024d322f2229a363dee0093c8c8';

insight.getUnspentUtxos(escrowAddress, function(error, utxos) {
  var tx = new bitcore.Transaction();
  tx.from(utxos, [bartPubKey, homerPubKey], 2);
  tx.to(homerAddress, 9800000);
  tx.to(bartAddress, 9800000);
  tx.sign(homerPrivKey);

  // Other part is irrelevant
  // ...
});

I'm getting error:

Invalid state: Provided public keys don't hash to the provided output

If you take a look at the redeem script of this multi-sig address on coinb you will see that bartPubKey and homerPubKey are listed in Signatures Required from.

antonin-arquey commented 6 years ago

I had the same problem today, I think it comes from the fact that the Transaction.from method sort automatically the public keys passed as parameters. As the keys order is relevant in multisig transaction, the generated script does not match the locking script from the transaction and throw.

IMO, the Transaction.from should accept an option object with noSorting, like the buildMultiSigOut function.