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:
JavaScript
x
4
1
throw new Error('bad secret key size');
2
^
3
Error: bad secret key size
4
My code is as below:
JavaScript
1
10
10
1
import { Connection, Keypair, Transaction } from '@solana/web3.js'
2
import fetch from 'cross-fetch'
3
import { Wallet } from '@project-serum/anchor'
4
import bs58 from 'bs58'
5
6
const connection = new Connection('https://ssc-dao.genesysgo.net')
7
8
const PRIVATE_KEY = 'my secret key is this very dumb long confusing and unnecessary string'
9
const wallet = new Wallet(Keypair.fromSecretKey(bs58.decode(process.env.PRIVATE_KEY || '')))
10
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.