Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import phantom wallet private key into solana CLI

I need to use a Phantom Wallet through the Solana CLI, but I'm unable to configure it.

For example, to check balance using

solana balance --keypair fileexportedfromphantom

but can't read the info.

How do I convert that private key into a valid form for use in Solana CLI?

like image 709
user1843376 Avatar asked Sep 19 '21 18:09

user1843376


People also ask

What is private key in phantom wallet?

Phantom is a self-custodial wallet. Your private keys are encrypted on your device by your password and are never shared with anyone.

How do I add a wallet to my phantom?

If you are using Chrome, it will be in the top right hand side of your browser. If you don't see it look for a "puzzle piece" icon and click on it to access a list of installed extensions. There you should find Phantom, click on the "pin icon" to make it easier to find in the future.

How do I connect Solana to Phantom wallet?

The private key for your cli address is stored in plain text in the file : ~/.config/solana/id.json Then, using the hamburger menu in the Phantom extension, click on “Add / Connect Wallet” In that screen, you can give a name and paste the private key of your cli address:

How to import private key from Solana to Phantom?

Mint your token (I am skipping most of the steps here, there are good resources on Solana website) 4. Now, go to your Phantom web wallet, click on three lines on the left 5. Click on Add/Connect Wallet 6. Click on Import private key 7. Paste the entire private key - starting with [ and ending ] 8. Provide a name of your choice, and click on Import

Can I use Phantom with Solana?

One non-obvious thing about Phantom: If you use the secret recovery phrase, as suggested in this answer, anyone who has that recovery phrase will be able to construct the private keys for ALL accounts in that wallet. I suspect the more common use case is to just get ONE special account/wallet imported into the Solana CLI.

How to use CLI wallet with Phantom and solflare?

Not sure if anyone is still looking for an answer, but here are the steps to use your CLI wallet and SPL token with Phantom and Solflare: This will give you your seedphrase and also will generate a private key that is written to id.json (you will get the path as well) - retain both seedphrase and private key.


Video Answer


2 Answers

Try:

solana-keygen recover 'prompt://?key=0/0' -o <file.json>

And enter the 24-word recovery phrase from Phantom under "Show Secret Recovery Phrase".

This is because Phantom uses the 0/0 derivation path for wallets and needs the extra provided path to get to the correct account.

You can use the same command with 1/0, 2/0 ... N/0 to get the different Phantom derived accounts.

See here for more info about hierarchical derivation with the Solana tools: https://docs.solana.com/wallet-guide/paper-wallet#hierarchical-derivation

Or use the Solflare wallet to check the derivation paths for your particular 24 word phrase here: https://solflare.com/access


As per the recent comment from @FutForFut, this assumes you have or want to use the Secret Recovery Phrase from Phantom. In certain cases, you might only have the private key from Phantom ("Show Private Key") in menus. This is a base58 encoded key, and you'll need to convert that into a byte-array in a JSON file.

Here's the Javascript snippet, using the bs58 package (https://www.npmjs.com/package/bs58):

const bs58 = require('bs58');
const fs = require('fs');
b = bs58.decode('privatekeyexportedfromphantom');
j = new Uint8Array(b.buffer, b.byteOffset, b.byteLength / Uint8Array.BYTES_PER_ELEMENT);
fs.writeFileSync('key.json', `[${j}]`);

Update fields privatekeyexportedfromphantom and key.json as required.

like image 181
yamen Avatar answered Oct 23 '22 01:10

yamen


It's a bit annoying, but you'll have to decode the base-58 private key returned by Phantom into an array of bytes. Here's a simple Python code snippet to accomplish this, using the base58 package (https://pypi.org/project/base58/):

import base58
byte_array = base58.b58decode(MY_PRIVATE_KEY_IN_BASE58)
json_string = "[" + ",".join(map(lambda b: str(b), byte_array)) + "]"
print(json_string)

You can pipe that output to a file, and then use that as your --keypair with the CLI tools.

like image 42
Jon C Avatar answered Oct 23 '22 01:10

Jon C