I’m attempting to ship the bitcoin with my personal key utilizing the script and the bitcoinjs-lib
. I’m continuously getting the error Anticipated property "signature" of sort Buffer, acquired Uint8Array
over the road once I name pstb.signInput()
.
I’m not ready to determine learn 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 perform sendBitcoin() {
strive {
// 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 out there 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 = 5000;
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) => {
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.put up('https://blockstream.information/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 bundle model record:
{
"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"
}
}