• About
  • FAQ
  • Earn Bitcoin while Surfing the net
  • Buy & Sell Crypto on Paxful
Newsletter
Approx Foundation
  • Home
    • Home – Layout 1
  • Bitcoin
  • Ethereum
  • Regulation
  • Market
  • Blockchain
  • Business
  • Guide
  • Contact Us
No Result
View All Result
  • Home
    • Home – Layout 1
  • Bitcoin
  • Ethereum
  • Regulation
  • Market
  • Blockchain
  • Business
  • Guide
  • Contact Us
No Result
View All Result
Approx Foundation
No Result
View All Result
Home Bitcoin

script – Sending Bitcoin: Expected property “signature” of type Buffer, got Uint8Array

Moussa by Moussa
December 24, 2024
in Bitcoin
0
peer discovery – how to obtain the IP addresses of nodes for mining pools?
189
SHARES
1.5k
VIEWS
Share on FacebookShare on Twitter


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"
  }
}



Source link

Related articles

How much data does it take to prove a given transaction exists on the blockchain?

August 24, 2026
Ripple Prime’s BBB Notes Actually Rely On

Ripple Prime’s BBB Notes Actually Rely On

August 24, 2026
Share76Tweet47

Related Posts

How much data does it take to prove a given transaction exists on the blockchain?

by Moussa
August 24, 2026
0

As I understand the SVP protocol, a client can be reasonably certain that a transaction has been accepted on the...

Ripple Prime’s BBB Notes Actually Rely On

Ripple Prime’s BBB Notes Actually Rely On

by Moussa
August 24, 2026
0

Ripple Prime closed an upsized $275 million private placement of senior unsecured notes, with Piper Sandler acting as lead placement...

Solana Validators Open Vote on Constitution

Solana Validators Open Vote on Constitution

by Moussa
August 24, 2026
0

Key TakeawaysSolana opened onchain voting on three governance proposals on August 22, 2026, running through epoch 1023.SIMD-0550 could cut future...

Private key of 44 chars? (old Blockchain.info wallet)

by Moussa
August 24, 2026
0

I'm trying to sweep some coins from an old (blockchain.info) wallet. I decoded the wallet I have and it gave...

PeaqOS and World ID Bring ZK Proofs to Autonomous Robots

PeaqOS and World ID Bring ZK Proofs to Autonomous Robots

by Moussa
August 24, 2026
0

Key TakeawaysPeaqOS integrated World ID on Aug. 21, 2026, allowing robots to natively verify users via robotic.sh.The integration replaces 1-to-1...

Load More

youssufi.com

sephina.com

[vc_row full_width="stretch_row" parallax="content-moving" vc_row_background="" background_repeat="no-repeat" background_position="center center" footer_scheme="dark" css=".vc_custom_1517813231908{padding-top: 60px !important;padding-bottom: 30px !important;background-color: #191818 !important;background-position: center;background-repeat: no-repeat !important;background-size: cover !important;}" footer_widget_title_color="#fcbf46" footer_button_bg="#fcb11e"][vc_column width="1/4"]

We bring you the latest in Crypto News

[/vc_column][vc_column width="1/4"][vc_wp_categories]
[/vc_column][vc_column width="1/4"][vc_wp_tagcloud taxonomy="post_tag"][/vc_column][vc_column width="1/4"]

Newsletter

[vc_raw_html]JTNDcCUzRSUzQ2RpdiUyMGNsYXNzJTNEJTIydG5wJTIwdG5wLXN1YnNjcmlwdGlvbiUyMiUzRSUwQSUzQ2Zvcm0lMjBtZXRob2QlM0QlMjJwb3N0JTIyJ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Y3klMjIlM0UlQzIlQTBCeSUyMGNvbnRpbnVpbmclMkMlMjB5b3UlMjBhY2NlcHQlMjB0aGUlMjBwcml2YWN5JTIwcG9saWN5JTNDJTJGbGFiZWwlM0UlM0MlMkZkaXYlM0UlM0NkaXYlMjBjbGFzcyUzRCUyMnRucC1maWVsZCUyMHRucC1maWVsZC1idXR0b24lMjIlM0UlM0NpbnB1dCUyMGNsYXNzJTNEJTIydG5wLXN1Ym1pdCUyMiUyMHR5cGUlM0QlMjJzdWJtaXQlMjIlMjB2YWx1ZSUzRCUyMlN1YnNjcmliZSUyMiUyMCUzRSUwQSUzQyUyRmRpdiUzRSUwQSUzQyUyRmZvcm0lM0UlMEElM0MlMkZkaXYlM0UlM0NiciUyRiUzRSUzQyUyRnAlM0U=[/vc_raw_html][/vc_column][/vc_row]
No Result
View All Result
  • Contact Us
  • Homepages
  • Business
  • Guide

© 2024 APPROX FOUNDATION - The Crypto Currency News