decode method
Decodes an Electrum V2 mnemonic string into entropy bytes.
-mnemonic: The Electrum V2 mnemonic to decode into entropy bytes.
Implementation
@override
List<int> decode(String mnemonic) {
/// Parse the mnemonic string
final mnemonicObj = ElectrumV2Mnemonic.fromString(mnemonic);
final wCount = mnemonicObj.wordsCount();
ElectrumV2MnemonicConst.mnemonicWordNum.firstWhere(
(element) => element.value == wCount,
orElse:
() =>
throw ArgumentException.invalidOperationArguments(
"decode",
name: "mnemonic",
reason: "Invalid mnemonic length.",
),
);
/// Check the validity of the mnemonic for the specified mnemonic type
if (!ElectrumV2MnemonicUtils.isValidMnemonic(mnemonicObj, mnemonicType)) {
throw ArgumentException.invalidOperationArguments(
"decode",
name: "mnemonic",
reason: "Invalid mnemonic.",
);
}
/// Get the list of words from the mnemonic
final words = mnemonicObj.toList();
/// Detect the language if it was not specified during construction
final wordsList = findLanguage(mnemonicObj).$1;
/// Decode the words into entropy as a BigInt
final n = BigInt.from(wordsList.length());
BigInt entropyInt = BigInt.zero;
for (final word in words.reversed) {
entropyInt = (entropyInt * n) + BigInt.from(wordsList.getWordIdx(word));
}
/// Convert the BigInt to bytes and return the result
return BigintUtils.toBytes(entropyInt);
}