|
|
|
|
@ -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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|