From 07ada933fbbb6589a5181d802c453e2c66f347a3 Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Tue, 9 Jan 2024 16:40:20 +0300 Subject: [PATCH 01/21] History package cleanup --- Example/IntegrationTests/Chat/ChatTests.swift | 10 +-- .../History/HistoryTests.swift | 73 ------------------- Package.swift | 4 +- Sources/Chat/Chat.swift | 3 +- Sources/Chat/ChatClientFactory.swift | 11 +-- .../History/HistoryService.swift | 27 +------ Sources/WalletConnectHistory/HistoryAPI.swift | 56 -------------- .../WalletConnectHistory/HistoryClient.swift | 42 +---------- .../HistoryClientFactory.swift | 24 +----- .../WalletConnectHistory/HistoryImports.swift | 3 +- .../HistoryNetworkService.swift | 41 ----------- .../WalletConnectHistory/HistoryRecord.swift | 11 --- .../Types/GetMessagesPayload.swift | 19 ----- .../Types/GetMessagesResponse.swift | 28 ------- .../Types/RegisterPayload.swift | 11 --- 15 files changed, 14 insertions(+), 349 deletions(-) delete mode 100644 Sources/WalletConnectHistory/HistoryAPI.swift delete mode 100644 Sources/WalletConnectHistory/HistoryNetworkService.swift delete mode 100644 Sources/WalletConnectHistory/HistoryRecord.swift delete mode 100644 Sources/WalletConnectHistory/Types/GetMessagesPayload.swift delete mode 100644 Sources/WalletConnectHistory/Types/GetMessagesResponse.swift delete mode 100644 Sources/WalletConnectHistory/Types/RegisterPayload.swift diff --git a/Example/IntegrationTests/Chat/ChatTests.swift b/Example/IntegrationTests/Chat/ChatTests.swift index 04cdc7e68..0b4528fe4 100644 --- a/Example/IntegrationTests/Chat/ChatTests.swift +++ b/Example/IntegrationTests/Chat/ChatTests.swift @@ -71,18 +71,10 @@ final class ChatTests: XCTestCase { keychain: keychain ) - let historyClient = HistoryClientFactory.create( - historyUrl: "https://history.walletconnect.com", - relayUrl: "wss://relay.walletconnect.com", - keyValueStorage: keyValueStorage, - keychain: keychain, - logger: logger - ) - let clientId = try! networkingInteractor.getClientId() logger.debug("My client id is: \(clientId)") - return ChatClientFactory.create(keyserverURL: keyserverURL, relayClient: relayClient, networkingInteractor: networkingInteractor, keychain: keychain, logger: logger, storage: keyValueStorage, syncClient: syncClient, historyClient: historyClient) + return ChatClientFactory.create(keyserverURL: keyserverURL, relayClient: relayClient, networkingInteractor: networkingInteractor, keychain: keychain, logger: logger, storage: keyValueStorage, syncClient: syncClient) } func testInvite() async throws { diff --git a/Example/IntegrationTests/History/HistoryTests.swift b/Example/IntegrationTests/History/HistoryTests.swift index d96e0f811..4ebbdd80d 100644 --- a/Example/IntegrationTests/History/HistoryTests.swift +++ b/Example/IntegrationTests/History/HistoryTests.swift @@ -5,77 +5,4 @@ import XCTest final class HistoryTests: XCTestCase { - var publishers = Set() - - let relayUrl = "wss://relay.walletconnect.com" - let historyUrl = "https://history.walletconnect.com" - - var relayClient1: RelayClient! - var relayClient2: RelayClient! - - var historyClient: HistoryNetworkService! - - override func setUp() { - let keychain1 = KeychainStorageMock() - let keychain2 = KeychainStorageMock() - let logger1 = ConsoleLoggerMock() - let defaults1 = RuntimeKeyValueStorage() - relayClient1 = makeRelayClient(prefix: "🐄", keychain: keychain1) - relayClient2 = makeRelayClient(prefix: "🐫", keychain: keychain2) - historyClient = makeHistoryClient(defaults: defaults1, keychain: keychain1, logger: logger1) - } - - private func makeRelayClient(prefix: String, keychain: KeychainStorageProtocol) -> RelayClient { - return RelayClientFactory.create( - relayHost: InputConfig.relayHost, - projectId: InputConfig.projectId, - keyValueStorage: RuntimeKeyValueStorage(), - keychainStorage: keychain, - socketFactory: DefaultSocketFactory(), - logger: ConsoleLogger(prefix: prefix + " [Relay]", loggingLevel: .debug)) - } - - private func makeHistoryClient(defaults: KeyValueStorage, keychain: KeychainStorageProtocol, logger: ConsoleLogging) -> HistoryNetworkService { - let clientIdStorage = ClientIdStorage(defaults: defaults, keychain: keychain, logger: logger) - return HistoryNetworkService(clientIdStorage: clientIdStorage) - } - - func testRegister() async throws { - let payload = RegisterPayload(tags: ["7000"], relayUrl: relayUrl) - - try await historyClient.registerTags(payload: payload, historyUrl: historyUrl) - } - - func testGetMessages() async throws { - let exp = expectation(description: "Test Get Messages") - let tag = 7000 - let payload = "{}" - let agreement = AgreementPrivateKey() - let topic = agreement.publicKey.rawRepresentation.sha256().hex - - relayClient2.messagePublisher.sink { (topic, message, publishedAt) in - exp.fulfill() - }.store(in: &publishers) - - try await historyClient.registerTags( - payload: RegisterPayload(tags: [String(tag)], relayUrl: relayUrl), - historyUrl: historyUrl) - - try await relayClient2.subscribe(topic: topic) - try await relayClient1.publish(topic: topic, payload: payload, tag: tag, prompt: false, ttl: 3000) - - wait(for: [exp], timeout: InputConfig.defaultTimeout) - - sleep(5) // History server has a queue - - let messages = try await historyClient.getMessages( - payload: GetMessagesPayload( - topic: topic, - originId: nil, - messageCount: 200, - direction: .forward), - historyUrl: historyUrl) - - XCTAssertEqual(messages.messages, [payload]) - } } diff --git a/Package.swift b/Package.swift index dcfa42b87..c53f5ee38 100644 --- a/Package.swift +++ b/Package.swift @@ -62,7 +62,7 @@ let package = Package( path: "Sources/WalletConnectSign"), .target( name: "WalletConnectChat", - dependencies: ["WalletConnectIdentity", "WalletConnectSync", "WalletConnectHistory"], + dependencies: ["WalletConnectIdentity", "WalletConnectSync"], path: "Sources/Chat"), .target( name: "Auth", @@ -94,7 +94,7 @@ let package = Package( dependencies: ["WalletConnectNetworking"]), .target( name: "WalletConnectHistory", - dependencies: ["HTTPClient", "WalletConnectRelay"]), + dependencies: []), .target( name: "WalletConnectSigner", dependencies: ["WalletConnectNetworking"]), diff --git a/Sources/Chat/Chat.swift b/Sources/Chat/Chat.swift index 2bb3ac56b..003b64335 100644 --- a/Sources/Chat/Chat.swift +++ b/Sources/Chat/Chat.swift @@ -12,8 +12,7 @@ public class Chat { keyserverUrl: keyserverUrl, relayClient: Relay.instance, networkingInteractor: Networking.interactor, - syncClient: Sync.instance, - historyClient: History.instance + syncClient: Sync.instance ) }() diff --git a/Sources/Chat/ChatClientFactory.swift b/Sources/Chat/ChatClientFactory.swift index ebce9f3c3..52bd97024 100644 --- a/Sources/Chat/ChatClientFactory.swift +++ b/Sources/Chat/ChatClientFactory.swift @@ -2,7 +2,7 @@ import Foundation public struct ChatClientFactory { - static func create(keyserverUrl: String, relayClient: RelayClient, networkingInteractor: NetworkingInteractor, syncClient: SyncClient, historyClient: HistoryClient) -> ChatClient { + static func create(keyserverUrl: String, relayClient: RelayClient, networkingInteractor: NetworkingInteractor, syncClient: SyncClient) -> ChatClient { fatalError("fix access group") let keychain = KeychainStorage(serviceIdentifier: "com.walletconnect.sdk", accessGroup: "") let keyserverURL = URL(string: keyserverUrl)! @@ -13,8 +13,7 @@ public struct ChatClientFactory { keychain: keychain, logger: ConsoleLogger(loggingLevel: .debug), storage: UserDefaults.standard, - syncClient: syncClient, - historyClient: historyClient + syncClient: syncClient ) } @@ -25,12 +24,10 @@ public struct ChatClientFactory { keychain: KeychainStorageProtocol, logger: ConsoleLogging, storage: KeyValueStorage, - syncClient: SyncClient, - historyClient: HistoryClient + syncClient: SyncClient ) -> ChatClient { let kms = KeyManagementService(keychain: keychain) - let serializer = Serializer(kms: kms, logger: logger) - let historyService = HistoryService(historyClient: historyClient, seiralizer: serializer) + let historyService = HistoryService() let messageStore = KeyedDatabase(storage: storage, identifier: ChatStorageIdentifiers.messages.rawValue) let receivedInviteStore = KeyedDatabase(storage: storage, identifier: ChatStorageIdentifiers.receivedInvites.rawValue) let threadStore: SyncStore = SyncStoreFactory.create(name: ChatStorageIdentifiers.thread.rawValue, syncClient: syncClient, storage: storage) diff --git a/Sources/Chat/ProtocolServices/History/HistoryService.swift b/Sources/Chat/ProtocolServices/History/HistoryService.swift index ebad3a50d..13f014333 100644 --- a/Sources/Chat/ProtocolServices/History/HistoryService.swift +++ b/Sources/Chat/ProtocolServices/History/HistoryService.swift @@ -2,36 +2,15 @@ import Foundation final class HistoryService { - private let historyClient: HistoryClient - private let seiralizer: Serializing + init() { - init(historyClient: HistoryClient, seiralizer: Serializing) { - self.historyClient = historyClient - self.seiralizer = seiralizer } func register() async throws { - try await historyClient.register(tags: ["2002"]) + fatalError() } func fetchMessageHistory(thread: Thread) async throws -> [Message] { - let wrappers: [MessagePayload.Wrapper] = try await historyClient.getMessages( - topic: thread.topic, - count: 200, direction: .backward - ) - - return wrappers.map { wrapper in - let (messagePayload, messageClaims) = try! MessagePayload.decodeAndVerify(from: wrapper) - - let authorAccount = messagePayload.recipientAccount == thread.selfAccount - ? thread.peerAccount - : thread.selfAccount - - return Message( - topic: thread.topic, - message: messagePayload.message, - authorAccount: authorAccount, - timestamp: messageClaims.iat) - } + fatalError() } } diff --git a/Sources/WalletConnectHistory/HistoryAPI.swift b/Sources/WalletConnectHistory/HistoryAPI.swift deleted file mode 100644 index 58f6b0cbd..000000000 --- a/Sources/WalletConnectHistory/HistoryAPI.swift +++ /dev/null @@ -1,56 +0,0 @@ -import Foundation - -enum HistoryAPI: HTTPService { - case register(payload: RegisterPayload, jwt: String) - case messages(payload: GetMessagesPayload) - - var path: String { - switch self { - case .register: - return "/register" - case .messages: - return "/messages" - } - } - - var method: HTTPMethod { - switch self { - case .register: - return .post - case .messages: - return .get - } - } - - var body: Data? { - switch self { - case .register(let payload, _): - return try? JSONEncoder().encode(payload) - case .messages: - return nil - } - } - - var additionalHeaderFields: [String : String]? { - switch self { - case .register(_, let jwt): - return ["Authorization": "Bearer \(jwt)"] - case .messages: - return nil - } - } - - var queryParameters: [String: String]? { - switch self { - case .messages(let payload): - return [ - "topic": payload.topic, - "originId": payload.originId.map { String($0) }, - "messageCount": payload.messageCount.map { String($0) }, - "direction": payload.direction.rawValue - ].compactMapValues { $0 } - case .register: - return nil - } - } -} diff --git a/Sources/WalletConnectHistory/HistoryClient.swift b/Sources/WalletConnectHistory/HistoryClient.swift index 3022a05aa..65f50acab 100644 --- a/Sources/WalletConnectHistory/HistoryClient.swift +++ b/Sources/WalletConnectHistory/HistoryClient.swift @@ -2,47 +2,7 @@ import Foundation public final class HistoryClient { - private let historyUrl: String - private let relayUrl: String - private let serializer: Serializer - private let historyNetworkService: HistoryNetworkService + init() { - init(historyUrl: String, relayUrl: String, serializer: Serializer, historyNetworkService: HistoryNetworkService) { - self.historyUrl = historyUrl - self.relayUrl = relayUrl - self.serializer = serializer - self.historyNetworkService = historyNetworkService - } - - public func register(tags: [String]) async throws { - let payload = RegisterPayload(tags: tags, relayUrl: relayUrl) - try await historyNetworkService.registerTags(payload: payload, historyUrl: historyUrl) - } - - public func getMessages(topic: String, count: Int, direction: GetMessagesPayload.Direction) async throws -> [T] { - return try await getRecords(topic: topic, count: count, direction: direction).map { $0.object } - } - - public func getRecords(topic: String, count: Int, direction: GetMessagesPayload.Direction) async throws -> [HistoryRecord] { - let payload = GetMessagesPayload(topic: topic, originId: nil, messageCount: count, direction: direction) - let response = try await historyNetworkService.getMessages(payload: payload, historyUrl: historyUrl) - - return response.messages.compactMap { payload in - do { - let (request, _, _): (RPCRequest, _, _) = try serializer.deserialize( - topic: topic, - encodedEnvelope: payload - ) - - guard - let id = request.id, - let object = try request.params?.get(T.self) - else { return nil } - - return HistoryRecord(id: id, object: object) - } catch { - fatalError(error.localizedDescription) - } - } } } diff --git a/Sources/WalletConnectHistory/HistoryClientFactory.swift b/Sources/WalletConnectHistory/HistoryClientFactory.swift index 26d0d2044..06784b9db 100644 --- a/Sources/WalletConnectHistory/HistoryClientFactory.swift +++ b/Sources/WalletConnectHistory/HistoryClientFactory.swift @@ -3,28 +3,6 @@ import Foundation class HistoryClientFactory { static func create() -> HistoryClient { - let keychain = KeychainStorage(serviceIdentifier: "com.walletconnect.sdk", accessGroup: "") - let keyValueStorage = UserDefaults.standard - let logger = ConsoleLogger() - return HistoryClientFactory.create( - historyUrl: "https://history.walletconnect.com", - relayUrl: "wss://relay.walletconnect.com", - keyValueStorage: keyValueStorage, - keychain: keychain, - logger: logger - ) - } - - static func create(historyUrl: String, relayUrl: String, keyValueStorage: KeyValueStorage, keychain: KeychainStorageProtocol, logger: ConsoleLogging) -> HistoryClient { - let clientIdStorage = ClientIdStorage(defaults: keyValueStorage, keychain: keychain, logger: logger) - let kms = KeyManagementService(keychain: keychain) - let serializer = Serializer(kms: kms, logger: ConsoleLogger(prefix: "🔐", loggingLevel: .off)) - let historyNetworkService = HistoryNetworkService(clientIdStorage: clientIdStorage) - return HistoryClient( - historyUrl: historyUrl, - relayUrl: relayUrl, - serializer: serializer, - historyNetworkService: historyNetworkService - ) + return HistoryClient() } } diff --git a/Sources/WalletConnectHistory/HistoryImports.swift b/Sources/WalletConnectHistory/HistoryImports.swift index e6d4b859c..2db10894a 100644 --- a/Sources/WalletConnectHistory/HistoryImports.swift +++ b/Sources/WalletConnectHistory/HistoryImports.swift @@ -1,4 +1,3 @@ #if !CocoaPods -@_exported import HTTPClient -@_exported import WalletConnectRelay + #endif diff --git a/Sources/WalletConnectHistory/HistoryNetworkService.swift b/Sources/WalletConnectHistory/HistoryNetworkService.swift deleted file mode 100644 index 906959577..000000000 --- a/Sources/WalletConnectHistory/HistoryNetworkService.swift +++ /dev/null @@ -1,41 +0,0 @@ -import Foundation - -final class HistoryNetworkService { - - private let clientIdStorage: ClientIdStorage - - init(clientIdStorage: ClientIdStorage) { - self.clientIdStorage = clientIdStorage - } - - func registerTags(payload: RegisterPayload, historyUrl: String) async throws { - let service = HTTPNetworkClient(host: try host(from: historyUrl)) - let api = HistoryAPI.register(payload: payload, jwt: try getJwt(historyUrl: historyUrl)) - try await service.request(service: api) - } - - func getMessages(payload: GetMessagesPayload, historyUrl: String) async throws -> GetMessagesResponse { - let service = HTTPNetworkClient(host: try host(from: historyUrl)) - let api = HistoryAPI.messages(payload: payload) - return try await service.request(GetMessagesResponse.self, at: api) - } -} - -private extension HistoryNetworkService { - - enum Errors: Error { - case couldNotResolveHost - } - - func getJwt(historyUrl: String) throws -> String { - let authenticator = ClientIdAuthenticator(clientIdStorage: clientIdStorage) - return try authenticator.createAuthToken(url: historyUrl) - } - - func host(from url: String) throws -> String { - guard let host = URL(string: url)?.host else { - throw Errors.couldNotResolveHost - } - return host - } -} diff --git a/Sources/WalletConnectHistory/HistoryRecord.swift b/Sources/WalletConnectHistory/HistoryRecord.swift deleted file mode 100644 index cd2793081..000000000 --- a/Sources/WalletConnectHistory/HistoryRecord.swift +++ /dev/null @@ -1,11 +0,0 @@ -import Foundation - -public struct HistoryRecord { - public let id: RPCID - public let object: Object - - public init(id: RPCID, object: Object) { - self.id = id - self.object = object - } -} diff --git a/Sources/WalletConnectHistory/Types/GetMessagesPayload.swift b/Sources/WalletConnectHistory/Types/GetMessagesPayload.swift deleted file mode 100644 index 7dcc9a08d..000000000 --- a/Sources/WalletConnectHistory/Types/GetMessagesPayload.swift +++ /dev/null @@ -1,19 +0,0 @@ -import Foundation - -public struct GetMessagesPayload: Codable { - public enum Direction: String, Codable { - case forward - case backward - } - public let topic: String - public let originId: Int64? - public let messageCount: Int? - public let direction: Direction - - public init(topic: String, originId: Int64?, messageCount: Int?, direction: GetMessagesPayload.Direction) { - self.topic = topic - self.originId = originId - self.messageCount = messageCount - self.direction = direction - } -} diff --git a/Sources/WalletConnectHistory/Types/GetMessagesResponse.swift b/Sources/WalletConnectHistory/Types/GetMessagesResponse.swift deleted file mode 100644 index 032bad07e..000000000 --- a/Sources/WalletConnectHistory/Types/GetMessagesResponse.swift +++ /dev/null @@ -1,28 +0,0 @@ -import Foundation - -public struct GetMessagesResponse: Decodable { - public struct Message: Codable { - public let message: String - } - public let topic: String - public let direction: GetMessagesPayload.Direction - public let nextId: Int64? - public let messages: [String] - - enum CodingKeys: String, CodingKey { - case topic - case direction - case nextId - case messages - } - - public init(from decoder: Decoder) throws { - let container = try decoder.container(keyedBy: CodingKeys.self) - self.topic = try container.decode(String.self, forKey: .topic) - self.direction = try container.decode(GetMessagesPayload.Direction.self, forKey: .direction) - self.nextId = try container.decodeIfPresent(Int64.self, forKey: .nextId) - - let messages = try container.decode([Message].self, forKey: .messages) - self.messages = messages.map { $0.message } - } -} diff --git a/Sources/WalletConnectHistory/Types/RegisterPayload.swift b/Sources/WalletConnectHistory/Types/RegisterPayload.swift deleted file mode 100644 index b759c5ce5..000000000 --- a/Sources/WalletConnectHistory/Types/RegisterPayload.swift +++ /dev/null @@ -1,11 +0,0 @@ -import Foundation - -public struct RegisterPayload: Codable { - public let tags: [String] - public let relayUrl: String - - public init(tags: [String], relayUrl: String) { - self.tags = tags - self.relayUrl = relayUrl - } -} From b29a0eab3f32d979139208e8407cd859522fe3be Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Fri, 12 Jan 2024 15:03:44 +0300 Subject: [PATCH 02/21] awaitResponse --- .../NetworkInteracting.swift | 10 ++++++ .../NetworkingInteractor.swift | 31 ++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Sources/WalletConnectNetworking/NetworkInteracting.swift b/Sources/WalletConnectNetworking/NetworkInteracting.swift index 6bad90646..75b7283b6 100644 --- a/Sources/WalletConnectNetworking/NetworkInteracting.swift +++ b/Sources/WalletConnectNetworking/NetworkInteracting.swift @@ -42,6 +42,16 @@ public protocol NetworkInteracting { subscription: @escaping (ResponseSubscriptionPayload) async throws -> Void ) + func awaitResponse( + request: RPCRequest, + topic: String, + requestOfType: Request, + requestMethod: ProtocolMethod, + responseOfType: Response, + responseMethod: ProtocolMethod, + envelopeType: Envelope.EnvelopeType + ) async throws -> Response + func getClientId() throws -> String } diff --git a/Sources/WalletConnectNetworking/NetworkingInteractor.swift b/Sources/WalletConnectNetworking/NetworkingInteractor.swift index b428a77f5..a4d36c0b5 100644 --- a/Sources/WalletConnectNetworking/NetworkingInteractor.swift +++ b/Sources/WalletConnectNetworking/NetworkingInteractor.swift @@ -29,7 +29,7 @@ public class NetworkingInteractor: NetworkInteracting { public var networkConnectionStatusPublisher: AnyPublisher public var socketConnectionStatusPublisher: AnyPublisher - + private let networkMonitor: NetworkMonitoring public init( @@ -139,6 +139,35 @@ public class NetworkingInteractor: NetworkInteracting { .eraseToAnyPublisher() } + public func awaitResponse( + request: RPCRequest, + topic: String, + requestOfType: Request, + requestMethod: ProtocolMethod, + responseOfType: Response, + responseMethod: ProtocolMethod, + envelopeType: Envelope.EnvelopeType + ) async throws -> Response { + + try await self.request(request, topic: topic, protocolMethod: requestMethod, envelopeType: envelopeType) + + return try await withCheckedThrowingContinuation { [unowned self] continuation in + var response, error: AnyCancellable? + + response = responseSubscription(on: responseMethod) + .sink { (payload: ResponseSubscriptionPayload) in + response?.cancel() + continuation.resume(with: .success(payload.response)) + } + + error = responseErrorSubscription(on: responseMethod) + .sink { (payload: ResponseSubscriptionErrorPayload) in + error?.cancel() + continuation.resume(throwing: payload.error) + } + } + } + public func responseSubscription(on request: ProtocolMethod) -> AnyPublisher, Never> { return responsePublisher .filter { rpcRequest in From c067781cda5aed5b04af3c5c5009bbe0dde465fd Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Fri, 12 Jan 2024 15:08:53 +0300 Subject: [PATCH 03/21] requestMethod == responseMethod --- Sources/WalletConnectHistory/HistoryClient.swift | 2 +- .../WalletConnectNetworking/NetworkingInteractor.swift | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Sources/WalletConnectHistory/HistoryClient.swift b/Sources/WalletConnectHistory/HistoryClient.swift index 65f50acab..6ddc09ee1 100644 --- a/Sources/WalletConnectHistory/HistoryClient.swift +++ b/Sources/WalletConnectHistory/HistoryClient.swift @@ -3,6 +3,6 @@ import Foundation public final class HistoryClient { init() { - + } } diff --git a/Sources/WalletConnectNetworking/NetworkingInteractor.swift b/Sources/WalletConnectNetworking/NetworkingInteractor.swift index a4d36c0b5..f4af51a6c 100644 --- a/Sources/WalletConnectNetworking/NetworkingInteractor.swift +++ b/Sources/WalletConnectNetworking/NetworkingInteractor.swift @@ -142,25 +142,24 @@ public class NetworkingInteractor: NetworkInteracting { public func awaitResponse( request: RPCRequest, topic: String, + method: ProtocolMethod, requestOfType: Request, - requestMethod: ProtocolMethod, responseOfType: Response, - responseMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType ) async throws -> Response { - try await self.request(request, topic: topic, protocolMethod: requestMethod, envelopeType: envelopeType) + try await self.request(request, topic: topic, protocolMethod: method, envelopeType: envelopeType) return try await withCheckedThrowingContinuation { [unowned self] continuation in var response, error: AnyCancellable? - response = responseSubscription(on: responseMethod) + response = responseSubscription(on: method) .sink { (payload: ResponseSubscriptionPayload) in response?.cancel() continuation.resume(with: .success(payload.response)) } - error = responseErrorSubscription(on: responseMethod) + error = responseErrorSubscription(on: method) .sink { (payload: ResponseSubscriptionErrorPayload) in error?.cancel() continuation.resume(throwing: payload.error) From 57fdaf2cfe61820d26f63dcf3c3d6518b8699b4e Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Fri, 12 Jan 2024 15:25:21 +0300 Subject: [PATCH 04/21] Imports + NotifyGetNotificationsRequestPayload mask --- Package.swift | 2 +- .../WalletConnectHistory/HistoryImports.swift | 2 +- ...NotifyGetNotificationsRequestPayload.swift | 38 +++++++++++++++++++ .../NetworkInteracting.swift | 3 +- .../NetworkingInteractor.swift | 2 + 5 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift diff --git a/Package.swift b/Package.swift index c53f5ee38..01bd9ebcc 100644 --- a/Package.swift +++ b/Package.swift @@ -94,7 +94,7 @@ let package = Package( dependencies: ["WalletConnectNetworking"]), .target( name: "WalletConnectHistory", - dependencies: []), + dependencies: ["WalletConnectIdentity"]), .target( name: "WalletConnectSigner", dependencies: ["WalletConnectNetworking"]), diff --git a/Sources/WalletConnectHistory/HistoryImports.swift b/Sources/WalletConnectHistory/HistoryImports.swift index 2db10894a..51e07cac3 100644 --- a/Sources/WalletConnectHistory/HistoryImports.swift +++ b/Sources/WalletConnectHistory/HistoryImports.swift @@ -1,3 +1,3 @@ #if !CocoaPods - +@_exported import WalletConnectIdentity #endif diff --git a/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift b/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift new file mode 100644 index 000000000..2e339da97 --- /dev/null +++ b/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift @@ -0,0 +1,38 @@ +import Foundation + +struct NotifyGetNotificationsRequestPayload: JWTClaimsCodable { + + struct Claims: JWTClaims { + var iss: String + + var iat: UInt64 + + var exp: UInt64 + + var act: String? + + static var action: String? { + return "notify_get_notifications" + } + } + + struct Wrapper: JWTWrapper { + let auth: String + + init(jwtString: String) { + self.auth = jwtString + } + + var jwtString: String { + return auth + } + } + + init(claims: Claims) throws { + fatalError() + } + + func encode(iss: String) throws -> Claims { + fatalError() + } +} diff --git a/Sources/WalletConnectNetworking/NetworkInteracting.swift b/Sources/WalletConnectNetworking/NetworkInteracting.swift index 75b7283b6..068ccb31b 100644 --- a/Sources/WalletConnectNetworking/NetworkInteracting.swift +++ b/Sources/WalletConnectNetworking/NetworkInteracting.swift @@ -45,10 +45,9 @@ public protocol NetworkInteracting { func awaitResponse( request: RPCRequest, topic: String, + method: ProtocolMethod, requestOfType: Request, - requestMethod: ProtocolMethod, responseOfType: Response, - responseMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType ) async throws -> Response diff --git a/Sources/WalletConnectNetworking/NetworkingInteractor.swift b/Sources/WalletConnectNetworking/NetworkingInteractor.swift index f4af51a6c..a3c360beb 100644 --- a/Sources/WalletConnectNetworking/NetworkingInteractor.swift +++ b/Sources/WalletConnectNetworking/NetworkingInteractor.swift @@ -156,11 +156,13 @@ public class NetworkingInteractor: NetworkInteracting { response = responseSubscription(on: method) .sink { (payload: ResponseSubscriptionPayload) in response?.cancel() + error?.cancel() continuation.resume(with: .success(payload.response)) } error = responseErrorSubscription(on: method) .sink { (payload: ResponseSubscriptionErrorPayload) in + response?.cancel() error?.cancel() continuation.resume(throwing: payload.error) } From 859a85ad591a71f0e5edac8793d1c3cc14614c24 Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Fri, 12 Jan 2024 16:23:34 +0300 Subject: [PATCH 05/21] NotifyGetNotificationsRequestPayload --- ...NotifyGetNotificationsRequestPayload.swift | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift b/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift index 2e339da97..a6444de52 100644 --- a/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift +++ b/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift @@ -3,13 +3,15 @@ import Foundation struct NotifyGetNotificationsRequestPayload: JWTClaimsCodable { struct Claims: JWTClaims { - var iss: String - - var iat: UInt64 - - var exp: UInt64 - - var act: String? + let iat: UInt64 + let exp: UInt64 + let act: String? // - `notify_get_notifications` + let iss: String // - did:key of client identity key + let ksu: String // - key server for identity key verification + let aud: String // - did:key of dapp authentication key + let app: String // - did:web of app domain that this request is associated with - Example: `did:web:app.example.com` + let lmt: UInt64 // - the max number of notifications to return. Maximum value is 50. + let aft: String? // - the notification ID to start returning messages after. Null to start with the most recent notification static var action: String? { return "notify_get_notifications" @@ -28,11 +30,28 @@ struct NotifyGetNotificationsRequestPayload: JWTClaimsCodable { } } + let identityKey: DIDKey + let keyserver: String + let dappAuthKey: DIDKey + let app: DIDWeb + let limit: UInt64 + let after: String? + init(claims: Claims) throws { fatalError() } func encode(iss: String) throws -> Claims { - fatalError() + return Claims( + iat: defaultIat(), + exp: expiry(days: 1), + act: Claims.action, + iss: identityKey.did(variant: .ED25519), + ksu: keyserver, + aud: dappAuthKey.did(variant: .ED25519), + app: app.did, + lmt: limit, + aft: after + ) } } From eb632db6f4d88c38cbb03cb8c3aac04d38ad20ad Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Fri, 12 Jan 2024 16:52:35 +0300 Subject: [PATCH 06/21] NotifyGetNotificationsResponsePayload --- Package.swift | 2 +- .../WalletConnectHistory/HistoryClient.swift | 1 + ...otifyGetNotificationsResponsePayload.swift | 39 +++++++++++++++++++ .../Types}/NotifyMessage.swift | 0 .../WalletConnectNotify/NotifyImports.swift | 2 +- 5 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsResponsePayload.swift rename Sources/{WalletConnectNotify/Types/DataStructures => WalletConnectHistory/Types}/NotifyMessage.swift (100%) diff --git a/Package.swift b/Package.swift index 01bd9ebcc..617226143 100644 --- a/Package.swift +++ b/Package.swift @@ -74,7 +74,7 @@ let package = Package( path: "Sources/Web3Wallet"), .target( name: "WalletConnectNotify", - dependencies: ["WalletConnectPairing", "WalletConnectPush", "WalletConnectIdentity", "WalletConnectSigner", "Database"], + dependencies: ["WalletConnectPairing", "WalletConnectPush", "WalletConnectHistory", "WalletConnectSigner", "Database"], path: "Sources/WalletConnectNotify"), .target( name: "WalletConnectPush", diff --git a/Sources/WalletConnectHistory/HistoryClient.swift b/Sources/WalletConnectHistory/HistoryClient.swift index 6ddc09ee1..3456f7fc0 100644 --- a/Sources/WalletConnectHistory/HistoryClient.swift +++ b/Sources/WalletConnectHistory/HistoryClient.swift @@ -5,4 +5,5 @@ public final class HistoryClient { init() { } + } diff --git a/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsResponsePayload.swift b/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsResponsePayload.swift new file mode 100644 index 000000000..cf8cb2243 --- /dev/null +++ b/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsResponsePayload.swift @@ -0,0 +1,39 @@ +import Foundation + +struct NotifyGetNotificationsResponsePayload: JWTClaimsCodable { + + struct Claims: JWTClaims { + let iat: UInt64 + let exp: UInt64 + let act: String? // - `notify_get_notifications_response` + let iss: String // - did:key of client identity key + let aud: String // - did:key of Notify Server authentication key + let nfs: [NotifyMessage] // array of [Notify Notifications](./data-structures.md#notify-notification) + + static var action: String? { + return "notify_get_notifications_response" + } + } + + struct Wrapper: JWTWrapper { + let auth: String + + init(jwtString: String) { + self.auth = jwtString + } + + var jwtString: String { + return auth + } + } + + let messages: [NotifyMessage] + + init(claims: Claims) throws { + self.messages = claims.nfs + } + + func encode(iss: String) throws -> Claims { + fatalError() + } +} diff --git a/Sources/WalletConnectNotify/Types/DataStructures/NotifyMessage.swift b/Sources/WalletConnectHistory/Types/NotifyMessage.swift similarity index 100% rename from Sources/WalletConnectNotify/Types/DataStructures/NotifyMessage.swift rename to Sources/WalletConnectHistory/Types/NotifyMessage.swift diff --git a/Sources/WalletConnectNotify/NotifyImports.swift b/Sources/WalletConnectNotify/NotifyImports.swift index 43b543236..e2f267c31 100644 --- a/Sources/WalletConnectNotify/NotifyImports.swift +++ b/Sources/WalletConnectNotify/NotifyImports.swift @@ -1,7 +1,7 @@ #if !CocoaPods @_exported import WalletConnectPairing @_exported import WalletConnectPush -@_exported import WalletConnectIdentity +@_exported import WalletConnectHistory @_exported import WalletConnectSigner @_exported import Database #endif From e4b37995ee74d5c7ef53c424fbc0d8fea3e01655 Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Fri, 12 Jan 2024 17:24:50 +0300 Subject: [PATCH 07/21] HistoryClient package --- Sources/WalletConnectHistory/History.swift | 12 ------ .../WalletConnectHistory/HistoryClient.swift | 42 ++++++++++++++++++- .../HistoryClientFactory.swift | 6 +-- ...NotifyGetNotificationsRequestPayload.swift | 9 ++++ ...NotifyGetNotificationsProtocolMethod.swift | 9 ++++ .../NetworkInteracting.swift | 4 +- .../NetworkingInteractor.swift | 4 +- 7 files changed, 65 insertions(+), 21 deletions(-) delete mode 100644 Sources/WalletConnectHistory/History.swift create mode 100644 Sources/WalletConnectHistory/ProtocolMethods/NotifyGetNotificationsProtocolMethod.swift diff --git a/Sources/WalletConnectHistory/History.swift b/Sources/WalletConnectHistory/History.swift deleted file mode 100644 index d0954f756..000000000 --- a/Sources/WalletConnectHistory/History.swift +++ /dev/null @@ -1,12 +0,0 @@ -import Foundation - -/// History instatnce wrapper -public class History { - - /// Sync client instance - public static var instance: HistoryClient = { - return HistoryClientFactory.create() - }() - - private init() { } -} diff --git a/Sources/WalletConnectHistory/HistoryClient.swift b/Sources/WalletConnectHistory/HistoryClient.swift index 3456f7fc0..4aef22b0c 100644 --- a/Sources/WalletConnectHistory/HistoryClient.swift +++ b/Sources/WalletConnectHistory/HistoryClient.swift @@ -2,8 +2,46 @@ import Foundation public final class HistoryClient { - init() { - + private let keyserver: URL + private let networkingClient: NetworkInteracting + private let identityClient: IdentityClient + + init(keyserver: URL, networkingClient: NetworkInteracting, identityClient: IdentityClient) { + self.keyserver = keyserver + self.networkingClient = networkingClient + self.identityClient = identityClient } + public func fetchHistory(account: Account, topic: String, appAuthenticationKey: String, host: String) async throws -> [NotifyMessage] { + let identityKey = try identityClient.getInviteKey(for: account) + let dappAuthKey = try DIDKey(did: appAuthenticationKey) + let app = DIDWeb(host: host) + + let requestPayload = NotifyGetNotificationsRequestPayload( + identityKey: DIDKey(rawData: identityKey.rawRepresentation), + keyserver: keyserver.absoluteString, + dappAuthKey: dappAuthKey, + app: app, + limit: 50, + after: nil + ) + + let wrapper = try identityClient.signAndCreateWrapper(payload: requestPayload, account: account) + + let protocolMethod = NotifyGetNotificationsProtocolMethod() + let request = RPCRequest(method: protocolMethod.method, params: wrapper) + + let response = try await networkingClient.awaitResponse( + request: request, + topic: topic, + method: protocolMethod, + requestOfType: NotifyGetNotificationsRequestPayload.Wrapper.self, + responseOfType: NotifyGetNotificationsResponsePayload.Wrapper.self, + envelopeType: .type0 + ) + + let (responsePayload, _) = try NotifyGetNotificationsResponsePayload.decodeAndVerify(from: response) + + return responsePayload.messages + } } diff --git a/Sources/WalletConnectHistory/HistoryClientFactory.swift b/Sources/WalletConnectHistory/HistoryClientFactory.swift index 06784b9db..9db17a320 100644 --- a/Sources/WalletConnectHistory/HistoryClientFactory.swift +++ b/Sources/WalletConnectHistory/HistoryClientFactory.swift @@ -1,8 +1,8 @@ import Foundation -class HistoryClientFactory { +public class HistoryClientFactory { - static func create() -> HistoryClient { - return HistoryClient() + public static func create(keyserver: URL, networkingInteractor: NetworkInteracting, identityClient: IdentityClient) -> HistoryClient { + return HistoryClient(keyserver: keyserver, networkingClient: networkingInteractor, identityClient: identityClient) } } diff --git a/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift b/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift index a6444de52..a479f3af0 100644 --- a/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift +++ b/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift @@ -37,6 +37,15 @@ struct NotifyGetNotificationsRequestPayload: JWTClaimsCodable { let limit: UInt64 let after: String? + init(identityKey: DIDKey, keyserver: String, dappAuthKey: DIDKey, app: DIDWeb, limit: UInt64, after: String? = nil) { + self.identityKey = identityKey + self.keyserver = keyserver + self.dappAuthKey = dappAuthKey + self.app = app + self.limit = limit + self.after = after + } + init(claims: Claims) throws { fatalError() } diff --git a/Sources/WalletConnectHistory/ProtocolMethods/NotifyGetNotificationsProtocolMethod.swift b/Sources/WalletConnectHistory/ProtocolMethods/NotifyGetNotificationsProtocolMethod.swift new file mode 100644 index 000000000..b86c57a1a --- /dev/null +++ b/Sources/WalletConnectHistory/ProtocolMethods/NotifyGetNotificationsProtocolMethod.swift @@ -0,0 +1,9 @@ +import Foundation + +struct NotifyGetNotificationsProtocolMethod: ProtocolMethod { + let method: String = "wc_notifyGetNotifications" + + let requestConfig: RelayConfig = RelayConfig(tag: 4014, prompt: false, ttl: 300) + + let responseConfig: RelayConfig = RelayConfig(tag: 4015, prompt: false, ttl: 300) +} diff --git a/Sources/WalletConnectNetworking/NetworkInteracting.swift b/Sources/WalletConnectNetworking/NetworkInteracting.swift index 068ccb31b..a79814399 100644 --- a/Sources/WalletConnectNetworking/NetworkInteracting.swift +++ b/Sources/WalletConnectNetworking/NetworkInteracting.swift @@ -46,8 +46,8 @@ public protocol NetworkInteracting { request: RPCRequest, topic: String, method: ProtocolMethod, - requestOfType: Request, - responseOfType: Response, + requestOfType: Request.Type, + responseOfType: Response.Type, envelopeType: Envelope.EnvelopeType ) async throws -> Response diff --git a/Sources/WalletConnectNetworking/NetworkingInteractor.swift b/Sources/WalletConnectNetworking/NetworkingInteractor.swift index a3c360beb..f0bc0a2bb 100644 --- a/Sources/WalletConnectNetworking/NetworkingInteractor.swift +++ b/Sources/WalletConnectNetworking/NetworkingInteractor.swift @@ -143,8 +143,8 @@ public class NetworkingInteractor: NetworkInteracting { request: RPCRequest, topic: String, method: ProtocolMethod, - requestOfType: Request, - responseOfType: Response, + requestOfType: Request.Type, + responseOfType: Response.Type, envelopeType: Envelope.EnvelopeType ) async throws -> Response { From edbbabcd2f9ac3748e4be47f2d14b85bfb6bd3bc Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Fri, 12 Jan 2024 17:40:29 +0300 Subject: [PATCH 08/21] fetchHistory method --- .../Client/Wallet/NotifyClient.swift | 14 ++++++++++++++ .../Client/Wallet/NotifyClientFactory.swift | 4 +++- .../Client/Wallet/NotifyStorage.swift | 4 ++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift b/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift index ba30e6956..d64f13879 100644 --- a/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift +++ b/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift @@ -25,6 +25,7 @@ public class NotifyClient { private let keyserverURL: URL private let pushClient: PushClient private let identityClient: IdentityClient + private let historyClient: HistoryClient private let notifyStorage: NotifyStorage private let notifyAccountProvider: NotifyAccountProvider private let notifyMessageSubscriber: NotifyMessageSubscriber @@ -42,6 +43,7 @@ public class NotifyClient { keyserverURL: URL, kms: KeyManagementServiceProtocol, identityClient: IdentityClient, + historyClient: HistoryClient, pushClient: PushClient, notifyMessageSubscriber: NotifyMessageSubscriber, notifyStorage: NotifyStorage, @@ -62,6 +64,7 @@ public class NotifyClient { self.keyserverURL = keyserverURL self.pushClient = pushClient self.identityClient = identityClient + self.historyClient = historyClient self.notifyMessageSubscriber = notifyMessageSubscriber self.notifyStorage = notifyStorage self.deleteNotifySubscriptionRequester = deleteNotifySubscriptionRequester @@ -144,6 +147,17 @@ public class NotifyClient { public func messagesPublisher(topic: String) -> AnyPublisher<[NotifyMessageRecord], Never> { return notifyStorage.messagesPublisher(topic: topic) } + + public func fetchHistory(subscription: NotifySubscription) async throws { + let messages = try await historyClient.fetchHistory( + account: subscription.account, + topic: subscription.topic, + appAuthenticationKey: subscription.appAuthenticationKey, + host: subscription.metadata.url + ) + + // TBD + } } private extension NotifyClient { diff --git a/Sources/WalletConnectNotify/Client/Wallet/NotifyClientFactory.swift b/Sources/WalletConnectNotify/Client/Wallet/NotifyClientFactory.swift index 3a08a5727..3bbcbe4af 100644 --- a/Sources/WalletConnectNotify/Client/Wallet/NotifyClientFactory.swift +++ b/Sources/WalletConnectNotify/Client/Wallet/NotifyClientFactory.swift @@ -68,12 +68,14 @@ public struct NotifyClientFactory { let notifyWatchSubscriptionsResponseSubscriber = NotifyWatchSubscriptionsResponseSubscriber(networkingInteractor: networkInteractor, kms: kms, logger: logger, notifyStorage: notifyStorage, groupKeychainStorage: groupKeychainStorage, notifySubscriptionsBuilder: notifySubscriptionsBuilder) let notifySubscriptionsChangedRequestSubscriber = NotifySubscriptionsChangedRequestSubscriber(keyserver: keyserverURL, networkingInteractor: networkInteractor, kms: kms, identityClient: identityClient, logger: logger, groupKeychainStorage: groupKeychainStorage, notifyStorage: notifyStorage, notifySubscriptionsBuilder: notifySubscriptionsBuilder) let subscriptionWatcher = SubscriptionWatcher(notifyWatchSubscriptionsRequester: notifyWatchSubscriptionsRequester, logger: logger) + let historyClient = HistoryClientFactory.create(keyserver: keyserverURL, networkingInteractor: networkInteractor, identityClient: identityClient) return NotifyClient( logger: logger, keyserverURL: keyserverURL, kms: kms, - identityClient: identityClient, + identityClient: identityClient, + historyClient: historyClient, pushClient: pushClient, notifyMessageSubscriber: notifyMessageSubscriber, notifyStorage: notifyStorage, diff --git a/Sources/WalletConnectNotify/Client/Wallet/NotifyStorage.swift b/Sources/WalletConnectNotify/Client/Wallet/NotifyStorage.swift index cca2c3e6a..e26667ec6 100644 --- a/Sources/WalletConnectNotify/Client/Wallet/NotifyStorage.swift +++ b/Sources/WalletConnectNotify/Client/Wallet/NotifyStorage.swift @@ -122,6 +122,10 @@ final class NotifyStorage: NotifyStoring { func setMessage(_ message: NotifyMessageRecord) throws { try database.save(message: message) } + + func setMessages(_ messages: [NotifyMessageRecord]) throws { + try database.save(messages: messages) + } } private extension NotifyStorage { From bdd99e7142156586d788e3ba7add39f3d8676096 Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Fri, 12 Jan 2024 17:45:22 +0300 Subject: [PATCH 09/21] sent_at for NotifyMessage --- Sources/WalletConnectHistory/Types/NotifyMessage.swift | 8 +++++++- .../Client/Wallet/NotifyMessageRecord.swift | 7 +++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Sources/WalletConnectHistory/Types/NotifyMessage.swift b/Sources/WalletConnectHistory/Types/NotifyMessage.swift index e61d46223..f7fc00a72 100644 --- a/Sources/WalletConnectHistory/Types/NotifyMessage.swift +++ b/Sources/WalletConnectHistory/Types/NotifyMessage.swift @@ -7,13 +7,19 @@ public struct NotifyMessage: Codable, Equatable { public let icon: String public let url: String public let type: String + public let sent_at: UInt64 - public init(id: String, title: String, body: String, icon: String, url: String, type: String) { + public var sentAt: Date { + return Date(timeIntervalSince1970: TimeInterval(sent_at)) + } + + public init(id: String, title: String, body: String, icon: String, url: String, type: String, sentAt: Date) { self.id = id self.title = title self.body = body self.icon = icon self.url = url self.type = type + self.sent_at = UInt64(sentAt.timeIntervalSince1970) } } diff --git a/Sources/WalletConnectNotify/Client/Wallet/NotifyMessageRecord.swift b/Sources/WalletConnectNotify/Client/Wallet/NotifyMessageRecord.swift index 9fc7b1c2b..97b9a3567 100644 --- a/Sources/WalletConnectNotify/Client/Wallet/NotifyMessageRecord.swift +++ b/Sources/WalletConnectNotify/Client/Wallet/NotifyMessageRecord.swift @@ -18,16 +18,19 @@ public struct NotifyMessageRecord: Codable, Equatable, SqliteRow { public init(decoder: SqliteRowDecoder) throws { self.topic = try decoder.decodeString(at: 1) + let sentAt = try decoder.decodeDate(at: 7) + self.message = NotifyMessage( id: try decoder.decodeString(at: 0), title: try decoder.decodeString(at: 2), body: try decoder.decodeString(at: 3), icon: try decoder.decodeString(at: 4), url: try decoder.decodeString(at: 5), - type: try decoder.decodeString(at: 6) + type: try decoder.decodeString(at: 6), + sentAt: sentAt ) - self.publishedAt = try decoder.decodeDate(at: 7) + self.publishedAt = sentAt } public func encode() -> SqliteRowEncoder { From 6f5c86ce498421aa4aedfabf47d77050b7063a43 Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Fri, 12 Jan 2024 17:51:41 +0300 Subject: [PATCH 10/21] fetchHistory finalized --- .../WalletConnectNotify/Client/Wallet/NotifyClient.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift b/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift index d64f13879..89f5342a3 100644 --- a/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift +++ b/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift @@ -156,7 +156,11 @@ public class NotifyClient { host: subscription.metadata.url ) - // TBD + let records = messages.map { message in + return NotifyMessageRecord(topic: subscription.topic, message: message, publishedAt: message.sentAt) + } + + try notifyStorage.setMessages(records) } } From abb66fc6ba281641f6f41663e9ccd7c8458513ab Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Fri, 12 Jan 2024 18:16:12 +0300 Subject: [PATCH 11/21] fetch history on sample app --- .../Wallet/PushMessages/SubscriptionInteractor.swift | 6 ++++++ .../Wallet/PushMessages/SubscriptionPresenter.swift | 2 ++ Sources/WalletConnectHistory/HistoryClient.swift | 2 -- .../NotifyGetNotificationsRequestPayload.swift | 10 +++++----- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionInteractor.swift b/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionInteractor.swift index 9b0c84ff7..e3d1a14e4 100644 --- a/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionInteractor.swift +++ b/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionInteractor.swift @@ -30,4 +30,10 @@ final class SubscriptionInteractor { try await Notify.instance.deleteSubscription(topic: subscription.topic) } } + + func fetchHistory() { + Task(priority: .high) { + try await Notify.instance.fetchHistory(subscription: subscription) + } + } } diff --git a/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionPresenter.swift b/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionPresenter.swift index 23f9729f9..c0527399e 100644 --- a/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionPresenter.swift +++ b/Example/WalletApp/PresentationLayer/Wallet/PushMessages/SubscriptionPresenter.swift @@ -77,6 +77,8 @@ extension SubscriptionPresenter: SceneViewModel { private extension SubscriptionPresenter { func setupInitialState() { + interactor.fetchHistory() + pushMessages = interactor.getPushMessages() interactor.messagesPublisher diff --git a/Sources/WalletConnectHistory/HistoryClient.swift b/Sources/WalletConnectHistory/HistoryClient.swift index 4aef22b0c..6125c2b4f 100644 --- a/Sources/WalletConnectHistory/HistoryClient.swift +++ b/Sources/WalletConnectHistory/HistoryClient.swift @@ -13,12 +13,10 @@ public final class HistoryClient { } public func fetchHistory(account: Account, topic: String, appAuthenticationKey: String, host: String) async throws -> [NotifyMessage] { - let identityKey = try identityClient.getInviteKey(for: account) let dappAuthKey = try DIDKey(did: appAuthenticationKey) let app = DIDWeb(host: host) let requestPayload = NotifyGetNotificationsRequestPayload( - identityKey: DIDKey(rawData: identityKey.rawRepresentation), keyserver: keyserver.absoluteString, dappAuthKey: dappAuthKey, app: app, diff --git a/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift b/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift index a479f3af0..cce3a69b3 100644 --- a/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift +++ b/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift @@ -12,6 +12,7 @@ struct NotifyGetNotificationsRequestPayload: JWTClaimsCodable { let app: String // - did:web of app domain that this request is associated with - Example: `did:web:app.example.com` let lmt: UInt64 // - the max number of notifications to return. Maximum value is 50. let aft: String? // - the notification ID to start returning messages after. Null to start with the most recent notification + let urf: Bool static var action: String? { return "notify_get_notifications" @@ -30,15 +31,13 @@ struct NotifyGetNotificationsRequestPayload: JWTClaimsCodable { } } - let identityKey: DIDKey let keyserver: String let dappAuthKey: DIDKey let app: DIDWeb let limit: UInt64 let after: String? - init(identityKey: DIDKey, keyserver: String, dappAuthKey: DIDKey, app: DIDWeb, limit: UInt64, after: String? = nil) { - self.identityKey = identityKey + init(keyserver: String, dappAuthKey: DIDKey, app: DIDWeb, limit: UInt64, after: String? = nil) { self.keyserver = keyserver self.dappAuthKey = dappAuthKey self.app = app @@ -55,12 +54,13 @@ struct NotifyGetNotificationsRequestPayload: JWTClaimsCodable { iat: defaultIat(), exp: expiry(days: 1), act: Claims.action, - iss: identityKey.did(variant: .ED25519), + iss: iss, ksu: keyserver, aud: dappAuthKey.did(variant: .ED25519), app: app.did, lmt: limit, - aft: after + aft: after, + urf: false ) } } From 5653ddccc6eaf2f1518f04dc222988ede691b70d Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Mon, 15 Jan 2024 17:08:06 +0300 Subject: [PATCH 12/21] sub missed --- Sources/WalletConnectHistory/HistoryClient.swift | 1 + .../Payloads/NotifyGetNotificationsRequestPayload.swift | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Sources/WalletConnectHistory/HistoryClient.swift b/Sources/WalletConnectHistory/HistoryClient.swift index 6125c2b4f..fc2fa3e0c 100644 --- a/Sources/WalletConnectHistory/HistoryClient.swift +++ b/Sources/WalletConnectHistory/HistoryClient.swift @@ -17,6 +17,7 @@ public final class HistoryClient { let app = DIDWeb(host: host) let requestPayload = NotifyGetNotificationsRequestPayload( + account: account, keyserver: keyserver.absoluteString, dappAuthKey: dappAuthKey, app: app, diff --git a/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift b/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift index cce3a69b3..d54825d85 100644 --- a/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift +++ b/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift @@ -5,6 +5,7 @@ struct NotifyGetNotificationsRequestPayload: JWTClaimsCodable { struct Claims: JWTClaims { let iat: UInt64 let exp: UInt64 + let sub: String let act: String? // - `notify_get_notifications` let iss: String // - did:key of client identity key let ksu: String // - key server for identity key verification @@ -31,13 +32,15 @@ struct NotifyGetNotificationsRequestPayload: JWTClaimsCodable { } } + let account: Account let keyserver: String let dappAuthKey: DIDKey let app: DIDWeb let limit: UInt64 let after: String? - init(keyserver: String, dappAuthKey: DIDKey, app: DIDWeb, limit: UInt64, after: String? = nil) { + init(account: Account, keyserver: String, dappAuthKey: DIDKey, app: DIDWeb, limit: UInt64, after: String? = nil) { + self.account = account self.keyserver = keyserver self.dappAuthKey = dappAuthKey self.app = app @@ -52,7 +55,8 @@ struct NotifyGetNotificationsRequestPayload: JWTClaimsCodable { func encode(iss: String) throws -> Claims { return Claims( iat: defaultIat(), - exp: expiry(days: 1), + exp: expiry(days: 1), + sub: account.did, act: Claims.action, iss: iss, ksu: keyserver, From 551a10ee529fc0d73b22b8da82d94c875a79a1fb Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Mon, 15 Jan 2024 17:17:12 +0300 Subject: [PATCH 13/21] Message sent_at fix --- Sources/WalletConnectHistory/Types/NotifyMessage.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/WalletConnectHistory/Types/NotifyMessage.swift b/Sources/WalletConnectHistory/Types/NotifyMessage.swift index f7fc00a72..6c88dfa7a 100644 --- a/Sources/WalletConnectHistory/Types/NotifyMessage.swift +++ b/Sources/WalletConnectHistory/Types/NotifyMessage.swift @@ -10,7 +10,7 @@ public struct NotifyMessage: Codable, Equatable { public let sent_at: UInt64 public var sentAt: Date { - return Date(timeIntervalSince1970: TimeInterval(sent_at)) + return Date(milliseconds: sent_at) } public init(id: String, title: String, body: String, icon: String, url: String, type: String, sentAt: Date) { @@ -20,6 +20,6 @@ public struct NotifyMessage: Codable, Equatable { self.icon = icon self.url = url self.type = type - self.sent_at = UInt64(sentAt.timeIntervalSince1970) + self.sent_at = UInt64(sentAt.millisecondsSince1970) } } From 04458460064cd2f8f5717ba0707971ae58082eee Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Wed, 17 Jan 2024 17:23:35 +0300 Subject: [PATCH 14/21] Unit tests fixed --- .../NetworkingInteractorMock.swift | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Tests/TestingUtils/NetworkingInteractorMock.swift b/Tests/TestingUtils/NetworkingInteractorMock.swift index 27fa35a5c..a1fa196ad 100644 --- a/Tests/TestingUtils/NetworkingInteractorMock.swift +++ b/Tests/TestingUtils/NetworkingInteractorMock.swift @@ -134,6 +134,36 @@ public class NetworkingInteractorMock: NetworkInteracting { }.store(in: &publishers) } + public func awaitResponse( + request: RPCRequest, + topic: String, + method: ProtocolMethod, + requestOfType: Request.Type, + responseOfType: Response.Type, + envelopeType: Envelope.EnvelopeType + ) async throws -> Response { + + try await self.request(request, topic: topic, protocolMethod: method, envelopeType: envelopeType) + + return try await withCheckedThrowingContinuation { [unowned self] continuation in + var response, error: AnyCancellable? + + response = responseSubscription(on: method) + .sink { (payload: ResponseSubscriptionPayload) in + response?.cancel() + error?.cancel() + continuation.resume(with: .success(payload.response)) + } + + error = responseErrorSubscription(on: method) + .sink { (payload: ResponseSubscriptionErrorPayload) in + response?.cancel() + error?.cancel() + continuation.resume(throwing: payload.error) + } + } + } + public func subscribe(topic: String) async throws { defer { onSubscribeCalled?() } subscriptions.append(topic) From ed7f3fb5baa26633765a04019fe05fcab8af4dae Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Wed, 17 Jan 2024 17:24:18 +0300 Subject: [PATCH 15/21] Integration tests fixed --- Example/IntegrationTests/Stubs/PushMessage.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Example/IntegrationTests/Stubs/PushMessage.swift b/Example/IntegrationTests/Stubs/PushMessage.swift index 634d78ea1..68d08b36e 100644 --- a/Example/IntegrationTests/Stubs/PushMessage.swift +++ b/Example/IntegrationTests/Stubs/PushMessage.swift @@ -9,6 +9,7 @@ extension NotifyMessage { body: "body", icon: "https://images.unsplash.com/photo-1581224463294-908316338239?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=250&q=80", url: "https://web3inbox.com", - type: type) + type: type, + sentAt: Date()) } } From 2f309312f72b014ce7a65ac4e874fe644d19a8a2 Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Fri, 19 Jan 2024 15:26:24 +0300 Subject: [PATCH 16/21] HistoryPackage removed --- Package.swift | 8 +------- Sources/WalletConnectHistory/HistoryClientFactory.swift | 8 -------- Sources/WalletConnectHistory/HistoryImports.swift | 3 --- .../Client/Wallet/HistoryService.swift} | 2 +- .../WalletConnectNotify/Client/Wallet/NotifyClient.swift | 8 ++++---- .../Client/Wallet/NotifyClientFactory.swift | 4 ++-- Sources/WalletConnectNotify/NotifyImports.swift | 2 +- .../NotifyGetNotificationsProtocolMethod.swift | 0 .../Types/DataStructures}/NotifyMessage.swift | 0 .../NotifyGetNotificationsRequestPayload.swift | 0 .../NotifyGetNotificationsResponsePayload.swift | 0 11 files changed, 9 insertions(+), 26 deletions(-) delete mode 100644 Sources/WalletConnectHistory/HistoryClientFactory.swift delete mode 100644 Sources/WalletConnectHistory/HistoryImports.swift rename Sources/{WalletConnectHistory/HistoryClient.swift => WalletConnectNotify/Client/Wallet/HistoryService.swift} (97%) rename Sources/{WalletConnectHistory => WalletConnectNotify}/ProtocolMethods/NotifyGetNotificationsProtocolMethod.swift (100%) rename Sources/{WalletConnectHistory/Types => WalletConnectNotify/Types/DataStructures}/NotifyMessage.swift (100%) rename Sources/{WalletConnectHistory/Payloads => WalletConnectNotify/Types/JWTPayloads/notify_get_notifications}/NotifyGetNotificationsRequestPayload.swift (100%) rename Sources/{WalletConnectHistory/Payloads => WalletConnectNotify/Types/JWTPayloads/notify_get_notifications}/NotifyGetNotificationsResponsePayload.swift (100%) diff --git a/Package.swift b/Package.swift index 617226143..23593b272 100644 --- a/Package.swift +++ b/Package.swift @@ -43,9 +43,6 @@ let package = Package( .library( name: "WalletConnectVerify", targets: ["WalletConnectVerify"]), - .library( - name: "WalletConnectHistory", - targets: ["WalletConnectHistory"]), .library( name: "WalletConnectModal", targets: ["WalletConnectModal"]), @@ -74,7 +71,7 @@ let package = Package( path: "Sources/Web3Wallet"), .target( name: "WalletConnectNotify", - dependencies: ["WalletConnectPairing", "WalletConnectPush", "WalletConnectHistory", "WalletConnectSigner", "Database"], + dependencies: ["WalletConnectPairing", "WalletConnectPush", "WalletConnectSigner", "Database"], path: "Sources/WalletConnectNotify"), .target( name: "WalletConnectPush", @@ -92,9 +89,6 @@ let package = Package( .target( name: "WalletConnectPairing", dependencies: ["WalletConnectNetworking"]), - .target( - name: "WalletConnectHistory", - dependencies: ["WalletConnectIdentity"]), .target( name: "WalletConnectSigner", dependencies: ["WalletConnectNetworking"]), diff --git a/Sources/WalletConnectHistory/HistoryClientFactory.swift b/Sources/WalletConnectHistory/HistoryClientFactory.swift deleted file mode 100644 index 9db17a320..000000000 --- a/Sources/WalletConnectHistory/HistoryClientFactory.swift +++ /dev/null @@ -1,8 +0,0 @@ -import Foundation - -public class HistoryClientFactory { - - public static func create(keyserver: URL, networkingInteractor: NetworkInteracting, identityClient: IdentityClient) -> HistoryClient { - return HistoryClient(keyserver: keyserver, networkingClient: networkingInteractor, identityClient: identityClient) - } -} diff --git a/Sources/WalletConnectHistory/HistoryImports.swift b/Sources/WalletConnectHistory/HistoryImports.swift deleted file mode 100644 index 51e07cac3..000000000 --- a/Sources/WalletConnectHistory/HistoryImports.swift +++ /dev/null @@ -1,3 +0,0 @@ -#if !CocoaPods -@_exported import WalletConnectIdentity -#endif diff --git a/Sources/WalletConnectHistory/HistoryClient.swift b/Sources/WalletConnectNotify/Client/Wallet/HistoryService.swift similarity index 97% rename from Sources/WalletConnectHistory/HistoryClient.swift rename to Sources/WalletConnectNotify/Client/Wallet/HistoryService.swift index fc2fa3e0c..21574e569 100644 --- a/Sources/WalletConnectHistory/HistoryClient.swift +++ b/Sources/WalletConnectNotify/Client/Wallet/HistoryService.swift @@ -1,6 +1,6 @@ import Foundation -public final class HistoryClient { +public final class HistoryService { private let keyserver: URL private let networkingClient: NetworkInteracting diff --git a/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift b/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift index 89f5342a3..e4eab7e08 100644 --- a/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift +++ b/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift @@ -25,7 +25,7 @@ public class NotifyClient { private let keyserverURL: URL private let pushClient: PushClient private let identityClient: IdentityClient - private let historyClient: HistoryClient + private let historyService: HistoryService private let notifyStorage: NotifyStorage private let notifyAccountProvider: NotifyAccountProvider private let notifyMessageSubscriber: NotifyMessageSubscriber @@ -43,7 +43,7 @@ public class NotifyClient { keyserverURL: URL, kms: KeyManagementServiceProtocol, identityClient: IdentityClient, - historyClient: HistoryClient, + historyService: HistoryService, pushClient: PushClient, notifyMessageSubscriber: NotifyMessageSubscriber, notifyStorage: NotifyStorage, @@ -64,7 +64,7 @@ public class NotifyClient { self.keyserverURL = keyserverURL self.pushClient = pushClient self.identityClient = identityClient - self.historyClient = historyClient + self.historyService = historyService self.notifyMessageSubscriber = notifyMessageSubscriber self.notifyStorage = notifyStorage self.deleteNotifySubscriptionRequester = deleteNotifySubscriptionRequester @@ -149,7 +149,7 @@ public class NotifyClient { } public func fetchHistory(subscription: NotifySubscription) async throws { - let messages = try await historyClient.fetchHistory( + let messages = try await historyService.fetchHistory( account: subscription.account, topic: subscription.topic, appAuthenticationKey: subscription.appAuthenticationKey, diff --git a/Sources/WalletConnectNotify/Client/Wallet/NotifyClientFactory.swift b/Sources/WalletConnectNotify/Client/Wallet/NotifyClientFactory.swift index 3bbcbe4af..ec417a765 100644 --- a/Sources/WalletConnectNotify/Client/Wallet/NotifyClientFactory.swift +++ b/Sources/WalletConnectNotify/Client/Wallet/NotifyClientFactory.swift @@ -68,14 +68,14 @@ public struct NotifyClientFactory { let notifyWatchSubscriptionsResponseSubscriber = NotifyWatchSubscriptionsResponseSubscriber(networkingInteractor: networkInteractor, kms: kms, logger: logger, notifyStorage: notifyStorage, groupKeychainStorage: groupKeychainStorage, notifySubscriptionsBuilder: notifySubscriptionsBuilder) let notifySubscriptionsChangedRequestSubscriber = NotifySubscriptionsChangedRequestSubscriber(keyserver: keyserverURL, networkingInteractor: networkInteractor, kms: kms, identityClient: identityClient, logger: logger, groupKeychainStorage: groupKeychainStorage, notifyStorage: notifyStorage, notifySubscriptionsBuilder: notifySubscriptionsBuilder) let subscriptionWatcher = SubscriptionWatcher(notifyWatchSubscriptionsRequester: notifyWatchSubscriptionsRequester, logger: logger) - let historyClient = HistoryClientFactory.create(keyserver: keyserverURL, networkingInteractor: networkInteractor, identityClient: identityClient) + let historyService = HistoryService(keyserver: keyserverURL, networkingClient: networkInteractor, identityClient: identityClient) return NotifyClient( logger: logger, keyserverURL: keyserverURL, kms: kms, identityClient: identityClient, - historyClient: historyClient, + historyService: historyService, pushClient: pushClient, notifyMessageSubscriber: notifyMessageSubscriber, notifyStorage: notifyStorage, diff --git a/Sources/WalletConnectNotify/NotifyImports.swift b/Sources/WalletConnectNotify/NotifyImports.swift index e2f267c31..43b543236 100644 --- a/Sources/WalletConnectNotify/NotifyImports.swift +++ b/Sources/WalletConnectNotify/NotifyImports.swift @@ -1,7 +1,7 @@ #if !CocoaPods @_exported import WalletConnectPairing @_exported import WalletConnectPush -@_exported import WalletConnectHistory +@_exported import WalletConnectIdentity @_exported import WalletConnectSigner @_exported import Database #endif diff --git a/Sources/WalletConnectHistory/ProtocolMethods/NotifyGetNotificationsProtocolMethod.swift b/Sources/WalletConnectNotify/ProtocolMethods/NotifyGetNotificationsProtocolMethod.swift similarity index 100% rename from Sources/WalletConnectHistory/ProtocolMethods/NotifyGetNotificationsProtocolMethod.swift rename to Sources/WalletConnectNotify/ProtocolMethods/NotifyGetNotificationsProtocolMethod.swift diff --git a/Sources/WalletConnectHistory/Types/NotifyMessage.swift b/Sources/WalletConnectNotify/Types/DataStructures/NotifyMessage.swift similarity index 100% rename from Sources/WalletConnectHistory/Types/NotifyMessage.swift rename to Sources/WalletConnectNotify/Types/DataStructures/NotifyMessage.swift diff --git a/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift b/Sources/WalletConnectNotify/Types/JWTPayloads/notify_get_notifications/NotifyGetNotificationsRequestPayload.swift similarity index 100% rename from Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsRequestPayload.swift rename to Sources/WalletConnectNotify/Types/JWTPayloads/notify_get_notifications/NotifyGetNotificationsRequestPayload.swift diff --git a/Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsResponsePayload.swift b/Sources/WalletConnectNotify/Types/JWTPayloads/notify_get_notifications/NotifyGetNotificationsResponsePayload.swift similarity index 100% rename from Sources/WalletConnectHistory/Payloads/NotifyGetNotificationsResponsePayload.swift rename to Sources/WalletConnectNotify/Types/JWTPayloads/notify_get_notifications/NotifyGetNotificationsResponsePayload.swift From af7f2707defda0b219c670a3cc34cdcaece3da51 Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Fri, 19 Jan 2024 15:51:15 +0300 Subject: [PATCH 17/21] testWalletCreatesAndUpdatesSubscription reenabled --- Example/ExampleApp.xcodeproj/project.pbxproj | 7 ------- Example/IntegrationTests/Push/NotifyTests.swift | 2 -- Package.swift | 2 +- 3 files changed, 1 insertion(+), 10 deletions(-) diff --git a/Example/ExampleApp.xcodeproj/project.pbxproj b/Example/ExampleApp.xcodeproj/project.pbxproj index f90232086..1524f8161 100644 --- a/Example/ExampleApp.xcodeproj/project.pbxproj +++ b/Example/ExampleApp.xcodeproj/project.pbxproj @@ -61,7 +61,6 @@ A50D53C32ABA055700A4FD8B /* NotifyPreferencesRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = A50D53BE2ABA055700A4FD8B /* NotifyPreferencesRouter.swift */; }; A50D53C42ABA055700A4FD8B /* NotifyPreferencesInteractor.swift in Sources */ = {isa = PBXBuildFile; fileRef = A50D53BF2ABA055700A4FD8B /* NotifyPreferencesInteractor.swift */; }; A50D53C52ABA055700A4FD8B /* NotifyPreferencesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A50D53C02ABA055700A4FD8B /* NotifyPreferencesView.swift */; }; - A50DF19D2A25084A0036EA6C /* WalletConnectHistory in Frameworks */ = {isa = PBXBuildFile; productRef = A50DF19C2A25084A0036EA6C /* WalletConnectHistory */; }; A50F3946288005B200064555 /* Types.swift in Sources */ = {isa = PBXBuildFile; fileRef = A50F3945288005B200064555 /* Types.swift */; }; A51606F82A2F47BD00CACB92 /* DefaultBIP44Provider.swift in Sources */ = {isa = PBXBuildFile; fileRef = A51606F72A2F47BD00CACB92 /* DefaultBIP44Provider.swift */; }; A51606F92A2F47BD00CACB92 /* DefaultBIP44Provider.swift in Sources */ = {isa = PBXBuildFile; fileRef = A51606F72A2F47BD00CACB92 /* DefaultBIP44Provider.swift */; }; @@ -767,7 +766,6 @@ A5E03DFF2864662500888481 /* WalletConnect in Frameworks */, A561C80529DFCD4500DF540D /* WalletConnectSync in Frameworks */, A5E03DF52864651200888481 /* Starscream in Frameworks */, - A50DF19D2A25084A0036EA6C /* WalletConnectHistory in Frameworks */, A5C8BE85292FE20B006CC85C /* Web3 in Frameworks */, 84DDB4ED28ABB663003D66ED /* WalletConnectAuth in Frameworks */, C5DD5BE1294E09E3008FD3A4 /* Web3Wallet in Frameworks */, @@ -2107,7 +2105,6 @@ C5DD5BE0294E09E3008FD3A4 /* Web3Wallet */, A561C80429DFCD4500DF540D /* WalletConnectSync */, A573C53A29EC365800E3CBFD /* HDWalletKit */, - A50DF19C2A25084A0036EA6C /* WalletConnectHistory */, A5B6C0F22A6EAB1700927332 /* WalletConnectNotify */, ); productName = IntegrationTests; @@ -3444,10 +3441,6 @@ isa = XCSwiftPackageProductDependency; productName = WalletConnectAuth; }; - A50DF19C2A25084A0036EA6C /* WalletConnectHistory */ = { - isa = XCSwiftPackageProductDependency; - productName = WalletConnectHistory; - }; A54195A42934E83F0035AD19 /* Web3 */ = { isa = XCSwiftPackageProductDependency; package = A5AE354528A1A2AC0059AE8A /* XCRemoteSwiftPackageReference "Web3" */; diff --git a/Example/IntegrationTests/Push/NotifyTests.swift b/Example/IntegrationTests/Push/NotifyTests.swift index 8d26941ae..5e7d06038 100644 --- a/Example/IntegrationTests/Push/NotifyTests.swift +++ b/Example/IntegrationTests/Push/NotifyTests.swift @@ -165,7 +165,6 @@ final class NotifyTests: XCTestCase { } } - /* func testWalletCreatesAndUpdatesSubscription() async throws { let created = expectation(description: "Subscription created") @@ -202,7 +201,6 @@ final class NotifyTests: XCTestCase { try await walletNotifyClientA.deleteSubscription(topic: subscription.topic) } - */ func testNotifyServerSubscribeAndNotifies() async throws { let subscribeExpectation = expectation(description: "creates notify subscription") diff --git a/Package.swift b/Package.swift index 23593b272..c40c3b05c 100644 --- a/Package.swift +++ b/Package.swift @@ -71,7 +71,7 @@ let package = Package( path: "Sources/Web3Wallet"), .target( name: "WalletConnectNotify", - dependencies: ["WalletConnectPairing", "WalletConnectPush", "WalletConnectSigner", "Database"], + dependencies: ["WalletConnectIdentity", "WalletConnectPairing", "WalletConnectPush", "WalletConnectSigner", "Database"], path: "Sources/WalletConnectNotify"), .target( name: "WalletConnectPush", From f3d2b68a4392472557ab4c1efcb993cabc976899 Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Fri, 19 Jan 2024 16:01:57 +0300 Subject: [PATCH 18/21] Missing WalletConnectIdentity added --- Example/ExampleApp.xcodeproj/project.pbxproj | 7 +++++++ Example/IntegrationTests/Push/NotifyTests.swift | 1 - Package.swift | 4 +++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Example/ExampleApp.xcodeproj/project.pbxproj b/Example/ExampleApp.xcodeproj/project.pbxproj index 1524f8161..9a7ee789d 100644 --- a/Example/ExampleApp.xcodeproj/project.pbxproj +++ b/Example/ExampleApp.xcodeproj/project.pbxproj @@ -176,6 +176,7 @@ A5B6C0F32A6EAB1700927332 /* WalletConnectNotify in Frameworks */ = {isa = PBXBuildFile; productRef = A5B6C0F22A6EAB1700927332 /* WalletConnectNotify */; }; A5B6C0F52A6EAB2800927332 /* WalletConnectNotify in Frameworks */ = {isa = PBXBuildFile; productRef = A5B6C0F42A6EAB2800927332 /* WalletConnectNotify */; }; A5B6C0F72A6EAB3200927332 /* WalletConnectNotify in Frameworks */ = {isa = PBXBuildFile; productRef = A5B6C0F62A6EAB3200927332 /* WalletConnectNotify */; }; + A5B814FA2B5AAA2F00AECCFD /* WalletConnectIdentity in Frameworks */ = {isa = PBXBuildFile; productRef = A5B814F92B5AAA2F00AECCFD /* WalletConnectIdentity */; }; A5BB7FA328B6A50400707FC6 /* WalletConnectAuth in Frameworks */ = {isa = PBXBuildFile; productRef = A5BB7FA228B6A50400707FC6 /* WalletConnectAuth */; }; A5BB7FAD28B6AA7D00707FC6 /* QRCodeGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5BB7FAC28B6AA7D00707FC6 /* QRCodeGenerator.swift */; }; A5C2020B287D9DEE007E3188 /* WelcomeModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5C20206287D9DEE007E3188 /* WelcomeModule.swift */; }; @@ -765,6 +766,7 @@ files = ( A5E03DFF2864662500888481 /* WalletConnect in Frameworks */, A561C80529DFCD4500DF540D /* WalletConnectSync in Frameworks */, + A5B814FA2B5AAA2F00AECCFD /* WalletConnectIdentity in Frameworks */, A5E03DF52864651200888481 /* Starscream in Frameworks */, A5C8BE85292FE20B006CC85C /* Web3 in Frameworks */, 84DDB4ED28ABB663003D66ED /* WalletConnectAuth in Frameworks */, @@ -2106,6 +2108,7 @@ A561C80429DFCD4500DF540D /* WalletConnectSync */, A573C53A29EC365800E3CBFD /* HDWalletKit */, A5B6C0F22A6EAB1700927332 /* WalletConnectNotify */, + A5B814F92B5AAA2F00AECCFD /* WalletConnectIdentity */, ); productName = IntegrationTests; productReference = A5E03DED286464DB00888481 /* IntegrationTests.xctest */; @@ -3523,6 +3526,10 @@ isa = XCSwiftPackageProductDependency; productName = WalletConnectNotify; }; + A5B814F92B5AAA2F00AECCFD /* WalletConnectIdentity */ = { + isa = XCSwiftPackageProductDependency; + productName = WalletConnectIdentity; + }; A5BB7FA228B6A50400707FC6 /* WalletConnectAuth */ = { isa = XCSwiftPackageProductDependency; productName = WalletConnectAuth; diff --git a/Example/IntegrationTests/Push/NotifyTests.swift b/Example/IntegrationTests/Push/NotifyTests.swift index 5e7d06038..3fd829afd 100644 --- a/Example/IntegrationTests/Push/NotifyTests.swift +++ b/Example/IntegrationTests/Push/NotifyTests.swift @@ -252,7 +252,6 @@ final class NotifyTests: XCTestCase { try await walletNotifyClientA.deleteSubscription(topic: notifyMessageRecord.topic) } } - } diff --git a/Package.swift b/Package.swift index c40c3b05c..3321d7eb7 100644 --- a/Package.swift +++ b/Package.swift @@ -46,7 +46,9 @@ let package = Package( .library( name: "WalletConnectModal", targets: ["WalletConnectModal"]), - + .library( + name: "WalletConnectIdentity", + targets: ["WalletConnectIdentity"]), ], dependencies: [ .package(url: "https://github.com/apple/swift-docc-plugin", from: "1.3.0"), From 79fe9950ae7a68e5cba904f6ad36a3b3828ff5bd Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Fri, 19 Jan 2024 17:11:49 +0300 Subject: [PATCH 19/21] Messages integration tests --- .../IntegrationTests/Push/NotifyTests.swift | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Example/IntegrationTests/Push/NotifyTests.swift b/Example/IntegrationTests/Push/NotifyTests.swift index 3fd829afd..fe1125fa9 100644 --- a/Example/IntegrationTests/Push/NotifyTests.swift +++ b/Example/IntegrationTests/Push/NotifyTests.swift @@ -252,6 +252,29 @@ final class NotifyTests: XCTestCase { try await walletNotifyClientA.deleteSubscription(topic: notifyMessageRecord.topic) } } + + func testFetchHistory() async throws { + let subscribeExpectation = expectation(description: "fetch notify subscription") + let account = Account("eip155:1:0x622b17376F76d72C43527a917f59273247A917b4")! + + var subscription: NotifySubscription! + walletNotifyClientA.subscriptionsPublisher + .sink { subscriptions in + subscription = subscriptions.first + subscribeExpectation.fulfill() + }.store(in: &publishers) + + try await walletNotifyClientA.register(account: account, domain: gmDappDomain) { message in + let privateKey = Data(hex: "c3ff8a0ae33ac5d58e515055c5870fa2f220d070997bd6fd77a5f2c148528ff0") + let signer = MessageSignerFactory(signerFactory: DefaultSignerFactory()).create(projectId: InputConfig.projectId) + return try! signer.sign(message: message, privateKey: privateKey, type: .eip191) + } + + await fulfillment(of: [subscribeExpectation], timeout: InputConfig.defaultTimeout) + + try await walletNotifyClientA.fetchHistory(subscription: subscription) + XCTAssertEqual(walletNotifyClientA.getMessageHistory(topic: subscription.topic).count, 41) + } } From ba16939601fa3f5d8f86069d666fd6c60cfb7c36 Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Fri, 19 Jan 2024 17:12:06 +0300 Subject: [PATCH 20/21] awaitResponse updated with ack --- .../NetworkingInteractor.swift | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/Sources/WalletConnectNetworking/NetworkingInteractor.swift b/Sources/WalletConnectNetworking/NetworkingInteractor.swift index f0bc0a2bb..078c8adf7 100644 --- a/Sources/WalletConnectNetworking/NetworkingInteractor.swift +++ b/Sources/WalletConnectNetworking/NetworkingInteractor.swift @@ -147,25 +147,34 @@ public class NetworkingInteractor: NetworkInteracting { responseOfType: Response.Type, envelopeType: Envelope.EnvelopeType ) async throws -> Response { - - try await self.request(request, topic: topic, protocolMethod: method, envelopeType: envelopeType) - return try await withCheckedThrowingContinuation { [unowned self] continuation in var response, error: AnyCancellable? + let cancel: () -> Void = { + response?.cancel() + error?.cancel() + } + response = responseSubscription(on: method) .sink { (payload: ResponseSubscriptionPayload) in - response?.cancel() - error?.cancel() + cancel() continuation.resume(with: .success(payload.response)) } error = responseErrorSubscription(on: method) .sink { (payload: ResponseSubscriptionErrorPayload) in - response?.cancel() - error?.cancel() + cancel() continuation.resume(throwing: payload.error) } + + Task(priority: .high) { + do { + try await self.request(request, topic: topic, protocolMethod: method, envelopeType: envelopeType) + } catch { + cancel() + continuation.resume(throwing: error) + } + } } } From c5319859e6f054e72d75e0ec02b79ce678f75a2d Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Fri, 19 Jan 2024 17:23:43 +0300 Subject: [PATCH 21/21] History imports removed --- Example/ExampleApp.xcodeproj/project.pbxproj | 12 ------------ Example/IntegrationTests/Chat/ChatTests.swift | 1 - Example/IntegrationTests/History/HistoryTests.swift | 8 -------- Example/IntegrationTests/Pairing/PairingTests.swift | 1 - Sources/Chat/ChatImports.swift | 1 - WalletConnectSwiftV2.podspec | 7 ------- 6 files changed, 30 deletions(-) delete mode 100644 Example/IntegrationTests/History/HistoryTests.swift diff --git a/Example/ExampleApp.xcodeproj/project.pbxproj b/Example/ExampleApp.xcodeproj/project.pbxproj index 9a7ee789d..a5ee276b5 100644 --- a/Example/ExampleApp.xcodeproj/project.pbxproj +++ b/Example/ExampleApp.xcodeproj/project.pbxproj @@ -78,7 +78,6 @@ A518B31428E33A6500A2CE93 /* InputConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = A518B31328E33A6500A2CE93 /* InputConfig.swift */; }; A51AC0D928E436A3001BACF9 /* InputConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = A51AC0D828E436A3001BACF9 /* InputConfig.swift */; }; A51AC0DF28E4379F001BACF9 /* InputConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = A51AC0DE28E4379F001BACF9 /* InputConfig.swift */; }; - A5321C2B2A250367006CADC3 /* HistoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5321C2A2A250367006CADC3 /* HistoryTests.swift */; }; A5417BBE299BFC3E00B469F3 /* ImportAccount.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5417BBD299BFC3E00B469F3 /* ImportAccount.swift */; }; A541959E2934BFEF0035AD19 /* CacaoSignerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A541959A2934BFEF0035AD19 /* CacaoSignerTests.swift */; }; A541959F2934BFEF0035AD19 /* SignerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A541959B2934BFEF0035AD19 /* SignerTests.swift */; }; @@ -475,7 +474,6 @@ A518B31328E33A6500A2CE93 /* InputConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InputConfig.swift; sourceTree = ""; }; A51AC0D828E436A3001BACF9 /* InputConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputConfig.swift; sourceTree = ""; }; A51AC0DE28E4379F001BACF9 /* InputConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputConfig.swift; sourceTree = ""; }; - A5321C2A2A250367006CADC3 /* HistoryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoryTests.swift; sourceTree = ""; }; A5417BBD299BFC3E00B469F3 /* ImportAccount.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImportAccount.swift; sourceTree = ""; }; A541959A2934BFEF0035AD19 /* CacaoSignerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CacaoSignerTests.swift; sourceTree = ""; }; A541959B2934BFEF0035AD19 /* SignerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SignerTests.swift; sourceTree = ""; }; @@ -1047,14 +1045,6 @@ path = Settings; sourceTree = ""; }; - A5321C292A25035A006CADC3 /* History */ = { - isa = PBXGroup; - children = ( - A5321C2A2A250367006CADC3 /* HistoryTests.swift */, - ); - path = History; - sourceTree = ""; - }; A54195992934BFDD0035AD19 /* Signer */ = { isa = PBXGroup; children = ( @@ -1481,7 +1471,6 @@ isa = PBXGroup; children = ( 847F07FE2A25DBC700B2A5A4 /* XPlatform */, - A5321C292A25035A006CADC3 /* History */, A561C80129DFCCD300DF540D /* Sync */, 849D7A91292E2115006A2BD4 /* Push */, 84CEC64728D8A98900D081A8 /* Pairing */, @@ -2488,7 +2477,6 @@ 84FE684628ACDB4700C893FF /* RequestParams.swift in Sources */, 7694A5262874296A0001257E /* RegistryTests.swift in Sources */, A541959F2934BFEF0035AD19 /* SignerTests.swift in Sources */, - A5321C2B2A250367006CADC3 /* HistoryTests.swift in Sources */, A58A1ECC29BF458600A82A20 /* ENSResolverTests.swift in Sources */, A5E03DFA286465C700888481 /* SignClientTests.swift in Sources */, 84A6E3C32A386BBC008A0571 /* Publisher.swift in Sources */, diff --git a/Example/IntegrationTests/Chat/ChatTests.swift b/Example/IntegrationTests/Chat/ChatTests.swift index 0b4528fe4..ff09c65f1 100644 --- a/Example/IntegrationTests/Chat/ChatTests.swift +++ b/Example/IntegrationTests/Chat/ChatTests.swift @@ -4,7 +4,6 @@ import XCTest import WalletConnectUtils @testable import WalletConnectKMS @testable import WalletConnectSync -@testable import WalletConnectHistory import WalletConnectRelay import Combine import Web3 diff --git a/Example/IntegrationTests/History/HistoryTests.swift b/Example/IntegrationTests/History/HistoryTests.swift deleted file mode 100644 index 4ebbdd80d..000000000 --- a/Example/IntegrationTests/History/HistoryTests.swift +++ /dev/null @@ -1,8 +0,0 @@ -import Foundation -import Combine -import XCTest -@testable import WalletConnectHistory - -final class HistoryTests: XCTestCase { - -} diff --git a/Example/IntegrationTests/Pairing/PairingTests.swift b/Example/IntegrationTests/Pairing/PairingTests.swift index 657530902..6bc02014c 100644 --- a/Example/IntegrationTests/Pairing/PairingTests.swift +++ b/Example/IntegrationTests/Pairing/PairingTests.swift @@ -9,7 +9,6 @@ import WalletConnectPush @testable import Auth @testable import WalletConnectPairing @testable import WalletConnectSync -@testable import WalletConnectHistory final class PairingTests: XCTestCase { diff --git a/Sources/Chat/ChatImports.swift b/Sources/Chat/ChatImports.swift index 447ee4a25..24dfe29a5 100644 --- a/Sources/Chat/ChatImports.swift +++ b/Sources/Chat/ChatImports.swift @@ -2,5 +2,4 @@ @_exported import WalletConnectSigner @_exported import WalletConnectIdentity @_exported import WalletConnectSync -@_exported import WalletConnectHistory #endif diff --git a/WalletConnectSwiftV2.podspec b/WalletConnectSwiftV2.podspec index c3cf47387..5a294b701 100644 --- a/WalletConnectSwiftV2.podspec +++ b/WalletConnectSwiftV2.podspec @@ -101,17 +101,10 @@ Pod::Spec.new do |spec| ss.dependency 'WalletConnectSwiftV2/WalletConnectNetworking' end - spec.subspec 'WalletConnectHistory' do |ss| - ss.source_files = 'Sources/WalletConnectHistory/**/*.{h,m,swift}' - ss.dependency 'WalletConnectSwiftV2/WalletConnectRelay' - ss.dependency 'WalletConnectSwiftV2/HTTPClient' - end - spec.subspec 'WalletConnectChat' do |ss| ss.source_files = 'Sources/Chat/**/*.{h,m,swift}' ss.dependency 'WalletConnectSwiftV2/WalletConnectSync' ss.dependency 'WalletConnectSwiftV2/WalletConnectIdentity' - ss.dependency 'WalletConnectSwiftV2/WalletConnectHistory' end spec.subspec 'WalletConnectSync' do |ss|