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 Want validator perform to validate signatures over the road once I name const isValid = psbt.validateSignaturesOfAllInputs();.
I’m not in a position to determine how you can resolve this,
const ecc = require('tiny-secp256k1');
const ECPairFactory = require('ecpair').default;
const ECPair = ECPairFactory(ecc);
const axios = require('axios');
const NETWORK = bitcoin.networks.testnet
// const community = bitcoin.networks.testnet; // Or bitcoin.networks.bitcoin for mainnet
const sender_address = "tb1qmrhvnv0w6p9asunmsua3ua829artanps6jfza7"; // Substitute along with your deal with
const PRIVATE_KEY_WIF = "PRIVATE_KEY";
const DESTINATION_ADDRESS = "mggvPKgz8UJu6sWWdxwPKLKYoyHKhJGzCY";
const FEE = 1000;
async perform sendBitcoin() {
attempt {
// Import personal key
const keyPair = ECPair.fromWIF(PRIVATE_KEY_WIF, NETWORK);
// Generate the deal with (Native SegWit - P2WPKH)
const { deal with } = bitcoin.funds.p2wpkh({
pubkey: Buffer.from(keyPair.publicKey),
community: NETWORK,
});
console.log(`Sending from: ${deal with}`);
// Fetch UTXOs for the deal with
const utxos = await getUtxos();
console.log(`Fetched UTXOs:`, utxos);
if (!Array.isArray(utxos) || utxos.size === 0) {
throw new Error("No UTXOs accessible for the given deal with.");
}
// 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.deal with.toOutputScript(deal with, NETWORK)),
worth: utxo.worth,
},
});
totalInput += utxo.worth;
});
// Calculate the quantity to ship
const sendAmount = 2000;
if (sendAmount <= 0) {
throw new Error("Inadequate funds after deducting charges.");
}
// Add output for vacation spot
psbt.addOutput({
deal with: 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,
}).deal with;
console.log(`Including change output: ${changeAddress}`);
psbt.addOutput({
deal with: changeAddress,
worth: change,
});
}
utxos.forEach((_, index) => {
psbt.signInput(index, {
publicKey: Buffer.from(keyPair.publicKey),
signal: (hash) => {
const signature = keyPair.signal(hash);
return Buffer.from(signature);
},
});
});
console.log(`Signed all inputs`);
const isValid = psbt.validateSignaturesOfAllInputs();
console.log(`Signatures legitimate: ${isValid}`);
psbt.finalizeAllInputs();
const rawTransaction = psbt.extractTransaction().toHex();
console.log(`Uncooked Transaction: ${rawTransaction}`);
let myHeaders = new Headers();
myHeaders.append("Content material-Sort", "software/json");
let uncooked = JSON.stringify({
"methodology": "sendrawtransaction",
"params": [
rawTransaction
]
});
let requestOptions = {
methodology: 'POST',
headers: myHeaders,
physique: uncooked,
redirect: 'comply with'
};
let quickNodeUrl = "https://abcd-efgh-igkl.btc-testnet.quiknode.professional/6a1xxxxxxxxxxxxxxxxxxxxxxxxx4/";
fetch(quickNodeUrl, requestOptions)
.then(response => response.json())
.then(end result => {
console.log("Broadcast Response: ", end result);
})
.catch(error => console.log('Error broadcasting transaction:', error));
} catch (error) {
console.error(`Error: ${error.stack}`);
}
}
sendBitcoin();
async perform getUtxos() {
attempt {
const response = await axios.get(`https://blockstream.data/testnet/api/deal with/${sender_address}/utxo`);
return response.knowledge;
} catch (error) {
console.error(`Error fetching UTXOs: ${error.message}`);
return [];
}
} ```.
Stack Hint Error:
``` Error: Want validator perform to validate signatures
at Psbt._validateSignaturesOfInput (/Customers/harshkushwah/Desktop/Btc_Tx_Listener/node_modules/bitcoinjs-lib/src/psbt.js:424:13)
at Psbt.validateSignaturesOfInput (/Customers/harshkushwah/Desktop/Btc_Tx_Listener/node_modules/bitcoinjs-lib/src/psbt.js:416:17)
at /Customers/harshkushwah/Desktop/Btc_Tx_Listener/node_modules/bitcoinjs-lib/src/psbt.js:404:12
at Array.map ()
at Psbt.validateSignaturesOfAllInputs (/Customers/harshkushwah/Desktop/Btc_Tx_Listener/node_modules/bitcoinjs-lib/src/psbt.js:403:52)
at sendBitcoin (/Customers/harshkushwah/Desktop/Btc_Tx_Listener/transfer-seqWit.js:93:30)
at course of.processTicksAndRejections (node:inner/course of/task_queues:95:5) ```