<p>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.</p>

<p>Mozilla's documentation says don't use <code>map</code> if you <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#When_not_to_use_map" rel="nofollow noreferrer">aren't constructing a new array</a>. So I'll use for loop. (because order is important, and for-of is too new)</p>

<pre><code>for (let i = 0, k = utxo.length; i &lt; 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 &lt; k; i++) {
    txb.sign(i, keypair, null, null, utxo[i].satoshis);
}
</code></pre>

CC BY-SA 4.0
2020-03-30T14:52:44.740Z
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.
Hamza Yasin
CC BY-SA 4.0
2020-03-30T15:57:13.687Z
Are you using bitcore-lib-ltc? The code in the question seems to use bitcoinjs instead of bitcore-lib.
MCCCS
CC BY-SA 4.0
2020-03-30T18:22:11.753Z
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.
Hamza Yasin
CC BY-SA 4.0
2020-03-30T18:34:01.513Z
Does it work for you now? Passing each `scriptPubkey` to each `sign` with each input.
MCCCS
CC BY-SA 4.0
2020-03-30T18:58:31.403Z
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);
`
Hamza Yasin
CC BY-SA 4.0
2020-03-30T19:02:38.973Z
Do i have your email so that i can send you my full file, so that you will know what i'm doing
Hamza Yasin
CC BY-SA 4.0
2020-03-30T19:16:23.567Z
I have updated the question with the new code please see the code above and also read the comments in code. Thanks
Hamza Yasin
CC BY-SA 4.0
2020-03-30T19:54:58.970Z
I'll post a solution tomorrow.
MCCCS
CC BY-SA 4.0
2020-03-31T05:33:43.917Z
Sure I'll be waiting
Hamza Yasin
CC BY-SA 4.0
2020-03-31T08:53:56.223Z
Does it work now?
MCCCS
CC BY-SA 4.0
2020-03-31T10:45:02.483Z
Yes it does. i would like to upvote this answer but can't don't have such reputations.
Thanks (Y)
Hamza Yasin
CC BY-SA 4.0
2020-03-30T13:05:05.123Z
2020-03-31T08:53:51.110Z
2020-03-31T08:53:51.110Z
MCCCS
<p>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</p>

<pre><code>    // 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 =&gt; {
    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 =&gt; {
    txb.sign(item.outputIndex, keypair, null, null, item.satoshis);
})
let tx = txb.build();
let txhex = tx.toHex();

console.log(txhex);
</code></pre>

<bitcoin-core><wallet><bitcoinjs>
Bitcoinjs-lib how to add multiple utxos in addInput
Hamza Yasin
# Bitcoinjs-lib how to add multiple utxos in addInput

(Asked by: username_0 on 2020-03-30)

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

```

**Tags:** bitcoin-core, wallet, bitcoinjs


---

## 1 answers

### Answer by username_1 on 2020-03-30. Score: 0 | Accepted answer

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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#When_not_to_use_map). 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);
}

```

> **Comments:**
> **username_0**: 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.  
> **username_1**: Are you using bitcore-lib-ltc? The code in the question seems to use bitcoinjs instead of bitcore-lib.  
> **username_0**: 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.  
> **username_1**: Does it work for you now? Passing each `scriptPubkey` to each `sign` with each input.  
> **username_0**: 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); `  
> **username_0**: Do i have your email so that i can send you my full file, so that you will know what i'm doing  
> **username_0**: I have updated the question with the new code please see the code above and also read the comments in code. Thanks  
> **username_1**: I'll post a solution tomorrow.  
> **username_0**: Sure I'll be waiting  
> **username_1**: Does it work now?  
> **username_0**: Yes it does. i would like to upvote this answer but can't don't have such reputations. Thanks (Y)  

---

