<p>I am creating a system where users will raise a request to transfer them some BTC, in my code i am using all the previous utxos of my wallet to transfer BTC, even i am signing all the utxos still i am getting error &quot;No inputs were signed&quot;, and i get this error for line
<code>psbt.signAllInputs(keyPair)</code></p>
<pre class="lang-js prettyprint-override"><code>import * as bitcoin from 'bitcoinjs-lib';
import { ECPairFactory } from 'ecpair';
import * as ecc from 'tiny-secp256k1';
import mempoolJS from '@mempool/mempool.js';

const ECPair = ECPairFactory(ecc);
const network = bitcoin.networks.testnet; // Use 'bitcoin.networks.bitcoin' for mainnet

const estimateTransactionSize = (numInputs: number, numOutputs: number): number =&gt; {
  return numInputs * 148 + numOutputs * 34 + 10;
};

const calculateFee = (feeRate: number, numInputs: number, numOutputs: number): number =&gt; {
  const txSize = estimateTransactionSize(numInputs, numOutputs);
  return feeRate * txSize;
};

export const sendBTC = async (
  privateKey: string,
  toAddress: string,
  amount: number
): Promise&lt;{ success: boolean; txHash: string }&gt; =&gt; {
  try {
    const { bitcoin: { addresses, transactions, fees } } = mempoolJS({
      hostname: 'mempool.space',
      network: 'testnet',
    });

    const keyPair = ECPair.fromPrivateKey(Buffer.from(privateKey, 'hex'));
    const p2wpkh = bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey, network });

    const address = p2wpkh.address;
    if (!address) {
      return { success: false, txHash: '' };
    }

    const utxos = await addresses.getAddressTxsUtxo({ address });
    const feesRecommended = await fees.getFeesRecommended();

    let totalSatoshisAvailable = 0;
    utxos.forEach(utxo =&gt; {
      if (utxo.value &gt;= 1000) {
        totalSatoshisAvailable += utxo.value;
      }
    });

    const numInputs = utxos.length;
    const numOutputs = 2; // Destination + Change
    const fee = calculateFee(feesRecommended.minimumFee, numInputs, numOutputs);
    const change = totalSatoshisAvailable - amount - fee;

    const psbt = new bitcoin.Psbt({ network });

    const promises = utxos.map(async (utxo) =&gt; {
      if (utxo.value &gt;= 1000) {
        psbt.addInput({
          hash: utxo.txid,
          index: utxo.vout,
          witnessUtxo: {
            script: Buffer.from(p2wpkh.output!.toString('hex'), 'hex'),
            value: utxo.value,
          },
        });
      }
    });
    await Promise.all(promises);

    psbt.addOutput({
      address: toAddress,
      value: amount,
    });

    if (change &gt; 0) {
      psbt.addOutput({
        address: address!,
        value: change,
      });
    }

    psbt.signAllInputs(keyPair);
    psbt.finalizeAllInputs();

    const rawTransaction = psbt.extractTransaction().toHex();
    const txId = await transactions.postTx({ txhex: rawTransaction });

    if (!txId) {
      return { success: false, txHash: '' };
    }

    return { success: true, txHash: txId.toString() };
  } catch (error) {
    console.error('Error while transferring bitcoin:', error);
    return { success: false, txHash: '' };
  }
};
</code></pre>

CC BY-SA 4.0
2024-08-13T19:27:46.347Z
This question could be improved by providing more context: What are you trying to do, what have you tried to fix your problem, where you do think is the issue in your code? Related: [Why are questions asking for proofreading of long descriptions of a concept not well received?](https://bitcoin.meta.stackexchange.com/q/1196/5406)
Murch
CC BY-SA 4.0
2024-08-14T07:05:24.687Z
I am creating a system where users will raise a request to transfer them some BTC, in my code i am using all the previous utxos of my wallet to transfer BTC, even i am signing all the utxos still i am getting error "No inputs were signed", and i get this error for line `psbt.signAllInputs(keyPair)`
Arun Lohia
CC BY-SA 4.0
2024-08-14T08:15:44.873Z
I guess you don't have `utxo` in your address(or if there is, value is less than 1000). Try logging either `utxo` or `psbt` right before `psbt.signAllInputs(keyPair)` to check emptiness.
Hyunhum Cho
CC BY-SA 4.0
2024-08-23T09:56:47.420Z
The wallet is having multiple utxos with value more than 1000
Arun Lohia
CC BY-SA 4.0
2024-08-24T15:06:47.480Z
You should write a clear question here. Please do not ask people to read and understand your code to afterwards even correct it. Describe what you are trying to accomplish and only paste the part of the code that is relevant to understand or reproduce the issue.
TomS
<signature><javascript><bip174-psbt><bitcoinjs>
Getting error for transferring BTC using node js code, getting error "No inputs were signed"
Arun Lohia
# Getting error for transferring BTC using node js code, getting error "No inputs were signed"

(Asked by: username_0 on 2024-08-13)

I am creating a system where users will raise a request to transfer them some BTC, in my code i am using all the previous utxos of my wallet to transfer BTC, even i am signing all the utxos still i am getting error "No inputs were signed", and i get this error for line
psbt.signAllInputs(keyPair)
import * as bitcoin from 'bitcoinjs-lib';
import { ECPairFactory } from 'ecpair';
import * as ecc from 'tiny-secp256k1';
import mempoolJS from '@mempool/mempool.js';

const ECPair = ECPairFactory(ecc);
const network = bitcoin.networks.testnet; // Use 'bitcoin.networks.bitcoin' for mainnet

const estimateTransactionSize = (numInputs: number, numOutputs: number): number => {
  return numInputs * 148 + numOutputs * 34 + 10;
};

const calculateFee = (feeRate: number, numInputs: number, numOutputs: number): number => {
  const txSize = estimateTransactionSize(numInputs, numOutputs);
  return feeRate * txSize;
};

export const sendBTC = async (
  privateKey: string,
  toAddress: string,
  amount: number
): Promise<{ success: boolean; txHash: string }> => {
  try {
    const { bitcoin: { addresses, transactions, fees } } = mempoolJS({
      hostname: 'mempool.space',
      network: 'testnet',
    });

    const keyPair = ECPair.fromPrivateKey(Buffer.from(privateKey, 'hex'));
    const p2wpkh = bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey, network });

    const address = p2wpkh.address;
    if (!address) {
      return { success: false, txHash: '' };
    }

    const utxos = await addresses.getAddressTxsUtxo({ address });
    const feesRecommended = await fees.getFeesRecommended();

    let totalSatoshisAvailable = 0;
    utxos.forEach(utxo => {
      if (utxo.value >= 1000) {
        totalSatoshisAvailable += utxo.value;
      }
    });

    const numInputs = utxos.length;
    const numOutputs = 2; // Destination + Change
    const fee = calculateFee(feesRecommended.minimumFee, numInputs, numOutputs);
    const change = totalSatoshisAvailable - amount - fee;

    const psbt = new bitcoin.Psbt({ network });

    const promises = utxos.map(async (utxo) => {
      if (utxo.value >= 1000) {
        psbt.addInput({
          hash: utxo.txid,
          index: utxo.vout,
          witnessUtxo: {
            script: Buffer.from(p2wpkh.output!.toString('hex'), 'hex'),
            value: utxo.value,
          },
        });
      }
    });
    await Promise.all(promises);

    psbt.addOutput({
      address: toAddress,
      value: amount,
    });

    if (change > 0) {
      psbt.addOutput({
        address: address!,
        value: change,
      });
    }

    psbt.signAllInputs(keyPair);
    psbt.finalizeAllInputs();

    const rawTransaction = psbt.extractTransaction().toHex();
    const txId = await transactions.postTx({ txhex: rawTransaction });

    if (!txId) {
      return { success: false, txHash: '' };
    }

    return { success: true, txHash: txId.toString() };
  } catch (error) {
    console.error('Error while transferring bitcoin:', error);
    return { success: false, txHash: '' };
  }
};

**Tags:** signature, javascript, bip174-psbt, bitcoinjs

> **Comments:**
> **username_1**: This question could be improved by providing more context: What are you trying to do, what have you tried to fix your problem, where you do think is the issue in your code? Related: [Why are questions asking for proofreading of long descriptions of a concept not well received?](https://bitcoin.meta.stackexchange.com/q/1196/5406)  
> **username_0**: I am creating a system where users will raise a request to transfer them some BTC, in my code i am using all the previous utxos of my wallet to transfer BTC, even i am signing all the utxos still i am getting error "No inputs were signed", and i get this error for line `psbt.signAllInputs(keyPair)`  
> **username_2**: I guess you don't have `utxo` in your address(or if there is, value is less than 1000). Try logging either `utxo` or `psbt` right before `psbt.signAllInputs(keyPair)` to check emptiness.  
> **username_0**: The wallet is having multiple utxos with value more than 1000  
> **username_3**: You should write a clear question here. Please do not ask people to read and understand your code to afterwards even correct it. Describe what you are trying to accomplish and only paste the part of the code that is relevant to understand or reproduce the issue.  

---

## 0 answers

