From d6df9c05401a98bc184770305e2e5765c733ceaf Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Mon, 26 Sep 2022 10:26:34 +0200 Subject: [PATCH 01/10] refactor request method --- Sources/Auth/Services/Common/DeletePairingService.swift | 2 +- Sources/Chat/ProtocolServices/Common/MessagingService.swift | 2 +- Sources/Chat/ProtocolServices/Inviter/InviteService.swift | 2 +- Sources/WalletConnectNetworking/NetworkInteracting.swift | 6 +++--- Sources/WalletConnectNetworking/NetworkInteractor.swift | 4 ++-- Sources/WalletConnectPairing/Services/PingRequester.swift | 2 +- Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift | 2 +- .../Engine/Common/DeletePairingService.swift | 2 +- .../Engine/Common/DeleteSessionService.swift | 2 +- Sources/WalletConnectSign/Engine/Common/SessionEngine.swift | 4 ++-- .../Engine/Controller/ControllerSessionStateMachine.swift | 4 ++-- Tests/TestingUtils/NetworkingInteractorMock.swift | 2 +- 12 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Sources/Auth/Services/Common/DeletePairingService.swift b/Sources/Auth/Services/Common/DeletePairingService.swift index 721bdc81f..4ec44f20e 100644 --- a/Sources/Auth/Services/Common/DeletePairingService.swift +++ b/Sources/Auth/Services/Common/DeletePairingService.swift @@ -29,7 +29,7 @@ class DeletePairingService { let reason = AuthError.userDisconnected logger.debug("Will delete pairing for reason: message: \(reason.message) code: \(reason.code)") let request = RPCRequest(method: AuthProtocolMethod.pairingDelete.rawValue, params: reason) - try await networkingInteractor.request(request, topic: topic, tag: AuthProtocolMethod.pairingDelete.requestTag) + try await networkingInteractor.request(request, topic: topic, protocolMethod: AuthProtocolMethod.pairingDelete) pairingStorage.delete(topic: topic) kms.deleteSymmetricKey(for: topic) networkingInteractor.unsubscribe(topic: topic) diff --git a/Sources/Chat/ProtocolServices/Common/MessagingService.swift b/Sources/Chat/ProtocolServices/Common/MessagingService.swift index 20edfb6b5..3e29ab505 100644 --- a/Sources/Chat/ProtocolServices/Common/MessagingService.swift +++ b/Sources/Chat/ProtocolServices/Common/MessagingService.swift @@ -34,7 +34,7 @@ class MessagingService { let timestamp = Int64(Date().timeIntervalSince1970 * 1000) let message = Message(topic: topic, message: messageString, authorAccount: authorAccount, timestamp: timestamp) let request = RPCRequest(method: ChatProtocolMethod.message.method, params: message) - try await networkingInteractor.request(request, topic: topic, tag: ChatProtocolMethod.message.requestTag) + try await networkingInteractor.request(request, topic: topic, protocolMethod: ChatProtocolMethod.message) Task(priority: .background) { await messagesStore.add(message) onMessage?(message) diff --git a/Sources/Chat/ProtocolServices/Inviter/InviteService.swift b/Sources/Chat/ProtocolServices/Inviter/InviteService.swift index 514138042..706baddc1 100644 --- a/Sources/Chat/ProtocolServices/Inviter/InviteService.swift +++ b/Sources/Chat/ProtocolServices/Inviter/InviteService.swift @@ -50,7 +50,7 @@ class InviteService { try kms.setSymmetricKey(symKeyI.sharedKey, for: responseTopic) try await networkingInteractor.subscribe(topic: responseTopic) - try await networkingInteractor.request(request, topic: inviteTopic, tag: ChatProtocolMethod.invite.requestTag, envelopeType: .type1(pubKey: selfPubKeyY.rawRepresentation)) + try await networkingInteractor.request(request, topic: inviteTopic, protocolMethod: ChatProtocolMethod.invite, envelopeType: .type1(pubKey: selfPubKeyY.rawRepresentation)) logger.debug("invite sent on topic: \(inviteTopic)") } diff --git a/Sources/WalletConnectNetworking/NetworkInteracting.swift b/Sources/WalletConnectNetworking/NetworkInteracting.swift index 85ca9177e..576887da0 100644 --- a/Sources/WalletConnectNetworking/NetworkInteracting.swift +++ b/Sources/WalletConnectNetworking/NetworkInteracting.swift @@ -8,7 +8,7 @@ public protocol NetworkInteracting { var socketConnectionStatusPublisher: AnyPublisher { get } func subscribe(topic: String) async throws func unsubscribe(topic: String) - func request(_ request: RPCRequest, topic: String, tag: Int, envelopeType: Envelope.EnvelopeType) async throws + func request(_ request: RPCRequest, topic: String, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws func requestNetworkAck(_ request: RPCRequest, topic: String, tag: Int) async throws func respond(topic: String, response: RPCResponse, tag: Int, envelopeType: Envelope.EnvelopeType) async throws func respondSuccess(topic: String, requestId: RPCID, tag: Int, envelopeType: Envelope.EnvelopeType) async throws @@ -28,8 +28,8 @@ public protocol NetworkInteracting { } extension NetworkInteracting { - public func request(_ request: RPCRequest, topic: String, tag: Int) async throws { - try await self.request(request, topic: topic, tag: tag, envelopeType: .type0) + public func request(_ request: RPCRequest, topic: String, protocolMethod: ProtocolMethod) async throws { + try await self.request(request, topic: topic, protocolMethod: protocolMethod, envelopeType: .type0) } public func respond(topic: String, response: RPCResponse, tag: Int) async throws { diff --git a/Sources/WalletConnectNetworking/NetworkInteractor.swift b/Sources/WalletConnectNetworking/NetworkInteractor.swift index 111d0003f..b8a5d8b9b 100644 --- a/Sources/WalletConnectNetworking/NetworkInteractor.swift +++ b/Sources/WalletConnectNetworking/NetworkInteractor.swift @@ -93,10 +93,10 @@ public class NetworkingInteractor: NetworkInteracting { .eraseToAnyPublisher() } - public func request(_ request: RPCRequest, topic: String, tag: Int, envelopeType: Envelope.EnvelopeType) async throws { + public func request(_ request: RPCRequest, topic: String, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws { try rpcHistory.set(request, forTopic: topic, emmitedBy: .local) let message = try! serializer.serialize(topic: topic, encodable: request, envelopeType: envelopeType) - try await relayClient.publish(topic: topic, payload: message, tag: tag, prompt: shouldPrompt(method: request.method)) + try await relayClient.publish(topic: topic, payload: message, tag: protocolMethod.requestTag, prompt: shouldPrompt(method: request.method)) } /// Completes with an acknowledgement from the relay network. diff --git a/Sources/WalletConnectPairing/Services/PingRequester.swift b/Sources/WalletConnectPairing/Services/PingRequester.swift index 9def19d28..f5007c9c6 100644 --- a/Sources/WalletConnectPairing/Services/PingRequester.swift +++ b/Sources/WalletConnectPairing/Services/PingRequester.swift @@ -13,6 +13,6 @@ public class PingRequester { public func ping(topic: String) async throws { let request = RPCRequest(method: method.method, params: PairingPingParams()) - try await networkingInteractor.request(request, topic: topic, tag: method.requestTag) + try await networkingInteractor.request(request, topic: topic, protocolMethod: method) } } diff --git a/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift b/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift index 5a23eb079..cfe3506cc 100644 --- a/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift +++ b/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift @@ -144,7 +144,7 @@ final class ApproveEngine { sessionStore.setSession(session) let request = RPCRequest(method: SignProtocolMethod.sessionSettle.method, params: settleParams) - try await networkingInteractor.request(request, topic: topic, tag: SignProtocolMethod.sessionSettle.requestTag) + try await networkingInteractor.request(request, topic: topic, protocolMethod: SignProtocolMethod.sessionSettle) onSessionSettle?(session.publicRepresentation()) } } diff --git a/Sources/WalletConnectSign/Engine/Common/DeletePairingService.swift b/Sources/WalletConnectSign/Engine/Common/DeletePairingService.swift index 2dbbede9d..1e6aa3d19 100644 --- a/Sources/WalletConnectSign/Engine/Common/DeletePairingService.swift +++ b/Sources/WalletConnectSign/Engine/Common/DeletePairingService.swift @@ -26,7 +26,7 @@ class DeletePairingService { let reason = SessionType.Reason(code: reasonCode.code, message: reasonCode.message) logger.debug("Will delete pairing for reason: message: \(reason.message) code: \(reason.code)") let request = RPCRequest(method: SignProtocolMethod.sessionDelete.method, params: reason) - try await networkingInteractor.request(request, topic: topic, tag: SignProtocolMethod.sessionDelete.requestTag) + try await networkingInteractor.request(request, topic: topic, protocolMethod: SignProtocolMethod.sessionDelete) pairingStorage.delete(topic: topic) kms.deleteSymmetricKey(for: topic) networkingInteractor.unsubscribe(topic: topic) diff --git a/Sources/WalletConnectSign/Engine/Common/DeleteSessionService.swift b/Sources/WalletConnectSign/Engine/Common/DeleteSessionService.swift index f2ff1442d..eda023715 100644 --- a/Sources/WalletConnectSign/Engine/Common/DeleteSessionService.swift +++ b/Sources/WalletConnectSign/Engine/Common/DeleteSessionService.swift @@ -25,7 +25,7 @@ class DeleteSessionService { let reason = SessionType.Reason(code: reasonCode.code, message: reasonCode.message) logger.debug("Will delete session for reason: message: \(reason.message) code: \(reason.code)") let request = RPCRequest(method: SignProtocolMethod.sessionDelete.method, params: reason) - try await networkingInteractor.request(request, topic: topic, tag: SignProtocolMethod.sessionDelete.requestTag) + try await networkingInteractor.request(request, topic: topic, protocolMethod: SignProtocolMethod.sessionDelete) sessionStore.delete(topic: topic) kms.deleteSymmetricKey(for: topic) networkingInteractor.unsubscribe(topic: topic) diff --git a/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift b/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift index 2c9c2be07..4ed7a78bd 100644 --- a/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift +++ b/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift @@ -60,7 +60,7 @@ final class SessionEngine { let sessionRequestParams = SessionType.RequestParams(request: chainRequest, chainId: request.chainId) let rpcRequest = RPCRequest(method: SignProtocolMethod.sessionRequest.method, params: sessionRequestParams) - try await networkingInteractor.request(rpcRequest, topic: request.topic, tag: SignProtocolMethod.sessionRequest.requestTag) + try await networkingInteractor.request(rpcRequest, topic: request.topic, protocolMethod: SignProtocolMethod.sessionRequest) } func respondSessionRequest(topic: String, requestId: RPCID, response: RPCResult) async throws { @@ -80,7 +80,7 @@ final class SessionEngine { throw WalletConnectError.invalidEvent } let rpcRequest = RPCRequest(method: SignProtocolMethod.sessionEvent.method, params: SessionType.EventParams(event: event, chainId: chainId)) - try await networkingInteractor.request(rpcRequest, topic: topic, tag: SignProtocolMethod.sessionEvent.requestTag) + try await networkingInteractor.request(rpcRequest, topic: topic, protocolMethod: SignProtocolMethod.sessionEvent) } } diff --git a/Sources/WalletConnectSign/Engine/Controller/ControllerSessionStateMachine.swift b/Sources/WalletConnectSign/Engine/Controller/ControllerSessionStateMachine.swift index be4733984..c7755e08e 100644 --- a/Sources/WalletConnectSign/Engine/Controller/ControllerSessionStateMachine.swift +++ b/Sources/WalletConnectSign/Engine/Controller/ControllerSessionStateMachine.swift @@ -35,7 +35,7 @@ final class ControllerSessionStateMachine { logger.debug("Controller will update methods") sessionStore.setSession(session) let request = RPCRequest(method: SignProtocolMethod.sessionUpdate.method, params: SessionType.UpdateParams(namespaces: namespaces)) - try await networkingInteractor.request(request, topic: topic, tag: SignProtocolMethod.sessionUpdate.requestTag) + try await networkingInteractor.request(request, topic: topic, protocolMethod: SignProtocolMethod.sessionUpdate) } func extend(topic: String, by ttl: Int64) async throws { @@ -45,7 +45,7 @@ final class ControllerSessionStateMachine { let newExpiry = Int64(session.expiryDate.timeIntervalSince1970 ) sessionStore.setSession(session) let request = RPCRequest(method: SignProtocolMethod.sessionExtend.method, params: SessionType.UpdateExpiryParams(expiry: newExpiry)) - try await networkingInteractor.request(request, topic: topic, tag: SignProtocolMethod.sessionExtend.requestTag) + try await networkingInteractor.request(request, topic: topic, protocolMethod: SignProtocolMethod.sessionExtend) } // MARK: - Handle Response diff --git a/Tests/TestingUtils/NetworkingInteractorMock.swift b/Tests/TestingUtils/NetworkingInteractorMock.swift index 286390803..6f7fff08a 100644 --- a/Tests/TestingUtils/NetworkingInteractorMock.swift +++ b/Tests/TestingUtils/NetworkingInteractorMock.swift @@ -96,7 +96,7 @@ public class NetworkingInteractorMock: NetworkInteracting { didCallUnsubscribe = true } - public func request(_ request: RPCRequest, topic: String, tag: Int, envelopeType: Envelope.EnvelopeType) async throws { + public func request(_ request: RPCRequest, topic: String, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws { requestCallCount += 1 requests.append((topic, request)) } From 3128d0986e8d2dca28a276998ab8fc734a4b07f6 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Mon, 26 Sep 2022 11:05:29 +0200 Subject: [PATCH 02/10] refactor respond method --- .../Wallet/WalletErrorResponder.swift | 3 +- .../Wallet/WalletRespondService.swift | 2 +- .../Common/MessagingService.swift | 2 +- .../Invitee/InvitationHandlingService.swift | 4 +-- .../NetworkInteracting.swift | 18 +++++------ .../NetworkInteractor.swift | 12 ++++---- .../Services/PingResponder.swift | 2 +- .../Engine/Common/ApproveEngine.swift | 20 ++++++------- .../Engine/Common/PairingEngine.swift | 2 +- .../Engine/Common/SessionEngine.swift | 30 +++++++++---------- .../NonControllerSessionStateMachine.swift | 24 ++++++++------- 11 files changed, 60 insertions(+), 59 deletions(-) diff --git a/Sources/Auth/Services/Wallet/WalletErrorResponder.swift b/Sources/Auth/Services/Wallet/WalletErrorResponder.swift index 10e318c24..b774c3865 100644 --- a/Sources/Auth/Services/Wallet/WalletErrorResponder.swift +++ b/Sources/Auth/Services/Wallet/WalletErrorResponder.swift @@ -32,9 +32,8 @@ actor WalletErrorResponder { try kms.setAgreementSecret(keys, topic: topic) - let tag = AuthProtocolMethod.authRequest.responseTag let envelopeType = Envelope.EnvelopeType.type1(pubKey: keys.publicKey.rawRepresentation) - try await networkingInteractor.respondError(topic: topic, requestId: requestId, tag: tag, reason: error, envelopeType: envelopeType) + try await networkingInteractor.respondError(topic: topic, requestId: requestId, protocolMethod: AuthProtocolMethod.authRequest, reason: error, envelopeType: envelopeType) } private func getAuthRequestParams(requestId: RPCID) throws -> AuthRequestParams { diff --git a/Sources/Auth/Services/Wallet/WalletRespondService.swift b/Sources/Auth/Services/Wallet/WalletRespondService.swift index f9a53b10d..c9529c197 100644 --- a/Sources/Auth/Services/Wallet/WalletRespondService.swift +++ b/Sources/Auth/Services/Wallet/WalletRespondService.swift @@ -39,7 +39,7 @@ actor WalletRespondService { let responseParams = AuthResponseParams(h: header, p: payload, s: signature) let response = RPCResponse(id: requestId, result: responseParams) - try await networkingInteractor.respond(topic: topic, response: response, tag: AuthProtocolMethod.authRequest.responseTag, envelopeType: .type1(pubKey: keys.publicKey.rawRepresentation)) + try await networkingInteractor.respond(topic: topic, response: response, protocolMethod: AuthProtocolMethod.authRequest, envelopeType: .type1(pubKey: keys.publicKey.rawRepresentation)) } func respondError(requestId: RPCID) async throws { diff --git a/Sources/Chat/ProtocolServices/Common/MessagingService.swift b/Sources/Chat/ProtocolServices/Common/MessagingService.swift index 3e29ab505..027afc5d8 100644 --- a/Sources/Chat/ProtocolServices/Common/MessagingService.swift +++ b/Sources/Chat/ProtocolServices/Common/MessagingService.swift @@ -59,7 +59,7 @@ class MessagingService { private func handleMessage(_ message: Message, topic: String, requestId: RPCID) { Task(priority: .background) { - try await networkingInteractor.respondSuccess(topic: topic, requestId: requestId, tag: ChatProtocolMethod.message.responseTag) + try await networkingInteractor.respondSuccess(topic: topic, requestId: requestId, protocolMethod: ChatProtocolMethod.message) await messagesStore.add(message) logger.debug("Received message") onMessage?(message) diff --git a/Sources/Chat/ProtocolServices/Invitee/InvitationHandlingService.swift b/Sources/Chat/ProtocolServices/Invitee/InvitationHandlingService.swift index c6dd9aede..1c07311b0 100644 --- a/Sources/Chat/ProtocolServices/Invitee/InvitationHandlingService.swift +++ b/Sources/Chat/ProtocolServices/Invitee/InvitationHandlingService.swift @@ -47,7 +47,7 @@ class InvitationHandlingService { let response = RPCResponse(id: payload.id, result: inviteResponse) let responseTopic = try getInviteResponseTopic(requestTopic: payload.topic, invite: payload.request) - try await networkingInteractor.respond(topic: responseTopic, response: response, tag: ChatProtocolMethod.invite.responseTag) + try await networkingInteractor.respond(topic: responseTopic, response: response, protocolMethod: ChatProtocolMethod.invite) let threadAgreementKeys = try kms.performKeyAgreement(selfPublicKey: selfThreadPubKey, peerPublicKey: payload.request.publicKey) let threadTopic = threadAgreementKeys.derivedTopic() @@ -71,7 +71,7 @@ class InvitationHandlingService { let responseTopic = try getInviteResponseTopic(requestTopic: payload.topic, invite: payload.request) - try await networkingInteractor.respondError(topic: responseTopic, requestId: payload.id, tag: ChatProtocolMethod.invite.responseTag, reason: ChatError.userRejected) + try await networkingInteractor.respondError(topic: responseTopic, requestId: payload.id, protocolMethod: ChatProtocolMethod.invite, reason: ChatError.userRejected) invitePayloadStore.delete(forKey: inviteId) } diff --git a/Sources/WalletConnectNetworking/NetworkInteracting.swift b/Sources/WalletConnectNetworking/NetworkInteracting.swift index 576887da0..33d5cea2d 100644 --- a/Sources/WalletConnectNetworking/NetworkInteracting.swift +++ b/Sources/WalletConnectNetworking/NetworkInteracting.swift @@ -10,9 +10,9 @@ public protocol NetworkInteracting { func unsubscribe(topic: String) func request(_ request: RPCRequest, topic: String, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws func requestNetworkAck(_ request: RPCRequest, topic: String, tag: Int) async throws - func respond(topic: String, response: RPCResponse, tag: Int, envelopeType: Envelope.EnvelopeType) async throws - func respondSuccess(topic: String, requestId: RPCID, tag: Int, envelopeType: Envelope.EnvelopeType) async throws - func respondError(topic: String, requestId: RPCID, tag: Int, reason: Reason, envelopeType: Envelope.EnvelopeType) async throws + func respond(topic: String, response: RPCResponse, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws + func respondSuccess(topic: String, requestId: RPCID, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws + func respondError(topic: String, requestId: RPCID, protocolMethod: ProtocolMethod, reason: Reason, envelopeType: Envelope.EnvelopeType) async throws func requestSubscription( on request: ProtocolMethod @@ -32,15 +32,15 @@ extension NetworkInteracting { try await self.request(request, topic: topic, protocolMethod: protocolMethod, envelopeType: .type0) } - public func respond(topic: String, response: RPCResponse, tag: Int) async throws { - try await self.respond(topic: topic, response: response, tag: tag, envelopeType: .type0) + public func respond(topic: String, response: RPCResponse, protocolMethod: ProtocolMethod) async throws { + try await self.respond(topic: topic, response: response, protocolMethod: protocolMethod, envelopeType: .type0) } - public func respondSuccess(topic: String, requestId: RPCID, tag: Int) async throws { - try await self.respondSuccess(topic: topic, requestId: requestId, tag: tag, envelopeType: .type0) + public func respondSuccess(topic: String, requestId: RPCID, protocolMethod: ProtocolMethod) async throws { + try await self.respondSuccess(topic: topic, requestId: requestId, protocolMethod: protocolMethod, envelopeType: .type0) } - public func respondError(topic: String, requestId: RPCID, tag: Int, reason: Reason) async throws { - try await self.respondError(topic: topic, requestId: requestId, tag: tag, reason: reason, envelopeType: .type0) + public func respondError(topic: String, requestId: RPCID, protocolMethod: ProtocolMethod, reason: Reason) async throws { + try await self.respondError(topic: topic, requestId: requestId, protocolMethod: protocolMethod, reason: reason, envelopeType: .type0) } } diff --git a/Sources/WalletConnectNetworking/NetworkInteractor.swift b/Sources/WalletConnectNetworking/NetworkInteractor.swift index b8a5d8b9b..a781b2049 100644 --- a/Sources/WalletConnectNetworking/NetworkInteractor.swift +++ b/Sources/WalletConnectNetworking/NetworkInteractor.swift @@ -120,21 +120,21 @@ public class NetworkingInteractor: NetworkInteracting { } } - public func respond(topic: String, response: RPCResponse, tag: Int, envelopeType: Envelope.EnvelopeType) async throws { + public func respond(topic: String, response: RPCResponse, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws { try rpcHistory.resolve(response) let message = try! serializer.serialize(topic: topic, encodable: response, envelopeType: envelopeType) - try await relayClient.publish(topic: topic, payload: message, tag: tag) + try await relayClient.publish(topic: topic, payload: message, tag: protocolMethod.responseTag) } - public func respondSuccess(topic: String, requestId: RPCID, tag: Int, envelopeType: Envelope.EnvelopeType) async throws { + public func respondSuccess(topic: String, requestId: RPCID, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws { let response = RPCResponse(id: requestId, result: true) - try await respond(topic: topic, response: response, tag: tag, envelopeType: envelopeType) + try await respond(topic: topic, response: response, protocolMethod: protocolMethod, envelopeType: envelopeType) } - public func respondError(topic: String, requestId: RPCID, tag: Int, reason: Reason, envelopeType: Envelope.EnvelopeType) async throws { + public func respondError(topic: String, requestId: RPCID, protocolMethod: ProtocolMethod, reason: Reason, envelopeType: Envelope.EnvelopeType) async throws { let error = JSONRPCError(code: reason.code, message: reason.message) let response = RPCResponse(id: requestId, error: error) - try await respond(topic: topic, response: response, tag: tag, envelopeType: envelopeType) + try await respond(topic: topic, response: response, protocolMethod: protocolMethod, envelopeType: envelopeType) } private func manageSubscription(_ topic: String, _ encodedEnvelope: String) { diff --git a/Sources/WalletConnectPairing/Services/PingResponder.swift b/Sources/WalletConnectPairing/Services/PingResponder.swift index 7718f6128..60835c29c 100644 --- a/Sources/WalletConnectPairing/Services/PingResponder.swift +++ b/Sources/WalletConnectPairing/Services/PingResponder.swift @@ -22,7 +22,7 @@ public class PingResponder { .sink { [unowned self] (payload: RequestSubscriptionPayload) in logger.debug("Responding for pairing ping") Task(priority: .high) { - try? await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, tag: method.responseTag) + try? await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, protocolMethod: method) } } .store(in: &publishers) diff --git a/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift b/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift index cfe3506cc..126ea595b 100644 --- a/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift +++ b/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift @@ -89,7 +89,7 @@ final class ApproveEngine { let result = SessionType.ProposeResponse(relay: relay, responderPublicKey: selfPublicKey.hexRepresentation) let response = RPCResponse(id: payload.id, result: result) - try await networkingInteractor.respond(topic: payload.topic, response: response, tag: SignProtocolMethod.sessionPropose.responseTag) + try await networkingInteractor.respond(topic: payload.topic, response: response, protocolMethod: SignProtocolMethod.sessionPropose) try pairing.updateExpiry() pairingStore.setPairing(pairing) @@ -102,7 +102,7 @@ final class ApproveEngine { throw Errors.proposalPayloadsNotFound } proposalPayloadsStore.delete(forKey: proposerPubKey) - try await networkingInteractor.respondError(topic: payload.topic, requestId: payload.id, tag: SignProtocolMethod.sessionPropose.responseTag, reason: reason) + try await networkingInteractor.respondError(topic: payload.topic, requestId: payload.id, protocolMethod: SignProtocolMethod.sessionPropose, reason: reason) // TODO: Delete pairing if inactive } @@ -189,10 +189,10 @@ private extension ApproveEngine { }.store(in: &publishers) } - func respondError(payload: SubscriptionPayload, reason: ReasonCode, tag: Int) { + func respondError(payload: SubscriptionPayload, reason: ReasonCode, protocolMethod: ProtocolMethod) { Task(priority: .high) { do { - try await networkingInteractor.respondError(topic: payload.topic, requestId: payload.id, tag: tag, reason: reason) + try await networkingInteractor.respondError(topic: payload.topic, requestId: payload.id, protocolMethod: protocolMethod, reason: reason) } catch { logger.error("Respond Error failed with: \(error.localizedDescription)") } @@ -292,7 +292,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, tag: SignProtocolMethod.sessionPropose.responseTag) + return respondError(payload: payload, reason: .invalidUpdateRequest, protocolMethod: SignProtocolMethod.sessionPropose) } proposalPayloadsStore.set(payload, forKey: proposal.proposer.publicKey) onSessionProposal?(proposal.publicRepresentation()) @@ -303,10 +303,10 @@ private extension ApproveEngine { func handleSessionSettleRequest(payload: RequestSubscriptionPayload) { logger.debug("Did receive session settle request") - let tag = SignProtocolMethod.sessionSettle.responseTag + let protocolMethod = SignProtocolMethod.sessionSettle guard let proposedNamespaces = settlingProposal?.requiredNamespaces else { - return respondError(payload: payload, reason: .invalidUpdateRequest, tag: tag) + return respondError(payload: payload, reason: .invalidUpdateRequest, protocolMethod: protocolMethod) } settlingProposal = nil @@ -318,9 +318,9 @@ private extension ApproveEngine { try Namespace.validate(sessionNamespaces) try Namespace.validateApproved(sessionNamespaces, against: proposedNamespaces) } catch WalletConnectError.unsupportedNamespace(let reason) { - return respondError(payload: payload, reason: reason, tag: tag) + return respondError(payload: payload, reason: reason, protocolMethod: protocolMethod) } catch { - return respondError(payload: payload, reason: .invalidUpdateRequest, tag: tag) + return respondError(payload: payload, reason: .invalidUpdateRequest, protocolMethod: protocolMethod) } let topic = payload.topic @@ -344,7 +344,7 @@ private extension ApproveEngine { ) sessionStore.setSession(session) Task(priority: .high) { - try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, tag: tag) + try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, protocolMethod: protocolMethod) } onSessionSettle?(session.publicRepresentation()) } diff --git a/Sources/WalletConnectSign/Engine/Common/PairingEngine.swift b/Sources/WalletConnectSign/Engine/Common/PairingEngine.swift index d6a012de0..6a9b7cae3 100644 --- a/Sources/WalletConnectSign/Engine/Common/PairingEngine.swift +++ b/Sources/WalletConnectSign/Engine/Common/PairingEngine.swift @@ -94,7 +94,7 @@ private extension PairingEngine { networkingInteractor.requestSubscription(on: SignProtocolMethod.pairingPing) .sink { [unowned self] (payload: RequestSubscriptionPayload) in Task(priority: .high) { - try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, tag: SignProtocolMethod.pairingPing.responseTag) + try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, protocolMethod: SignProtocolMethod.pairingPing) } } .store(in: &publishers) diff --git a/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift b/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift index 4ed7a78bd..ec8c1911c 100644 --- a/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift +++ b/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift @@ -68,7 +68,7 @@ final class SessionEngine { throw Errors.sessionNotFound(topic: topic) } let response = RPCResponse(id: requestId, result: response) - try await networkingInteractor.respond(topic: topic, response: response, tag: 1109) // FIXME: Hardcoded tag + try await networkingInteractor.respond(topic: topic, response: response, protocolMethod: SignProtocolMethod.sessionRequest) } func emit(topic: String, event: SessionType.EventParams.Event, chainId: Blockchain) async throws { @@ -141,10 +141,10 @@ private extension SessionEngine { } } - func respondError(payload: SubscriptionPayload, reason: ReasonCode, tag: Int) { + func respondError(payload: SubscriptionPayload, reason: ReasonCode, protocolMethod: ProtocolMethod) { Task(priority: .high) { do { - try await networkingInteractor.respondError(topic: payload.topic, requestId: payload.id, tag: tag, reason: reason) + try await networkingInteractor.respondError(topic: payload.topic, requestId: payload.id, protocolMethod: protocolMethod, reason: reason) } catch { logger.error("Respond Error failed with: \(error.localizedDescription)") } @@ -152,21 +152,21 @@ private extension SessionEngine { } func onSessionDelete(payload: RequestSubscriptionPayload) { - let tag = SignProtocolMethod.sessionDelete.responseTag + let protocolMethod = SignProtocolMethod.sessionDelete let topic = payload.topic guard sessionStore.hasSession(forTopic: topic) else { - return respondError(payload: payload, reason: .noSessionForTopic, tag: tag) + return respondError(payload: payload, reason: .noSessionForTopic, protocolMethod: protocolMethod) } sessionStore.delete(topic: topic) networkingInteractor.unsubscribe(topic: topic) Task(priority: .high) { - try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, tag: tag) + try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, protocolMethod: protocolMethod) } onSessionDelete?(topic, payload.request) } func onSessionRequest(payload: RequestSubscriptionPayload) { - let tag = SignProtocolMethod.sessionRequest.responseTag + let protocolMethod = SignProtocolMethod.sessionRequest let topic = payload.topic let request = Request( id: payload.id, @@ -176,35 +176,35 @@ private extension SessionEngine { chainId: payload.request.chainId) guard let session = sessionStore.getSession(forTopic: topic) else { - return respondError(payload: payload, reason: .noSessionForTopic, tag: tag) + return respondError(payload: payload, reason: .noSessionForTopic, protocolMethod: protocolMethod) } guard session.hasNamespace(for: request.chainId) else { - return respondError(payload: payload, reason: .unauthorizedChain, tag: tag) + return respondError(payload: payload, reason: .unauthorizedChain, protocolMethod: protocolMethod) } guard session.hasPermission(forMethod: request.method, onChain: request.chainId) else { - return respondError(payload: payload, reason: .unauthorizedMethod(request.method), tag: tag) + return respondError(payload: payload, reason: .unauthorizedMethod(request.method), protocolMethod: protocolMethod) } onSessionRequest?(request) } func onSessionPing(payload: SubscriptionPayload) { Task(priority: .high) { - try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, tag: SignProtocolMethod.sessionPing.responseTag) + try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, protocolMethod: SignProtocolMethod.sessionPing) } } func onSessionEvent(payload: RequestSubscriptionPayload) { - let tag = SignProtocolMethod.sessionEvent.responseTag + let protocolMethod = SignProtocolMethod.sessionEvent let event = payload.request.event let topic = payload.topic guard let session = sessionStore.getSession(forTopic: topic) else { - return respondError(payload: payload, reason: .noSessionForTopic, tag: tag) + return respondError(payload: payload, reason: .noSessionForTopic, protocolMethod: protocolMethod) } guard session.peerIsController, session.hasPermission(forEvent: event.name, onChain: payload.request.chainId) else { - return respondError(payload: payload, reason: .unauthorizedEvent(event.name), tag: tag) + return respondError(payload: payload, reason: .unauthorizedEvent(event.name), protocolMethod: protocolMethod) } Task(priority: .high) { - try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, tag: tag) + try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, protocolMethod: protocolMethod) } onEventReceived?(topic, event.publicRepresentation(), payload.request.chainId) } diff --git a/Sources/WalletConnectSign/Engine/NonController/NonControllerSessionStateMachine.swift b/Sources/WalletConnectSign/Engine/NonController/NonControllerSessionStateMachine.swift index 7cca1066b..a2af79032 100644 --- a/Sources/WalletConnectSign/Engine/NonController/NonControllerSessionStateMachine.swift +++ b/Sources/WalletConnectSign/Engine/NonController/NonControllerSessionStateMachine.swift @@ -38,10 +38,10 @@ final class NonControllerSessionStateMachine { }.store(in: &publishers) } - private func respondError(payload: SubscriptionPayload, reason: ReasonCode, tag: Int) { + private func respondError(payload: SubscriptionPayload, reason: ReasonCode, protocolMethod: ProtocolMethod) { Task(priority: .high) { do { - try await networkingInteractor.respondError(topic: payload.topic, requestId: payload.id, tag: tag, reason: reason) + try await networkingInteractor.respondError(topic: payload.topic, requestId: payload.id, protocolMethod: protocolMethod, reason: reason) } catch { logger.error("Respond Error failed with: \(error.localizedDescription)") } @@ -50,48 +50,50 @@ final class NonControllerSessionStateMachine { // TODO: Update stored session namespaces private func onSessionUpdateNamespacesRequest(payload: SubscriptionPayload, updateParams: SessionType.UpdateParams) { + let protocolMethod = SignProtocolMethod.sessionUpdate do { try Namespace.validate(updateParams.namespaces) } catch { - return respondError(payload: payload, reason: .invalidUpdateRequest, tag: SignProtocolMethod.sessionUpdate.responseTag) + return respondError(payload: payload, reason: .invalidUpdateRequest, protocolMethod: protocolMethod) } guard var session = sessionStore.getSession(forTopic: payload.topic) else { - return respondError(payload: payload, reason: .noSessionForTopic, tag: SignProtocolMethod.sessionUpdate.responseTag) + return respondError(payload: payload, reason: .noSessionForTopic, protocolMethod: protocolMethod) } guard session.peerIsController else { - return respondError(payload: payload, reason: .unauthorizedUpdateRequest, tag: SignProtocolMethod.sessionUpdate.responseTag) + return respondError(payload: payload, reason: .unauthorizedUpdateRequest, protocolMethod: protocolMethod) } do { try session.updateNamespaces(updateParams.namespaces, timestamp: payload.id.timestamp) } catch { - return respondError(payload: payload, reason: .invalidUpdateRequest, tag: SignProtocolMethod.sessionUpdate.responseTag) + return respondError(payload: payload, reason: .invalidUpdateRequest, protocolMethod: protocolMethod) } sessionStore.setSession(session) Task(priority: .high) { - try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, tag: SignProtocolMethod.sessionUpdate.responseTag) + try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, protocolMethod: protocolMethod) } onNamespacesUpdate?(session.topic, updateParams.namespaces) } private func onSessionUpdateExpiry(payload: SubscriptionPayload, updateExpiryParams: SessionType.UpdateExpiryParams) { + let protocolMethod = SignProtocolMethod.sessionExtend let topic = payload.topic guard var session = sessionStore.getSession(forTopic: topic) else { - return respondError(payload: payload, reason: .noSessionForTopic, tag: SignProtocolMethod.sessionExtend.responseTag) + return respondError(payload: payload, reason: .noSessionForTopic, protocolMethod: protocolMethod) } guard session.peerIsController else { - return respondError(payload: payload, reason: .unauthorizedExtendRequest, tag: SignProtocolMethod.sessionExtend.responseTag) + return respondError(payload: payload, reason: .unauthorizedExtendRequest, protocolMethod: protocolMethod) } do { try session.updateExpiry(to: updateExpiryParams.expiry) } catch { - return respondError(payload: payload, reason: .invalidExtendRequest, tag: SignProtocolMethod.sessionExtend.responseTag) + return respondError(payload: payload, reason: .invalidExtendRequest, protocolMethod: protocolMethod) } sessionStore.setSession(session) Task(priority: .high) { - try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, tag: SignProtocolMethod.sessionExtend.responseTag) + try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, protocolMethod: SignProtocolMethod.sessionExtend) } onExtend?(session.topic, session.expiryDate) From 8a04461ad381fca904f684204f0f4b5bb2c738b5 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Mon, 26 Sep 2022 12:05:14 +0200 Subject: [PATCH 03/10] refactor protocol methods in auth and chat --- Sources/Auth/AuthProtocolMethod.swift | 54 +++++++++---------- .../Auth/Services/App/AppRequestService.swift | 2 +- .../Services/App/AppRespondSubscriber.swift | 4 +- .../Common/DeletePairingService.swift | 5 +- .../Wallet/WalletErrorResponder.swift | 2 +- .../Wallet/WalletRequestSubscriber.swift | 2 +- .../Wallet/WalletRespondService.swift | 2 +- .../Common/MessagingService.swift | 11 ++-- .../Invitee/InvitationHandlingService.swift | 8 +-- .../Inviter/InviteService.swift | 7 +-- Sources/Chat/Types/ChatProtocolMethod.swift | 46 ++++++---------- .../NetworkInteracting.swift | 2 +- .../NetworkInteractor.swift | 17 ++---- .../ProtocolMethod.swift | 14 ++++- .../PairingProtocolMethod.swift | 16 ++---- .../Services/PairingPingService.swift | 7 +-- .../Engine/Common/PairingEngine.swift | 2 +- .../NetworkingInteractorMock.swift | 2 +- 18 files changed, 91 insertions(+), 112 deletions(-) diff --git a/Sources/Auth/AuthProtocolMethod.swift b/Sources/Auth/AuthProtocolMethod.swift index c3df72dae..c15cc76ef 100644 --- a/Sources/Auth/AuthProtocolMethod.swift +++ b/Sources/Auth/AuthProtocolMethod.swift @@ -1,34 +1,28 @@ import Foundation import WalletConnectNetworking -enum AuthProtocolMethod: String, ProtocolMethod { - case authRequest = "wc_authRequest" - case pairingDelete = "wc_pairingDelete" - case pairingPing = "wc_pairingPing" - - var method: String { - return self.rawValue - } - - var requestTag: Int { - switch self { - case .authRequest: - return 3000 - case .pairingDelete: - return 1000 - case .pairingPing: - return 1002 - } - } - - var responseTag: Int { - switch self { - case .authRequest: - return 3001 - case .pairingDelete: - return 1001 - case .pairingPing: - return 1003 - } - } +struct AuthRequestProtocolMethod: ProtocolMethod { + var method: String = "wc_authRequest" + + var request = RelayConfigrable(tag: 3000, prompt: true) + + var response = RelayConfigrable(tag: 3001, prompt: false) +} + + +struct PairingPingProtocolMethod: ProtocolMethod { + var method: String = "wc_pairingPing" + + var request = RelayConfigrable(tag: 1002, prompt: false) + + var response = RelayConfigrable(tag: 1003, prompt: false) +} + + +struct PairingDeleteProtocolMethod: ProtocolMethod { + var method: String = "wc_pairingDelete" + + var request = RelayConfigrable(tag: 1000, prompt: false) + + var response = RelayConfigrable(tag: 1001, prompt: false) } diff --git a/Sources/Auth/Services/App/AppRequestService.swift b/Sources/Auth/Services/App/AppRequestService.swift index da88c993a..54c492e8a 100644 --- a/Sources/Auth/Services/App/AppRequestService.swift +++ b/Sources/Auth/Services/App/AppRequestService.swift @@ -30,7 +30,7 @@ actor AppRequestService { let request = RPCRequest(method: "wc_authRequest", params: params) try kms.setPublicKey(publicKey: pubKey, for: responseTopic) logger.debug("AppRequestService: Subscribibg for response topic: \(responseTopic)") - try await networkingInteractor.requestNetworkAck(request, topic: topic, tag: AuthProtocolMethod.authRequest.responseTag) + try await networkingInteractor.requestNetworkAck(request, topic: topic, protocolMethod: AuthRequestProtocolMethod()) try await networkingInteractor.subscribe(topic: responseTopic) } } diff --git a/Sources/Auth/Services/App/AppRespondSubscriber.swift b/Sources/Auth/Services/App/AppRespondSubscriber.swift index a0e9f8c40..542d4e27c 100644 --- a/Sources/Auth/Services/App/AppRespondSubscriber.swift +++ b/Sources/Auth/Services/App/AppRespondSubscriber.swift @@ -32,13 +32,13 @@ class AppRespondSubscriber { } private func subscribeForResponse() { - networkingInteractor.responseErrorSubscription(on: AuthProtocolMethod.authRequest) + networkingInteractor.responseErrorSubscription(on: AuthRequestProtocolMethod()) .sink { [unowned self] (payload: ResponseSubscriptionErrorPayload) in guard let error = AuthError(code: payload.error.code) else { return } onResponse?(payload.id, .failure(error)) }.store(in: &publishers) - networkingInteractor.responseSubscription(on: AuthProtocolMethod.authRequest) + networkingInteractor.responseSubscription(on: AuthRequestProtocolMethod()) .sink { [unowned self] (payload: ResponseSubscriptionPayload) in activatePairingIfNeeded(id: payload.id) diff --git a/Sources/Auth/Services/Common/DeletePairingService.swift b/Sources/Auth/Services/Common/DeletePairingService.swift index 4ec44f20e..414118348 100644 --- a/Sources/Auth/Services/Common/DeletePairingService.swift +++ b/Sources/Auth/Services/Common/DeletePairingService.swift @@ -26,10 +26,11 @@ class DeletePairingService { func delete(topic: String) async throws { guard pairingStorage.hasPairing(forTopic: topic) else { throw Errors.pairingNotFound} + let protocolMethod = PairingDeleteProtocolMethod() let reason = AuthError.userDisconnected logger.debug("Will delete pairing for reason: message: \(reason.message) code: \(reason.code)") - let request = RPCRequest(method: AuthProtocolMethod.pairingDelete.rawValue, params: reason) - try await networkingInteractor.request(request, topic: topic, protocolMethod: AuthProtocolMethod.pairingDelete) + let request = RPCRequest(method: protocolMethod.method, params: reason) + try await networkingInteractor.request(request, topic: topic, protocolMethod: protocolMethod) pairingStorage.delete(topic: topic) kms.deleteSymmetricKey(for: topic) networkingInteractor.unsubscribe(topic: topic) diff --git a/Sources/Auth/Services/Wallet/WalletErrorResponder.swift b/Sources/Auth/Services/Wallet/WalletErrorResponder.swift index b774c3865..491cf73ad 100644 --- a/Sources/Auth/Services/Wallet/WalletErrorResponder.swift +++ b/Sources/Auth/Services/Wallet/WalletErrorResponder.swift @@ -33,7 +33,7 @@ actor WalletErrorResponder { try kms.setAgreementSecret(keys, topic: topic) let envelopeType = Envelope.EnvelopeType.type1(pubKey: keys.publicKey.rawRepresentation) - try await networkingInteractor.respondError(topic: topic, requestId: requestId, protocolMethod: AuthProtocolMethod.authRequest, reason: error, envelopeType: envelopeType) + try await networkingInteractor.respondError(topic: topic, requestId: requestId, protocolMethod: AuthRequestProtocolMethod(), reason: error, envelopeType: envelopeType) } private func getAuthRequestParams(requestId: RPCID) throws -> AuthRequestParams { diff --git a/Sources/Auth/Services/Wallet/WalletRequestSubscriber.swift b/Sources/Auth/Services/Wallet/WalletRequestSubscriber.swift index c204cc526..05d028fe1 100644 --- a/Sources/Auth/Services/Wallet/WalletRequestSubscriber.swift +++ b/Sources/Auth/Services/Wallet/WalletRequestSubscriber.swift @@ -33,7 +33,7 @@ class WalletRequestSubscriber { private func subscribeForRequest() { guard let address = address else { return } - networkingInteractor.requestSubscription(on: AuthProtocolMethod.authRequest) + networkingInteractor.requestSubscription(on: AuthRequestProtocolMethod()) .sink { [unowned self] (payload: RequestSubscriptionPayload) in logger.debug("WalletRequestSubscriber: Received request") guard let message = messageFormatter.formatMessage(from: payload.request.payloadParams, address: address) else { diff --git a/Sources/Auth/Services/Wallet/WalletRespondService.swift b/Sources/Auth/Services/Wallet/WalletRespondService.swift index c9529c197..32090f956 100644 --- a/Sources/Auth/Services/Wallet/WalletRespondService.swift +++ b/Sources/Auth/Services/Wallet/WalletRespondService.swift @@ -39,7 +39,7 @@ actor WalletRespondService { let responseParams = AuthResponseParams(h: header, p: payload, s: signature) let response = RPCResponse(id: requestId, result: responseParams) - try await networkingInteractor.respond(topic: topic, response: response, protocolMethod: AuthProtocolMethod.authRequest, envelopeType: .type1(pubKey: keys.publicKey.rawRepresentation)) + try await networkingInteractor.respond(topic: topic, response: response, protocolMethod: AuthRequestProtocolMethod(), envelopeType: .type1(pubKey: keys.publicKey.rawRepresentation)) } func respondError(requestId: RPCID) async throws { diff --git a/Sources/Chat/ProtocolServices/Common/MessagingService.swift b/Sources/Chat/ProtocolServices/Common/MessagingService.swift index 027afc5d8..ea13faca4 100644 --- a/Sources/Chat/ProtocolServices/Common/MessagingService.swift +++ b/Sources/Chat/ProtocolServices/Common/MessagingService.swift @@ -29,12 +29,13 @@ class MessagingService { func send(topic: String, messageString: String) async throws { // TODO - manage author account + let protocolMethod = ChatMessageProtocolMethod() let thread = await threadStore.first {$0.topic == topic} guard let authorAccount = thread?.selfAccount else { throw Errors.threadDoNotExist} let timestamp = Int64(Date().timeIntervalSince1970 * 1000) let message = Message(topic: topic, message: messageString, authorAccount: authorAccount, timestamp: timestamp) - let request = RPCRequest(method: ChatProtocolMethod.message.method, params: message) - try await networkingInteractor.request(request, topic: topic, protocolMethod: ChatProtocolMethod.message) + let request = RPCRequest(method: protocolMethod.method, params: message) + try await networkingInteractor.request(request, topic: topic, protocolMethod: protocolMethod) Task(priority: .background) { await messagesStore.add(message) onMessage?(message) @@ -42,14 +43,14 @@ class MessagingService { } private func setUpResponseHandling() { - networkingInteractor.responseSubscription(on: ChatProtocolMethod.message) + networkingInteractor.responseSubscription(on: ChatMessageProtocolMethod()) .sink { [unowned self] (payload: ResponseSubscriptionPayload) in logger.debug("Received Message response") }.store(in: &publishers) } private func setUpRequestHandling() { - networkingInteractor.requestSubscription(on: ChatProtocolMethod.message) + networkingInteractor.requestSubscription(on: ChatMessageProtocolMethod()) .sink { [unowned self] (payload: RequestSubscriptionPayload) in var message = payload.request message.topic = payload.topic @@ -59,7 +60,7 @@ class MessagingService { private func handleMessage(_ message: Message, topic: String, requestId: RPCID) { Task(priority: .background) { - try await networkingInteractor.respondSuccess(topic: topic, requestId: requestId, protocolMethod: ChatProtocolMethod.message) + try await networkingInteractor.respondSuccess(topic: topic, requestId: requestId, protocolMethod: ChatMessageProtocolMethod()) await messagesStore.add(message) logger.debug("Received message") onMessage?(message) diff --git a/Sources/Chat/ProtocolServices/Invitee/InvitationHandlingService.swift b/Sources/Chat/ProtocolServices/Invitee/InvitationHandlingService.swift index 1c07311b0..e3e30028c 100644 --- a/Sources/Chat/ProtocolServices/Invitee/InvitationHandlingService.swift +++ b/Sources/Chat/ProtocolServices/Invitee/InvitationHandlingService.swift @@ -39,6 +39,8 @@ class InvitationHandlingService { } func accept(inviteId: String) async throws { + let protocolMethod = ChatInviteProtocolMethod() + guard let payload = try invitePayloadStore.get(key: inviteId) else { throw Error.inviteForIdNotFound } let selfThreadPubKey = try kms.createX25519KeyPair() @@ -47,7 +49,7 @@ class InvitationHandlingService { let response = RPCResponse(id: payload.id, result: inviteResponse) let responseTopic = try getInviteResponseTopic(requestTopic: payload.topic, invite: payload.request) - try await networkingInteractor.respond(topic: responseTopic, response: response, protocolMethod: ChatProtocolMethod.invite) + try await networkingInteractor.respond(topic: responseTopic, response: response, protocolMethod: protocolMethod) let threadAgreementKeys = try kms.performKeyAgreement(selfPublicKey: selfThreadPubKey, peerPublicKey: payload.request.publicKey) let threadTopic = threadAgreementKeys.derivedTopic() @@ -71,13 +73,13 @@ class InvitationHandlingService { let responseTopic = try getInviteResponseTopic(requestTopic: payload.topic, invite: payload.request) - try await networkingInteractor.respondError(topic: responseTopic, requestId: payload.id, protocolMethod: ChatProtocolMethod.invite, reason: ChatError.userRejected) + try await networkingInteractor.respondError(topic: responseTopic, requestId: payload.id, protocolMethod: ChatInviteProtocolMethod(), reason: ChatError.userRejected) invitePayloadStore.delete(forKey: inviteId) } private func setUpRequestHandling() { - networkingInteractor.requestSubscription(on: ChatProtocolMethod.invite) + networkingInteractor.requestSubscription(on: ChatInviteProtocolMethod()) .sink { [unowned self] (payload: RequestSubscriptionPayload) in logger.debug("did receive an invite") invitePayloadStore.set(payload, forKey: payload.request.publicKey) diff --git a/Sources/Chat/ProtocolServices/Inviter/InviteService.swift b/Sources/Chat/ProtocolServices/Inviter/InviteService.swift index 706baddc1..9568bfac4 100644 --- a/Sources/Chat/ProtocolServices/Inviter/InviteService.swift +++ b/Sources/Chat/ProtocolServices/Inviter/InviteService.swift @@ -33,6 +33,7 @@ class InviteService { func invite(peerPubKey: String, peerAccount: Account, openingMessage: String, account: Account) async throws { // TODO ad storage + let protocolMethod = ChatInviteProtocolMethod() self.peerAccount = peerAccount let selfPubKeyY = try kms.createX25519KeyPair() let invite = Invite(message: openingMessage, account: account, publicKey: selfPubKeyY.hexRepresentation) @@ -42,7 +43,7 @@ class InviteService { // overrides on invite toipic try kms.setSymmetricKey(symKeyI.sharedKey, for: inviteTopic) - let request = RPCRequest(method: ChatProtocolMethod.invite.method, params: invite) + let request = RPCRequest(method: protocolMethod.method, params: invite) // 2. Proposer subscribes to topic R which is the hash of the derived symKey let responseTopic = symKeyI.derivedTopic() @@ -50,13 +51,13 @@ class InviteService { try kms.setSymmetricKey(symKeyI.sharedKey, for: responseTopic) try await networkingInteractor.subscribe(topic: responseTopic) - try await networkingInteractor.request(request, topic: inviteTopic, protocolMethod: ChatProtocolMethod.invite, envelopeType: .type1(pubKey: selfPubKeyY.rawRepresentation)) + try await networkingInteractor.request(request, topic: inviteTopic, protocolMethod: protocolMethod, envelopeType: .type1(pubKey: selfPubKeyY.rawRepresentation)) logger.debug("invite sent on topic: \(inviteTopic)") } private func setUpResponseHandling() { - networkingInteractor.responseSubscription(on: ChatProtocolMethod.invite) + networkingInteractor.responseSubscription(on: ChatInviteProtocolMethod()) .sink { [unowned self] (payload: ResponseSubscriptionPayload) in logger.debug("Invite has been accepted") diff --git a/Sources/Chat/Types/ChatProtocolMethod.swift b/Sources/Chat/Types/ChatProtocolMethod.swift index a32e5d4bf..6e5197f60 100644 --- a/Sources/Chat/Types/ChatProtocolMethod.swift +++ b/Sources/Chat/Types/ChatProtocolMethod.swift @@ -1,34 +1,20 @@ import Foundation import WalletConnectNetworking -enum ChatProtocolMethod: ProtocolMethod { - case invite - case message - - var requestTag: Int { - switch self { - case .invite: - return 2000 - case .message: - return 2002 - } - } - - var responseTag: Int { - switch self { - case .invite: - return 2001 - case .message: - return 2003 - } - } - - var method: String { - switch self { - case .invite: - return "wc_chatInvite" - case .message: - return "wc_chatMessage" - } - } +struct ChatInviteProtocolMethod: ProtocolMethod { + var method: String = "wc_chatInvite" + + var request = RelayConfigrable(tag: 2000, prompt: true) + + var response = RelayConfigrable(tag: 2001, prompt: false) + +} + +struct ChatMessageProtocolMethod: ProtocolMethod { + var method: String = "wc_chatMessage" + + var request = RelayConfigrable(tag: 2002, prompt: true) + + var response = RelayConfigrable(tag: 2003, prompt: false) + } diff --git a/Sources/WalletConnectNetworking/NetworkInteracting.swift b/Sources/WalletConnectNetworking/NetworkInteracting.swift index 33d5cea2d..665834735 100644 --- a/Sources/WalletConnectNetworking/NetworkInteracting.swift +++ b/Sources/WalletConnectNetworking/NetworkInteracting.swift @@ -9,7 +9,7 @@ public protocol NetworkInteracting { func subscribe(topic: String) async throws func unsubscribe(topic: String) func request(_ request: RPCRequest, topic: String, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws - func requestNetworkAck(_ request: RPCRequest, topic: String, tag: Int) async throws + func requestNetworkAck(_ request: RPCRequest, topic: String, protocolMethod: ProtocolMethod) async throws func respond(topic: String, response: RPCResponse, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws func respondSuccess(topic: String, requestId: RPCID, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws func respondError(topic: String, requestId: RPCID, protocolMethod: ProtocolMethod, reason: Reason, envelopeType: Envelope.EnvelopeType) async throws diff --git a/Sources/WalletConnectNetworking/NetworkInteractor.swift b/Sources/WalletConnectNetworking/NetworkInteractor.swift index a781b2049..339c87656 100644 --- a/Sources/WalletConnectNetworking/NetworkInteractor.swift +++ b/Sources/WalletConnectNetworking/NetworkInteractor.swift @@ -96,18 +96,18 @@ public class NetworkingInteractor: NetworkInteracting { public func request(_ request: RPCRequest, topic: String, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws { try rpcHistory.set(request, forTopic: topic, emmitedBy: .local) let message = try! serializer.serialize(topic: topic, encodable: request, envelopeType: envelopeType) - try await relayClient.publish(topic: topic, payload: message, tag: protocolMethod.requestTag, prompt: shouldPrompt(method: request.method)) + try await relayClient.publish(topic: topic, payload: message, tag: protocolMethod.request.tag, prompt: protocolMethod.request.prompt) } /// Completes with an acknowledgement from the relay network. /// completes with error if networking client was not able to send a message /// TODO - relay client should provide async function - continualion should be removed from here - public func requestNetworkAck(_ request: RPCRequest, topic: String, tag: Int) async throws { + public func requestNetworkAck(_ request: RPCRequest, topic: String, protocolMethod: ProtocolMethod) async throws { do { try rpcHistory.set(request, forTopic: topic, emmitedBy: .local) let message = try serializer.serialize(topic: topic, encodable: request) return try await withCheckedThrowingContinuation { continuation in - relayClient.publish(topic: topic, payload: message, tag: tag, prompt: shouldPrompt(method: request.method)) { error in + relayClient.publish(topic: topic, payload: message, tag: protocolMethod.request.tag, prompt: protocolMethod.request.prompt) { error in if let error = error { continuation.resume(throwing: error) } else { @@ -123,7 +123,7 @@ public class NetworkingInteractor: NetworkInteracting { public func respond(topic: String, response: RPCResponse, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws { try rpcHistory.resolve(response) let message = try! serializer.serialize(topic: topic, encodable: response, envelopeType: envelopeType) - try await relayClient.publish(topic: topic, payload: message, tag: protocolMethod.responseTag) + try await relayClient.publish(topic: topic, payload: message, tag: protocolMethod.response.tag) } public func respondSuccess(topic: String, requestId: RPCID, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws { @@ -165,13 +165,4 @@ public class NetworkingInteractor: NetworkInteracting { logger.debug("Handle json rpc response error: \(error)") } } - - private func shouldPrompt(method: String) -> Bool { - switch method { - case "wc_sessionRequest": // TODO: Include promt in ProtocolMethod - return true - default: - return false - } - } } diff --git a/Sources/WalletConnectNetworking/ProtocolMethod.swift b/Sources/WalletConnectNetworking/ProtocolMethod.swift index dea8bd255..10a8c5e7b 100644 --- a/Sources/WalletConnectNetworking/ProtocolMethod.swift +++ b/Sources/WalletConnectNetworking/ProtocolMethod.swift @@ -2,6 +2,16 @@ import Foundation public protocol ProtocolMethod { var method: String { get } - var requestTag: Int { get } - var responseTag: Int { get } + var request: RelayConfigrable { get } + var response: RelayConfigrable { get } +} + +public struct RelayConfigrable { + var tag: Int + var prompt: Bool + + public init(tag: Int, prompt: Bool) { + self.tag = tag + self.prompt = prompt + } } diff --git a/Sources/WalletConnectPairing/PairingProtocolMethod.swift b/Sources/WalletConnectPairing/PairingProtocolMethod.swift index a6f9d14cf..640517ff6 100644 --- a/Sources/WalletConnectPairing/PairingProtocolMethod.swift +++ b/Sources/WalletConnectPairing/PairingProtocolMethod.swift @@ -1,18 +1,10 @@ import Foundation import WalletConnectNetworking -enum PairingProtocolMethod: String, ProtocolMethod { - case ping = "wc_pairingPing" +struct PairingPingProtocolMethod: ProtocolMethod { + var method: String = "wc_pairingPing" - var method: String { - return self.rawValue - } + var request = RelayConfigrable(tag: 1002, prompt: false) - var requestTag: Int { - return 1002 - } - - var responseTag: Int { - return 1003 - } + var response = RelayConfigrable(tag: 1003, prompt: false) } diff --git a/Sources/WalletConnectPairing/Services/PairingPingService.swift b/Sources/WalletConnectPairing/Services/PairingPingService.swift index f435ec72b..053461717 100644 --- a/Sources/WalletConnectPairing/Services/PairingPingService.swift +++ b/Sources/WalletConnectPairing/Services/PairingPingService.swift @@ -21,10 +21,11 @@ public class PairingPingService { pairingStorage: WCPairingStorage, networkingInteractor: NetworkInteracting, logger: ConsoleLogging) { + let protocolMethod = PairingPingProtocolMethod() self.pairingStorage = pairingStorage - self.pingRequester = PingRequester(networkingInteractor: networkingInteractor, method: PairingProtocolMethod.ping) - self.pingResponder = PingResponder(networkingInteractor: networkingInteractor, method: PairingProtocolMethod.ping, logger: logger) - self.pingResponseSubscriber = PingResponseSubscriber(networkingInteractor: networkingInteractor, method: PairingProtocolMethod.ping, logger: logger) + self.pingRequester = PingRequester(networkingInteractor: networkingInteractor, method: protocolMethod) + self.pingResponder = PingResponder(networkingInteractor: networkingInteractor, method: protocolMethod, logger: logger) + self.pingResponseSubscriber = PingResponseSubscriber(networkingInteractor: networkingInteractor, method: protocolMethod, logger: logger) } public func ping(topic: String) async throws { diff --git a/Sources/WalletConnectSign/Engine/Common/PairingEngine.swift b/Sources/WalletConnectSign/Engine/Common/PairingEngine.swift index 6a9b7cae3..56a572022 100644 --- a/Sources/WalletConnectSign/Engine/Common/PairingEngine.swift +++ b/Sources/WalletConnectSign/Engine/Common/PairingEngine.swift @@ -73,7 +73,7 @@ final class PairingEngine { requiredNamespaces: namespaces) let request = RPCRequest(method: SignProtocolMethod.sessionPropose.method, params: proposal) - try await networkingInteractor.requestNetworkAck(request, topic: pairingTopic, tag: SignProtocolMethod.sessionPropose.requestTag) + try await networkingInteractor.requestNetworkAck(request, topic: pairingTopic, protocolMethod: SignProtocolMethod.sessionPropose) } } diff --git a/Tests/TestingUtils/NetworkingInteractorMock.swift b/Tests/TestingUtils/NetworkingInteractorMock.swift index 6f7fff08a..b3e6714bb 100644 --- a/Tests/TestingUtils/NetworkingInteractorMock.swift +++ b/Tests/TestingUtils/NetworkingInteractorMock.swift @@ -114,7 +114,7 @@ public class NetworkingInteractorMock: NetworkInteracting { didRespondError = true } - public func requestNetworkAck(_ request: RPCRequest, topic: String, tag: Int) async throws { + public func requestNetworkAck(_ request: RPCRequest, topic: String, protocolMethod: ProtocolMethod) async throws { requestCallCount += 1 requests.append((topic, request)) } From 5f0601f036e1e30e40cf7a5ae84bdfe391b9e661 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Mon, 26 Sep 2022 12:19:36 +0200 Subject: [PATCH 04/10] refactor sign protocol methods --- Sources/Auth/AuthProtocolMethod.swift | 12 +- Sources/Chat/Types/ChatProtocolMethod.swift | 8 +- .../ProtocolMethod.swift | 6 +- .../PairingProtocolMethod.swift | 4 +- .../Types/SignProtocolMethod.swift | 145 ++++++++++-------- 5 files changed, 95 insertions(+), 80 deletions(-) diff --git a/Sources/Auth/AuthProtocolMethod.swift b/Sources/Auth/AuthProtocolMethod.swift index c15cc76ef..4de009c42 100644 --- a/Sources/Auth/AuthProtocolMethod.swift +++ b/Sources/Auth/AuthProtocolMethod.swift @@ -4,25 +4,25 @@ import WalletConnectNetworking struct AuthRequestProtocolMethod: ProtocolMethod { var method: String = "wc_authRequest" - var request = RelayConfigrable(tag: 3000, prompt: true) + var request = RelayConfig(tag: 3000, prompt: true) - var response = RelayConfigrable(tag: 3001, prompt: false) + var response = RelayConfig(tag: 3001, prompt: false) } struct PairingPingProtocolMethod: ProtocolMethod { var method: String = "wc_pairingPing" - var request = RelayConfigrable(tag: 1002, prompt: false) + var request = RelayConfig(tag: 1002, prompt: false) - var response = RelayConfigrable(tag: 1003, prompt: false) + var response = RelayConfig(tag: 1003, prompt: false) } struct PairingDeleteProtocolMethod: ProtocolMethod { var method: String = "wc_pairingDelete" - var request = RelayConfigrable(tag: 1000, prompt: false) + var request = RelayConfig(tag: 1000, prompt: false) - var response = RelayConfigrable(tag: 1001, prompt: false) + var response = RelayConfig(tag: 1001, prompt: false) } diff --git a/Sources/Chat/Types/ChatProtocolMethod.swift b/Sources/Chat/Types/ChatProtocolMethod.swift index 6e5197f60..c6ca16dfe 100644 --- a/Sources/Chat/Types/ChatProtocolMethod.swift +++ b/Sources/Chat/Types/ChatProtocolMethod.swift @@ -4,17 +4,17 @@ import WalletConnectNetworking struct ChatInviteProtocolMethod: ProtocolMethod { var method: String = "wc_chatInvite" - var request = RelayConfigrable(tag: 2000, prompt: true) + var request = RelayConfig(tag: 2000, prompt: true) - var response = RelayConfigrable(tag: 2001, prompt: false) + var response = RelayConfig(tag: 2001, prompt: false) } struct ChatMessageProtocolMethod: ProtocolMethod { var method: String = "wc_chatMessage" - var request = RelayConfigrable(tag: 2002, prompt: true) + var request = RelayConfig(tag: 2002, prompt: true) - var response = RelayConfigrable(tag: 2003, prompt: false) + var response = RelayConfig(tag: 2003, prompt: false) } diff --git a/Sources/WalletConnectNetworking/ProtocolMethod.swift b/Sources/WalletConnectNetworking/ProtocolMethod.swift index 10a8c5e7b..21c8ad1b5 100644 --- a/Sources/WalletConnectNetworking/ProtocolMethod.swift +++ b/Sources/WalletConnectNetworking/ProtocolMethod.swift @@ -2,11 +2,11 @@ import Foundation public protocol ProtocolMethod { var method: String { get } - var request: RelayConfigrable { get } - var response: RelayConfigrable { get } + var request: RelayConfig { get } + var response: RelayConfig { get } } -public struct RelayConfigrable { +public struct RelayConfig { var tag: Int var prompt: Bool diff --git a/Sources/WalletConnectPairing/PairingProtocolMethod.swift b/Sources/WalletConnectPairing/PairingProtocolMethod.swift index 640517ff6..b4231dade 100644 --- a/Sources/WalletConnectPairing/PairingProtocolMethod.swift +++ b/Sources/WalletConnectPairing/PairingProtocolMethod.swift @@ -4,7 +4,7 @@ import WalletConnectNetworking struct PairingPingProtocolMethod: ProtocolMethod { var method: String = "wc_pairingPing" - var request = RelayConfigrable(tag: 1002, prompt: false) + var request = RelayConfig(tag: 1002, prompt: false) - var response = RelayConfigrable(tag: 1003, prompt: false) + var response = RelayConfig(tag: 1003, prompt: false) } diff --git a/Sources/WalletConnectSign/Types/SignProtocolMethod.swift b/Sources/WalletConnectSign/Types/SignProtocolMethod.swift index 90d4a0c54..87f727441 100644 --- a/Sources/WalletConnectSign/Types/SignProtocolMethod.swift +++ b/Sources/WalletConnectSign/Types/SignProtocolMethod.swift @@ -4,69 +4,84 @@ import WalletConnectPairing import WalletConnectUtils import WalletConnectNetworking -enum SignProtocolMethod: ProtocolMethod { - case pairingDelete - case pairingPing - case sessionPropose - case sessionSettle - case sessionUpdate - case sessionExtend - case sessionDelete - case sessionRequest - case sessionPing - case sessionEvent - - var method: String { - switch self { - case .pairingDelete: - return "wc_pairingDelete" - case .pairingPing: - return "wc_pairingPing" - case .sessionPropose: - return "wc_sessionPropose" - case .sessionSettle: - return "wc_sessionSettle" - case .sessionUpdate: - return "wc_sessionUpdate" - case .sessionExtend: - return "wc_sessionExtend" - case .sessionDelete: - return "wc_sessionDelete" - case .sessionRequest: - return "wc_sessionRequest" - case .sessionPing: - return "wc_sessionPing" - case .sessionEvent: - return "wc_sessionEvent" - } - } - - var requestTag: Int { - switch self { - case .pairingDelete: - return 1000 - case .pairingPing: - return 1002 - case .sessionPropose: - return 1100 - case .sessionSettle: - return 1102 - case .sessionUpdate: - return 1104 - case .sessionExtend: - return 1106 - case .sessionDelete: - return 1112 - case .sessionRequest: - return 1108 - case .sessionPing: - return 1114 - case .sessionEvent: - return 1110 - } - } - - var responseTag: Int { - return requestTag + 1 - } +struct PairingPingProtocolMethod: ProtocolMethod { + var method: String = "wc_pairingPing" + + var request = RelayConfig(tag: 1002, prompt: false) + + var response = RelayConfig(tag: 1003, prompt: false) +} + +struct PairingDeleteProtocolMethod: ProtocolMethod { + var method: String = "wc_pairingDelete" + + var request = RelayConfig(tag: 1000, prompt: false) + + var response = RelayConfig(tag: 1001, prompt: false) +} + + + + +struct SessionProposeProtocolMethod: ProtocolMethod { + var method: String = "wc_sessionPropose" + + var request = RelayConfig(tag: 1100, prompt: true) + + var response = RelayConfig(tag: 1101, prompt: false) +} + +struct SessionSettleProtocolMethod: ProtocolMethod { + var method: String = "wc_sessionSettle" + + var request = RelayConfig(tag: 1102, prompt: false) + + var response = RelayConfig(tag: 1103, prompt: false) +} + +struct SessionUpdateProtocolMethod: ProtocolMethod { + var method: String = "wc_sessionUpdate" + + var request = RelayConfig(tag: 1104, prompt: false) + + var response = RelayConfig(tag: 1105, prompt: false) +} + +struct SessionExtendProtocolMethod: ProtocolMethod { + var method: String = "wc_sessionExtend" + + var request = RelayConfig(tag: 1106, prompt: false) + + var response = RelayConfig(tag: 1107, prompt: false) +} + +struct SessionDeleteProtocolMethod: ProtocolMethod { + var method: String = "wc_sessionDelete" + + var request = RelayConfig(tag: 1112, prompt: false) + + var response = RelayConfig(tag: 1113, prompt: false) +} + + +struct SessionRequestProtocolMethod: ProtocolMethod { + var method: String = "wc_sessionRequest" + + var request = RelayConfig(tag: 1108, prompt: true) + + var response = RelayConfig(tag: 1109, prompt: false) +} +struct SessionPingProtocolMethod: ProtocolMethod { + var method: String = "wc_sessionPing" + + var request = RelayConfig(tag: 1114, prompt: false) + + var response = RelayConfig(tag: 1115, prompt: false) +} +struct SessionEventProtocolMethod: ProtocolMethod { + var method: String = "wc_sessionEvent" + + var request = RelayConfig(tag: 1110, prompt: true) + + var response = RelayConfig(tag: 1111, prompt: false) } From 4a91d0cd57136bed1da7e72c8dd781008b424dac Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Mon, 26 Sep 2022 12:33:30 +0200 Subject: [PATCH 05/10] refactor sign protocol methods --- .../Engine/Common/ApproveEngine.swift | 25 ++++++++-------- .../Engine/Common/DeletePairingService.swift | 5 ++-- .../Engine/Common/DeleteSessionService.swift | 5 ++-- .../Engine/Common/PairingEngine.swift | 10 ++++--- .../Engine/Common/SessionEngine.swift | 29 ++++++++++--------- .../ControllerSessionStateMachine.swift | 14 +++++---- .../NonControllerSessionStateMachine.swift | 10 +++---- .../Services/SessionPingService.swift | 7 +++-- .../SignProtocolMethod.swift | 8 ++--- 9 files changed, 59 insertions(+), 54 deletions(-) rename Sources/WalletConnectSign/Types/{ => ProtocolMethods}/SignProtocolMethod.swift (96%) diff --git a/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift b/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift index 126ea595b..7dbc9f476 100644 --- a/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift +++ b/Sources/WalletConnectSign/Engine/Common/ApproveEngine.swift @@ -89,7 +89,7 @@ final class ApproveEngine { let result = SessionType.ProposeResponse(relay: relay, responderPublicKey: selfPublicKey.hexRepresentation) let response = RPCResponse(id: payload.id, result: result) - try await networkingInteractor.respond(topic: payload.topic, response: response, protocolMethod: SignProtocolMethod.sessionPropose) + try await networkingInteractor.respond(topic: payload.topic, response: response, protocolMethod: SessionProposeProtocolMethod()) try pairing.updateExpiry() pairingStore.setPairing(pairing) @@ -102,7 +102,7 @@ final class ApproveEngine { throw Errors.proposalPayloadsNotFound } proposalPayloadsStore.delete(forKey: proposerPubKey) - try await networkingInteractor.respondError(topic: payload.topic, requestId: payload.id, protocolMethod: SignProtocolMethod.sessionPropose, reason: reason) + try await networkingInteractor.respondError(topic: payload.topic, requestId: payload.id, protocolMethod: SessionProposeProtocolMethod(), reason: reason) // TODO: Delete pairing if inactive } @@ -143,8 +143,9 @@ final class ApproveEngine { try await networkingInteractor.subscribe(topic: topic) sessionStore.setSession(session) - let request = RPCRequest(method: SignProtocolMethod.sessionSettle.method, params: settleParams) - try await networkingInteractor.request(request, topic: topic, protocolMethod: SignProtocolMethod.sessionSettle) + let protocolMethod = SessionSettleProtocolMethod() + let request = RPCRequest(method: protocolMethod.method, params: settleParams) + try await networkingInteractor.request(request, topic: topic, protocolMethod: protocolMethod) onSessionSettle?(session.publicRepresentation()) } } @@ -154,36 +155,36 @@ final class ApproveEngine { private extension ApproveEngine { func setupRequestSubscriptions() { - networkingInteractor.requestSubscription(on: SignProtocolMethod.sessionPropose) + networkingInteractor.requestSubscription(on: SessionProposeProtocolMethod()) .sink { [unowned self] (payload: RequestSubscriptionPayload) in handleSessionProposeRequest(payload: payload) }.store(in: &publishers) - networkingInteractor.requestSubscription(on: SignProtocolMethod.sessionSettle) + networkingInteractor.requestSubscription(on: SessionSettleProtocolMethod()) .sink { [unowned self] (payload: RequestSubscriptionPayload) in handleSessionSettleRequest(payload: payload) }.store(in: &publishers) } func setupResponseSubscriptions() { - networkingInteractor.responseSubscription(on: SignProtocolMethod.sessionPropose) + networkingInteractor.responseSubscription(on: SessionProposeProtocolMethod()) .sink { [unowned self] (payload: ResponseSubscriptionPayload) in handleSessionProposeResponse(payload: payload) }.store(in: &publishers) - networkingInteractor.responseSubscription(on: SignProtocolMethod.sessionSettle) + networkingInteractor.responseSubscription(on: SessionSettleProtocolMethod()) .sink { [unowned self] (payload: ResponseSubscriptionPayload) in handleSessionSettleResponse(payload: payload) }.store(in: &publishers) } func setupResponseErrorSubscriptions() { - networkingInteractor.responseErrorSubscription(on: SignProtocolMethod.sessionPropose) + networkingInteractor.responseErrorSubscription(on: SessionProposeProtocolMethod()) .sink { [unowned self] (payload: ResponseSubscriptionErrorPayload) in handleSessionProposeResponseError(payload: payload) }.store(in: &publishers) - networkingInteractor.responseErrorSubscription(on: SignProtocolMethod.sessionSettle) + networkingInteractor.responseErrorSubscription(on: SessionSettleProtocolMethod()) .sink { [unowned self] (payload: ResponseSubscriptionErrorPayload) in handleSessionSettleResponseError(payload: payload) }.store(in: &publishers) @@ -292,7 +293,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: SignProtocolMethod.sessionPropose) + return respondError(payload: payload, reason: .invalidUpdateRequest, protocolMethod: SessionProposeProtocolMethod()) } proposalPayloadsStore.set(payload, forKey: proposal.proposer.publicKey) onSessionProposal?(proposal.publicRepresentation()) @@ -303,7 +304,7 @@ private extension ApproveEngine { func handleSessionSettleRequest(payload: RequestSubscriptionPayload) { logger.debug("Did receive session settle request") - let protocolMethod = SignProtocolMethod.sessionSettle + let protocolMethod = SessionSettleProtocolMethod() guard let proposedNamespaces = settlingProposal?.requiredNamespaces else { return respondError(payload: payload, reason: .invalidUpdateRequest, protocolMethod: protocolMethod) diff --git a/Sources/WalletConnectSign/Engine/Common/DeletePairingService.swift b/Sources/WalletConnectSign/Engine/Common/DeletePairingService.swift index 1e6aa3d19..8928013b5 100644 --- a/Sources/WalletConnectSign/Engine/Common/DeletePairingService.swift +++ b/Sources/WalletConnectSign/Engine/Common/DeletePairingService.swift @@ -23,10 +23,11 @@ class DeletePairingService { func delete(topic: String) async throws { let reasonCode = ReasonCode.userDisconnected + let protocolMethod = PairingDeleteProtocolMethod() let reason = SessionType.Reason(code: reasonCode.code, message: reasonCode.message) logger.debug("Will delete pairing for reason: message: \(reason.message) code: \(reason.code)") - let request = RPCRequest(method: SignProtocolMethod.sessionDelete.method, params: reason) - try await networkingInteractor.request(request, topic: topic, protocolMethod: SignProtocolMethod.sessionDelete) + let request = RPCRequest(method: protocolMethod.method, params: reason) + try await networkingInteractor.request(request, topic: topic, protocolMethod: protocolMethod) pairingStorage.delete(topic: topic) kms.deleteSymmetricKey(for: topic) networkingInteractor.unsubscribe(topic: topic) diff --git a/Sources/WalletConnectSign/Engine/Common/DeleteSessionService.swift b/Sources/WalletConnectSign/Engine/Common/DeleteSessionService.swift index eda023715..be2be4c42 100644 --- a/Sources/WalletConnectSign/Engine/Common/DeleteSessionService.swift +++ b/Sources/WalletConnectSign/Engine/Common/DeleteSessionService.swift @@ -22,10 +22,11 @@ class DeleteSessionService { func delete(topic: String) async throws { let reasonCode = ReasonCode.userDisconnected + let protocolMethod = SessionDeleteProtocolMethod() let reason = SessionType.Reason(code: reasonCode.code, message: reasonCode.message) logger.debug("Will delete session for reason: message: \(reason.message) code: \(reason.code)") - let request = RPCRequest(method: SignProtocolMethod.sessionDelete.method, params: reason) - try await networkingInteractor.request(request, topic: topic, protocolMethod: SignProtocolMethod.sessionDelete) + let request = RPCRequest(method: protocolMethod.method, params: reason) + try await networkingInteractor.request(request, topic: topic, protocolMethod: protocolMethod) sessionStore.delete(topic: topic) kms.deleteSymmetricKey(for: topic) networkingInteractor.unsubscribe(topic: topic) diff --git a/Sources/WalletConnectSign/Engine/Common/PairingEngine.swift b/Sources/WalletConnectSign/Engine/Common/PairingEngine.swift index 56a572022..deee4888b 100644 --- a/Sources/WalletConnectSign/Engine/Common/PairingEngine.swift +++ b/Sources/WalletConnectSign/Engine/Common/PairingEngine.swift @@ -63,6 +63,7 @@ final class PairingEngine { func propose(pairingTopic: String, namespaces: [String: ProposalNamespace], relay: RelayProtocolOptions) async throws { logger.debug("Propose Session on topic: \(pairingTopic)") try Namespace.validate(namespaces) + let protocolMethod = SessionProposeProtocolMethod() let publicKey = try! kms.createX25519KeyPair() let proposer = Participant( publicKey: publicKey.hexRepresentation, @@ -72,8 +73,8 @@ final class PairingEngine { proposer: proposer, requiredNamespaces: namespaces) - let request = RPCRequest(method: SignProtocolMethod.sessionPropose.method, params: proposal) - try await networkingInteractor.requestNetworkAck(request, topic: pairingTopic, protocolMethod: SignProtocolMethod.sessionPropose) + let request = RPCRequest(method: protocolMethod.method, params: proposal) + try await networkingInteractor.requestNetworkAck(request, topic: pairingTopic, protocolMethod: protocolMethod) } } @@ -90,11 +91,12 @@ private extension PairingEngine { } } .store(in: &publishers) + let protocolMethod = PairingPingProtocolMethod() - networkingInteractor.requestSubscription(on: SignProtocolMethod.pairingPing) + networkingInteractor.requestSubscription(on: protocolMethod) .sink { [unowned self] (payload: RequestSubscriptionPayload) in Task(priority: .high) { - try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, protocolMethod: SignProtocolMethod.pairingPing) + try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, protocolMethod: protocolMethod) } } .store(in: &publishers) diff --git a/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift b/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift index ec8c1911c..2ca824ff1 100644 --- a/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift +++ b/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift @@ -59,8 +59,8 @@ final class SessionEngine { let chainRequest = SessionType.RequestParams.Request(method: request.method, params: request.params) let sessionRequestParams = SessionType.RequestParams(request: chainRequest, chainId: request.chainId) - let rpcRequest = RPCRequest(method: SignProtocolMethod.sessionRequest.method, params: sessionRequestParams) - try await networkingInteractor.request(rpcRequest, topic: request.topic, protocolMethod: SignProtocolMethod.sessionRequest) + let rpcRequest = RPCRequest(method: SessionRequestProtocolMethod().method, params: sessionRequestParams) + try await networkingInteractor.request(rpcRequest, topic: request.topic, protocolMethod: SessionRequestProtocolMethod()) } func respondSessionRequest(topic: String, requestId: RPCID, response: RPCResult) async throws { @@ -68,10 +68,11 @@ final class SessionEngine { throw Errors.sessionNotFound(topic: topic) } let response = RPCResponse(id: requestId, result: response) - try await networkingInteractor.respond(topic: topic, response: response, protocolMethod: SignProtocolMethod.sessionRequest) + try await networkingInteractor.respond(topic: topic, response: response, protocolMethod: SessionRequestProtocolMethod()) } func emit(topic: String, event: SessionType.EventParams.Event, chainId: Blockchain) async throws { + let protocolMethod = SessionEventProtocolMethod() guard let session = sessionStore.getSession(forTopic: topic) else { logger.debug("Could not find session for topic \(topic)") return @@ -79,8 +80,8 @@ final class SessionEngine { guard session.hasPermission(forEvent: event.name, onChain: chainId) else { throw WalletConnectError.invalidEvent } - let rpcRequest = RPCRequest(method: SignProtocolMethod.sessionEvent.method, params: SessionType.EventParams(event: event, chainId: chainId)) - try await networkingInteractor.request(rpcRequest, topic: topic, protocolMethod: SignProtocolMethod.sessionEvent) + let rpcRequest = RPCRequest(method: protocolMethod.method, params: SessionType.EventParams(event: event, chainId: chainId)) + try await networkingInteractor.request(rpcRequest, topic: topic, protocolMethod: protocolMethod) } } @@ -100,29 +101,29 @@ private extension SessionEngine { } func setupRequestSubscriptions() { - networkingInteractor.requestSubscription(on: SignProtocolMethod.sessionDelete) + networkingInteractor.requestSubscription(on: SessionDeleteProtocolMethod()) .sink { [unowned self] (payload: RequestSubscriptionPayload) in onSessionDelete(payload: payload) }.store(in: &publishers) - networkingInteractor.requestSubscription(on: SignProtocolMethod.sessionRequest) + networkingInteractor.requestSubscription(on: SessionRequestProtocolMethod()) .sink { [unowned self] (payload: RequestSubscriptionPayload) in onSessionRequest(payload: payload) }.store(in: &publishers) - networkingInteractor.requestSubscription(on: SignProtocolMethod.sessionPing) + networkingInteractor.requestSubscription(on: SessionPingProtocolMethod()) .sink { [unowned self] (payload: RequestSubscriptionPayload) in onSessionPing(payload: payload) }.store(in: &publishers) - networkingInteractor.requestSubscription(on: SignProtocolMethod.sessionEvent) + networkingInteractor.requestSubscription(on: SessionEventProtocolMethod()) .sink { [unowned self] (payload: RequestSubscriptionPayload) in onSessionEvent(payload: payload) }.store(in: &publishers) } func setupResponseSubscriptions() { - networkingInteractor.responseSubscription(on: SignProtocolMethod.sessionRequest) + networkingInteractor.responseSubscription(on: SessionRequestProtocolMethod()) .sink { [unowned self] (payload: ResponseSubscriptionPayload) in onSessionResponse?(Response( id: payload.id, @@ -152,7 +153,7 @@ private extension SessionEngine { } func onSessionDelete(payload: RequestSubscriptionPayload) { - let protocolMethod = SignProtocolMethod.sessionDelete + let protocolMethod = SessionDeleteProtocolMethod() let topic = payload.topic guard sessionStore.hasSession(forTopic: topic) else { return respondError(payload: payload, reason: .noSessionForTopic, protocolMethod: protocolMethod) @@ -166,7 +167,7 @@ private extension SessionEngine { } func onSessionRequest(payload: RequestSubscriptionPayload) { - let protocolMethod = SignProtocolMethod.sessionRequest + let protocolMethod = SessionRequestProtocolMethod() let topic = payload.topic let request = Request( id: payload.id, @@ -189,12 +190,12 @@ private extension SessionEngine { func onSessionPing(payload: SubscriptionPayload) { Task(priority: .high) { - try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, protocolMethod: SignProtocolMethod.sessionPing) + try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, protocolMethod: SessionPingProtocolMethod()) } } func onSessionEvent(payload: RequestSubscriptionPayload) { - let protocolMethod = SignProtocolMethod.sessionEvent + let protocolMethod = SessionEventProtocolMethod() let event = payload.request.event let topic = payload.topic guard let session = sessionStore.getSession(forTopic: topic) else { diff --git a/Sources/WalletConnectSign/Engine/Controller/ControllerSessionStateMachine.swift b/Sources/WalletConnectSign/Engine/Controller/ControllerSessionStateMachine.swift index c7755e08e..748ae7713 100644 --- a/Sources/WalletConnectSign/Engine/Controller/ControllerSessionStateMachine.swift +++ b/Sources/WalletConnectSign/Engine/Controller/ControllerSessionStateMachine.swift @@ -30,34 +30,36 @@ final class ControllerSessionStateMachine { func update(topic: String, namespaces: [String: SessionNamespace]) async throws { let session = try getSession(for: topic) + let protocolMethod = SessionUpdateProtocolMethod() try validateController(session) try Namespace.validate(namespaces) logger.debug("Controller will update methods") sessionStore.setSession(session) - let request = RPCRequest(method: SignProtocolMethod.sessionUpdate.method, params: SessionType.UpdateParams(namespaces: namespaces)) - try await networkingInteractor.request(request, topic: topic, protocolMethod: SignProtocolMethod.sessionUpdate) + let request = RPCRequest(method: protocolMethod.method, params: SessionType.UpdateParams(namespaces: namespaces)) + try await networkingInteractor.request(request, topic: topic, protocolMethod: protocolMethod) } func extend(topic: String, by ttl: Int64) async throws { var session = try getSession(for: topic) + let protocolMethod = SessionExtendProtocolMethod() try validateController(session) try session.updateExpiry(by: ttl) let newExpiry = Int64(session.expiryDate.timeIntervalSince1970 ) sessionStore.setSession(session) - let request = RPCRequest(method: SignProtocolMethod.sessionExtend.method, params: SessionType.UpdateExpiryParams(expiry: newExpiry)) - try await networkingInteractor.request(request, topic: topic, protocolMethod: SignProtocolMethod.sessionExtend) + let request = RPCRequest(method: protocolMethod.method, params: SessionType.UpdateExpiryParams(expiry: newExpiry)) + try await networkingInteractor.request(request, topic: topic, protocolMethod: protocolMethod) } // MARK: - Handle Response private func setupSubscriptions() { - networkingInteractor.responseSubscription(on: SignProtocolMethod.sessionUpdate) + networkingInteractor.responseSubscription(on: SessionUpdateProtocolMethod()) .sink { [unowned self] (payload: ResponseSubscriptionPayload) in handleUpdateResponse(payload: payload) } .store(in: &publishers) - networkingInteractor.responseSubscription(on: SignProtocolMethod.sessionExtend) + networkingInteractor.responseSubscription(on: SessionExtendProtocolMethod()) .sink { [unowned self] (payload: ResponseSubscriptionPayload) in handleUpdateExpiryResponse(payload: payload) } diff --git a/Sources/WalletConnectSign/Engine/NonController/NonControllerSessionStateMachine.swift b/Sources/WalletConnectSign/Engine/NonController/NonControllerSessionStateMachine.swift index a2af79032..dbde21388 100644 --- a/Sources/WalletConnectSign/Engine/NonController/NonControllerSessionStateMachine.swift +++ b/Sources/WalletConnectSign/Engine/NonController/NonControllerSessionStateMachine.swift @@ -27,12 +27,12 @@ final class NonControllerSessionStateMachine { } private func setupSubscriptions() { - networkingInteractor.requestSubscription(on: SignProtocolMethod.sessionUpdate) + networkingInteractor.requestSubscription(on: SessionUpdateProtocolMethod()) .sink { [unowned self] (payload: RequestSubscriptionPayload) in onSessionUpdateNamespacesRequest(payload: payload, updateParams: payload.request) }.store(in: &publishers) - networkingInteractor.requestSubscription(on: SignProtocolMethod.sessionExtend) + networkingInteractor.requestSubscription(on: SessionExtendProtocolMethod()) .sink { [unowned self] (payload: RequestSubscriptionPayload) in onSessionUpdateExpiry(payload: payload, updateExpiryParams: payload.request) }.store(in: &publishers) @@ -50,7 +50,7 @@ final class NonControllerSessionStateMachine { // TODO: Update stored session namespaces private func onSessionUpdateNamespacesRequest(payload: SubscriptionPayload, updateParams: SessionType.UpdateParams) { - let protocolMethod = SignProtocolMethod.sessionUpdate + let protocolMethod = SessionUpdateProtocolMethod() do { try Namespace.validate(updateParams.namespaces) } catch { @@ -77,7 +77,7 @@ final class NonControllerSessionStateMachine { } private func onSessionUpdateExpiry(payload: SubscriptionPayload, updateExpiryParams: SessionType.UpdateExpiryParams) { - let protocolMethod = SignProtocolMethod.sessionExtend + let protocolMethod = SessionExtendProtocolMethod() let topic = payload.topic guard var session = sessionStore.getSession(forTopic: topic) else { return respondError(payload: payload, reason: .noSessionForTopic, protocolMethod: protocolMethod) @@ -93,7 +93,7 @@ final class NonControllerSessionStateMachine { sessionStore.setSession(session) Task(priority: .high) { - try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, protocolMethod: SignProtocolMethod.sessionExtend) + try await networkingInteractor.respondSuccess(topic: payload.topic, requestId: payload.id, protocolMethod: protocolMethod) } onExtend?(session.topic, session.expiryDate) diff --git a/Sources/WalletConnectSign/Services/SessionPingService.swift b/Sources/WalletConnectSign/Services/SessionPingService.swift index 697dcab89..1d10e2105 100644 --- a/Sources/WalletConnectSign/Services/SessionPingService.swift +++ b/Sources/WalletConnectSign/Services/SessionPingService.swift @@ -22,10 +22,11 @@ class SessionPingService { sessionStorage: WCSessionStorage, networkingInteractor: NetworkInteracting, logger: ConsoleLogging) { + let protocolMethod = SessionPingProtocolMethod() self.sessionStorage = sessionStorage - self.pingRequester = PingRequester(networkingInteractor: networkingInteractor, method: SignProtocolMethod.sessionPing) - self.pingResponder = PingResponder(networkingInteractor: networkingInteractor, method: SignProtocolMethod.sessionPing, logger: logger) - self.pingResponseSubscriber = PingResponseSubscriber(networkingInteractor: networkingInteractor, method: SignProtocolMethod.sessionPing, logger: logger) + self.pingRequester = PingRequester(networkingInteractor: networkingInteractor, method: protocolMethod) + self.pingResponder = PingResponder(networkingInteractor: networkingInteractor, method: protocolMethod, logger: logger) + self.pingResponseSubscriber = PingResponseSubscriber(networkingInteractor: networkingInteractor, method: protocolMethod, logger: logger) } func ping(topic: String) async throws { diff --git a/Sources/WalletConnectSign/Types/SignProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SignProtocolMethod.swift similarity index 96% rename from Sources/WalletConnectSign/Types/SignProtocolMethod.swift rename to Sources/WalletConnectSign/Types/ProtocolMethods/SignProtocolMethod.swift index 87f727441..ca2a263d4 100644 --- a/Sources/WalletConnectSign/Types/SignProtocolMethod.swift +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/SignProtocolMethod.swift @@ -1,7 +1,4 @@ import Foundation -import JSONRPC -import WalletConnectPairing -import WalletConnectUtils import WalletConnectNetworking struct PairingPingProtocolMethod: ProtocolMethod { @@ -20,9 +17,6 @@ struct PairingDeleteProtocolMethod: ProtocolMethod { var response = RelayConfig(tag: 1001, prompt: false) } - - - struct SessionProposeProtocolMethod: ProtocolMethod { var method: String = "wc_sessionPropose" @@ -71,6 +65,7 @@ struct SessionRequestProtocolMethod: ProtocolMethod { var response = RelayConfig(tag: 1109, prompt: false) } + struct SessionPingProtocolMethod: ProtocolMethod { var method: String = "wc_sessionPing" @@ -78,6 +73,7 @@ struct SessionPingProtocolMethod: ProtocolMethod { var response = RelayConfig(tag: 1115, prompt: false) } + struct SessionEventProtocolMethod: ProtocolMethod { var method: String = "wc_sessionEvent" From a466d2999c6fd79533d2b12207b88409e1d7f705 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Mon, 26 Sep 2022 12:34:52 +0200 Subject: [PATCH 06/10] rename properties --- Sources/Auth/AuthProtocolMethod.swift | 12 +++--- Sources/Chat/Types/ChatProtocolMethod.swift | 8 ++-- .../NetworkInteractor.swift | 6 +-- .../ProtocolMethod.swift | 4 +- .../PairingProtocolMethod.swift | 4 +- .../ProtocolMethods/SignProtocolMethod.swift | 40 +++++++++---------- 6 files changed, 37 insertions(+), 37 deletions(-) diff --git a/Sources/Auth/AuthProtocolMethod.swift b/Sources/Auth/AuthProtocolMethod.swift index 4de009c42..df6e982be 100644 --- a/Sources/Auth/AuthProtocolMethod.swift +++ b/Sources/Auth/AuthProtocolMethod.swift @@ -4,25 +4,25 @@ import WalletConnectNetworking struct AuthRequestProtocolMethod: ProtocolMethod { var method: String = "wc_authRequest" - var request = RelayConfig(tag: 3000, prompt: true) + var requestConfig = RelayConfig(tag: 3000, prompt: true) - var response = RelayConfig(tag: 3001, prompt: false) + var responseConfig = RelayConfig(tag: 3001, prompt: false) } struct PairingPingProtocolMethod: ProtocolMethod { var method: String = "wc_pairingPing" - var request = RelayConfig(tag: 1002, prompt: false) + var requestConfig = RelayConfig(tag: 1002, prompt: false) - var response = RelayConfig(tag: 1003, prompt: false) + var responseConfig = RelayConfig(tag: 1003, prompt: false) } struct PairingDeleteProtocolMethod: ProtocolMethod { var method: String = "wc_pairingDelete" - var request = RelayConfig(tag: 1000, prompt: false) + var requestConfig = RelayConfig(tag: 1000, prompt: false) - var response = RelayConfig(tag: 1001, prompt: false) + var responseConfig = RelayConfig(tag: 1001, prompt: false) } diff --git a/Sources/Chat/Types/ChatProtocolMethod.swift b/Sources/Chat/Types/ChatProtocolMethod.swift index c6ca16dfe..96a5dd9af 100644 --- a/Sources/Chat/Types/ChatProtocolMethod.swift +++ b/Sources/Chat/Types/ChatProtocolMethod.swift @@ -4,17 +4,17 @@ import WalletConnectNetworking struct ChatInviteProtocolMethod: ProtocolMethod { var method: String = "wc_chatInvite" - var request = RelayConfig(tag: 2000, prompt: true) + var requestConfig = RelayConfig(tag: 2000, prompt: true) - var response = RelayConfig(tag: 2001, prompt: false) + var responseConfig = RelayConfig(tag: 2001, prompt: false) } struct ChatMessageProtocolMethod: ProtocolMethod { var method: String = "wc_chatMessage" - var request = RelayConfig(tag: 2002, prompt: true) + var requestConfig = RelayConfig(tag: 2002, prompt: true) - var response = RelayConfig(tag: 2003, prompt: false) + var responseConfig = RelayConfig(tag: 2003, prompt: false) } diff --git a/Sources/WalletConnectNetworking/NetworkInteractor.swift b/Sources/WalletConnectNetworking/NetworkInteractor.swift index 339c87656..b13026a39 100644 --- a/Sources/WalletConnectNetworking/NetworkInteractor.swift +++ b/Sources/WalletConnectNetworking/NetworkInteractor.swift @@ -96,7 +96,7 @@ public class NetworkingInteractor: NetworkInteracting { public func request(_ request: RPCRequest, topic: String, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws { try rpcHistory.set(request, forTopic: topic, emmitedBy: .local) let message = try! serializer.serialize(topic: topic, encodable: request, envelopeType: envelopeType) - try await relayClient.publish(topic: topic, payload: message, tag: protocolMethod.request.tag, prompt: protocolMethod.request.prompt) + try await relayClient.publish(topic: topic, payload: message, tag: protocolMethod.requestConfig.tag, prompt: protocolMethod.requestConfig.prompt) } /// Completes with an acknowledgement from the relay network. @@ -107,7 +107,7 @@ public class NetworkingInteractor: NetworkInteracting { try rpcHistory.set(request, forTopic: topic, emmitedBy: .local) let message = try serializer.serialize(topic: topic, encodable: request) return try await withCheckedThrowingContinuation { continuation in - relayClient.publish(topic: topic, payload: message, tag: protocolMethod.request.tag, prompt: protocolMethod.request.prompt) { error in + relayClient.publish(topic: topic, payload: message, tag: protocolMethod.requestConfig.tag, prompt: protocolMethod.requestConfig.prompt) { error in if let error = error { continuation.resume(throwing: error) } else { @@ -123,7 +123,7 @@ public class NetworkingInteractor: NetworkInteracting { public func respond(topic: String, response: RPCResponse, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws { try rpcHistory.resolve(response) let message = try! serializer.serialize(topic: topic, encodable: response, envelopeType: envelopeType) - try await relayClient.publish(topic: topic, payload: message, tag: protocolMethod.response.tag) + try await relayClient.publish(topic: topic, payload: message, tag: protocolMethod.responseConfig.tag) } public func respondSuccess(topic: String, requestId: RPCID, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws { diff --git a/Sources/WalletConnectNetworking/ProtocolMethod.swift b/Sources/WalletConnectNetworking/ProtocolMethod.swift index 21c8ad1b5..b482f09c1 100644 --- a/Sources/WalletConnectNetworking/ProtocolMethod.swift +++ b/Sources/WalletConnectNetworking/ProtocolMethod.swift @@ -2,8 +2,8 @@ import Foundation public protocol ProtocolMethod { var method: String { get } - var request: RelayConfig { get } - var response: RelayConfig { get } + var requestConfig: RelayConfig { get } + var responseConfig: RelayConfig { get } } public struct RelayConfig { diff --git a/Sources/WalletConnectPairing/PairingProtocolMethod.swift b/Sources/WalletConnectPairing/PairingProtocolMethod.swift index b4231dade..c223a51c4 100644 --- a/Sources/WalletConnectPairing/PairingProtocolMethod.swift +++ b/Sources/WalletConnectPairing/PairingProtocolMethod.swift @@ -4,7 +4,7 @@ import WalletConnectNetworking struct PairingPingProtocolMethod: ProtocolMethod { var method: String = "wc_pairingPing" - var request = RelayConfig(tag: 1002, prompt: false) + var requestConfig = RelayConfig(tag: 1002, prompt: false) - var response = RelayConfig(tag: 1003, prompt: false) + var responseConfig = RelayConfig(tag: 1003, prompt: false) } diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/SignProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SignProtocolMethod.swift index ca2a263d4..2058a0044 100644 --- a/Sources/WalletConnectSign/Types/ProtocolMethods/SignProtocolMethod.swift +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/SignProtocolMethod.swift @@ -4,80 +4,80 @@ import WalletConnectNetworking struct PairingPingProtocolMethod: ProtocolMethod { var method: String = "wc_pairingPing" - var request = RelayConfig(tag: 1002, prompt: false) + var requestConfig = RelayConfig(tag: 1002, prompt: false) - var response = RelayConfig(tag: 1003, prompt: false) + var responseConfig = RelayConfig(tag: 1003, prompt: false) } struct PairingDeleteProtocolMethod: ProtocolMethod { var method: String = "wc_pairingDelete" - var request = RelayConfig(tag: 1000, prompt: false) + var requestConfig = RelayConfig(tag: 1000, prompt: false) - var response = RelayConfig(tag: 1001, prompt: false) + var responseConfig = RelayConfig(tag: 1001, prompt: false) } struct SessionProposeProtocolMethod: ProtocolMethod { var method: String = "wc_sessionPropose" - var request = RelayConfig(tag: 1100, prompt: true) + var requestConfig = RelayConfig(tag: 1100, prompt: true) - var response = RelayConfig(tag: 1101, prompt: false) + var responseConfig = RelayConfig(tag: 1101, prompt: false) } struct SessionSettleProtocolMethod: ProtocolMethod { var method: String = "wc_sessionSettle" - var request = RelayConfig(tag: 1102, prompt: false) + var requestConfig = RelayConfig(tag: 1102, prompt: false) - var response = RelayConfig(tag: 1103, prompt: false) + var responseConfig = RelayConfig(tag: 1103, prompt: false) } struct SessionUpdateProtocolMethod: ProtocolMethod { var method: String = "wc_sessionUpdate" - var request = RelayConfig(tag: 1104, prompt: false) + var requestConfig = RelayConfig(tag: 1104, prompt: false) - var response = RelayConfig(tag: 1105, prompt: false) + var responseConfig = RelayConfig(tag: 1105, prompt: false) } struct SessionExtendProtocolMethod: ProtocolMethod { var method: String = "wc_sessionExtend" - var request = RelayConfig(tag: 1106, prompt: false) + var requestConfig = RelayConfig(tag: 1106, prompt: false) - var response = RelayConfig(tag: 1107, prompt: false) + var responseConfig = RelayConfig(tag: 1107, prompt: false) } struct SessionDeleteProtocolMethod: ProtocolMethod { var method: String = "wc_sessionDelete" - var request = RelayConfig(tag: 1112, prompt: false) + var requestConfig = RelayConfig(tag: 1112, prompt: false) - var response = RelayConfig(tag: 1113, prompt: false) + var responseConfig = RelayConfig(tag: 1113, prompt: false) } struct SessionRequestProtocolMethod: ProtocolMethod { var method: String = "wc_sessionRequest" - var request = RelayConfig(tag: 1108, prompt: true) + var requestConfig = RelayConfig(tag: 1108, prompt: true) - var response = RelayConfig(tag: 1109, prompt: false) + var responseConfig = RelayConfig(tag: 1109, prompt: false) } struct SessionPingProtocolMethod: ProtocolMethod { var method: String = "wc_sessionPing" - var request = RelayConfig(tag: 1114, prompt: false) + var requestConfig = RelayConfig(tag: 1114, prompt: false) - var response = RelayConfig(tag: 1115, prompt: false) + var responseConfig = RelayConfig(tag: 1115, prompt: false) } struct SessionEventProtocolMethod: ProtocolMethod { var method: String = "wc_sessionEvent" - var request = RelayConfig(tag: 1110, prompt: true) + var requestConfig = RelayConfig(tag: 1110, prompt: true) - var response = RelayConfig(tag: 1111, prompt: false) + var responseConfig = RelayConfig(tag: 1111, prompt: false) } From 9ab2979b9178616b44f7f60358f6c5eb5fe5a748 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Mon, 26 Sep 2022 12:39:01 +0200 Subject: [PATCH 07/10] savepoint --- .../WalletConnectNetworking/NetworkInteractor.swift | 2 +- Sources/WalletConnectNetworking/ProtocolMethod.swift | 8 +++++--- Sources/WalletConnectRelay/RelayClient.swift | 10 +++++----- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Sources/WalletConnectNetworking/NetworkInteractor.swift b/Sources/WalletConnectNetworking/NetworkInteractor.swift index b13026a39..10250f025 100644 --- a/Sources/WalletConnectNetworking/NetworkInteractor.swift +++ b/Sources/WalletConnectNetworking/NetworkInteractor.swift @@ -123,7 +123,7 @@ public class NetworkingInteractor: NetworkInteracting { public func respond(topic: String, response: RPCResponse, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws { try rpcHistory.resolve(response) let message = try! serializer.serialize(topic: topic, encodable: response, envelopeType: envelopeType) - try await relayClient.publish(topic: topic, payload: message, tag: protocolMethod.responseConfig.tag) + try await relayClient.publish(topic: topic, payload: message, tag: protocolMethod.responseConfig.tag, prompt: protocolMethod.responseConfig.prompt, ttl: protocolMethod.responseConfig.ttl) } public func respondSuccess(topic: String, requestId: RPCID, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws { diff --git a/Sources/WalletConnectNetworking/ProtocolMethod.swift b/Sources/WalletConnectNetworking/ProtocolMethod.swift index b482f09c1..fa061623f 100644 --- a/Sources/WalletConnectNetworking/ProtocolMethod.swift +++ b/Sources/WalletConnectNetworking/ProtocolMethod.swift @@ -7,11 +7,13 @@ public protocol ProtocolMethod { } public struct RelayConfig { - var tag: Int - var prompt: Bool + let tag: Int + let prompt: Bool + let ttl: Int - public init(tag: Int, prompt: Bool) { + public init(tag: Int, prompt: Bool, ttl: Int) { self.tag = tag self.prompt = prompt + self.ttl = ttl } } diff --git a/Sources/WalletConnectRelay/RelayClient.swift b/Sources/WalletConnectRelay/RelayClient.swift index 9aa2ba8a4..c038cb0dd 100644 --- a/Sources/WalletConnectRelay/RelayClient.swift +++ b/Sources/WalletConnectRelay/RelayClient.swift @@ -20,7 +20,6 @@ public final class RelayClient { case subscriptionIdNotFound } - let defaultTtl = 6*Time.hour var subscriptions: [String: String] = [:] public var messagePublisher: AnyPublisher<(topic: String, message: String), Never> { @@ -120,8 +119,8 @@ public final class RelayClient { } /// Completes when networking client sends a request, error if it fails on client side - public func publish(topic: String, payload: String, tag: Int, prompt: Bool = false) async throws { - let request = Publish(params: .init(topic: topic, message: payload, ttl: defaultTtl, prompt: prompt, tag: tag)) + public func publish(topic: String, payload: String, tag: Int, prompt: Bool, ttl: Int) async throws { + let request = Publish(params: .init(topic: topic, message: payload, ttl: ttl, prompt: prompt, tag: tag)) .wrapToIRN() .asRPCRequest() let message = try request.asJSONEncodedString() @@ -134,10 +133,11 @@ public final class RelayClient { topic: String, payload: String, tag: Int, - prompt: Bool = false, + prompt: Bool, + ttl: Int, onNetworkAcknowledge: @escaping ((Error?) -> Void) ) { - let rpc = Publish(params: .init(topic: topic, message: payload, ttl: defaultTtl, prompt: prompt, tag: tag)) + let rpc = Publish(params: .init(topic: topic, message: payload, ttl: ttl, prompt: prompt, tag: tag)) let request = rpc .wrapToIRN() .asRPCRequest() From 5a23be7d9efb46c7f3ff65e6788c9fda430c3bfc Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Mon, 26 Sep 2022 12:49:10 +0200 Subject: [PATCH 08/10] restructure files --- .../Relay/RelayClientEndToEndTests.swift | 4 +- Sources/Auth/AuthProtocolMethod.swift | 12 +-- Sources/Chat/Types/ChatProtocolMethod.swift | 8 +- .../NetworkInteractor.swift | 4 +- .../PairingProtocolMethod.swift | 4 +- Sources/WalletConnectRelay/Misc/Time.swift | 7 -- .../PairingProtocolMethods.swift | 18 ++++ .../SessionDeleteProtocolMethod.swift | 11 +++ .../SessionEventProtocolMethod.swift | 10 +++ .../SessionExtendProtocolMethod.swift | 10 +++ .../SessionPingProtocolMethod.swift | 11 +++ .../SessionProposeProtocolMethod.swift | 10 +++ .../SessionRequestProtocolMethod.swift | 10 +++ .../SessionSettleProtocolMethod.swift | 10 +++ .../SessionUpdateProtocolMethod.swift | 11 +++ .../ProtocolMethods/SignProtocolMethod.swift | 83 ------------------- 16 files changed, 117 insertions(+), 106 deletions(-) delete mode 100644 Sources/WalletConnectRelay/Misc/Time.swift create mode 100644 Sources/WalletConnectSign/Types/ProtocolMethods/PairingProtocolMethods.swift create mode 100644 Sources/WalletConnectSign/Types/ProtocolMethods/SessionDeleteProtocolMethod.swift create mode 100644 Sources/WalletConnectSign/Types/ProtocolMethods/SessionEventProtocolMethod.swift create mode 100644 Sources/WalletConnectSign/Types/ProtocolMethods/SessionExtendProtocolMethod.swift create mode 100644 Sources/WalletConnectSign/Types/ProtocolMethods/SessionPingProtocolMethod.swift create mode 100644 Sources/WalletConnectSign/Types/ProtocolMethods/SessionProposeProtocolMethod.swift create mode 100644 Sources/WalletConnectSign/Types/ProtocolMethods/SessionRequestProtocolMethod.swift create mode 100644 Sources/WalletConnectSign/Types/ProtocolMethods/SessionSettleProtocolMethod.swift create mode 100644 Sources/WalletConnectSign/Types/ProtocolMethods/SessionUpdateProtocolMethod.swift delete mode 100644 Sources/WalletConnectSign/Types/ProtocolMethods/SignProtocolMethod.swift diff --git a/Example/IntegrationTests/Relay/RelayClientEndToEndTests.swift b/Example/IntegrationTests/Relay/RelayClientEndToEndTests.swift index d456d69dc..5c1843eb3 100644 --- a/Example/IntegrationTests/Relay/RelayClientEndToEndTests.swift +++ b/Example/IntegrationTests/Relay/RelayClientEndToEndTests.swift @@ -77,7 +77,7 @@ final class RelayClientEndToEndTests: XCTestCase { }.store(in: &publishers) relayA.socketConnectionStatusPublisher.sink { _ in - relayA.publish(topic: randomTopic, payload: payloadA, tag: 0, onNetworkAcknowledge: { error in + relayA.publish(topic: randomTopic, payload: payloadA, tag: 0, prompt: false, ttl: 60, onNetworkAcknowledge: { error in XCTAssertNil(error) }) relayA.subscribe(topic: randomTopic) { error in @@ -85,7 +85,7 @@ final class RelayClientEndToEndTests: XCTestCase { } }.store(in: &publishers) relayB.socketConnectionStatusPublisher.sink { _ in - relayB.publish(topic: randomTopic, payload: payloadB, tag: 0, onNetworkAcknowledge: { error in + relayB.publish(topic: randomTopic, payload: payloadB, tag: 0, prompt: false, ttl: 60, onNetworkAcknowledge: { error in XCTAssertNil(error) }) relayB.subscribe(topic: randomTopic) { error in diff --git a/Sources/Auth/AuthProtocolMethod.swift b/Sources/Auth/AuthProtocolMethod.swift index df6e982be..e54889755 100644 --- a/Sources/Auth/AuthProtocolMethod.swift +++ b/Sources/Auth/AuthProtocolMethod.swift @@ -4,25 +4,25 @@ import WalletConnectNetworking struct AuthRequestProtocolMethod: ProtocolMethod { var method: String = "wc_authRequest" - var requestConfig = RelayConfig(tag: 3000, prompt: true) + var requestConfig = RelayConfig(tag: 3000, prompt: true, ttl: 86400) - var responseConfig = RelayConfig(tag: 3001, prompt: false) + var responseConfig = RelayConfig(tag: 3001, prompt: false, ttl: 86400) } struct PairingPingProtocolMethod: ProtocolMethod { var method: String = "wc_pairingPing" - var requestConfig = RelayConfig(tag: 1002, prompt: false) + var requestConfig = RelayConfig(tag: 1002, prompt: false, ttl: 30) - var responseConfig = RelayConfig(tag: 1003, prompt: false) + var responseConfig = RelayConfig(tag: 1003, prompt: false, ttl: 30) } struct PairingDeleteProtocolMethod: ProtocolMethod { var method: String = "wc_pairingDelete" - var requestConfig = RelayConfig(tag: 1000, prompt: false) + var requestConfig = RelayConfig(tag: 1000, prompt: false, ttl: 86400) - var responseConfig = RelayConfig(tag: 1001, prompt: false) + var responseConfig = RelayConfig(tag: 1001, prompt: false, ttl: 86400) } diff --git a/Sources/Chat/Types/ChatProtocolMethod.swift b/Sources/Chat/Types/ChatProtocolMethod.swift index 96a5dd9af..82dcd15dc 100644 --- a/Sources/Chat/Types/ChatProtocolMethod.swift +++ b/Sources/Chat/Types/ChatProtocolMethod.swift @@ -4,17 +4,17 @@ import WalletConnectNetworking struct ChatInviteProtocolMethod: ProtocolMethod { var method: String = "wc_chatInvite" - var requestConfig = RelayConfig(tag: 2000, prompt: true) + var requestConfig = RelayConfig(tag: 2000, prompt: true, ttl: 86400) - var responseConfig = RelayConfig(tag: 2001, prompt: false) + var responseConfig = RelayConfig(tag: 2001, prompt: false, ttl: 86400) } struct ChatMessageProtocolMethod: ProtocolMethod { var method: String = "wc_chatMessage" - var requestConfig = RelayConfig(tag: 2002, prompt: true) + var requestConfig = RelayConfig(tag: 2002, prompt: true, ttl: 86400) - var responseConfig = RelayConfig(tag: 2003, prompt: false) + var responseConfig = RelayConfig(tag: 2003, prompt: false, ttl: 86400) } diff --git a/Sources/WalletConnectNetworking/NetworkInteractor.swift b/Sources/WalletConnectNetworking/NetworkInteractor.swift index 10250f025..6b8cdecbd 100644 --- a/Sources/WalletConnectNetworking/NetworkInteractor.swift +++ b/Sources/WalletConnectNetworking/NetworkInteractor.swift @@ -96,7 +96,7 @@ public class NetworkingInteractor: NetworkInteracting { public func request(_ request: RPCRequest, topic: String, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws { try rpcHistory.set(request, forTopic: topic, emmitedBy: .local) let message = try! serializer.serialize(topic: topic, encodable: request, envelopeType: envelopeType) - try await relayClient.publish(topic: topic, payload: message, tag: protocolMethod.requestConfig.tag, prompt: protocolMethod.requestConfig.prompt) + try await relayClient.publish(topic: topic, payload: message, tag: protocolMethod.requestConfig.tag, prompt: protocolMethod.requestConfig.prompt, ttl: protocolMethod.requestConfig.ttl) } /// Completes with an acknowledgement from the relay network. @@ -107,7 +107,7 @@ public class NetworkingInteractor: NetworkInteracting { try rpcHistory.set(request, forTopic: topic, emmitedBy: .local) let message = try serializer.serialize(topic: topic, encodable: request) return try await withCheckedThrowingContinuation { continuation in - relayClient.publish(topic: topic, payload: message, tag: protocolMethod.requestConfig.tag, prompt: protocolMethod.requestConfig.prompt) { error in + relayClient.publish(topic: topic, payload: message, tag: protocolMethod.requestConfig.tag, prompt: protocolMethod.requestConfig.prompt, ttl: protocolMethod.requestConfig.ttl) { error in if let error = error { continuation.resume(throwing: error) } else { diff --git a/Sources/WalletConnectPairing/PairingProtocolMethod.swift b/Sources/WalletConnectPairing/PairingProtocolMethod.swift index c223a51c4..2cd6ac361 100644 --- a/Sources/WalletConnectPairing/PairingProtocolMethod.swift +++ b/Sources/WalletConnectPairing/PairingProtocolMethod.swift @@ -4,7 +4,7 @@ import WalletConnectNetworking struct PairingPingProtocolMethod: ProtocolMethod { var method: String = "wc_pairingPing" - var requestConfig = RelayConfig(tag: 1002, prompt: false) + var requestConfig = RelayConfig(tag: 1002, prompt: false, ttl: 30) - var responseConfig = RelayConfig(tag: 1003, prompt: false) + var responseConfig = RelayConfig(tag: 1003, prompt: false, ttl: 30) } diff --git a/Sources/WalletConnectRelay/Misc/Time.swift b/Sources/WalletConnectRelay/Misc/Time.swift deleted file mode 100644 index 5aaaafb87..000000000 --- a/Sources/WalletConnectRelay/Misc/Time.swift +++ /dev/null @@ -1,7 +0,0 @@ -// - -import Foundation - -enum Time { - static let hour = 3600 -} diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/PairingProtocolMethods.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/PairingProtocolMethods.swift new file mode 100644 index 000000000..cfd3bd53c --- /dev/null +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/PairingProtocolMethods.swift @@ -0,0 +1,18 @@ +import Foundation +import WalletConnectNetworking + +struct PairingPingProtocolMethod: ProtocolMethod { + var method: String = "wc_pairingPing" + + var requestConfig = RelayConfig(tag: 1002, prompt: false, ttl: 30) + + var responseConfig = RelayConfig(tag: 1003, prompt: false, ttl: 30) +} + +struct PairingDeleteProtocolMethod: ProtocolMethod { + var method: String = "wc_pairingDelete" + + var requestConfig = RelayConfig(tag: 1000, prompt: false, ttl: 86400) + + var responseConfig = RelayConfig(tag: 1001, prompt: false, ttl: 86400) +} diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionDeleteProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionDeleteProtocolMethod.swift new file mode 100644 index 000000000..e1b487aa8 --- /dev/null +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionDeleteProtocolMethod.swift @@ -0,0 +1,11 @@ +import Foundation +import WalletConnectNetworking + +struct SessionDeleteProtocolMethod: ProtocolMethod { + var method: String = "wc_sessionDelete" + + var requestConfig = RelayConfig(tag: 1112, prompt: false, ttl: 86400) + + var responseConfig = RelayConfig(tag: 1113, prompt: false, ttl: 86400) +} + diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionEventProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionEventProtocolMethod.swift new file mode 100644 index 000000000..585796e3f --- /dev/null +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionEventProtocolMethod.swift @@ -0,0 +1,10 @@ +import Foundation +import WalletConnectNetworking + +struct SessionEventProtocolMethod: ProtocolMethod { + var method: String = "wc_sessionEvent" + + var requestConfig = RelayConfig(tag: 1110, prompt: true, ttl: 300) + + var responseConfig = RelayConfig(tag: 1111, prompt: false, ttl: 300) +} diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionExtendProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionExtendProtocolMethod.swift new file mode 100644 index 000000000..94425014c --- /dev/null +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionExtendProtocolMethod.swift @@ -0,0 +1,10 @@ +import Foundation +import WalletConnectNetworking + +struct SessionExtendProtocolMethod: ProtocolMethod { + var method: String = "wc_sessionExtend" + + var requestConfig = RelayConfig(tag: 1106, prompt: false, ttl: 86400) + + var responseConfig = RelayConfig(tag: 1107, prompt: false, ttl: 86400) +} diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionPingProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionPingProtocolMethod.swift new file mode 100644 index 000000000..9be7feea0 --- /dev/null +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionPingProtocolMethod.swift @@ -0,0 +1,11 @@ +import Foundation +import WalletConnectNetworking + +struct SessionPingProtocolMethod: ProtocolMethod { + var method: String = "wc_sessionPing" + + var requestConfig = RelayConfig(tag: 1114, prompt: false, ttl: 30) + + var responseConfig = RelayConfig(tag: 1115, prompt: false, ttl: 30) +} + diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionProposeProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionProposeProtocolMethod.swift new file mode 100644 index 000000000..017325d99 --- /dev/null +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionProposeProtocolMethod.swift @@ -0,0 +1,10 @@ +import Foundation +import WalletConnectNetworking + +struct SessionProposeProtocolMethod: ProtocolMethod { + var method: String = "wc_sessionPropose" + + var requestConfig = RelayConfig(tag: 1100, prompt: true, ttl: 300) + + var responseConfig = RelayConfig(tag: 1101, prompt: false, ttl: 300) +} diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionRequestProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionRequestProtocolMethod.swift new file mode 100644 index 000000000..90047d521 --- /dev/null +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionRequestProtocolMethod.swift @@ -0,0 +1,10 @@ +import Foundation +import WalletConnectNetworking + +struct SessionRequestProtocolMethod: ProtocolMethod { + var method: String = "wc_sessionRequest" + + var requestConfig = RelayConfig(tag: 1108, prompt: true, ttl: 300) + + var responseConfig = RelayConfig(tag: 1109, prompt: false, ttl: 300) +} diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionSettleProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionSettleProtocolMethod.swift new file mode 100644 index 000000000..7a0341c1f --- /dev/null +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionSettleProtocolMethod.swift @@ -0,0 +1,10 @@ +import Foundation +import WalletConnectNetworking + +struct SessionSettleProtocolMethod: ProtocolMethod { + var method: String = "wc_sessionSettle" + + var requestConfig = RelayConfig(tag: 1102, prompt: false, ttl: 300) + + var responseConfig = RelayConfig(tag: 1103, prompt: false, ttl: 300) +} diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionUpdateProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionUpdateProtocolMethod.swift new file mode 100644 index 000000000..7ff5eb754 --- /dev/null +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionUpdateProtocolMethod.swift @@ -0,0 +1,11 @@ +import Foundation +import WalletConnectNetworking + +struct SessionUpdateProtocolMethod: ProtocolMethod { + var method: String = "wc_sessionUpdate" + + var requestConfig = RelayConfig(tag: 1104, prompt: false, ttl: 86400) + + var responseConfig = RelayConfig(tag: 1105, prompt: false, ttl: 86400) +} + diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/SignProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SignProtocolMethod.swift deleted file mode 100644 index 2058a0044..000000000 --- a/Sources/WalletConnectSign/Types/ProtocolMethods/SignProtocolMethod.swift +++ /dev/null @@ -1,83 +0,0 @@ -import Foundation -import WalletConnectNetworking - -struct PairingPingProtocolMethod: ProtocolMethod { - var method: String = "wc_pairingPing" - - var requestConfig = RelayConfig(tag: 1002, prompt: false) - - var responseConfig = RelayConfig(tag: 1003, prompt: false) -} - -struct PairingDeleteProtocolMethod: ProtocolMethod { - var method: String = "wc_pairingDelete" - - var requestConfig = RelayConfig(tag: 1000, prompt: false) - - var responseConfig = RelayConfig(tag: 1001, prompt: false) -} - -struct SessionProposeProtocolMethod: ProtocolMethod { - var method: String = "wc_sessionPropose" - - var requestConfig = RelayConfig(tag: 1100, prompt: true) - - var responseConfig = RelayConfig(tag: 1101, prompt: false) -} - -struct SessionSettleProtocolMethod: ProtocolMethod { - var method: String = "wc_sessionSettle" - - var requestConfig = RelayConfig(tag: 1102, prompt: false) - - var responseConfig = RelayConfig(tag: 1103, prompt: false) -} - -struct SessionUpdateProtocolMethod: ProtocolMethod { - var method: String = "wc_sessionUpdate" - - var requestConfig = RelayConfig(tag: 1104, prompt: false) - - var responseConfig = RelayConfig(tag: 1105, prompt: false) -} - -struct SessionExtendProtocolMethod: ProtocolMethod { - var method: String = "wc_sessionExtend" - - var requestConfig = RelayConfig(tag: 1106, prompt: false) - - var responseConfig = RelayConfig(tag: 1107, prompt: false) -} - -struct SessionDeleteProtocolMethod: ProtocolMethod { - var method: String = "wc_sessionDelete" - - var requestConfig = RelayConfig(tag: 1112, prompt: false) - - var responseConfig = RelayConfig(tag: 1113, prompt: false) -} - - -struct SessionRequestProtocolMethod: ProtocolMethod { - var method: String = "wc_sessionRequest" - - var requestConfig = RelayConfig(tag: 1108, prompt: true) - - var responseConfig = RelayConfig(tag: 1109, prompt: false) -} - -struct SessionPingProtocolMethod: ProtocolMethod { - var method: String = "wc_sessionPing" - - var requestConfig = RelayConfig(tag: 1114, prompt: false) - - var responseConfig = RelayConfig(tag: 1115, prompt: false) -} - -struct SessionEventProtocolMethod: ProtocolMethod { - var method: String = "wc_sessionEvent" - - var requestConfig = RelayConfig(tag: 1110, prompt: true) - - var responseConfig = RelayConfig(tag: 1111, prompt: false) -} From 80e050f8ca83328e347c66d3a7bb4aa629047c54 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Mon, 26 Sep 2022 12:54:21 +0200 Subject: [PATCH 09/10] fix unit tests --- Tests/AuthTests/WalletRequestSubscriberTests.swift | 2 +- Tests/RelayerTests/RelayClientTests.swift | 4 ++-- Tests/TestingUtils/NetworkingInteractorMock.swift | 6 +++--- Tests/WalletConnectSignTests/ApproveEngineTests.swift | 8 ++++---- Tests/WalletConnectSignTests/Stub/Stubs.swift | 8 ++++---- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Tests/AuthTests/WalletRequestSubscriberTests.swift b/Tests/AuthTests/WalletRequestSubscriberTests.swift index 5f30d166e..6df79685b 100644 --- a/Tests/AuthTests/WalletRequestSubscriberTests.swift +++ b/Tests/AuthTests/WalletRequestSubscriberTests.swift @@ -37,7 +37,7 @@ class WalletRequestSubscriberTests: XCTestCase { messageExpectation.fulfill() } - let request = RPCRequest(method: AuthProtocolMethod.authRequest.method, params: AuthRequestParams.stub(id: expectedRequestId), id: expectedRequestId.right!) + let request = RPCRequest(method: AuthRequestProtocolMethod().method, params: AuthRequestParams.stub(id: expectedRequestId), id: expectedRequestId.right!) networkingInteractor.requestPublisherSubject.send(("123", request)) wait(for: [messageExpectation], timeout: defaultTimeout) diff --git a/Tests/RelayerTests/RelayClientTests.swift b/Tests/RelayerTests/RelayClientTests.swift index f9c9384c2..f9094bf17 100644 --- a/Tests/RelayerTests/RelayClientTests.swift +++ b/Tests/RelayerTests/RelayClientTests.swift @@ -55,7 +55,7 @@ final class RelayClientTests: XCTestCase { func testPublishRequestAcknowledge() { let expectation = expectation(description: "Publish must callback on relay server acknowledgement") - sut.publish(topic: "", payload: "{}", tag: 0) { error in + sut.publish(topic: "", payload: "{}", tag: 0, prompt: false, ttl: 60) { error in XCTAssertNil(error) expectation.fulfill() } @@ -93,7 +93,7 @@ final class RelayClientTests: XCTestCase { } func testSendOnPublish() { - sut.publish(topic: "", payload: "", tag: 0, onNetworkAcknowledge: { _ in}) + sut.publish(topic: "", payload: "", tag: 0, prompt: false, ttl: 60, onNetworkAcknowledge: { _ in}) XCTAssertTrue(dispatcher.sent) } diff --git a/Tests/TestingUtils/NetworkingInteractorMock.swift b/Tests/TestingUtils/NetworkingInteractorMock.swift index b3e6714bb..171602eba 100644 --- a/Tests/TestingUtils/NetworkingInteractorMock.swift +++ b/Tests/TestingUtils/NetworkingInteractorMock.swift @@ -101,15 +101,15 @@ public class NetworkingInteractorMock: NetworkInteracting { requests.append((topic, request)) } - public func respond(topic: String, response: RPCResponse, tag: Int, envelopeType: Envelope.EnvelopeType) async throws { + public func respond(topic: String, response: RPCResponse, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws { didRespondOnTopic = topic } - public func respondSuccess(topic: String, requestId: RPCID, tag: Int, envelopeType: Envelope.EnvelopeType) async throws { + public func respondSuccess(topic: String, requestId: RPCID, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws { didRespondSuccess = true } - public func respondError(topic: String, requestId: RPCID, tag: Int, reason: Reason, envelopeType: Envelope.EnvelopeType) async throws { + public func respondError(topic: String, requestId: RPCID, protocolMethod: ProtocolMethod, reason: Reason, envelopeType: Envelope.EnvelopeType) async throws { lastErrorCode = reason.code didRespondError = true } diff --git a/Tests/WalletConnectSignTests/ApproveEngineTests.swift b/Tests/WalletConnectSignTests/ApproveEngineTests.swift index 3f2c39da8..4d0b75a7b 100644 --- a/Tests/WalletConnectSignTests/ApproveEngineTests.swift +++ b/Tests/WalletConnectSignTests/ApproveEngineTests.swift @@ -54,7 +54,7 @@ final class ApproveEngineTests: XCTestCase { pairingStorageMock.setPairing(pairing) let proposerPubKey = AgreementPrivateKey().publicKey.hexRepresentation let proposal = SessionProposal.stub(proposerPubKey: proposerPubKey) - let request = RPCRequest(method: SignProtocolMethod.sessionPropose.method, params: proposal) + let request = RPCRequest(method: SessionProposeProtocolMethod().method, params: proposal) networkingInteractor.requestPublisherSubject.send((topicA, request)) try await engine.approveProposal(proposerPubKey: proposal.proposer.publicKey, validating: SessionNamespace.stubDictionary()) @@ -75,7 +75,7 @@ final class ApproveEngineTests: XCTestCase { var sessionProposed = false let proposerPubKey = AgreementPrivateKey().publicKey.hexRepresentation let proposal = SessionProposal.stub(proposerPubKey: proposerPubKey) - let request = RPCRequest(method: SignProtocolMethod.sessionPropose.method, params: proposal) + let request = RPCRequest(method: SessionProposeProtocolMethod().method, params: proposal) engine.onSessionProposal = { _ in sessionProposed = true @@ -119,7 +119,7 @@ final class ApproveEngineTests: XCTestCase { let session = WCSession.stub(isSelfController: true, acknowledged: false) sessionStorageMock.setSession(session) - let request = RPCRequest(method: SignProtocolMethod.sessionSettle.method, params: SessionType.SettleParams.stub()) + let request = RPCRequest(method: SessionSettleProtocolMethod().method, params: SessionType.SettleParams.stub()) let response = RPCResponse(matchingRequest: request, result: RPCResult.response(AnyCodable(true))) networkingInteractor.responsePublisherSubject.send((session.topic, request, response)) @@ -134,7 +134,7 @@ final class ApproveEngineTests: XCTestCase { cryptoMock.setAgreementSecret(AgreementKeys.stub(), topic: session.topic) try! cryptoMock.setPrivateKey(privateKey) - let request = RPCRequest(method: SignProtocolMethod.sessionSettle.method, params: SessionType.SettleParams.stub()) + let request = RPCRequest(method: SessionSettleProtocolMethod().method, params: SessionType.SettleParams.stub()) let response = RPCResponse.stubError(forRequest: request) networkingInteractor.responsePublisherSubject.send((session.topic, request, response)) diff --git a/Tests/WalletConnectSignTests/Stub/Stubs.swift b/Tests/WalletConnectSignTests/Stub/Stubs.swift index f0b87a080..0a15c18d5 100644 --- a/Tests/WalletConnectSignTests/Stub/Stubs.swift +++ b/Tests/WalletConnectSignTests/Stub/Stubs.swift @@ -63,22 +63,22 @@ extension AgreementPeer { extension RPCRequest { static func stubUpdateNamespaces(namespaces: [String: SessionNamespace] = SessionNamespace.stubDictionary()) -> RPCRequest { - return RPCRequest(method: SignProtocolMethod.sessionUpdate.method, params: SessionType.UpdateParams(namespaces: namespaces)) + return RPCRequest(method: SessionUpdateProtocolMethod().method, params: SessionType.UpdateParams(namespaces: namespaces)) } static func stubUpdateExpiry(expiry: Int64) -> RPCRequest { - return RPCRequest(method: SignProtocolMethod.sessionExtend.method, params: SessionType.UpdateExpiryParams(expiry: expiry)) + return RPCRequest(method: SessionExtendProtocolMethod().method, params: SessionType.UpdateExpiryParams(expiry: expiry)) } static func stubSettle() -> RPCRequest { - return RPCRequest(method: SignProtocolMethod.sessionSettle.method, params: SessionType.SettleParams.stub()) + return RPCRequest(method: SessionSettleProtocolMethod().method, params: SessionType.SettleParams.stub()) } static func stubRequest(method: String, chainId: Blockchain) -> RPCRequest { let params = SessionType.RequestParams( request: SessionType.RequestParams.Request(method: method, params: AnyCodable(EmptyCodable())), chainId: chainId) - return RPCRequest(method: SignProtocolMethod.sessionRequest.method, params: params) + return RPCRequest(method: SessionRequestProtocolMethod().method, params: params) } } From ac90e6f97a819f058b88d4e45afcccd39b568124 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Tue, 27 Sep 2022 11:03:41 +0200 Subject: [PATCH 10/10] change vars for lets in protocol methods --- Sources/Auth/AuthProtocolMethod.swift | 18 +++++++++--------- Sources/Chat/Types/ChatProtocolMethod.swift | 12 ++++++------ .../PairingProtocolMethod.swift | 6 +++--- .../Engine/Common/SessionEngine.swift | 4 ++-- .../PairingProtocolMethods.swift | 12 ++++++------ .../SessionDeleteProtocolMethod.swift | 6 +++--- .../SessionEventProtocolMethod.swift | 6 +++--- .../SessionExtendProtocolMethod.swift | 6 +++--- .../SessionPingProtocolMethod.swift | 6 +++--- .../SessionProposeProtocolMethod.swift | 6 +++--- .../SessionRequestProtocolMethod.swift | 6 +++--- .../SessionSettleProtocolMethod.swift | 6 +++--- .../SessionUpdateProtocolMethod.swift | 6 +++--- 13 files changed, 50 insertions(+), 50 deletions(-) diff --git a/Sources/Auth/AuthProtocolMethod.swift b/Sources/Auth/AuthProtocolMethod.swift index e54889755..02d53d559 100644 --- a/Sources/Auth/AuthProtocolMethod.swift +++ b/Sources/Auth/AuthProtocolMethod.swift @@ -2,27 +2,27 @@ import Foundation import WalletConnectNetworking struct AuthRequestProtocolMethod: ProtocolMethod { - var method: String = "wc_authRequest" + let method: String = "wc_authRequest" - var requestConfig = RelayConfig(tag: 3000, prompt: true, ttl: 86400) + let requestConfig = RelayConfig(tag: 3000, prompt: true, ttl: 86400) - var responseConfig = RelayConfig(tag: 3001, prompt: false, ttl: 86400) + let responseConfig = RelayConfig(tag: 3001, prompt: false, ttl: 86400) } struct PairingPingProtocolMethod: ProtocolMethod { - var method: String = "wc_pairingPing" + let method: String = "wc_pairingPing" - var requestConfig = RelayConfig(tag: 1002, prompt: false, ttl: 30) + let requestConfig = RelayConfig(tag: 1002, prompt: false, ttl: 30) - var responseConfig = RelayConfig(tag: 1003, prompt: false, ttl: 30) + let responseConfig = RelayConfig(tag: 1003, prompt: false, ttl: 30) } struct PairingDeleteProtocolMethod: ProtocolMethod { - var method: String = "wc_pairingDelete" + let method: String = "wc_pairingDelete" - var requestConfig = RelayConfig(tag: 1000, prompt: false, ttl: 86400) + let requestConfig = RelayConfig(tag: 1000, prompt: false, ttl: 86400) - var responseConfig = RelayConfig(tag: 1001, prompt: false, ttl: 86400) + let responseConfig = RelayConfig(tag: 1001, prompt: false, ttl: 86400) } diff --git a/Sources/Chat/Types/ChatProtocolMethod.swift b/Sources/Chat/Types/ChatProtocolMethod.swift index 82dcd15dc..f24a5f971 100644 --- a/Sources/Chat/Types/ChatProtocolMethod.swift +++ b/Sources/Chat/Types/ChatProtocolMethod.swift @@ -2,19 +2,19 @@ import Foundation import WalletConnectNetworking struct ChatInviteProtocolMethod: ProtocolMethod { - var method: String = "wc_chatInvite" + let method: String = "wc_chatInvite" - var requestConfig = RelayConfig(tag: 2000, prompt: true, ttl: 86400) + let requestConfig = RelayConfig(tag: 2000, prompt: true, ttl: 86400) - var responseConfig = RelayConfig(tag: 2001, prompt: false, ttl: 86400) + let responseConfig = RelayConfig(tag: 2001, prompt: false, ttl: 86400) } struct ChatMessageProtocolMethod: ProtocolMethod { - var method: String = "wc_chatMessage" + let method: String = "wc_chatMessage" - var requestConfig = RelayConfig(tag: 2002, prompt: true, ttl: 86400) + let requestConfig = RelayConfig(tag: 2002, prompt: true, ttl: 86400) - var responseConfig = RelayConfig(tag: 2003, prompt: false, ttl: 86400) + let responseConfig = RelayConfig(tag: 2003, prompt: false, ttl: 86400) } diff --git a/Sources/WalletConnectPairing/PairingProtocolMethod.swift b/Sources/WalletConnectPairing/PairingProtocolMethod.swift index 2cd6ac361..10d39127f 100644 --- a/Sources/WalletConnectPairing/PairingProtocolMethod.swift +++ b/Sources/WalletConnectPairing/PairingProtocolMethod.swift @@ -2,9 +2,9 @@ import Foundation import WalletConnectNetworking struct PairingPingProtocolMethod: ProtocolMethod { - var method: String = "wc_pairingPing" + let method: String = "wc_pairingPing" - var requestConfig = RelayConfig(tag: 1002, prompt: false, ttl: 30) + let requestConfig = RelayConfig(tag: 1002, prompt: false, ttl: 30) - var responseConfig = RelayConfig(tag: 1003, prompt: false, ttl: 30) + let responseConfig = RelayConfig(tag: 1003, prompt: false, ttl: 30) } diff --git a/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift b/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift index 2ca824ff1..2a2ca7d19 100644 --- a/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift +++ b/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift @@ -58,8 +58,8 @@ final class SessionEngine { } let chainRequest = SessionType.RequestParams.Request(method: request.method, params: request.params) let sessionRequestParams = SessionType.RequestParams(request: chainRequest, chainId: request.chainId) - - let rpcRequest = RPCRequest(method: SessionRequestProtocolMethod().method, params: sessionRequestParams) + let protocolMethod = SessionRequestProtocolMethod() + let rpcRequest = RPCRequest(method: protocolMethod.method, params: sessionRequestParams) try await networkingInteractor.request(rpcRequest, topic: request.topic, protocolMethod: SessionRequestProtocolMethod()) } diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/PairingProtocolMethods.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/PairingProtocolMethods.swift index cfd3bd53c..19167c4b5 100644 --- a/Sources/WalletConnectSign/Types/ProtocolMethods/PairingProtocolMethods.swift +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/PairingProtocolMethods.swift @@ -2,17 +2,17 @@ import Foundation import WalletConnectNetworking struct PairingPingProtocolMethod: ProtocolMethod { - var method: String = "wc_pairingPing" + let method: String = "wc_pairingPing" - var requestConfig = RelayConfig(tag: 1002, prompt: false, ttl: 30) + let requestConfig = RelayConfig(tag: 1002, prompt: false, ttl: 30) - var responseConfig = RelayConfig(tag: 1003, prompt: false, ttl: 30) + let responseConfig = RelayConfig(tag: 1003, prompt: false, ttl: 30) } struct PairingDeleteProtocolMethod: ProtocolMethod { - var method: String = "wc_pairingDelete" + let method: String = "wc_pairingDelete" - var requestConfig = RelayConfig(tag: 1000, prompt: false, ttl: 86400) + let requestConfig = RelayConfig(tag: 1000, prompt: false, ttl: 86400) - var responseConfig = RelayConfig(tag: 1001, prompt: false, ttl: 86400) + let responseConfig = RelayConfig(tag: 1001, prompt: false, ttl: 86400) } diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionDeleteProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionDeleteProtocolMethod.swift index e1b487aa8..b9d7fc9f3 100644 --- a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionDeleteProtocolMethod.swift +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionDeleteProtocolMethod.swift @@ -2,10 +2,10 @@ import Foundation import WalletConnectNetworking struct SessionDeleteProtocolMethod: ProtocolMethod { - var method: String = "wc_sessionDelete" + let method: String = "wc_sessionDelete" - var requestConfig = RelayConfig(tag: 1112, prompt: false, ttl: 86400) + let requestConfig = RelayConfig(tag: 1112, prompt: false, ttl: 86400) - var responseConfig = RelayConfig(tag: 1113, prompt: false, ttl: 86400) + let responseConfig = RelayConfig(tag: 1113, prompt: false, ttl: 86400) } diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionEventProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionEventProtocolMethod.swift index 585796e3f..bee208ed4 100644 --- a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionEventProtocolMethod.swift +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionEventProtocolMethod.swift @@ -2,9 +2,9 @@ import Foundation import WalletConnectNetworking struct SessionEventProtocolMethod: ProtocolMethod { - var method: String = "wc_sessionEvent" + let method: String = "wc_sessionEvent" - var requestConfig = RelayConfig(tag: 1110, prompt: true, ttl: 300) + let requestConfig = RelayConfig(tag: 1110, prompt: true, ttl: 300) - var responseConfig = RelayConfig(tag: 1111, prompt: false, ttl: 300) + let responseConfig = RelayConfig(tag: 1111, prompt: false, ttl: 300) } diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionExtendProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionExtendProtocolMethod.swift index 94425014c..b77e5b9ff 100644 --- a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionExtendProtocolMethod.swift +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionExtendProtocolMethod.swift @@ -2,9 +2,9 @@ import Foundation import WalletConnectNetworking struct SessionExtendProtocolMethod: ProtocolMethod { - var method: String = "wc_sessionExtend" + let method: String = "wc_sessionExtend" - var requestConfig = RelayConfig(tag: 1106, prompt: false, ttl: 86400) + let requestConfig = RelayConfig(tag: 1106, prompt: false, ttl: 86400) - var responseConfig = RelayConfig(tag: 1107, prompt: false, ttl: 86400) + let responseConfig = RelayConfig(tag: 1107, prompt: false, ttl: 86400) } diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionPingProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionPingProtocolMethod.swift index 9be7feea0..109ff0870 100644 --- a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionPingProtocolMethod.swift +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionPingProtocolMethod.swift @@ -2,10 +2,10 @@ import Foundation import WalletConnectNetworking struct SessionPingProtocolMethod: ProtocolMethod { - var method: String = "wc_sessionPing" + let method: String = "wc_sessionPing" - var requestConfig = RelayConfig(tag: 1114, prompt: false, ttl: 30) + let requestConfig = RelayConfig(tag: 1114, prompt: false, ttl: 30) - var responseConfig = RelayConfig(tag: 1115, prompt: false, ttl: 30) + let responseConfig = RelayConfig(tag: 1115, prompt: false, ttl: 30) } diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionProposeProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionProposeProtocolMethod.swift index 017325d99..47191c4a2 100644 --- a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionProposeProtocolMethod.swift +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionProposeProtocolMethod.swift @@ -2,9 +2,9 @@ import Foundation import WalletConnectNetworking struct SessionProposeProtocolMethod: ProtocolMethod { - var method: String = "wc_sessionPropose" + let method: String = "wc_sessionPropose" - var requestConfig = RelayConfig(tag: 1100, prompt: true, ttl: 300) + let requestConfig = RelayConfig(tag: 1100, prompt: true, ttl: 300) - var responseConfig = RelayConfig(tag: 1101, prompt: false, ttl: 300) + let responseConfig = RelayConfig(tag: 1101, prompt: false, ttl: 300) } diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionRequestProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionRequestProtocolMethod.swift index 90047d521..e2c0e4234 100644 --- a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionRequestProtocolMethod.swift +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionRequestProtocolMethod.swift @@ -2,9 +2,9 @@ import Foundation import WalletConnectNetworking struct SessionRequestProtocolMethod: ProtocolMethod { - var method: String = "wc_sessionRequest" + let method: String = "wc_sessionRequest" - var requestConfig = RelayConfig(tag: 1108, prompt: true, ttl: 300) + let requestConfig = RelayConfig(tag: 1108, prompt: true, ttl: 300) - var responseConfig = RelayConfig(tag: 1109, prompt: false, ttl: 300) + let responseConfig = RelayConfig(tag: 1109, prompt: false, ttl: 300) } diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionSettleProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionSettleProtocolMethod.swift index 7a0341c1f..131560b4c 100644 --- a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionSettleProtocolMethod.swift +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionSettleProtocolMethod.swift @@ -2,9 +2,9 @@ import Foundation import WalletConnectNetworking struct SessionSettleProtocolMethod: ProtocolMethod { - var method: String = "wc_sessionSettle" + let method: String = "wc_sessionSettle" - var requestConfig = RelayConfig(tag: 1102, prompt: false, ttl: 300) + let requestConfig = RelayConfig(tag: 1102, prompt: false, ttl: 300) - var responseConfig = RelayConfig(tag: 1103, prompt: false, ttl: 300) + let responseConfig = RelayConfig(tag: 1103, prompt: false, ttl: 300) } diff --git a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionUpdateProtocolMethod.swift b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionUpdateProtocolMethod.swift index 7ff5eb754..859bddbd8 100644 --- a/Sources/WalletConnectSign/Types/ProtocolMethods/SessionUpdateProtocolMethod.swift +++ b/Sources/WalletConnectSign/Types/ProtocolMethods/SessionUpdateProtocolMethod.swift @@ -2,10 +2,10 @@ import Foundation import WalletConnectNetworking struct SessionUpdateProtocolMethod: ProtocolMethod { - var method: String = "wc_sessionUpdate" + let method: String = "wc_sessionUpdate" - var requestConfig = RelayConfig(tag: 1104, prompt: false, ttl: 86400) + let requestConfig = RelayConfig(tag: 1104, prompt: false, ttl: 86400) - var responseConfig = RelayConfig(tag: 1105, prompt: false, ttl: 86400) + let responseConfig = RelayConfig(tag: 1105, prompt: false, ttl: 86400) }