From f694e298ba82e92d28baf05e0ee2a1b9fb03cb07 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Fri, 28 Jan 2022 13:49:35 +0100 Subject: [PATCH] add query session request record by jsonrpc id --- Example/DApp/ResponseViewController.swift | 6 ++++-- Sources/WalletConnect/WalletConnectClient.swift | 14 ++++++++------ Sources/WalletConnectUtils/JsonRpcRecord.swift | 13 +++++++++++++ 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/Example/DApp/ResponseViewController.swift b/Example/DApp/ResponseViewController.swift index e53f89688..e85baaafe 100644 --- a/Example/DApp/ResponseViewController.swift +++ b/Example/DApp/ResponseViewController.swift @@ -24,12 +24,13 @@ class ResponseViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() + let record = ClientDelegate.shared.client.getSessionRequestRecord(id: response.result.id)! switch response.result { case .response(let response): - responseView.nameLabel.text = "Received Response" + responseView.nameLabel.text = "Received Response\n\(record.request.method)" responseView.descriptionLabel.text = try! response.result.get(String.self).description case .error(let error): - responseView.nameLabel.text = "Received Error" + responseView.nameLabel.text = "Received Error\n\(record.request.method)" responseView.descriptionLabel.text = error.error.message } responseView.dismissButton.addTarget(self, action: #selector(dismissSelf), for: .touchUpInside) @@ -45,6 +46,7 @@ final class ResponseView: UIView { let nameLabel: UILabel = { let label = UILabel() + label.numberOfLines = 0 label.font = UIFont.systemFont(ofSize: 17.0, weight: .heavy) return label }() diff --git a/Sources/WalletConnect/WalletConnectClient.swift b/Sources/WalletConnect/WalletConnectClient.swift index c10609373..c79d0097e 100644 --- a/Sources/WalletConnect/WalletConnectClient.swift +++ b/Sources/WalletConnect/WalletConnectClient.swift @@ -263,12 +263,14 @@ public final class WalletConnectClient { } } -// /// <#Description#> -// /// - Parameter id: <#id description#> -// /// - Returns: <#description#> -// public func getSessionRequestRecord(id: Int64) -> [WalletConnectUtils.JsonRpcRecord] { -// -// } + /// - Parameter id: id of a wc_sessionPayload jsonrpc request + /// - Returns: json rpc record object for given id or nil if record for give id does not exits + public func getSessionRequestRecord(id: Int64) -> WalletConnectUtils.JsonRpcRecord? { + guard let record = history.get(id: id), + case .sessionPayload(let payload) = record.request.params else {return nil} + let request = WalletConnectUtils.JsonRpcRecord.Request(method: payload.request.method, params: payload.request.params) + return WalletConnectUtils.JsonRpcRecord(id: record.id, topic: record.topic, request: request, response: record.response, chainId: record.chainId) + } // MARK: - Private diff --git a/Sources/WalletConnectUtils/JsonRpcRecord.swift b/Sources/WalletConnectUtils/JsonRpcRecord.swift index 598d17dfe..151f51651 100644 --- a/Sources/WalletConnectUtils/JsonRpcRecord.swift +++ b/Sources/WalletConnectUtils/JsonRpcRecord.swift @@ -8,9 +8,22 @@ public struct JsonRpcRecord: Codable { public var response: JsonRpcResult? public let chainId: String? + public init(id: Int64, topic: String, request: JsonRpcRecord.Request, response: JsonRpcResult? = nil, chainId: String?) { + self.id = id + self.topic = topic + self.request = request + self.response = response + self.chainId = chainId + } + public struct Request: Codable { public let method: String public let params: AnyCodable + + public init(method: String, params: AnyCodable) { + self.method = method + self.params = params + } } }