deriveKeypair static method

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

Derive an Ed25519 keypair from mnemonics and a SLIP-0010 hardened path (m/44'/784'/{account}'/{change}'/{address}').

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 = ed25519_hd_key_lib
      .derivePath(path, mnemonicToSeedHex(normalizeMnemonics))
      .key!;
  final pubkey = ed25519_hd_key_lib.getPublicKey(key, false);

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

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