diff --git a/Example/ExampleApp.xcodeproj/project.pbxproj b/Example/ExampleApp.xcodeproj/project.pbxproj index f90232086..a5ee276b5 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 */; }; @@ -79,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 */; }; @@ -177,6 +175,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 */; }; @@ -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 = ""; }; @@ -766,8 +764,8 @@ files = ( A5E03DFF2864662500888481 /* WalletConnect in Frameworks */, A561C80529DFCD4500DF540D /* WalletConnectSync in Frameworks */, + A5B814FA2B5AAA2F00AECCFD /* WalletConnectIdentity in Frameworks */, A5E03DF52864651200888481 /* Starscream in Frameworks */, - A50DF19D2A25084A0036EA6C /* WalletConnectHistory in Frameworks */, A5C8BE85292FE20B006CC85C /* Web3 in Frameworks */, 84DDB4ED28ABB663003D66ED /* WalletConnectAuth in Frameworks */, C5DD5BE1294E09E3008FD3A4 /* Web3Wallet in Frameworks */, @@ -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 */, @@ -2107,8 +2096,8 @@ C5DD5BE0294E09E3008FD3A4 /* Web3Wallet */, A561C80429DFCD4500DF540D /* WalletConnectSync */, A573C53A29EC365800E3CBFD /* HDWalletKit */, - A50DF19C2A25084A0036EA6C /* WalletConnectHistory */, A5B6C0F22A6EAB1700927332 /* WalletConnectNotify */, + A5B814F92B5AAA2F00AECCFD /* WalletConnectIdentity */, ); productName = IntegrationTests; productReference = A5E03DED286464DB00888481 /* IntegrationTests.xctest */; @@ -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 */, @@ -3444,10 +3432,6 @@ isa = XCSwiftPackageProductDependency; productName = WalletConnectAuth; }; - A50DF19C2A25084A0036EA6C /* WalletConnectHistory */ = { - isa = XCSwiftPackageProductDependency; - productName = WalletConnectHistory; - }; A54195A42934E83F0035AD19 /* Web3 */ = { isa = XCSwiftPackageProductDependency; package = A5AE354528A1A2AC0059AE8A /* XCRemoteSwiftPackageReference "Web3" */; @@ -3530,6 +3514,10 @@ isa = XCSwiftPackageProductDependency; productName = WalletConnectNotify; }; + A5B814F92B5AAA2F00AECCFD /* WalletConnectIdentity */ = { + isa = XCSwiftPackageProductDependency; + productName = WalletConnectIdentity; + }; A5BB7FA228B6A50400707FC6 /* WalletConnectAuth */ = { isa = XCSwiftPackageProductDependency; productName = WalletConnectAuth; diff --git a/Example/IntegrationTests/Chat/ChatTests.swift b/Example/IntegrationTests/Chat/ChatTests.swift index 04cdc7e68..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 @@ -71,18 +70,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 deleted file mode 100644 index d96e0f811..000000000 --- a/Example/IntegrationTests/History/HistoryTests.swift +++ /dev/null @@ -1,81 +0,0 @@ -import Foundation -import Combine -import XCTest -@testable import WalletConnectHistory - -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/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/Example/IntegrationTests/Push/NotifyTests.swift b/Example/IntegrationTests/Push/NotifyTests.swift index 8d26941ae..fe1125fa9 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") @@ -255,6 +253,28 @@ final class NotifyTests: XCTestCase { } } + 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) + } } 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()) } } 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/Package.swift b/Package.swift index dcfa42b87..3321d7eb7 100644 --- a/Package.swift +++ b/Package.swift @@ -43,13 +43,12 @@ let package = Package( .library( name: "WalletConnectVerify", targets: ["WalletConnectVerify"]), - .library( - name: "WalletConnectHistory", - targets: ["WalletConnectHistory"]), .library( name: "WalletConnectModal", targets: ["WalletConnectModal"]), - + .library( + name: "WalletConnectIdentity", + targets: ["WalletConnectIdentity"]), ], dependencies: [ .package(url: "https://github.com/apple/swift-docc-plugin", from: "1.3.0"), @@ -62,7 +61,7 @@ let package = Package( path: "Sources/WalletConnectSign"), .target( name: "WalletConnectChat", - dependencies: ["WalletConnectIdentity", "WalletConnectSync", "WalletConnectHistory"], + dependencies: ["WalletConnectIdentity", "WalletConnectSync"], path: "Sources/Chat"), .target( name: "Auth", @@ -74,7 +73,7 @@ let package = Package( path: "Sources/Web3Wallet"), .target( name: "WalletConnectNotify", - dependencies: ["WalletConnectPairing", "WalletConnectPush", "WalletConnectIdentity", "WalletConnectSigner", "Database"], + dependencies: ["WalletConnectIdentity", "WalletConnectPairing", "WalletConnectPush", "WalletConnectSigner", "Database"], path: "Sources/WalletConnectNotify"), .target( name: "WalletConnectPush", @@ -92,9 +91,6 @@ let package = Package( .target( name: "WalletConnectPairing", dependencies: ["WalletConnectNetworking"]), - .target( - name: "WalletConnectHistory", - dependencies: ["HTTPClient", "WalletConnectRelay"]), .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/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/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/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/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 deleted file mode 100644 index 3022a05aa..000000000 --- a/Sources/WalletConnectHistory/HistoryClient.swift +++ /dev/null @@ -1,48 +0,0 @@ -import Foundation - -public final class HistoryClient { - - private let historyUrl: String - private let relayUrl: String - private let serializer: Serializer - private let historyNetworkService: HistoryNetworkService - - 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 deleted file mode 100644 index 26d0d2044..000000000 --- a/Sources/WalletConnectHistory/HistoryClientFactory.swift +++ /dev/null @@ -1,30 +0,0 @@ -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 - ) - } -} diff --git a/Sources/WalletConnectHistory/HistoryImports.swift b/Sources/WalletConnectHistory/HistoryImports.swift deleted file mode 100644 index e6d4b859c..000000000 --- a/Sources/WalletConnectHistory/HistoryImports.swift +++ /dev/null @@ -1,4 +0,0 @@ -#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 - } -} diff --git a/Sources/WalletConnectNetworking/NetworkInteracting.swift b/Sources/WalletConnectNetworking/NetworkInteracting.swift index 6bad90646..a79814399 100644 --- a/Sources/WalletConnectNetworking/NetworkInteracting.swift +++ b/Sources/WalletConnectNetworking/NetworkInteracting.swift @@ -42,6 +42,15 @@ public protocol NetworkInteracting { subscription: @escaping (ResponseSubscriptionPayload) async throws -> Void ) + func awaitResponse( + request: RPCRequest, + topic: String, + method: ProtocolMethod, + requestOfType: Request.Type, + responseOfType: Response.Type, + envelopeType: Envelope.EnvelopeType + ) async throws -> Response + func getClientId() throws -> String } diff --git a/Sources/WalletConnectNetworking/NetworkingInteractor.swift b/Sources/WalletConnectNetworking/NetworkingInteractor.swift index b428a77f5..078c8adf7 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,45 @@ public class NetworkingInteractor: NetworkInteracting { .eraseToAnyPublisher() } + public func awaitResponse( + request: RPCRequest, + topic: String, + method: ProtocolMethod, + requestOfType: Request.Type, + responseOfType: Response.Type, + envelopeType: Envelope.EnvelopeType + ) async throws -> Response { + 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 + cancel() + continuation.resume(with: .success(payload.response)) + } + + error = responseErrorSubscription(on: method) + .sink { (payload: ResponseSubscriptionErrorPayload) in + 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) + } + } + } + } + public func responseSubscription(on request: ProtocolMethod) -> AnyPublisher, Never> { return responsePublisher .filter { rpcRequest in diff --git a/Sources/WalletConnectNotify/Client/Wallet/HistoryService.swift b/Sources/WalletConnectNotify/Client/Wallet/HistoryService.swift new file mode 100644 index 000000000..21574e569 --- /dev/null +++ b/Sources/WalletConnectNotify/Client/Wallet/HistoryService.swift @@ -0,0 +1,46 @@ +import Foundation + +public final class HistoryService { + + 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 dappAuthKey = try DIDKey(did: appAuthenticationKey) + let app = DIDWeb(host: host) + + let requestPayload = NotifyGetNotificationsRequestPayload( + account: account, + 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/WalletConnectNotify/Client/Wallet/NotifyClient.swift b/Sources/WalletConnectNotify/Client/Wallet/NotifyClient.swift index ba30e6956..e4eab7e08 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 historyService: HistoryService 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, + historyService: HistoryService, pushClient: PushClient, notifyMessageSubscriber: NotifyMessageSubscriber, notifyStorage: NotifyStorage, @@ -62,6 +64,7 @@ public class NotifyClient { self.keyserverURL = keyserverURL self.pushClient = pushClient self.identityClient = identityClient + self.historyService = historyService self.notifyMessageSubscriber = notifyMessageSubscriber self.notifyStorage = notifyStorage self.deleteNotifySubscriptionRequester = deleteNotifySubscriptionRequester @@ -144,6 +147,21 @@ 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 historyService.fetchHistory( + account: subscription.account, + topic: subscription.topic, + appAuthenticationKey: subscription.appAuthenticationKey, + host: subscription.metadata.url + ) + + let records = messages.map { message in + return NotifyMessageRecord(topic: subscription.topic, message: message, publishedAt: message.sentAt) + } + + try notifyStorage.setMessages(records) + } } private extension NotifyClient { diff --git a/Sources/WalletConnectNotify/Client/Wallet/NotifyClientFactory.swift b/Sources/WalletConnectNotify/Client/Wallet/NotifyClientFactory.swift index 3a08a5727..ec417a765 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 historyService = HistoryService(keyserver: keyserverURL, networkingClient: networkInteractor, identityClient: identityClient) return NotifyClient( logger: logger, keyserverURL: keyserverURL, kms: kms, - identityClient: identityClient, + identityClient: identityClient, + historyService: historyService, pushClient: pushClient, notifyMessageSubscriber: notifyMessageSubscriber, notifyStorage: notifyStorage, 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 { 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 { diff --git a/Sources/WalletConnectNotify/ProtocolMethods/NotifyGetNotificationsProtocolMethod.swift b/Sources/WalletConnectNotify/ProtocolMethods/NotifyGetNotificationsProtocolMethod.swift new file mode 100644 index 000000000..b86c57a1a --- /dev/null +++ b/Sources/WalletConnectNotify/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/WalletConnectNotify/Types/DataStructures/NotifyMessage.swift b/Sources/WalletConnectNotify/Types/DataStructures/NotifyMessage.swift index e61d46223..6c88dfa7a 100644 --- a/Sources/WalletConnectNotify/Types/DataStructures/NotifyMessage.swift +++ b/Sources/WalletConnectNotify/Types/DataStructures/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(milliseconds: 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.millisecondsSince1970) } } diff --git a/Sources/WalletConnectNotify/Types/JWTPayloads/notify_get_notifications/NotifyGetNotificationsRequestPayload.swift b/Sources/WalletConnectNotify/Types/JWTPayloads/notify_get_notifications/NotifyGetNotificationsRequestPayload.swift new file mode 100644 index 000000000..d54825d85 --- /dev/null +++ b/Sources/WalletConnectNotify/Types/JWTPayloads/notify_get_notifications/NotifyGetNotificationsRequestPayload.swift @@ -0,0 +1,70 @@ +import Foundation + +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 + 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 + let urf: Bool + + 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 + } + } + + let account: Account + let keyserver: String + let dappAuthKey: DIDKey + let app: DIDWeb + let limit: UInt64 + let after: String? + + 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 + self.limit = limit + self.after = after + } + + init(claims: Claims) throws { + fatalError() + } + + func encode(iss: String) throws -> Claims { + return Claims( + iat: defaultIat(), + exp: expiry(days: 1), + sub: account.did, + act: Claims.action, + iss: iss, + ksu: keyserver, + aud: dappAuthKey.did(variant: .ED25519), + app: app.did, + lmt: limit, + aft: after, + urf: false + ) + } +} diff --git a/Sources/WalletConnectNotify/Types/JWTPayloads/notify_get_notifications/NotifyGetNotificationsResponsePayload.swift b/Sources/WalletConnectNotify/Types/JWTPayloads/notify_get_notifications/NotifyGetNotificationsResponsePayload.swift new file mode 100644 index 000000000..cf8cb2243 --- /dev/null +++ b/Sources/WalletConnectNotify/Types/JWTPayloads/notify_get_notifications/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/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) 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|