entropyToMnemonic method

String entropyToMnemonic(
  1. String entropyString
)

Converts binary entropy data to a BIP-39 mnemonic phrase.

This method takes binary entropy data, typically generated from a cryptographic source, and converts it into a BIP-39 mnemonic phrase. The mnemonic phrase is a human-readable representation of the binary entropy, providing a more user-friendly way to manage cryptographic secrets.

Parameters:

  • entropyString: Binary entropy data as a hexadecimal string.

Returns: A BIP-39 mnemonic phrase.

Throws:

Implementation

String entropyToMnemonic(String entropyString) {
  final entropy = Uint8List.fromList(hexToBytes(entropyString));

  /// Validate the entropy length and format.
  if (entropy.length < 16 || entropy.length > 32 || entropy.length % 4 != 0) {
    throw ArgumentError("Invalid entropy");
  }

  final entropyBits = bytesToBinary(entropy);
  final checksumBits = _deriveChecksumBits(entropy);
  final bits = entropyBits + checksumBits;

  /// Split bits into groups of 11 and map to corresponding BIP-39 words.
  final regex = RegExp(r".{1,11}", caseSensitive: false, multiLine: false);
  final chunks = regex
      .allMatches(bits)
      .map((match) => match.group(0)!)
      .toList(growable: false);

  final words = chunks.map((binary) {
    int index = binaryToByte(binary);
    return language.words[index];
  }).join(' ');

  return words;
}