entropyToMnemonic static method

String entropyToMnemonic(
  1. String entropyString
)

Implementation

static String entropyToMnemonic(String entropyString) {
  final entropy = Uint8List.fromList(hex.decode(entropyString));

  if (entropy.length < 16) {
    throw ArgumentError('Invalid entropy');
  }

  if (entropy.length > 32) {
    throw ArgumentError('Invalid entropy');
  }

  if (entropy.length % 4 != 0) {
    throw ArgumentError('Invalid entropy');
  }

  final entropyBits = _bytesToBinary(entropy);
  final checksumBits = _deriveChecksumBits(entropy);
  final bits = entropyBits + checksumBits;
  final regex = RegExp('.{1,11}', caseSensitive: false, multiLine: false);

  final chunks = regex
      .allMatches(bits)
      .map((match) => match.group(0)!)
      .toList(growable: false);

  final words = chunks
      .map((binary) => bip39_english_wordlist[_binaryToByte(binary)])
      .join(' ');

  return words;
}