clean up inpage rpc logic

This commit is contained in:
ivan grachev 2024-01-24 12:11:25 +03:00
parent e59e1f1d78
commit 6f6a042b38
4 changed files with 12 additions and 12 deletions

View File

@ -9,21 +9,19 @@ extension ResponseToExtension {
let result: String?
let results: [String]?
let chainId: String?
let rpcURL: String?
init(result: String) {
self.init(result: result, results: nil, chainId: nil, rpcURL: nil)
self.init(result: result, results: nil, chainId: nil)
}
init(results: [String], chainId: String, rpcURL: String) {
self.init(result: nil, results: results, chainId: chainId, rpcURL: rpcURL)
init(results: [String], chainId: String) {
self.init(result: nil, results: results, chainId: chainId)
}
private init(result: String?, results: [String]?, chainId: String?, rpcURL: String?) {
private init(result: String?, results: [String]?, chainId: String?) {
self.result = result
self.results = results
self.chainId = chainId
self.rpcURL = rpcURL
}
}

View File

@ -43,9 +43,9 @@ class SafariWebExtensionHandler: NSObject, NSExtensionRequestHandling {
let url = URL(string: "tokenary://safari?request=\(query)") {
if case let .ethereum(ethereumRequest) = request.body,
ethereumRequest.method == .switchEthereumChain || ethereumRequest.method == .addEthereumChain {
if let switchToChainId = ethereumRequest.switchToChainId, let rpcURL = Nodes.getNode(chainId: switchToChainId) {
if let switchToChainId = ethereumRequest.switchToChainId, Nodes.knowsNode(chainId: switchToChainId) {
let chainId = String.hex(switchToChainId, withPrefix: true)
let responseBody = ResponseToExtension.Ethereum(results: [ethereumRequest.address], chainId: chainId, rpcURL: rpcURL)
let responseBody = ResponseToExtension.Ethereum(results: [ethereumRequest.address], chainId: chainId)
let response = ResponseToExtension(for: request, body: .ethereum(responseBody))
respond(with: response.json, context: context)
} else {

View File

@ -14,6 +14,10 @@ struct Nodes {
}
}()
static func knowsNode(chainId: Int) -> Bool {
return getNode(chainId: chainId) != nil
}
static func getNode(chainId: Int) -> String? {
let https = "https://"
if let infura = BundledNodes.infura[chainId], let infuraKey = infuraKey {

View File

@ -49,9 +49,7 @@ struct DappRequestProcessor {
let account = specificWalletAccount.account
switch account.coin {
case .ethereum:
let responseBody = ResponseToExtension.Ethereum(results: [account.address],
chainId: chain.chainIdHexString,
rpcURL: chain.nodeURLString)
let responseBody = ResponseToExtension.Ethereum(results: [account.address], chainId: chain.chainIdHexString)
specificProviderBodies.append(.ethereum(responseBody))
default:
fatalError("Can't select that coin")
@ -92,7 +90,7 @@ struct DappRequestProcessor {
network: nil,
source: .safariExtension) { chain, specificWalletAccounts in
if let chain = chain, let specificWalletAccount = specificWalletAccounts?.first, specificWalletAccount.account.coin == .ethereum {
let responseBody = ResponseToExtension.Ethereum(results: [specificWalletAccount.account.address], chainId: chain.chainIdHexString, rpcURL: chain.nodeURLString)
let responseBody = ResponseToExtension.Ethereum(results: [specificWalletAccount.account.address], chainId: chain.chainIdHexString)
respond(to: request, body: .ethereum(responseBody), completion: completion)
} else {
respond(to: request, error: Strings.canceled, completion: completion)