Wednesday, December 25, 2024
HomeBitcoinscript - Sending Bitcoin: Anticipated property "signature" of kind Buffer, received Uint8Array

script – Sending Bitcoin: Anticipated property “signature” of kind Buffer, received Uint8Array


I’m making an attempt to ship the bitcoin with my personal key utilizing the script and the bitcoinjs-lib. I’m consistently getting the error Anticipated property "signature" of kind Buffer, received Uint8Array over the road after I name pstb.signInput().

I’m not ready to determine how one can resolve this,

const bitcoin = require('bitcoinjs-lib');
const ecc = require('tiny-secp256k1');
const ECPairFactory = require('ecpair').default;
const ECPair = ECPairFactory(ecc);
const { getUtxos } = require('./utxo');

const NETWORK = bitcoin.networks.bitcoin
const PRIVATE_KEY_WIF = "PRIVATE_KEY"; 
const DESTINATION_ADDRESS = "bc1q9es00454u6n49ullgg2cyh23prpglpees5ptyc"; 
const FEE = 1500; 

async perform sendBitcoin() {
    strive {
        // Import personal key
        const keyPair = ECPair.fromWIF(PRIVATE_KEY_WIF, NETWORK);

        // Generate the tackle (Native SegWit - P2WPKH)
        const { tackle } = bitcoin.funds.p2wpkh({
            pubkey: Buffer.from(keyPair.publicKey),
            community: NETWORK,
        });
        console.log(`Sending from: ${tackle}`);

        // Fetch UTXOs for the tackle
        const utxos = await getUtxos();
        console.log(`Fetched UTXOs:`, utxos);

        if (!Array.isArray(utxos) || utxos.size === 0) {
            throw new Error("No UTXOs obtainable for the given tackle.");
        }

        // Create a brand new PSBT
        const psbt = new bitcoin.Psbt({ community: NETWORK });
        console.log(`Initialized PSBT:`, psbt);

        let totalInput = 0;

        // Add inputs from UTXOs
        utxos.forEach((utxo) => {
            console.log(`Including UTXO: ${JSON.stringify(utxo)}`);
            psbt.addInput({
                hash: utxo.txid,
                index: utxo.vout,
                witnessUtxo: {
                    script: Buffer.from(bitcoin.tackle.toOutputScript(tackle, NETWORK)),
                    worth: utxo.worth,
                },
            });
            totalInput += utxo.worth;
        });

        // Calculate the quantity to ship
        const sendAmount = 5000;
        if (sendAmount <= 0) {
            throw new Error("Inadequate funds after deducting charges.");
        }

        // Add output for vacation spot
        psbt.addOutput({
            tackle: DESTINATION_ADDRESS,
            worth: sendAmount,
        });

        // Add change output if relevant
        const change = totalInput - sendAmount - FEE;
        if (change > 0) {
            const changeAddress = bitcoin.funds.p2wpkh({
                pubkey: Buffer.from(keyPair.publicKey),
                community: NETWORK,
            }).tackle;
            console.log(`Including change output: ${changeAddress}`);
            psbt.addOutput({
                tackle: changeAddress,
                worth: change,
            });
        }

        utxos.forEach((_, index) => {
            console.log(`Signing enter at index: ${index}`);
            psbt.signInput(index, keyPair);
        });

        const isValid = psbt.validateSignaturesOfAllInputs();
        console.log(`Signatures legitimate: ${isValid}`);

        psbt.finalizeAllInputs();

        const rawTransaction = psbt.extractTransaction().toHex();
        console.log(`Uncooked Transaction: ${rawTransaction}`);

        console.log('Transaction able to broadcast:', rawTransaction);

        const broadcastResponse = await axios.submit('https://blockstream.data/api/tx', rawTransaction);
        console.log(`Transaction efficiently broadcasted. TXID: ${broadcastResponse.knowledge}`);
    } catch (error) {
        console.error(`Error: ${error.stack}`);
    }
}

sendBitcoin();

If required, the under is the package deal model listing:

{
  "dependencies": {
    "@mempool/mempool.js": "^2.3.0",
    "axios": "^1.7.9",
    "bip32": "^5.0.0-rc.0",
    "bip39": "^3.1.0",
    "bitcoinjs-lib": "^6.1.7",
    "bs58": "^6.0.0",
    "ecpair": "^3.0.0-rc.0",
    "hdkey": "^2.1.0",
    "tiny-secp256k1": "^2.2.3"
  },
  "devDependencies": {
    "dotenv": "^16.4.7"
  }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments