decrypt function

Future<String> decrypt(
  1. Map<String, dynamic> keyStore, {
  2. String? password,
})

Implementation

Future<String> decrypt(
  Map<String, dynamic> keyStore, {
  String? password,
}) async {
  final Uint8List ciphertext =
      (keyStore['crypto']['ciphertext'] as String).toU8a();
  final String kdf = keyStore['crypto']['kdf'];
  final Map<String, dynamic> kdfParams =
      keyStore['crypto']['kdfparams'] is String
          ? json.decode(keyStore['crypto']['kdfparams'])
          : keyStore['crypto']['kdfparams'];
  final cipherParams = keyStore['crypto']['cipherparams'];
  final Uint8List iv = (cipherParams['iv'] as String).toU8a();
  final deriveKeyResult = await nativeDeriveKey(
    kdf: kdf,
    iv: iv,
    message: null,
    useCipherText: ciphertext,
    kdfParams: kdfParams,
    passphrase: password,
    salt: (kdfParams['salt'] as String).replaceAll('0x', ''),
  );
  final String macString = keyStore['crypto']['mac'];
  if (!const ListEquality().equals(
    deriveKeyResult.mac.toUpperCase().codeUnits,
    macString.toUpperCase().codeUnits,
  )) {
    throw StateError('Decryption failed.');
  }

  final encryptedPrivateKey =
      (keyStore['crypto']['ciphertext'] as String).toU8a();
  return (await _decryptPhraseAsync(
    cipherText: encryptedPrivateKey,
    key: deriveKeyResult.leftBits,
    iv: iv,
  ))
      .u8aToString();
}