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
// THIS THING I WANT BUT CANT DO IT BECAUSE LITECORE-LIB DOESNOT SUPPORT BECH32.
//---------------------------------------------------------------------------------------
const litecore = require('litecore-lib');
let utxo = [{
txId: 'a3f2565ab944629bc0ca46990593c8ec49c50b5eaee0789d58b1b4bc97a76eb9',
outputIndex: 1,
address: 'ltc1q9umns6nuxv3xrhzdwlmhkzujewu6hulyuwgx30',
script: '00142f37386a7c332261dc4d77f77b0b92cbb9abf3e4',
satoshis: 3000000
},
{
txId: '9d1517a2499ce5496ba3c826785b25982c63dc5b59b0c256303bf19128e44747',
outputIndex: 0,
address: 'ltc1q9umns6nuxv3xrhzdwlmhkzujewu6hulyuwgx30',
script: '00142f37386a7c332261dc4d77f77b0b92cbb9abf3e4',
satoshis: 2000000
}
]
let to = 'ltc1q9umns6nuxv3xrhzdwlmhkzujewu6hulyuwgx30'
let from = 'ltc1q9umns6nuxv3xrhzdwlmhkzujewu6hulyuwgx30'
let privateKey = ''
let value = 1000
// // 4. Create transaction
var transaction = new litecore.Transaction()
.from(utxo)
.to(to, value)
.fee(1000)
.change(from)
.sign(privateKey);
console.log(transaction)
//----------------------------------------------------------------------------------------
// NOW I WANT TO USE THIS LIBRARY AS ABOVE.
const bitcoin = require("bitcoinjs-lib");
let utxo = [{
txId: 'a3f2565ab944629bc0ca46990593c8ec49c50b5eaee0789d58b1b4bc97a76eb9',
outputIndex: 1,
address: 'ltc1q9umns6nuxv3xrhzdwlmhkzujewu6hulyuwgx30',
script: '00142f37386a7c332261dc4d77f77b0b92cbb9abf3e4',
satoshis: 20000
},
{
txId: '9d1517a2499ce5496ba3c826785b25982c63dc5b59b0c256303bf19128e44747',
outputIndex: 0,
address: 'ltc1q9umns6nuxv3xrhzdwlmhkzujewu6hulyuwgx30',
script: '00142f37386a7c332261dc4d77f77b0b92cbb9abf3e4',
satoshis: 30000
}
]
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
//need scriptPubKey for adding input
let WIF = ""; //private key of bech32 output
let keypair = bitcoin.ECPair.fromWIF(WIF, NETWORK);
let scriptPubkey = bitcoin.script.witnessPubKeyHash.output.encode(
bitcoin.crypto.hash160(
keypair.getPublicKeyBuffer()
)
);
//add input
utxo.map(item => {
txb.addInput(item.txId, item.outputIndex, null, scriptPubkey);
})
//add output - Total UTXO SATOSHIS are: 20000 + 30000 = 50000/ So sending 10000 rest should be in the same address
txb.addOutput("ltc1q9umns6nuxv3xrhzdwlmhkzujewu6hulyuwgx30", 10000);
//signing
utxo.map(item => {
txb.sign(item.outputIndex, keypair, null, null, item.satoshis);
})
let tx = txb.build();
let txhex = tx.toHex();
console.log(txhex);