seedWordsToSeed function

Uint8List seedWordsToSeed(
  1. Uint16List seedWords
)

Implementation

Uint8List seedWordsToSeed(Uint16List seedWords) {
  if (seedWords.length != SEED_WORDS_LENGTH) {
    throw 'Input seed was not of length $SEED_WORDS_LENGTH';
  }

  // We are getting 16 bytes of entropy.
  final bytes = Uint8List(SEED_LENGTH);
  var curByte = 0;
  var curBit = 0;

  for (var i = 0; i < SEED_WORDS_LENGTH; i++) {
    final word = seedWords[i];
    var wordBits = 10;
    if (i == 0) {
      wordBits = 8;
    }

    // Iterate over the bits of the 10- or 8-bit word.
    for (var j = 0; j < wordBits; j++) {
      final bitSet = (word & (1 << (wordBits - j - 1))) > 0;

      if (bitSet) {
        bytes[curByte] |= 1 << (8 - curBit - 1);
      }

      curBit += 1;
      if (curBit >= 8) {
        curByte += 1;
        curBit = 0;
      }
    }
  }

  return bytes;
}