Search code examples
node.jsbitcoinbitcoinjs-lib

How to send bitcoin BTC using send-crypto using WIF key in node,js


I am trying to send bitcoin using the send-crypto library. It seems to require the private key to be in hex format, but my key is of the form "L3fKJ..." which I understand to be in WIF format? I think I need to use ECPair to convert it but it does not work.

The current error is "TypeError: ECPair.fromWIF is not a function". If I create a new private using send-crypto the balance code works fine.

If there is an easier way without using send-crypto will to try that too.

const bitcoin = require('bitcoinjs-lib');
const ECPair = require('ecpair');
const CryptoAccount = require("send-crypto");

/* Load account from private key */
const privateKeyWIF = "L3fKJ...";
const keyPair = ECPair.fromWIF(privateKeyWIF);
const privateKey = keyPair.privateKey;
console.log(privateKey);
const account = new CryptoAccount(privateKey);

async function start() {
    console.log(await account.address("BTC"));

    console.log(await account.getBalance("BTC"));
    console.log(await account.getBalance("BTC", {address:"bc1qe6..."}));

    const balance = await account.getBalance("BTC");
    await account.send("bc1qe6...", balance, "BTC", {
            subtractFee: true,
    });

};
start();

Solution

  • So this is the code that ended up working. Note it looks like the bitcoinjs code needs to be different depending on the version, not at all backward compatible, this code is working on the latest as of Oct 2023.

    // npm install --save send-crypto
    // npm install --save bitcoinjs-lib
    // npm install --save ecpair
    // npm install --save tiny-secp256k1
    
    const bitcoin = require('bitcoinjs-lib');
    const ECPairFactory = require('ecpair');
    const ecc = require('tiny-secp256k1');
    const CryptoAccount = require("send-crypto");
    
    const network = bitcoin.networks.bitcoin;
    
    // Load your private key (WIF)
    const ECPair = ECPairFactory.ECPairFactory(ecc);
    const privateKeyWIF = 'L3fK...';
    const keyPair = ECPair.fromWIF(privateKeyWIF, network);
    
    /* Load account from private key */
    //const privateKey = process.env.PRIVATE_KEY || CryptoAccount.newPrivateKey();
    const privateKey = keyPair.privateKey;
    console.log(privateKey);
    const account = new CryptoAccount(privateKey);
    
    async function start() {
        console.log(await account.address("BTC"));
    
        console.log(await account.getBalance("BTC"));
        console.log(await account.getBalance("BTC", {address:"bc1q..."}));
    
        const balance = await account.getBalance("BTC");
        await account.send("bc1q...", balance, "BTC", {
                subtractFee: true,
        });
    
        /*const txHash = await account
                .send("bc1q...", 0.01, "BTC")
                .on("transactionHash", console.log)
                .on("confirmation", console.log);*/
    
    
        console.log(await account.getBalance("BTC"));
        console.log(await account.getBalance("BTC", {address:"bc1q..."}));
    
    };
    start();