mnemonicFromDewif method

String mnemonicFromDewif(
  1. String dewif,
  2. String password, {
  3. String lang = 'english',
  4. int dewifCurrencyCode = DEWIF_CURRENCY_CODE_G1,
  5. int dewifVersion = DEWIF_VERSION,
  6. bool testRfc = false,
})

Decrypt the given 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 mnemonic.

Implementation

String mnemonicFromDewif(String dewif, String password,
    {String lang = 'english',
    int dewifCurrencyCode = DEWIF_CURRENCY_CODE_G1,
    int dewifVersion = DEWIF_VERSION,
    bool testRfc = 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 decryptedLang = decryptedData.substring(0, 2);
  final decryptedEntropyLength = decryptedData.substring(2, 4);
  final mnemonicEntropy = decryptedData.substring(4, 36);
  final decryptedCheksum = decryptedData.substring(68);

  final builtChecksum = HEX.encode(sha256(HEX.decode(nonce) +
          HEX.decode(decryptedLang) +
          HEX.decode(decryptedEntropyLength) +
          HEX.decode(mnemonicEntropy))
      .sublist(0, 8));

  if (decryptedCheksum != builtChecksum) {
    throw ChecksumException('Error: bad checksum');
  } else {
    return bip39.entropyToMnemonic(mnemonicEntropy, language: lang);
  }
}