Skip to content

Commit

Permalink
[Network] Feature: #297 Message Tag (#300)
Browse files Browse the repository at this point in the history
* Introduces tag parameter in publish calls

* Fixed unit tests build

* Fix typo

* Fixed broken tests

Co-authored-by: André Vants <andrevants@icloud.com>
  • Loading branch information
André Vants and André Vants authored Jul 4, 2022
1 parent ea32675 commit f8e855b
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Example/IntegrationTests/Relay/RelayClientEndToEndTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ final class RelayClientEndToEndTests: XCTestCase {
expectationB.fulfill()
}
relayA.socketConnectionStatusPublisher.sink { _ in
relayA.publish(topic: randomTopic, payload: payloadA, onNetworkAcknowledge: { error in
relayA.publish(topic: randomTopic, payload: payloadA, tag: .unknown, onNetworkAcknowledge: { error in
XCTAssertNil(error)
})
relayA.subscribe(topic: randomTopic) { error in
XCTAssertNil(error)
}
}.store(in: &publishers)
relayB.socketConnectionStatusPublisher.sink { _ in
relayB.publish(topic: randomTopic, payload: payloadB, onNetworkAcknowledge: { error in
relayB.publish(topic: randomTopic, payload: payloadB, tag: .unknown, onNetworkAcknowledge: { error in
XCTAssertNil(error)
})
relayB.subscribe(topic: randomTopic) { error in
Expand Down
6 changes: 3 additions & 3 deletions Sources/Chat/NetworkingInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ class NetworkingInteractor: NetworkInteracting {
func requestUnencrypted(_ request: JSONRPCRequest<ChatRequestParams>, topic: String) async throws {
try jsonRpcHistory.set(topic: topic, request: request)
let message = try! request.json()
try await relayClient.publish(topic: topic, payload: message)
try await relayClient.publish(topic: topic, payload: message, tag: .chat)
}

func request(_ request: JSONRPCRequest<ChatRequestParams>, topic: String, envelopeType: Envelope.EnvelopeType) async throws {
try jsonRpcHistory.set(topic: topic, request: request)
let message = try! serializer.serialize(topic: topic, encodable: request, envelopeType: envelopeType)
try await relayClient.publish(topic: topic, payload: message)
try await relayClient.publish(topic: topic, payload: message, tag: .chat)
}

func respond(topic: String, response: JsonRpcResult) async throws {
let message = try serializer.serialize(topic: topic, encodable: response.value)
try await relayClient.publish(topic: topic, payload: message, prompt: false)
try await relayClient.publish(topic: topic, payload: message, tag: .chat, prompt: false)
}

func subscribe(topic: String) async throws {
Expand Down
5 changes: 5 additions & 0 deletions Sources/WalletConnectRelay/PublishTag.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public enum PublishTag: Int {
case unknown = 0
case sign
case chat
}
7 changes: 4 additions & 3 deletions Sources/WalletConnectRelay/RelayClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public final class RelayClient {
}

/// Completes when networking client sends a request, error if it fails on client side
public func publish(topic: String, payload: String, prompt: Bool = false) async throws {
let params = RelayJSONRPC.PublishParams(topic: topic, message: payload, ttl: defaultTtl, prompt: prompt)
public func publish(topic: String, payload: String, tag: PublishTag, prompt: Bool = false) async throws {
let params = RelayJSONRPC.PublishParams(topic: topic, message: payload, ttl: defaultTtl, prompt: prompt, tag: tag.rawValue)
let request = JSONRPCRequest<RelayJSONRPC.PublishParams>(method: RelayJSONRPC.Method.publish.method, params: params)
logger.debug("Publishing Payload on Topic: \(topic)")
let requestJson = try request.json()
Expand All @@ -107,9 +107,10 @@ public final class RelayClient {
@discardableResult public func publish(
topic: String,
payload: String,
tag: PublishTag,
prompt: Bool = false,
onNetworkAcknowledge: @escaping ((Error?) -> Void)) -> Int64 {
let params = RelayJSONRPC.PublishParams(topic: topic, message: payload, ttl: defaultTtl, prompt: prompt)
let params = RelayJSONRPC.PublishParams(topic: topic, message: payload, ttl: defaultTtl, prompt: prompt, tag: tag.rawValue)
let request = JSONRPCRequest<RelayJSONRPC.PublishParams>(method: RelayJSONRPC.Method.publish.method, params: params)
let requestJson = try! request.json()
logger.debug("iridium: Publishing Payload on Topic: \(topic)")
Expand Down
1 change: 1 addition & 0 deletions Sources/WalletConnectRelay/RelayJSONRPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ enum RelayJSONRPC {
let message: String
let ttl: Int
let prompt: Bool?
let tag: Int?
}

struct SubscribeParams: Codable, Equatable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class NetworkInteractor: NetworkInteracting {
try jsonRpcHistory.set(topic: topic, request: payload, chainId: getChainId(payload))
let message = try serializer.serialize(topic: topic, encodable: payload)
let prompt = shouldPrompt(payload.method)
try await relayClient.publish(topic: topic, payload: message, prompt: prompt)
try await relayClient.publish(topic: topic, payload: message, tag: .sign, prompt: prompt)
}

func requestPeerResponse(_ wcMethod: WCMethod, onTopic topic: String, completion: ((Result<JSONRPCResponse<AnyCodable>, JSONRPCErrorResponse>) -> Void)?) {
Expand All @@ -80,7 +80,7 @@ class NetworkInteractor: NetworkInteracting {
try jsonRpcHistory.set(topic: topic, request: payload, chainId: getChainId(payload))
let message = try serializer.serialize(topic: topic, encodable: payload)
let prompt = shouldPrompt(payload.method)
relayClient.publish(topic: topic, payload: message, prompt: prompt) { [weak self] error in
relayClient.publish(topic: topic, payload: message, tag: .sign, prompt: prompt) { [weak self] error in
guard let self = self else {return}
if let error = error {
self.logger.error(error)
Expand Down Expand Up @@ -116,7 +116,7 @@ class NetworkInteractor: NetworkInteracting {
try jsonRpcHistory.set(topic: topic, request: payload, chainId: getChainId(payload))
let message = try serializer.serialize(topic: topic, encodable: payload)
let prompt = shouldPrompt(payload.method)
relayClient.publish(topic: topic, payload: message, prompt: prompt) { error in
relayClient.publish(topic: topic, payload: message, tag: .sign, prompt: prompt) { error in
completion(error)
}
} catch WalletConnectError.internal(.jsonRpcDuplicateDetected) {
Expand All @@ -133,7 +133,7 @@ class NetworkInteractor: NetworkInteracting {
logger.debug("Responding....topic: \(topic)")

do {
try await relayClient.publish(topic: topic, payload: message, prompt: false)
try await relayClient.publish(topic: topic, payload: message, tag: .sign, prompt: false)
} catch WalletConnectError.internal(.jsonRpcDuplicateDetected) {
logger.info("Info: Json Rpc Duplicate Detected")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ protocol NetworkRelaying {
var socketConnectionStatusPublisher: AnyPublisher<SocketConnectionStatus, Never> { get }
func connect() throws
func disconnect(closeCode: URLSessionWebSocketTask.CloseCode) throws
func publish(topic: String, payload: String, prompt: Bool) async throws
func publish(topic: String, payload: String, tag: PublishTag, prompt: Bool) async throws
/// - returns: request id
@discardableResult func publish(topic: String, payload: String, prompt: Bool, onNetworkAcknowledge: @escaping ((Error?) -> Void)) -> Int64
@discardableResult func publish(topic: String, payload: String, tag: PublishTag, prompt: Bool, onNetworkAcknowledge: @escaping ((Error?) -> Void)) -> Int64
func subscribe(topic: String, completion: @escaping (Error?) -> Void)
func subscribe(topic: String) async throws
/// - returns: request id
Expand Down
4 changes: 2 additions & 2 deletions Tests/RelayerTests/IridiumRelayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class IridiumRelayTests: XCTestCase {

func testPublishRequestAcknowledge() {
let acknowledgeExpectation = expectation(description: "completion with no error on iridium request acknowledge after publish")
let requestId = iridiumRelay.publish(topic: "", payload: "{}", onNetworkAcknowledge: { error in
let requestId = iridiumRelay.publish(topic: "", payload: "{}", tag: .unknown, onNetworkAcknowledge: { error in
acknowledgeExpectation.fulfill()
XCTAssertNil(error)
})
Expand Down Expand Up @@ -72,7 +72,7 @@ class IridiumRelayTests: XCTestCase {
}

func testSendOnPublish() {
iridiumRelay.publish(topic: "", payload: "", onNetworkAcknowledge: { _ in})
iridiumRelay.publish(topic: "", payload: "", tag: .unknown, onNetworkAcknowledge: { _ in})
XCTAssertTrue(dispatcher.sent)
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/WalletConnectSignTests/Mocks/MockedRelayClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ class MockedRelayClient: NetworkRelaying {
socketConnectionStatusPublisherSubject.eraseToAnyPublisher()
}

func publish(topic: String, payload: String, prompt: Bool) async throws {
func publish(topic: String, payload: String, tag: PublishTag, prompt: Bool) async throws {
self.prompt = prompt
}

var onMessage: ((String, String) -> Void)?
var error: Error?
var prompt = false
func publish(topic: String, payload: String, prompt: Bool, onNetworkAcknowledge: @escaping ((Error?) -> Void)) -> Int64 {
func publish(topic: String, payload: String, tag: PublishTag, prompt: Bool, onNetworkAcknowledge: @escaping ((Error?) -> Void)) -> Int64 {
self.prompt = prompt
onNetworkAcknowledge(error)
return 0
Expand Down

0 comments on commit f8e855b

Please sign in to comment.