1

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);

1 Answer 1

0

What's different with spending SegWit outputs is that signatures cover input values. In other words, every SegWit input must be accompanied by its input value.

Mozilla's documentation says don't use map if you aren't constructing a new array. So I'll use for loop. (because order is important, and for-of is too new)

for (let i = 0, k = utxo.length; i < k; i++) {
    txb.addInput(utxo[i].txId, utxo[i].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
for (let i = 0, k = utxo.length; i < k; i++) {
    txb.sign(i, keypair, null, null, utxo[i].satoshis);
}
11
  • I thought to do this, but i think i have a stupid question here, You know when using bitcore-lib, we pass utxo array of object in from function like this: txb.from(utxo).to(recipient, value).sign() I want to use addInput function like that. Ass i'm doing this for litecoin so i can't use litecore-lib as it don't support bech32.I hope you understand my question. Commented Mar 30, 2020 at 14:52
  • Are you using bitcore-lib-ltc? The code in the question seems to use bitcoinjs instead of bitcore-lib.
    – MCCCS
    Commented Mar 30, 2020 at 15:57
  • Yes i'm using bitcoinjs-lib instead of bitcore-lib, Let me tell you, I have used bitcore-lib for bitcoin bech32 addresses and it works fine to sign the transacations, But when i used litecore-lib to sign bech32 transactions for LTC so it fails because litecore-lib doesn't support it. So that's wht i'm using bitcoinjs-lib to sign transactions for litecoin bech32 transactions, which works fine when i have sigle utxo object on my address but when array has multiple utxos so i got confuse which object i have to use? on the same case bitcore-lib txb.from(utxo) it accept array. Commented Mar 30, 2020 at 18:22
  • Does it work for you now? Passing each scriptPubkey to each sign with each input.
    – MCCCS
    Commented Mar 30, 2020 at 18:34
  • It doesn't work actually, Here is my inputs now: ` let utxo = [{ txId: 'hash', outputIndex: 1, address: 'ltc1...', script: '00142f37386a7c332261dc4d77f77b0b92cbb9abf3e4', satoshis: 3000000 }, { txId: 'hash', outputIndex: 0, address: 'ltc1...', script: '00142f37386a7c332261dc4d77f77b0b92cbb9abf3e4', satoshis: 2000000 } ] utxo.map(item => { txb.addInput(item.txId, item.outputIndex, null, scriptPubkey); }) txb.addOutput("ltc1q9umns6nuxv3xrhzdwlmhkzujewu6hulyuwgx30", 4999000); txb.sign(0, keypair, null, null, 5000000); ` Commented Mar 30, 2020 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.