2019-04-30 06:27:39 +02:00
|
|
|
import PromiseKit
|
|
|
|
|
2019-05-08 07:32:10 +02:00
|
|
|
// Helper function for objc apis
|
|
|
|
private extension Promise where T == Any {
|
|
|
|
func recoverNetworkError(on queue: DispatchQueue) -> Promise<T> {
|
|
|
|
return self.recover(on: queue) { error -> Promise<T> in
|
|
|
|
switch error {
|
|
|
|
case NetworkManagerError.taskError(_, let underlyingError):
|
|
|
|
throw underlyingError
|
|
|
|
default:
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 01:29:07 +02:00
|
|
|
@objc public final class LokiAPI : NSObject {
|
2019-04-30 06:27:39 +02:00
|
|
|
|
2019-05-08 01:29:07 +02:00
|
|
|
private static let version = "v1"
|
|
|
|
public static let defaultMessageTTL: UInt64 = 4 * 24 * 60 * 60
|
2019-04-30 06:27:39 +02:00
|
|
|
|
|
|
|
// MARK: Types
|
|
|
|
private enum Method : String {
|
2019-05-07 08:03:57 +02:00
|
|
|
case getMessages = "retrieve"
|
2019-05-06 08:13:32 +02:00
|
|
|
case sendMessage = "store"
|
2019-05-07 08:03:57 +02:00
|
|
|
case getSwarm = "get_snodes_for_pubkey"
|
2019-04-30 06:27:39 +02:00
|
|
|
}
|
|
|
|
|
2019-05-08 01:29:07 +02:00
|
|
|
public struct Target : Hashable {
|
2019-05-07 07:03:54 +02:00
|
|
|
let address: String
|
|
|
|
let port: UInt16
|
|
|
|
}
|
|
|
|
|
2019-05-07 05:53:31 +02:00
|
|
|
public typealias RawResponse = Any
|
2019-04-30 06:27:39 +02:00
|
|
|
|
2019-05-07 02:10:15 +02:00
|
|
|
public enum Error : LocalizedError {
|
|
|
|
case proofOfWorkCalculationFailed
|
|
|
|
|
|
|
|
public var errorDescription: String? {
|
|
|
|
switch self {
|
|
|
|
case .proofOfWorkCalculationFailed: return NSLocalizedString("Failed to calculate proof of work.", comment: "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-30 06:27:39 +02:00
|
|
|
// MARK: Lifecycle
|
2019-05-06 08:13:32 +02:00
|
|
|
override private init() { }
|
2019-04-30 06:27:39 +02:00
|
|
|
|
|
|
|
// MARK: API
|
2019-05-07 07:03:54 +02:00
|
|
|
private static func invoke(_ method: Method, on target: Target, with parameters: [String:String] = [:]) -> Promise<RawResponse> {
|
2019-05-08 01:29:07 +02:00
|
|
|
let url = URL(string: "\(target.address):\(target.port)/\(version)/storage_rpc")!
|
2019-04-30 06:27:39 +02:00
|
|
|
let request = TSRequest(url: url, method: "POST", parameters: [ "method" : method.rawValue, "params" : parameters ])
|
2019-05-07 05:53:31 +02:00
|
|
|
return TSNetworkManager.shared().makePromise(request: request).map { $0.responseObject }
|
2019-04-30 06:27:39 +02:00
|
|
|
}
|
2019-05-07 02:29:44 +02:00
|
|
|
|
2019-05-07 08:03:57 +02:00
|
|
|
public static func getRandomSnode() -> Promise<Target> {
|
|
|
|
return Promise<Target> { seal in
|
|
|
|
seal.fulfill(Target(address: "http://13.238.53.205", port: 8080)) // TODO: Temporary
|
|
|
|
}
|
2019-04-30 06:27:39 +02:00
|
|
|
}
|
|
|
|
|
2019-05-07 08:03:57 +02:00
|
|
|
public static func getMessages() -> Promise<RawResponse> {
|
2019-04-30 07:12:15 +02:00
|
|
|
let parameters = [
|
|
|
|
"pubKey" : OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey,
|
2019-05-06 08:13:32 +02:00
|
|
|
"lastHash" : "" // TODO: Implement
|
2019-04-30 07:12:15 +02:00
|
|
|
]
|
2019-05-08 01:29:07 +02:00
|
|
|
return getRandomSnode().then { invoke(.getMessages, on: $0, with: parameters) } // TODO: Use getSwarm()
|
2019-05-07 08:03:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public static func sendMessage(_ lokiMessage: LokiMessage) -> Promise<RawResponse> {
|
2019-05-08 01:29:07 +02:00
|
|
|
return getRandomSnode().then { invoke(.sendMessage, on: $0, with: lokiMessage.toJSON()) } // TODO: Use getSwarm()
|
2019-05-07 08:03:57 +02:00
|
|
|
}
|
|
|
|
|
2019-05-08 02:04:19 +02:00
|
|
|
public static func ping(_ hexEncodedPublicKey: String) -> Promise<RawResponse> {
|
|
|
|
return getRandomSnode().then { invoke(.sendMessage, on: $0, with: [ "destination" : hexEncodedPublicKey ]) } // TODO: Use getSwarm() and figure out correct parameters
|
|
|
|
}
|
|
|
|
|
2019-05-08 01:29:07 +02:00
|
|
|
public static func getSwarm(for hexEncodedPublicKey: String) -> Promise<Set<Target>> {
|
|
|
|
return getRandomSnode().then { invoke(.getSwarm, on: $0, with: [ "pubKey" : hexEncodedPublicKey ]) }.map { rawResponse in return [] } // TODO: Parse targets from raw response
|
2019-04-30 06:27:39 +02:00
|
|
|
}
|
2019-05-07 02:29:44 +02:00
|
|
|
|
|
|
|
// MARK: Obj-C API
|
2019-05-08 06:01:23 +02:00
|
|
|
@objc public static func objc_getMessages() -> AnyPromise {
|
2019-05-08 04:37:38 +02:00
|
|
|
let promise = getMessages()
|
2019-05-08 07:32:10 +02:00
|
|
|
.recoverNetworkError(on: DispatchQueue.global())
|
2019-05-08 04:37:38 +02:00
|
|
|
let anyPromise = AnyPromise(promise)
|
|
|
|
anyPromise.retainUntilComplete()
|
|
|
|
return anyPromise
|
2019-05-08 03:30:02 +02:00
|
|
|
}
|
|
|
|
|
2019-05-08 06:01:23 +02:00
|
|
|
@objc public static func objc_sendSignalMessage(_ signalMessage: SignalMessage, to destination: String, requiringPoW isPoWRequired: Bool) -> AnyPromise {
|
2019-05-08 07:32:10 +02:00
|
|
|
let promise = LokiMessage.fromSignalMessage(signalMessage, requiringPoW: isPoWRequired)
|
|
|
|
.then(sendMessage)
|
|
|
|
.recoverNetworkError(on: DispatchQueue.global())
|
2019-05-08 04:37:38 +02:00
|
|
|
let anyPromise = AnyPromise(promise)
|
|
|
|
anyPromise.retainUntilComplete()
|
|
|
|
return anyPromise
|
2019-05-07 02:29:44 +02:00
|
|
|
}
|
2019-04-30 06:27:39 +02:00
|
|
|
}
|