Wallet.derive constructor

Wallet.derive(
  1. List<String> mnemonic,
  2. NetworkInfo networkInfo,
  3. {String derivationPath = derivationPath}
)

Derives the private key from the given mnemonic using the specified networkInfo.

Implementation

factory Wallet.derive(
  List<String> mnemonic,
  NetworkInfo networkInfo, {
  String derivationPath = derivationPath,
}) {
  // Validate the mnemonic
  if (!Bip39.validateMnemonic(mnemonic)) {
    throw Exception('Invalid mnemonic');
  }

  // Convert the mnemonic to a BIP32 instance
  final seed = Bip39.mnemonicToSeed(mnemonic);
  final root = Bip32.fromSeed(seed);

  // Get the node from the derivation path
  final derivedNode = root.derivePath(derivationPath);

  // Get the curve data
  final secp256k1 = ECCurve_secp256k1();
  final point = secp256k1.G;

  // Compute the curve point associated to the private key
  final bigInt = BigInt.parse(HEX.encode(derivedNode.privateKey!), radix: 16);
  final curvePoint = point * bigInt;

  // Get the public key
  final publicKeyBytes = curvePoint!.getEncoded();

  // Get the address
  final sha256Digest = SHA256Digest().process(publicKeyBytes);
  final address = RIPEMD160Digest().process(sha256Digest);

  // Return the key bytes
  return Wallet(
    address: address,
    publicKey: publicKeyBytes,
    privateKey: derivedNode.privateKey!,
    networkInfo: networkInfo,
  );
}