decode function

BigInt decode(
  1. List<String> words, {
  2. List<String>? wordlist,
  3. EncodingConfig config = RecoveryConstants.defaultConfig,
})

Implementation

BigInt decode(
  List<String> words, {
  List<String>? wordlist,
  EncodingConfig config = RecoveryConstants.defaultConfig,
}) {
  if (words.isEmpty) {
    throw ArgumentError('Must provide at least one word');
  }

  final effectiveWordlist = wordlist ?? wordlistEn;

  List<String> dataWords = words;

  if (config.useChecksum) {
    if (words.length < 2) {
      throw ArgumentError('Need at least 2 words when using checksum');
    }

    dataWords = words.sublist(0, words.length - 1);
    final indices = dataWords.map((w) {
      final idx = effectiveWordlist.indexOf(w);
      if (idx == -1) throw ArgumentError('Invalid word: $w');
      return idx;
    }).toList();

    final expectedChecksum = computeChecksum(indices, effectiveWordlist.length);
    final actualChecksum = effectiveWordlist.indexOf(words.last);

    if (actualChecksum == -1) {
      throw ArgumentError('Invalid checksum word: ${words.last}');
    }

    if (expectedChecksum != actualChecksum) {
      throw ArgumentError(
        'Checksum mismatch: expected ${effectiveWordlist[expectedChecksum]}, '
        'got ${words.last}',
      );
    }
  }

  return decodeFromWords(dataWords, effectiveWordlist, config);
}