bitcoinjs-lib icon indicating copy to clipboard operation
bitcoinjs-lib copied to clipboard

Uncaught Error: Expected property "pubkey" of type ?isPoint, got Uint8Array

Open moodisk opened this issue 10 months ago • 5 comments

This problem has been bothering me for many days, I want to generate a P2WPKH address from an existing private key, but I always get an error. Thank you for helping me.

root@instance-2:~/bitcoinjs-lib# npm install bitcoinjs-lib root@instance-2:~/bitcoinjs-lib# npm install ecpair bip32 root@instance-2:~/bitcoinjs-lib# npm install tiny-secp256k1

root@instance-2:~/bitcoinjs-lib# node Welcome to Node.js v22.12.0. Type ".help" for more information.

const bitcoin = require('bitcoinjs-lib'); undefined const ECPairFactory = require('ecpair'); undefined const ecc = require('tiny-secp256k1'); undefined const ECPair = ECPairFactory.ECPairFactory(ecc); undefined const keyPair = ECPair.fromWIF('KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn'); undefined const { address } = bitcoin.payments.p2pkh({pubkey:keyPair.publicKey}); Uncaught Error: Expected property "pubkey" of type ?isPoint, got Uint8Array at captureStackTrace (/root/bitcoinjs-lib/node_modules/typeforce/errors.js:20:11) at tfSubError (/root/bitcoinjs-lib/node_modules/typeforce/errors.js:99:3) at _object (/root/bitcoinjs-lib/node_modules/typeforce/index.js:117:15) at typeforce (/root/bitcoinjs-lib/node_modules/typeforce/index.js:233:9) at typeforce (/root/bitcoinjs-lib/node_modules/typeforce/index.js:239:10) at Object.p2pkh (/root/bitcoinjs-lib/node_modules/bitcoinjs-lib/src/payments/p2pkh.js:25:25) { __label: undefined, __property: 'pubkey', __type: [Function: _maybe] { toJSON: [Function (anonymous)] }, __value: Uint8Array(33) [ 2, 121, 190, 102, 126, 249, 220, 187, 172, 85, 160, 98, 149, 206, 135, 11, 7, 2, 155, 252, 219, 45, 206, 40, 217, 89, 242, 129, 91, 22, 248, 23, 152 ], __valueTypeName: 'Uint8Array' }

moodisk avatar Feb 09 '25 12:02 moodisk

try this

const { address } = bitcoin.payments.p2pkh( {pubkey: Buffer.from(keyPair.publicKey)} )

HenryPendry avatar Feb 23 '25 17:02 HenryPendry

Thank you very much @HenryPendry This solved my problem.

AFwcxx avatar Mar 06 '25 03:03 AFwcxx

There is still the issue or close?

TomaszWaszczyk avatar Apr 13 '25 12:04 TomaszWaszczyk

Hello actually i am facing issue BTC testnet i am transfering tBTC from one address to another this is my script

require('dotenv').config(); const axios = require('axios'); const bitcoin = require('bitcoinjs-lib'); const { ECPairFactory } = require('ecpair'); const ecc = require('tiny-secp256k1');

const ECPair = ECPairFactory(ecc);

const NETWORK = bitcoin.networks.testnet; const FEE = 500; // satoshis

const wallets = [ { address: 'mnTTnB98zC4XgR2qirBmVyDKo2y9ANi7GK', privateKeyWIF: 'cPbVhk4cmTFEhKN9SYeM4c6cuEkTfmjUpNWnqZQ5KdEqdnC2RVYN' }, { address: 'mjqxs9AmWdWbssJ29oAWL9GUJ9DJFpj7Uo', privateKeyWIF: 'cSpKGaH75M9aezyGHeeoPgf9LwMBCx5xYuTyJAaUWtHZy8GP1NMG' } ];

const MASTER_ADDRESS = 'mq27qECfD8EqDQV9Q6MBZvXWP6RPKbSJYG';

async function getUtxos(address) { const url = https://blockstream.info/testnet/api/address/${address}/utxo; const response = await axios.get(url); return response.data; }

async function broadcastTx(rawHex) { const url = 'https://blockstream.info/testnet/api/tx'; const res = await axios.post(url, rawHex); return res.data; }

async function sweepWallet(wallet) { const keyPair = ECPair.fromWIF(wallet.privateKeyWIF, NETWORK);

const p2pkh = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: NETWORK });

const utxos = await getUtxos(wallet.address); if (!utxos.length) { console.log(No UTXOs for ${wallet.address}); return; }

const psbt = new bitcoin.Psbt({ network: NETWORK }); let totalInput = 0;

for (const utxo of utxos) { const txHex = (await axios.get(https://blockstream.info/testnet/api/tx/${utxo.txid}/hex)).data; psbt.addInput({ hash: utxo.txid, index: utxo.vout, nonWitnessUtxo: Buffer.from(txHex, 'hex'), }); totalInput += utxo.value; }

const sendAmount = totalInput - FEE; if (sendAmount <= 0) { console.log(Insufficient balance in ${wallet.address} after fee.); return; }

psbt.addOutput({ address: MASTER_ADDRESS, value: sendAmount, });

psbt.signAllInputs(keyPair); psbt.finalizeAllInputs();

const rawTx = psbt.extractTransaction().toHex(); const txid = await broadcastTx(rawTx); console.log(Swept ${sendAmount} sats from ${wallet.address} to master. TXID: ${txid}); }

(async () => { for (const wallet of wallets) { try { await sweepWallet(wallet); } catch (err) { console.error(Error sweeping ${wallet.address}:, err.message); } } })();

Error sweeping mnTTnB98zC4XgR2qirBmVyDKo2y9ANi7GK: Expected property "pubkey" of type ?isPoint, got Uint8Array Error sweeping mjqxs9AmWdWbssJ29oAWL9GUJ9DJFpj7Uo: Expected property "pubkey" of type ?isPoint, got Uint8Array poonam@poonam-HP

Poonam11-blockchain avatar Jul 24 '25 11:07 Poonam11-blockchain

Hello @HenryPendry

actually i am facing on btc testnet . i write a script for transfer bct one address to another but it through error again and again

require('dotenv').config(); const axios = require('axios'); const bitcoin = require('bitcoinjs-lib'); const { ECPairFactory } = require('ecpair'); const ecc = require('tiny-secp256k1'); const ECPair = ECPairFactory(ecc);

const NETWORK = bitcoin.networks.testnet; const FEE = 500; // sats

const wallets = [ { address: 'mnTTnB98zC4XgR2qirBmVyDKo2y9ANi7GK', privateKeyWIF: 'cPbVhk4cmTFEhKN9SYeM4c6cuEkTfmjUpNWnqZQ5KdEqdnC2RVYN' }, { address: 'mjqxs9AmWdWbssJ29oAWL9GUJ9DJFpj7Uo', privateKeyWIF: 'cSpKGaH75M9aezyGHeeoPgf9LwMBCx5xYuTyJAaUWtHZy8GP1NMG' } ];

const MASTER_ADDRESS = 'mq27qECfD8EqDQV9Q6MBZvXWP6RPKbSJYG';

async function getUtxos(address) { const url = https://blockstream.info/testnet/api/address/${address}/utxo; const response = await axios.get(url); return response.data; }

async function broadcastTx(rawHex) { const url = 'https://blockstream.info/testnet/api/tx'; const response = await axios.post(url, rawHex); return response.data; }

async function sweepWallet(wallet) { const keyPair = ECPair.fromWIF(wallet.privateKeyWIF, NETWORK); const address = wallet.address;

const utxos = await getUtxos(address); if (!utxos.length) { console.log(⚠️ No UTXOs for ${address}); return; }

const psbt = new bitcoin.Psbt({ network: NETWORK }); let totalInput = 0;

for (const utxo of utxos) { const rawTxHex = (await axios.get(https://blockstream.info/testnet/api/tx/${utxo.txid}/hex)).data;

try {
  psbt.addInput({
    hash: utxo.txid,
    index: utxo.vout,
    nonWitnessUtxo: Buffer.from(rawTxHex, 'hex'),
  });
  totalInput += utxo.value;
} catch (err) {
  console.error(`❌ Failed to add input for ${address}:`, err.message);
  return;
}

}

const sendAmount = totalInput - FEE; if (sendAmount <= 0) { console.log(❌ Not enough balance in ${address} after fee.); return; }

psbt.addOutput({ address: MASTER_ADDRESS, value: sendAmount, });

console.log(🔍 Inputs: ${psbt.inputCount}); console.log(🔍 Outputs: ${psbt.data.outputs.length});

try { psbt.signAllInputs(keyPair); const valid = psbt.validateSignaturesOfAllInputs(); console.log(✅ Valid signatures: ${valid});

if (!valid) {
  console.error(`❌ Signature validation failed for ${address}`);
  return;
}

psbt.finalizeAllInputs();
const rawTx = psbt.extractTransaction().toHex();
const txid = await broadcastTx(rawTx);
console.log(`✅ Swept ${sendAmount} sats from ${address} to ${MASTER_ADDRESS}. TXID: ${txid}`);

} catch (err) { console.error(❌ Signing or broadcasting failed for ${address}:, err.message); } }

(async () => { for (const wallet of wallets) { try { await sweepWallet(wallet); } catch (err) { console.error(❌ Error sweeping ${wallet.address}:, err.message); } } })();

please help me out how to resolve this issue poonam@poonam-HP-Notebook:~/Documents/Office/project$ node sweeperbct.js 🔍 Inputs: 1 🔍 Outputs: 1 ❌ Signing or broadcasting failed for mnTTnB98zC4XgR2qirBmVyDKo2y9ANi7GK: No inputs were signed 🔍 Inputs: 1 🔍 Outputs: 1 ❌ Signing or broadcasting failed for mjqxs9AmWdWbssJ29oAWL9GUJ9DJFpj7Uo: No inputs were signed

thanks and regards Poonam

Poonam11-blockchain avatar Jul 24 '25 12:07 Poonam11-blockchain