-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
108 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
Sources/WalletConnectNotify/Client/Wallet/NotifySubsctiptionsUpdater.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import Foundation | ||
import Combine | ||
|
||
final class NotifySubsctiptionsUpdater { | ||
private let networkingInteractor: NetworkInteracting | ||
private let kms: KeyManagementServiceProtocol | ||
private let logger: ConsoleLogging | ||
private let notifyStorage: NotifyStorage | ||
private let groupKeychainStorage: KeychainStorageProtocol | ||
|
||
private let subscriptionChangedSubject = PassthroughSubject<[NotifySubscription], Never>() | ||
|
||
var subscriptionChangedPublisher: AnyPublisher<[NotifySubscription], Never> { | ||
return subscriptionChangedSubject.eraseToAnyPublisher() | ||
} | ||
|
||
init(networkingInteractor: NetworkInteracting, kms: KeyManagementServiceProtocol, logger: ConsoleLogging, notifyStorage: NotifyStorage, groupKeychainStorage: KeychainStorageProtocol) { | ||
self.networkingInteractor = networkingInteractor | ||
self.kms = kms | ||
self.logger = logger | ||
self.notifyStorage = notifyStorage | ||
self.groupKeychainStorage = groupKeychainStorage | ||
} | ||
|
||
func update(subscriptions newSubscriptions: [NotifySubscription], for account: Account) async throws { | ||
let oldSubscriptions = notifyStorage.getSubscriptions(account: account) | ||
|
||
subscriptionChangedSubject.send(newSubscriptions) | ||
|
||
try Task.checkCancellation() | ||
|
||
let subscriptions = oldSubscriptions.difference(from: newSubscriptions) | ||
|
||
logger.debug("Received: \(newSubscriptions.count), changed: \(subscriptions.count)") | ||
|
||
if subscriptions.count > 0 { | ||
try notifyStorage.replaceAllSubscriptions(newSubscriptions) | ||
|
||
for subscription in newSubscriptions { | ||
let symKey = try SymmetricKey(hex: subscription.symKey) | ||
try groupKeychainStorage.add(symKey, forKey: subscription.topic) | ||
try kms.setSymmetricKey(symKey, for: subscription.topic) | ||
} | ||
|
||
let topics = newSubscriptions.map { $0.topic } | ||
|
||
try await networkingInteractor.batchSubscribe(topics: topics) | ||
|
||
try Task.checkCancellation() | ||
|
||
logger.debug("Updated Subscriptions by Subscriptions Changed Request", properties: [ | ||
"topics": newSubscriptions.map { $0.topic }.joined(separator: ",") | ||
]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.