BIP32HWallet.fromMnemonic constructor
Factory constructor to create a BIP32 hierarchical wallet from a mnemonic.
mnemonic
is the BIP-39 mnemonic phrase.
passphrase
is an optional passphrase to be used with the mnemonic (default is an empty string).
key
is the key used for hashing (default is the Bitcoin seed key).
Implementation
factory BIP32HWallet.fromMnemonic(String mnemonic,
{String passphrase = "", String key = _bitcoinKey}) {
/// Generate a seed from the mnemonic and passphrase using BIP-39.
final seed = BIP39.toSeed(mnemonic, passphrase: passphrase);
/// Validate the length of the generated seed.
if (seed.length < 16) {
throw ArgumentError("Seed should be at least 128 bits");
}
if (seed.length > 64) {
throw ArgumentError("Seed should be at most 512 bits");
}
/// Compute the HMAC-SHA512 hash of the seed using the provided key.
final hash = hmacSHA512(utf8.encode(key) as Uint8List, seed);
/// Extract the private key (first 32 bytes) and chain code (remaining bytes).
final private = hash.sublist(0, 32);
final chainCode = hash.sublist(32);
/// Create and return a BIP32 hierarchical wallet from the private key and chain code.
final wallet =
BIP32HWallet._fromPrivateKey(privateKey: private, chainCode: chainCode);
return wallet;
}