Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sign] Cleanup method opened #636

Merged
merged 3 commits into from
Dec 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/WalletConnectNetworking/NetworkInteracting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public protocol NetworkInteracting {
var requestPublisher: AnyPublisher<(topic: String, request: RPCRequest), Never> { get }
func subscribe(topic: String) async throws
func unsubscribe(topic: String)
func batchUnsubscribe(topics: [String]) async throws
func request(_ request: RPCRequest, topic: String, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws
func requestNetworkAck(_ request: RPCRequest, topic: String, protocolMethod: ProtocolMethod) async throws
func respond(topic: String, response: RPCResponse, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws
Expand Down
5 changes: 5 additions & 0 deletions Sources/WalletConnectNetworking/NetworkingInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public class NetworkingInteractor: NetworkInteracting {
}
}

public func batchUnsubscribe(topics: [String]) async throws {
try await relayClient.batchUnsubscribe(topics: topics)
rpcHistory.deleteAll(forTopics: topics)
}

public func requestSubscription<RequestParams: Codable>(on request: ProtocolMethod) -> AnyPublisher<RequestSubscriptionPayload<RequestParams>, Never> {
return requestPublisher
.filter { rpcRequest in
Expand Down
26 changes: 24 additions & 2 deletions Sources/WalletConnectRelay/RelayClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,31 @@ public final class RelayClient {
subscribe(topic: topic) { error in
if let error = error {
continuation.resume(throwing: error)
return
} else {
continuation.resume(returning: ())
}
}
}
}

public func unsubscribe(topic: String) async throws {
return try await withCheckedThrowingContinuation { continuation in
unsubscribe(topic: topic) { error in
if let error = error {
continuation.resume(throwing: error)
} else {
continuation.resume(returning: ())
}
}
}
}

public func batchUnsubscribe(topics: [String]) async throws {
await withThrowingTaskGroup(of: Void.self) { group in
for topic in topics {
group.addTask {
try await self.unsubscribe(topic: topic)
}
continuation.resume(returning: ())
}
}
}
Expand Down
23 changes: 22 additions & 1 deletion Sources/WalletConnectSign/Services/SignCleanupService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,36 @@ final class SignCleanupService {
private let sessionStore: WCSessionStorage
private let kms: KeyManagementServiceProtocol
private let sessionToPairingTopic: CodableStore<String>
private let networkInteractor: NetworkInteracting

init(pairingStore: WCPairingStorage, sessionStore: WCSessionStorage, kms: KeyManagementServiceProtocol, sessionToPairingTopic: CodableStore<String>) {
init(pairingStore: WCPairingStorage, sessionStore: WCSessionStorage, kms: KeyManagementServiceProtocol, sessionToPairingTopic: CodableStore<String>, networkInteractor: NetworkInteracting) {
self.pairingStore = pairingStore
self.sessionStore = sessionStore
self.sessionToPairingTopic = sessionToPairingTopic
self.networkInteractor = networkInteractor
self.kms = kms
}

func cleanup() async throws {
try await unsubscribe()
try cleanupStorages()
}

func cleanup() throws {
try cleanupStorages()
}
}

private extension SignCleanupService {

func unsubscribe() async throws {
let pairing = pairingStore.getAll().map { $0.topic }
let session = sessionStore.getAll().map { $0.topic }

try await networkInteractor.batchUnsubscribe(topics: pairing + session)
}

func cleanupStorages() throws {
pairingStore.deleteAll()
sessionStore.deleteAll()
sessionToPairingTopic.deleteAll()
Expand Down
7 changes: 7 additions & 0 deletions Sources/WalletConnectSign/Sign/SignClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,13 @@ public final class SignClient {
return Request(id: record.id, topic: record.topic, method: record.request.method, params: request, chainId: request.chainId)
}

/// Delete all stored data such as: pairings, sessions, keys
///
/// - Note: Will unsubscribe from all topics
public func cleanup() async throws {
try await cleanupService.cleanup()
}

#if DEBUG
/// Delete all stored data such as: pairings, sessions, keys
///
Expand Down
2 changes: 1 addition & 1 deletion Sources/WalletConnectSign/Sign/SignClientFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public struct SignClientFactory {
let nonControllerSessionStateMachine = NonControllerSessionStateMachine(networkingInteractor: networkingClient, kms: kms, sessionStore: sessionStore, logger: logger)
let controllerSessionStateMachine = ControllerSessionStateMachine(networkingInteractor: networkingClient, kms: kms, sessionStore: sessionStore, logger: logger)
let approveEngine = ApproveEngine(networkingInteractor: networkingClient, proposalPayloadsStore: proposalPayloadsStore, sessionToPairingTopic: sessionToPairingTopic, pairingRegisterer: pairingClient, metadata: metadata, kms: kms, logger: logger, pairingStore: pairingStore, sessionStore: sessionStore)
let cleanupService = SignCleanupService(pairingStore: pairingStore, sessionStore: sessionStore, kms: kms, sessionToPairingTopic: sessionToPairingTopic)
let cleanupService = SignCleanupService(pairingStore: pairingStore, sessionStore: sessionStore, kms: kms, sessionToPairingTopic: sessionToPairingTopic, networkInteractor: networkingClient)
let deleteSessionService = DeleteSessionService(networkingInteractor: networkingClient, kms: kms, sessionStore: sessionStore, logger: logger)
let disconnectService = DisconnectService(deleteSessionService: deleteSessionService, sessionStorage: sessionStore)
let sessionPingService = SessionPingService(sessionStorage: sessionStore, networkingInteractor: networkingClient, logger: logger)
Expand Down
8 changes: 6 additions & 2 deletions Sources/WalletConnectUtils/RPCHistory/RPCHistory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,18 @@ public final class RPCHistory {
return record
}

public func deleteAll(forTopic topic: String) {
public func deleteAll(forTopics topics: [String]){
storage.getAll().forEach { record in
if record.topic == topic {
if topics.contains(record.topic) {
storage.delete(forKey: "\(record.id)")
}
}
}

public func deleteAll(forTopic topic: String) {
deleteAll(forTopics: [topic])
}

public func getPending() -> [Record] {
storage.getAll().filter {$0.response == nil}
}
Expand Down
6 changes: 6 additions & 0 deletions Tests/TestingUtils/NetworkingInteractorMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ public class NetworkingInteractorMock: NetworkInteracting {
didCallUnsubscribe = true
}

public func batchUnsubscribe(topics: [String]) async throws {
for topic in topics {
unsubscribe(topic: topic)
}
}

public func request(_ request: RPCRequest, topic: String, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws {
requestCallCount += 1
requests.append((topic, request))
Expand Down