// THIS THING I WANT BUT CANT DO IT BECAUSE LITECORE-LIB DOESNOT SUPPORT BECH32.
//---------------------------------------------------------------------------------------
const bitcoinlitecore = require("bitcoinjs'litecore-lib"lib');
//creatinglet andutxo signing= a[{
native p2wpkh txId: 'a3f2565ab944629bc0ca46990593c8ec49c50b5eaee0789d58b1b4bc97a76eb9',
//p2wpk outputIndex: addInput('00000....'1,
0 address: 'ltc1q9umns6nuxv3xrhzdwlmhkzujewu6hulyuwgx30',
prevOutScript) script: '00142f37386a7c332261dc4d77f77b0b92cbb9abf3e4',
//sign(0 satoshis: 3000000
},
keyPair {
txId: '9d1517a2499ce5496ba3c826785b25982c63dc5b59b0c256303bf19128e44747',
outputIndex: 0,
address: 'ltc1q9umns6nuxv3xrhzdwlmhkzujewu6hulyuwgx30',value)
script: '00142f37386a7c332261dc4d77f77b0b92cbb9abf3e4',
satoshis: 2000000
}
]
let NETWORKto = bitcoin.networks.litecoin;'ltc1q9umns6nuxv3xrhzdwlmhkzujewu6hulyuwgx30'
let from = 'ltc1q9umns6nuxv3xrhzdwlmhkzujewu6hulyuwgx30'
let txbprivateKey = new''
let bitcoin.TransactionBuilder(NETWORK);value = 1000
//get unspent// output4. detailsCreate transaction
letvar txidtransaction = "9d1517a2499ce5496ba3c826785b25982c63dc5b59b0c256303bf19128e44747";new //transactionlitecore.Transaction()
id of the output.from(utxo)
you want .to(to, spendvalue)
let outn = 0; .fee(1000)
// n out
.change(from)
let keypair = bitcoin .ECPairsign(privateKey);
console.fromWIFlog(WIF,transaction)
//----------------------------------------------------------------------------------------
// NETWORKNOW I WANT TO USE THIS LIBRARY AS ABOVE.
const bitcoin = require("bitcoinjs-lib");
let scriptPubkeyutxo = bitcoin.script.witnessPubKeyHash.output.encode([{
txId: 'a3f2565ab944629bc0ca46990593c8ec49c50b5eaee0789d58b1b4bc97a76eb9',
outputIndex: 1,
bitcoin.crypto.hash160( address: 'ltc1q9umns6nuxv3xrhzdwlmhkzujewu6hulyuwgx30',
script: '00142f37386a7c332261dc4d77f77b0b92cbb9abf3e4',
satoshis: 20000
},
{
keypair.getPublicKeyBuffer() txId: '9d1517a2499ce5496ba3c826785b25982c63dc5b59b0c256303bf19128e44747',
outputIndex: 0,
address: 'ltc1q9umns6nuxv3xrhzdwlmhkzujewu6hulyuwgx30',
) script: '00142f37386a7c332261dc4d77f77b0b92cbb9abf3e4',
satoshis: 30000
}
]
let NETWORK = bitcoin.networks.litecoin;
let txb = new bitcoin.TransactionBuilder(NETWORK);
//addget inputunspent output details
txb.addInput(let txid, = "9d1517a2499ce5496ba3c826785b25982c63dc5b59b0c256303bf19128e44747"; //transaction id of the output you want to spend
let outn, null,= scriptPubkey);0; // n out
//addneed scriptPubKey for adding input
let WIF = ""; //private key of bech32 output
txblet keypair = bitcoin.addOutputECPair.fromWIF("ltc1q9umns6nuxv3xrhzdwlmhkzujewu6hulyuwgx30"WIF,4999000 NETWORK);
let //firstscriptPubkey argument= isbitcoin.script.witnessPubKeyHash.output.encode(
receiving address, the secondbitcoin.crypto.hash160(
is the amount we are sending after deductingkeypair.getPublicKeyBuffer()
a mining fee )
);
//signingadd input
txbutxo.signmap(0,item keypair=> {
txb.addInput(item.txId, nullitem.outputIndex, null, 5000000scriptPubkey);
})
//NOTEadd theoutput amount- isTotal theUTXO FULLSATOSHIS amountare: of20000 + 30000 = 50000/ So sending 10000 rest should be in the unspentsame outputaddress
txb.addOutput("ltc1q9umns6nuxv3xrhzdwlmhkzujewu6hulyuwgx30", NOT10000);
//signing
utxo.map(item the=> amount{
we are sending txb.sign(item.outputIndex, keypair, null, null, item.satoshis);
})
let tx = txb.build();
let txhex = tx.toHex();
console.log(txhex);
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);
// 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);
Bitcoinjs-lib how to add multiple utxos in addInput
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);