Skip to content

Commit

Permalink
Merge pull request #1072 from WalletConnect/feature/notify-sync-remove
Browse files Browse the repository at this point in the history
[Notify] Sync remove
  • Loading branch information
llbartekll authored Sep 2, 2023
2 parents 4989111 + c4a0eb2 commit 4c99850
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 336 deletions.
114 changes: 53 additions & 61 deletions Example/IntegrationTests/Push/NotifyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import WalletConnectNetworking
import WalletConnectPush
@testable import WalletConnectNotify
@testable import WalletConnectPairing
@testable import WalletConnectSync
@testable import WalletConnectHistory
import WalletConnectIdentity
import WalletConnectSigner

Expand All @@ -22,8 +20,6 @@ final class NotifyTests: XCTestCase {

let gmDappUrl = "https://notify.gm.walletconnect.com/"

var pairingStorage: PairingStorage!

let pk = try! EthereumPrivateKey()

var privateKey: Data {
Expand All @@ -34,9 +30,9 @@ final class NotifyTests: XCTestCase {
return Account("eip155:1:" + pk.address.hex(eip55: true))!
}

private var publishers = [AnyCancellable]()
private var publishers = Set<AnyCancellable>()

func makeClientDependencies(prefix: String) -> (PairingClient, NetworkInteracting, SyncClient, KeychainStorageProtocol, KeyValueStorage) {
func makeClientDependencies(prefix: String) -> (PairingClient, NetworkInteracting, KeychainStorageProtocol, KeyValueStorage) {
let keychain = KeychainStorageMock()
let keyValueStorage = RuntimeKeyValueStorage()

Expand Down Expand Up @@ -64,28 +60,21 @@ final class NotifyTests: XCTestCase {
keychainStorage: keychain,
networkingClient: networkingClient)

let syncClient = SyncClientFactory.create(networkInteractor: networkingClient, bip44: DefaultBIP44Provider(), keychain: keychain)

let clientId = try! networkingClient.getClientId()
networkingLogger.debug("My client id is: \(clientId)")
return (pairingClient, networkingClient, syncClient, keychain, keyValueStorage)
return (pairingClient, networkingClient, keychain, keyValueStorage)
}

func makeWalletClients() {
let prefix = "🦋 Wallet: "
let (pairingClient, networkingInteractor, syncClient, keychain, keyValueStorage) = makeClientDependencies(prefix: prefix)
let (pairingClient, networkingInteractor, keychain, keyValueStorage) = makeClientDependencies(prefix: prefix)
let notifyLogger = ConsoleLogger(prefix: prefix + " [Notify]", loggingLevel: .debug)
walletPairingClient = pairingClient
let pushClient = PushClientFactory.create(projectId: "",
pushHost: "echo.walletconnect.com",
keychainStorage: keychain,
environment: .sandbox)
let keyserverURL = URL(string: "https://keys.walletconnect.com")!
let historyClient = HistoryClientFactory.create(
historyUrl: "https://history.walletconnect.com",
relayUrl: "wss://relay.walletconnect.com",
keychain: keychain
)
walletNotifyClient = NotifyClientFactory.create(keyserverURL: keyserverURL,
logger: notifyLogger,
keyValueStorage: keyValueStorage,
Expand All @@ -94,8 +83,6 @@ final class NotifyTests: XCTestCase {
networkInteractor: networkingInteractor,
pairingRegisterer: pairingClient,
pushClient: pushClient,
syncClient: syncClient,
historyClient: historyClient,
crypto: DefaultCryptoProvider())
}

Expand All @@ -106,15 +93,18 @@ final class NotifyTests: XCTestCase {
func testWalletCreatesSubscription() async {
let expectation = expectation(description: "expects to create notify subscription")
let metadata = AppMetadata(name: "GM Dapp", description: "", url: gmDappUrl, icons: [])

walletNotifyClient.newSubscriptionPublisher
.sink { [unowned self] subscription in
Task(priority: .high) {
try! await walletNotifyClient.deleteSubscription(topic: subscription.topic)
expectation.fulfill()
}
}.store(in: &publishers)

try! await walletNotifyClient.register(account: account, onSign: sign)
try! await walletNotifyClient.subscribe(metadata: metadata, account: account, onSign: sign)
walletNotifyClient.subscriptionsPublisher
.first()
.sink { [unowned self] subscriptions in
XCTAssertNotNil(subscriptions.first)
Task { try! await walletNotifyClient.deleteSubscription(topic: subscriptions.first!.topic) }
expectation.fulfill()
}.store(in: &publishers)

wait(for: [expectation], timeout: InputConfig.defaultTimeout)
}

Expand All @@ -124,55 +114,57 @@ final class NotifyTests: XCTestCase {
let updateScope: Set<String> = ["alerts"]
try! await walletNotifyClient.register(account: account, onSign: sign)
try! await walletNotifyClient.subscribe(metadata: metadata, account: account, onSign: sign)
walletNotifyClient.subscriptionsPublisher
.first()
.sink { [unowned self] subscriptions in
sleep(1)
Task { try! await walletNotifyClient.update(topic: subscriptions.first!.topic, scope: updateScope) }
walletNotifyClient.newSubscriptionPublisher
.sink { [unowned self] subscription in
Task(priority: .high) {
try! await walletNotifyClient.update(topic: subscription.topic, scope: updateScope)
}
}
.store(in: &publishers)

walletNotifyClient.updateSubscriptionPublisher
.sink { [unowned self] result in
guard case .success(let subscription) = result else { XCTFail(); return }
.sink { [unowned self] subscription in
let updatedScope = Set(subscription.scope.filter{ $0.value.enabled == true }.keys)
XCTAssertEqual(updatedScope, updateScope)
Task { try! await walletNotifyClient.deleteSubscription(topic: subscription.topic) }
expectation.fulfill()
Task(priority: .high) {
try! await walletNotifyClient.deleteSubscription(topic: subscription.topic)
expectation.fulfill()
}
}.store(in: &publishers)

wait(for: [expectation], timeout: InputConfig.defaultTimeout)
}

// func testNotifyServerSubscribeAndNotifies() async throws {
// let subscribeExpectation = expectation(description: "creates notify subscription")
// let messageExpectation = expectation(description: "receives a notify message")
// let notifyMessage = NotifyMessage.stub()
//
// let metadata = AppMetadata(name: "GM Dapp", description: "", url: gmDappUrl, icons: [])
// try! await walletNotifyClient.register(account: account, onSign: sign)
// try! await walletNotifyClient.subscribe(metadata: metadata, account: account, onSign: sign)
// var subscription: NotifySubscription!
// walletNotifyClient.subscriptionsPublisher
// .first()
// .sink { subscriptions in
// XCTAssertNotNil(subscriptions.first)
// subscribeExpectation.fulfill()
// subscription = subscriptions.first!
// let notifier = Publisher()
// sleep(5)
// Task(priority: .high) { try await notifier.notify(topic: subscriptions.first!.topic, account: subscriptions.first!.account, message: notifyMessage) }
// }.store(in: &publishers)
// walletNotifyClient.notifyMessagePublisher
// .sink { notifyMessageRecord in
// XCTAssertEqual(notifyMessage, notifyMessageRecord.message)
// messageExpectation.fulfill()
// }.store(in: &publishers)
//
// wait(for: [subscribeExpectation, messageExpectation], timeout: InputConfig.defaultTimeout)
// try await walletNotifyClient.deleteSubscription(topic: subscription.topic)
// }
func testNotifyServerSubscribeAndNotifies() async throws {
let subscribeExpectation = expectation(description: "creates notify subscription")
let messageExpectation = expectation(description: "receives a notify message")
let notifyMessage = NotifyMessage.stub()

let metadata = AppMetadata(name: "GM Dapp", description: "", url: gmDappUrl, icons: [])
try! await walletNotifyClient.register(account: account, onSign: sign)
try! await walletNotifyClient.subscribe(metadata: metadata, account: account, onSign: sign)

walletNotifyClient.newSubscriptionPublisher
.sink { subscription in
let notifier = Publisher()
Task(priority: .high) {
try await notifier.notify(topic: subscription.topic, account: subscription.account, message: notifyMessage)
subscribeExpectation.fulfill()
}
}.store(in: &publishers)

walletNotifyClient.notifyMessagePublisher
.sink { [unowned self] notifyMessageRecord in
XCTAssertEqual(notifyMessage, notifyMessageRecord.message)

Task(priority: .high) {
try await walletNotifyClient.deleteSubscription(topic: notifyMessageRecord.topic)
messageExpectation.fulfill()
}
}.store(in: &publishers)

wait(for: [subscribeExpectation, messageExpectation], timeout: InputConfig.defaultTimeout)
}
}


Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ let package = Package(
path: "Sources/Web3Wallet"),
.target(
name: "WalletConnectNotify",
dependencies: ["WalletConnectPairing", "WalletConnectPush", "WalletConnectIdentity", "WalletConnectSync", "WalletConnectHistory"],
dependencies: ["WalletConnectPairing", "WalletConnectPush", "WalletConnectIdentity", "WalletConnectSigner"],
path: "Sources/WalletConnectNotify"),
.target(
name: "WalletConnectPush",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ class DeleteNotifySubscriptionService {
guard let subscription = notifyStorage.getSubscription(topic: topic)
else { throw Errors.notifySubscriptionNotFound}

try await notifyStorage.deleteSubscription(topic: topic)
notifyStorage.deleteMessages(topic: topic)

let protocolMethod = NotifyDeleteProtocolMethod()
let dappPubKey = try await webDidResolver.resolvePublicKey(dappUrl: subscription.metadata.url)

Expand All @@ -52,7 +49,8 @@ class DeleteNotifySubscriptionService {
let request = RPCRequest(method: protocolMethod.method, params: wrapper)
try await networkingInteractor.request(request, topic: topic, protocolMethod: protocolMethod)

try await notifyStorage.deleteSubscription(topic: topic)
try notifyStorage.deleteSubscription(topic: topic)
notifyStorage.deleteMessages(topic: topic)

networkingInteractor.unsubscribe(topic: topic)

Expand Down
22 changes: 7 additions & 15 deletions Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class NotifyClient {
return notifyMessageSubscriber.notifyMessagePublisher
}

public var updateSubscriptionPublisher: AnyPublisher<Result<NotifySubscription, Error>, Never> {
return notifyUpdateResponseSubscriber.updateSubscriptionPublisher
public var updateSubscriptionPublisher: AnyPublisher<NotifySubscription, Never> {
return notifyStorage.updateSubscriptionPublisher
}

public var logsPublisher: AnyPublisher<Log, Never> {
Expand All @@ -41,8 +41,8 @@ public class NotifyClient {
public let logger: ConsoleLogging

private let pushClient: PushClient
private let identityClient: IdentityClient
private let notifyStorage: NotifyStorage
private let notifySyncService: NotifySyncService
private let notifyMessageSubscriber: NotifyMessageSubscriber
private let resubscribeService: NotifyResubscribeService
private let notifySubscribeResponseSubscriber: NotifySubscribeResponseSubscriber
Expand All @@ -53,10 +53,10 @@ public class NotifyClient {

init(logger: ConsoleLogging,
kms: KeyManagementServiceProtocol,
identityClient: IdentityClient,
pushClient: PushClient,
notifyMessageSubscriber: NotifyMessageSubscriber,
notifyStorage: NotifyStorage,
notifySyncService: NotifySyncService,
deleteNotifySubscriptionService: DeleteNotifySubscriptionService,
resubscribeService: NotifyResubscribeService,
notifySubscribeRequester: NotifySubscribeRequester,
Expand All @@ -68,9 +68,9 @@ public class NotifyClient {
) {
self.logger = logger
self.pushClient = pushClient
self.identityClient = identityClient
self.notifyMessageSubscriber = notifyMessageSubscriber
self.notifyStorage = notifyStorage
self.notifySyncService = notifySyncService
self.deleteNotifySubscriptionService = deleteNotifySubscriptionService
self.resubscribeService = resubscribeService
self.notifySubscribeRequester = notifySubscribeRequester
Expand All @@ -82,11 +82,7 @@ public class NotifyClient {
}

public func register(account: Account, onSign: @escaping SigningCallback) async throws {
try await notifySyncService.registerSyncIfNeeded(account: account, onSign: onSign)
try await notifySyncService.registerIdentity(account: account, onSign: onSign)
try await notifyStorage.initialize(account: account)
try await notifyStorage.subscribe(account: account)
try await notifySyncService.fetchHistoryIfNeeded(account: account)
_ = try await identityClient.register(account: account, onSign: onSign)
}

public func setLogging(level: LoggingLevel) {
Expand Down Expand Up @@ -122,11 +118,7 @@ public class NotifyClient {
}

public func isIdentityRegistered(account: Account) -> Bool {
return notifySyncService.isIdentityRegistered(account: account)
}

public func isSyncRegistered(account: Account) -> Bool {
return notifySyncService.isSyncRegistered(account: account)
return identityClient.isIdentityRegistered(account: account)
}

public func messagesPublisher(topic: String) -> AnyPublisher<[NotifyMessageRecord], Never> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation

public struct NotifyClientFactory {

public static func create(networkInteractor: NetworkInteracting, pairingRegisterer: PairingRegisterer, pushClient: PushClient, syncClient: SyncClient, historyClient: HistoryClient, crypto: CryptoProvider) -> NotifyClient {
public static func create(networkInteractor: NetworkInteracting, pairingRegisterer: PairingRegisterer, pushClient: PushClient, crypto: CryptoProvider) -> NotifyClient {
let logger = ConsoleLogger(prefix: "🔔",loggingLevel: .debug)
let keyValueStorage = UserDefaults.standard
let keyserverURL = URL(string: "https://keys.walletconnect.com")!
Expand All @@ -18,8 +18,6 @@ public struct NotifyClientFactory {
networkInteractor: networkInteractor,
pairingRegisterer: pairingRegisterer,
pushClient: pushClient,
syncClient: syncClient,
historyClient: historyClient,
crypto: crypto
)
}
Expand All @@ -33,18 +31,13 @@ public struct NotifyClientFactory {
networkInteractor: NetworkInteracting,
pairingRegisterer: PairingRegisterer,
pushClient: PushClient,
syncClient: SyncClient,
historyClient: HistoryClient,
crypto: CryptoProvider
) -> NotifyClient {
let kms = KeyManagementService(keychain: keychainStorage)
let subscriptionStore: SyncStore<NotifySubscription> = SyncStoreFactory.create(name: NotifyStorageIdntifiers.notifySubscription, syncClient: syncClient, storage: keyValueStorage)
let subscriptionStoreDelegate = NotifySubscriptionStoreDelegate(networkingInteractor: networkInteractor, kms: kms, groupKeychainStorage: groupKeychainStorage)
let subscriptionStore = KeyedDatabase<NotifySubscription>(storage: keyValueStorage, identifier: NotifyStorageIdntifiers.notifySubscription)
let messagesStore = KeyedDatabase<NotifyMessageRecord>(storage: keyValueStorage, identifier: NotifyStorageIdntifiers.notifyMessagesRecords)
let notifyStorage = NotifyStorage(subscriptionStore: subscriptionStore, messagesStore: messagesStore, subscriptionStoreDelegate: subscriptionStoreDelegate)
let coldStartStore = CodableStore<Date>(defaults: keyValueStorage, identifier: NotifyStorageIdntifiers.coldStartStore)
let notifyStorage = NotifyStorage(subscriptionStore: subscriptionStore, messagesStore: messagesStore)
let identityClient = IdentityClientFactory.create(keyserver: keyserverURL, keychain: keychainStorage, logger: logger)
let notifySyncService = NotifySyncService(syncClient: syncClient, logger: logger, historyClient: historyClient, identityClient: identityClient, subscriptionsStore: subscriptionStore, messagesStore: messagesStore, networkingInteractor: networkInteractor, kms: kms, coldStartStore: coldStartStore, groupKeychainStorage: groupKeychainStorage)
let notifyMessageSubscriber = NotifyMessageSubscriber(keyserver: keyserverURL, networkingInteractor: networkInteractor, identityClient: identityClient, notifyStorage: notifyStorage, crypto: crypto, logger: logger)
let webDidResolver = WebDidResolver()
let deleteNotifySubscriptionService = DeleteNotifySubscriptionService(keyserver: keyserverURL, networkingInteractor: networkInteractor, identityClient: identityClient, webDidResolver: webDidResolver, kms: kms, logger: logger, notifyStorage: notifyStorage)
Expand All @@ -68,10 +61,10 @@ public struct NotifyClientFactory {
return NotifyClient(
logger: logger,
kms: kms,
identityClient: identityClient,
pushClient: pushClient,
notifyMessageSubscriber: notifyMessageSubscriber,
notifyStorage: notifyStorage,
notifySyncService: notifySyncService,
deleteNotifySubscriptionService: deleteNotifySubscriptionService,
resubscribeService: resubscribeService,
notifySubscribeRequester: notifySubscribeRequester,
Expand Down
Loading

0 comments on commit 4c99850

Please sign in to comment.