Skip to content

Commit

Permalink
Merge pull request #443 from WalletConnect/develop_main
Browse files Browse the repository at this point in the history
Develop main
  • Loading branch information
llbartekll authored Aug 17, 2022
2 parents 5d25aa3 + 851bc66 commit 3e83bec
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 7 deletions.
24 changes: 24 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Description

<!--
Please include:
* summary of the changes and the related issue
* relevant motivation and context
-->

Resolves # (issue)

## How Has This Been Tested?

<!--
Please:
* describe the tests that you ran to verify your changes.
* provide instructions so we can reproduce.
-->

<!-- If valid for smoke test on feature add screenshots -->

## Due Dilligence

* [ ] Breaking change
* [ ] Requires a documentation update
2 changes: 1 addition & 1 deletion Sources/Auth/Services/App/AppRequestService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ actor AppRequestService {
let payload = AuthPayload(requestParams: params, iat: issueAt)
let params = AuthRequestParams(requester: requester, payloadParams: payload)
let request = RPCRequest(method: "wc_authRequest", params: params)
try await networkingInteractor.request(request, topic: topic, tag: AuthRequestParams.tag)
try await networkingInteractor.requestNetworkAck(request, topic: topic, tag: AuthRequestParams.tag)
try await networkingInteractor.subscribe(topic: responseTopic)
}
}
4 changes: 2 additions & 2 deletions Sources/Auth/Services/App/AppRespondSubscriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ class AppRespondSubscriber {
private func subscribeForResponse() {
networkingInteractor.responsePublisher.sink { [unowned self] subscriptionPayload in
guard
let requestId = subscriptionPayload.request.id,
let requestId = subscriptionPayload.response.id,
let request = rpcHistory.get(recordId: requestId)?.request,
let requestParams = request.params, request.method == "wc_authRequest"
else { return }

networkingInteractor.unsubscribe(topic: subscriptionPayload.topic)

do {
guard let cacao = try subscriptionPayload.request.result?.get(Cacao.self) else {
guard let cacao = try subscriptionPayload.response.result?.get(Cacao.self) else {
return logger.debug("Malformed auth response params")
}

Expand Down
60 changes: 60 additions & 0 deletions Sources/Auth/Services/Common/NetworkingInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ protocol NetworkInteracting {
func subscribe(topic: String) async throws
func unsubscribe(topic: String)
func request(_ request: RPCRequest, topic: String, tag: Int, envelopeType: Envelope.EnvelopeType) async throws
func requestNetworkAck(_ request: RPCRequest, topic: String, tag: Int) async throws
func respond(topic: String, response: RPCResponse, tag: Int, envelopeType: Envelope.EnvelopeType) async throws
}

Expand All @@ -24,6 +25,7 @@ class NetworkingInteractor: NetworkInteracting {
private let relayClient: RelayClient
private let serializer: Serializing
private let rpcHistory: RPCHistory
private let logger: ConsoleLogging
var requestPublisher: AnyPublisher<RequestSubscriptionPayload, Never> {
requestPublisherSubject.eraseToAnyPublisher()
}
Expand All @@ -32,13 +34,20 @@ class NetworkingInteractor: NetworkInteracting {
responsePublisherSubject.eraseToAnyPublisher()
}
private let responsePublisherSubject = PassthroughSubject<ResponseSubscriptionPayload, Never>()
var socketConnectionStatusPublisher: AnyPublisher<SocketConnectionStatus, Never>

init(relayClient: RelayClient,
serializer: Serializing,
logger: ConsoleLogging,
rpcHistory: RPCHistory) {
self.relayClient = relayClient
self.serializer = serializer
self.rpcHistory = rpcHistory
self.logger = logger
self.socketConnectionStatusPublisher = relayClient.socketConnectionStatusPublisher
relayClient.onMessage = { [unowned self] topic, message in
manageSubscription(topic, message)
}
}

func subscribe(topic: String) async throws {
Expand All @@ -55,9 +64,60 @@ class NetworkingInteractor: NetworkInteracting {
try await relayClient.publish(topic: topic, payload: message, tag: tag)
}

/// Completes with an acknowledgement from the relay network.
/// completes with error if networking client was not able to send a message
/// TODO - relay client should provide async function - continualion should be removed from here
func requestNetworkAck(_ request: RPCRequest, topic: String, tag: Int) async throws {
do {
try rpcHistory.set(request, forTopic: topic, emmitedBy: .local)
let message = try serializer.serialize(topic: topic, encodable: request)
return try await withCheckedThrowingContinuation { continuation in
relayClient.publish(topic: topic, payload: message, tag: tag) { error in
if let error = error {
continuation.resume(throwing: error)
} else {
continuation.resume()
}
}
}
} catch {
logger.error(error)
}
}

func respond(topic: String, response: RPCResponse, tag: Int, envelopeType: Envelope.EnvelopeType) async throws {
try rpcHistory.resolve(response)
let message = try! serializer.serialize(topic: topic, encodable: response, envelopeType: envelopeType)
try await relayClient.publish(topic: topic, payload: message, tag: tag)
}

private func manageSubscription(_ topic: String, _ encodedEnvelope: String) {
if let deserializedJsonRpcRequest: RPCRequest = serializer.tryDeserialize(topic: topic, encodedEnvelope: encodedEnvelope) {
handleRequest(topic: topic, request: deserializedJsonRpcRequest)
} else if let deserializedJsonRpcResponse: RPCResponse = serializer.tryDeserialize(topic: topic, encodedEnvelope: encodedEnvelope) {
handleResponse(response: deserializedJsonRpcResponse)
} else {
logger.debug("Networking Interactor - Received unknown object type from networking relay")
}
}

private func handleRequest(topic: String, request: RPCRequest) {
do {
try rpcHistory.set(request, forTopic: topic, emmitedBy: .remote)
let payload = RequestSubscriptionPayload(topic: topic, request: request)
requestPublisherSubject.send(payload)
} catch {
logger.debug(error)
}
}

private func handleResponse(response: RPCResponse) {
do {
try rpcHistory.resolve(response)
let record = rpcHistory.get(recordId: response.id!)!
responsePublisherSubject.send(ResponseSubscriptionPayload(topic: record.topic, response: response))
} catch {
logger.debug("Handle json rpc response error: \(error)")
}
}
}
2 changes: 1 addition & 1 deletion Sources/Auth/Types/ResponseSubscriptionPayload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import JSONRPC

struct ResponseSubscriptionPayload: Codable, Equatable {
let topic: String
let request: RPCResponse
let response: RPCResponse
}
6 changes: 3 additions & 3 deletions Sources/WalletConnectUtils/RPCHistory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ public final class RPCHistory {
case local
case remote
}
let id: RPCID
let topic: String
public let id: RPCID
public let topic: String
let origin: Origin
public let request: RPCRequest
var response: RPCResponse?
public var response: RPCResponse?
}

enum HistoryError: Error {
Expand Down
4 changes: 4 additions & 0 deletions Tests/AuthTests/Mocks/NetworkingInteractorMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@ struct NetworkingInteractorMock: NetworkInteracting {

}

func requestNetworkAck(_ request: RPCRequest, topic: String, tag: Int) async throws {

}

}

0 comments on commit 3e83bec

Please sign in to comment.