Sending Bitcoin: Expected property "signature" of type Buffer, got Uint8Array
I am trying to send the bitcoin with my private key using the script and the bitcoinjs-lib
. I am constantly getting the error Expected property "signature" of type Buffer, got Uint8Array
over the line when I call pstb.signInput()
.
I am not able to figure out how to 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 function sendBitcoin() {
try {
// Import private key
const keyPair = ECPair.fromWIF(PRIVATE_KEY_WIF, NETWORK);
// Generate the address (Native SegWit - P2WPKH)
const { address } = bitcoin.payments.p2wpkh({
pubkey: Buffer.from(keyPair.publicKey),
network: NETWORK,
});
console.log(`Sending from: ${address}`);
// Fetch UTXOs for the address
const utxos = await getUtxos();
console.log(`Fetched UTXOs:`, utxos);
if (!Array.isArray(utxos) || utxos.length === 0) {
throw new Error("No UTXOs available for the given address.");
}
// Create a new PSBT
const psbt = new bitcoin.Psbt({ network: NETWORK });
console.log(`Initialized PSBT:`, psbt);
let totalInput = 0;
// Add inputs from UTXOs
utxos.forEach((utxo) => {
console.log(`Adding UTXO: ${JSON.stringify(utxo)}`);
psbt.addInput({
hash: utxo.txid,
index: utxo.vout,
witnessUtxo: {
script: Buffer.from(bitcoin.address.toOutputScript(address, NETWORK)),
value: utxo.value,
},
});
totalInput += utxo.value;
});
// Calculate the amount to send
const sendAmount = 5000;
if (sendAmount <= 0) {
throw new Error("Insufficient funds after deducting fees.");
}
// Add output for destination
psbt.addOutput({
address: DESTINATION_ADDRESS,
value: sendAmount,
});
// Add change output if applicable
const change = totalInput - sendAmount - FEE;
if (change > 0) {
const changeAddress = bitcoin.payments.p2wpkh({
pubkey: Buffer.from(keyPair.publicKey),
network: NETWORK,
}).address;
console.log(`Adding change output: ${changeAddress}`);
psbt.addOutput({
address: changeAddress,
value: change,
});
}
utxos.forEach((_, index) => {
console.log(`Signing input at index: ${index}`);
psbt.signInput(index, keyPair);
});
const isValid = psbt.validateSignaturesOfAllInputs();
console.log(`Signatures valid: ${isValid}`);
psbt.finalizeAllInputs();
const rawTransaction = psbt.extractTransaction().toHex();
console.log(`Raw Transaction: ${rawTransaction}`);
console.log('Transaction ready to broadcast:', rawTransaction);
const broadcastResponse = await axios.post('https://blockstream.info/api/tx', rawTransaction);
console.log(`Transaction successfully broadcasted. TXID: ${broadcastResponse.data}`);
} catch (error) {
console.error(`Error: ${error.stack}`);
}
}
sendBitcoin();
If required, the below is the package version list:
{
"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"
}
}
from Recent Questions - Bitcoin Stack Exchange https://ift.tt/48IhPlA
via IFTTT