loadFromResponse method

Future<void> loadFromResponse(
  1. RoomKeys keys
)

Implementation

Future<void> loadFromResponse(RoomKeys keys) async {
  if (!(await isCached())) {
    return;
  }
  final privateKey =
      base64decodeUnpadded((await encryption.ssss.getCached(megolmKey))!);
  final decryption = olm.PkDecryption();
  final info = await getRoomKeysBackupInfo();
  String backupPubKey;
  try {
    backupPubKey = decryption.init_with_private_key(privateKey);

    if (info.algorithm != BackupAlgorithm.mMegolmBackupV1Curve25519AesSha2 ||
        info.authData['public_key'] != backupPubKey) {
      return;
    }
    for (final roomEntry in keys.rooms.entries) {
      final roomId = roomEntry.key;
      for (final sessionEntry in roomEntry.value.sessions.entries) {
        final sessionId = sessionEntry.key;
        final session = sessionEntry.value;
        final sessionData = session.sessionData;
        Map<String, Object?>? decrypted;
        try {
          decrypted = json.decode(
            decryption.decrypt(
              sessionData['ephemeral'] as String,
              sessionData['mac'] as String,
              sessionData['ciphertext'] as String,
            ),
          );
        } catch (e, s) {
          Logs().e('[LibOlm] Error decrypting room key', e, s);
        }
        final senderKey = decrypted?.tryGet<String>('sender_key');
        if (decrypted != null && senderKey != null) {
          decrypted['session_id'] = sessionId;
          decrypted['room_id'] = roomId;
          await setInboundGroupSession(
            roomId,
            sessionId,
            senderKey,
            decrypted,
            forwarded: true,
            senderClaimedKeys:
                decrypted.tryGetMap<String, String>('sender_claimed_keys') ??
                    <String, String>{},
            uploaded: true,
          );
        }
      }
    }
  } finally {
    decryption.free();
  }
}