Search code examples
solanapkikey-pair

How do I create an HD wallet and child wallets in Solana?


I know how to make a wallet in Solana:

const keypair = solanaWeb3.Keypair.fromSeed(seed);

Similar to 'what is a HD wallet for Ether and how to create one using nodejs?' I'd like to create an HD wallet using Solana.

Specifically, I have created a keypair and would like to create a child keypair. I can't find any references to creating HD wallets in the Solana Cookbook.

But how do I make child wallets? Here's what I'd do in Ethereum:

var mnemonic = "seed sock milk ...";
var path = "m/44'/60'/0'/0/0";

var hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(mnemonic));
var wallet = hdwallet.derivePath(path).getWallet();

How do I create an HD wallet and child wallets in Solana?


Solution

  • You can use the BIP-44 derivePath helper for this purpose. From the Solana cookbook:

    
    import { derivePath } from 'ed25519-hd-key';
    
    const mnemonic =
      "neither lonely flavor argue grass remind eye tag avocado spot unusual intact";
    const seed = bip39.mnemonicToSeedSync(mnemonic, ""); // (mnemonic, password)
    for (let i = 0; i < 10; i++) {
      const path = `m/44'/501'/${i}'/0'`;
      const keypair = Keypair.fromSeed(derivePath(path, seed.toString("hex")).key);
      console.log(`${path} => ${keypair.publicKey.toBase58()}`);
    }