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

The 30-Minute Trading Routine For Busy People » Learn To Trade The Market

June 18, 2025

15 Activities to Bookend Your Trip to MozCon London

June 18, 2025

FLUZ  – ein Update zum Ökosystem – Der Altcoinspekulant

June 18, 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

Crypto Products ‘Defy Geopolitical Tensions’ in Sudden $1,900,000,000 Inflow Rebound: CoinShares

June 16, 2025

Bitcoin Layer 2: Statechains

June 15, 2025

ETH Whales and Sharks Add 1.49M in 30 Days, Now Hold 27% of Total Ether Supply

June 14, 2025

Keeping our platform safe: How we protect against fraud and criminal activity

June 13, 2025
Add A Comment
Leave A Reply Cancel Reply

Top Insights

The 30-Minute Trading Routine For Busy People » Learn To Trade The Market

June 18, 2025

15 Activities to Bookend Your Trip to MozCon London

June 18, 2025

FLUZ  – ein Update zum Ökosystem – Der Altcoinspekulant

June 18, 2025

Darma System Indicator (beta) MT4 Indicator

June 17, 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

The 30-Minute Trading Routine For Busy People » Learn To Trade The Market

June 18, 2025

15 Activities to Bookend Your Trip to MozCon London

June 18, 2025

FLUZ  – ein Update zum Ökosystem – Der Altcoinspekulant

June 18, 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.