diff --git a/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift b/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift index a83325296..69273a50f 100644 --- a/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift +++ b/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift @@ -17,7 +17,7 @@ public class NotifyClient { return logger.logsPublisher } - private let deleteNotifySubscriptionRequester: DeleteNotifySubscriptionRequester + private let notifyDeleteSubscriptionRequester: NotifyDeleteSubscriptionRequester private let notifySubscribeRequester: NotifySubscribeRequester public let logger: ConsoleLogging @@ -31,6 +31,7 @@ public class NotifyClient { private let notifyMessageSubscriber: NotifyMessageSubscriber private let resubscribeService: NotifyResubscribeService private let notifySubscribeResponseSubscriber: NotifySubscribeResponseSubscriber + private let notifyDeleteSubscriptionSubscriber: NotifyDeleteSubscriptionSubscriber private let notifyUpdateRequester: NotifyUpdateRequester private let notifyUpdateResponseSubscriber: NotifyUpdateResponseSubscriber private let subscriptionsAutoUpdater: SubscriptionsAutoUpdater @@ -48,10 +49,11 @@ public class NotifyClient { pushClient: PushClient, notifyMessageSubscriber: NotifyMessageSubscriber, notifyStorage: NotifyStorage, - deleteNotifySubscriptionRequester: DeleteNotifySubscriptionRequester, + notifyDeleteSubscriptionRequester: NotifyDeleteSubscriptionRequester, resubscribeService: NotifyResubscribeService, notifySubscribeRequester: NotifySubscribeRequester, notifySubscribeResponseSubscriber: NotifySubscribeResponseSubscriber, + notifyDeleteSubscriptionSubscriber: NotifyDeleteSubscriptionSubscriber, notifyUpdateRequester: NotifyUpdateRequester, notifyUpdateResponseSubscriber: NotifyUpdateResponseSubscriber, notifyAccountProvider: NotifyAccountProvider, @@ -69,10 +71,11 @@ public class NotifyClient { self.historyService = historyService self.notifyMessageSubscriber = notifyMessageSubscriber self.notifyStorage = notifyStorage - self.deleteNotifySubscriptionRequester = deleteNotifySubscriptionRequester + self.notifyDeleteSubscriptionRequester = notifyDeleteSubscriptionRequester self.resubscribeService = resubscribeService self.notifySubscribeRequester = notifySubscribeRequester self.notifySubscribeResponseSubscriber = notifySubscribeResponseSubscriber + self.notifyDeleteSubscriptionSubscriber = notifyDeleteSubscriptionSubscriber self.notifyUpdateRequester = notifyUpdateRequester self.notifyUpdateResponseSubscriber = notifyUpdateResponseSubscriber self.notifyAccountProvider = notifyAccountProvider @@ -128,7 +131,7 @@ public class NotifyClient { } public func deleteSubscription(topic: String) async throws { - try await deleteNotifySubscriptionRequester.delete(topic: topic) + try await notifyDeleteSubscriptionRequester.delete(topic: topic) } public func deleteNotifyMessage(id: String) { diff --git a/Sources/WalletConnectNotify/Client/Wallet/NotifyClientFactory.swift b/Sources/WalletConnectNotify/Client/Wallet/NotifyClientFactory.swift index b6b5fcdfc..f7a27ca44 100644 --- a/Sources/WalletConnectNotify/Client/Wallet/NotifyClientFactory.swift +++ b/Sources/WalletConnectNotify/Client/Wallet/NotifyClientFactory.swift @@ -47,7 +47,7 @@ public struct NotifyClientFactory { let identityClient = IdentityClientFactory.create(keyserver: keyserverURL, keychain: keychainStorage, logger: logger) let notifyMessageSubscriber = NotifyMessageSubscriber(keyserver: keyserverURL, networkingInteractor: networkInteractor, identityClient: identityClient, notifyStorage: notifyStorage, crypto: crypto, logger: logger) let webDidResolver = NotifyWebDidResolver() - let deleteNotifySubscriptionRequester = DeleteNotifySubscriptionRequester(keyserver: keyserverURL, networkingInteractor: networkInteractor, identityClient: identityClient, kms: kms, logger: logger, notifyStorage: notifyStorage) + let notifyDeleteSubscriptionRequester = NotifyDeleteSubscriptionRequester(keyserver: keyserverURL, networkingInteractor: networkInteractor, identityClient: identityClient, logger: logger, notifyStorage: notifyStorage) let resubscribeService = NotifyResubscribeService(networkInteractor: networkInteractor, notifyStorage: notifyStorage, logger: logger) let notifyConfigProvider = NotifyConfigProvider(projectId: projectId, explorerHost: explorerHost) @@ -62,7 +62,7 @@ public struct NotifyClientFactory { let notifyUpdateRequester = NotifyUpdateRequester(keyserverURL: keyserverURL, identityClient: identityClient, networkingInteractor: networkInteractor, notifyConfigProvider: notifyConfigProvider, logger: logger, notifyStorage: notifyStorage) - let notifyUpdateResponseSubscriber = NotifyUpdateResponseSubscriber(networkingInteractor: networkInteractor, logger: logger) + let notifyUpdateResponseSubscriber = NotifyUpdateResponseSubscriber(networkingInteractor: networkInteractor, logger: logger, notifySubscriptionsBuilder: notifySubscriptionsBuilder, notifySubscriptionsUpdater: notifySubscriptionsUpdater) let subscriptionsAutoUpdater = SubscriptionsAutoUpdater(notifyUpdateRequester: notifyUpdateRequester, logger: logger, notifyStorage: notifyStorage) @@ -72,6 +72,7 @@ public struct NotifyClientFactory { let notifySubscriptionsChangedRequestSubscriber = NotifySubscriptionsChangedRequestSubscriber(keyserver: keyserverURL, networkingInteractor: networkInteractor, identityClient: identityClient, logger: logger, notifySubscriptionsUpdater: notifySubscriptionsUpdater, notifySubscriptionsBuilder: notifySubscriptionsBuilder) let subscriptionWatcher = SubscriptionWatcher(notifyWatchSubscriptionsRequester: notifyWatchSubscriptionsRequester, logger: logger) let historyService = HistoryService(keyserver: keyserverURL, networkingClient: networkInteractor, identityClient: identityClient) + let notifyDeleteSubscriptionSubscriber = NotifyDeleteSubscriptionSubscriber(networkingInteractor: networkInteractor, kms: kms, logger: logger, notifySubscriptionsBuilder: notifySubscriptionsBuilder, notifySubscriptionsUpdater: notifySubscriptionsUpdater) return NotifyClient( logger: logger, @@ -82,10 +83,11 @@ public struct NotifyClientFactory { pushClient: pushClient, notifyMessageSubscriber: notifyMessageSubscriber, notifyStorage: notifyStorage, - deleteNotifySubscriptionRequester: deleteNotifySubscriptionRequester, + notifyDeleteSubscriptionRequester: notifyDeleteSubscriptionRequester, resubscribeService: resubscribeService, notifySubscribeRequester: notifySubscribeRequester, - notifySubscribeResponseSubscriber: notifySubscribeResponseSubscriber, + notifySubscribeResponseSubscriber: notifySubscribeResponseSubscriber, + notifyDeleteSubscriptionSubscriber: notifyDeleteSubscriptionSubscriber, notifyUpdateRequester: notifyUpdateRequester, notifyUpdateResponseSubscriber: notifyUpdateResponseSubscriber, notifyAccountProvider: notifyAccountProvider, diff --git a/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyDelete/DeleteNotifySubscriptionRequester.swift b/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyDelete/NotifyDeleteSubscriptionRequester.swift similarity index 84% rename from Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyDelete/DeleteNotifySubscriptionRequester.swift rename to Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyDelete/NotifyDeleteSubscriptionRequester.swift index b9c86009c..762f4948d 100644 --- a/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyDelete/DeleteNotifySubscriptionRequester.swift +++ b/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyDelete/NotifyDeleteSubscriptionRequester.swift @@ -1,13 +1,12 @@ import Foundation -class DeleteNotifySubscriptionRequester { +class NotifyDeleteSubscriptionRequester { enum Errors: Error { case notifySubscriptionNotFound } private let keyserver: URL private let networkingInteractor: NetworkInteracting private let identityClient: IdentityClient - private let kms: KeyManagementServiceProtocol private let logger: ConsoleLogging private let notifyStorage: NotifyStorage @@ -15,14 +14,12 @@ class DeleteNotifySubscriptionRequester { keyserver: URL, networkingInteractor: NetworkInteracting, identityClient: IdentityClient, - kms: KeyManagementServiceProtocol, logger: ConsoleLogging, notifyStorage: NotifyStorage ) { self.keyserver = keyserver self.networkingInteractor = networkingInteractor self.identityClient = identityClient - self.kms = kms self.logger = logger self.notifyStorage = notifyStorage } @@ -49,15 +46,11 @@ class DeleteNotifySubscriptionRequester { try notifyStorage.deleteSubscription(topic: topic) try notifyStorage.deleteMessages(topic: topic) - networkingInteractor.unsubscribe(topic: topic) - - logger.debug("Subscription removed, topic: \(topic)") - - kms.deleteSymmetricKey(for: topic) + logger.debug("Subscription delete request sent, topic: \(topic)") } } -private extension DeleteNotifySubscriptionRequester { +private extension NotifyDeleteSubscriptionRequester { func createJWTWrapper(dappPubKey: DIDKey, reason: String, app: DIDWeb, account: Account) throws -> NotifyDeletePayload.Wrapper { let jwtPayload = NotifyDeletePayload(account: account, keyserver: keyserver, dappPubKey: dappPubKey, app: app) diff --git a/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyDelete/NotifyDeleteSubscriptionSubscriber.swift b/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyDelete/NotifyDeleteSubscriptionSubscriber.swift new file mode 100644 index 000000000..3768b6697 --- /dev/null +++ b/Sources/WalletConnectNotify/Client/Wallet/ProtocolEngine/wc_notifyDelete/NotifyDeleteSubscriptionSubscriber.swift @@ -0,0 +1,51 @@ +import Foundation +import Combine + +class NotifyDeleteSubscriptionSubscriber { + + private let networkingInteractor: NetworkInteracting + private let kms: KeyManagementServiceProtocol + private let logger: ConsoleLogging + private let notifySubscriptionsBuilder: NotifySubscriptionsBuilder + private let notifySubscriptionsUpdater: NotifySubsctiptionsUpdater + + init( + networkingInteractor: NetworkInteracting, + kms: KeyManagementServiceProtocol, + logger: ConsoleLogging, + notifySubscriptionsBuilder: NotifySubscriptionsBuilder, + notifySubscriptionsUpdater: NotifySubsctiptionsUpdater + ) { + self.networkingInteractor = networkingInteractor + self.kms = kms + self.logger = logger + self.notifySubscriptionsBuilder = notifySubscriptionsBuilder + self.notifySubscriptionsUpdater = notifySubscriptionsUpdater + + subscribeForDeleteResponse() + } +} + +private extension NotifyDeleteSubscriptionSubscriber { + + func subscribeForDeleteResponse() { + networkingInteractor.subscribeOnResponse( + protocolMethod: NotifyDeleteProtocolMethod(), + requestOfType: NotifyDeletePayload.Wrapper.self, + responseOfType: NotifyDeleteResponsePayload.Wrapper.self, + errorHandler: logger + ) { [unowned self] payload in + + let (responsePayload, _) = try NotifyDeleteResponsePayload.decodeAndVerify(from: payload.response) + + let subscriptions = try await notifySubscriptionsBuilder.buildSubscriptions(responsePayload.subscriptions) + + try await notifySubscriptionsUpdater.update(subscriptions: subscriptions, for: responsePayload.account) + + logger.debug("Received Notify Delete response") + + networkingInteractor.unsubscribe(topic: payload.topic) + kms.deleteSymmetricKey(for: payload.topic) + } + } +} diff --git a/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyDeletePayload.swift b/Sources/WalletConnectNotify/Types/JWTPayloads/notify_delete/NotifyDeletePayload.swift similarity index 100% rename from Sources/WalletConnectNotify/Types/JWTPayloads/NotifyDeletePayload.swift rename to Sources/WalletConnectNotify/Types/JWTPayloads/notify_delete/NotifyDeletePayload.swift diff --git a/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyDeleteResponsePayload.swift b/Sources/WalletConnectNotify/Types/JWTPayloads/notify_delete/NotifyDeleteResponsePayload.swift similarity index 84% rename from Sources/WalletConnectNotify/Types/JWTPayloads/NotifyDeleteResponsePayload.swift rename to Sources/WalletConnectNotify/Types/JWTPayloads/notify_delete/NotifyDeleteResponsePayload.swift index b9f97cc43..ac7f76e0d 100644 --- a/Sources/WalletConnectNotify/Types/JWTPayloads/NotifyDeleteResponsePayload.swift +++ b/Sources/WalletConnectNotify/Types/JWTPayloads/notify_delete/NotifyDeleteResponsePayload.swift @@ -16,6 +16,8 @@ struct NotifyDeleteResponsePayload: JWTClaimsCodable { let aud: String /// Blockchain account that notify subscription has been proposed for -`did:pkh` let sub: String + /// array of Notify Server Subscriptions + let sbs: [NotifyServerSubscription] /// Dapp's domain url let app: String @@ -39,22 +41,16 @@ struct NotifyDeleteResponsePayload: JWTClaimsCodable { let account: Account let selfPubKey: DIDKey let app: DIDWeb + let subscriptions: [NotifyServerSubscription] init(claims: Claims) throws { self.account = try Account(DIDPKHString: claims.sub) self.selfPubKey = try DIDKey(did: claims.aud) self.app = try DIDWeb(did: claims.app) + self.subscriptions = claims.sbs } func encode(iss: String) throws -> Claims { - return Claims( - iat: defaultIat(), - exp: expiry(days: 1), - act: Claims.action, - iss: iss, - aud: selfPubKey.did(variant: .ED25519), - sub: account.did, - app: app.did - ) + fatalError() } }