committed by
William Casarin
parent
0da10eb716
commit
9847f12c95
@@ -187,11 +187,6 @@ func make_ln_url(_ str: String?) -> URL? {
|
||||
return str.flatMap { URL(string: "lightning:" + $0) }
|
||||
}
|
||||
|
||||
struct NostrSubscription {
|
||||
let sub_id: String
|
||||
let filter: NostrFilter
|
||||
}
|
||||
|
||||
func lnaddress_to_lnurl(_ lnaddr: String) -> String? {
|
||||
let parts = lnaddr.split(separator: "@")
|
||||
guard parts.count == 2 else {
|
||||
|
||||
@@ -18,20 +18,6 @@ enum ValidationResult: Decodable {
|
||||
case ok
|
||||
case bad_id
|
||||
case bad_sig
|
||||
|
||||
var is_bad: Bool {
|
||||
return self == .bad_id || self == .bad_sig
|
||||
}
|
||||
}
|
||||
|
||||
struct OtherEvent {
|
||||
let event_id: String
|
||||
let relay_url: String
|
||||
}
|
||||
|
||||
struct KeyEvent {
|
||||
let key: String
|
||||
let relay_url: String
|
||||
}
|
||||
|
||||
struct ReferencedId: Identifiable, Hashable, Equatable {
|
||||
@@ -50,18 +36,6 @@ struct ReferencedId: Identifiable, Hashable, Equatable {
|
||||
static func e(_ id: String, relay_id: String? = nil) -> ReferencedId {
|
||||
return ReferencedId(ref_id: id, relay_id: relay_id, key: "e")
|
||||
}
|
||||
|
||||
static func p(_ id: String, relay_id: String? = nil) -> ReferencedId {
|
||||
return ReferencedId(ref_id: id, relay_id: relay_id, key: "p")
|
||||
}
|
||||
}
|
||||
|
||||
struct EventId: Identifiable, CustomStringConvertible {
|
||||
let id: String
|
||||
|
||||
var description: String {
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
class NostrEvent: Codable, Identifiable, CustomStringConvertible, Equatable, Hashable, Comparable {
|
||||
@@ -186,21 +160,9 @@ class NostrEvent: Codable, Identifiable, CustomStringConvertible, Equatable, Has
|
||||
}
|
||||
|
||||
return content
|
||||
|
||||
/*
|
||||
switch validity {
|
||||
case .ok:
|
||||
return content
|
||||
case .bad_id:
|
||||
return content + "\n\n*WARNING: invalid note id, could be forged!*"
|
||||
case .bad_sig:
|
||||
return content + "\n\n*WARNING: invalid signature, could be forged!*"
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
var description: String {
|
||||
//let p = pow.map { String($0) } ?? "?"
|
||||
return "NostrEvent { id: \(id) pubkey \(pubkey) kind \(kind) tags \(tags) content '\(content)' }"
|
||||
}
|
||||
|
||||
@@ -216,15 +178,6 @@ class NostrEvent: Codable, Identifiable, CustomStringConvertible, Equatable, Has
|
||||
return damus.get_referenced_ids(tags: self.tags, key: key)
|
||||
}
|
||||
|
||||
public func is_root_event() -> Bool {
|
||||
for tag in tags {
|
||||
if tag.count >= 1 && tag[0] == "e" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
public func direct_replies(_ privkey: String?) -> [ReferencedId] {
|
||||
return event_refs(privkey).reduce(into: []) { acc, evref in
|
||||
if let direct_reply = evref.is_direct_reply {
|
||||
@@ -299,31 +252,10 @@ class NostrEvent: Codable, Identifiable, CustomStringConvertible, Equatable, Has
|
||||
return get_referenced_ids(key: "e")
|
||||
}
|
||||
|
||||
public func count_ids() -> Int {
|
||||
return count_refs("e")
|
||||
}
|
||||
|
||||
public func count_refs(_ type: String) -> Int {
|
||||
var count: Int = 0
|
||||
for tag in tags {
|
||||
if tag.count >= 2 && tag[0] == "e" {
|
||||
count += 1
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
public var referenced_pubkeys: [ReferencedId] {
|
||||
return get_referenced_ids(key: "p")
|
||||
}
|
||||
|
||||
/// Make a local event
|
||||
public static func local(content: String, pubkey: String) -> NostrEvent {
|
||||
let ev = NostrEvent(content: content, pubkey: pubkey)
|
||||
ev.flags |= 1
|
||||
return ev
|
||||
}
|
||||
|
||||
public var is_local: Bool {
|
||||
return (self.flags & 1) != 0
|
||||
}
|
||||
@@ -339,45 +271,8 @@ class NostrEvent: Codable, Identifiable, CustomStringConvertible, Equatable, Has
|
||||
self.created_at = createdAt
|
||||
}
|
||||
|
||||
init(from: NostrEvent, content: String? = nil) {
|
||||
self.id = from.id
|
||||
self.sig = from.sig
|
||||
|
||||
self.content = content ?? from.content
|
||||
self.pubkey = from.pubkey
|
||||
self.kind = from.kind
|
||||
self.tags = from.tags
|
||||
self.created_at = from.created_at
|
||||
}
|
||||
|
||||
func calculate_id() {
|
||||
self.id = calculate_event_id(ev: self)
|
||||
//self.pow = count_hash_leading_zero_bits(self.id)
|
||||
}
|
||||
|
||||
// TODO: timeout
|
||||
/*
|
||||
func mine_id(pow: Int, done: @escaping (String) -> ()) {
|
||||
let nonce_ind = self.ensure_nonce_tag()
|
||||
let nonce: Int64 = 0
|
||||
|
||||
DispatchQueue.global(qos: .background).async {
|
||||
while
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
private func ensure_nonce_tag() -> Int {
|
||||
for (i, tags) in self.tags.enumerated() {
|
||||
for tag in tags {
|
||||
if tags.count == 2 && tag == "nonce" {
|
||||
return i
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.tags.append(["nonce", "0"])
|
||||
return self.tags.count - 1
|
||||
}
|
||||
|
||||
func sign(privkey: String) {
|
||||
@@ -538,18 +433,6 @@ func get_referenced_ids(tags: [[String]], key: String) -> [ReferencedId] {
|
||||
}
|
||||
}
|
||||
|
||||
func get_referenced_id_set(tags: [[String]], key: String) -> Set<ReferencedId> {
|
||||
return tags.reduce(into: Set()) { (acc, tag) in
|
||||
if tag.count >= 2 && tag[0] == key {
|
||||
var relay_id: String? = nil
|
||||
if tag.count >= 3 {
|
||||
relay_id = tag[2]
|
||||
}
|
||||
acc.insert(ReferencedId(ref_id: tag[1], relay_id: relay_id, key: key))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func make_first_contact_event(keypair: Keypair) -> NostrEvent? {
|
||||
guard let privkey = keypair.privkey else {
|
||||
return nil
|
||||
@@ -1018,14 +901,6 @@ func last_etag(tags: [[String]]) -> String? {
|
||||
return e
|
||||
}
|
||||
|
||||
func inner_event_or_self(ev: NostrEvent, cache: EventCache) -> NostrEvent {
|
||||
guard let inner_ev = ev.get_inner_event(cache: cache) else {
|
||||
return ev
|
||||
}
|
||||
|
||||
return inner_ev
|
||||
}
|
||||
|
||||
func first_eref_mention(ev: NostrEvent, privkey: String?) -> Mention? {
|
||||
let blocks = ev.blocks(privkey).filter { block in
|
||||
guard case .mention(let mention) = block else {
|
||||
|
||||
@@ -39,10 +39,6 @@ struct NostrFilter: Codable, Equatable {
|
||||
public static func filter_hashtag(_ htags: [String]) -> NostrFilter {
|
||||
return NostrFilter(ids: nil, kinds: nil, referenced_ids: nil, pubkeys: nil, since: nil, until: nil, authors: nil, hashtag: htags.map { $0.lowercased() })
|
||||
}
|
||||
|
||||
public static var filter_text: NostrFilter {
|
||||
return filter_kinds([NostrKind.text.rawValue])
|
||||
}
|
||||
|
||||
public static func filter_ids(_ ids: [String]) -> NostrFilter {
|
||||
return NostrFilter(ids: ids, kinds: nil, referenced_ids: nil, pubkeys: nil, since: nil, until: nil, authors: nil, hashtag: nil)
|
||||
@@ -63,8 +59,4 @@ struct NostrFilter: Codable, Equatable {
|
||||
public static func filter_kinds(_ kinds: [Int]) -> NostrFilter {
|
||||
return NostrFilter(ids: nil, kinds: kinds, referenced_ids: nil, pubkeys: nil, since: nil, until: nil, authors: nil)
|
||||
}
|
||||
|
||||
public static func filter_since(_ val: Int64) -> NostrFilter {
|
||||
return NostrFilter(ids: nil, kinds: nil, referenced_ids: nil, pubkeys: nil, since: val, until: nil, authors: nil)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,54 +7,6 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
|
||||
func zero_bits(_ argb: UInt8) -> Int
|
||||
{
|
||||
var b = argb
|
||||
var n: Int = 0;
|
||||
|
||||
if b == 0 {
|
||||
return 8;
|
||||
}
|
||||
|
||||
while true {
|
||||
b >>= 1;
|
||||
if b != 0 {
|
||||
n += 1;
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return 7-n;
|
||||
}
|
||||
|
||||
func count_hash_leading_zero_bits(_ hash: String) -> Int?
|
||||
{
|
||||
guard let decoded = hex_decode(hash) else {
|
||||
return nil
|
||||
}
|
||||
return count_leading_zero_bits(decoded)
|
||||
}
|
||||
|
||||
/* find the number of leading zero bits in a hash */
|
||||
func count_leading_zero_bits(_ hash: [UInt8]) -> Int
|
||||
{
|
||||
var bits: Int = 0
|
||||
var total: Int = 0
|
||||
|
||||
for c in hash {
|
||||
bits = zero_bits(c)
|
||||
total += bits
|
||||
if (bits != 8) {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return total
|
||||
}
|
||||
|
||||
|
||||
func char_to_hex(_ c: UInt8) -> UInt8?
|
||||
{
|
||||
// 0 && 9
|
||||
|
||||
@@ -60,10 +60,6 @@ class Relay: Identifiable {
|
||||
self.connection = connection
|
||||
}
|
||||
|
||||
func mark_broken() {
|
||||
flags |= RelayFlags.broken.rawValue
|
||||
}
|
||||
|
||||
var is_broken: Bool {
|
||||
return (flags & RelayFlags.broken.rawValue) == RelayFlags.broken.rawValue
|
||||
}
|
||||
@@ -76,7 +72,6 @@ class Relay: Identifiable {
|
||||
|
||||
enum RelayError: Error {
|
||||
case RelayAlreadyExists
|
||||
case RelayNotFound
|
||||
}
|
||||
|
||||
func get_relay_id(_ url: RelayURL) -> String {
|
||||
|
||||
@@ -8,22 +8,6 @@
|
||||
import Foundation
|
||||
import Network
|
||||
|
||||
struct SubscriptionId: Identifiable, CustomStringConvertible {
|
||||
let id: String
|
||||
|
||||
var description: String {
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
struct RelayId: Identifiable, CustomStringConvertible {
|
||||
let id: String
|
||||
|
||||
var description: String {
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
struct RelayHandler {
|
||||
let sub_id: String
|
||||
let callback: (String, NostrConnectionEvent) -> ()
|
||||
@@ -34,11 +18,6 @@ struct QueuedRequest {
|
||||
let relay: String
|
||||
}
|
||||
|
||||
struct NostrRequestId: Equatable, Hashable {
|
||||
let relay: String?
|
||||
let sub_id: String
|
||||
}
|
||||
|
||||
class RelayPool {
|
||||
var relays: [Relay] = []
|
||||
var handlers: [RelayHandler] = []
|
||||
@@ -67,10 +46,6 @@ class RelayPool {
|
||||
relays.map { $0.descriptor }
|
||||
}
|
||||
|
||||
var num_connecting: Int {
|
||||
return relays.reduce(0) { n, r in n + (r.connection.isConnecting ? 1 : 0) }
|
||||
}
|
||||
|
||||
var num_connected: Int {
|
||||
return relays.reduce(0) { n, r in n + (r.connection.isConnected ? 1 : 0) }
|
||||
}
|
||||
@@ -151,12 +126,6 @@ class RelayPool {
|
||||
relay.connection.reconnect()
|
||||
}
|
||||
}
|
||||
|
||||
func mark_broken(_ relay_id: String) {
|
||||
for relay in relays {
|
||||
relay.mark_broken()
|
||||
}
|
||||
}
|
||||
|
||||
func connect(to: [String]? = nil) {
|
||||
let relays = to.map{ get_relays($0) } ?? self.relays
|
||||
|
||||
Reference in New Issue
Block a user