3

I'm trying to send bitcoin with the bitcoinjs-lib library and I get the following error:

TypeError: bitcoin.TransactionBuilder is not a constructor at sendmoney (/Users/....../bitcoin.js:53:18) at processTicksAndRejections (node:internal/process/task_queues:96:5)

And my code:

const sendmoney = async (amountSatoshis, fromAddress, privatekey, toAddress) => {
    const requestConfig = {
        method: 'GET',
        url: `https://blockchain.info/unspent?active=${fromAddress}`
    };

    try {
        const ECPair = ECPairFactory(ecc)
        let keyPair = ECPair.fromWIF(privatekey);

        const response = await axios(requestConfig);
        console.log(response.unspent_outputs)

        let tx = new bitcoin.TransactionBuilder();
        let txfeemin = 1000;

        let balance = 0;
        let inputCount = 0;

        response.data.unspent_outputs.forEach(uo => {
            tx.addInput(uo.tx_hash_big_endian, uo.tx_output_n);
            balance += uo.value;
            inputCount++;
        })


        var txfee = txfeemin;
        //Estimate transaction size in bytes
        var txSize = inputCount * 180 + 2 * 34 + 10 + inputCount;
        //Blockchain: Minimum fee is 1.5 satoshi / Byte
        if (txSize * 2 > txfeemin){
            txfee = txSize * 2;
        }
        tx.addOutput(toAddress, amountSatoshis); //without fee
        //tx.addOutput(toAddress, amountSatoshis - txfee); // with fee
        for (var i = 0; i < inputCount; i++) {
            tx.sign(i, keyPair);
        }
        var txHex = tx.build().toHex();

        let transactionConfig = {
            method: 'POST',
            url: 'https://blockchain.info/pushtx?cors=true',
            data: 'tx=' + txHex
        };

        try{
            const transaction = await axios(requestConfig);
            conasole.log('transaction', transaction)
        }
        catch(err){
            console.log(err)
        }


    }
    catch (err) {
        console.log(err);
        return err;
    }
}
2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.