getStored method

Future<String> getStored(
  1. String type,
  2. String keyId,
  3. Uint8List key
)

Implementation

Future<String> getStored(String type, String keyId, Uint8List key) async {
  final secretInfo = client.accountData[type];
  if (secretInfo == null) {
    throw Exception('Not found');
  }
  final encryptedContent =
      secretInfo.content.tryGetMap<String, Object?>('encrypted');
  if (encryptedContent == null) {
    throw Exception('Content is not encrypted');
  }
  final enc = encryptedContent.tryGetMap<String, Object?>(keyId);
  if (enc == null) {
    throw Exception('Wrong / unknown key');
  }
  final ciphertext = enc.tryGet<String>('ciphertext');
  final iv = enc.tryGet<String>('iv');
  final mac = enc.tryGet<String>('mac');
  if (ciphertext == null || iv == null || mac == null) {
    throw Exception('Wrong types for encrypted content or missing keys.');
  }
  final encryptInfo = EncryptedContent(
    iv: iv,
    ciphertext: ciphertext,
    mac: mac,
  );
  final decrypted = await decryptAes(encryptInfo, key, type);
  final db = client.database;
  if (cacheTypes.contains(type) && db != null) {
    // cache the thing
    await db.storeSSSSCache(type, keyId, ciphertext, decrypted);
    onSecretStored.add(keyId);
    if (_cacheCallbacks.containsKey(type) && await getCached(type) == null) {
      _cacheCallbacks[type]!(decrypted);
    }
  }
  return decrypted;
}