Bitcoinjs-lib: Issue with creating a p2sh transaction

Created on 30 Apr 2018  ·  7Comments  ·  Source: bitcoinjs/bitcoinjs-lib

I started trying to create a p2sh transaction using the following issue: https://github.com/bitcoinjs/bitcoinjs-lib/issues/856

And arrived at the following:

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

//Create P2SH Address
var witnessScript = bitcoin.script.compile([
    ops.OP_ADD,
    ops.OP_7,
    ops.OP_EQUAL
]);

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 P2SHaddress = bitcoin.address.fromOutputScript(scriptPubKey, network);

var txb = new bitcoin.TransactionBuilder(network);
txb.addInput('17146fedf0ba15036a67dcb5ef386e6683946aae2581a54c105f2b56d458ba49', 16); 
txb.addOutput(P2SHaddress, 12470000);

var keyPair = bitcoin.ECPair.fromWIF('cMahea7zqjxrtgAbB7LSGbcQUr1uX1ojuat9jZodMN87JcbXMTcA', network);
txb.sign(0, keyPair, redeemScript, null, 12470920);

But when I try to sign, I get the following error:

/home/user/dev/bitswap/node_modules/bitcoinjs-lib/src/transaction_builder.js:313
    if (!expanded.pubKeys) throw new Error('RedeemScript not supported "' + bscript.toASM(redeemScript) + '"')
                           ^
Error: RedeemScript not supported "OP_0 f5ac8d00e92b7131aae203ea7c8836842ec5fbd63f6c99c3ec1224d8adbd1075"
    at prepareInput (/home/user/dev/bitswap/node_modules/bitcoinjs-lib/src/transaction_builder.js:313:34)
    at TransactionBuilder.sign (/home/user/dev/bitswap/node_modules/bitcoinjs-lib/src/transaction_builder.js:692:26)
    at Object.<anonymous> (/home/user/dev/bitswap/tbitcoin.js:28:5)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)

Anything immediately standing out?

how to / question / docs

Most helpful comment

Thanks for the quick turn around!

I think I just confused two different examples and merged them into this. I've got it working now though!

I wasn't sure what "OP_0 f5ac8d00e92b7131aae203ea7c8836842ec5fbd63f6c99c3ec1224d8adbd1075" was, so I felt my scripts my have been causing issues.

All 7 comments

You can’t sign for a script that requires no signatures........

The puzzle you made asks “what can you add together to get 7?” And you are trying to sign it. The puzzle asks nothing about signatures. (Unless you want to make it ask, I guess)

Either way, TransactionBuilder only works with a few set formats of script.

My example in your link never used TransactionBuilder.

@dabura667 interesting, in the future API, would you classify "completing" this puzzle as "signing"?

What would the term be?
".complete(...)"?

Would it make sense that in the Transaction Builder they must declare the type of Tx they are using?
It took me a bit of digging to realize that TxBuilder is for a set of templates only

Something like this?

var txb = new bitcoin.TransactionBuilder(network);
txb.setTxType("P2PKH")

@rbndg the TransactionBuilder itself doesn't adhere to a template, it can only support particular "input" types.

Yes. That is correct. My concern is that it's not explicitly clear what input types are supported by the TransactionBuilder. Better error message? Maybe a test Case of TransactionBuilder failing when the input type is not supported.

@rbndg it does throw if it doesn't recognize what you provide?

Error: RedeemScript not supported ...

Etc

Thanks for the quick turn around!

I think I just confused two different examples and merged them into this. I've got it working now though!

I wasn't sure what "OP_0 f5ac8d00e92b7131aae203ea7c8836842ec5fbd63f6c99c3ec1224d8adbd1075" was, so I felt my scripts my have been causing issues.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dakk picture dakk  ·  3Comments

itsMikeLowrey picture itsMikeLowrey  ·  3Comments

zhaozhiming picture zhaozhiming  ·  3Comments

askucher picture askucher  ·  3Comments

thrastarson picture thrastarson  ·  3Comments