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



Solution 1:[1]

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

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Jon C