decode method

  1. @override
List<int> decode(
  1. String mnemonic
)
override

Decodes a Monero mnemonic string into a List

This method takes a Monero mnemonic string as input and processes it to obtain the corresponding entropy bytes. It validates the mnemonic's word count and detects the language if it was not specified during construction. Then, it converts the mnemonic words into entropy bytes by considering 3 words at a time, where 3 words represent 4 bytes.

Throws a StateError if the mnemonic words count is not valid.

Returns a List

mnemonic: The Monero mnemonic string to decode.

Implementation

@override
List<int> decode(String mnemonic) {
  final mn = MoneroMnemonic.fromString(mnemonic);
  final wcount = mn.wordsCount();
  try {
    MoneroMnemonicConst.mnemonicWordNum
        .where((element) => element.value == wcount);
  } on StateError {
    throw ArgumentException("Mnemonic words count is not valid ($wcount)");
  }
  final lang = findLanguage(mn);
  final words = mn.toList();
  validateCheckSum(words, lang.item2 as MoneroLanguages);
  List<int> entropyBytes = List.empty();
  for (int i = 0; i < words.length ~/ 3; i++) {
    String word1 = words[i * 3];
    String word2 = words[i * 3 + 1];
    String word3 = words[i * 3 + 2];
    List<int> chunkBytes = MnemonicUtils.wordsToBytesChunk(
        word1, word2, word3, lang.item1,
        endian: Endian.little);
    entropyBytes = List<int>.from([...entropyBytes, ...chunkBytes]);
  }
  return entropyBytes;
}