Skip to content
Snippets Groups Projects
Select Git revision
  • f4e22b7d1fed61b0bbfd20fa7c0de5763107c934
  • release default protected
  • master protected
  • NationalTreasure/NotificationUpgrade
  • XX-4441
  • xx-4417/gw-poll-earliest-client-round
  • tls-websockets
  • hotfix/drain
  • hotfix/matcher
  • projects/crust_RELEASE
  • XX-4055/ChannelIdentityTracking
  • XX-4066/CrustUpgrade_MASTER
  • Ace/Huawei
  • hotfix/accumulate-notifs
  • XX-3564/TlsCipherSuite
  • hotfix/groupNotification
  • Anne/License-Update
  • hotfix/trustoldgatewaysonly
  • hotfix/notifications
  • notls
  • url-repo-rename
  • v0.0.4
  • v0.0.3
  • v0.0.2
  • v0.0.1
25 results

utils.go

Blame
  • ClientStorage.swift 2.11 KiB
    import Bindings
    
    public struct ClientStorage {
      public var hasStoredClient: () -> Bool
      public var createClient: () throws -> Client
      public var loadClient: () throws -> Client
      public var removeClient: () throws -> Void
    }
    
    extension ClientStorage {
      public static let defaultDirectoryURL = FileManager.default
        .urls(for: .applicationSupportDirectory, in: .userDomainMask)
        .first!
        .appendingPathComponent("xx.network.client")
    
      public static func live(
        environment: Environment = .mainnet,
        directoryURL: URL = defaultDirectoryURL,
        fileManager: FileManager = .default,
        generatePassword: PasswordGenerator = .live,
        passwordStorage: PasswordStorage,
        downloadNDF: NDFDownloader = .live,
        createClient: ClientCreator = .live,
        loadClient: ClientLoader = .live
      ) -> ClientStorage {
        ClientStorage(
          hasStoredClient: {
            let contents = try? fileManager.contentsOfDirectory(atPath: directoryURL.path)
            return contents.map { $0.isEmpty == false } ?? false
          },
          createClient: {
            let ndf = try downloadNDF(environment)
            let password = generatePassword()
            try passwordStorage.save(password)
            try? fileManager.removeItem(at: directoryURL)
            try? fileManager.createDirectory(at: directoryURL, withIntermediateDirectories: true)
            try createClient(directoryURL: directoryURL, ndf: ndf, password: password)
            return try loadClient(directoryURL: directoryURL, password: password)
          },
          loadClient: {
            let password = try passwordStorage.load()
            return try loadClient(directoryURL: directoryURL, password: password)
          },
          removeClient: {
            try fileManager.removeItem(at: directoryURL)
          }
        )
      }
    }
    
    #if DEBUG
    extension ClientStorage {
      public static let failing = ClientStorage(
        hasStoredClient: { false },
        createClient: {
          struct NotImplemented: Error {}
          throw NotImplemented()
        },
        loadClient: {
          struct NotImplemented: Error {}
          throw NotImplemented()
        },
        removeClient: {
          struct NotImplemented: Error {}
          throw NotImplemented()
        }
      )
    }
    #endif