From f5e3d114e53cf00b91f2e3e458ef78484eaf8532 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Mon, 19 Aug 2024 12:00:17 +0200 Subject: [PATCH 1/3] savepoint --- Example/DApp/Modules/Sign/SignPresenter.swift | 7 ++ Example/ExampleApp.xcodeproj/project.pbxproj | 4 -- .../ConnectionDetailsInteractor.swift | 9 --- .../ConnectionDetailsModule.swift | 2 - .../ConnectionDetailsPresenter.swift | 66 +++++++++++++++++-- .../ConnectionDetailsView.swift | 9 +++ 6 files changed, 77 insertions(+), 20 deletions(-) delete mode 100644 Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsInteractor.swift diff --git a/Example/DApp/Modules/Sign/SignPresenter.swift b/Example/DApp/Modules/Sign/SignPresenter.swift index 0b2e86e7d..85541d69e 100644 --- a/Example/DApp/Modules/Sign/SignPresenter.swift +++ b/Example/DApp/Modules/Sign/SignPresenter.swift @@ -169,6 +169,13 @@ extension SignPresenter { } .store(in: &subscriptions) + Sign.instance.sessionUpdatePublisher + .receive(on: DispatchQueue.main) + .sink { [unowned self] (topic, namespace) in + print(namespace) + } + .store(in: &subscriptions) + Sign.instance.authResponsePublisher .receive(on: DispatchQueue.main) .sink { [unowned self] response in diff --git a/Example/ExampleApp.xcodeproj/project.pbxproj b/Example/ExampleApp.xcodeproj/project.pbxproj index a984e0a53..f7829209b 100644 --- a/Example/ExampleApp.xcodeproj/project.pbxproj +++ b/Example/ExampleApp.xcodeproj/project.pbxproj @@ -314,7 +314,6 @@ C5DD5BE1294E09E3008FD3A4 /* Web3Wallet in Frameworks */ = {isa = PBXBuildFile; productRef = C5DD5BE0294E09E3008FD3A4 /* Web3Wallet */; }; C5F32A2C2954814200A6476E /* ConnectionDetailsModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5F32A2B2954814200A6476E /* ConnectionDetailsModule.swift */; }; C5F32A2E2954814A00A6476E /* ConnectionDetailsRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5F32A2D2954814A00A6476E /* ConnectionDetailsRouter.swift */; }; - C5F32A302954816100A6476E /* ConnectionDetailsInteractor.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5F32A2F2954816100A6476E /* ConnectionDetailsInteractor.swift */; }; C5F32A322954816C00A6476E /* ConnectionDetailsPresenter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5F32A312954816C00A6476E /* ConnectionDetailsPresenter.swift */; }; C5F32A342954817600A6476E /* ConnectionDetailsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C5F32A332954817600A6476E /* ConnectionDetailsView.swift */; }; C5F32A362954FE3C00A6476E /* Colors.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C5F32A352954FE3C00A6476E /* Colors.xcassets */; }; @@ -654,7 +653,6 @@ C5BE021A2AF79B960064FC88 /* SessionAccountModule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SessionAccountModule.swift; sourceTree = ""; }; C5F32A2B2954814200A6476E /* ConnectionDetailsModule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionDetailsModule.swift; sourceTree = ""; }; C5F32A2D2954814A00A6476E /* ConnectionDetailsRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionDetailsRouter.swift; sourceTree = ""; }; - C5F32A2F2954816100A6476E /* ConnectionDetailsInteractor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionDetailsInteractor.swift; sourceTree = ""; }; C5F32A312954816C00A6476E /* ConnectionDetailsPresenter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionDetailsPresenter.swift; sourceTree = ""; }; C5F32A332954817600A6476E /* ConnectionDetailsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionDetailsView.swift; sourceTree = ""; }; C5F32A352954FE3C00A6476E /* Colors.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Colors.xcassets; sourceTree = ""; }; @@ -1805,7 +1803,6 @@ children = ( C5F32A2B2954814200A6476E /* ConnectionDetailsModule.swift */, C5F32A2D2954814A00A6476E /* ConnectionDetailsRouter.swift */, - C5F32A2F2954816100A6476E /* ConnectionDetailsInteractor.swift */, C5F32A312954816C00A6476E /* ConnectionDetailsPresenter.swift */, C5F32A332954817600A6476E /* ConnectionDetailsView.swift */, ); @@ -2533,7 +2530,6 @@ C55D34AF2965FB750004314A /* SessionProposalPresenter.swift in Sources */, A5D610D22AB35B1100C20083 /* Listings.swift in Sources */, A5B4F7C82ABB21190099AF7C /* CacheAsyncImage.swift in Sources */, - C5F32A302954816100A6476E /* ConnectionDetailsInteractor.swift in Sources */, 847BD1E8298A806800076C90 /* NotificationsView.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsInteractor.swift b/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsInteractor.swift deleted file mode 100644 index aafd41678..000000000 --- a/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsInteractor.swift +++ /dev/null @@ -1,9 +0,0 @@ -import Combine - -import Web3Wallet - -final class ConnectionDetailsInteractor { - func disconnectSession(session: Session) async throws { - try await Web3Wallet.instance.disconnect(topic: session.topic) - } -} diff --git a/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsModule.swift b/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsModule.swift index d42807d98..860400ab0 100644 --- a/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsModule.swift +++ b/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsModule.swift @@ -6,9 +6,7 @@ final class ConnectionDetailsModule { @discardableResult static func create(app: Application, session: Session) -> UIViewController { let router = ConnectionDetailsRouter(app: app) - let interactor = ConnectionDetailsInteractor() let presenter = ConnectionDetailsPresenter( - interactor: interactor, router: router, session: session ) diff --git a/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsPresenter.swift b/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsPresenter.swift index 50d6f1d59..084f6fd18 100644 --- a/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsPresenter.swift +++ b/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsPresenter.swift @@ -4,7 +4,6 @@ import Combine import Web3Wallet final class ConnectionDetailsPresenter: ObservableObject { - private let interactor: ConnectionDetailsInteractor private let router: ConnectionDetailsRouter let session: Session @@ -12,11 +11,9 @@ final class ConnectionDetailsPresenter: ObservableObject { private var disposeBag = Set() init( - interactor: ConnectionDetailsInteractor, router: ConnectionDetailsRouter, session: Session ) { - self.interactor = interactor self.router = router self.session = session } @@ -25,7 +22,7 @@ final class ConnectionDetailsPresenter: ObservableObject { Task { do { ActivityIndicatorManager.shared.start() - try await interactor.disconnectSession(session: session) + try await Web3Wallet.instance.disconnect(topic: session.topic) ActivityIndicatorManager.shared.stop() DispatchQueue.main.async { self.router.dismiss() @@ -36,10 +33,69 @@ final class ConnectionDetailsPresenter: ObservableObject { } } } - + + func accountReferences(namespace: String) -> [String] { session.namespaces[namespace]?.accounts.map { "\($0.namespace):\(($0.reference))" } ?? [] } + + + func createUpdatedSessionNamespaces(existingNamespaces: [String: SessionNamespace]) -> [String: SessionNamespace] { + // Define the Arbitrum chain identifier + let arbitrumChainIdentifier = "eip155:1" + let arbitrumChain = Blockchain(arbitrumChainIdentifier)! + + var updatedNamespaces = existingNamespaces + + for (key, namespace) in existingNamespaces { + // Use the address of the first account in the namespace + let newAccountAddress = namespace.accounts.first!.address + let newAccount = Account(chainIdentifier: arbitrumChainIdentifier, address: newAccountAddress)! + + var updatedChains = namespace.chains ?? [] + var updatedAccounts = namespace.accounts + + // Ensure Arbitrum chain is at the first position + if let index = updatedChains.firstIndex(of: arbitrumChain) { + updatedChains.remove(at: index) + } + updatedChains.insert(arbitrumChain, at: 0) + + // Ensure the new account for Arbitrum is at the first position + if let index = updatedAccounts.firstIndex(of: newAccount) { + updatedAccounts.remove(at: index) + } + updatedAccounts.insert(newAccount, at: 0) + + // Update the session namespace with the modified chains and accounts + let updatedNamespace = SessionNamespace(chains: updatedChains, accounts: updatedAccounts, methods: namespace.methods, events: namespace.events) + updatedNamespaces[key] = updatedNamespace + } + + return updatedNamespaces + } + + func onUpdate() { + Task { + do { + ActivityIndicatorManager.shared.start() + + let existingNamespaces = session.namespaces + + let updatedNamespaces = createUpdatedSessionNamespaces(existingNamespaces: existingNamespaces) + + try await Web3Wallet.instance.update(topic: session.topic, namespaces: updatedNamespaces) + + ActivityIndicatorManager.shared.stop() + DispatchQueue.main.async { + self.router.dismiss() + } + } catch { + ActivityIndicatorManager.shared.stop() + print(error) + } + } + } } // MARK: - Private functions diff --git a/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsView.swift b/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsView.swift index 5174bc80e..6245ae88f 100644 --- a/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsView.swift +++ b/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsView.swift @@ -184,6 +184,15 @@ struct ConnectionDetailsView: View { .padding(.horizontal, 20) .padding(.top, 30) + Button { + presenter.onUpdate() + } label: { + Text("Update") + .foregroundColor(.grey50) + .font(.system(size: 20, weight: .semibold, design: .rounded)) + } + .padding(.top, 20) + Button { presenter.onDelete() } label: { From 80f15e0f530773641a1381fd890147cd2daa4139 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Mon, 19 Aug 2024 12:20:16 +0200 Subject: [PATCH 2/3] emit event --- .../ConnectionDetailsPresenter.swift | 47 +------------------ .../ConnectionDetailsView.swift | 4 +- 2 files changed, 4 insertions(+), 47 deletions(-) diff --git a/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsPresenter.swift b/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsPresenter.swift index 084f6fd18..9ca9a33aa 100644 --- a/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsPresenter.swift +++ b/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsPresenter.swift @@ -39,57 +39,14 @@ final class ConnectionDetailsPresenter: ObservableObject { session.namespaces[namespace]?.accounts.map { "\($0.namespace):\(($0.reference))" } ?? [] } - - func createUpdatedSessionNamespaces(existingNamespaces: [String: SessionNamespace]) -> [String: SessionNamespace] { - // Define the Arbitrum chain identifier - let arbitrumChainIdentifier = "eip155:1" - let arbitrumChain = Blockchain(arbitrumChainIdentifier)! - - var updatedNamespaces = existingNamespaces - - for (key, namespace) in existingNamespaces { - // Use the address of the first account in the namespace - let newAccountAddress = namespace.accounts.first!.address - let newAccount = Account(chainIdentifier: arbitrumChainIdentifier, address: newAccountAddress)! - - var updatedChains = namespace.chains ?? [] - var updatedAccounts = namespace.accounts - - // Ensure Arbitrum chain is at the first position - if let index = updatedChains.firstIndex(of: arbitrumChain) { - updatedChains.remove(at: index) - } - updatedChains.insert(arbitrumChain, at: 0) - - // Ensure the new account for Arbitrum is at the first position - if let index = updatedAccounts.firstIndex(of: newAccount) { - updatedAccounts.remove(at: index) - } - updatedAccounts.insert(newAccount, at: 0) - - // Update the session namespace with the modified chains and accounts - let updatedNamespace = SessionNamespace(chains: updatedChains, accounts: updatedAccounts, methods: namespace.methods, events: namespace.events) - updatedNamespaces[key] = updatedNamespace - } - - return updatedNamespaces - } - - func onUpdate() { + func changeForMainnet() { Task { do { ActivityIndicatorManager.shared.start() - let existingNamespaces = session.namespaces - - let updatedNamespaces = createUpdatedSessionNamespaces(existingNamespaces: existingNamespaces) - - try await Web3Wallet.instance.update(topic: session.topic, namespaces: updatedNamespaces) + try await Web3Wallet.instance.emit(topic: session.topic, event: Session.Event(name: "chainChanged", data: AnyCodable("1")), chainId: Blockchain("eip155:1")!) ActivityIndicatorManager.shared.stop() - DispatchQueue.main.async { - self.router.dismiss() - } } catch { ActivityIndicatorManager.shared.stop() print(error) diff --git a/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsView.swift b/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsView.swift index 6245ae88f..86fcf1020 100644 --- a/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsView.swift +++ b/Example/WalletApp/PresentationLayer/Wallet/ConnectionDetails/ConnectionDetailsView.swift @@ -185,9 +185,9 @@ struct ConnectionDetailsView: View { .padding(.top, 30) Button { - presenter.onUpdate() + presenter.changeForMainnet() } label: { - Text("Update") + Text("Change for Mainnet") .foregroundColor(.grey50) .font(.system(size: 20, weight: .semibold, design: .rounded)) } From 8cf911b48d277d2e237543bb5e83698a3aee41cc Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Mon, 19 Aug 2024 12:21:48 +0200 Subject: [PATCH 3/3] remove print --- Example/DApp/Modules/Sign/SignPresenter.swift | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Example/DApp/Modules/Sign/SignPresenter.swift b/Example/DApp/Modules/Sign/SignPresenter.swift index 85541d69e..0b2e86e7d 100644 --- a/Example/DApp/Modules/Sign/SignPresenter.swift +++ b/Example/DApp/Modules/Sign/SignPresenter.swift @@ -169,13 +169,6 @@ extension SignPresenter { } .store(in: &subscriptions) - Sign.instance.sessionUpdatePublisher - .receive(on: DispatchQueue.main) - .sink { [unowned self] (topic, namespace) in - print(namespace) - } - .store(in: &subscriptions) - Sign.instance.authResponsePublisher .receive(on: DispatchQueue.main) .sink { [unowned self] response in