encode method

  1. @override
Bip39Mnemonic encode(
  1. List<int> entropyBytes
)
override

Encode the provided entropy bytes into a BIP39 mnemonic phrase.

Parameters:

  • entropyBytes: The entropy bytes to encode into a mnemonic phrase.

Returns: A BIP39 mnemonic phrase representing the given entropy.

Implementation

@override
Bip39Mnemonic encode(List<int> entropyBytes) {
  entropyBytes = entropyBytes.asImmutableBytes;
  final entropyByteLen = entropyBytes.length;
  if (!Bip39EntropyGenerator.isValidEntropyByteLen(entropyByteLen)) {
    throw ArgumentException(
        'Entropy byte length ($entropyByteLen) is not valid');
  }

  final entropyBinStr =
      BytesUtils.toBinary(entropyBytes, zeroPadBitLen: entropyByteLen * 8);
  final entropyHash = QuickCrypto.sha256Hash(entropyBytes);
  final entropyHashBinStr = BytesUtils.toBinary(entropyHash,
      zeroPadBitLen: QuickCrypto.sha256DigestSize * 8);
  final mnemonicBinStr =
      entropyBinStr + entropyHashBinStr.substring(0, entropyByteLen ~/ 4);
  final mnemonic = <String>[];
  for (var i = 0;
      i < mnemonicBinStr.length;
      i += Bip39MnemonicConst.wordBitLen) {
    final wordBinStr =
        mnemonicBinStr.substring(i, i + Bip39MnemonicConst.wordBitLen);

    final wordIdx = int.parse(wordBinStr, radix: 2);
    mnemonic.add(wordsList.getWordAtIdx(wordIdx));
  }

  return Bip39Mnemonic.fromList(mnemonic);
}