<p>Sure, here's an example of how you could sign a transaction with multiple UTXOs using only the extended private key in JavaScript:</p>
<pre><code>const bitcoin = require('bitcoinjs-lib');

// Define the network (e.g. testnet or mainnet)
const network = bitcoin.networks.testnet;

// Define the transaction inputs
const utxos = [
  {
    txid: 'txid1',
    vout: 0,
    value: 5000000,
    scriptPubKey: 'scriptPubKey1'
  },
  {
    txid: 'txid2',
    vout: 0,
    value: 7000000,
    scriptPubKey: 'scriptPubKey2'
  }
];

// Define the transaction outputs
const outputs = [
  {
    address: 'recipientAddress1',
    value: 10000000
  }
];

// Define the extended private key
const extendedPrivateKey = 'xprvXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

// Create a new transaction builder
const txb = new bitcoin.TransactionBuilder(network);

// Add each UTXO as an input to the transaction
for (const utxo of utxos) {
  txb.addInput(utxo.txid, utxo.vout);
}

// Add each output to the transaction
for (const output of outputs) {
  txb.addOutput(output.address, output.value);
}

// Derive the private key for each input using the extended private key
for (let i = 0; i &lt; utxos.length; i++) {
  const key = bitcoin.bip32.fromBase58(extendedPrivateKey, network).derivePath(`m/0/${i}`).privateKey;
  txb.sign(i, key, null, bitcoin.Transaction.SIGHASH_ALL, utxos[i].value);
}

// Build the transaction and print it
const tx = txb.build();
console.log(tx.toHex());
</code></pre>

CC BY-SA 4.0
2023-04-03T00:02:29.267Z
2023-04-03T00:02:29.267Z
Irtaza fayaz
<p>For anyone that might be interested in this, there is a great <a href="https://github.com/bitcoinjs/bip32" rel="nofollow noreferrer">BIP32 library</a> from the same people who created the bitcoinjs-lib and it has the tools to derive your private keys from the extended master-private-key.</p>

<pre><code>const t = bip32.fromBase58(extPrivateKey, zenCashNetwork);

const derivedIndex = `${utxo.AddrType}/${utxo.AddrIndex}`;

const wifKey = t.derivePath(derivedIndex).toWIF();
</code></pre>

<p>In our case, we needed this to sign a ZenCash transaction</p>

CC BY-SA 4.0
2018-08-10T15:07:17.803Z
2018-08-10T15:07:17.803Z
Prem Govind
<p>Is there any Javascript code out there or examples on how to sign a transaction by using the extended-private-key?</p>

<p>What I am essentially looking for is <strong><em>code that will help derive the private key</em></strong>, that I can then use to sign an individual UTXO with.</p>

<p>Thanks in advance!</p>

<private-key><bitcoinjs><xpub>
Looking for JS/Node.js code example to sign a transaction (with multiple UTXOs) with only the extended private key
Prem Govind
# Looking for JS/Node.js code example to sign a transaction (with multiple UTXOs) with only the extended private key

(Asked by: username_0 on 2018-08-09)

Is there any Javascript code out there or examples on how to sign a transaction by using the extended-private-key?

What I am essentially looking for is ***code that will help derive the private key***, that I can then use to sign an individual UTXO with.

Thanks in advance!

**Tags:** private-key, bitcoinjs, xpub


---

## 2 answers

### Answer by username_1 on 2023-04-03. Score: 0

Sure, here's an example of how you could sign a transaction with multiple UTXOs using only the extended private key in JavaScript:

```
const bitcoin = require('bitcoinjs-lib');

// Define the network (e.g. testnet or mainnet)
const network = bitcoin.networks.testnet;

// Define the transaction inputs
const utxos = [
  {
    txid: 'txid1',
    vout: 0,
    value: 5000000,
    scriptPubKey: 'scriptPubKey1'
  },
  {
    txid: 'txid2',
    vout: 0,
    value: 7000000,
    scriptPubKey: 'scriptPubKey2'
  }
];

// Define the transaction outputs
const outputs = [
  {
    address: 'recipientAddress1',
    value: 10000000
  }
];

// Define the extended private key
const extendedPrivateKey = 'xprvXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

// Create a new transaction builder
const txb = new bitcoin.TransactionBuilder(network);

// Add each UTXO as an input to the transaction
for (const utxo of utxos) {
  txb.addInput(utxo.txid, utxo.vout);
}

// Add each output to the transaction
for (const output of outputs) {
  txb.addOutput(output.address, output.value);
}

// Derive the private key for each input using the extended private key
for (let i = 0; i < utxos.length; i++) {
  const key = bitcoin.bip32.fromBase58(extendedPrivateKey, network).derivePath(`m/0/${i}`).privateKey;
  txb.sign(i, key, null, bitcoin.Transaction.SIGHASH_ALL, utxos[i].value);
}

// Build the transaction and print it
const tx = txb.build();
console.log(tx.toHex());

```


---

### Answer by username_0 on 2018-08-10. Score: 0

For anyone that might be interested in this, there is a great [BIP32 library](https://github.com/bitcoinjs/bip32) from the same people who created the bitcoinjs-lib and it has the tools to derive your private keys from the extended master-private-key.

```
const t = bip32.fromBase58(extPrivateKey, zenCashNetwork);

const derivedIndex = `${utxo.AddrType}/${utxo.AddrIndex}`;

const wifKey = t.derivePath(derivedIndex).toWIF();

```

In our case, we needed this to sign a ZenCash transaction


---

