I'm attempting to use the private key I generated using the Solana command-line to create a wallet in JavaScript / Node.
I want to use the web3.Keypair.fromSeed()
method.
Here are the steps I've take so far.
solana-keygen new -o keyfile.json
[237,158,92,107,132,192,1,57,8,20,213,108,29,227,37,8,3,105,196,244,8,221,184,199,62,253,98,131,33,165,165,215,14,7,46,23,221,242,240,226,94,79,161,31,192,163,13,25,106,53,34,215,83,124,162,156,8,97,194,180,213,179,33,68]
However, the call to fromSeed()
only wants 32 bytes.
3. check the solana address so I know when everything is working properly :
> solana address -->
wm4MapPtFaUoSsyBJppVnChYMGvHzTeAL6BNZ5GmSqH
That's the public key
How do I call web3.Keypair.fromSeed()
to load that private key and get my public address (aka public key)?
let web3 = require('@solana/web3.js');
let splToken = require('@solana/spl-token');
// load up the first 32 bytes of the 64 byte array that was in our keyfile.json
// Only need the first 32 bytes so I use slice() just to make sure it's the correct length
let firstWinPrivKey = [237,158,92,107,132,192,1,57,8,20,213,108,29,227,37,8,3,105,196,244,8,221,184,199,62,253,98,131,33,165,165,215,14,7,46,23,221,242,240,226,94,79,161,31,192,163,13,25,106,53,34,215,83,124,162,156,8,97,194,180,213,179,33,68]
.slice(0,32);
// print the length of the array so we know it is correct
// the fromSeed() method requires 32 bytes
console.log(firstWinPrivKey.length);
let firstWinWallet = web3.Keypair.fromSeed(Uint8Array.from(firstWinPrivKey));
console.log(firstWinWallet.secretKey);
console.log(firstWinWallet.publicKey.toString());
Notice that you have to cast the array to a Uint8Array (Uint8Array.from()) When we print out the secretKey, you'll see the same bytes you passed in.
And finally when we print out the publicKey you'll see that same value that we saw with the command line
> solana address
Now you can use the wallet in code.
Here's the final output from this short script:
32
Uint8Array(64) [
237, 158, 92, 107, 132, 192, 1, 57, 8, 20, 213,
108, 29, 227, 37, 8, 3, 105, 196, 244, 8, 221,
184, 199, 62, 253, 98, 131, 33, 165, 165, 215, 14,
7, 46, 23, 221, 242, 240, 226, 94, 79, 161, 31,
192, 163, 13, 25, 106, 53, 34, 215, 83, 124, 162,
156, 8, 97, 194, 180, 213, 179, 33, 68
]
wm4MapPtFaUoSsyBJppVnChYMGvHzTeAL6BNZ5GmSqH