Skip to content

Commit

Permalink
Merge pull request #328 from WalletConnect/#253-Add-user-disconnected…
Browse files Browse the repository at this point in the history
…-6000-code

[Sign] add 6000 code on disconnect
  • Loading branch information
llbartekll authored Jul 12, 2022
2 parents 566fed3 + 2b441f0 commit 52ac6f6
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Example/DApp/ Accounts/AccountsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ final class AccountsViewController: UIViewController, UITableViewDataSource, UIT
private func disconnect() {
Task {
do {
try await Sign.instance.disconnect(topic: session.topic, reason: Reason(code: 0, message: "disconnect"))
try await Sign.instance.disconnect(topic: session.topic)
DispatchQueue.main.async { [weak self] in
self?.onDisconnect?()
}
} catch {
print(error)
print(error)
// show failure alert
}
}
Expand Down
2 changes: 1 addition & 1 deletion Example/ExampleApp/Wallet/WalletViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ extension WalletViewController: UITableViewDataSource, UITableViewDelegate {
let item = sessionItems[indexPath.row]
Task {
do {
try await Sign.instance.disconnect(topic: item.topic, reason: Reason(code: 0, message: "disconnect"))
try await Sign.instance.disconnect(topic: item.topic)
DispatchQueue.main.async { [weak self] in
self?.sessionItems.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
Expand Down
2 changes: 1 addition & 1 deletion Example/IntegrationTests/Sign/SignClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ final class SignClientTests: XCTestCase {
}
dapp.onSessionSettled = { settledSession in
Task(priority: .high) {
try await dapp.client.disconnect(topic: settledSession.topic, reason: Reason(code: 5900, message: "User disconnected session"))
try await dapp.client.disconnect(topic: settledSession.topic)
}
}
wallet.onSessionDelete = {
Expand Down
6 changes: 4 additions & 2 deletions Sources/WalletConnectSign/Engine/Common/SessionEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ final class SessionEngine {
sessionStore.getAll().map {$0.publicRepresentation()}
}

func delete(topic: String, reason: Reason) async throws {
func delete(topic: String) async throws {
let reasonCode = ReasonCode.userDisconnected
let reason = SessionType.Reason(code: reasonCode.code, message: reasonCode.message)
logger.debug("Will delete session for reason: message: \(reason.message) code: \(reason.code)")
try await networkingInteractor.request(.wcSessionDelete(reason.internalRepresentation()), onTopic: topic)
try await networkingInteractor.request(.wcSessionDelete(reason), onTopic: topic)
sessionStore.delete(topic: topic)
networkingInteractor.unsubscribe(topic: topic)
}
Expand Down
5 changes: 2 additions & 3 deletions Sources/WalletConnectSign/Sign/Sign.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,8 @@ extension Sign {

/// - Parameters:
/// - topic: Session topic that you want to delete
/// - reason: Reason of session deletion
public func disconnect(topic: String, reason: Reason) async throws {
try await client.disconnect(topic: topic, reason: reason)
public func disconnect(topic: String) async throws {
try await client.disconnect(topic: topic)
}

/// - Returns: All sessions
Expand Down
4 changes: 2 additions & 2 deletions Sources/WalletConnectSign/Sign/SignClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ public final class SignClient {
/// - Parameters:
/// - topic: Session topic that you want to delete
/// - reason: Reason of session deletion
public func disconnect(topic: String, reason: Reason) async throws {
try await sessionEngine.delete(topic: topic, reason: reason)
public func disconnect(topic: String) async throws {
try await sessionEngine.delete(topic: topic)
}

/// - Returns: All sessions
Expand Down
7 changes: 7 additions & 0 deletions Sources/WalletConnectSign/Types/ReasonCode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ enum ReasonCode {
case unsupportedAccounts
case unsupportedNamespaceKey

// 6000
case userDisconnected

var code: Int {
switch self {
case .generic: return 0
Expand Down Expand Up @@ -61,6 +64,8 @@ enum ReasonCode {
case .unsupportedEvents: return 5102
case .unsupportedAccounts: return 5103
case .unsupportedNamespaceKey: return 5104

case .userDisconnected: return 6000
}
}

Expand Down Expand Up @@ -108,6 +113,8 @@ enum ReasonCode {
return "Unsupported or empty accounts for namespace"
case .unsupportedNamespaceKey:
return "Unsupported namespace key"
case .userDisconnected:
return "User discconnected"
}
}
}

0 comments on commit 52ac6f6

Please sign in to comment.