Hello,
I tried to create (and broadcast via 3PBP) a Transaction with a SegWit P2SH(P2WPKH) input, followed exactly the example: https://github.com/bitcoinjs/bitcoinjs-lib/blob/master/test/integration/transactions.js#L151
However I kept getting the error:
if (!extracted) throw new Error('RedeemScript not supported "' + bscript.toASM(redeemScript) + '"')
Error: RedeemScript not supported "OP_0 751e76e8199196d454941c45d1b3a323f1433bd6"
Here's my code:
var bitcoin = require('bitcoinjs-lib');
var testnet = bitcoin.networks.testnet
var keyPair = bitcoin.ECPair.fromWIF('cMahea7zqjxrtgAbB7LSGbcQUr1uX1ojuat9jZodMN87JcbXMTcA', testnet)
var pubKey = keyPair.getPublicKeyBuffer()
var pubKeyHash = bitcoin.crypto.hash160(pubKey)
var redeemScript = bitcoin.script.witnessPubKeyHashOutput(pubKeyHash)
var redeemScriptHash = bitcoin.crypto.hash160(redeemScript)
var scriptPubKey = bitcoin.script.scriptHashOutput(redeemScriptHash)
var address = bitcoin.address.fromOutputScript(scriptPubKey, testnet)
console.log(address);
var txb = new bitcoin.TransactionBuilder(testnet)
txb.addInput('d7291bd8b60c1d9ee14b0f09ac2860c91fe1040824206f53156bda34dd3b563e', 1)
txb.addOutput(address, 450000)
txb.sign(0, keyPair, redeemScript, null, 500000)
var tx = txb.build()
Am I missing something?
Thank you.
bitcoin.script.witnessPubKeyHashOutput(pubKeyHash)
bitcoin.script.scriptHashOutput(redeemScriptHash)
should be
bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash)
bitcoin.script.scriptHash.output.encode(redeemScriptHash)
With that change, and console.log(tx.toHex()), the code prints (as expected):
2NAUYAHhujozruyzpsFRP63mbrdaU5wnEpN
010000000001013e563bdd34da6b15536f20240804e11fc96028ac090f4be19e1d0cb6d81b29d70100000017160014751e76e8199196d454941c45d1b3a323f1433bd6ffffffff01d0dd06000000000017a914bcfeb728b584253d5f3f70bcb780e9ef218a68f48702473044022014c8e69bf448884e3f4892c577c54be5efad509c3b87035e59540e2ae436198502203dacba139bc17169ea1d4140968951687f5089659983e4621bd3769c7eeca73901210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f8179800000000
I have no idea how you got the error you posted.
With this code:
bitcoin.script.witnessPubKeyHashOutput(pubKeyHash)
bitcoin.script.scriptHashOutput(redeemScriptHash)
I still can print out the address: 2NAUYAHhujozruyzpsFRP63mbrdaU5wnEpN
Change to this code:
bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash)
bitcoin.script.scriptHash.output.encode(redeemScriptHash)
It throws error:
var redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(pubKeyHash)
^
TypeError: Cannot read property 'output' of undefined
at Object.<anonymous> (/Users/anhoang/Desktop/JS/wallet/transaction_segwit2.js:8:52)
at Module._compile (module.js:571:32)
What's version of the lib you are using?
Latest version.
@hoangan the example you linked is the master branch, it may be the case that you're running an older version. You can find the source for your version here https://github.com/bitcoinjs/bitcoinjs-lib/releases, then check that examples folder.
Maybe paste the version or commit hash here so we can look too!
My bet is v2.3.0, in which case, @hoangan I'd recommend upgrading as several changes and fixes have been added to TransactionBuilder.
@dcousens Thanks, just did a clean up and updated newest version. By some reasons, my machine kept downloading the old version.
Most helpful comment
@dcousens Thanks, just did a clean up and updated newest version. By some reasons, my machine kept downloading the old version.