-
Notifications
You must be signed in to change notification settings - Fork 191
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
Changes from all commits
043c1ce
caf3809
9e86741
d8d8564
84fb09d
ccacb2f
527fb66
4c6a553
156e32b
ad826a6
a9fc882
ccd153f
9b7f82f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)) | ||
} | ||
} |
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 } | ||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I mentioned before I do not like those wrappings. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
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 | ||
} | ||
} |
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)) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed it