TransactionBuilder.fromTransaction (Transaction.fromHex ()) and segwit input signing
See original GitHub issueVersion: Bitcoinjs-0.3.2 and also bitcoinjs/bitcoinjs-lib#fixes
I’m trying to integrate segwit in my walleting software, but I’m stuck in one problem. The flow is the following:
- I create a segwit address from 2of3 p2sh keys
- I send some testnet coin to this address
- I get the unspent tx
- I create the tx with this unspent as input
- I generate the incomplete hex, passing it to another software
- I then try to sign the transaction with the following code:
var signTxSegwit = function (txhex, pubkeys, privkey, utxos, n) {
var txb = new bitcoinjs.TransactionBuilder.fromTransaction (
bitcoinjs.Transaction.fromHex (txhex), bitcoinjs.networks.testnet);
var upair = bitcoinjs.ECPair.fromWIF(privkey, bitcoinjs.networks.testnet);
var pubkeys_raw = pubkeys.map(function (hex) { return new Buffer(hex, 'hex'); });
var witnessScript = bitcoinjs.script.multisig.output.encode (n, pubkeys_raw);
var redeemScript = bitcoinjs.script.witnessScriptHash.output.encode (bitcoinjs.crypto.sha256(witnessScript));
var scriptPubKey = bitcoinjs.script.scriptHash.output.encode (bitcoinjs.crypto.hash160(redeemScript));
var address = bitcoinjs.address.fromOutputScript(scriptPubKey, bitcoinjs.networks.testnet);
for (var j = 0; j < txb.tx.ins.length; j++) {
txb.sign (j, upair, redeemScript, null, parseInt (utxos[j].value * 100000000), witnessScript);
}
var tx = txb.build ();
return tx.toHex ();
}
The program fail at txb.sign, with this error:
Message:
Error: Expected property "2" of type Satoshi, got undefined
Stacktrace:
Error
at TfTypeError.Error (native)
at new TfTypeError (/home/dakk/Repositories/MyRepos/backend/node_modules/typeforce/errors.js:43:24)
at typeforce (/home/dakk/Repositories/MyRepos/backend/node_modules/typeforce/index.js:193:11)
at /home/dakk/Repositories/MyRepos/backend/node_modules/typeforce/index.js:152:18
at Array.every (native)
at _tuple (/home/dakk/Repositories/MyRepos/backend/node_modules/typeforce/index.js:150:20)
at typeforce (/home/dakk/Repositories/MyRepos/backend/node_modules/typeforce/index.js:191:9)
at Transaction.hashForWitnessV0 (/home/dakk/Repositories/MyRepos/backend/node_modules/bitcoinjs-lib/src/transaction.js:320:3)
at TransactionBuilder.sign (/home/dakk/Repositories/MyRepos/backend/node_modules/bitcoinjs-lib/src/transaction_builder.js:692:29)
at signTxSegwit (/home/dakk/Repositories/MyRepos/backend/tests/api/middlewares/wallet.js:79:7)
Then I jumped to bitcoinjs-lib/src/transaction_builder:692 and I found this:
signatureHash = this.tx.hashForWitnessV0(vin, input.signScript, input.value, hashType)
Which is using input.value; so I guessed prepareInput should inject this field, and I’ve found that it injects input.value if witnessScript and reedemScript are not undefined; but they are defined in my example, right? I can’t figure out what’s the problem here
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
How to use the bitcoinjs-lib.TransactionBuilder function in ...
To help you get started, we've selected a few bitcoinjs-lib.TransactionBuilder examples, based on popular ways it is used in public projects.
Read more >How do you switch your bitcoinjs integrationfrom ...
You're only signing the first input? tx.sign(0, owner); Usually ... Segwit inputs can be simpler but they can also be created using the...
Read more >Creating and Signing a SegWit Transaction from Scratch
Step by Step. We previously created and signed a P2PKH transaction from scratch. This time we will create a SegWit transaction with 3...
Read more >api documentation for bitcoinjs-lib (v3.0.2)
TransactionBuilder.prototype.sign (vin, keyPair, redeemScript, hashType, witnessValue, ... function bitcoinjs-lib.input.decode (buffer, allowIncomplete) ...
Read more >A Beginner's Guide to Segregated Witness (SegWit)
By removing the signature data from the transaction input, more transactions can be stored within a single block. Transactions consist of two ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Reproduced with
Which isolates the variables with block scoping.
Will investigate.
See https://github.com/bitcoinjs/bitcoinjs-lib/pull/910