bitcoinjs / bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
MIT License
5.6k stars 2.08k forks source link

transaction build error: Transaction is not complete #1122

Closed zhaozhiming closed 6 years ago

zhaozhiming commented 6 years ago

Hi, I create a transaction with a SegWit P2SH(P2WPKH) input. But when I run use my testnet address I got the error as follow:

Error: Transaction is not complete
    at /Users/zhaozhiming/playground/bitcoinjs-transaction-not-complete/node_modules/bitcoinjs-lib/src/transaction_builder.js:632:48
    at Array.forEach (<anonymous>)
    at TransactionBuilder.__build (/Users/zhaozhiming/playground/bitcoinjs-transaction-not-complete/node_modules/bitcoinjs-lib/src/transaction_builder.js:630:15)
    at TransactionBuilder.build (/Users/zhaozhiming/playground/bitcoinjs-transaction-not-complete/node_modules/bitcoinjs-lib/src/transaction_builder.js:616:15)
    at createSegwitTransaction (/Users/zhaozhiming/playground/bitcoinjs-transaction-not-complete/bitcoin.js:34:9)
    at Object.<anonymous> (/Users/zhaozhiming/playground/bitcoinjs-transaction-not-complete/bitcoin.js:40:1)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)

My bitcoinjs-lib version is 3.3.2 Node version: 8.9.0 Npm version: 5.5.1

The reproduce github repository is here. My testnet address info: https://testnet.blockchain.info/address/2NBPFSS1aLjE9uf2rdzQVWhBT9VHoDxKvyh Please help. Thanks.

dcousens commented 6 years ago

@zhaozhiming you have two inputs.

https://github.com/zhaozhiming/bitcoinjs-transaction-not-complete/blob/8d5c66ae301d633e4f9e5b8619cf02add82a93f3/bitcoin.js#L20-L27

You need to sign both.

zhaozhiming commented 6 years ago

@dcousens Thanks for your reply. I try sign twice but I get another error: Error: Signature already exists

Change from

    txb.sign(0, keyPair, redeemScript, null, 2000000);

to

    txb.sign(0, keyPair, redeemScript, null, 10000);
    txb.sign(0, keyPair, redeemScript, null, 1990000);

The error:

Error: Signature already exists
    at /Users/zhaozhiming/playground/bitcoinjs-transaction-not-complete/node_modules/bitcoinjs-lib/src/transaction_builder.js:707:36
    at Array.some (<anonymous>)
    at TransactionBuilder.sign (/Users/zhaozhiming/playground/bitcoinjs-transaction-not-complete/node_modules/bitcoinjs-lib/src/transaction_builder.js:705:30)
    at createSegwitTransaction (/Users/zhaozhiming/playground/bitcoinjs-transaction-not-complete/bitcoin.js:33:9)
    at Object.<anonymous> (/Users/zhaozhiming/playground/bitcoinjs-transaction-not-complete/bitcoin.js:40:1)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)

These 2 inputs is the same segwit address so I think it should use the same keypair and redeemScript.
I have not idea what is wrong here. Please help. Thanks.

zhaozhiming commented 6 years ago

Oh, I understand now. I change the code as follow and it work. I should change the first parameter of the second sign method to 1 . Thanks.

    txb.sign(0, keyPair, redeemScript, null, 10000);
    txb.sign(1, keyPair, redeemScript, null, 1990000);