From 44d4977b25b39489520d4406d93f8c0236547fe7 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Wed, 29 May 2024 13:20:22 +0200 Subject: [PATCH 01/10] integrate 1click auth --- .../WalletConnectPairing/PairingClient.swift | 12 ----------- .../WalletConnectSign/Sign/SignClient.swift | 21 ------------------- .../Sign/SignClientProtocol.swift | 2 ++ 3 files changed, 2 insertions(+), 33 deletions(-) diff --git a/Sources/WalletConnectPairing/PairingClient.swift b/Sources/WalletConnectPairing/PairingClient.swift index ead5d85be..44e79f7b2 100644 --- a/Sources/WalletConnectPairing/PairingClient.swift +++ b/Sources/WalletConnectPairing/PairingClient.swift @@ -2,9 +2,6 @@ import Foundation import Combine public class PairingClient: PairingRegisterer, PairingInteracting, PairingClientProtocol { - enum Errors: Error { - case pairingDoesNotSupportRequiredMethod - } public var pingResponsePublisher: AnyPublisher<(String), Never> { pingResponsePublisherSubject.eraseToAnyPublisher() } @@ -139,15 +136,6 @@ public class PairingClient: PairingRegisterer, PairingInteracting, PairingClient _ = try pairingsProvider.getPairing(for: topic) } - public func validateMethodSupport(topic: String, method: String) throws { - _ = try pairingsProvider.getPairing(for: topic) - let pairing = pairingStorage.getPairing(forTopic: topic) - guard let methods = pairing?.methods, - methods.contains(method) else { - throw Errors.pairingDoesNotSupportRequiredMethod - } - } - public func register(method: ProtocolMethod) -> AnyPublisher, Never> { logger.debug("Pairing Client - registering for \(method.method)") return pairingRequestsSubscriber.subscribeForRequest(method) diff --git a/Sources/WalletConnectSign/Sign/SignClient.swift b/Sources/WalletConnectSign/Sign/SignClient.swift index be4339671..5694955c3 100644 --- a/Sources/WalletConnectSign/Sign/SignClient.swift +++ b/Sources/WalletConnectSign/Sign/SignClient.swift @@ -320,27 +320,6 @@ public final class SignClient: SignClientProtocol { //---------------------------------------AUTH----------------------------------- - /// For a dApp to propose an authenticated session to a wallet. - /// Function will propose a session on existing pairing. - public func authenticate( - _ params: AuthRequestParams, - topic: String - ) async throws { - try pairingClient.validatePairingExistance(topic) - try pairingClient.validateMethodSupport(topic: topic, method: SessionAuthenticatedProtocolMethod().method) - logger.debug("Requesting Authentication on existing pairing") - try await appRequestService.request(params: params, topic: topic) - - let namespaces = try ProposalNamespaceBuilder.buildNamespace(from: params) - try await appProposeService.propose( - pairingTopic: topic, - namespaces: [:], - optionalNamespaces: namespaces, - sessionProperties: nil, - relay: RelayProtocolOptions(protocol: "irn", data: nil) - ) - } - /// For a dApp to propose an authenticated session to a wallet. public func authenticate( _ params: AuthRequestParams, diff --git a/Sources/WalletConnectSign/Sign/SignClientProtocol.swift b/Sources/WalletConnectSign/Sign/SignClientProtocol.swift index 292579f16..f74e2c9c7 100644 --- a/Sources/WalletConnectSign/Sign/SignClientProtocol.swift +++ b/Sources/WalletConnectSign/Sign/SignClientProtocol.swift @@ -19,8 +19,10 @@ public protocol SignClientProtocol { var requestExpirationPublisher: AnyPublisher { get } func connect(requiredNamespaces: [String: ProposalNamespace], optionalNamespaces: [String: ProposalNamespace]?, sessionProperties: [String: String]?, topic: String) async throws + func request(params: Request) async throws func approve(proposalId: String, namespaces: [String: SessionNamespace], sessionProperties: [String: String]?) async throws -> Session + func authenticate(_ params: AuthRequestParams, walletUniversalLink: String?) async throws -> WalletConnectURI? func rejectSession(proposalId: String, reason: RejectionReason) async throws func rejectSession(requestId: RPCID) async throws func update(topic: String, namespaces: [String: SessionNamespace]) async throws From e56430b11ed7d93187fda7f76630f0734e57af6f Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Wed, 29 May 2024 14:43:36 +0200 Subject: [PATCH 02/10] remove force unwrap --- Sources/WalletConnectModal/Modal/Screens/WalletList.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/WalletConnectModal/Modal/Screens/WalletList.swift b/Sources/WalletConnectModal/Modal/Screens/WalletList.swift index 96efd6a13..0180d4d35 100644 --- a/Sources/WalletConnectModal/Modal/Screens/WalletList.swift +++ b/Sources/WalletConnectModal/Modal/Screens/WalletList.swift @@ -215,7 +215,7 @@ struct WalletList: View { .stroke(.gray.opacity(0.4), lineWidth: 1) ) - Text(String(wallet.name.split(separator: " ").first!)) + Text(String(wallet.name.split(separator: " ").first ?? " ")) .font(.system(size: 12)) .foregroundColor(.foreground1) .multilineTextAlignment(.center) From a575e09ed7ad12e3ac6466f0b755c0020851c6a0 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Wed, 29 May 2024 15:29:35 +0200 Subject: [PATCH 03/10] not display error alert on session request --- Example/WalletApp/ApplicationLayer/SceneDelegate.swift | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Example/WalletApp/ApplicationLayer/SceneDelegate.swift b/Example/WalletApp/ApplicationLayer/SceneDelegate.swift index 0edf0a1ea..c3b5b2ab3 100644 --- a/Example/WalletApp/ApplicationLayer/SceneDelegate.swift +++ b/Example/WalletApp/ApplicationLayer/SceneDelegate.swift @@ -83,6 +83,12 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate, UNUserNotificatio if case WalletConnectURI.Errors.expired = error { AlertPresenter.present(message: error.localizedDescription, type: .error) } else { + guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false), + let queryItems = components.queryItems, + queryItems.contains(where: { $0.name == "wc_ev" }) else { + return + } + do { try Web3Wallet.instance.dispatchEnvelope(url.absoluteString) } catch { From 6bbf71a079138d05a94897bcdde256ce18c43c24 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Fri, 31 May 2024 11:04:09 +0200 Subject: [PATCH 04/10] add 1click auth w3m --- Example/DApp/SceneDelegate.swift | 5 +++-- Example/ExampleApp.xcodeproj/project.pbxproj | 2 +- .../xcshareddata/swiftpm/Package.resolved | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Example/DApp/SceneDelegate.swift b/Example/DApp/SceneDelegate.swift index 61cdec4c0..e2710ba6b 100644 --- a/Example/DApp/SceneDelegate.swift +++ b/Example/DApp/SceneDelegate.swift @@ -46,14 +46,15 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { projectId: InputConfig.projectId, metadata: metadata, crypto: DefaultCryptoProvider(), - customWallets: [ + authRequestParams: .stub(), customWallets: [ .init( id: "swift-sample", name: "Swift Sample Wallet", homepage: "https://walletconnect.com/", imageUrl: "https://avatars.githubusercontent.com/u/37784886?s=200&v=4", order: 1, - mobileLink: "walletapp://" + mobileLink: "walletapp://", + linkMode: "https://lab.web3modal.com/dapp" ) ] ) diff --git a/Example/ExampleApp.xcodeproj/project.pbxproj b/Example/ExampleApp.xcodeproj/project.pbxproj index 0ec6a24f9..b6ca472c5 100644 --- a/Example/ExampleApp.xcodeproj/project.pbxproj +++ b/Example/ExampleApp.xcodeproj/project.pbxproj @@ -3280,7 +3280,7 @@ repositoryURL = "https://github.com/WalletConnect/web3modal-swift"; requirement = { kind = revision; - revision = c73ce390bc249af155b7320346b7045e53b89866; + revision = 4b746f76d524f5ba9279e6ec3c33014a43bae244; }; }; A5434021291E6A270068F706 /* XCRemoteSwiftPackageReference "solana-swift" */ = { diff --git a/Example/ExampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Example/ExampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 84752135e..b400440f5 100644 --- a/Example/ExampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Example/ExampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -186,7 +186,7 @@ "repositoryURL": "https://github.com/WalletConnect/web3modal-swift", "state": { "branch": null, - "revision": "c73ce390bc249af155b7320346b7045e53b89866", + "revision": "4b746f76d524f5ba9279e6ec3c33014a43bae244", "version": null } } From fb85cd3c460114e2149b4846557276c4e8f97d80 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Fri, 31 May 2024 13:33:14 +0200 Subject: [PATCH 05/10] fix redirect issue --- Example/DApp/SceneDelegate.swift | 2 +- Example/ExampleApp.xcodeproj/project.pbxproj | 2 +- .../project.xcworkspace/xcshareddata/swiftpm/Package.resolved | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Example/DApp/SceneDelegate.swift b/Example/DApp/SceneDelegate.swift index e2710ba6b..89b4712d4 100644 --- a/Example/DApp/SceneDelegate.swift +++ b/Example/DApp/SceneDelegate.swift @@ -54,7 +54,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { imageUrl: "https://avatars.githubusercontent.com/u/37784886?s=200&v=4", order: 1, mobileLink: "walletapp://", - linkMode: "https://lab.web3modal.com/dapp" + linkMode: "https://lab.web3modal.com/wallet" ) ] ) diff --git a/Example/ExampleApp.xcodeproj/project.pbxproj b/Example/ExampleApp.xcodeproj/project.pbxproj index b6ca472c5..663d23895 100644 --- a/Example/ExampleApp.xcodeproj/project.pbxproj +++ b/Example/ExampleApp.xcodeproj/project.pbxproj @@ -3280,7 +3280,7 @@ repositoryURL = "https://github.com/WalletConnect/web3modal-swift"; requirement = { kind = revision; - revision = 4b746f76d524f5ba9279e6ec3c33014a43bae244; + revision = d84d37a0cda748bd69db340065f464b859c38158; }; }; A5434021291E6A270068F706 /* XCRemoteSwiftPackageReference "solana-swift" */ = { diff --git a/Example/ExampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Example/ExampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index b400440f5..db719c925 100644 --- a/Example/ExampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Example/ExampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -186,7 +186,7 @@ "repositoryURL": "https://github.com/WalletConnect/web3modal-swift", "state": { "branch": null, - "revision": "4b746f76d524f5ba9279e6ec3c33014a43bae244", + "revision": "d84d37a0cda748bd69db340065f464b859c38158", "version": null } } From de6bf8e25fa062d76281d77a0f0d1d63fba5d6cb Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Mon, 3 Jun 2024 08:52:13 +0200 Subject: [PATCH 06/10] linkmode metadata optional --- Sources/WalletConnectPairing/Types/AppMetadata.swift | 2 +- .../Auth/Services/App/AuthResponseSubscriber.swift | 3 ++- .../LinkAndRelayDispatchers/WalletErrorResponder.swift | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Sources/WalletConnectPairing/Types/AppMetadata.swift b/Sources/WalletConnectPairing/Types/AppMetadata.swift index f71fe44ae..ebd32f965 100644 --- a/Sources/WalletConnectPairing/Types/AppMetadata.swift +++ b/Sources/WalletConnectPairing/Types/AppMetadata.swift @@ -22,7 +22,7 @@ public struct AppMetadata: Codable, Equatable { /// Universal link URL string. public let universal: String? - public let linkMode: Bool + public let linkMode: Bool? /** Creates a new Redirect object with the specified information. diff --git a/Sources/WalletConnectSign/Auth/Services/App/AuthResponseSubscriber.swift b/Sources/WalletConnectSign/Auth/Services/App/AuthResponseSubscriber.swift index cab1ac6bd..6c3a304d8 100644 --- a/Sources/WalletConnectSign/Auth/Services/App/AuthResponseSubscriber.swift +++ b/Sources/WalletConnectSign/Auth/Services/App/AuthResponseSubscriber.swift @@ -143,7 +143,8 @@ class AuthResponseSubscriber { // remove record if let peerRedirect = peerMetadata.redirect, - peerRedirect.linkMode, + let peerLinkMode = peerRedirect.linkMode, + peerLinkMode == true, let universalLink = peerRedirect.universal, supportLinkMode { linkModeLinksStore.set(true, forKey: universalLink) diff --git a/Sources/WalletConnectSign/LinkAndRelayDispatchers/WalletErrorResponder.swift b/Sources/WalletConnectSign/LinkAndRelayDispatchers/WalletErrorResponder.swift index e965ebffb..7da610522 100644 --- a/Sources/WalletConnectSign/LinkAndRelayDispatchers/WalletErrorResponder.swift +++ b/Sources/WalletConnectSign/LinkAndRelayDispatchers/WalletErrorResponder.swift @@ -55,7 +55,8 @@ actor WalletErrorResponder { let (sessionAuthenticateRequestParams, _) = try getsessionAuthenticateRequestParams(requestId: requestId) guard let redirect = sessionAuthenticateRequestParams.requester.metadata.redirect, - redirect.linkMode else { + let linkMode = redirect.linkMode, + linkMode == true else { throw Errors.linkModeNotSupported } guard let peerUniversalLink = redirect.universal else { From 7eae272a9a0eef54bfe0897fe5c267dbb302568d Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Mon, 3 Jun 2024 10:15:29 +0200 Subject: [PATCH 07/10] update w3m --- .../Modules/Sign/SessionAccount/SessionAccountPresenter.swift | 2 +- Example/DApp/Modules/Sign/SignPresenter.swift | 2 +- Example/ExampleApp.xcodeproj/project.pbxproj | 2 +- .../project.xcworkspace/xcshareddata/swiftpm/Package.resolved | 2 +- .../Auth/Services/App/AuthResponseSubscriber.swift | 1 - 5 files changed, 4 insertions(+), 5 deletions(-) diff --git a/Example/DApp/Modules/Sign/SessionAccount/SessionAccountPresenter.swift b/Example/DApp/Modules/Sign/SessionAccount/SessionAccountPresenter.swift index 31920ce46..e89ea8a8e 100644 --- a/Example/DApp/Modules/Sign/SessionAccount/SessionAccountPresenter.swift +++ b/Example/DApp/Modules/Sign/SessionAccount/SessionAccountPresenter.swift @@ -87,7 +87,7 @@ extension SessionAccountPresenter { } private func getRequest(for method: String) throws -> AnyCodable { - let account = session.namespaces.first!.value.accounts.first!.absoluteString + let account = session.namespaces.first!.value.accounts.first!.address if method == "eth_sendTransaction" { let tx = Stub.tx return AnyCodable(tx) diff --git a/Example/DApp/Modules/Sign/SignPresenter.swift b/Example/DApp/Modules/Sign/SignPresenter.swift index 919f7326a..f21cc84e2 100644 --- a/Example/DApp/Modules/Sign/SignPresenter.swift +++ b/Example/DApp/Modules/Sign/SignPresenter.swift @@ -244,7 +244,7 @@ extension AuthRequestParams { domain: String = "lab.web3modal.com", chains: [String] = ["eip155:1", "eip155:137"], nonce: String = "32891756", - uri: String = "https://app.web3inbox.com/login", + uri: String = "https://lab.web3modal.com", nbf: String? = nil, exp: String? = nil, statement: String? = "I accept the ServiceOrg Terms of Service: https://app.web3inbox.com/tos", diff --git a/Example/ExampleApp.xcodeproj/project.pbxproj b/Example/ExampleApp.xcodeproj/project.pbxproj index 663d23895..00f54ddc5 100644 --- a/Example/ExampleApp.xcodeproj/project.pbxproj +++ b/Example/ExampleApp.xcodeproj/project.pbxproj @@ -3280,7 +3280,7 @@ repositoryURL = "https://github.com/WalletConnect/web3modal-swift"; requirement = { kind = revision; - revision = d84d37a0cda748bd69db340065f464b859c38158; + revision = babf2a567ea06a4deb56c8152a4e92c96ef7b6c3; }; }; A5434021291E6A270068F706 /* XCRemoteSwiftPackageReference "solana-swift" */ = { diff --git a/Example/ExampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Example/ExampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index db719c925..091c4e2c8 100644 --- a/Example/ExampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Example/ExampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -186,7 +186,7 @@ "repositoryURL": "https://github.com/WalletConnect/web3modal-swift", "state": { "branch": null, - "revision": "d84d37a0cda748bd69db340065f464b859c38158", + "revision": "babf2a567ea06a4deb56c8152a4e92c96ef7b6c3", "version": null } } diff --git a/Sources/WalletConnectSign/Auth/Services/App/AuthResponseSubscriber.swift b/Sources/WalletConnectSign/Auth/Services/App/AuthResponseSubscriber.swift index 6c3a304d8..8bf8ebac9 100644 --- a/Sources/WalletConnectSign/Auth/Services/App/AuthResponseSubscriber.swift +++ b/Sources/WalletConnectSign/Auth/Services/App/AuthResponseSubscriber.swift @@ -140,7 +140,6 @@ class AuthResponseSubscriber { private func getTransportTypeUpgradeIfPossible(peerMetadata: AppMetadata, requestId: RPCID) -> WCSession.TransportType { // upgrade to link mode only if dapp requested universallink because dapp may not be prepared for handling a response - add this to doc] - // remove record if let peerRedirect = peerMetadata.redirect, let peerLinkMode = peerRedirect.linkMode, From aee6e31eef5930755946001cc422074a4732f3d2 Mon Sep 17 00:00:00 2001 From: Jack Pooley <169029079+jackpooleywc@users.noreply.github.com> Date: Thu, 23 May 2024 17:11:59 +0200 Subject: [PATCH 08/10] Session rejection tags --- .../Auth/Link/LinkAuthRequestSubscriber.swift | 3 +- .../Auth/Link/LinkAuthRequester.swift | 2 +- .../Services/App/AuthResponseSubscriber.swift | 10 ++-- .../AuthenticateTransportTypeSwitcher.swift | 4 +- .../App/SessionAuthRequestService.swift | 2 +- .../Wallet/AuthRequestSubscriber.swift | 11 +++-- .../Wallet/SessionAuthenticateResponder.swift | 8 +++- .../SessionAuthenticatedProtocolMethod.swift | 48 +++++++++++++++---- .../Engine/Common/ApproveEngine.swift | 25 ++++++---- .../WalletErrorResponder.swift | 8 +++- .../Services/App/AppProposeService.swift | 2 +- .../SessionProposeProtocolMethod.swift | 41 +++++++++++++++- 12 files changed, 130 insertions(+), 34 deletions(-) diff --git a/Sources/WalletConnectSign/Auth/Link/LinkAuthRequestSubscriber.swift b/Sources/WalletConnectSign/Auth/Link/LinkAuthRequestSubscriber.swift index 2edda30d6..c245a19d2 100644 --- a/Sources/WalletConnectSign/Auth/Link/LinkAuthRequestSubscriber.swift +++ b/Sources/WalletConnectSign/Auth/Link/LinkAuthRequestSubscriber.swift @@ -29,7 +29,8 @@ class LinkAuthRequestSubscriber { private func subscribeForRequest() { - envelopesDispatcher.requestSubscription(on: SessionAuthenticatedProtocolMethod().method) + envelopesDispatcher + .requestSubscription(on: SessionAuthenticatedProtocolMethod.responseApprove().method) .sink { [unowned self] (payload: RequestSubscriptionPayload) in logger.debug("LinkAuthRequestSubscriber: Received request") diff --git a/Sources/WalletConnectSign/Auth/Link/LinkAuthRequester.swift b/Sources/WalletConnectSign/Auth/Link/LinkAuthRequester.swift index 2d6846040..c18b9fb78 100644 --- a/Sources/WalletConnectSign/Auth/Link/LinkAuthRequester.swift +++ b/Sources/WalletConnectSign/Auth/Link/LinkAuthRequester.swift @@ -37,7 +37,7 @@ actor LinkAuthRequester { var params = params let pubKey = try kms.createX25519KeyPair() let responseTopic = pubKey.rawRepresentation.sha256().toHexString() - let protocolMethod = SessionAuthenticatedProtocolMethod(ttl: params.ttl) + let protocolMethod = SessionAuthenticatedProtocolMethod.responseApprove(ttl: params.ttl) guard let chainNamespace = Blockchain(params.chains.first!)?.namespace, chainNamespace == "eip155" else { diff --git a/Sources/WalletConnectSign/Auth/Services/App/AuthResponseSubscriber.swift b/Sources/WalletConnectSign/Auth/Services/App/AuthResponseSubscriber.swift index cab1ac6bd..315818f3e 100644 --- a/Sources/WalletConnectSign/Auth/Services/App/AuthResponseSubscriber.swift +++ b/Sources/WalletConnectSign/Auth/Services/App/AuthResponseSubscriber.swift @@ -52,13 +52,15 @@ class AuthResponseSubscriber { } private func subscribeForResponse() { - networkingInteractor.responseErrorSubscription(on: SessionAuthenticatedProtocolMethod()) + networkingInteractor + .responseErrorSubscription(on: SessionAuthenticatedProtocolMethod.responseApprove()) .sink { [unowned self] (payload: ResponseSubscriptionErrorPayload) in guard let error = AuthError(code: payload.error.code) else { return } authResponsePublisherSubject.send((payload.id, .failure(error))) }.store(in: &publishers) - networkingInteractor.responseSubscription(on: SessionAuthenticatedProtocolMethod()) + networkingInteractor + .responseSubscription(on: SessionAuthenticatedProtocolMethod.responseApprove()) .sink { [unowned self] (payload: ResponseSubscriptionPayload) in let transportType = getTransportTypeUpgradeIfPossible(peerMetadata: payload.response.responder.metadata, requestId: payload.id) @@ -86,13 +88,13 @@ class AuthResponseSubscriber { } private func subscribeForLinkResponse() { - linkEnvelopesDispatcher.responseErrorSubscription(on: SessionAuthenticatedProtocolMethod()) + linkEnvelopesDispatcher.responseErrorSubscription(on: SessionAuthenticatedProtocolMethod.responseApprove()) .sink { [unowned self] (payload: ResponseSubscriptionErrorPayload) in guard let error = AuthError(code: payload.error.code) else { return } authResponsePublisherSubject.send((payload.id, .failure(error))) }.store(in: &publishers) - linkEnvelopesDispatcher.responseSubscription(on: SessionAuthenticatedProtocolMethod()) + linkEnvelopesDispatcher.responseSubscription(on: SessionAuthenticatedProtocolMethod.responseApprove()) .sink { [unowned self] (payload: ResponseSubscriptionPayload) in let pairingTopic = payload.topic diff --git a/Sources/WalletConnectSign/Auth/Services/App/AuthenticateTransportTypeSwitcher.swift b/Sources/WalletConnectSign/Auth/Services/App/AuthenticateTransportTypeSwitcher.swift index 30223401b..494ec1f22 100644 --- a/Sources/WalletConnectSign/Auth/Services/App/AuthenticateTransportTypeSwitcher.swift +++ b/Sources/WalletConnectSign/Auth/Services/App/AuthenticateTransportTypeSwitcher.swift @@ -45,7 +45,9 @@ class AuthenticateTransportTypeSwitcher { // Continue with relay if the error is walletLinkSupportNotProven } - let pairingURI = try await pairingClient.create(methods: [SessionAuthenticatedProtocolMethod().method]) + let pairingURI = try await pairingClient.create( + methods: [SessionAuthenticatedProtocolMethod.responseApprove().method] + ) logger.debug("Requesting Authentication on existing pairing") try await appRequestService.request(params: params, topic: pairingURI.topic) diff --git a/Sources/WalletConnectSign/Auth/Services/App/SessionAuthRequestService.swift b/Sources/WalletConnectSign/Auth/Services/App/SessionAuthRequestService.swift index b26a40f0e..3b75365a4 100644 --- a/Sources/WalletConnectSign/Auth/Services/App/SessionAuthRequestService.swift +++ b/Sources/WalletConnectSign/Auth/Services/App/SessionAuthRequestService.swift @@ -29,7 +29,7 @@ actor SessionAuthRequestService { var params = params let pubKey = try kms.createX25519KeyPair() let responseTopic = pubKey.rawRepresentation.sha256().toHexString() - let protocolMethod = SessionAuthenticatedProtocolMethod(ttl: params.ttl) + let protocolMethod = SessionAuthenticatedProtocolMethod.responseApprove(ttl: params.ttl) guard let chainNamespace = Blockchain(params.chains.first!)?.namespace, chainNamespace == "eip155" else { diff --git a/Sources/WalletConnectSign/Auth/Services/Wallet/AuthRequestSubscriber.swift b/Sources/WalletConnectSign/Auth/Services/Wallet/AuthRequestSubscriber.swift index ecc44bb78..a4389ee9f 100644 --- a/Sources/WalletConnectSign/Auth/Services/Wallet/AuthRequestSubscriber.swift +++ b/Sources/WalletConnectSign/Auth/Services/Wallet/AuthRequestSubscriber.swift @@ -36,7 +36,7 @@ class AuthRequestSubscriber { } private func subscribeForRequest() { - pairingRegisterer.register(method: SessionAuthenticatedProtocolMethod()) + pairingRegisterer.register(method: SessionAuthenticatedProtocolMethod.responseApprove()) .sink { [unowned self] (payload: RequestSubscriptionPayload) in guard !payload.request.isExpired() else { @@ -70,10 +70,15 @@ class AuthRequestSubscriber { }.store(in: &publishers) } - func respondError(payload: SubscriptionPayload, reason: SignReasonCode) { + private func respondError(payload: SubscriptionPayload, reason: SignReasonCode) { Task(priority: .high) { do { - try await networkingInteractor.respondError(topic: payload.topic, requestId: payload.id, protocolMethod: SessionAuthenticatedProtocolMethod(), reason: reason) + try await networkingInteractor.respondError( + topic: payload.topic, + requestId: payload.id, + protocolMethod: SessionAuthenticatedProtocolMethod.responseAutoReject(), + reason: reason + ) } catch { logger.error("Respond Error failed with: \(error.localizedDescription)") } diff --git a/Sources/WalletConnectSign/Auth/Services/Wallet/SessionAuthenticateResponder.swift b/Sources/WalletConnectSign/Auth/Services/Wallet/SessionAuthenticateResponder.swift index 2f5a6123c..a4b229830 100644 --- a/Sources/WalletConnectSign/Auth/Services/Wallet/SessionAuthenticateResponder.swift +++ b/Sources/WalletConnectSign/Auth/Services/Wallet/SessionAuthenticateResponder.swift @@ -53,7 +53,13 @@ actor SessionAuthenticateResponder { let responseParams = SessionAuthenticateResponseParams(responder: selfParticipant, cacaos: auths) let response = RPCResponse(id: requestId, result: responseParams) - try await networkingInteractor.respond(topic: responseTopic, response: response, protocolMethod: SessionAuthenticatedProtocolMethod(), envelopeType: .type1(pubKey: responseKeys.publicKey.rawRepresentation)) + + try await networkingInteractor.respond( + topic: responseTopic, + response: response, + protocolMethod: SessionAuthenticatedProtocolMethod.responseApprove(), + envelopeType: .type1(pubKey: responseKeys.publicKey.rawRepresentation) + ) let session = try util.createSession( diff --git a/Sources/WalletConnectSign/Auth/SessionAuthenticatedProtocolMethod.swift b/Sources/WalletConnectSign/Auth/SessionAuthenticatedProtocolMethod.swift index 6005e63e5..4b44218f5 100644 --- a/Sources/WalletConnectSign/Auth/SessionAuthenticatedProtocolMethod.swift +++ b/Sources/WalletConnectSign/Auth/SessionAuthenticatedProtocolMethod.swift @@ -1,17 +1,47 @@ import Foundation struct SessionAuthenticatedProtocolMethod: ProtocolMethod { + + enum Tag: Int { + case sessionAuthenticate = 1116 + case sessionAuthenticateResponseApprove = 1117 + case sessionAuthenticateResponseReject = 1118 + case sessionAuthenticateResponseAutoReject = 1119 + } + let method: String = "wc_sessionAuthenticate" - let requestConfig = RelayConfig(tag: 1116, prompt: true, ttl: 3600) - - let responseConfig = RelayConfig(tag: 1117, prompt: false, ttl: 3600) - - - static let defaultTtl: TimeInterval = 3600 - private let ttl: Int + let requestConfig: RelayConfig + + let responseConfig: RelayConfig - init(ttl: TimeInterval = Self.defaultTtl) { - self.ttl = Int(ttl) + static let defaultTtl: TimeInterval = 300 + + private init( + ttl: TimeInterval, + responseTag: Tag + ) { + self.requestConfig = RelayConfig( + tag: Tag.sessionAuthenticate.rawValue, + prompt: true, + ttl: Int(ttl) + ) + self.responseConfig = RelayConfig( + tag: responseTag.rawValue, + prompt: false, + ttl: Int(ttl) + ) + } + + static func responseApprove(ttl: TimeInterval = Self.defaultTtl) -> Self { + Self(ttl: ttl, responseTag: .sessionAuthenticateResponseApprove) + } + + static func responseReject(ttl: TimeInterval = Self.defaultTtl) -> Self { + Self(ttl: ttl, responseTag: .sessionAuthenticateResponseReject) + } + + static func responseAutoReject(ttl: TimeInterval = Self.defaultTtl) -> Self { + Self(ttl: ttl, responseTag: .sessionAuthenticateResponseAutoReject) } } diff --git a/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift b/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift index 3d0d9727b..eabb88d6b 100644 --- a/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift +++ b/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift @@ -114,7 +114,7 @@ final class ApproveEngine { async let proposeResponseTask: () = networkingInteractor.respond( topic: payload.topic, response: response, - protocolMethod: SessionProposeProtocolMethod() + protocolMethod: SessionProposeProtocolMethod.responseApprove() ) async let settleRequestTask: WCSession = settle( @@ -146,8 +146,13 @@ final class ApproveEngine { guard let payload = try proposalPayloadsStore.get(key: proposerPubKey) else { throw Errors.proposalNotFound } - - try await networkingInteractor.respondError(topic: payload.topic, requestId: payload.id, protocolMethod: SessionProposeProtocolMethod(), reason: reason) + + try await networkingInteractor.respondError( + topic: payload.topic, + requestId: payload.id, + protocolMethod: SessionProposeProtocolMethod.responseReject(), + reason: reason + ) if let pairingTopic = rpcHistory.get(recordId: payload.id)?.topic, let pairing = pairingStore.getPairing(forTopic: pairingTopic), @@ -216,12 +221,14 @@ final class ApproveEngine { private extension ApproveEngine { func setupRequestSubscriptions() { - pairingRegisterer.register(method: SessionProposeProtocolMethod()) + pairingRegisterer.register(method: SessionProposeProtocolMethod.responseAutoReject()) .sink { [unowned self] (payload: RequestSubscriptionPayload) in guard let pairing = pairingStore.getPairing(forTopic: payload.topic) else { return } + let responseApproveMethod = SessionAuthenticatedProtocolMethod.responseApprove().method if let methods = pairing.methods, - methods.flatMap({ $0 }) - .contains(SessionAuthenticatedProtocolMethod().method), authRequestSubscribersTracking.hasSubscribers() { + methods.contains(responseApproveMethod), + authRequestSubscribersTracking.hasSubscribers() + { logger.debug("Ignoring Session Proposal") // respond with an error? return @@ -236,7 +243,7 @@ private extension ApproveEngine { } func setupResponseSubscriptions() { - networkingInteractor.responseSubscription(on: SessionProposeProtocolMethod()) + networkingInteractor.responseSubscription(on: SessionProposeProtocolMethod.responseApprove()) .sink { [unowned self] (payload: ResponseSubscriptionPayload) in handleSessionProposeResponse(payload: payload) }.store(in: &publishers) @@ -248,7 +255,7 @@ private extension ApproveEngine { } func setupResponseErrorSubscriptions() { - networkingInteractor.responseErrorSubscription(on: SessionProposeProtocolMethod()) + networkingInteractor.responseErrorSubscription(on: SessionProposeProtocolMethod.responseApprove()) .sink { [unowned self] (payload: ResponseSubscriptionErrorPayload) in handleSessionProposeResponseError(payload: payload) }.store(in: &publishers) @@ -340,7 +347,7 @@ private extension ApproveEngine { logger.debug("Received Session Proposal") let proposal = payload.request do { try Namespace.validate(proposal.requiredNamespaces) } catch { - return respondError(payload: payload, reason: .invalidUpdateRequest, protocolMethod: SessionProposeProtocolMethod()) + return respondError(payload: payload, reason: .invalidUpdateRequest, protocolMethod: SessionProposeProtocolMethod.responseAutoReject()) } proposalPayloadsStore.set(payload, forKey: proposal.proposer.publicKey) diff --git a/Sources/WalletConnectSign/LinkAndRelayDispatchers/WalletErrorResponder.swift b/Sources/WalletConnectSign/LinkAndRelayDispatchers/WalletErrorResponder.swift index e965ebffb..ca3e8e4c4 100644 --- a/Sources/WalletConnectSign/LinkAndRelayDispatchers/WalletErrorResponder.swift +++ b/Sources/WalletConnectSign/LinkAndRelayDispatchers/WalletErrorResponder.swift @@ -48,7 +48,13 @@ actor WalletErrorResponder { private func respondErrorRelay(_ error: AuthError, requestId: RPCID, topic: String, type1EnvelopeKey: Data) async throws { let envelopeType = Envelope.EnvelopeType.type1(pubKey: type1EnvelopeKey) - try await networkingInteractor.respondError(topic: topic, requestId: requestId, protocolMethod: SessionAuthenticatedProtocolMethod(), reason: error, envelopeType: envelopeType) + try await networkingInteractor.respondError( + topic: topic, + requestId: requestId, + protocolMethod: SessionAuthenticatedProtocolMethod.responseReject(), + reason: error, + envelopeType: envelopeType + ) } private func respondErrorLinkMode(_ error: AuthError, requestId: RPCID, topic: String, type1EnvelopeKey: Data) async throws -> String { diff --git a/Sources/WalletConnectSign/Services/App/AppProposeService.swift b/Sources/WalletConnectSign/Services/App/AppProposeService.swift index a92b80bea..bdeca0df5 100644 --- a/Sources/WalletConnectSign/Services/App/AppProposeService.swift +++ b/Sources/WalletConnectSign/Services/App/AppProposeService.swift @@ -33,7 +33,7 @@ final class AppProposeService { if let sessionProperties { try SessionProperties.validate(sessionProperties) } - let protocolMethod = SessionProposeProtocolMethod() + let protocolMethod = SessionProposeProtocolMethod.responseApprove() let publicKey = try! kms.createX25519KeyPair() let proposer = Participant( publicKey: publicKey.hexRepresentation, diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionProposeProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionProposeProtocolMethod.swift index f7068cfb4..f62db43af 100644 --- a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionProposeProtocolMethod.swift +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionProposeProtocolMethod.swift @@ -2,8 +2,45 @@ import Foundation struct SessionProposeProtocolMethod: ProtocolMethod { let method: String = "wc_sessionPropose" + + enum Tag: Int { + case sessionPropose = 1100 + case sessionProposeResponseApprove = 1101 + case sessionProposeResponseReject = 1120 + case sessionProposeResponseAutoReject = 1121 + } - let requestConfig = RelayConfig(tag: 1100, prompt: true, ttl: 300) + let requestConfig: RelayConfig - let responseConfig = RelayConfig(tag: 1101, prompt: false, ttl: 300) + let responseConfig: RelayConfig + + static let defaultTtl: TimeInterval = 300 + + private init( + ttl: TimeInterval, + responseTag: Tag + ) { + self.requestConfig = RelayConfig( + tag: Tag.sessionPropose.rawValue, + prompt: true, + ttl: Int(ttl) + ) + self.responseConfig = RelayConfig( + tag: responseTag.rawValue, + prompt: false, + ttl: Int(ttl) + ) + } + + static func responseApprove(ttl: TimeInterval = Self.defaultTtl) -> Self { + Self(ttl: ttl, responseTag: .sessionProposeResponseApprove) + } + + static func responseReject(ttl: TimeInterval = Self.defaultTtl) -> Self { + Self(ttl: ttl, responseTag: .sessionProposeResponseReject) + } + + static func responseAutoReject(ttl: TimeInterval = Self.defaultTtl) -> Self { + Self(ttl: ttl, responseTag: .sessionProposeResponseAutoReject) + } } From 6989c77efbb7a6bfcddfb79db83e21df40cedf57 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Mon, 3 Jun 2024 13:44:15 +0200 Subject: [PATCH 09/10] add authentication alert --- Example/DApp/SceneDelegate.swift | 11 ++++++++++- Example/ExampleApp.xcodeproj/project.pbxproj | 2 +- .../xcshareddata/swiftpm/Package.resolved | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Example/DApp/SceneDelegate.swift b/Example/DApp/SceneDelegate.swift index 89b4712d4..2de2c9534 100644 --- a/Example/DApp/SceneDelegate.swift +++ b/Example/DApp/SceneDelegate.swift @@ -58,7 +58,16 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { ) ] ) - + + Web3Modal.instance.authResponsePublisher.sink { (id, result) in + switch result { + case .success((_, _)): + AlertPresenter.present(message: "User Authenticted with SIWE", type: .success) + case .failure(_): + break + } + }.store(in: &publishers) + WalletConnectModal.configure( projectId: InputConfig.projectId, metadata: metadata diff --git a/Example/ExampleApp.xcodeproj/project.pbxproj b/Example/ExampleApp.xcodeproj/project.pbxproj index 00f54ddc5..b7d71d28a 100644 --- a/Example/ExampleApp.xcodeproj/project.pbxproj +++ b/Example/ExampleApp.xcodeproj/project.pbxproj @@ -3280,7 +3280,7 @@ repositoryURL = "https://github.com/WalletConnect/web3modal-swift"; requirement = { kind = revision; - revision = babf2a567ea06a4deb56c8152a4e92c96ef7b6c3; + revision = 3baac675811b5fdeb689cef703e3dfc7682704e6; }; }; A5434021291E6A270068F706 /* XCRemoteSwiftPackageReference "solana-swift" */ = { diff --git a/Example/ExampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Example/ExampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 091c4e2c8..3198e81b5 100644 --- a/Example/ExampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Example/ExampleApp.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -186,7 +186,7 @@ "repositoryURL": "https://github.com/WalletConnect/web3modal-swift", "state": { "branch": null, - "revision": "babf2a567ea06a4deb56c8152a4e92c96ef7b6c3", + "revision": "3baac675811b5fdeb689cef703e3dfc7682704e6", "version": null } } From 075d74eb3a8db1988867d3d63678fba6cc4327eb Mon Sep 17 00:00:00 2001 From: llbartekll Date: Tue, 4 Jun 2024 07:08:14 +0000 Subject: [PATCH 10/10] Set User Agent --- Sources/WalletConnectRelay/PackageConfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/WalletConnectRelay/PackageConfig.json b/Sources/WalletConnectRelay/PackageConfig.json index b363160a7..e01fd4d43 100644 --- a/Sources/WalletConnectRelay/PackageConfig.json +++ b/Sources/WalletConnectRelay/PackageConfig.json @@ -1 +1 @@ -{"version": "1.19.0"} +{"version": "1.19.1"}