6

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.

  1. created a solana wallet : solana-keygen new -o keyfile.json
  2. display what is in that file -- it's a 64 byte array (this is a test key so no worries that this is the private key

[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)?

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
raddevus
  • 8,142
  • 7
  • 66
  • 87

3 Answers3

7
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
raddevus
  • 8,142
  • 7
  • 66
  • 87
  • 1
    I've also used `Keypair.fromSecretKey(Uint8Array.from())` – C.OG Oct 27 '21 at 08:45
  • 1
    Thanks for the really helpful example, I was able to use this to generate a working wallet, but found that my address differs from what I had on Phantom, where I had exported the seed phrase (12 words BIP39). I used `solana-keygen recover 'prompt://?key=0/0' -o keypair.json` to generate the seed file per https://docs.solana.com/wallet-guide/file-system-wallet Any idea what I'm doing wrong? – blockchainwtf Jan 31 '22 at 15:34
  • Try logging into your Phantom wallet in your browser. You need to export your private key. You have to choose Setttings (gear in lower right) of Phantom wallet. Then you have to scroll down and choose the Export Private Key. You'll have to provide your password. You can see all of that in this image: https://i.stack.imgur.com/w8LIq.png Once you do that, you'll have the proper key which you'll use in a call to var phantomSecret = from_b58("your-private-key-as-a-string"); – raddevus Feb 01 '22 at 15:55
  • seriously config your code it's all white how hard is it to put a javascript tag in your ``` – max89 Mar 27 '22 at 13:14
2

If you want to use ".json" file, you can do something like this:

import Fs from "@supercharge/fs";
import { Connection, Keypair, LAMPORTS_PER_SOL } from "@solana/web3.js";

const decodedKey = new Uint8Array(
JSON.parse(
  //replace with actual path from home dir. For example '.config/solana/devnet.json'
  Fs.readFileSync(Fs.homeDir("path to key.json")).toString();
));

let keyPair = Keypair.fromSecretKey(decodedKey);

I use additional package https://www.npmjs.com/package/@supercharge/fs for working with files.

0
import { Keypair } from "@solana/web3.js";
import fs from "fs";

function loadKeypairFromFile(filename: string): Keypair {
  
  const secret = JSON.parse(fs.readFileSync(filename).toString()) as number[];
  const secretKey = Uint8Array.from(secret);
  return Keypair.fromSecretKey(secretKey);
}
Yilmaz
  • 35,338
  • 10
  • 157
  • 202