generateUploadKeysImplementation function
you would likely want to use NativeImplementations and Client.nativeImplementations instead
Implementation
RoomKeys generateUploadKeysImplementation(GenerateUploadKeysArgs args) {
try {
final enc = vod.PkEncryption.fromPublicKey(
vod.Curve25519PublicKey.fromBase64(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!.exportAtFirstKnownIndex(),
};
// 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
final (ciphertext, mac, ephemeral) = encrypted.toBase64();
roomKeyBackup.sessions[sess.sessionId] = KeyBackupData(
firstMessageIndex: sess.inboundGroupSession!.firstKnownIndex,
forwardedCount: sess.forwardingCurve25519KeyChain.length,
isVerified: dbSession.verified, //device?.verified ?? false,
sessionData: {
'ephemeral': ephemeral,
'ciphertext': ciphertext,
'mac': mac,
},
);
}
return roomKeys;
} catch (e, s) {
Logs().e('[Key Manager] Error generating payload', e, s);
rethrow;
}
}