Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/web3 #1473

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Mixin.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -851,9 +851,8 @@
94B2CD782B0F88D300D268E1 /* DepositView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 94B2CD772B0F88D300D268E1 /* DepositView.xib */; };
94B5776A2B5E554800AE576E /* ExploreViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94B577692B5E554800AE576E /* ExploreViewController.swift */; };
94B5776F2B5E555B00AE576E /* ExploreView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 94B5776E2B5E555B00AE576E /* ExploreView.xib */; };
94B577772B5E6E3700AE576E /* ExploreFavoriteViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94B577752B5E6E3700AE576E /* ExploreFavoriteViewController.swift */; };
94B5777D2B5E755100AE576E /* SeparatorHeaderFooterView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94B5777C2B5E755100AE576E /* SeparatorHeaderFooterView.swift */; };
94B577822B5E79B800AE576E /* ExploreAllAppsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94B577812B5E79B800AE576E /* ExploreAllAppsViewController.swift */; };
94B577822B5E79B800AE576E /* ExploreBotsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94B577812B5E79B800AE576E /* ExploreBotsViewController.swift */; };
94B577872B5EC11500AE576E /* ExploreAction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94B577862B5EC11500AE576E /* ExploreAction.swift */; };
94B61EC32B344A7A00BD39AF /* HomeTabBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94B61EC22B344A7A00BD39AF /* HomeTabBarController.swift */; };
94B6EDFE2B707F15001DCC50 /* WithdrawPreviewViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94B6EDFD2B707F15001DCC50 /* WithdrawPreviewViewController.swift */; };
Expand Down Expand Up @@ -929,6 +928,7 @@
94E619B92645BAA000A38049 /* SuspiciousLinkView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 94E619B82645BAA000A38049 /* SuspiciousLinkView.xib */; };
94E82DBC2BB54A1C00092249 /* LoadingIndicatorTableFooterView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 94E82DBB2BB54A1C00092249 /* LoadingIndicatorTableFooterView.xib */; };
94E8913925C019F000F1E5D4 /* Pods-Mixin-acknowledgements.plist in Resources */ = {isa = PBXBuildFile; fileRef = 94E8913825C019F000F1E5D4 /* Pods-Mixin-acknowledgements.plist */; };
94E9C0962BC6978F00D6157C /* Web3PopupCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94E9C0952BC6978F00D6157C /* Web3PopupCoordinator.swift */; };
94ECCCAB261A029F004E9E2A /* Window.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94ECCCAA261A029F004E9E2A /* Window.swift */; };
94ED08B02B5FAFC900493312 /* AppUserSearchResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94ED08AF2B5FAFC900493312 /* AppUserSearchResult.swift */; };
94ED08B62B5FB1C300493312 /* ExploreActionCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94ED08B42B5FB1C300493312 /* ExploreActionCell.swift */; };
Expand Down Expand Up @@ -2053,9 +2053,8 @@
94B2CD772B0F88D300D268E1 /* DepositView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = DepositView.xib; sourceTree = "<group>"; };
94B577692B5E554800AE576E /* ExploreViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExploreViewController.swift; sourceTree = "<group>"; };
94B5776E2B5E555B00AE576E /* ExploreView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ExploreView.xib; sourceTree = "<group>"; };
94B577752B5E6E3700AE576E /* ExploreFavoriteViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExploreFavoriteViewController.swift; sourceTree = "<group>"; };
94B5777C2B5E755100AE576E /* SeparatorHeaderFooterView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeparatorHeaderFooterView.swift; sourceTree = "<group>"; };
94B577812B5E79B800AE576E /* ExploreAllAppsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExploreAllAppsViewController.swift; sourceTree = "<group>"; };
94B577812B5E79B800AE576E /* ExploreBotsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExploreBotsViewController.swift; sourceTree = "<group>"; };
94B577862B5EC11500AE576E /* ExploreAction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExploreAction.swift; sourceTree = "<group>"; };
94B61EC22B344A7A00BD39AF /* HomeTabBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeTabBarController.swift; sourceTree = "<group>"; };
94B6EDFD2B707F15001DCC50 /* WithdrawPreviewViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WithdrawPreviewViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2127,6 +2126,7 @@
94E619B82645BAA000A38049 /* SuspiciousLinkView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = SuspiciousLinkView.xib; sourceTree = "<group>"; };
94E82DBB2BB54A1C00092249 /* LoadingIndicatorTableFooterView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = LoadingIndicatorTableFooterView.xib; sourceTree = "<group>"; };
94E8913825C019F000F1E5D4 /* Pods-Mixin-acknowledgements.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; name = "Pods-Mixin-acknowledgements.plist"; path = "Pods/Target Support Files/Pods-Mixin/Pods-Mixin-acknowledgements.plist"; sourceTree = SOURCE_ROOT; };
94E9C0952BC6978F00D6157C /* Web3PopupCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Web3PopupCoordinator.swift; sourceTree = "<group>"; };
94ECCCAA261A029F004E9E2A /* Window.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Window.swift; sourceTree = "<group>"; };
94ED08AF2B5FAFC900493312 /* AppUserSearchResult.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppUserSearchResult.swift; sourceTree = "<group>"; };
94ED08B42B5FB1C300493312 /* ExploreActionCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExploreActionCell.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3108,6 +3108,7 @@
942F4728299CDF1800528759 /* URLSessionWebSocketFactory.swift */,
942F472A299CDF8100528759 /* Web3SignerFactory.swift */,
94C0D7E829AD348500E372FC /* InPlaceKeyStorage.swift */,
94E9C0952BC6978F00D6157C /* Web3PopupCoordinator.swift */,
94E35CA2298B836300ADB40D /* WalletConnectService.swift */,
940764C229B2633C00B779A6 /* WalletConnectDecodedSigningRequest.swift */,
947C5E5A29B0FE9B00838458 /* WalletConnectSession.swift */,
Expand Down Expand Up @@ -3846,8 +3847,7 @@
9473F8272AE51097004E5739 /* RegisterToSafeViewController.swift */,
94B5776E2B5E555B00AE576E /* ExploreView.xib */,
94B577692B5E554800AE576E /* ExploreViewController.swift */,
94B577752B5E6E3700AE576E /* ExploreFavoriteViewController.swift */,
94B577812B5E79B800AE576E /* ExploreAllAppsViewController.swift */,
94B577812B5E79B800AE576E /* ExploreBotsViewController.swift */,
94B13FC12B5ED5F9001333BE /* ExploreSearchView.xib */,
94B13FC02B5ED5F9001333BE /* ExploreSearchViewController.swift */,
94CF1F262BA1B74A00BC52C1 /* Web3WalletViewController.swift */,
Expand Down Expand Up @@ -5296,8 +5296,7 @@
7B5E9B4A243740C4000AE24E /* ConversationCircleEditorFooterView.swift in Sources */,
7BFE47E22284394000FC4379 /* CheckmarkPeerCell.swift in Sources */,
7CDF316C29890FB200421808 /* ConversationFontSet.swift in Sources */,
94B577772B5E6E3700AE576E /* ExploreFavoriteViewController.swift in Sources */,
94B577822B5E79B800AE576E /* ExploreAllAppsViewController.swift in Sources */,
94B577822B5E79B800AE576E /* ExploreBotsViewController.swift in Sources */,
7B369208233A43C1007321A7 /* SegmentedControl.swift in Sources */,
9427D51A25E0D42E00B1EF0E /* MusicInfoView.swift in Sources */,
7C2AC42928F2B384005F369A /* DepositSuspendedView.swift in Sources */,
Expand Down Expand Up @@ -5431,6 +5430,7 @@
7B5A748F230274E200C6107E /* GroupIconMaker.swift in Sources */,
DFC0ED0923BB7D990091E7AC /* GiphyImage.swift in Sources */,
7CEB735E29DBC44A006FB5B2 /* DeviceTransferParticipant.swift in Sources */,
94E9C0962BC6978F00D6157C /* Web3PopupCoordinator.swift in Sources */,
7CEB735C29DBB737006FB5B2 /* DeviceTransferConversation.swift in Sources */,
7B0B01921FEA19BA000EEE4F /* DecryptionFailedMessageViewModel.swift in Sources */,
7B2E3E4B1FA07F4500DDDDEB /* SelectCountryViewController.swift in Sources */,
Expand Down
1 change: 1 addition & 0 deletions Mixin/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ extension AppDelegate {
WKWebsiteDataStore.default().removeAuthenticationRelatedData()
BackupJobQueue.shared.cancelAllOperations()
WalletConnectService.shared.disconnectAllSessions()
Web3PopupCoordinator.rejectAllPopups()

UIApplication.shared.setShortcutItemsEnabled(false)
UIApplication.shared.applicationIconBadgeNumber = 1
Expand Down
86 changes: 17 additions & 69 deletions Mixin/Service/Web3/WalletConnectService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ final class WalletConnectService {

private var subscribes = Set<AnyCancellable>()

// Only one request or proposal can be presented at a time
// New incoming requests will be rejected if `presentedViewController` is not nil
private weak var presentedViewController: UIViewController?

private init() {
Networking.configure(groupIdentifier: appGroupIdentifier,
projectId: MixinKeys.walletConnect,
Expand Down Expand Up @@ -109,40 +105,6 @@ final class WalletConnectService {
}
}

func presentRequest(viewController: UIViewController) {
guard let container = UIApplication.homeContainerViewController else {
return
}
let hasPendingRequest: Bool
if let presentedViewController {
// FIXME: Workaround for the issue that requests can't be processed. In some cases, even after the previous
// request has been dismissed, the presentedViewController remains non-nil, preventing the handling of
// subsequent requests. There might be a retain cycle somewhere inside the view controller.
hasPendingRequest = presentedViewController.presentingViewController != nil
if hasPendingRequest {
Logger.web3.warn(category: "Service", message: "Previous request not released")
}
} else {
hasPendingRequest = false
}
guard !hasPendingRequest else {
presentRejection(title: R.string.localizable.request_rejected(),
message: R.string.localizable.request_rejected_reason_another_request_in_process())
return
}
container.presentOnTopMostPresentedController(viewController, animated: true)
presentedViewController = viewController
}

func presentRejection(title: String, message: String) {
guard let container = UIApplication.homeContainerViewController else {
return
}
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: R.string.localizable.ok(), style: .cancel))
container.presentOnTopMostPresentedController(alert, animated: true)
}

private func reloadSessions(sessions: [WalletConnectSign.Session]) {
self.sessions = sessions.map(WalletConnectSession.init(session:))
let topics = self.sessions.map(\.topic)
Expand Down Expand Up @@ -300,8 +262,9 @@ extension WalletConnectService {
requiredNamespaces = requiredChains.joined(separator: ", ")
}
DispatchQueue.main.async {
self.presentRejection(title: "Chain not supported",
message: "\(proposal.proposer.name) requires to support \(requiredNamespaces)")
let title = "Chain not supported"
let message = "\(proposal.proposer.name) requires to support \(requiredNamespaces)"
Web3PopupCoordinator.enqueue(popup: .rejection(title: title, message: message))
}
Task {
try await Web3Wallet.instance.rejectSession(proposalId: proposal.id, reason: .unsupportedChains)
Expand All @@ -315,8 +278,9 @@ extension WalletConnectService {
logger.warn(category: "Service", message: "Requires to support \(proposalEvents)")
let events = proposalEvents.joined(separator: ", ")
DispatchQueue.main.async {
self.presentRejection(title: "Chain not supported",
message: "\(proposal.proposer.name) requires to support \(events))")
let title = "Chain not supported"
let message = "\(proposal.proposer.name) requires to support \(events)"
Web3PopupCoordinator.enqueue(popup: .rejection(title: title, message: message))
}
Task {
try await Web3Wallet.instance.rejectSession(proposalId: proposal.id, reason: .upsupportedEvents)
Expand All @@ -325,32 +289,15 @@ extension WalletConnectService {
}

let account: String? = PropertiesDAO.shared.value(forKey: .evmAddress)
if account == nil {
DispatchQueue.main.async {
let unlock = UnlockWeb3WalletViewController(chain: chains[0])
unlock.onDismiss = { isUnlocked in
if isUnlocked {
self.presentedViewController = nil // Value may not released immediately
let connectWallet = ConnectWalletViewController(proposal: proposal,
chains: chains.map(\.caip2),
events: Array(events))
self.presentRequest(viewController: connectWallet)
} else {
Task {
try await Web3Wallet.instance.rejectSession(proposalId: proposal.id, reason: .userRejected)
}
}
}
self.presentRequest(viewController: unlock)
}
} else {
logger.info(category: "Service", message: "Showing: \(proposal))")
DispatchQueue.main.async {
let connectWallet = ConnectWalletViewController(proposal: proposal,
chains: chains.map(\.caip2),
events: Array(events))
self.presentRequest(viewController: connectWallet)
DispatchQueue.main.async {
if account == nil {
let controller = UnlockWeb3WalletViewController(chain: chains[0])
Web3PopupCoordinator.enqueue(popup: .unlock(controller))
}
let connectWallet = ConnectWalletViewController(proposal: proposal,
chains: chains.map(\.caip2),
events: Array(events))
Web3PopupCoordinator.enqueue(popup: .request(connectWallet))
}
}
}
Expand All @@ -366,8 +313,9 @@ extension WalletConnectService {
let error = JSONRPCError(code: -1, message: "Missing session")
try await Web3Wallet.instance.respond(topic: topic, requestId: request.id, response: .error(error))
}
self.presentRejection(title: R.string.localizable.request_rejected(),
message: R.string.localizable.session_not_found())
let title = R.string.localizable.request_rejected()
let message = R.string.localizable.session_not_found()
Web3PopupCoordinator.enqueue(popup: .rejection(title: title, message: message))
}
}
}
Expand Down
Loading
Loading