damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

RelayBootstrap.swift (2311B)


      1 //
      2 //  RelayBootstrap.swift
      3 //  damus
      4 //
      5 //  Created by William Casarin on 2023-04-04.
      6 //
      7 
      8 import Foundation
      9 
     10 // This is `fileprivate` because external code should use the `get_default_bootstrap_relays` instead.
     11 fileprivate let BOOTSTRAP_RELAYS = [
     12     "wss://relay.damus.io",
     13     "wss://nostr.land",
     14     "wss://nostr.wine",
     15     "wss://nos.lol",
     16 ]
     17 
     18 fileprivate let REGION_SPECIFIC_BOOTSTRAP_RELAYS: [Locale.Region: [String]] = [
     19     Locale.Region.japan: [
     20         "wss://relay-jp.nostr.wirednet.jp",
     21         "wss://yabu.me",
     22         "wss://r.kojira.io",
     23     ],
     24     Locale.Region.thailand: [
     25         "wss://relay.siamstr.com",
     26         "wss://relay.zerosatoshi.xyz",
     27         "wss://th2.nostr.earnkrub.xyz",
     28     ],
     29     Locale.Region.germany: [
     30         "wss://nostr.einundzwanzig.space",
     31         "wss://nostr.cercatrova.me",
     32         "wss://nostr.bitcoinplebs.de",
     33     ]
     34 ]
     35 
     36 func bootstrap_relays_setting_key(pubkey: Pubkey) -> String {
     37     return pk_setting_key(pubkey, key: "bootstrap_relays")
     38 }
     39 
     40 func save_bootstrap_relays(pubkey: Pubkey, relays: [RelayURL])  {
     41     let key = bootstrap_relays_setting_key(pubkey: pubkey)
     42 
     43     UserDefaults.standard.set(relays.map({ $0.absoluteString }), forKey: key)
     44 }
     45 
     46 func load_bootstrap_relays(pubkey: Pubkey) -> [RelayURL] {
     47     let key = bootstrap_relays_setting_key(pubkey: pubkey)
     48 
     49     guard let relays = UserDefaults.standard.stringArray(forKey: key) else {
     50         print("loading default bootstrap relays")
     51         return get_default_bootstrap_relays().map { $0 }
     52     }
     53     
     54     if relays.count == 0 {
     55         print("loading default bootstrap relays")
     56         return get_default_bootstrap_relays().map { $0 }
     57     }
     58 
     59     let relay_urls = relays.compactMap({ RelayURL($0) })
     60 
     61     let loaded_relays = Array(Set(relay_urls + get_default_bootstrap_relays()))
     62     print("Loading custom bootstrap relays: \(loaded_relays)")
     63     return loaded_relays
     64 }
     65 
     66 func get_default_bootstrap_relays() -> [RelayURL] {
     67     var default_bootstrap_relays: [RelayURL] = BOOTSTRAP_RELAYS.compactMap({ RelayURL($0) })
     68 
     69     if let user_region = Locale.current.region, let regional_bootstrap_relays = REGION_SPECIFIC_BOOTSTRAP_RELAYS[user_region] {
     70         default_bootstrap_relays.append(contentsOf: regional_bootstrap_relays.compactMap({ RelayURL($0) }))
     71     }
     72 
     73     return default_bootstrap_relays
     74 }