Fix minor code style issues

pull/6/head
Niels Andriesse 6 years ago
parent 177d9f7b87
commit dd18e65e3e

@ -44,7 +44,6 @@ private extension MutableCollection where Element == UInt8, Index == Int {
* Ref: libloki/proof-of-work.js * Ref: libloki/proof-of-work.js
*/ */
@objc public class ProofOfWork : NSObject { @objc public class ProofOfWork : NSObject {
private override init() {}
// If this changes then we also have to use something other than UInt64 to support the new length // If this changes then we also have to use something other than UInt64 to support the new length
private static let nonceLength = 8 private static let nonceLength = 8
@ -56,6 +55,8 @@ private extension MutableCollection where Element == UInt8, Index == Int {
} }
}() }()
private override init() { }
/// Calculate a proof of work with the given configuration /// Calculate a proof of work with the given configuration
/// ///
/// Ref: https://bitmessage.org/wiki/Proof_of_work /// Ref: https://bitmessage.org/wiki/Proof_of_work
@ -66,7 +67,7 @@ private extension MutableCollection where Element == UInt8, Index == Int {
/// - timestamp: The timestamp /// - timestamp: The timestamp
/// - ttl: The message time to live /// - ttl: The message time to live
/// - Returns: A nonce string or nil if it failed /// - 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 payload = getPayload(pubKey: pubKey, data: data, timestamp: timestamp, ttl: ttl)
let target = calcTarget(ttl: ttl, payloadLength: payload.count, nonceTrials: nonceTrialCount) 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 /// 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 timestampString = String(timestamp)
let ttlString = String(ttl) let ttlString = String(ttl)
let payloadString = timestampString + ttlString + pubKey + data 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 /// 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 two16 = UInt64(pow(2, 16) - 1)
let two64 = UInt64(pow(2, 64) - 1) let two64 = UInt64(pow(2, 64) - 1)

@ -918,18 +918,18 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
- (AnyPromise *)calculateProofOfWorkForDeviceMessages:(NSArray<NSDictionary *> *)deviceMessages - (AnyPromise *)calculateProofOfWorkForDeviceMessages:(NSArray<NSDictionary *> *)deviceMessages
ttl:(NSNumber *)ttl ttl:(NSNumber *)ttl
{ {
// LOKI: Calculate the proof of work for each device message // Loki: Calculate the proof of work for each device message
NSMutableArray *promises = [[NSMutableArray alloc] init]; NSMutableArray *promises = [NSMutableArray new];
for (NSDictionary<NSString *, id> *deviceMessage in deviceMessages) { for (NSDictionary<NSString *, id> *deviceMessage in deviceMessages) {
AnyPromise *promise = [AnyPromise promiseWithValue:deviceMessage] AnyPromise *promise = [AnyPromise promiseWithValue:deviceMessage]
.thenOn(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(NSDictionary<NSString *, id> *message) { .thenOn(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(NSDictionary<NSString *, id> *message) {
NSTimeInterval timestampInterval = [[NSDate date] timeIntervalSince1970]; NSTimeInterval timestampInterval = [[NSDate new] timeIntervalSince1970];
NSNumber *timestamp = [NSNumber numberWithDouble:timestampInterval]; NSNumber *timestamp = [NSNumber numberWithDouble:timestampInterval];
NSString *destination = message[@"destination"]; NSString *destination = message[@"destination"];
NSString *data = message[@"content"]; 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 // Return our timestamp along with the nonce
// These will help us identify which nonce belongs to which message // These will help us identify which nonce belongs to which message
@ -1122,19 +1122,21 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
// TODO: Update message here to show the pow cog icon // 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)]; NSNumber *ttl = [NSNumber numberWithInteger:@(4 * 24 * 60 * 60)];
AnyPromise *PoWPromise = [self calculateProofOfWorkForDeviceMessages:deviceMessages ttl:ttl]; AnyPromise *powPromise = [self calculateProofOfWorkForDeviceMessages:deviceMessages ttl:ttl];
[PoWPromise [powPromise
.thenOn([OWSDispatch sendingQueue], ^(NSArray *nonceArray) { .thenOn([OWSDispatch sendingQueue], ^(NSArray *nonceArray) {
OWSRequestMaker *requestMaker = [[OWSRequestMaker alloc] initWithLabel:@"Message Send" OWSRequestMaker *requestMaker = [[OWSRequestMaker alloc] initWithLabel:@"Message Send"
requestFactoryBlock:^(SMKUDAccessKey *_Nullable udAccessKey) { requestFactoryBlock:^(SMKUDAccessKey *_Nullable udAccessKey) {
// Loki Changes: // Loki
// ========
return [OWSRequestFactory submitLokiMessageRequestWithRecipient:recipient.recipientId return [OWSRequestFactory submitLokiMessageRequestWithRecipient:recipient.recipientId
messages:deviceMessages messages:deviceMessages
nonceArray:nonceArray nonceArray:nonceArray
ttl:ttl]; ttl:ttl];
/* Original Code: // ========
/* Original code:
return [OWSRequestFactory submitMessageRequestWithRecipient:recipient.recipientId return [OWSRequestFactory submitMessageRequestWithRecipient:recipient.recipientId
messages:deviceMessages messages:deviceMessages
timeStamp:message.timestamp timeStamp:message.timestamp

@ -354,14 +354,14 @@ NS_ASSUME_NONNULL_BEGIN
return [accountAttributes copy]; 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 // Refer to OWSMessageServiceParams for the Signal JSON params
+ (NSDictionary *)lokiMessagesFromMessages:(NSArray *)messages + (NSDictionary *)lokiMessagesFromMessages:(NSArray *)messages
nonceArray:(NSArray *)nonceArray nonceArray:(NSArray *)nonceArray
ttl:(NSNumber *)ttl { ttl:(NSNumber *)ttl {
NSMutableArray *modifiedMessages = [[NSMutableArray alloc] init]; NSMutableArray *modifiedMessages = [NSMutableArray new];
for (NSDictionary *message in messages) { for (NSDictionary *message in messages) {
NSMutableDictionary *lokiMessage = [[NSMutableDictionary alloc] init]; NSMutableDictionary *lokiMessage = [NSMutableDictionary new];
// Params for our message server // Params for our message server
lokiMessage[@"pubKey"] = message[@"destination"]; lokiMessage[@"pubKey"] = message[@"destination"];
@ -387,21 +387,19 @@ NS_ASSUME_NONNULL_BEGIN
return filtered.count > 0 ? [filtered objectAtIndex:0] : nil; 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 + (TSRequest *)submitLokiMessageRequestWithRecipient:(NSString *)recipientId
messages:(NSArray *)messages messages:(NSArray *)messages
nonceArray:(NSArray *)nonceArray 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); OWSAssertDebug(recipientId.length > 0);
NSDictionary *lokiMessages = [self lokiMessagesFromMessages:messages nonceArray:nonceArray ttl:ttl]; NSDictionary *lokiMessages = [self lokiMessagesFromMessages:messages nonceArray:nonceArray ttl:ttl];
NSString *path = [textSecureMessagesAPI stringByAppendingString:recipientId]; NSString *path = [textSecureMessagesAPI stringByAppendingString:recipientId];
NSDictionary *parameters = @{ NSDictionary *parameters = @{ @"messages" : lokiMessages };
@"messages" : lokiMessages,
};
TSRequest *request = [TSRequest requestWithUrl:[NSURL URLWithString:path] method:@"PUT" parameters:parameters]; TSRequest *request = [TSRequest requestWithUrl:[NSURL URLWithString:path] method:@"PUT" parameters:parameters];
return request; return request;

Loading…
Cancel
Save