I see wallets like phantom and solflare pop up with a list of accounts when you import a seed phrase. How does that work? How can you find all of those through code? Can you generate new ones? Also if I just generated a new keypair. How could I use that seed as the base for the rest of the wallets I make?
1 Answer
This is due to hierarchical derivation, which essentially defines many different ways of generating a private key from a seed phrase.
For example, given a seed phrase like immense soup feed arrest erupt arrow little nephew regular trap radio danger
, which solana-keygen
gives a pubkey of jtFuWc4r1Y6UNuU13LngguXuyozapVnRVfnv9VwqWKr
, you can do:
solana-keygen pubkey prompt://?key=0/1
<Paste the words here>
EqqwJANhbtzB2bwFv7N4wEQA8fj3hxAJsoq7iE9szEN8
Or
solana-keygen pubkey prompt://?full-path=m/44/2017/0/1
<Paste the words here>
97oZZGsDN5nobMDsk4XDhPHjExxqAS5ehfFRUAyBNHEF
Depending on your derivation path, you'll different private keys.
More information in the Solana docs at https://docs.solana.com/wallet-guide/paper-wallet#hierarchical-derivation
To do this in code, you can use the ed25519_dalek_bip32
crate, and do something like:
let seed = "blouse clerk fault regular grocery chat figure squeeze question catalog athlete predict";
let derivation_path = "44'/501'";
let extended = ed25519_dalek_bip32::ExtendedSecretKey::from_seed(seed)
.and_then(|extended| extended.derive(&derivation_path))?;
let extended_public_key = extended.public_key();
ed25519_dalek::Keypair {
secret: extended.secret_key,
public: extended_public_key,
}
You can find the whole source code for solana-keygen
at You can see all of the solana-keygen
code at https://github.com/solana-labs/solana/blob/master/keygen/src/keygen.rs