Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add message id for to-device events #1652

Merged
merged 1 commit into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions MatrixSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1939,6 +1939,8 @@
ED751DAF28EDEC7E003748C3 /* MXKeyVerificationStateResolverUnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED751DAD28EDEC7E003748C3 /* MXKeyVerificationStateResolverUnitTests.swift */; };
ED76A4AD28EDA2CE00036FF0 /* MXKeyVerificationStateResolver.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED76A4AC28EDA2CE00036FF0 /* MXKeyVerificationStateResolver.swift */; };
ED76A4AE28EDA2CE00036FF0 /* MXKeyVerificationStateResolver.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED76A4AC28EDA2CE00036FF0 /* MXKeyVerificationStateResolver.swift */; };
ED79B9852940BB45008952F6 /* MXToDevicePayloadUnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED79B9842940BB45008952F6 /* MXToDevicePayloadUnitTests.swift */; };
ED79B9862940BB45008952F6 /* MXToDevicePayloadUnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED79B9842940BB45008952F6 /* MXToDevicePayloadUnitTests.swift */; };
ED825F8F29014EDA006A614E /* MXSession+LegacyCrypto.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED825F8E29014EDA006A614E /* MXSession+LegacyCrypto.swift */; };
ED825F9029014EDA006A614E /* MXSession+LegacyCrypto.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED825F8E29014EDA006A614E /* MXSession+LegacyCrypto.swift */; };
ED88999127F2065D00718486 /* MXRoomAliasResolution.h in Headers */ = {isa = PBXBuildFile; fileRef = ED88998F27F2065C00718486 /* MXRoomAliasResolution.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -2003,6 +2005,8 @@
EDD578EA2881C37C006739DD /* MXCryptoUserIdentityWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDD578E02881C37C006739DD /* MXCryptoUserIdentityWrapper.swift */; };
EDD578EC2881C38C006739DD /* MXCrossSigningV2.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDD578EB2881C38C006739DD /* MXCrossSigningV2.swift */; };
EDD578ED2881C38C006739DD /* MXCrossSigningV2.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDD578EB2881C38C006739DD /* MXCrossSigningV2.swift */; };
EDDBA7F0293F353900AD1480 /* MXToDevicePayload.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDDBA7EF293F353900AD1480 /* MXToDevicePayload.swift */; };
EDDBA7F1293F353900AD1480 /* MXToDevicePayload.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDDBA7EF293F353900AD1480 /* MXToDevicePayload.swift */; };
EDDD90C82901611600B760E0 /* MXLegacyCrypto+LegacyCrossSigning.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDDD90C72901611600B760E0 /* MXLegacyCrypto+LegacyCrossSigning.swift */; };
EDDD90C92901611600B760E0 /* MXLegacyCrypto+LegacyCrossSigning.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDDD90C72901611600B760E0 /* MXLegacyCrypto+LegacyCrossSigning.swift */; };
EDE1B13B28B7BEAB000DEEE8 /* MXCrossSigningV2UnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDE1B13A28B7BEAB000DEEE8 /* MXCrossSigningV2UnitTests.swift */; };
Expand Down Expand Up @@ -3079,6 +3083,7 @@
ED751DA928EDE4F4003748C3 /* MXKeyVerificationManagerV2UnitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXKeyVerificationManagerV2UnitTests.swift; sourceTree = "<group>"; };
ED751DAD28EDEC7E003748C3 /* MXKeyVerificationStateResolverUnitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXKeyVerificationStateResolverUnitTests.swift; sourceTree = "<group>"; };
ED76A4AC28EDA2CE00036FF0 /* MXKeyVerificationStateResolver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXKeyVerificationStateResolver.swift; sourceTree = "<group>"; };
ED79B9842940BB45008952F6 /* MXToDevicePayloadUnitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXToDevicePayloadUnitTests.swift; sourceTree = "<group>"; };
ED825F8E29014EDA006A614E /* MXSession+LegacyCrypto.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MXSession+LegacyCrypto.swift"; sourceTree = "<group>"; };
ED88998F27F2065C00718486 /* MXRoomAliasResolution.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MXRoomAliasResolution.h; sourceTree = "<group>"; };
ED88999027F2065D00718486 /* MXRoomAliasResolution.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MXRoomAliasResolution.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3112,6 +3117,7 @@
EDD578DF2881C37C006739DD /* MXCryptoDeviceWrapper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MXCryptoDeviceWrapper.swift; sourceTree = "<group>"; };
EDD578E02881C37C006739DD /* MXCryptoUserIdentityWrapper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MXCryptoUserIdentityWrapper.swift; sourceTree = "<group>"; };
EDD578EB2881C38C006739DD /* MXCrossSigningV2.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MXCrossSigningV2.swift; sourceTree = "<group>"; };
EDDBA7EF293F353900AD1480 /* MXToDevicePayload.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXToDevicePayload.swift; sourceTree = "<group>"; };
EDDD90C72901611600B760E0 /* MXLegacyCrypto+LegacyCrossSigning.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MXLegacyCrypto+LegacyCrossSigning.swift"; sourceTree = "<group>"; };
EDE1B13A28B7BEAB000DEEE8 /* MXCrossSigningV2UnitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MXCrossSigningV2UnitTests.swift; sourceTree = "<group>"; };
EDE70DC728DA22F800099736 /* MXKeyBackupEngine.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MXKeyBackupEngine.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3379,6 +3385,7 @@
EDF1B68F2876CD2C00BBBCEE /* MXTaskQueue.swift */,
ED1AE9292881AC7100D3432A /* MXWarnings.h */,
ED6DAC2028C7A4F000ECDCB6 /* MXDateProvider.swift */,
EDDBA7EF293F353900AD1480 /* MXToDevicePayload.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -4357,6 +4364,7 @@
3A96CD482901512C00F9A5AB /* MXReceiptDataIntegrationTests.swift */,
3A2A3237291031A7005EF477 /* MXThreadsNotificationCountTests.swift */,
3A4BB661291D93EA006F7585 /* MXRoomEventFilterUnitTests.swift */,
ED79B9842940BB45008952F6 /* MXToDevicePayloadUnitTests.swift */,
);
path = MatrixSDKTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -7122,6 +7130,7 @@
B11556EE230C45C600B2A2CF /* MXIdentityServerRestClient.swift in Sources */,
EDAAC41F28E30F4C00DD89B5 /* (null) in Sources */,
321CFDE722525A49004D31DF /* MXSASTransaction.m in Sources */,
EDDBA7F0293F353900AD1480 /* MXToDevicePayload.swift in Sources */,
32720D9D222EAA6F0086FFF5 /* MXDiscoveredClientConfig.m in Sources */,
EC5C560C2798CEA00014CBE9 /* NSDictionary+MutableDeepCopy.m in Sources */,
ECD2897926E8EE9300F268CF /* MXRoomListDataSortOptions.swift in Sources */,
Expand Down Expand Up @@ -7230,6 +7239,7 @@
EDB4209927DF842F0036AF39 /* MXEventFixtures.swift in Sources */,
ECDBE69328E5E16F000C83AF /* MXClientInformationServiceUnitTests.swift in Sources */,
32114A7F1A24E15500FF2EC4 /* MXMyUserTests.m in Sources */,
ED79B9852940BB45008952F6 /* MXToDevicePayloadUnitTests.swift in Sources */,
32832B5E1BCC048300241108 /* MXStoreNoStoreTests.m in Sources */,
A816247C25F60C7700A46F05 /* MXDeviceListOperationsPoolUnitTests.swift in Sources */,
B1660F1C260A20B900C3AA12 /* MXSpaceServiceTest.swift in Sources */,
Expand Down Expand Up @@ -7765,6 +7775,7 @@
B14EF2782397E90400758AF0 /* MXTransactionCancelCode.m in Sources */,
EDAAC42028E30F4C00DD89B5 /* (null) in Sources */,
B14EF2792397E90400758AF0 /* MXEventListener.m in Sources */,
EDDBA7F1293F353900AD1480 /* MXToDevicePayload.swift in Sources */,
B1710B202613D01400A9B429 /* MXSpaceChildrenRequestParameters.swift in Sources */,
B14EF27A2397E90400758AF0 /* MXSessionEventListener.swift in Sources */,
B14EF27B2397E90400758AF0 /* MXOlmSessionResult.m in Sources */,
Expand Down Expand Up @@ -7873,6 +7884,7 @@
EDB4209A27DF842F0036AF39 /* MXEventFixtures.swift in Sources */,
ECDBE69428E5E16F000C83AF /* MXClientInformationServiceUnitTests.swift in Sources */,
B1E09A2E2397FD750057C069 /* MXRestClientTests.m in Sources */,
ED79B9862940BB45008952F6 /* MXToDevicePayloadUnitTests.swift in Sources */,
32C9B71923E81A1C00C6F30A /* MXCrossSigningVerificationTests.m in Sources */,
B1E09A1D2397FCE90057C069 /* MXCryptoKeyVerificationTests.m in Sources */,
B1E09A472397FD990057C069 /* MXEventScanStoreUnitTests.m in Sources */,
Expand Down
7 changes: 3 additions & 4 deletions MatrixSDK/Contrib/Swift/MXRestClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1833,15 +1833,14 @@ public extension MXRestClient {
Send an event to a specific list of devices

- paramaeters:
- eventType: the type of event to send
- contentMap: content to send. Map from user_id to device_id to content dictionary.
- payload: Payload with `eventType` and `contentMap` to be sent
- completion: A block object called when the operation completes.
- response: Indicates whether the operation was successful.

- returns: a `MXHTTPOperation` instance.
*/
@nonobjc @discardableResult func sendDirectToDevice(eventType: String, contentMap: MXUsersDevicesMap<NSDictionary>, txnId: String?, completion: @escaping (_ response: MXResponse<Void>) -> Void) -> MXHTTPOperation {
return __send(toDevice: eventType, contentMap: contentMap, txnId: txnId, success: currySuccess(completion), failure: curryFailure(completion))
@nonobjc @discardableResult func sendDirectToDevice(payload: MXToDevicePayload, completion: @escaping (_ response: MXResponse<Void>) -> Void) -> MXHTTPOperation {
return __send(toDevice: payload, success: currySuccess(completion), failure: curryFailure(completion))
}


Expand Down
8 changes: 4 additions & 4 deletions MatrixSDK/Crypto/Algorithms/Megolm/MXMegolmDecryption.m
Original file line number Diff line number Diff line change
Expand Up @@ -366,10 +366,10 @@ - (MXHTTPOperation *)shareKeysWitUserId:(NSString *)userId
}

MXLogDebug(@"[MXMegolmDecryption] shareKeysWithDevices: sharing keys for session %@|%@ with devices of user %@", senderKey, sessionId, userId);

MXHTTPOperation *operation2 = [self->crypto.matrixRestClient sendToDevice:kMXEventTypeStringRoomEncrypted
contentMap:contentMap
txnId:nil
MXToDevicePayload *toDevicePayload = [[MXToDevicePayload alloc] initWithEventType:kMXEventTypeStringRoomEncrypted
contentMap:contentMap];
MXHTTPOperation *operation2 = [self->crypto.matrixRestClient sendToDevice:toDevicePayload
success:success
failure:failure];
[operation mutateTo:operation2];
Expand Down
9 changes: 7 additions & 2 deletions MatrixSDK/Crypto/Algorithms/Megolm/MXMegolmEncryption.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#import "MXOutboundSessionInfo.h"
#import <OLMKit/OLMKit.h>
#import "MXSharedHistoryKeyService.h"
#import "MatrixSDKSwiftHeader.h"


@interface MXMegolmEncryption ()
Expand Down Expand Up @@ -422,7 +423,9 @@ - (MXHTTPOperation*)shareKey:(MXOutboundSessionInfo*)session
//MXLogDebug(@"[MXMegolmEncryption] shareKey. Actually share with %tu users and %tu devices: %@", contentMap.userIds.count, contentMap.count, contentMap);
MXLogDebug(@"[MXMegolmEncryption] shareKey: Actually share with %tu users and %tu devices", contentMap.userIds.count, contentMap.count);

MXHTTPOperation *operation2 = [self->crypto.matrixRestClient sendToDevice:kMXEventTypeStringRoomEncrypted contentMap:contentMap txnId:nil success:^{
MXToDevicePayload *payload = [[MXToDevicePayload alloc] initWithEventType:kMXEventTypeStringRoomEncrypted
contentMap:contentMap];
MXHTTPOperation *operation2 = [self->crypto.matrixRestClient sendToDevice:payload success:^{

MXLogDebug(@"[MXMegolmEncryption] shareKey: request succeeded");

Expand Down Expand Up @@ -541,7 +544,9 @@ - (MXHTTPOperation*)reshareKey:(NSString*)sessionId
[contentMap setObject:[self->crypto encryptMessage:payload forDevices:@[deviceInfo]]
forUser:userId andDevice:deviceId];

MXHTTPOperation *operation2 = [self->crypto.matrixRestClient sendToDevice:kMXEventTypeStringRoomEncrypted contentMap:contentMap txnId:nil success:success failure:failure];
MXToDevicePayload *toDevicePayload = [[MXToDevicePayload alloc] initWithEventType:kMXEventTypeStringRoomEncrypted
contentMap:contentMap];
MXHTTPOperation *operation2 = [self->crypto.matrixRestClient sendToDevice:toDevicePayload success:success failure:failure];
[operation mutateTo:operation2];

} failure:failure];
Expand Down
6 changes: 4 additions & 2 deletions MatrixSDK/Crypto/CryptoMachine/MXCryptoMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ extension MXCryptoMachine: MXCryptoSyncing {
switch request {
case .toDevice(let requestId, let eventType, let body):
try await requests.sendToDevice(
request: .init(eventType: eventType, body: body)
request: .init(eventType: eventType, body: body, addMessageId: true)
)
try markRequestAsSent(requestId: requestId, requestType: .toDevice)

Expand Down Expand Up @@ -595,7 +595,9 @@ extension MXCryptoMachine: MXCryptoVerificationRequesting {
try await requests.sendToDevice(
request: .init(
eventType: eventType,
body: body
body: body,
// Should not add anything for verification events as it would break their signatures
addMessageId: false
)
)
case .inRoom(_, let roomId, let eventType, let content):
Expand Down
13 changes: 9 additions & 4 deletions MatrixSDK/Crypto/CryptoMachine/MXCryptoRequests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ struct MXCryptoRequests {
func sendToDevice(request: ToDeviceRequest) async throws {
return try await performCallbackRequest {
restClient.sendDirectToDevice(
eventType: request.eventType,
contentMap: request.contentMap,
txnId: nil,
payload: .init(
eventType: request.eventType,
contentMap: request.contentMap,
transactionId: nil,
addMessageId: request.addMessageId
),
completion: $0
)
}
Expand Down Expand Up @@ -131,8 +134,9 @@ extension MXCryptoRequests {
struct ToDeviceRequest {
let eventType: String
let contentMap: MXUsersDevicesMap<NSDictionary>
let addMessageId: Bool

init(eventType: String, body: String) throws {
init(eventType: String, body: String, addMessageId: Bool) throws {
guard
let json = MXTools.deserialiseJSONString(body) as? [String: [String: NSDictionary]],
let contentMap = MXUsersDevicesMap<NSDictionary>(map: json)
Expand All @@ -142,6 +146,7 @@ extension MXCryptoRequests {

self.eventType = eventType
self.contentMap = contentMap
self.addMessageId = addMessageId
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#import "MXTools.h"
#import "MXOutgoingRoomKeyRequest.h"
#import "MatrixSDKSwiftHeader.h"

#ifdef MX_CRYPTO

Expand Down Expand Up @@ -359,7 +360,11 @@ - (void)sendMessageToDevices:(NSDictionary*)message
[contentMap setObject:message forUser:recipient[@"userId"] andDevice:recipient[@"deviceId"]];
}

[matrixRestClient sendToDevice:kMXEventTypeStringRoomKeyRequest contentMap:contentMap txnId:txnId success:success failure:failure];
MXToDevicePayload *payload = [[MXToDevicePayload alloc] initWithEventType:kMXEventTypeStringRoomKeyRequest
contentMap:contentMap
transactionId:txnId
addMessageId:YES];
[matrixRestClient sendToDevice:payload success:success failure:failure];
}

/**
Expand Down
9 changes: 7 additions & 2 deletions MatrixSDK/Crypto/KeySharing/Secret/MXSecretShareManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#import "MXPendingSecretShareRequest.h"
#import "MXSecretShareSend.h"
#import "MXTools.h"
#import "MatrixSDKSwiftHeader.h"


#pragma mark - Constants
Expand Down Expand Up @@ -210,7 +211,9 @@ - (MXHTTPOperation*)sendMessage:(NSDictionary*)message
[contentMap setObject:message forUser:myUser.userId andDevice:@"*"];
}

return [_crypto.matrixRestClient sendToDevice:kMXEventTypeStringSecretRequest contentMap:contentMap txnId:nil success:success failure:failure];
MXToDevicePayload *payload = [[MXToDevicePayload alloc] initWithEventType:kMXEventTypeStringSecretRequest
contentMap:contentMap];
return [_crypto.matrixRestClient sendToDevice:payload success:success failure:failure];
}

- (BOOL)isSecretShareEvent:(MXEventTypeString)type
Expand Down Expand Up @@ -390,7 +393,9 @@ - (void)shareSecret:(NSString*)secret toRequest:(MXSecretShareRequest*)request
MXUsersDevicesMap<NSDictionary*> *contentMap = [MXUsersDevicesMap new];
[contentMap setObject:encryptedContent forUser:myUser.userId andDevice:device.deviceId];

[self.crypto.matrixRestClient sendToDevice:kMXEventTypeStringRoomEncrypted contentMap:contentMap txnId:nil success:nil failure:^(NSError *error) {
MXToDevicePayload *payload = [[MXToDevicePayload alloc] initWithEventType:kMXEventTypeStringRoomEncrypted
contentMap:contentMap];
[self.crypto.matrixRestClient sendToDevice:payload success:nil failure:^(NSError *error) {
MXLogDebug(@"[MXSecretShareManager] shareSecret: ERROR for sendToDevice: %@", error);
}];

Expand Down
4 changes: 3 additions & 1 deletion MatrixSDK/Crypto/MXCrypto.m
Original file line number Diff line number Diff line change
Expand Up @@ -3239,7 +3239,9 @@ - (void)markOlmSessionForUnwedgingInEvent:(MXEvent*)event
MXUsersDevicesMap<NSDictionary*> *contentMap = [MXUsersDevicesMap new];
[contentMap setObject:encryptedContent forUser:sender andDevice:device.deviceId];

[self.matrixRestClient sendToDevice:kMXEventTypeStringRoomEncrypted contentMap:contentMap txnId:nil success:nil failure:^(NSError *error) {
MXToDevicePayload *payload = [[MXToDevicePayload alloc] initWithEventType:kMXEventTypeStringRoomEncrypted
contentMap:contentMap];
[self.matrixRestClient sendToDevice:payload success:nil failure:^(NSError *error) {
MXLogDebug(@"[MXCrypto] markOlmSessionForUnwedgingInEvent: ERROR for sendToDevice: %@", error);
}];

Expand Down
Loading