keep latest infura key

This commit is contained in:
ivan grachev 2023-11-07 20:18:29 +03:00
parent e1ac601a16
commit 6694536e0f
3 changed files with 18 additions and 10 deletions

View File

@ -10,6 +10,15 @@ struct ExtensionBridge {
return String(id)
}
static var defaultInfuraKeys: [String]? {
get {
return defaults?.array(forKey: "defaultInfuraKeys") as? [String]
}
set {
defaults?.set(newValue, forKey: "defaultInfuraKeys")
}
}
private static var initiatedRequests: Set<Int> {
get {
Set(defaults?.array(forKey: "initiatedRequests") as? [Int] ?? [])

View File

@ -5,13 +5,14 @@ import Foundation
struct Nodes {
private static let infuraKey: String = {
if let path = Bundle.main.path(forResource: "shared", ofType: "plist"),
if let latest = ExtensionBridge.defaultInfuraKeys?.first, !latest.isEmpty {
return latest
} else if let path = Bundle.main.path(forResource: "shared", ofType: "plist"),
let dict = NSDictionary(contentsOfFile: path) as? [String: AnyObject],
let infuraKey = dict["InfuraKey"] as? String, !infuraKey.isEmpty {
return infuraKey
} else {
// TODO: hande infura key not being bundled
return ""
return "" // TODO: return nil instead
}
}()

View File

@ -26,9 +26,8 @@ class ConfigurationService {
if !shouldUpdateApp {
getConfiguration()
}
getInfuraKeyFromCloudKit { infuraKey in
// TODO: keep latest version in defaults and make it accessible to Nodes
print(infuraKey)
getInfuraKeysFromCloudKit { infuraKeys in
ExtensionBridge.defaultInfuraKeys = infuraKeys
}
}
@ -50,7 +49,7 @@ class ConfigurationService {
dataTask.resume()
}
private func getInfuraKeyFromCloudKit(completion: @escaping (String) -> Void) {
private func getInfuraKeysFromCloudKit(completion: @escaping ([String]) -> Void) {
let container = CKContainer(identifier: "iCloud.tokenary")
let publicDatabase = container.publicCloudDatabase
let predicate = NSPredicate(value: true)
@ -58,10 +57,9 @@ class ConfigurationService {
publicDatabase.fetch(withQuery: query) { result in
if case .success(let success) = result,
case let .success(record) = success.matchResults.first?.1,
let infuraKeys = record["infuraKeys"] as? [String],
let infuraKey = infuraKeys.first {
let infuraKeys = record["infuraKeys"] as? [String], infuraKeys.first?.isEmpty == false {
DispatchQueue.main.async {
completion(infuraKey)
completion(infuraKeys)
}
}
}