Skip to content
Advertisement

Trying to test Wallet instantiation in Solana JS

I’m trying to follow some code to test out instantiating a wallet in code. I’m hardcoding private key (will not do this in prod obviously) just to see how everything works. I’m getting this error:

    throw new Error('bad secret key size');
          ^
Error: bad secret key size

My code is as below:

import { Connection, Keypair, Transaction } from '@solana/web3.js'
import fetch from 'cross-fetch'
import { Wallet } from '@project-serum/anchor'
import bs58 from 'bs58'

const connection = new Connection('https://ssc-dao.genesysgo.net')

const PRIVATE_KEY = 'my secret key is this very dumb long confusing and unnecessary string'
const wallet = new Wallet(Keypair.fromSecretKey(bs58.decode(process.env.PRIVATE_KEY || '')))

Advertisement

Answer

So the issue here was the encoding. For ease of use, it’s best to use the phantom wallet. I tried converting the seed to bs58 but I was still unsuccessful. If you insist on using another wallet and are using the .js web3 library for Solana, you’d probably be better off using

const wallet = new Wallet(Keypair.fromSecretKey(new Uint8Array([1,2,3,4,5,6]))) but this would require you to keep your private key array exposed which is not recommended.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement