deriveKeypair static method

Ed25519Keypair deriveKeypair(
  1. String path,
  2. String mnemonics
)

Derive Ed25519 keypair from mnemonics and path. The mnemonics must be normalized and validated against the english wordlist.

If path is none, it will default to m/44'/784'/0'/0'/0', otherwise the path must be compliant to SLIP-0010 in form m/44'/784'/{account_index}'/{change_index}'/{address_index}'.

Implementation

static Ed25519Keypair deriveKeypair(String path, String mnemonics) {

  if (!isValidHardenedPath(path)) {
    throw ArgumentError('Invalid derivation path');
  }

  final normalizeMnemonics = mnemonics
    .trim()
    .split(r"\s+")
    .map((part) => part.toLowerCase())
    .join(" ");

  if(!isValidMnemonics(mnemonics)) {
    throw ArgumentError('Invalid mnemonics');
  }

  final key = ed25519HDKey.derivePath(path, mnemonicToSeedHex(normalizeMnemonics)).key!;
  final pubkey = ed25519HDKey.getPublicKey(key, false);

  final fullPrivateKey = Uint8List(64);
  fullPrivateKey.setAll(0, key);
  fullPrivateKey.setAll(32, pubkey);

  return Ed25519Keypair(Uint8List.fromList(fullPrivateKey));
}