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

banner

Converter

Source: CurrencyRate
Top Selling Multipurpose WP Theme

Newsletter

Subscribe my Newsletter for new blog posts, tips & new photos. Let's stay updated!

banner

Leave a Comment

Layer 1
Your Crypto & Blockchain Beacon

CryptoInsightful

Welcome to CryptoInsightful.com, your trusted source for in-depth analysis, news, and insights into the world of cryptocurrencies, blockchain technology, NFTs (Non-Fungible Tokens), and cybersecurity. Our mission is to empower you with the knowledge and understanding you need to navigate the rapidly evolving landscape of digital assets and emerging technologies.