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

What is Tokenised Equity? – Bitfinex blog

October 26, 2025

InstaTrade – A Bright Success at Forex Expo Dubai 2025! Diamond Sponsor, Industry Recognition, and Special Prizes « Blog InstaForex

October 26, 2025

Why Canada’s Reagan Tariff Ad Made President Trump so Angry

October 26, 2025
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

What is Tokenised Equity? – Bitfinex blog

October 26, 2025

litecoin – how to get my stolen crypto back

October 25, 2025

Bitcoin’s Next Bull Phase Could Be Near As BTC-Stablecoin Ratio Plummets

October 23, 2025

How I Used Vibe Coding to Build Custom SEO Tools (Without Writing Code!)

October 23, 2025
Add A Comment
Leave A Reply Cancel Reply

Top Insights

What is Tokenised Equity? – Bitfinex blog

October 26, 2025

InstaTrade – A Bright Success at Forex Expo Dubai 2025! Diamond Sponsor, Industry Recognition, and Special Prizes « Blog InstaForex

October 26, 2025

Why Canada’s Reagan Tariff Ad Made President Trump so Angry

October 26, 2025

litecoin – how to get my stolen crypto back

October 25, 2025
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

What is Tokenised Equity? – Bitfinex blog

October 26, 2025

InstaTrade – A Bright Success at Forex Expo Dubai 2025! Diamond Sponsor, Industry Recognition, and Special Prizes « Blog InstaForex

October 26, 2025

Why Canada’s Reagan Tariff Ad Made President Trump so Angry

October 26, 2025
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.