95 lines
3.5 KiB
Swift
95 lines
3.5 KiB
Swift
//
|
|
// NetworkServiceAccounts.swift
|
|
// Wallet
|
|
//
|
|
// Created by Igor on 05.11.2020.
|
|
// Copyright © 2020 List. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import Alamofire
|
|
import EosioSwift
|
|
|
|
extension Network.Service.Accounts {
|
|
typealias Model = Network.Model.Account
|
|
}
|
|
|
|
extension Network.Service {
|
|
class Accounts: Common.Service.Provider {}
|
|
}
|
|
|
|
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) {
|
|
self.getAccount(account) {
|
|
switch $0 {
|
|
case let .success(response): completion(.init(response: response))
|
|
case .failure(_): completion(nil)
|
|
}
|
|
}
|
|
}
|
|
|
|
func getAccount(_ account: String, completion: @escaping (EosioResult<EosioRpcAccountResponse, EosioError>) -> Void) {
|
|
EosioRpcProvider(
|
|
endpoint: URL(string: Network.servers.current.node)!
|
|
).getAccount(requestParameters: EosioRpcAccountRequest(accountName: account), completion: completion)
|
|
}
|
|
|
|
func getChatPublicKey(_ account: String, completion: @escaping (Result<String, String>) -> Void) {
|
|
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))
|
|
}
|
|
}
|
|
}
|
|
|
|
func fetch(publicKey: String, completion: @escaping ([String]) -> Void) {
|
|
Network.servers.current.useScatter
|
|
? fetchScatter(publicKey: publicKey, completion: completion)
|
|
: fetchHyperion(publicKey: publicKey, completion: completion)
|
|
}
|
|
}
|
|
|
|
extension Network.Service.Accounts {
|
|
private struct Response: Codable {
|
|
let accountNames: [String]
|
|
enum CodingKeys: String, CodingKey {
|
|
case accountNames = "account_names"
|
|
}
|
|
}
|
|
private func fetchHyperion(publicKey: String, completion: @escaping ([String]) -> Void) {
|
|
AF.request(
|
|
"\(Network.servers.current.hyperion)/v2/state/get_key_accounts",
|
|
method: .get,
|
|
parameters: ["public_key": publicKey, "skip": 0, "limit": 5000]
|
|
).responseDecodable(of: Response.self) { completion($0.value?.accountNames ?? []) }
|
|
}
|
|
private func fetchScatter(publicKey: String, completion: @escaping ([String]) -> Void) {
|
|
AF.request(
|
|
"https://api.light.xeos.me/api/key/\(publicKey)",
|
|
method: .get
|
|
).responseDecodable(of: Response.self) { (response) in
|
|
guard
|
|
let data = response.data,
|
|
let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String:Any],
|
|
let accounts = (json["eos"] as? [String:Any])?["accounts"] as? NSDictionary
|
|
else { completion([]);return }
|
|
completion((accounts.allKeys as? [String]) ?? [])
|
|
}
|
|
}
|
|
}
|