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?
Phantom is a self-custodial wallet. Your private keys are encrypted on your device by your password and are never shared with anyone.
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.
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:
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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With