Search code examples
dartencryptionethereum

How to generate new private keys from a mnemonic in Dart?


I am having trouble understanding how to generate new private keys (for example in case a new account is created) from the same mnemonic. The current function returns the same private key.

Future<String> getPrivateKey(String mnemonic) async {

  final seed = bip39.mnemonicToSeed(mnemonic);
  final master = await ED25519_HD_KEY.getMasterKeyFromSeed(seed);
  final privateKey = HEX.encode(master.key);
  return privateKey; 
}

Thhank you in advance!


Solution

  • For anyone wondering the same I have found the solution. If you take the seed phrase and then you feed it a derivation path it'll generate the same private/public key every time. So to derive more wallets you have to change the path: original Ethereum derivation path is m/44'/60'/0'/0/0 and you can get a new wallet by using the path m/44'/60'/0'/1/0, m/44'/60'/0'/2/0 etc. Each number represents a different category but usually you change the last two numbers.