validateAndStripOtherKeys method

Future<void> validateAndStripOtherKeys(
  1. String type,
  2. String secret,
  3. String keyId,
  4. Uint8List key,
)

Implementation

Future<void> validateAndStripOtherKeys(
    String type, String secret, String keyId, Uint8List key) async {
  if (await getStored(type, keyId, key) != secret) {
    throw Exception('Secrets do not match up!');
  }
  // now remove all other keys
  final content = client.accountData[type]?.content.copy();
  if (content == null) {
    throw InvalidPassphraseException('Key has no content!');
  }
  final encryptedContent = content.tryGetMap<String, Object?>('encrypted');
  if (encryptedContent == null) {
    throw Exception('Wrong type for encrypted content!');
  }

  final otherKeys =
      Set<String>.from(encryptedContent.keys.where((k) => k != keyId));
  encryptedContent.removeWhere((k, v) => otherKeys.contains(k));
  // yes, we are paranoid...
  if (await getStored(type, keyId, key) != secret) {
    throw Exception('Secrets do not match up!');
  }
  // store the thing in your account data
  await client.setAccountData(client.userID!, type, content);
  if (cacheTypes.contains(type)) {
    // cache the thing
    final ciphertext = encryptedContent
        .tryGetMap<String, Object?>(keyId)
        ?.tryGet<String>('ciphertext');
    if (ciphertext == null) {
      throw Exception('Wrong type for ciphertext!');
    }
    await client.database?.storeSSSSCache(type, keyId, ciphertext, secret);
    onSecretStored.add(keyId);
  }
}