From fb0509d89e3da2e40e02d2932b6e7cd54bdb8fe6 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Tue, 16 Aug 2022 11:05:36 +0200 Subject: [PATCH 1/8] savepoint --- Example/ExampleApp.xcodeproj/project.pbxproj | 12 ++++ Example/IntegrationTests/Auth/AuthTests.swift | 60 +++++++++++++++++++ Sources/Auth/AuthClient.swift | 2 +- Sources/Auth/AuthClientFactory.swift | 49 +++++++++++++++ Sources/Auth/StorageDomainIdentifiers.swift | 6 ++ Sources/Auth/Types/Aliases/Account.swift | 2 +- 6 files changed, 129 insertions(+), 2 deletions(-) create mode 100644 Example/IntegrationTests/Auth/AuthTests.swift create mode 100644 Sources/Auth/AuthClientFactory.swift create mode 100644 Sources/Auth/StorageDomainIdentifiers.swift diff --git a/Example/ExampleApp.xcodeproj/project.pbxproj b/Example/ExampleApp.xcodeproj/project.pbxproj index 91b9ee090..6ed75d168 100644 --- a/Example/ExampleApp.xcodeproj/project.pbxproj +++ b/Example/ExampleApp.xcodeproj/project.pbxproj @@ -43,6 +43,7 @@ 84CE644E279ED2FF00142511 /* SelectChainView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84CE644D279ED2FF00142511 /* SelectChainView.swift */; }; 84CE6452279ED42B00142511 /* ConnectView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84CE6451279ED42B00142511 /* ConnectView.swift */; }; 84CE645527A29D4D00142511 /* ResponseViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84CE645427A29D4C00142511 /* ResponseViewController.swift */; }; + 84D2A66628A4F51E0088AE09 /* AuthTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84D2A66528A4F51E0088AE09 /* AuthTests.swift */; }; 84F568C2279582D200D0A289 /* Signer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84F568C1279582D200D0A289 /* Signer.swift */; }; 84F568C42795832A00D0A289 /* EthereumTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84F568C32795832A00D0A289 /* EthereumTransaction.swift */; }; A50F3946288005B200064555 /* Types.swift in Sources */ = {isa = PBXBuildFile; fileRef = A50F3945288005B200064555 /* Types.swift */; }; @@ -203,6 +204,7 @@ 84CE6451279ED42B00142511 /* ConnectView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectView.swift; sourceTree = ""; }; 84CE6453279FFE1100142511 /* Wallet.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Wallet.entitlements; sourceTree = ""; }; 84CE645427A29D4C00142511 /* ResponseViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResponseViewController.swift; sourceTree = ""; }; + 84D2A66528A4F51E0088AE09 /* AuthTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthTests.swift; sourceTree = ""; }; 84F568C1279582D200D0A289 /* Signer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Signer.swift; sourceTree = ""; }; 84F568C32795832A00D0A289 /* EthereumTransaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EthereumTransaction.swift; sourceTree = ""; }; A50F3945288005B200064555 /* Types.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Types.swift; sourceTree = ""; }; @@ -520,6 +522,14 @@ path = Connect; sourceTree = ""; }; + 84D2A66728A4F5260088AE09 /* Auth */ = { + isa = PBXGroup; + children = ( + 84D2A66528A4F51E0088AE09 /* AuthTests.swift */, + ); + path = Auth; + sourceTree = ""; + }; A50F3944288005A700064555 /* Types */ = { isa = PBXGroup; children = ( @@ -857,6 +867,7 @@ A5E03E0A28646A8A00888481 /* Stubs */, A5E03E0928646A8100888481 /* Sign */, A5E03E0828646A7B00888481 /* Chat */, + 84D2A66728A4F5260088AE09 /* Auth */, ); path = IntegrationTests; sourceTree = ""; @@ -1262,6 +1273,7 @@ 767DC83528997F8E00080FA9 /* EthSendTransaction.swift in Sources */, A5E03DFB286465C700888481 /* ClientDelegate.swift in Sources */, A5E03E03286466F400888481 /* ChatTests.swift in Sources */, + 84D2A66628A4F51E0088AE09 /* AuthTests.swift in Sources */, 7694A5262874296A0001257E /* RegistryTests.swift in Sources */, A5E03E0D28646AD200888481 /* RelayClientEndToEndTests.swift in Sources */, A5E03DFA286465C700888481 /* SignClientTests.swift in Sources */, diff --git a/Example/IntegrationTests/Auth/AuthTests.swift b/Example/IntegrationTests/Auth/AuthTests.swift new file mode 100644 index 000000000..d8186726a --- /dev/null +++ b/Example/IntegrationTests/Auth/AuthTests.swift @@ -0,0 +1,60 @@ +import Foundation +import XCTest +@testable import Auth +import WalletConnectUtils +@testable import WalletConnectKMS +import WalletConnectRelay +import Combine + +final class AuthTests: XCTestCase { + var registry: KeyValueRegistry! + private var publishers = [AnyCancellable]() + + override func setUp() { + registry = KeyValueRegistry() + invitee = makeClient(prefix: "🦖 Registered") + inviter = makeClient(prefix: "🍄 Inviter") + + waitClientsConnected() + } + + private func waitClientsConnected() { + let expectation = expectation(description: "Wait Clients Connected") + expectation.expectedFulfillmentCount = 2 + + invitee.socketConnectionStatusPublisher.sink { status in + if status == .connected { + expectation.fulfill() + } + }.store(in: &publishers) + + inviter.socketConnectionStatusPublisher.sink { status in + if status == .connected { + expectation.fulfill() + } + }.store(in: &publishers) + + wait(for: [expectation], timeout: 5) + } + + func makeClient(prefix: String) -> ChatClient { + let logger = ConsoleLogger(suffix: prefix, loggingLevel: .debug) + let relayHost = "relay.walletconnect.com" + let projectId = "8ba9ee138960775e5231b70cc5ef1c3a" + let keychain = KeychainStorageMock() + let relayClient = RelayClient(relayHost: relayHost, projectId: projectId, keychainStorage: keychain, socketFactory: SocketFactory(), logger: logger) + return ChatClientFactory.create(registry: registry, relayClient: relayClient, kms: KeyManagementService(keychain: keychain), logger: logger, keyValueStorage: RuntimeKeyValueStorage()) + } + + func testInvite() async { + let inviteExpectation = expectation(description: "invitation expectation") + let inviteeAccount = Account(chainIdentifier: "eip155:1", address: "0x3627523167367216556273151")! + let inviterAccount = Account(chainIdentifier: "eip155:1", address: "0x36275231673672234423f")! + let pubKey = try! await invitee.register(account: inviteeAccount) + try! await inviter.invite(publicKey: pubKey, peerAccount: inviteeAccount, openingMessage: "", account: inviterAccount) + invitee.invitePublisher.sink { _ in + inviteExpectation.fulfill() + }.store(in: &publishers) + wait(for: [inviteExpectation], timeout: 4) + } +} diff --git a/Sources/Auth/AuthClient.swift b/Sources/Auth/AuthClient.swift index 6ea7bc053..1b7569b2e 100644 --- a/Sources/Auth/AuthClient.swift +++ b/Sources/Auth/AuthClient.swift @@ -3,7 +3,7 @@ import Combine import WalletConnectUtils import WalletConnectPairing -class AuthClient { +public class AuthClient { enum Errors: Error { case malformedPairingURI case unknownWalletAddress diff --git a/Sources/Auth/AuthClientFactory.swift b/Sources/Auth/AuthClientFactory.swift new file mode 100644 index 000000000..379fbc471 --- /dev/null +++ b/Sources/Auth/AuthClientFactory.swift @@ -0,0 +1,49 @@ +import Foundation +import WalletConnectRelay +import WalletConnectUtils +import WalletConnectKMS +import WalletConnectPairing + +public struct AuthClientFactory { + + public static func create(metadata: AppMetadata, account: Account, relayClient: RelayClient) -> AuthClient { + + let keyValueStorage = UserDefaults.standard + let historyStorage = CodableStore(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.jsonRpcHistory.rawValue) + + let keychainStorage = KeychainStorage(serviceIdentifier: "com.walletconnect.sdk") + let pairingStore = PairingStorage(storage: SequenceStore(store: .init(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.pairings.rawValue))) + + + let logger = ConsoleLogger(loggingLevel: .off) + let kms = KeyManagementService(keychain: keychainStorage) + let serializer = Serializer(kms: kms) + let history = RPCHistory(keyValueStore: historyStorage) + let networkingInteractor = NetworkingInteractor(relayClient: relayClient, serializer: serializer, rpcHistory: history) + + + let appPairService = AppPairService(networkingInteractor: networkingInteractor, kms: kms, pairingStorage: <#T##WCPairingStorage#>) + let appRequestService = AppRequestService(networkingInteractor: networkingInteractor, kms: kms, appMetadata: metadata) + let appRespondSubscriber = AppRespondSubscriber(networkingInteractor: networkingInteractor, logger: <#T##ConsoleLogging#>, rpcHistory: <#T##RPCHistory#>)networkingInteractor + let walletPairService = WalletPairService(networkingInteractor: networkingInteractor, kms: kms, pairingStorage: <#T##WCPairingStorage#>) + let walletRequestSubscriber = WalletRequestSubscriber(networkingInteractor: networkingInteractor, logger: <#T##ConsoleLogging#>, messageFormatter: <#T##SIWEMessageFormatting#>, address: account.address) + let walletRespondService = WalletRespondService(networkingInteractor: networkingInteractor, logger: <#T##ConsoleLogging#>, kms: kms, rpcHistory: <#T##RPCHistory#>) + let account = account + let pendingRequestsProvider = + let cleanupService = + let logger = + let pairingStorage = + + return AuthClient(appPairService: <#T##AppPairService#>, + appRequestService: <#T##AppRequestService#>, + appRespondSubscriber: <#T##AppRespondSubscriber#>, + walletPairService: <#T##WalletPairService#>, + walletRequestSubscriber: <#T##WalletRequestSubscriber#>, + walletRespondService: <#T##WalletRespondService#>, + account: <#T##Account?#>, + pendingRequestsProvider: <#T##PendingRequestsProvider#>, + cleanupService: <#T##CleanupService#>, + logger: <#T##ConsoleLogging#>, + pairingStorage: <#T##WCPairingStorage#>) + } +} diff --git a/Sources/Auth/StorageDomainIdentifiers.swift b/Sources/Auth/StorageDomainIdentifiers.swift new file mode 100644 index 000000000..ed7156c67 --- /dev/null +++ b/Sources/Auth/StorageDomainIdentifiers.swift @@ -0,0 +1,6 @@ +import Foundation + +enum StorageDomainIdentifiers: String { + case jsonRpcHistory = "com.walletconnect.sdk.wc_jsonRpcHistoryRecord" + case pairings = "com.walletconnect.sdk.pairingSequences" +} diff --git a/Sources/Auth/Types/Aliases/Account.swift b/Sources/Auth/Types/Aliases/Account.swift index 84759e639..3ab41d381 100644 --- a/Sources/Auth/Types/Aliases/Account.swift +++ b/Sources/Auth/Types/Aliases/Account.swift @@ -1,4 +1,4 @@ import WalletConnectUtils import Foundation -typealias Account = WalletConnectUtils.Account +public typealias Account = WalletConnectUtils.Account From a697ee815e509c4e242eae97e856c2cc659e461a Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Tue, 16 Aug 2022 11:42:40 +0200 Subject: [PATCH 2/8] Add Auth Client Factory --- Sources/Auth/AuthClientFactory.swift | 48 ++++++++----------- .../Auth/Services/Common/CleanupService.swift | 2 +- 2 files changed, 21 insertions(+), 29 deletions(-) diff --git a/Sources/Auth/AuthClientFactory.swift b/Sources/Auth/AuthClientFactory.swift index 379fbc471..fd72c3c02 100644 --- a/Sources/Auth/AuthClientFactory.swift +++ b/Sources/Auth/AuthClientFactory.swift @@ -7,43 +7,35 @@ import WalletConnectPairing public struct AuthClientFactory { public static func create(metadata: AppMetadata, account: Account, relayClient: RelayClient) -> AuthClient { - let keyValueStorage = UserDefaults.standard let historyStorage = CodableStore(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.jsonRpcHistory.rawValue) - let keychainStorage = KeychainStorage(serviceIdentifier: "com.walletconnect.sdk") let pairingStore = PairingStorage(storage: SequenceStore(store: .init(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.pairings.rawValue))) - - let logger = ConsoleLogger(loggingLevel: .off) let kms = KeyManagementService(keychain: keychainStorage) let serializer = Serializer(kms: kms) let history = RPCHistory(keyValueStore: historyStorage) let networkingInteractor = NetworkingInteractor(relayClient: relayClient, serializer: serializer, rpcHistory: history) - - - let appPairService = AppPairService(networkingInteractor: networkingInteractor, kms: kms, pairingStorage: <#T##WCPairingStorage#>) + let SIWEMessageFormatter = SIWEMessageFormatter() + let appPairService = AppPairService(networkingInteractor: networkingInteractor, kms: kms, pairingStorage: pairingStore) let appRequestService = AppRequestService(networkingInteractor: networkingInteractor, kms: kms, appMetadata: metadata) - let appRespondSubscriber = AppRespondSubscriber(networkingInteractor: networkingInteractor, logger: <#T##ConsoleLogging#>, rpcHistory: <#T##RPCHistory#>)networkingInteractor - let walletPairService = WalletPairService(networkingInteractor: networkingInteractor, kms: kms, pairingStorage: <#T##WCPairingStorage#>) - let walletRequestSubscriber = WalletRequestSubscriber(networkingInteractor: networkingInteractor, logger: <#T##ConsoleLogging#>, messageFormatter: <#T##SIWEMessageFormatting#>, address: account.address) - let walletRespondService = WalletRespondService(networkingInteractor: networkingInteractor, logger: <#T##ConsoleLogging#>, kms: kms, rpcHistory: <#T##RPCHistory#>) - let account = account - let pendingRequestsProvider = - let cleanupService = - let logger = - let pairingStorage = - - return AuthClient(appPairService: <#T##AppPairService#>, - appRequestService: <#T##AppRequestService#>, - appRespondSubscriber: <#T##AppRespondSubscriber#>, - walletPairService: <#T##WalletPairService#>, - walletRequestSubscriber: <#T##WalletRequestSubscriber#>, - walletRespondService: <#T##WalletRespondService#>, - account: <#T##Account?#>, - pendingRequestsProvider: <#T##PendingRequestsProvider#>, - cleanupService: <#T##CleanupService#>, - logger: <#T##ConsoleLogging#>, - pairingStorage: <#T##WCPairingStorage#>) + let appRespondSubscriber = AppRespondSubscriber(networkingInteractor: networkingInteractor, logger: logger, rpcHistory: history) + let walletPairService = WalletPairService(networkingInteractor: networkingInteractor, kms: kms, pairingStorage: pairingStore) + let walletRequestSubscriber = WalletRequestSubscriber(networkingInteractor: networkingInteractor, logger: logger, messageFormatter: SIWEMessageFormatter, address: account.address) + let walletRespondService = WalletRespondService(networkingInteractor: networkingInteractor, logger: logger, kms: kms, rpcHistory: history) + let pendingRequestsProvider = PendingRequestsProvider(rpcHistory: history) + let cleanupService = CleanupService(pairingStore: pairingStore, kms: kms) + + return AuthClient(appPairService: appPairService, + appRequestService: appRequestService, + appRespondSubscriber: appRespondSubscriber, + walletPairService: walletPairService, + walletRequestSubscriber: walletRequestSubscriber, + walletRespondService: walletRespondService, + account: account, + pendingRequestsProvider: pendingRequestsProvider, + cleanupService: cleanupService, + logger: logger, + pairingStorage: pairingStore) } } diff --git a/Sources/Auth/Services/Common/CleanupService.swift b/Sources/Auth/Services/Common/CleanupService.swift index dac2b63c5..6a5a01334 100644 --- a/Sources/Auth/Services/Common/CleanupService.swift +++ b/Sources/Auth/Services/Common/CleanupService.swift @@ -8,7 +8,7 @@ final class CleanupService { private let pairingStore: WCPairingStorage private let kms: KeyManagementServiceProtocol - init(pairingStore: WCPairingStorage, kms: KeyManagementServiceProtocol, sessionToPairingTopic: CodableStore) { + init(pairingStore: WCPairingStorage, kms: KeyManagementServiceProtocol) { self.pairingStore = pairingStore self.kms = kms } From d1a226000a7d07dd43a42b9e93a312721e4b9d19 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Tue, 16 Aug 2022 11:45:09 +0200 Subject: [PATCH 3/8] Add public access for request and respond params --- Sources/Auth/Types/RequestParams.swift | 2 +- Sources/Auth/Types/RespondParams.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/Auth/Types/RequestParams.swift b/Sources/Auth/Types/RequestParams.swift index 24e7afab9..a3f456add 100644 --- a/Sources/Auth/Types/RequestParams.swift +++ b/Sources/Auth/Types/RequestParams.swift @@ -1,6 +1,6 @@ import Foundation -struct RequestParams { +public struct RequestParams { let domain: String let chainId: String let nonce: String diff --git a/Sources/Auth/Types/RespondParams.swift b/Sources/Auth/Types/RespondParams.swift index fcd9b3293..b3757a64a 100644 --- a/Sources/Auth/Types/RespondParams.swift +++ b/Sources/Auth/Types/RespondParams.swift @@ -1,6 +1,6 @@ import Foundation -struct RespondParams { +public struct RespondParams { let id: Int64 let topic: String let signature: CacaoSignature From e87a3dae5f246a28965f07967f821ae122b0d876 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Tue, 16 Aug 2022 12:22:02 +0200 Subject: [PATCH 4/8] update auth test --- Example/ExampleApp.xcodeproj/project.pbxproj | 1 + Example/IntegrationTests/Auth/AuthTests.swift | 25 +++++++++---------- Package.swift | 3 +++ Sources/Auth/AuthClient.swift | 2 +- .../WalletConnectSign/Sign/SignClient.swift | 2 +- 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/Example/ExampleApp.xcodeproj/project.pbxproj b/Example/ExampleApp.xcodeproj/project.pbxproj index 6ed75d168..3817b2d02 100644 --- a/Example/ExampleApp.xcodeproj/project.pbxproj +++ b/Example/ExampleApp.xcodeproj/project.pbxproj @@ -1435,6 +1435,7 @@ IPHONEOS_DEPLOYMENT_TARGET = 13.0; MTL_ENABLE_DEBUG_INFO = NO; MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = NO; SDKROOT = iphoneos; SWIFT_COMPILATION_MODE = wholemodule; SWIFT_OPTIMIZATION_LEVEL = "-O"; diff --git a/Example/IntegrationTests/Auth/AuthTests.swift b/Example/IntegrationTests/Auth/AuthTests.swift index d8186726a..b171a3ff0 100644 --- a/Example/IntegrationTests/Auth/AuthTests.swift +++ b/Example/IntegrationTests/Auth/AuthTests.swift @@ -1,52 +1,51 @@ import Foundation import XCTest -@testable import Auth import WalletConnectUtils @testable import WalletConnectKMS import WalletConnectRelay import Combine +@testable import Auth final class AuthTests: XCTestCase { - var registry: KeyValueRegistry! + var app: AuthClient! + var wallet: AuthClient! private var publishers = [AnyCancellable]() override func setUp() { registry = KeyValueRegistry() - invitee = makeClient(prefix: "🦖 Registered") - inviter = makeClient(prefix: "🍄 Inviter") - - waitClientsConnected() - } + app = makeClient(prefix: "👻 App") + wallet = makeClient(prefix: "🤑 Wallet") - private func waitClientsConnected() { let expectation = expectation(description: "Wait Clients Connected") expectation.expectedFulfillmentCount = 2 - invitee.socketConnectionStatusPublisher.sink { status in + app.socketConnectionStatusPublisher.sink { status in if status == .connected { expectation.fulfill() } }.store(in: &publishers) - inviter.socketConnectionStatusPublisher.sink { status in + wallet.socketConnectionStatusPublisher.sink { status in if status == .connected { expectation.fulfill() } }.store(in: &publishers) wait(for: [expectation], timeout: 5) + } - func makeClient(prefix: String) -> ChatClient { + + func makeClient(prefix: String) -> AuthClient { let logger = ConsoleLogger(suffix: prefix, loggingLevel: .debug) let relayHost = "relay.walletconnect.com" let projectId = "8ba9ee138960775e5231b70cc5ef1c3a" let keychain = KeychainStorageMock() let relayClient = RelayClient(relayHost: relayHost, projectId: projectId, keychainStorage: keychain, socketFactory: SocketFactory(), logger: logger) - return ChatClientFactory.create(registry: registry, relayClient: relayClient, kms: KeyManagementService(keychain: keychain), logger: logger, keyValueStorage: RuntimeKeyValueStorage()) + return AuthClientFactory() } - func testInvite() async { + func testRequest() async { let inviteExpectation = expectation(description: "invitation expectation") let inviteeAccount = Account(chainIdentifier: "eip155:1", address: "0x3627523167367216556273151")! let inviterAccount = Account(chainIdentifier: "eip155:1", address: "0x36275231673672234423f")! diff --git a/Package.swift b/Package.swift index 46b112754..4bf5abd95 100644 --- a/Package.swift +++ b/Package.swift @@ -16,6 +16,9 @@ let package = Package( .library( name: "WalletConnectChat", targets: ["Chat"]), + .library( + name: "WalletConnectAurth", + targets: ["Auth"]), .library( name: "WalletConnectRouter", targets: ["WalletConnectRouter"]), diff --git a/Sources/Auth/AuthClient.swift b/Sources/Auth/AuthClient.swift index 1b7569b2e..24f54f50b 100644 --- a/Sources/Auth/AuthClient.swift +++ b/Sources/Auth/AuthClient.swift @@ -92,7 +92,7 @@ public class AuthClient { } #if DEBUG - /// Delete all stored data sach as: pairings, sessions, keys + /// Delete all stored data such as: pairings, keys /// /// - Note: Doesn't unsubscribe from topics public func cleanup() throws { diff --git a/Sources/WalletConnectSign/Sign/SignClient.swift b/Sources/WalletConnectSign/Sign/SignClient.swift index 43df10383..d21285649 100644 --- a/Sources/WalletConnectSign/Sign/SignClient.swift +++ b/Sources/WalletConnectSign/Sign/SignClient.swift @@ -279,7 +279,7 @@ public final class SignClient { } #if DEBUG - /// Delete all stored data sach as: pairings, sessions, keys + /// Delete all stored data such as: pairings, sessions, keys /// /// - Note: Doesn't unsubscribe from topics public func cleanup() throws { From 6dda075dfc720aea4cfafc4e87fc36cb4965ed06 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Tue, 16 Aug 2022 14:42:21 +0200 Subject: [PATCH 5/8] add socket connection publisher fix build --- Example/ExampleApp.xcodeproj/project.pbxproj | 7 +++++++ Example/IntegrationTests/Auth/AuthTests.swift | 1 - Package.swift | 2 +- Sources/Auth/AuthClient.swift | 8 +++++++- Sources/Auth/AuthClientFactory.swift | 5 +++-- 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Example/ExampleApp.xcodeproj/project.pbxproj b/Example/ExampleApp.xcodeproj/project.pbxproj index 3817b2d02..1b2c1316e 100644 --- a/Example/ExampleApp.xcodeproj/project.pbxproj +++ b/Example/ExampleApp.xcodeproj/project.pbxproj @@ -44,6 +44,7 @@ 84CE6452279ED42B00142511 /* ConnectView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84CE6451279ED42B00142511 /* ConnectView.swift */; }; 84CE645527A29D4D00142511 /* ResponseViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84CE645427A29D4C00142511 /* ResponseViewController.swift */; }; 84D2A66628A4F51E0088AE09 /* AuthTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84D2A66528A4F51E0088AE09 /* AuthTests.swift */; }; + 84DDB4ED28ABB663003D66ED /* WalletConnectAuth in Frameworks */ = {isa = PBXBuildFile; productRef = 84DDB4EC28ABB663003D66ED /* WalletConnectAuth */; }; 84F568C2279582D200D0A289 /* Signer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84F568C1279582D200D0A289 /* Signer.swift */; }; 84F568C42795832A00D0A289 /* EthereumTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84F568C32795832A00D0A289 /* EthereumTransaction.swift */; }; A50F3946288005B200064555 /* Types.swift in Sources */ = {isa = PBXBuildFile; fileRef = A50F3945288005B200064555 /* Types.swift */; }; @@ -343,6 +344,7 @@ files = ( A5E03DFF2864662500888481 /* WalletConnect in Frameworks */, A5E03DF52864651200888481 /* Starscream in Frameworks */, + 84DDB4ED28ABB663003D66ED /* WalletConnectAuth in Frameworks */, A5E03E01286466EA00888481 /* WalletConnectChat in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; @@ -1028,6 +1030,7 @@ A5E03DF42864651200888481 /* Starscream */, A5E03DFE2864662500888481 /* WalletConnect */, A5E03E00286466EA00888481 /* WalletConnectChat */, + 84DDB4EC28ABB663003D66ED /* WalletConnectAuth */, ); productName = IntegrationTests; productReference = A5E03DED286464DB00888481 /* IntegrationTests.xctest */; @@ -1780,6 +1783,10 @@ isa = XCSwiftPackageProductDependency; productName = WalletConnect; }; + 84DDB4EC28ABB663003D66ED /* WalletConnectAuth */ = { + isa = XCSwiftPackageProductDependency; + productName = WalletConnectAuth; + }; A5629AE92877F2D600094373 /* WalletConnectChat */ = { isa = XCSwiftPackageProductDependency; productName = WalletConnectChat; diff --git a/Example/IntegrationTests/Auth/AuthTests.swift b/Example/IntegrationTests/Auth/AuthTests.swift index b171a3ff0..da6a42c8e 100644 --- a/Example/IntegrationTests/Auth/AuthTests.swift +++ b/Example/IntegrationTests/Auth/AuthTests.swift @@ -12,7 +12,6 @@ final class AuthTests: XCTestCase { private var publishers = [AnyCancellable]() override func setUp() { - registry = KeyValueRegistry() app = makeClient(prefix: "👻 App") wallet = makeClient(prefix: "🤑 Wallet") diff --git a/Package.swift b/Package.swift index 4bf5abd95..df0509232 100644 --- a/Package.swift +++ b/Package.swift @@ -17,7 +17,7 @@ let package = Package( name: "WalletConnectChat", targets: ["Chat"]), .library( - name: "WalletConnectAurth", + name: "WalletConnectAuth", targets: ["Auth"]), .library( name: "WalletConnectRouter", diff --git a/Sources/Auth/AuthClient.swift b/Sources/Auth/AuthClient.swift index 24f54f50b..d4ce191e9 100644 --- a/Sources/Auth/AuthClient.swift +++ b/Sources/Auth/AuthClient.swift @@ -2,6 +2,7 @@ import Foundation import Combine import WalletConnectUtils import WalletConnectPairing +import WalletConnectRelay public class AuthClient { enum Errors: Error { @@ -19,6 +20,8 @@ public class AuthClient { authResponsePublisherSubject.eraseToAnyPublisher() } + public let socketConnectionStatusPublisher: AnyPublisher + private let appPairService: AppPairService private let appRequestService: AppRequestService private let appRespondSubscriber: AppRespondSubscriber @@ -43,7 +46,9 @@ public class AuthClient { pendingRequestsProvider: PendingRequestsProvider, cleanupService: CleanupService, logger: ConsoleLogging, - pairingStorage: WCPairingStorage) { + pairingStorage: WCPairingStorage, + socketConnectionStatusPublisher: AnyPublisher +) { self.appPairService = appPairService self.appRequestService = appRequestService self.walletPairService = walletPairService @@ -55,6 +60,7 @@ public class AuthClient { self.cleanupService = cleanupService self.logger = logger self.pairingStorage = pairingStorage + self.socketConnectionStatusPublisher = socketConnectionStatusPublisher setUpPublishers() } diff --git a/Sources/Auth/AuthClientFactory.swift b/Sources/Auth/AuthClientFactory.swift index fd72c3c02..4486e126f 100644 --- a/Sources/Auth/AuthClientFactory.swift +++ b/Sources/Auth/AuthClientFactory.swift @@ -19,7 +19,8 @@ public struct AuthClientFactory { let SIWEMessageFormatter = SIWEMessageFormatter() let appPairService = AppPairService(networkingInteractor: networkingInteractor, kms: kms, pairingStorage: pairingStore) let appRequestService = AppRequestService(networkingInteractor: networkingInteractor, kms: kms, appMetadata: metadata) - let appRespondSubscriber = AppRespondSubscriber(networkingInteractor: networkingInteractor, logger: logger, rpcHistory: history) + let messageSigner = MessageSigner(signer: Signer()) + let appRespondSubscriber = AppRespondSubscriber(networkingInteractor: networkingInteractor, logger: logger, rpcHistory: history, signatureVerifier: messageSigner, messageFormatter: SIWEMessageFormatter) let walletPairService = WalletPairService(networkingInteractor: networkingInteractor, kms: kms, pairingStorage: pairingStore) let walletRequestSubscriber = WalletRequestSubscriber(networkingInteractor: networkingInteractor, logger: logger, messageFormatter: SIWEMessageFormatter, address: account.address) let walletRespondService = WalletRespondService(networkingInteractor: networkingInteractor, logger: logger, kms: kms, rpcHistory: history) @@ -36,6 +37,6 @@ public struct AuthClientFactory { pendingRequestsProvider: pendingRequestsProvider, cleanupService: cleanupService, logger: logger, - pairingStorage: pairingStore) + pairingStorage: pairingStore, socketConnectionStatusPublisher: relayClient.socketConnectionStatusPublisher) } } From 8cd5c9701cad768bf8f7a04d5f3afd4ac66b6574 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Tue, 16 Aug 2022 14:59:22 +0200 Subject: [PATCH 6/8] fix integration tests build --- Example/IntegrationTests/Auth/AuthTests.swift | 23 ++++++++----------- Sources/Auth/AuthClientFactory.swift | 5 ++-- .../Wallet/WalletRequestSubscriber.swift | 8 +++++-- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Example/IntegrationTests/Auth/AuthTests.swift b/Example/IntegrationTests/Auth/AuthTests.swift index da6a42c8e..99dce1da0 100644 --- a/Example/IntegrationTests/Auth/AuthTests.swift +++ b/Example/IntegrationTests/Auth/AuthTests.swift @@ -13,7 +13,8 @@ final class AuthTests: XCTestCase { override func setUp() { app = makeClient(prefix: "👻 App") - wallet = makeClient(prefix: "🤑 Wallet") + let walletAccount = Account(chainIdentifier: "eip155:1", address: "0x3627523167367216556273151")! + wallet = makeClient(prefix: "🤑 Wallet", account: walletAccount) let expectation = expectation(description: "Wait Clients Connected") expectation.expectedFulfillmentCount = 2 @@ -31,28 +32,24 @@ final class AuthTests: XCTestCase { }.store(in: &publishers) wait(for: [expectation], timeout: 5) - } - func makeClient(prefix: String) -> AuthClient { + func makeClient(prefix: String, account: Account? = nil) -> AuthClient { let logger = ConsoleLogger(suffix: prefix, loggingLevel: .debug) let relayHost = "relay.walletconnect.com" let projectId = "8ba9ee138960775e5231b70cc5ef1c3a" let keychain = KeychainStorageMock() let relayClient = RelayClient(relayHost: relayHost, projectId: projectId, keychainStorage: keychain, socketFactory: SocketFactory(), logger: logger) - return AuthClientFactory() + + return AuthClientFactory.create( + metadata: AppMetadata(name: name, description: "", url: "", icons: [""]), + account: account, + relayClient: relayClient, + logger: logger) } func testRequest() async { - let inviteExpectation = expectation(description: "invitation expectation") - let inviteeAccount = Account(chainIdentifier: "eip155:1", address: "0x3627523167367216556273151")! - let inviterAccount = Account(chainIdentifier: "eip155:1", address: "0x36275231673672234423f")! - let pubKey = try! await invitee.register(account: inviteeAccount) - try! await inviter.invite(publicKey: pubKey, peerAccount: inviteeAccount, openingMessage: "", account: inviterAccount) - invitee.invitePublisher.sink { _ in - inviteExpectation.fulfill() - }.store(in: &publishers) - wait(for: [inviteExpectation], timeout: 4) + } } diff --git a/Sources/Auth/AuthClientFactory.swift b/Sources/Auth/AuthClientFactory.swift index 4486e126f..c3b128e0f 100644 --- a/Sources/Auth/AuthClientFactory.swift +++ b/Sources/Auth/AuthClientFactory.swift @@ -6,12 +6,11 @@ import WalletConnectPairing public struct AuthClientFactory { - public static func create(metadata: AppMetadata, account: Account, relayClient: RelayClient) -> AuthClient { + public static func create(metadata: AppMetadata, account: Account?, relayClient: RelayClient, logger: ConsoleLogging) -> AuthClient { let keyValueStorage = UserDefaults.standard let historyStorage = CodableStore(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.jsonRpcHistory.rawValue) let keychainStorage = KeychainStorage(serviceIdentifier: "com.walletconnect.sdk") let pairingStore = PairingStorage(storage: SequenceStore(store: .init(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.pairings.rawValue))) - let logger = ConsoleLogger(loggingLevel: .off) let kms = KeyManagementService(keychain: keychainStorage) let serializer = Serializer(kms: kms) let history = RPCHistory(keyValueStore: historyStorage) @@ -22,7 +21,7 @@ public struct AuthClientFactory { let messageSigner = MessageSigner(signer: Signer()) let appRespondSubscriber = AppRespondSubscriber(networkingInteractor: networkingInteractor, logger: logger, rpcHistory: history, signatureVerifier: messageSigner, messageFormatter: SIWEMessageFormatter) let walletPairService = WalletPairService(networkingInteractor: networkingInteractor, kms: kms, pairingStorage: pairingStore) - let walletRequestSubscriber = WalletRequestSubscriber(networkingInteractor: networkingInteractor, logger: logger, messageFormatter: SIWEMessageFormatter, address: account.address) + let walletRequestSubscriber = WalletRequestSubscriber(networkingInteractor: networkingInteractor, logger: logger, messageFormatter: SIWEMessageFormatter, address: account?.address) let walletRespondService = WalletRespondService(networkingInteractor: networkingInteractor, logger: logger, kms: kms, rpcHistory: history) let pendingRequestsProvider = PendingRequestsProvider(rpcHistory: history) let cleanupService = CleanupService(pairingStore: pairingStore, kms: kms) diff --git a/Sources/Auth/Services/Wallet/WalletRequestSubscriber.swift b/Sources/Auth/Services/Wallet/WalletRequestSubscriber.swift index ddd28089c..2dfad7f3a 100644 --- a/Sources/Auth/Services/Wallet/WalletRequestSubscriber.swift +++ b/Sources/Auth/Services/Wallet/WalletRequestSubscriber.swift @@ -6,7 +6,7 @@ import JSONRPC class WalletRequestSubscriber { private let networkingInteractor: NetworkInteracting private let logger: ConsoleLogging - private let address: String + private let address: String? private var publishers = [AnyCancellable]() private let messageFormatter: SIWEMessageFormatting var onRequest: ((_ id: RPCID, _ message: String)->Void)? @@ -14,7 +14,7 @@ class WalletRequestSubscriber { init(networkingInteractor: NetworkInteracting, logger: ConsoleLogging, messageFormatter: SIWEMessageFormatting, - address: String) { + address: String?) { self.networkingInteractor = networkingInteractor self.logger = logger self.address = address @@ -23,6 +23,10 @@ class WalletRequestSubscriber { } private func subscribeForRequest() { + guard let address = address else { + logger.warn("unexpected request") + return + } networkingInteractor.requestPublisher.sink { [unowned self] subscriptionPayload in guard let requestId = subscriptionPayload.request.id, From 55a38d260072cec845120590ba0419e7c8bbbe75 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Wed, 17 Aug 2022 10:35:25 +0200 Subject: [PATCH 7/8] refactor Auth client factory --- Example/IntegrationTests/Auth/AuthTests.swift | 6 ++++-- Sources/Auth/AuthClientFactory.swift | 9 +++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Example/IntegrationTests/Auth/AuthTests.swift b/Example/IntegrationTests/Auth/AuthTests.swift index 99dce1da0..1b721162f 100644 --- a/Example/IntegrationTests/Auth/AuthTests.swift +++ b/Example/IntegrationTests/Auth/AuthTests.swift @@ -45,8 +45,10 @@ final class AuthTests: XCTestCase { return AuthClientFactory.create( metadata: AppMetadata(name: name, description: "", url: "", icons: [""]), account: account, - relayClient: relayClient, - logger: logger) + logger: logger, + keyValueStorage: RuntimeKeyValueStorage(), + keychainStorage: keychain, + relayClient: relayClient) } func testRequest() async { diff --git a/Sources/Auth/AuthClientFactory.swift b/Sources/Auth/AuthClientFactory.swift index c3b128e0f..9948bb638 100644 --- a/Sources/Auth/AuthClientFactory.swift +++ b/Sources/Auth/AuthClientFactory.swift @@ -6,10 +6,15 @@ import WalletConnectPairing public struct AuthClientFactory { - public static func create(metadata: AppMetadata, account: Account?, relayClient: RelayClient, logger: ConsoleLogging) -> AuthClient { + public static func create(metadata: AppMetadata, account: Account?, relayClient: RelayClient) -> AuthClient { + let logger = ConsoleLogger(loggingLevel: .off) let keyValueStorage = UserDefaults.standard - let historyStorage = CodableStore(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.jsonRpcHistory.rawValue) let keychainStorage = KeychainStorage(serviceIdentifier: "com.walletconnect.sdk") + return AuthClientFactory.create(metadata: metadata, account: account, logger: logger, keyValueStorage: keyValueStorage, keychainStorage: keychainStorage, relayClient: relayClient) + } + + static func create(metadata: AppMetadata, account: Account?, logger: ConsoleLogging, keyValueStorage: KeyValueStorage, keychainStorage: KeychainStorageProtocol, relayClient: RelayClient) -> AuthClient { + let historyStorage = CodableStore(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.jsonRpcHistory.rawValue) let pairingStore = PairingStorage(storage: SequenceStore(store: .init(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.pairings.rawValue))) let kms = KeyManagementService(keychain: keychainStorage) let serializer = Serializer(kms: kms) From bd6424b9a3be48820bed0a860eebf06ca07dcd9e Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Wed, 17 Aug 2022 13:25:26 +0200 Subject: [PATCH 8/8] rename property to messageFormatter --- Sources/Auth/AuthClientFactory.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Auth/AuthClientFactory.swift b/Sources/Auth/AuthClientFactory.swift index 9948bb638..f5e6a6567 100644 --- a/Sources/Auth/AuthClientFactory.swift +++ b/Sources/Auth/AuthClientFactory.swift @@ -20,7 +20,7 @@ public struct AuthClientFactory { let serializer = Serializer(kms: kms) let history = RPCHistory(keyValueStore: historyStorage) let networkingInteractor = NetworkingInteractor(relayClient: relayClient, serializer: serializer, rpcHistory: history) - let SIWEMessageFormatter = SIWEMessageFormatter() + let messageFormatter = SIWEMessageFormatter() let appPairService = AppPairService(networkingInteractor: networkingInteractor, kms: kms, pairingStorage: pairingStore) let appRequestService = AppRequestService(networkingInteractor: networkingInteractor, kms: kms, appMetadata: metadata) let messageSigner = MessageSigner(signer: Signer())