generateUploadKeysImplementation function

RoomKeys generateUploadKeysImplementation(
  1. GenerateUploadKeysArgs args
)

you would likely want to use NativeImplementations and Client.nativeImplementations instead

Implementation

RoomKeys generateUploadKeysImplementation(GenerateUploadKeysArgs args) {
  final enc = olm.PkEncryption();
  try {
    enc.set_recipient_key(args.pubkey);
    // first we generate the payload to upload all the session keys in this chunk
    final roomKeys = RoomKeys(rooms: {});
    for (final dbSession in args.dbSessions) {
      final sess = SessionKey.fromDb(dbSession.dbSession, args.userId);
      if (!sess.isValid) {
        continue;
      }
      // create the room if it doesn't exist
      final roomKeyBackup =
          roomKeys.rooms[sess.roomId] ??= RoomKeyBackup(sessions: {});
      // generate the encrypted content
      final payload = <String, dynamic>{
        'algorithm': AlgorithmTypes.megolmV1AesSha2,
        'forwarding_curve25519_key_chain': sess.forwardingCurve25519KeyChain,
        'sender_key': sess.senderKey,
        'sender_claimed_keys': sess.senderClaimedKeys,
        'session_key': sess.inboundGroupSession!
            .export_session(sess.inboundGroupSession!.first_known_index()),
      };
      // encrypt the content
      final encrypted = enc.encrypt(json.encode(payload));
      // fetch the device, if available...
      //final device = args.client.getUserDeviceKeysByCurve25519Key(sess.senderKey);
      // aaaand finally add the session key to our payload
      roomKeyBackup.sessions[sess.sessionId] = KeyBackupData(
        firstMessageIndex: sess.inboundGroupSession!.first_known_index(),
        forwardedCount: sess.forwardingCurve25519KeyChain.length,
        isVerified: dbSession.verified, //device?.verified ?? false,
        sessionData: {
          'ephemeral': encrypted.ephemeral,
          'ciphertext': encrypted.ciphertext,
          'mac': encrypted.mac,
        },
      );
    }
    enc.free();
    return roomKeys;
  } catch (e, s) {
    Logs().e('[Key Manager] Error generating payload', e, s);
    enc.free();
    rethrow;
  }
}