From dd18e65e3ea4fa66149165948db35bc8cb8b814d Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 6 May 2019 14:23:44 +1000 Subject: [PATCH] Fix minor code style issues --- SignalServiceKit/src/Loki/ProofOfWork.swift | 11 ++-- .../src/Messages/OWSMessageSender.m | 56 ++++++++++--------- .../Network/API/Requests/OWSRequestFactory.h | 2 +- .../Network/API/Requests/OWSRequestFactory.m | 16 +++--- 4 files changed, 43 insertions(+), 42 deletions(-) diff --git a/SignalServiceKit/src/Loki/ProofOfWork.swift b/SignalServiceKit/src/Loki/ProofOfWork.swift index 245de1daf..55b0838dc 100644 --- a/SignalServiceKit/src/Loki/ProofOfWork.swift +++ b/SignalServiceKit/src/Loki/ProofOfWork.swift @@ -43,8 +43,7 @@ private extension MutableCollection where Element == UInt8, Index == Int { * This was copied from the desktop messenger. * Ref: libloki/proof-of-work.js */ -@objc public class ProofOfWork: NSObject { - private override init() {} +@objc public class ProofOfWork : NSObject { // If this changes then we also have to use something other than UInt64 to support the new length private static let nonceLength = 8 @@ -55,6 +54,8 @@ private extension MutableCollection where Element == UInt8, Index == Int { case .production: return 100 } }() + + private override init() { } /// Calculate a proof of work with the given configuration /// @@ -66,7 +67,7 @@ private extension MutableCollection where Element == UInt8, Index == Int { /// - timestamp: The timestamp /// - ttl: The message time to live /// - Returns: A nonce string or nil if it failed - @objc public class func calculate(forData data: String, pubKey: String, timestamp: UInt64, ttl: Int) -> String? { + @objc public static func calculate(data: String, pubKey: String, timestamp: UInt64, ttl: Int) -> String? { let payload = getPayload(pubKey: pubKey, data: data, timestamp: timestamp, ttl: ttl) let target = calcTarget(ttl: ttl, payloadLength: payload.count, nonceTrials: nonceTrialCount) @@ -90,7 +91,7 @@ private extension MutableCollection where Element == UInt8, Index == Int { } /// Get the proof of work payload - private class func getPayload(pubKey: String, data: String, timestamp: UInt64, ttl: Int) -> [UInt8] { + private static func getPayload(pubKey: String, data: String, timestamp: UInt64, ttl: Int) -> [UInt8] { let timestampString = String(timestamp) let ttlString = String(ttl) let payloadString = timestampString + ttlString + pubKey + data @@ -98,7 +99,7 @@ private extension MutableCollection where Element == UInt8, Index == Int { } /// Calculate the target we need to reach - private class func calcTarget(ttl: Int, payloadLength: Int, nonceTrials: Int) -> UInt64 { + private static func calcTarget(ttl: Int, payloadLength: Int, nonceTrials: Int) -> UInt64 { let two16 = UInt64(pow(2, 16) - 1) let two64 = UInt64(pow(2, 64) - 1) diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index 0697213f2..89070d10b 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -918,27 +918,27 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; - (AnyPromise *)calculateProofOfWorkForDeviceMessages:(NSArray *)deviceMessages ttl:(NSNumber *)ttl { - // LOKI: Calculate the proof of work for each device message - NSMutableArray *promises = [[NSMutableArray alloc] init]; + // Loki: Calculate the proof of work for each device message + NSMutableArray *promises = [NSMutableArray new]; for (NSDictionary *deviceMessage in deviceMessages) { AnyPromise *promise = [AnyPromise promiseWithValue:deviceMessage] .thenOn(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(NSDictionary *message) { - NSTimeInterval timestampInterval = [[NSDate date] timeIntervalSince1970]; + NSTimeInterval timestampInterval = [[NSDate new] timeIntervalSince1970]; NSNumber *timestamp = [NSNumber numberWithDouble:timestampInterval]; NSString *destination = message[@"destination"]; NSString *data = message[@"content"]; - - NSString *_Nullable nonce = [ProofOfWork calculateForData:data pubKey:destination timestamp:timestamp.unsignedIntegerValue ttl:ttl.integerValue]; + + NSString *_Nullable nonce = [ProofOfWork calculateWithData:data pubKey:destination timestamp:timestamp.unsignedIntegerValue ttl:ttl.integerValue]; // Return our timestamp along with the nonce // These will help us identify which nonce belongs to which message return @{ - @"destination": destination, - @"deviceId": message[@"destinationDeviceId"], - @"timestamp": timestamp, - @"nonce": nonce - }; + @"destination" : destination, + @"deviceId" : message[@"destinationDeviceId"], + @"timestamp" : timestamp, + @"nonce" : nonce + }; }); [promises addObject:promise]; } @@ -1122,38 +1122,40 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; // TODO: Update message here to show the pow cog icon - // LOKI: Calculate the proof of work for each device message + // Loki: Calculate the proof of work for each device message NSNumber *ttl = [NSNumber numberWithInteger:@(4 * 24 * 60 * 60)]; - AnyPromise *PoWPromise = [self calculateProofOfWorkForDeviceMessages:deviceMessages ttl:ttl]; - [PoWPromise + AnyPromise *powPromise = [self calculateProofOfWorkForDeviceMessages:deviceMessages ttl:ttl]; + [powPromise .thenOn([OWSDispatch sendingQueue], ^(NSArray *nonceArray) { OWSRequestMaker *requestMaker = [[OWSRequestMaker alloc] initWithLabel:@"Message Send" - requestFactoryBlock:^(SMKUDAccessKey *_Nullable udAccessKey) { - // Loki Changes: - return [OWSRequestFactory submitLokiMessageRequestWithRecipient:recipient.recipientId + requestFactoryBlock:^(SMKUDAccessKey *_Nullable udAccessKey) { + // Loki + // ======== + return [OWSRequestFactory submitLokiMessageRequestWithRecipient:recipient.recipientId messages:deviceMessages nonceArray:nonceArray ttl:ttl]; - /* Original Code: - return [OWSRequestFactory submitMessageRequestWithRecipient:recipient.recipientId + // ======== + /* Original code: + return [OWSRequestFactory submitMessageRequestWithRecipient:recipient.recipientId messages:deviceMessages timeStamp:message.timestamp udAccessKey:udAccessKey]; */ - } + } udAuthFailureBlock:^{ // Note the UD auth failure so subsequent retries // to this recipient also use basic auth. [messageSend setHasUDAuthFailed]; } - websocketFailureBlock:^{ - // Note the websocket failure so subsequent retries - // to this recipient also use REST. - messageSend.hasWebsocketSendFailed = YES; - } - recipientId:recipient.recipientId - udAccess:messageSend.udAccess - canFailoverUDAuth:NO]; + websocketFailureBlock:^{ + // Note the websocket failure so subsequent retries + // to this recipient also use REST. + messageSend.hasWebsocketSendFailed = YES; + } + recipientId:recipient.recipientId + udAccess:messageSend.udAccess + canFailoverUDAuth:NO]; return requestMaker; }) .thenOn([OWSDispatch sendingQueue], ^(OWSRequestMaker *requestMaker) { diff --git a/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.h b/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.h index 71eeb07f3..3056b30ad 100644 --- a/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.h +++ b/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.h @@ -62,7 +62,7 @@ typedef NS_ENUM(NSUInteger, TSVerificationTransport) { TSVerificationTransportVo + (TSRequest *)submitLokiMessageRequestWithRecipient:(NSString *)recipientId messages:(NSArray *)messages nonceArray:(NSArray *)nonceArray - ttl: (NSNumber *)ttl; + ttl:(NSNumber *)ttl; + (TSRequest *)submitMessageRequestWithRecipient:(NSString *)recipientId messages:(NSArray *)messages diff --git a/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.m b/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.m index e0fc4ad5f..427dc75a3 100644 --- a/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.m +++ b/SignalServiceKit/src/Network/API/Requests/OWSRequestFactory.m @@ -354,14 +354,14 @@ NS_ASSUME_NONNULL_BEGIN return [accountAttributes copy]; } -// LOKI: Convert Signal JSON messages to Loki messages +// Loki: Convert Signal JSON messages to Loki messages // Refer to OWSMessageServiceParams for the Signal JSON params + (NSDictionary *)lokiMessagesFromMessages:(NSArray *)messages nonceArray:(NSArray *)nonceArray ttl:(NSNumber *)ttl { - NSMutableArray *modifiedMessages = [[NSMutableArray alloc] init]; + NSMutableArray *modifiedMessages = [NSMutableArray new]; for (NSDictionary *message in messages) { - NSMutableDictionary *lokiMessage = [[NSMutableDictionary alloc] init]; + NSMutableDictionary *lokiMessage = [NSMutableDictionary new]; // Params for our message server lokiMessage[@"pubKey"] = message[@"destination"]; @@ -387,21 +387,19 @@ NS_ASSUME_NONNULL_BEGIN return filtered.count > 0 ? [filtered objectAtIndex:0] : nil; } -// LOKI: This is the function below with our changes +// Loki: This is the function below with our changes + (TSRequest *)submitLokiMessageRequestWithRecipient:(NSString *)recipientId messages:(NSArray *)messages nonceArray:(NSArray *)nonceArray - ttl: (NSNumber *)ttl + ttl:(NSNumber *)ttl { - // NOTE: messages may be empty; See comments in OWSDeviceManager. + // Messages may be empty; see comments in OWSDeviceManager OWSAssertDebug(recipientId.length > 0); NSDictionary *lokiMessages = [self lokiMessagesFromMessages:messages nonceArray:nonceArray ttl:ttl]; NSString *path = [textSecureMessagesAPI stringByAppendingString:recipientId]; - NSDictionary *parameters = @{ - @"messages" : lokiMessages, - }; + NSDictionary *parameters = @{ @"messages" : lokiMessages }; TSRequest *request = [TSRequest requestWithUrl:[NSURL URLWithString:path] method:@"PUT" parameters:parameters]; return request;