Bitcoinjs-lib: Bitcoinjs-lib how to add multiple utxos in addInput

Created on 30 Mar 2020  路  4Comments  路  Source: bitcoinjs/bitcoinjs-lib

Hi i want to make transactions of bitcoin bech32 addresses using bitcoinjs-lib , here is my code below, it works fine when my address has only one utxos but when it increases to two or more as its array which doesn't pass into addInput method, so it fails. Please help me out to loop through it or what is the solution to handle multiple utxo as input. Thanks

const bitcoin = require("bitcoinjs-lib");

//creating and signing a native p2wpkh
//p2wpk: addInput('00000....', 0, prevOutScript)
//sign(0, keyPair,,,value)

let NETWORK = bitcoin.networks.litecoin; 
let txb = new bitcoin.TransactionBuilder(NETWORK);

//get unspent output details
let txid = "9d1517a2499ce5496ba3c826785b25982c63dc5b59b0c256303bf19128e44747"; //transaction id of the output you want to spend
let outn = 0;  // n out

let keypair = bitcoin.ECPair.fromWIF(WIF, NETWORK);
let scriptPubkey = bitcoin.script.witnessPubKeyHash.output.encode(
                       bitcoin.crypto.hash160(  
                           keypair.getPublicKeyBuffer()
                       )
                   );

//add input
txb.addInput(txid, outn, null, scriptPubkey);

//add output
txb.addOutput("ltc1q9umns6nuxv3xrhzdwlmhkzujewu6hulyuwgx30",4999000); //first argument is receiving address, the second is the amount we are sending after deducting a mining fee

//signing
txb.sign(0, keypair, null, null, 5000000); //NOTE the amount is the FULL amount of the unspent output, NOT the amount we are sending
let tx = txb.build();
let txhex = tx.toHex();

console.log(txhex);


All 4 comments

You have to post the faulty code; if you add more than one input you should call addInput() many time as many outputs you have. And you also have to signo all of them.
Btw I suggest you to use PSBT since TransactionBuilder is deprecated.

Bro i have asked the same question on bitcoin stack exchange with the full code and what i want to do and what i'm doing,
can you please help to check this one:
https://bitcoin.stackexchange.com/questions/95019/bitcoinjs-lib-how-to-add-multiple-utxos-in-addinput

And you said use PSBT what is that?

@HamzaYasin1 in the stackexchange question the code is using litecore-lib which is a different library; btw the answer from MCCCS on stackexchange is kinda correct.

_PSBT is an interchange format for Bitcoin transactions_ which is now the "common" format for build and transfer transaction; bitcoinjs-lib deprecated TransactionBuilder in favor of this new format which is common between other libs.

@dakk You are right, but i have wrote the code with two different libraries.
well yes MCCCS has solve that question.
Now gonna close this issue.
Thanks @dakk for your help though.
Much Appreciated !

Was this page helpful?
0 / 5 - 0 ratings

Related issues

prahaladbelavadi picture prahaladbelavadi  路  3Comments

Beardcoding picture Beardcoding  路  3Comments

dcousens picture dcousens  路  3Comments

silence-may picture silence-may  路  3Comments

yakitorifoodie picture yakitorifoodie  路  3Comments