I’m making an attempt to ship the bitcoin with my non-public key utilizing the script and the bitcoinjs-lib
. I’m continually getting the error Anticipated property "signature" of kind Buffer, acquired Uint8Array
over the road once I name pstb.signInput()
.
I’m not in a position to determine the right way 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 operate sendBitcoin() {
attempt {
// Import non-public 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 = 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.submit('https://blockstream.data/api/tx', rawTransaction);
console.log(`Transaction efficiently broadcasted. TXID: ${broadcastResponse.information}`);
} catch (error) {
console.error(`Error: ${error.stack}`);
}
}
sendBitcoin();
If required, the beneath is the package deal 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"
}
}
Stack Hint Error:
Error: Error: Anticipated property "signature" of kind Buffer, acquired Uint8Array
at captureStackTrace (/house/self-cutodial-wallet/node_modules/typeforce/errors.js:20:11)
at tfSubError (/house/self-cutodial-wallet/node_modules/typeforce/errors.js:99:3)
at _object (/house/self-cutodial-wallet/node_modules/typeforce/index.js:117:15)
at typeforce (/house/self-cutodial-wallet/node_modules/typeforce/index.js:233:9)
at typeforce (/house/self-cutodial-wallet/node_modules/typeforce/index.js:239:10)
at Object.encode (/house/self-cutodial-wallet/node_modules/bitcoinjs-lib/src/script_signature.js:63:3)
at Psbt._signInput (/house/self-cutodial-wallet/node_modules/bitcoinjs-lib/src/psbt.js:679:38)
at Psbt.signInput (/house/self-cutodial-wallet/node_modules/bitcoinjs-lib/src/psbt.js:648:17)
at /house/self-cutodial-wallet/gg.js:82:18
at Array.forEach ()
```