Close Menu
  • Home
  • Altcoin
  • Bitcoin
  • Crypto
  • Forex
  • Online Money
What's Hot

South Korea Fines Bithumb $24 Million Over AML Violations

March 16, 2026

JCPenney Takes on Fashion Elitism With a Runway Show in Paris, Texas

March 16, 2026

Bitcoin set for best week since September 2025 as correlation with tech stocks weakens

March 15, 2026
Facebook X (Twitter) Instagram
  • Altcoin
  • Bitcoin
  • Crypto
  • Forex
  • Online Money
Facebook X (Twitter) Instagram
Cointelegraphe
  • Home
  • Altcoin
  • Bitcoin
  • Crypto
  • Forex
  • Online Money
Cointelegraphe
Home»Bitcoin»signature – Getting error for transferring BTC using node js code, getting error “No inputs were signed”
signature – Getting error for transferring BTC using node js code, getting error “No inputs were signed”
Bitcoin

signature – Getting error for transferring BTC using node js code, getting error “No inputs were signed”

adminBy adminAugust 14, 2024No Comments2 Mins Read
Share
Facebook Twitter LinkedIn Pinterest Email


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: '' };
  }
};



Source link

btc code Error inputs node signature signed transferring
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
admin
  • Website

Related Posts

South Korea Fines Bithumb $24 Million Over AML Violations

March 16, 2026

Bitcoin set for best week since September 2025 as correlation with tech stocks weakens

March 15, 2026

PI is available for trading!

March 14, 2026

Why the Future of Tokenised Assets is Collateral

March 13, 2026
Add A Comment
Leave A Reply Cancel Reply

Top Insights

South Korea Fines Bithumb $24 Million Over AML Violations

March 16, 2026

JCPenney Takes on Fashion Elitism With a Runway Show in Paris, Texas

March 16, 2026

Bitcoin set for best week since September 2025 as correlation with tech stocks weakens

March 15, 2026

The Professional Trading Tool at an Unprecedented Price – My Trading – 15 March 2026

March 15, 2026
ads

Subscribe to Updates

Get the latest creative news from Cointelegraphe about Crypto, bItcoin and Altcoin.

About Us
About Us

At CoinTelegraphe, we are dedicated to bringing you the latest and most insightful news, analysis, and updates from the dynamic world of cryptocurrency. Our mission is to provide our readers with accurate, timely, and comprehensive information to help them navigate the complexities of the crypto market.

Facebook X (Twitter) Instagram Pinterest YouTube
Top Insights

South Korea Fines Bithumb $24 Million Over AML Violations

March 16, 2026

JCPenney Takes on Fashion Elitism With a Runway Show in Paris, Texas

March 16, 2026

Bitcoin set for best week since September 2025 as correlation with tech stocks weakens

March 15, 2026
Get Informed

Subscribe to Updates

Get the latest creative news from Cointelegraphe about Crypto, bItcoin and Altcoin.

Please enable JavaScript in your browser to complete this form.
Loading
  • About us
  • Contact Us
  • Shop
  • Privacy Policy
  • Terms and Conditions
Copyright 2024 Cointelegraphe Design By Horaam Sultan.

Type above and press Enter to search. Press Esc to cancel.