From bca590910aa560a3c0a6df423d058fbc24231b0f Mon Sep 17 00:00:00 2001 From: ivan grachev Date: Wed, 1 Nov 2023 13:25:56 +0300 Subject: [PATCH] add networks source --- Shared/Ethereum/EthereumNetwork.swift | 25 ++++------------------ Shared/Ethereum/Networks.swift | 22 +++++++++++++++++++ Shared/Services/DappRequestProcessor.swift | 4 ++-- Tokenary.xcodeproj/project.pbxproj | 8 ++++++- 4 files changed, 35 insertions(+), 24 deletions(-) create mode 100644 Shared/Ethereum/Networks.swift diff --git a/Shared/Ethereum/EthereumNetwork.swift b/Shared/Ethereum/EthereumNetwork.swift index 5cb27165..09ce371a 100644 --- a/Shared/Ethereum/EthereumNetwork.swift +++ b/Shared/Ethereum/EthereumNetwork.swift @@ -10,27 +10,10 @@ struct EthereumNetwork: Codable, Equatable { let nodeURLString: String var symbolIsETH: Bool { return symbol == "ETH" } - var hasUSDPrice: Bool { return chainId == 1 } // TODO: list more chains with usd price + var hasUSDPrice: Bool { return chainId == EthereumNetwork.ethMainnetChainId } // TODO: list more chains with usd price var chainIdHexString: String { String.hex(chainId, withPrefix: true) } - var isEthMainnet: Bool { return chainId == 1 } - -} - -extension EthereumNetwork { - - static var ethereum: EthereumNetwork { - return withChainId(1)! - } - - static func withChainId(_ chainId: Int?) -> EthereumNetwork? { - guard let chainId = chainId else { return nil } - // TODO: get from json / defaults / etc - // TODO: initialize infura urls correctly (adding api key) - return EthereumNetwork(chainId: chainId, name: "", symbol: "", nodeURLString: "") - } - - static func withChainIdHex(_ chainIdHex: String?) -> EthereumNetwork? { - return nil // TODO: implement - } + var isEthMainnet: Bool { return chainId == EthereumNetwork.ethMainnetChainId } + + static let ethMainnetChainId = 1 } diff --git a/Shared/Ethereum/Networks.swift b/Shared/Ethereum/Networks.swift new file mode 100644 index 00000000..be5fed94 --- /dev/null +++ b/Shared/Ethereum/Networks.swift @@ -0,0 +1,22 @@ +// Copyright © 2023 Tokenary. All rights reserved. + +import Foundation + +struct Networks { + + static var ethereum: EthereumNetwork { + return withChainId(EthereumNetwork.ethMainnetChainId)! + } + + static func withChainId(_ chainId: Int?) -> EthereumNetwork? { + guard let chainId = chainId else { return nil } + // TODO: get from json / defaults / etc + // TODO: initialize infura urls correctly (adding api key) + return EthereumNetwork(chainId: chainId, name: "", symbol: "", nodeURLString: "") + } + + static func withChainIdHex(_ chainIdHex: String?) -> EthereumNetwork? { + return nil // TODO: implement + } + +} diff --git a/Shared/Services/DappRequestProcessor.swift b/Shared/Services/DappRequestProcessor.swift index 6bb5f036..ba047955 100644 --- a/Shared/Services/DappRequestProcessor.swift +++ b/Shared/Services/DappRequestProcessor.swift @@ -28,7 +28,7 @@ struct DappRequestProcessor { return walletsManager.getSpecificAccount(coin: coin, address: configuration.address) } let chainId = body.providerConfigurations.compactMap { $0.chainId }.first - let network = EthereumNetwork.withChainIdHex(chainId) + let network = Networks.withChainIdHex(chainId) let initiallyConnectedProviders = Set(body.providerConfigurations.map { $0.provider }) let action = SelectAccountAction(peer: request.peerMeta, coinType: nil, @@ -141,7 +141,7 @@ struct DappRequestProcessor { case .signTransaction: if let transaction = ethereumRequest.transaction, let chainId = ethereumRequest.currentChainId, - let chain = EthereumNetwork.withChainId(chainId), + let chain = Networks.withChainId(chainId), let account = account, let privateKey = privateKey { let action = SendTransactionAction(provider: request.provider, diff --git a/Tokenary.xcodeproj/project.pbxproj b/Tokenary.xcodeproj/project.pbxproj index ebbf4949..0b8cb434 100644 --- a/Tokenary.xcodeproj/project.pbxproj +++ b/Tokenary.xcodeproj/project.pbxproj @@ -191,6 +191,8 @@ 2CED86B82AF18192006F9E26 /* Nodes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CED86B52AF17D5A006F9E26 /* Nodes.swift */; }; 2CED86BA2AF1820E006F9E26 /* NodesService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CED86B92AF1820E006F9E26 /* NodesService.swift */; }; 2CED86BB2AF1820E006F9E26 /* NodesService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CED86B92AF1820E006F9E26 /* NodesService.swift */; }; + 2CED86BF2AF25C32006F9E26 /* Networks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CED86BE2AF25C32006F9E26 /* Networks.swift */; }; + 2CED86C02AF25C32006F9E26 /* Networks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2CED86BE2AF25C32006F9E26 /* Networks.swift */; }; 2CEFEB16274D5DCA00CE23BD /* inpage.js in Resources */ = {isa = PBXBuildFile; fileRef = 2CEFEB15274D5DC900CE23BD /* inpage.js */; }; 2CF25597275A46D300AE54B9 /* Defaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C528A15267FA8EB00CA3ADD /* Defaults.swift */; }; 2CF25598275A46D600AE54B9 /* Strings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C901C492689F01700D0926A /* Strings.swift */; }; @@ -407,6 +409,7 @@ 2CED86AF2AF0167F006F9E26 /* EIP155ChainData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EIP155ChainData.swift; sourceTree = ""; }; 2CED86B52AF17D5A006F9E26 /* Nodes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Nodes.swift; sourceTree = ""; }; 2CED86B92AF1820E006F9E26 /* NodesService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NodesService.swift; sourceTree = ""; }; + 2CED86BE2AF25C32006F9E26 /* Networks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Networks.swift; sourceTree = ""; }; 2CEFEB15274D5DC900CE23BD /* inpage.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = inpage.js; sourceTree = ""; }; 2CF255B3275A744000AE54B9 /* PasswordViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PasswordViewController.swift; sourceTree = ""; }; 2CF255B5275A746000AE54B9 /* AccountsListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountsListViewController.swift; sourceTree = ""; }; @@ -654,9 +657,10 @@ isa = PBXGroup; children = ( 2C1995552674D0F300A8E370 /* Ethereum.swift */, + 2CE3D011267F73C00032A62E /* Transaction.swift */, 2C8944CA2AEB0C10006A711F /* EthereumRPC.swift */, 2C9F0B6726BDCB2E008FA3D6 /* EthereumNetwork.swift */, - 2CE3D011267F73C00032A62E /* Transaction.swift */, + 2CED86BE2AF25C32006F9E26 /* Networks.swift */, ); path = Ethereum; sourceTree = ""; @@ -1328,6 +1332,7 @@ 2C264BC127B2F2C100234393 /* EthereumSafariRequest.swift in Sources */, 2C264BD027B2F30C00234393 /* UnknownSafariRequest.swift in Sources */, 2CED86B82AF18192006F9E26 /* Nodes.swift in Sources */, + 2CED86BF2AF25C32006F9E26 /* Networks.swift in Sources */, 2C134BA627553EC500DAFBDB /* Browser.swift in Sources */, 2CC89471269A334A00879245 /* UserDefaults.swift in Sources */, 2C78F8282683BDCC00C10670 /* Alert.swift in Sources */, @@ -1405,6 +1410,7 @@ 2CF255A7275A48BB00AE54B9 /* PriceService.swift in Sources */, 2CF25598275A46D600AE54B9 /* Strings.swift in Sources */, 2C4768B52826ED83005E8D4D /* CoinType.swift in Sources */, + 2CED86C02AF25C32006F9E26 /* Networks.swift in Sources */, 2CF255AE275A48CF00AE54B9 /* Transaction.swift in Sources */, 2C96D38F2762317300687301 /* AccountTableViewCell.swift in Sources */, 2C264BEC27B6B50700234393 /* DappRequestProcessor.swift in Sources */,