encode method
Overrides the encode method to convert entropy bytes into a BIP-39 mnemonic phrase.
Implementation
@override
Bip39Mnemonic encode(List<int> entropyBytes) {
/// Validates the length of the entropy bytes.
final entropyByteLen = entropyBytes.length;
if (!TonMnemonicEntropyGenerator.isValidEntropyByteLen(entropyByteLen)) {
throw MnemonicException(
'Entropy byte length ($entropyByteLen) is not valid');
}
/// Converts entropy bytes to a binary string with zero padding.
final entropyBinStr =
BytesUtils.toBinary(entropyBytes, zeroPadBitLen: entropyByteLen * 8);
/// Converts the binary string to a list of mnemonic words.
final List<String> mnemonic = [];
for (int i = 0;
i < entropyBinStr.length;
i += Bip39MnemonicConst.wordBitLen) {
if (i + Bip39MnemonicConst.wordBitLen > entropyBinStr.length) break;
final wordBinStr =
entropyBinStr.substring(i, i + Bip39MnemonicConst.wordBitLen);
final wordIdx = int.parse(wordBinStr, radix: 2);
mnemonic.add(wordsList.getWordAtIdx(wordIdx));
}
/// Returns a BIP-39 mnemonic phrase constructed from the list of words.
return Bip39Mnemonic.fromList(mnemonic);
}