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

[Network] Refactor: #355 Relay client RPC #384

Merged
merged 13 commits into from
Aug 1, 2022
8 changes: 4 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ let package = Package(
path: "Sources/Auth"),
.target(
name: "WalletConnectRelay",
dependencies: ["WalletConnectUtils", "WalletConnectKMS"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WalletConnectUtils depends on JSONRPC, do we need to add JSONRPC dependency here also?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed it

dependencies: ["WalletConnectUtils", "WalletConnectKMS", "JSONRPC"],
path: "Sources/WalletConnectRelay"),
.target(
name: "WalletConnectKMS",
Expand All @@ -47,7 +47,7 @@ let package = Package(
dependencies: ["WalletConnectUtils"]),
.target(
name: "WalletConnectUtils",
dependencies: ["Commons"]),
dependencies: ["Commons", "JSONRPC"]),
.target(
name: "JSONRPC",
dependencies: ["Commons"]),
Expand All @@ -71,11 +71,11 @@ let package = Package(
dependencies: ["WalletConnectKMS", "WalletConnectUtils", "TestingUtils"]),
.target(
name: "TestingUtils",
dependencies: ["WalletConnectUtils", "WalletConnectKMS"],
dependencies: ["WalletConnectUtils", "WalletConnectKMS", "JSONRPC"],
path: "Tests/TestingUtils"),
.testTarget(
name: "WalletConnectUtilsTests",
dependencies: ["WalletConnectUtils"]),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed it

dependencies: ["WalletConnectUtils", "JSONRPC", "TestingUtils"]),
.testTarget(
name: "JSONRPCTests",
dependencies: ["JSONRPC", "TestingUtils"]),
Expand Down
12 changes: 12 additions & 0 deletions Sources/Commons/Either.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,15 @@ extension Either: Codable where L: Codable, R: Codable {
}
}
}

extension Either: CustomStringConvertible {
flypaper0 marked this conversation as resolved.
Show resolved Hide resolved

public var description: String {
switch self {
case let .left(left):
return "\(left)"
case let .right(right):
return "\(right)"
}
}
}
4 changes: 2 additions & 2 deletions Sources/JSONRPC/RPCRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ public struct RPCRequest: Equatable {

extension RPCRequest {

static func notification<C>(method: String, params: C) -> RPCRequest where C: Codable {
public static func notification<C>(method: String, params: C) -> RPCRequest where C: Codable {
return RPCRequest(method: method, params: AnyCodable(params), id: nil)
}

static func notification(method: String) -> RPCRequest {
public static func notification(method: String) -> RPCRequest {
return RPCRequest(method: method, params: nil, id: nil)
}

Expand Down
10 changes: 9 additions & 1 deletion Sources/JSONRPC/RPCResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,22 @@ public struct RPCResponse: Equatable {
return nil
}

private let outcome: Result<AnyCodable, JSONRPCError>
public let outcome: Result<AnyCodable, JSONRPCError>

internal init(id: RPCID?, outcome: Result<AnyCodable, JSONRPCError>) {
self.jsonrpc = "2.0"
self.id = id
self.outcome = outcome
}

public init<C>(matchingRequest: RPCRequest, result: C) where C: Codable {
self.init(id: matchingRequest.id, outcome: .success(AnyCodable(result)))
}

public init(matchingRequest: RPCRequest, error: JSONRPCError) {
self.init(id: matchingRequest.id, outcome: .failure(error))
}

public init<C>(id: Int, result: C) where C: Codable {
self.init(id: RPCID(id), outcome: .success(AnyCodable(result)))
}
Expand Down
65 changes: 65 additions & 0 deletions Sources/WalletConnectRelay/RPC/Methods.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
struct Subscribe: RelayRPC {

struct Params: Codable {
let topic: String
}

let params: Params

var method: String {
"subscribe"
}
}

struct Unsubscribe: RelayRPC {

struct Params: Codable {
let id: String
let topic: String
}

let params: Params

var method: String {
"unsubscribe"
}
}

struct Publish: RelayRPC {

struct Params: Codable {
let topic: String
let message: String
let ttl: Int
let prompt: Bool?
let tag: Int?
}

let params: Params

var method: String {
"publish"
}
}

struct Subscription: RelayRPC {

struct Params: Codable {
struct Contents: Codable {
let topic: String
let message: String
}
let id: String
let data: Contents
}

let params: Params

var method: String {
"subscription"
}

init(id: String, topic: String, message: String) {
self.params = Params(id: id, data: Params.Contents(topic: topic, message: message))
}
}
5 changes: 5 additions & 0 deletions Sources/WalletConnectRelay/RPC/RPCMethod.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
protocol RPCMethod {
associatedtype Parameters
var method: String { get }
var params: Parameters { get }
}
38 changes: 38 additions & 0 deletions Sources/WalletConnectRelay/RPC/RelayRPC.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import JSONRPC

protocol RelayRPC: RPCMethod {}

extension RelayRPC where Parameters: Codable {

var idGenerator: IdentifierGenerator {
return WalletConnectRPCID()
}

func wrapToIridium() -> PrefixDecorator<Self> {
return PrefixDecorator(rpcMethod: self, prefix: "iridium")
}

func wrapToIRN() -> PrefixDecorator<Self> {
return PrefixDecorator(rpcMethod: self, prefix: "irn")
}
Comment on lines +11 to +17
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned before I do not like those wrappings.
I'd prefer concrete types like IrnPublish etc.
imho, it adds extra unnecessary complexity, couples RelayRPC to wrapToIridium/wrapToIRN and eventually it will leave some wrapping methods that are not in use.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already saw the the protocol be renamed twice, it's better to be prepared for any future changes. Ideally, the RelayClient calls should be refactored in a way that the wrapping is called in just one line, so it's easy to change it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to have a way to configure this in single place (not for every request).

After next protocol renaming is better to replace method than add another wrapToXXX method 😁


func asRPCRequest() -> RPCRequest {
RPCRequest(method: self.method, params: self.params, idGenerator: self.idGenerator)
}
}

struct PrefixDecorator<T>: RelayRPC where T: RelayRPC {

typealias Parameters = T.Parameters

let rpcMethod: T
let prefix: String

var method: String {
"\(prefix)_\(rpcMethod.method)"
}

var params: Parameters {
rpcMethod.params
}
}
11 changes: 11 additions & 0 deletions Sources/WalletConnectRelay/RPC/WalletConnectRPCID.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation
import JSONRPC

struct WalletConnectRPCID: IdentifierGenerator {

func next() -> RPCID {
let timestamp = Int64(Date().timeIntervalSince1970 * 1000) * 1000
let random = Int64.random(in: 0..<1000)
return .right(Int(timestamp + random))
}
}
Loading