Make POW related fields optional

pull/8/head
Niels Andriesse 7 years ago
parent a47f43b71c
commit e6bfd56487

@ -7,12 +7,12 @@ public struct LokiMessage {
let data: LosslessStringConvertible let data: LosslessStringConvertible
/// The time to live for the message. /// The time to live for the message.
let ttl: UInt64 let ttl: UInt64
/// When the proof of work was calculated. /// When the proof of work was calculated, if applicable.
let timestamp: UInt64 let timestamp: UInt64?
/// The base 64 encoded proof of work. /// The base 64 encoded proof of work, if applicable.
let nonce: String 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.destination = destination
self.data = data self.data = data
self.ttl = ttl self.ttl = ttl
@ -20,30 +20,34 @@ public struct LokiMessage {
self.nonce = nonce self.nonce = nonce
} }
public static func fromSignalMessage(_ signalMessage: SignalMessage) -> Promise<LokiMessage> { public static func fromSignalMessage(_ signalMessage: SignalMessage, requiringPOW isPOWRequired: Bool) -> Promise<LokiMessage> {
return Promise<LokiMessage> { seal in return Promise<LokiMessage> { seal in
DispatchQueue.global(qos: .default).async { DispatchQueue.global(qos: .default).async {
let destination = signalMessage["destination"]! let destination = signalMessage["destination"]!
let data = signalMessage["content"]! let data = signalMessage["content"]!
let ttl = LokiMessagingAPI.defaultTTL let ttl = LokiMessagingAPI.defaultTTL
let timestamp = UInt64(Date().timeIntervalSince1970) if isPOWRequired {
if let nonce = ProofOfWork.calculate(data: data, pubKey: destination, timestamp: timestamp, ttl: Int(ttl)) { let timestamp = UInt64(Date().timeIntervalSince1970)
let result = LokiMessage(destination: destination, data: data, ttl: ttl, timestamp: timestamp, nonce: nonce) if let nonce = ProofOfWork.calculate(data: data, pubKey: destination, timestamp: timestamp, ttl: Int(ttl)) {
seal.fulfill(result) let result = LokiMessage(destination: destination, data: data, ttl: ttl, timestamp: timestamp, nonce: nonce)
seal.fulfill(result)
} else {
seal.reject(LokiMessagingAPI.Error.proofOfWorkCalculationFailed)
}
} else { } 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] { public func toJSON() -> [String:String] {
return [ var result = [ "destination" : destination, "data" : data.description, "ttl" : String(ttl) ]
"destination" : destination, if let timestamp = timestamp, let nonce = nonce {
"data" : data.description, result["timestamp"] = String(timestamp)
"ttl" : String(ttl), result["nonce"] = nonce
"timestamp" : String(timestamp), }
"nonce" : nonce return result
]
} }
} }

@ -35,8 +35,8 @@ import PromiseKit
return TSNetworkManager.shared().makePromise(request: request) return TSNetworkManager.shared().makePromise(request: request)
} }
public static func sendSignalMessage(_ signalMessage: SignalMessage, to destination: String) -> Promise<RawResponse> { public static func sendSignalMessage(_ signalMessage: SignalMessage, to destination: String, requiringPOW isPOWRequired: Bool) -> Promise<RawResponse> {
return LokiMessage.fromSignalMessage(signalMessage).then(sendMessage) return LokiMessage.fromSignalMessage(signalMessage, requiringPOW: isPOWRequired).then(sendMessage)
} }
public static func sendMessage(_ lokiMessage: LokiMessage) -> Promise<RawResponse> { public static func sendMessage(_ lokiMessage: LokiMessage) -> Promise<RawResponse> {
@ -52,7 +52,7 @@ import PromiseKit
} }
// MARK: Obj-C API // MARK: Obj-C API
@objc public static func sendSignalMessage(_ signalMessage: SignalMessage, to destination: String, completionHandler: @escaping (Any?, NSError?) -> Void) { @objc public static func sendSignalMessage(_ signalMessage: SignalMessage, to destination: String, requiringPOW isPOWRequired: Bool, completionHandler: @escaping (Any?, NSError?) -> Void) {
sendSignalMessage(signalMessage, to: destination).done { completionHandler($0.responseObject, nil) }.catch { completionHandler(nil, $0 as NSError) } sendSignalMessage(signalMessage, to: destination, requiringPOW: isPOWRequired).done { completionHandler($0.responseObject, nil) }.catch { completionHandler(nil, $0 as NSError) }
} }
} }

Loading…
Cancel
Save