// // NetworkServiceAccounts.swift // Wallet // // Created by Igor on 05.11.2020. // Copyright © 2020 List. All rights reserved. // import Foundation import Alamofire import EosioSwift import WalletNetwork extension Network.Service.Accounts { typealias Model = Network.Model.Account } extension Network.Service { final class Accounts: Common.Service.Provider { private var service: EOSService? } } extension Network.Service.Accounts { func exists(account: String, completion: @escaping (Bool) -> Void) { self.account(account) { completion($0 != nil) } } func account(_ account: String, completion: @escaping (Model?) -> Void) { if !self.service.isExist { guard let environment = try? ApplicationEnvironment.shared().current.networkEnvironment() else { completion(nil) return } self.service = EOSService(environment: environment) } self.service?.getAccount(account) { switch $0 { case let .success(response): completion(.init(response: response)) case .failure(_): completion(nil) } } } func getChatPublicKey(_ account: String, completion: @escaping (Result) -> Void) { if !self.service.isExist { guard let environment = try? ApplicationEnvironment.shared().current.networkEnvironment() else { completion(.failure("Cannot find me.chat or active keys for account \(account)")) return } self.service = EOSService(environment: environment) } self.service?.getAccount(account) { switch $0 { case let .success(account): let receiverPublicKey = account.permissions.filter({ $0.permName.lowercased() == "me.chat" }).first?.requiredAuth.keys.first?.key ?? account.permissions.filter({ $0.permName.lowercased() == "active" }).first?.requiredAuth.keys.first?.key ?? "" if receiverPublicKey.isEmpty { completion(.failure("Cannot find me.chat or active keys for account \(account)")) } else { completion(.success(receiverPublicKey)) } case let .failure(error): completion(.failure(error.description)) } } } }