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
/// 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<LokiMessage> {
public static func fromSignalMessage(_ signalMessage: SignalMessage, requiringPOW isPOWRequired: Bool) -> Promise<LokiMessage> {
return Promise<LokiMessage> { 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
}
}

@ -35,8 +35,8 @@ import PromiseKit
return TSNetworkManager.shared().makePromise(request: request)
}
public static func sendSignalMessage(_ signalMessage: SignalMessage, to destination: String) -> Promise<RawResponse> {
return LokiMessage.fromSignalMessage(signalMessage).then(sendMessage)
public static func sendSignalMessage(_ signalMessage: SignalMessage, to destination: String, requiringPOW isPOWRequired: Bool) -> Promise<RawResponse> {
return LokiMessage.fromSignalMessage(signalMessage, requiringPOW: isPOWRequired).then(sendMessage)
}
public static func sendMessage(_ lokiMessage: LokiMessage) -> Promise<RawResponse> {
@ -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) }
}
}

Loading…
Cancel
Save