cesiumSeedFromDewif method

Uint8List cesiumSeedFromDewif(
  1. String dewif,
  2. String password, {
  3. int dewifCurrencyCode = DEWIF_CURRENCY_CODE_G1,
  4. int dewifVersion = DEWIF_VERSION,
  5. bool test = false,
})

Decrypt the given Cesium DEWIF in base64 string format and associated password. Return error if DEWIF is corrupted, or the password is not the good one. If success, return seed.

Implementation

Uint8List cesiumSeedFromDewif(String dewif, String password,
    {int dewifCurrencyCode = DEWIF_CURRENCY_CODE_G1,
    int dewifVersion = DEWIF_VERSION,
    bool test = false}) {
  const int log_n = 14;

  bool isValid = verifyDewif(dewif,
      dewifCurrencyCode: dewifCurrencyCode, dewifVersion: dewifVersion);

  if (!isValid) {
    throw Exception('Error: Dewif is corrupted');
  }

  final rawDewif = HEX.encode(base64.decode(dewif));
  final nonce = rawDewif.substring(20, 44);
  final contentData = rawDewif.substring(44);

  final salt =
      sha256("dewif".codeUnits + HEX.decode(nonce) + password.codeUnits);

  final scrypt = KeyDerivator('scrypt');
  scrypt.init(
    ScryptParameters(
      pow(2, log_n).toInt(), //16384
      16,
      1,
      42,
      salt.toUint8List(),
    ),
  );
  final scryptedPassword = scrypt.process(password.codeUnits.toUint8List());

  final decryptedData = HEX.encode(
      byteXor(HEX.decode(contentData).toUint8List(), scryptedPassword));

  final decryptedSeedLength = decryptedData.substring(0, 2);
  final seed = decryptedData.substring(2, 66);
  final decryptedCheksum = decryptedData.substring(82);

  final builtChecksum = HEX.encode(sha256(HEX.decode(nonce) +
          HEX.decode(decryptedSeedLength) +
          HEX.decode(seed))
      .sublist(0, 8));

  // print(decryptedSeedLength);
  // print(seed);
  // print("$decryptedCheksum != $builtChecksum");

  if (decryptedCheksum != builtChecksum) {
    throw ChecksumException('Error: bad checksum');
  } else {
    return Uint8List.fromList(HEX.decode(seed));
  }
}