From 6bec5effeecc60f38495849e883c6b84f5479869 Mon Sep 17 00:00:00 2001 From: Artur Guseinov Date: Tue, 31 May 2022 18:07:29 +0500 Subject: [PATCH] Cleanup Service + SequenceStore refactor (#241) * Cleanup service * Clean sessionToPairingTopic * SequenceStore refactor * Rename KeyValueStore -> CodableStore --- .../Crypto/KeyManagementService.swift | 5 ++- .../Keychain/KeychainStorage.swift | 1 + Sources/WalletConnectRelay/RelayClient.swift | 2 +- .../Engine/Common/PairingEngine.swift | 8 ++-- .../Engine/Common/SessionEngine.swift | 4 +- .../JsonRpcHistory/JsonRpcHistory.swift | 4 +- .../Services/CelanupService.swift | 25 ++++++++++++ .../WalletConnectSign/Sign/SignClient.swift | 29 +++++++++----- .../Storage/PairingStorage.swift | 5 +++ .../Storage/SequenceStore.swift | 38 +++++++------------ .../Storage/SessionStorage.swift | 5 +++ ...KeyValueStore.swift => CodableStore.swift} | 20 +++++++--- .../WalletConnectUtils/JsonRpcHistory.swift | 4 +- .../Mocks/KeyManagementServiceMock.swift | 6 +++ .../Mocks/KeychainStorageMock.swift | 4 ++ .../JsonRpcHistoryTests.swift | 2 +- .../Mocks/WCPairingStorageMock.swift | 6 ++- .../Mocks/WCSessionStorageMock.swift | 4 ++ .../PairEngineTests.swift | 4 +- .../PairingEngineTests.swift | 6 +-- .../SequenceStoreTests.swift | 26 ++++++++++++- .../SessionEngineTests.swift | 2 +- .../WalletConnectSignTests/WCRelayTests.swift | 2 +- 23 files changed, 151 insertions(+), 61 deletions(-) create mode 100644 Sources/WalletConnectSign/Services/CelanupService.swift rename Sources/WalletConnectUtils/{KeyValueStore.swift => CodableStore.swift} (63%) diff --git a/Sources/WalletConnectKMS/Crypto/KeyManagementService.swift b/Sources/WalletConnectKMS/Crypto/KeyManagementService.swift index 8cc5edc8d..5eeb63cec 100644 --- a/Sources/WalletConnectKMS/Crypto/KeyManagementService.swift +++ b/Sources/WalletConnectKMS/Crypto/KeyManagementService.swift @@ -13,6 +13,7 @@ public protocol KeyManagementServiceProtocol { func deletePrivateKey(for publicKey: String) func deleteAgreementSecret(for topic: String) func deleteSymmetricKey(for topic: String) + func deleteAll() throws func performKeyAgreement(selfPublicKey: AgreementPublicKey, peerPublicKey hexRepresentation: String) throws -> AgreementKeys } @@ -122,7 +123,9 @@ public class KeyManagementService: KeyManagementServiceProtocol { return try KeyManagementService.generateAgreementKey(from: privateKey, peerPublicKey: hexRepresentation) } - + public func deleteAll() throws { + try keychain.deleteAll() + } static func generateAgreementKey(from privateKey: AgreementPrivateKey, peerPublicKey hexRepresentation: String) throws -> AgreementKeys { let peerPublicKey = try AgreementPublicKey(rawRepresentation: Data(hex: hexRepresentation)) diff --git a/Sources/WalletConnectKMS/Keychain/KeychainStorage.swift b/Sources/WalletConnectKMS/Keychain/KeychainStorage.swift index d91051c41..44c049859 100644 --- a/Sources/WalletConnectKMS/Keychain/KeychainStorage.swift +++ b/Sources/WalletConnectKMS/Keychain/KeychainStorage.swift @@ -4,6 +4,7 @@ protocol KeychainStorageProtocol { func add(_ item: T, forKey key: String) throws func read(key: String) throws -> T func delete(key: String) throws + func deleteAll() throws } final class KeychainStorage: KeychainStorageProtocol { diff --git a/Sources/WalletConnectRelay/RelayClient.swift b/Sources/WalletConnectRelay/RelayClient.swift index a0756161e..2ed75fabe 100644 --- a/Sources/WalletConnectRelay/RelayClient.swift +++ b/Sources/WalletConnectRelay/RelayClient.swift @@ -44,7 +44,7 @@ public final class RelayClient { self.logger = logger self.dispatcher = dispatcher - self.jsonRpcSubscriptionsHistory = JsonRpcHistory(logger: logger, keyValueStore: KeyValueStore(defaults: keyValueStorage, identifier: Self.historyIdentifier)) + self.jsonRpcSubscriptionsHistory = JsonRpcHistory(logger: logger, keyValueStore: CodableStore(defaults: keyValueStorage, identifier: Self.historyIdentifier)) setUpBindings() } diff --git a/Sources/WalletConnectSign/Engine/Common/PairingEngine.swift b/Sources/WalletConnectSign/Engine/Common/PairingEngine.swift index 93cb40672..4d242e7e8 100644 --- a/Sources/WalletConnectSign/Engine/Common/PairingEngine.swift +++ b/Sources/WalletConnectSign/Engine/Common/PairingEngine.swift @@ -9,11 +9,11 @@ final class PairingEngine { var onProposeResponse: ((String, SessionProposal)->())? var onSessionRejected: ((Session.Proposal, SessionType.Reason)->())? - private let proposalPayloadsStore: KeyValueStore + private let proposalPayloadsStore: CodableStore private let networkingInteractor: NetworkInteracting private let kms: KeyManagementServiceProtocol private let pairingStore: WCPairingStorage - private let sessionToPairingTopic: KeyValueStore + private let sessionToPairingTopic: CodableStore private var metadata: AppMetadata private var publishers = [AnyCancellable]() private let logger: ConsoleLogging @@ -22,11 +22,11 @@ final class PairingEngine { init(networkingInteractor: NetworkInteracting, kms: KeyManagementServiceProtocol, pairingStore: WCPairingStorage, - sessionToPairingTopic: KeyValueStore, + sessionToPairingTopic: CodableStore, metadata: AppMetadata, logger: ConsoleLogging, topicGenerator: @escaping () -> String = String.generateTopic, - proposalPayloadsStore: KeyValueStore = KeyValueStore(defaults: RuntimeKeyValueStorage(), identifier: StorageDomainIdentifiers.proposals.rawValue)) { + proposalPayloadsStore: CodableStore = CodableStore(defaults: RuntimeKeyValueStorage(), identifier: StorageDomainIdentifiers.proposals.rawValue)) { self.networkingInteractor = networkingInteractor self.kms = kms self.metadata = metadata diff --git a/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift b/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift index d49a6880c..56f930780 100644 --- a/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift +++ b/Sources/WalletConnectSign/Engine/Common/SessionEngine.swift @@ -16,7 +16,7 @@ final class SessionEngine { private let sessionStore: WCSessionStorage private let pairingStore: WCPairingStorage - private let sessionToPairingTopic: KeyValueStore + private let sessionToPairingTopic: CodableStore private let networkingInteractor: NetworkInteracting private let kms: KeyManagementServiceProtocol private var metadata: AppMetadata @@ -28,7 +28,7 @@ final class SessionEngine { kms: KeyManagementServiceProtocol, pairingStore: WCPairingStorage, sessionStore: WCSessionStorage, - sessionToPairingTopic: KeyValueStore, + sessionToPairingTopic: CodableStore, metadata: AppMetadata, logger: ConsoleLogging, topicGenerator: @escaping () -> String = String.generateTopic) { diff --git a/Sources/WalletConnectSign/JsonRpcHistory/JsonRpcHistory.swift b/Sources/WalletConnectSign/JsonRpcHistory/JsonRpcHistory.swift index 2d0724c0f..3ef1e2be7 100644 --- a/Sources/WalletConnectSign/JsonRpcHistory/JsonRpcHistory.swift +++ b/Sources/WalletConnectSign/JsonRpcHistory/JsonRpcHistory.swift @@ -11,10 +11,10 @@ protocol JsonRpcHistoryRecording { } //TODO -remove and use jsonrpc history only from utils class JsonRpcHistory: JsonRpcHistoryRecording { - let storage: KeyValueStore + let storage: CodableStore let logger: ConsoleLogging - init(logger: ConsoleLogging, keyValueStore: KeyValueStore) { + init(logger: ConsoleLogging, keyValueStore: CodableStore) { self.logger = logger self.storage = keyValueStore } diff --git a/Sources/WalletConnectSign/Services/CelanupService.swift b/Sources/WalletConnectSign/Services/CelanupService.swift new file mode 100644 index 000000000..649aa343d --- /dev/null +++ b/Sources/WalletConnectSign/Services/CelanupService.swift @@ -0,0 +1,25 @@ +import Foundation +import WalletConnectKMS +import WalletConnectUtils + +final class CleanupService { + + private let pairingStore: WCPairingStorage + private let sessionStore: WCSessionStorage + private let kms: KeyManagementServiceProtocol + private let sessionToPairingTopic: CodableStore + + init(pairingStore: WCPairingStorage, sessionStore: WCSessionStorage, kms: KeyManagementServiceProtocol, sessionToPairingTopic: CodableStore) { + self.pairingStore = pairingStore + self.sessionStore = sessionStore + self.sessionToPairingTopic = sessionToPairingTopic + self.kms = kms + } + + func cleanup() throws { + pairingStore.deleteAll() + sessionStore.deleteAll() + sessionToPairingTopic.deleteAll() + try kms.deleteAll() + } +} diff --git a/Sources/WalletConnectSign/Sign/SignClient.swift b/Sources/WalletConnectSign/Sign/SignClient.swift index edf9a9cbb..b9ccd902e 100644 --- a/Sources/WalletConnectSign/Sign/SignClient.swift +++ b/Sources/WalletConnectSign/Sign/SignClient.swift @@ -33,6 +33,7 @@ public final class SignClient { private let networkingInteractor: NetworkInteracting private let kms: KeyManagementService private let history: JsonRpcHistory + private let cleanupService: CleanupService // MARK: - Initializers @@ -52,20 +53,20 @@ public final class SignClient { init(metadata: AppMetadata, projectId: String, relayHost: String, logger: ConsoleLogging, kms: KeyManagementService, keyValueStorage: KeyValueStorage) { self.metadata = metadata self.logger = logger -// try? keychain.deleteAll() // Use for cleanup while lifecycles are not handled yet, but FIXME whenever self.kms = kms let relayClient = RelayClient(relayHost: relayHost, projectId: projectId, keyValueStorage: keyValueStorage, logger: logger) let serializer = Serializer(kms: kms) - self.history = JsonRpcHistory(logger: logger, keyValueStore: KeyValueStore(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.jsonRpcHistory.rawValue)) + self.history = JsonRpcHistory(logger: logger, keyValueStore: CodableStore(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.jsonRpcHistory.rawValue)) self.networkingInteractor = NetworkInteractor(relayClient: relayClient, serializer: serializer, logger: logger, jsonRpcHistory: history) - let pairingStore = PairingStorage(storage: SequenceStore(storage: keyValueStorage, identifier: StorageDomainIdentifiers.pairings.rawValue)) - let sessionStore = SessionStorage(storage: SequenceStore(storage: keyValueStorage, identifier: StorageDomainIdentifiers.sessions.rawValue)) - let sessionToPairingTopic = KeyValueStore(defaults: RuntimeKeyValueStorage(), identifier: StorageDomainIdentifiers.sessionToPairingTopic.rawValue) + let pairingStore = PairingStorage(storage: SequenceStore(store: .init(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.pairings.rawValue))) + let sessionStore = SessionStorage(storage: SequenceStore(store: .init(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.sessions.rawValue))) + let sessionToPairingTopic = CodableStore(defaults: RuntimeKeyValueStorage(), identifier: StorageDomainIdentifiers.sessionToPairingTopic.rawValue) self.pairingEngine = PairingEngine(networkingInteractor: networkingInteractor, kms: kms, pairingStore: pairingStore, sessionToPairingTopic: sessionToPairingTopic, metadata: metadata, logger: logger) self.sessionEngine = SessionEngine(networkingInteractor: networkingInteractor, kms: kms, pairingStore: pairingStore, sessionStore: sessionStore, sessionToPairingTopic: sessionToPairingTopic, metadata: metadata, logger: logger) self.nonControllerSessionStateMachine = NonControllerSessionStateMachine(networkingInteractor: networkingInteractor, kms: kms, sessionStore: sessionStore, logger: logger) self.controllerSessionStateMachine = ControllerSessionStateMachine(networkingInteractor: networkingInteractor, kms: kms, sessionStore: sessionStore, logger: logger) self.pairEngine = PairEngine(networkingInteractor: networkingInteractor, kms: kms, pairingStore: pairingStore) + self.cleanupService = CleanupService(pairingStore: pairingStore, sessionStore: sessionStore, kms: kms, sessionToPairingTopic: sessionToPairingTopic) setUpConnectionObserving(relayClient: relayClient) setUpEnginesCallbacks() } @@ -87,16 +88,17 @@ public final class SignClient { self.logger = logger self.kms = kms let serializer = Serializer(kms: kms) - self.history = JsonRpcHistory(logger: logger, keyValueStore: KeyValueStore(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.jsonRpcHistory.rawValue)) + self.history = JsonRpcHistory(logger: logger, keyValueStore: CodableStore(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.jsonRpcHistory.rawValue)) self.networkingInteractor = NetworkInteractor(relayClient: relayClient, serializer: serializer, logger: logger, jsonRpcHistory: history) - let pairingStore = PairingStorage(storage: SequenceStore(storage: keyValueStorage, identifier: StorageDomainIdentifiers.pairings.rawValue)) - let sessionStore = SessionStorage(storage: SequenceStore(storage: keyValueStorage, identifier: StorageDomainIdentifiers.sessions.rawValue)) - let sessionToPairingTopic = KeyValueStore(defaults: RuntimeKeyValueStorage(), identifier: StorageDomainIdentifiers.sessionToPairingTopic.rawValue) + let pairingStore = PairingStorage(storage: SequenceStore(store: .init(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.pairings.rawValue))) + let sessionStore = SessionStorage(storage: SequenceStore(store: .init(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.sessions.rawValue))) + let sessionToPairingTopic = CodableStore(defaults: RuntimeKeyValueStorage(), identifier: StorageDomainIdentifiers.sessionToPairingTopic.rawValue) self.pairingEngine = PairingEngine(networkingInteractor: networkingInteractor, kms: kms, pairingStore: pairingStore, sessionToPairingTopic: sessionToPairingTopic, metadata: metadata, logger: logger) self.sessionEngine = SessionEngine(networkingInteractor: networkingInteractor, kms: kms, pairingStore: pairingStore, sessionStore: sessionStore, sessionToPairingTopic: sessionToPairingTopic, metadata: metadata, logger: logger) self.nonControllerSessionStateMachine = NonControllerSessionStateMachine(networkingInteractor: networkingInteractor, kms: kms, sessionStore: sessionStore, logger: logger) self.controllerSessionStateMachine = ControllerSessionStateMachine(networkingInteractor: networkingInteractor, kms: kms, sessionStore: sessionStore, logger: logger) self.pairEngine = PairEngine(networkingInteractor: networkingInteractor, kms: kms, pairingStore: pairingStore) + self.cleanupService = CleanupService(pairingStore: pairingStore, sessionStore: sessionStore, kms: kms, sessionToPairingTopic: sessionToPairingTopic) setUpConnectionObserving(relayClient: relayClient) setUpEnginesCallbacks() } @@ -327,4 +329,13 @@ public final class SignClient { sessionEngine.setSubscription(topic: sessionTopic) } } + +#if DEBUG + /// Delete all stored data sach as: pairings, sessions, keys + /// + /// - Note: Doesn't unsubscribe from topics + public func cleanup() throws { + try cleanupService.cleanup() + } +#endif } diff --git a/Sources/WalletConnectSign/Storage/PairingStorage.swift b/Sources/WalletConnectSign/Storage/PairingStorage.swift index 8cd66e5db..6fb7c0ec7 100644 --- a/Sources/WalletConnectSign/Storage/PairingStorage.swift +++ b/Sources/WalletConnectSign/Storage/PairingStorage.swift @@ -5,6 +5,7 @@ protocol WCPairingStorage: AnyObject { func getPairing(forTopic topic: String) -> WCPairing? func getAll() -> [WCPairing] func delete(topic: String) + func deleteAll() } final class PairingStorage: WCPairingStorage { @@ -39,4 +40,8 @@ final class PairingStorage: WCPairingStorage { func delete(topic: String) { storage.delete(topic: topic) } + + func deleteAll() { + storage.deleteAll() + } } diff --git a/Sources/WalletConnectSign/Storage/SequenceStore.swift b/Sources/WalletConnectSign/Storage/SequenceStore.swift index 0fa21579c..1be014d6e 100644 --- a/Sources/WalletConnectSign/Storage/SequenceStore.swift +++ b/Sources/WalletConnectSign/Storage/SequenceStore.swift @@ -14,57 +14,47 @@ final class SequenceStore where T: ExpirableSequence { var onSequenceExpiration: ((_ sequence: T) -> Void)? - private let storage: KeyValueStorage + private let store: CodableStore private let dateInitializer: () -> Date - private let identifier: String - init(storage: KeyValueStorage, identifier: String, dateInitializer: @escaping () -> Date = Date.init) { - self.storage = storage + init(store: CodableStore, dateInitializer: @escaping () -> Date = Date.init) { + self.store = store self.dateInitializer = dateInitializer - self.identifier = identifier } func hasSequence(forTopic topic: String) -> Bool { (try? getSequence(forTopic: topic)) != nil } - // This force-unwrap is safe because Expirable Sequances are JSON Encodable func setSequence(_ sequence: T) { - let encoded = try! JSONEncoder().encode(sequence) - storage.set(encoded, forKey: getKey(for: sequence.topic)) + store.set(sequence, forKey: sequence.topic) } func getSequence(forTopic topic: String) throws -> T? { - guard let data = storage.object(forKey: getKey(for: topic)) as? Data else { return nil } - let sequence = try JSONDecoder().decode(T.self, from: data) - return verifyExpiry(on: sequence) + guard let value = try store.get(key: topic) else { return nil } + return verifyExpiry(on: value) } func getAll() -> [T] { - return storage.dictionaryRepresentation().compactMap { - guard $0.key.hasPrefix(identifier) else {return nil} - if let data = $0.value as? Data, let sequence = try? JSONDecoder().decode(T.self, from: data) { - return verifyExpiry(on: sequence) - } - return nil - } + let values = store.getAll() + return values.compactMap { verifyExpiry(on: $0) } } func delete(topic: String) { - storage.removeObject(forKey: getKey(for: topic)) + store.delete(forKey: topic) + } + + func deleteAll() { + store.deleteAll() } private func verifyExpiry(on sequence: T) -> T? { let now = dateInitializer() if now >= sequence.expiryDate { - storage.removeObject(forKey: getKey(for: sequence.topic)) + store.delete(forKey: sequence.topic) onSequenceExpiration?(sequence) return nil } return sequence } - - private func getKey(for topic: String) -> String { - return "\(identifier).\(topic)" - } } diff --git a/Sources/WalletConnectSign/Storage/SessionStorage.swift b/Sources/WalletConnectSign/Storage/SessionStorage.swift index 9d2548dbd..a71d455ff 100644 --- a/Sources/WalletConnectSign/Storage/SessionStorage.swift +++ b/Sources/WalletConnectSign/Storage/SessionStorage.swift @@ -5,6 +5,7 @@ protocol WCSessionStorage: AnyObject { func getSession(forTopic topic: String) -> WCSession? func getAll() -> [WCSession] func delete(topic: String) + func deleteAll() } final class SessionStorage: WCSessionStorage { @@ -39,4 +40,8 @@ final class SessionStorage: WCSessionStorage { func delete(topic: String) { storage.delete(topic: topic) } + + func deleteAll() { + storage.deleteAll() + } } diff --git a/Sources/WalletConnectUtils/KeyValueStore.swift b/Sources/WalletConnectUtils/CodableStore.swift similarity index 63% rename from Sources/WalletConnectUtils/KeyValueStore.swift rename to Sources/WalletConnectUtils/CodableStore.swift index cccbd1da5..c1356a31f 100644 --- a/Sources/WalletConnectUtils/KeyValueStore.swift +++ b/Sources/WalletConnectUtils/CodableStore.swift @@ -1,7 +1,7 @@ import Foundation -public final class KeyValueStore where T: Codable { +public final class CodableStore where T: Codable { private let defaults: KeyValueStorage private let prefix: String @@ -10,8 +10,9 @@ public final class KeyValueStore where T: Codable { self.prefix = identifier } - public func set(_ item: T, forKey key: String) throws { - let encoded = try JSONEncoder().encode(item) + public func set(_ item: T, forKey key: String) { + // This force-unwrap is safe because T are JSON Encodable + let encoded = try! JSONEncoder().encode(item) defaults.set(encoded, forKey: getContextPrefixedKey(for: key)) } @@ -22,8 +23,7 @@ public final class KeyValueStore where T: Codable { } public func getAll() -> [T] { - return defaults.dictionaryRepresentation().compactMap { - guard $0.key.hasPrefix(prefix) else {return nil} + return dictionaryForIdentifier().compactMap { if let data = $0.value as? Data, let item = try? JSONDecoder().decode(T.self, from: data) { return item @@ -36,7 +36,17 @@ public final class KeyValueStore where T: Codable { defaults.removeObject(forKey: getContextPrefixedKey(for: key)) } + public func deleteAll() { + dictionaryForIdentifier() + .forEach { defaults.removeObject(forKey: $0.key) } + } + private func getContextPrefixedKey(for key: String) -> String { return "\(prefix).\(key)" } + + private func dictionaryForIdentifier() -> [String : Any] { + return defaults.dictionaryRepresentation() + .filter { $0.key.hasPrefix("\(prefix).") } + } } diff --git a/Sources/WalletConnectUtils/JsonRpcHistory.swift b/Sources/WalletConnectUtils/JsonRpcHistory.swift index 8d2f27b58..28f9e8b31 100644 --- a/Sources/WalletConnectUtils/JsonRpcHistory.swift +++ b/Sources/WalletConnectUtils/JsonRpcHistory.swift @@ -6,10 +6,10 @@ public class JsonRpcHistory where T: Codable&Equatable { case jsonRpcDuplicateDetected case noJsonRpcRequestMatchingResponse } - private let storage: KeyValueStore + private let storage: CodableStore private let logger: ConsoleLogging - public init(logger: ConsoleLogging, keyValueStore: KeyValueStore) { + public init(logger: ConsoleLogging, keyValueStore: CodableStore) { self.logger = logger self.storage = keyValueStore } diff --git a/Tests/TestingUtils/Mocks/KeyManagementServiceMock.swift b/Tests/TestingUtils/Mocks/KeyManagementServiceMock.swift index 3b8526bd5..0686e0986 100644 --- a/Tests/TestingUtils/Mocks/KeyManagementServiceMock.swift +++ b/Tests/TestingUtils/Mocks/KeyManagementServiceMock.swift @@ -85,6 +85,12 @@ final class KeyManagementServiceMock: KeyManagementServiceProtocol { func deleteAgreementSecret(for topic: String) { agreementKeys[topic] = nil } + + func deleteAll() throws { + privateKeys = [:] + symmetricKeys = [:] + agreementKeys = [:] + } } extension KeyManagementServiceMock { diff --git a/Tests/TestingUtils/Mocks/KeychainStorageMock.swift b/Tests/TestingUtils/Mocks/KeychainStorageMock.swift index 0d281f21d..58bbdc73c 100644 --- a/Tests/TestingUtils/Mocks/KeychainStorageMock.swift +++ b/Tests/TestingUtils/Mocks/KeychainStorageMock.swift @@ -26,4 +26,8 @@ final class KeychainStorageMock: KeychainStorageProtocol { didCallDelete = true storage[key] = nil } + + func deleteAll() throws { + storage = [:] + } } diff --git a/Tests/WalletConnectSignTests/JsonRpcHistoryTests.swift b/Tests/WalletConnectSignTests/JsonRpcHistoryTests.swift index 3d241639a..a467501f5 100644 --- a/Tests/WalletConnectSignTests/JsonRpcHistoryTests.swift +++ b/Tests/WalletConnectSignTests/JsonRpcHistoryTests.swift @@ -10,7 +10,7 @@ final class JsonRpcHistoryTests: XCTestCase { var sut: WalletConnectSign.JsonRpcHistory! override func setUp() { - sut = JsonRpcHistory(logger: ConsoleLoggerMock(), keyValueStore: KeyValueStore(defaults: RuntimeKeyValueStorage(), identifier: "")) + sut = JsonRpcHistory(logger: ConsoleLoggerMock(), keyValueStore: CodableStore(defaults: RuntimeKeyValueStorage(), identifier: "")) } override func tearDown() { diff --git a/Tests/WalletConnectSignTests/Mocks/WCPairingStorageMock.swift b/Tests/WalletConnectSignTests/Mocks/WCPairingStorageMock.swift index 98d0a1e90..9961946a8 100644 --- a/Tests/WalletConnectSignTests/Mocks/WCPairingStorageMock.swift +++ b/Tests/WalletConnectSignTests/Mocks/WCPairingStorageMock.swift @@ -1,7 +1,7 @@ @testable import WalletConnectSign final class WCPairingStorageMock: WCPairingStorage { - + var onPairingExpiration: ((WCPairing) -> Void)? private(set) var pairings: [String: WCPairing] = [:] @@ -25,4 +25,8 @@ final class WCPairingStorageMock: WCPairingStorage { func delete(topic: String) { pairings[topic] = nil } + + func deleteAll() { + pairings = [:] + } } diff --git a/Tests/WalletConnectSignTests/Mocks/WCSessionStorageMock.swift b/Tests/WalletConnectSignTests/Mocks/WCSessionStorageMock.swift index a771fd4c9..c543ecfea 100644 --- a/Tests/WalletConnectSignTests/Mocks/WCSessionStorageMock.swift +++ b/Tests/WalletConnectSignTests/Mocks/WCSessionStorageMock.swift @@ -25,5 +25,9 @@ final class WCSessionStorageMock: WCSessionStorage { func delete(topic: String) { sessions[topic] = nil } + + func deleteAll() { + sessions = [:] + } } diff --git a/Tests/WalletConnectSignTests/PairEngineTests.swift b/Tests/WalletConnectSignTests/PairEngineTests.swift index 63a30f607..f6c85e4b8 100644 --- a/Tests/WalletConnectSignTests/PairEngineTests.swift +++ b/Tests/WalletConnectSignTests/PairEngineTests.swift @@ -12,7 +12,7 @@ final class PairEngineTests: XCTestCase { var networkingInteractor: MockedWCRelay! var storageMock: WCPairingStorageMock! var cryptoMock: KeyManagementServiceMock! - var proposalPayloadsStore: KeyValueStore! + var proposalPayloadsStore: CodableStore! var topicGenerator: TopicGenerator! @@ -21,7 +21,7 @@ final class PairEngineTests: XCTestCase { storageMock = WCPairingStorageMock() cryptoMock = KeyManagementServiceMock() topicGenerator = TopicGenerator() - proposalPayloadsStore = KeyValueStore(defaults: RuntimeKeyValueStorage(), identifier: "") + proposalPayloadsStore = CodableStore(defaults: RuntimeKeyValueStorage(), identifier: "") setupEngine() } diff --git a/Tests/WalletConnectSignTests/PairingEngineTests.swift b/Tests/WalletConnectSignTests/PairingEngineTests.swift index 825da57ba..26177b51a 100644 --- a/Tests/WalletConnectSignTests/PairingEngineTests.swift +++ b/Tests/WalletConnectSignTests/PairingEngineTests.swift @@ -16,7 +16,7 @@ final class PairingEngineTests: XCTestCase { var networkingInteractor: MockedWCRelay! var storageMock: WCPairingStorageMock! var cryptoMock: KeyManagementServiceMock! - var proposalPayloadsStore: KeyValueStore! + var proposalPayloadsStore: CodableStore! var topicGenerator: TopicGenerator! @@ -25,7 +25,7 @@ final class PairingEngineTests: XCTestCase { storageMock = WCPairingStorageMock() cryptoMock = KeyManagementServiceMock() topicGenerator = TopicGenerator() - proposalPayloadsStore = KeyValueStore(defaults: RuntimeKeyValueStorage(), identifier: "") + proposalPayloadsStore = CodableStore(defaults: RuntimeKeyValueStorage(), identifier: "") setupEngine() } @@ -44,7 +44,7 @@ final class PairingEngineTests: XCTestCase { networkingInteractor: networkingInteractor, kms: cryptoMock, pairingStore: storageMock, - sessionToPairingTopic: KeyValueStore(defaults: RuntimeKeyValueStorage(), identifier: ""), + sessionToPairingTopic: CodableStore(defaults: RuntimeKeyValueStorage(), identifier: ""), metadata: meta, logger: logger, topicGenerator: topicGenerator.getTopic, diff --git a/Tests/WalletConnectSignTests/SequenceStoreTests.swift b/Tests/WalletConnectSignTests/SequenceStoreTests.swift index 6667bc3a7..4a20b1812 100644 --- a/Tests/WalletConnectSignTests/SequenceStoreTests.swift +++ b/Tests/WalletConnectSignTests/SequenceStoreTests.swift @@ -20,8 +20,7 @@ final class SequenceStoreTests: XCTestCase { override func setUp() { timeTraveler = TimeTraveler() - storageFake = RuntimeKeyValueStorage() - sut = SequenceStore(storage: storageFake, identifier: "", dateInitializer: timeTraveler.generateDate) + sut = makeStore("test") sut.onSequenceExpiration = { _ in XCTFail("Unexpected expiration call") } @@ -33,6 +32,13 @@ final class SequenceStoreTests: XCTestCase { sut = nil } + private func makeStore(_ identifier: String) -> SequenceStore { + return SequenceStore( + store: .init(defaults: RuntimeKeyValueStorage(), identifier: identifier), + dateInitializer: timeTraveler.generateDate + ) + } + private func stubSequence(expiry: TimeInterval? = nil) -> ExpirableSequenceStub { ExpirableSequenceStub( topic: String.generateTopic(), @@ -73,6 +79,22 @@ final class SequenceStoreTests: XCTestCase { XCTAssertNil(retrieved) } + func testDeleteAll() { + let sequence = stubSequence() + sut.setSequence(sequence) + + let sut2 = makeStore("test2") + sut2.setSequence(sequence) + + XCTAssertFalse(sut.getAll().isEmpty) + XCTAssertFalse(sut2.getAll().isEmpty) + + sut.deleteAll() + + XCTAssertTrue(sut.getAll().isEmpty) + XCTAssertFalse(sut2.getAll().isEmpty) + } + // MARK: - Expiration Tests func testHasSequenceExpiration() { diff --git a/Tests/WalletConnectSignTests/SessionEngineTests.swift b/Tests/WalletConnectSignTests/SessionEngineTests.swift index db4d9ccc0..cabea8efa 100644 --- a/Tests/WalletConnectSignTests/SessionEngineTests.swift +++ b/Tests/WalletConnectSignTests/SessionEngineTests.swift @@ -46,7 +46,7 @@ final class SessionEngineTests: XCTestCase { kms: cryptoMock, pairingStore: WCPairingStorageMock(), sessionStore: storageMock, - sessionToPairingTopic: KeyValueStore(defaults: RuntimeKeyValueStorage(), identifier: ""), + sessionToPairingTopic: CodableStore(defaults: RuntimeKeyValueStorage(), identifier: ""), metadata: metadata, logger: logger, topicGenerator: topicGenerator.getTopic) diff --git a/Tests/WalletConnectSignTests/WCRelayTests.swift b/Tests/WalletConnectSignTests/WCRelayTests.swift index c605f886e..b0978732d 100644 --- a/Tests/WalletConnectSignTests/WCRelayTests.swift +++ b/Tests/WalletConnectSignTests/WCRelayTests.swift @@ -17,7 +17,7 @@ class NetworkingInteractorTests: XCTestCase { let logger = ConsoleLoggerMock() serializer = SerializerMock() relayClient = MockedRelayClient() - networkingInteractor = NetworkInteractor(relayClient: relayClient, serializer: serializer, logger: logger, jsonRpcHistory: JsonRpcHistory(logger: logger, keyValueStore: KeyValueStore(defaults: RuntimeKeyValueStorage(), identifier: ""))) + networkingInteractor = NetworkInteractor(relayClient: relayClient, serializer: serializer, logger: logger, jsonRpcHistory: JsonRpcHistory(logger: logger, keyValueStore: CodableStore(defaults: RuntimeKeyValueStorage(), identifier: ""))) } override func tearDown() {