Bitcoinjs-lib: Witness program does not match script hash error when pushing tx

Created on 18 Sep 2017  路  1Comment  路  Source: bitcoinjs/bitcoinjs-lib

I tried to spend from a P2WPKH-in-P2SH (Multisig 2-of-2) Bip49 address, followed the sample code at https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.js#L183 and https://github.com/bitcoinjs/bitcoinjs-lib/issues/851 . However, when pushing the transaction, I kept getting this error:

Error sending transaction: Error running script for input 0 referencing e58216a8ccf3e1b7c892dd6afee0d86435c28cbf25fc210c99c60a7aee9dadbc at 1: witness program does not match script hash.

Here is my code:

var bitcoin = require('bitcoinjs-lib');
var testnet = bitcoin.networks.testnet;

var keyPairs = [
    'tprv8jHUtUt75gK3nFRSSCQAbrHsdjGJKj7y1HmUzqDTrHyjpM7K4g5YXSdY3WobZwwdkJK1sEG5ZheUcjGFFzJuBKaNzqCvzGLgaVY7aVkYvHh',
    'tprv8k97oTF9aSAcB8vpDNUH3eqje4avFbhTLBLhXDcUZPtGjjLTbT1AuN41TfBB4awgiEzVPbMVjDReBuoABqESUFpEubBr5XiM8ji9HSjt46h'
].map(function(xpriv) {
    return bitcoin.HDNode.fromBase58(xpriv, testnet).keyPair;
});

var pubKeys = keyPairs.map(function (xpub) { return xpub.getPublicKeyBuffer() });

var witnessScript = bitcoin.script.multisig.output.encode(2, pubKeys)
var witnessScriptHash = bitcoin.crypto.sha256(witnessScript)

var redeemScript = bitcoin.script.witnessScriptHash.output.encode(witnessScriptHash)
var redeemScriptHash = bitcoin.crypto.hash160(redeemScript)
var scriptPubKey = bitcoin.script.scriptHash.output.encode(redeemScriptHash)
var address = bitcoin.address.fromOutputScript(scriptPubKey, testnet)

console.log(address);

var txb = new bitcoin.TransactionBuilder(testnet)
txb.addInput('e58216a8ccf3e1b7c892dd6afee0d86435c28cbf25fc210c99c60a7aee9dadbc', 1)
txb.addOutput(address, 150000)
txb.sign(0, keyPairs[0], redeemScript, null, 200000, witnessScript)
txb.sign(0, keyPairs[1], redeemScript, null, 200000, witnessScript)

var tx = txb.build()
var txid = tx.getId()
var txhex = tx.toHex();

console.log(txid);
console.log(txhex);

Anyone got any idea why?

Thanks

how to / question / docs

Most helpful comment

Ok, I've just figured out the problem. The node I used to push (blockcypher.com) doesn't support this kind of transaction. Switching to Smartbit.com, the transaction got accepted.

>All comments

Ok, I've just figured out the problem. The node I used to push (blockcypher.com) doesn't support this kind of transaction. Switching to Smartbit.com, the transaction got accepted.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LeonYanghaha picture LeonYanghaha  路  3Comments

dakk picture dakk  路  3Comments

silence-may picture silence-may  路  3Comments

ghost picture ghost  路  3Comments

ishwarchandratiwari picture ishwarchandratiwari  路  3Comments