diff --git a/SignalServiceKit/src/Loki/LokiMessage.swift b/SignalServiceKit/src/Loki/LokiMessage.swift index e937802ef..af065008b 100644 --- a/SignalServiceKit/src/Loki/LokiMessage.swift +++ b/SignalServiceKit/src/Loki/LokiMessage.swift @@ -7,12 +7,12 @@ public struct LokiMessage { let data: LosslessStringConvertible /// The time to live for the message. let ttl: UInt64 - /// When the proof of work was calculated. - let timestamp: UInt64 - /// The base 64 encoded proof of work. - let nonce: String + /// When the proof of work was calculated, if applicable. + let timestamp: UInt64? + /// The base 64 encoded proof of work, if applicable. + let nonce: String? - public init(destination: String, data: LosslessStringConvertible, ttl: UInt64, timestamp: UInt64, nonce: String) { + public init(destination: String, data: LosslessStringConvertible, ttl: UInt64, timestamp: UInt64?, nonce: String?) { self.destination = destination self.data = data self.ttl = ttl @@ -20,30 +20,34 @@ public struct LokiMessage { self.nonce = nonce } - public static func fromSignalMessage(_ signalMessage: SignalMessage) -> Promise { + public static func fromSignalMessage(_ signalMessage: SignalMessage, requiringPOW isPOWRequired: Bool) -> Promise { return Promise { seal in DispatchQueue.global(qos: .default).async { let destination = signalMessage["destination"]! let data = signalMessage["content"]! let ttl = LokiMessagingAPI.defaultTTL - let timestamp = UInt64(Date().timeIntervalSince1970) - if let nonce = ProofOfWork.calculate(data: data, pubKey: destination, timestamp: timestamp, ttl: Int(ttl)) { - let result = LokiMessage(destination: destination, data: data, ttl: ttl, timestamp: timestamp, nonce: nonce) - seal.fulfill(result) + if isPOWRequired { + let timestamp = UInt64(Date().timeIntervalSince1970) + if let nonce = ProofOfWork.calculate(data: data, pubKey: destination, timestamp: timestamp, ttl: Int(ttl)) { + let result = LokiMessage(destination: destination, data: data, ttl: ttl, timestamp: timestamp, nonce: nonce) + seal.fulfill(result) + } else { + seal.reject(LokiMessagingAPI.Error.proofOfWorkCalculationFailed) + } } else { - seal.reject(LokiMessagingAPI.Error.proofOfWorkCalculationFailed) + let result = LokiMessage(destination: destination, data: data, ttl: ttl, timestamp: nil, nonce: nil) + seal.fulfill(result) } } } } public func toJSON() -> [String:String] { - return [ - "destination" : destination, - "data" : data.description, - "ttl" : String(ttl), - "timestamp" : String(timestamp), - "nonce" : nonce - ] + var result = [ "destination" : destination, "data" : data.description, "ttl" : String(ttl) ] + if let timestamp = timestamp, let nonce = nonce { + result["timestamp"] = String(timestamp) + result["nonce"] = nonce + } + return result } } diff --git a/SignalServiceKit/src/Loki/LokiMessagingAPI.swift b/SignalServiceKit/src/Loki/LokiMessagingAPI.swift index 668e31755..e221d785e 100644 --- a/SignalServiceKit/src/Loki/LokiMessagingAPI.swift +++ b/SignalServiceKit/src/Loki/LokiMessagingAPI.swift @@ -35,8 +35,8 @@ import PromiseKit return TSNetworkManager.shared().makePromise(request: request) } - public static func sendSignalMessage(_ signalMessage: SignalMessage, to destination: String) -> Promise { - return LokiMessage.fromSignalMessage(signalMessage).then(sendMessage) + public static func sendSignalMessage(_ signalMessage: SignalMessage, to destination: String, requiringPOW isPOWRequired: Bool) -> Promise { + return LokiMessage.fromSignalMessage(signalMessage, requiringPOW: isPOWRequired).then(sendMessage) } public static func sendMessage(_ lokiMessage: LokiMessage) -> Promise { @@ -52,7 +52,7 @@ import PromiseKit } // MARK: Obj-C API - @objc public static func sendSignalMessage(_ signalMessage: SignalMessage, to destination: String, completionHandler: @escaping (Any?, NSError?) -> Void) { - sendSignalMessage(signalMessage, to: destination).done { completionHandler($0.responseObject, nil) }.catch { completionHandler(nil, $0 as NSError) } + @objc public static func sendSignalMessage(_ signalMessage: SignalMessage, to destination: String, requiringPOW isPOWRequired: Bool, completionHandler: @escaping (Any?, NSError?) -> Void) { + sendSignalMessage(signalMessage, to: destination, requiringPOW: isPOWRequired).done { completionHandler($0.responseObject, nil) }.catch { completionHandler(nil, $0 as NSError) } } }