session-ios/SessionSnodeKit/Snode.swift

65 lines
2.1 KiB
Swift
Raw Normal View History

2020-11-05 02:07:21 +01:00
import Foundation
public final class Snode : NSObject, NSCoding { // NSObject/NSCoding conformance is needed for YapDatabase compatibility
2020-11-05 02:07:21 +01:00
public let address: String
public let port: UInt16
2020-11-11 00:58:56 +01:00
public let publicKeySet: KeySet
2020-11-05 02:07:21 +01:00
public var ip: String {
address.removingPrefix("https://")
}
2020-11-05 04:16:45 +01:00
// MARK: Nested Types
public enum Method : String {
2020-11-05 02:07:21 +01:00
case getSwarm = "get_snodes_for_pubkey"
case getMessages = "retrieve"
case sendMessage = "store"
2021-05-03 02:29:08 +02:00
case oxenDaemonRPCCall = "oxend_request"
2021-07-12 05:22:35 +02:00
case getInfo = "info"
case clearAllData = "delete_all"
2020-11-05 02:07:21 +01:00
}
2020-11-11 00:58:56 +01:00
public struct KeySet {
public let ed25519Key: String
public let x25519Key: String
2020-11-05 02:07:21 +01:00
}
2020-11-05 04:16:45 +01:00
2020-11-05 02:07:21 +01:00
// MARK: Initialization
internal init(address: String, port: UInt16, publicKeySet: KeySet) {
self.address = address
self.port = port
self.publicKeySet = publicKeySet
}
2020-11-05 04:16:45 +01:00
// MARK: Coding
public init?(coder: NSCoder) {
address = coder.decodeObject(forKey: "address") as! String
port = coder.decodeObject(forKey: "port") as! UInt16
guard let idKey = coder.decodeObject(forKey: "idKey") as? String,
let encryptionKey = coder.decodeObject(forKey: "encryptionKey") as? String else { return nil }
publicKeySet = KeySet(ed25519Key: idKey, x25519Key: encryptionKey)
super.init()
}
public func encode(with coder: NSCoder) {
coder.encode(address, forKey: "address")
coder.encode(port, forKey: "port")
coder.encode(publicKeySet.ed25519Key, forKey: "idKey")
coder.encode(publicKeySet.x25519Key, forKey: "encryptionKey")
}
2020-11-05 02:07:21 +01:00
// MARK: Equality
2020-11-05 04:16:45 +01:00
override public func isEqual(_ other: Any?) -> Bool {
guard let other = other as? Snode else { return false }
return address == other.address && port == other.port
2020-11-05 02:07:21 +01:00
}
// MARK: Hashing
2020-11-05 04:16:45 +01:00
override public var hash: Int { // Override NSObject.hash and not Hashable.hashValue or Hashable.hash(into:)
return address.hashValue ^ port.hashValue
2020-11-05 02:07:21 +01:00
}
// MARK: Description
2020-11-05 04:16:45 +01:00
override public var description: String { return "\(address):\(port)" }
2020-11-05 02:07:21 +01:00
}