mnemonicToEntropy function
Implementation
Uint8List mnemonicToEntropy(List<String> mnemonic) {
if (mnemonic.length % 3 != 0) {
throw ArgumentError(_invalidMnemonic);
}
final wordlist = english;
// convert word indices to 11 bit binary strings
final bits = mnemonic.map((word) {
final index = wordlist.indexOf(word);
if (index == -1) {
throw ArgumentError(_invalidMnemonic);
}
return index.toRadixString(2).padLeft(11, '0');
}).join('');
// split the binary string into ENT/CS
final dividerIndex = (bits.length / 33).floor() * 32;
final entropyBits = bits.substring(0, dividerIndex);
final checksumBits = bits.substring(dividerIndex);
// calculate the checksum and compare
final regex = RegExp(r'.{1,8}');
final entropyBytes = Uint8List.fromList(regex
.allMatches(entropyBits)
.map((match) => _binaryToByte(match.group(0)!))
.toList(growable: false));
if (entropyBytes.length < 16) {
throw StateError(_invalidEntropy);
}
if (entropyBytes.length > 32) {
throw StateError(_invalidEntropy);
}
if (entropyBytes.length % 4 != 0) {
throw StateError(_invalidEntropy);
}
final newChecksum = _deriveChecksumBits(entropyBytes);
if (newChecksum != checksumBits) {
throw StateError(_invalidChecksum);
}
return entropyBytes;
}