validateAndStripOtherKeys method

Future<void> validateAndStripOtherKeys(
  1. String type,
  2. String secret,
  3. String keyId,
  4. Uint8List key, {
  5. bool isDefaultKey = true,
})

Implementation

Future<void> validateAndStripOtherKeys(
  String type,
  String secret,
  String keyId,
  Uint8List key, {
  bool isDefaultKey = true,
}) 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 defaultKeyId = this.defaultKeyId;
  final otherKeys = Set<String>.from(
    encryptedContent.keys.where(
      (k) => isDefaultKey || defaultKeyId == null
          ? k != keyId
          : k != keyId && k != defaultKeyId,
    ),
  );
  encryptedContent.removeWhere((k, v) => otherKeys.contains(k));
  content['encrypted'] = encryptedContent;
  // Yes, we are paranoid...
  if (await getStored(type, keyId, key) != secret) {
    throw Exception('Secrets do not match up!');
  }
  await _setAccountDataAndWaitForSync(type, content);
  if (cacheTypes.contains(type)) {
    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);
  }
}