How do I load my Solana wallet using my private key file?
Asked Answered
H

5

9

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

Heathcote answered 24/10, 2021 at 23:39 Comment(0)
H
9
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
Heathcote answered 24/10, 2021 at 23:39 Comment(4)
I've also used Keypair.fromSecretKey(Uint8Array.from(<private_key_here>))Laburnum
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 docs.solana.com/wallet-guide/file-system-wallet Any idea what I'm doing wrong?Certainty
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: i.sstatic.net/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");Heathcote
seriously config your code it's all white how hard is it to put a javascript tag in your ```Examen
P
4
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);
}
Papyrology answered 29/1, 2023 at 3:29 Comment(0)
M
3

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.

Moreland answered 29/7, 2022 at 13:16 Comment(0)
F
2

Okay, if the other answers look a bit overwhelming, then you can use a very easy way:

import bs58 from 'bs58';


const privateKey = 'private key'; // replace with your private key
const secret = bs58.decode(privateKey);
console.log ("My Uint8Array: ", secret);

const _privateKey = bs58.encode(secret);
console.log ("My private key", _privateKey);

one is for encoding and the other for decoding.

Fractional answered 26/1, 2024 at 18:2 Comment(0)
S
1

2023 answer

A few of us at Solana Foundation made the @solana-developers/helpers package to make this easier.

npm i @solana-developers/helpers @digitak/esrun

Then in show-pubkey.ts:

import { getKeypairFromFile } from "@solana-developers/node-helpers";
const keypair = await getKeypairFromFile();

You can run this with npx esrun load-pubkey.ts

See the @solana-developers/helpers docs for more.

Shinshina answered 13/11, 2023 at 20:22 Comment(2)
This is likely a legitimate tool, however, I would avoid using third-party tools to load your keysPidgin
@KurtisStreutker correct on both counts! Please look inside the package's contents to ensure you trust the source.Shinshina

© 2022 - 2025 — McMap. All rights reserved.