Implement new messaging API

This commit is contained in:
Niels Andriesse 2019-05-07 10:10:15 +10:00
parent 18d1225faa
commit 78adfabf0c
4 changed files with 72 additions and 6 deletions

View File

@ -0,0 +1,49 @@
import PromiseKit
@objc public final class LokiMessage : NSObject {
/// The hex encoded public key of the receiver.
let destination: String
/// The content of the message.
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
init(destination: String, data: LosslessStringConvertible, ttl: UInt64, timestamp: UInt64, nonce: String) {
self.destination = destination
self.data = data
self.ttl = ttl
self.timestamp = timestamp
self.nonce = nonce
}
public static func fromSignalMessage(_ signalMessage: SignalMessage) -> 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)
} else {
seal.reject(LokiMessagingAPI.Error.proofOfWorkCalculationFailed)
}
}
}
}
func toJSON() -> [String:String] {
return [
"destination" : destination,
"data" : data.description,
"ttl" : String(ttl),
"timestamp" : String(timestamp),
"nonce" : nonce
]
}
}

View File

@ -5,6 +5,7 @@ import PromiseKit
private static var baseURL: String { return textSecureServerURL }
private static var port: String { return "8080" }
private static var apiVersion: String { return "v1" }
public static let defaultTTL: UInt64 = 4 * 24 * 60 * 60
// MARK: Types
private enum Method : String {
@ -14,18 +15,32 @@ import PromiseKit
public typealias RawResponse = TSNetworkManager.NetworkManagerResult
public enum Error : LocalizedError {
case proofOfWorkCalculationFailed
public var errorDescription: String? {
switch self {
case .proofOfWorkCalculationFailed: return NSLocalizedString("Failed to calculate proof of work.", comment: "")
}
}
}
// MARK: Lifecycle
override private init() { }
// MARK: API
private static func invoke(_ method: Method, parameters: [String:String] = [:]) -> (request: TSRequest, promise: Promise<RawResponse>) {
private static func invoke(_ method: Method, parameters: [String:String] = [:]) -> Promise<RawResponse> {
let url = URL(string: "\(baseURL):\(port)/\(apiVersion)/storage_rpc")!
let request = TSRequest(url: url, method: "POST", parameters: [ "method" : method.rawValue, "params" : parameters ])
return (request, TSNetworkManager.shared().makePromise(request: request))
return TSNetworkManager.shared().makePromise(request: request)
}
@objc public static func sendMessage(_ message: [String:String]) -> TSRequest {
return invoke(.sendMessage, parameters: message).request
public static func sendSignalMessage(_ signalMessage: SignalMessage, to destination: String) -> Promise<RawResponse> {
return LokiMessage.fromSignalMessage(signalMessage).then(sendMessage)
}
public static func sendMessage(_ lokiMessage: LokiMessage) -> Promise<RawResponse> {
return invoke(.sendMessage, parameters: lokiMessage.toJSON())
}
public static func retrieveAllMessages() -> Promise<RawResponse> {
@ -33,6 +48,6 @@ import PromiseKit
"pubKey" : OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey,
"lastHash" : "" // TODO: Implement
]
return invoke(.retrieveAllMessages, parameters: parameters).promise
return invoke(.retrieveAllMessages, parameters: parameters)
}
}

View File

@ -0,0 +1,2 @@
public typealias SignalMessage = [String:String]

View File

@ -405,7 +405,7 @@ NS_ASSUME_NONNULL_BEGIN
NSString *path = [textSecureMessagesAPI stringByAppendingString:recipientId];
NSDictionary *parameters = [lokiMessages objectAtIndex:0];
return [LokiMessagingAPI sendMessage:parameters];
return [TSRequest new]; // TODO: Just here to make things build
}
+ (TSRequest *)submitMessageRequestWithRecipient:(NSString *)recipientId