2

i am using this in react native and amazed with thing that in one project its giving me the balance without any error but that project is only for testing ,but when i integrated it with the main project it gives me the above mentioned error i also doubled checked the versions for both but still getting this, attaching my code snippest below

  const connection = new Web3.Connection('https://api.mainnet-beta.solana.com');

  // 5YNmS1R9nNSCDzb5a7mMJ1dwK9uHeAAF4CmPEwKgVWr8
  const feePayer = Web3.Keypair.fromSecretKey(
    base58.decode(
      'reQRFKEf8jk9BZ8AptA2xww3qQ4h8omtvheKBaG9u2cBGjRcj547wVgQDkQgbPrsu2Z6phTonpAocaqmxGTR7ox',
    ),
  );
  const getBalanceSol = async () => {
    try {
      let balance = await connection.getBalance(feePayer.publicKey);
      console.log(`${balance / Web3.LAMPORTS_PER_SOL} SOL`);
    } catch (error) {
      console.log('error while getting balance ', error);
    }
  };

2 Answers 2

3

It looks like there's an error in your secret key. When I try to run this locally, I get:

.../node_modules/@solana/web3.js/lib/index.cjs.js:9870
      throw new Error('bad secret key size');
            ^

Error: bad secret key size
    at Keypair.fromSecretKey (.../node_modules/@solana/web3.js/lib/index.cjs.js:9870:13)
    at .../index.js:4:26
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

So be sure that the base58 version of your private key is correct!

1
  • Jon C thanks for your attention but in my case private key was ok ,that what i got after retry i posted in my answer below
    – Mian Bilal
    Commented Apr 19, 2023 at 11:20
0

Actually i was using @ethersproject/shims this package caused that error after just removing import of this package it worked for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.