-
Notifications
You must be signed in to change notification settings - Fork 172
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
[Chat] feature/#244-create thread #266
Merged
Merged
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
46aac86
Add registry manager
llbartekll ff48341
Update api methods
llbartekll bf61703
update resolve
llbartekll 57c872d
Add description to api methods
llbartekll f30f1f7
update invite method
llbartekll ce2d6d6
remove teardown
llbartekll 2e61a6d
make registryStore private
llbartekll 2d47389
update new thread e2e test
llbartekll 8826ab4
Add invite service
llbartekll 53e8963
savepoint
llbartekll 9a26d1a
Update invite service
llbartekll 61db474
Add invitation handling
llbartekll a05524e
test - decrypts an invite
llbartekll 3f39bf6
savepoint
llbartekll 95488fd
Accept and create a Thread on Invitee
llbartekll 930851f
savepoint
llbartekll 1ddaf69
add json rpc history
llbartekll a423533
savepoint
llbartekll 10c9d2a
remove ChatRequest type
llbartekll f26b7fb
create a Thread on both peers
llbartekll 0f67452
clean up
llbartekll 642155d
Merge branch 'develop' of github.com:WalletConnect/WalletConnectSwift…
llbartekll 568cddd
Merge branch 'develop' of github.com:WalletConnect/WalletConnectSwift…
llbartekll 529bfd5
swiftlint fix
llbartekll File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,23 @@ import WalletConnectRelay | |
import WalletConnectUtils | ||
|
||
protocol NetworkInteracting { | ||
func subscribe(topic: String) async throws | ||
var requestPublisher: AnyPublisher<RequestSubscriptionPayload, Never> {get} | ||
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. run 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. done |
||
var responsePublisher: AnyPublisher<ChatResponse, Never> {get} | ||
func subscribe(topic: String) async throws | ||
func requestUnencrypted(_ request: JSONRPCRequest<ChatRequestParams>, topic: String) async throws | ||
func request(_ request: JSONRPCRequest<ChatRequestParams>, topic: String) async throws | ||
func respond(topic: String, response: JsonRpcResult) async throws | ||
|
||
} | ||
|
||
class NetworkingInteractor: NetworkInteracting { | ||
let relayClient: RelayClient | ||
enum Error: Swift.Error { | ||
case failedToInitialiseMethodFromRecord | ||
} | ||
private let jsonRpcHistory: JsonRpcHistory<ChatRequestParams> | ||
private let serializer: Serializing | ||
private let relayClient: RelayClient | ||
private let logger: ConsoleLogging | ||
var requestPublisher: AnyPublisher<RequestSubscriptionPayload, Never> { | ||
requestPublisherSubject.eraseToAnyPublisher() | ||
} | ||
|
@@ -20,39 +31,47 @@ class NetworkingInteractor: NetworkInteracting { | |
responsePublisherSubject.eraseToAnyPublisher() | ||
} | ||
private let responsePublisherSubject = PassthroughSubject<ChatResponse, Never>() | ||
|
||
init(relayClient: RelayClient, | ||
serializer: Serializing) { | ||
serializer: Serializing, | ||
logger: ConsoleLogging, | ||
jsonRpcHistory: JsonRpcHistory<ChatRequestParams> | ||
) { | ||
self.relayClient = relayClient | ||
self.serializer = serializer | ||
|
||
self.jsonRpcHistory = jsonRpcHistory | ||
self.logger = logger | ||
relayClient.onMessage = { [unowned self] topic, message in | ||
manageSubscription(topic, message) | ||
} | ||
} | ||
|
||
func requestUnencrypted(_ request: ChatRequest, topic: String) { | ||
// TODO - remove the method | ||
func requestUnencrypted(_ request: JSONRPCRequest<ChatRequestParams>, topic: String) async throws { | ||
try jsonRpcHistory.set(topic: topic, request: request) | ||
let message = try! request.json() | ||
relayClient.publish(topic: topic, payload: message) {error in | ||
// print(error) | ||
} | ||
try await relayClient.publish(topic: topic, payload: message) | ||
} | ||
|
||
func request(_ request: ChatRequest, topic: String) { | ||
func request(_ request: JSONRPCRequest<ChatRequestParams>, topic: String) async throws { | ||
try jsonRpcHistory.set(topic: topic, request: request) | ||
let message = try! serializer.serialize(topic: topic, encodable: request) | ||
relayClient.publish(topic: topic, payload: message) {error in | ||
// print(error) | ||
} | ||
try await relayClient.publish(topic: topic, payload: message) | ||
} | ||
|
||
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) | ||
} | ||
|
||
func subscribe(topic: String) async throws { | ||
try await relayClient.subscribe(topic: topic) | ||
} | ||
|
||
private func manageSubscription(_ topic: String, _ message: String) { | ||
if let deserializedJsonRpcRequest: ChatRequest = serializer.tryDeserialize(topic: topic, message: message) { | ||
if let deserializedJsonRpcRequest: JSONRPCRequest<ChatRequestParams> = serializer.tryDeserialize(topic: topic, message: message) { | ||
handleWCRequest(topic: topic, request: deserializedJsonRpcRequest) | ||
} else if let decodedJsonRpcRequest: ChatRequest = tryDecodeRequest(message: message) { | ||
} else if let decodedJsonRpcRequest: JSONRPCRequest<ChatRequestParams> = tryDecodeRequest(message: message) { | ||
handleWCRequest(topic: topic, request: decodedJsonRpcRequest) | ||
|
||
} else if let deserializedJsonRpcResponse: JSONRPCResponse<AnyCodable> = serializer.tryDeserialize(topic: topic, message: message) { | ||
|
@@ -65,20 +84,31 @@ class NetworkingInteractor: NetworkInteracting { | |
} | ||
|
||
|
||
private func tryDecodeRequest(message: String) -> ChatRequest? { | ||
private func tryDecodeRequest(message: String) -> JSONRPCRequest<ChatRequestParams>? { | ||
guard let messageData = message.data(using: .utf8) else { | ||
return nil | ||
} | ||
return try? JSONDecoder().decode(ChatRequest.self, from: messageData) | ||
return try? JSONDecoder().decode(JSONRPCRequest<ChatRequestParams>.self, from: messageData) | ||
} | ||
|
||
private func handleWCRequest(topic: String, request: ChatRequest) { | ||
private func handleWCRequest(topic: String, request: JSONRPCRequest<ChatRequestParams>) { | ||
let payload = RequestSubscriptionPayload(topic: topic, request: request) | ||
requestPublisherSubject.send(payload) | ||
} | ||
|
||
private func handleJsonRpcResponse(response: JSONRPCResponse<AnyCodable>) { | ||
//todo | ||
do { | ||
let record = try jsonRpcHistory.resolve(response: JsonRpcResult.response(response)) | ||
let params = try record.request.params.get(ChatRequestParams.self) | ||
let chatResponse = ChatResponse( | ||
topic: record.topic, | ||
requestMethod: record.request.method, | ||
requestParams: params, | ||
result: JsonRpcResult.response(response)) | ||
responsePublisherSubject.send(chatResponse) | ||
} catch { | ||
logger.debug("Handle json rpc response error: \(error)") | ||
} | ||
} | ||
|
||
private func handleJsonRpcErrorResponse(response: JSONRPCErrorResponse) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
|
||
import Foundation |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I like ***Service naming, let's use more often