Added public key to LokiAPITarget

This commit is contained in:
Mikunj 2020-01-20 12:54:34 +11:00
parent b37378a2e9
commit 920520077d
4 changed files with 13 additions and 8 deletions

View File

@ -58,7 +58,8 @@ public extension LokiAPI {
"limit" : 24,
"fields" : [
"public_ip" : true,
"storage_port" : true
"storage_port" : true,
"pubkey_ed25519": true
]
]
])
@ -67,11 +68,11 @@ public extension LokiAPI {
let rawResponse = intermediate.responseObject
guard let json = rawResponse as? JSON, let intermediate = json["result"] as? JSON, let rawTargets = intermediate["service_node_states"] as? [JSON] else { throw "Failed to update random snode pool from: \(rawResponse)." }
randomSnodePool = try Set(rawTargets.flatMap { rawTarget in
guard let address = rawTarget["public_ip"] as? String, let port = rawTarget["storage_port"] as? Int, address != "0.0.0.0" else {
guard let address = rawTarget["public_ip"] as? String, let port = rawTarget["storage_port"] as? Int, let publicKey = rawTarget["pubkey_ed25519"] as? String, address != "0.0.0.0" else {
print("Failed to update random snode pool from: \(rawTarget).")
return nil
}
return LokiAPITarget(address: "https://\(address)", port: UInt16(port))
return LokiAPITarget(address: "https://\(address)", port: UInt16(port), publicKey: publicKey)
})
return randomSnodePool.randomElement()!
}.recover(on: DispatchQueue.global()) { error -> Promise<LokiAPITarget> in
@ -108,11 +109,11 @@ public extension LokiAPI {
return []
}
return rawSnodes.flatMap { rawSnode in
guard let address = rawSnode["ip"] as? String, let portAsString = rawSnode["port"] as? String, let port = UInt16(portAsString), address != "0.0.0.0" else {
guard let address = rawSnode["ip"] as? String, let portAsString = rawSnode["port"] as? String, let port = UInt16(portAsString), let publicKey = rawSnode["pubkey_ed25519"] as? String, address != "0.0.0.0" else {
print("[Loki] Failed to parse target from: \(rawSnode).")
return nil
}
return LokiAPITarget(address: "https://\(address)", port: port)
return LokiAPITarget(address: "https://\(address)", port: port, publicKey: publicKey)
}
}
}

View File

@ -180,7 +180,7 @@ public final class LokiAPI : NSObject {
}
}
if let peer = LokiP2PAPI.getInfo(for: destination), (lokiMessage.isPing || peer.isOnline) {
let target = LokiAPITarget(address: peer.address, port: peer.port)
let target = LokiAPITarget(address: peer.address, port: peer.port, publicKey: nil)
return Promise.value([ target ]).mapValues { sendLokiMessage(lokiMessage, to: $0) }.map { Set($0) }.retryingIfNeeded(maxRetryCount: maxRetryCount).get { _ in
LokiP2PAPI.markOnline(destination)
onP2PSuccess()

View File

@ -2,6 +2,7 @@
internal final class LokiAPITarget : NSObject, NSCoding {
internal let address: String
internal let port: UInt16
internal let publicKey: String?
// MARK: Types
internal enum Method : String {
@ -13,21 +14,24 @@ internal final class LokiAPITarget : NSObject, NSCoding {
}
// MARK: Initialization
internal init(address: String, port: UInt16) {
internal init(address: String, port: UInt16, publicKey: String?) {
self.address = address
self.port = port
self.publicKey = publicKey
}
// MARK: Coding
internal init?(coder: NSCoder) {
address = coder.decodeObject(forKey: "address") as! String
port = coder.decodeObject(forKey: "port") as! UInt16
publicKey = coder.decodeObject(forKey: "publicKey") as? String
super.init()
}
internal func encode(with coder: NSCoder) {
coder.encode(address, forKey: "address")
coder.encode(port, forKey: "port")
coder.encode(publicKey, forKey: "publicKey")
}
// MARK: Equality

View File

@ -33,7 +33,7 @@ public class LokiP2PAPI : NSObject {
/// - Parameter url: The url to our local server
@objc public static func setOurP2PAddress(url: URL) {
guard let scheme = url.scheme, let host = url.host, let port = url.port else { return }
let target = LokiAPITarget(address: "\(scheme)://\(host)", port: UInt16(port))
let target = LokiAPITarget(address: "\(scheme)://\(host)", port: UInt16(port), publicKey: nil)
ourP2PAddress = target
}