Files
damus/damus/Nostr/Relay.swift
William Casarin 7267662330 relay: introduce ephemeral relays
Epehmeral relays are relays that are connected in your relay pool but
not a part of your official relay list. This will be used by
nostr-wallet-connect to communicate with an NWC relay to pay invoices.
2023-05-10 12:01:26 -07:00

88 lines
1.8 KiB
Swift

//
// Relay.swift
// damus
//
// Created by William Casarin on 2022-04-11.
//
import Foundation
public struct RelayInfo: Codable {
let read: Bool
let write: Bool
let ephemeral: Bool
init(read: Bool, write: Bool, ephemeral: Bool = false) {
self.read = read
self.write = write
self.ephemeral = ephemeral
}
static let rw = RelayInfo(read: true, write: true, ephemeral: false)
static let ephemeral = RelayInfo(read: true, write: true, ephemeral: true)
}
public struct RelayDescriptor {
public let url: RelayURL
public let info: RelayInfo
}
enum RelayFlags: Int {
case none = 0
case broken = 1
}
struct Limitations: Codable {
let payment_required: Bool?
static var empty: Limitations {
Limitations(payment_required: nil)
}
}
struct RelayMetadata: Codable {
let name: String?
let description: String?
let pubkey: String?
let contact: String?
let supported_nips: [Int]?
let software: String?
let version: String?
let limitation: Limitations?
let payments_url: String?
var is_paid: Bool {
return limitation?.payment_required ?? false
}
}
class Relay: Identifiable {
let descriptor: RelayDescriptor
let connection: RelayConnection
var flags: Int
init(descriptor: RelayDescriptor, connection: RelayConnection) {
self.flags = 0
self.descriptor = descriptor
self.connection = connection
}
var is_broken: Bool {
return (flags & RelayFlags.broken.rawValue) == RelayFlags.broken.rawValue
}
var id: String {
return get_relay_id(descriptor.url)
}
}
enum RelayError: Error {
case RelayAlreadyExists
}
func get_relay_id(_ url: RelayURL) -> String {
return url.url.absoluteString
}