Skip to content

Commit

Permalink
Delete response subsciber
Browse files Browse the repository at this point in the history
  • Loading branch information
flypaper0 committed Jan 23, 2024
1 parent 83de948 commit 2a24c25
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 27 deletions.
11 changes: 7 additions & 4 deletions Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand All @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
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

init(
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
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()
}
}

0 comments on commit 2a24c25

Please sign in to comment.