Skip to content

Commit

Permalink
Merge pull request #514 from WalletConnect/#502-add-tll-prompt-to-req…
Browse files Browse the repository at this point in the history
…uests

[Networking] #502 Add tll and prompt to requests
  • Loading branch information
llbartekll authored Sep 27, 2022
2 parents c63004b + ac90e6f commit 245d100
Show file tree
Hide file tree
Showing 44 changed files with 319 additions and 306 deletions.
4 changes: 2 additions & 2 deletions Example/IntegrationTests/Relay/RelayClientEndToEndTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ final class RelayClientEndToEndTests: XCTestCase {
}.store(in: &publishers)

relayA.socketConnectionStatusPublisher.sink { _ in
relayA.publish(topic: randomTopic, payload: payloadA, tag: 0, onNetworkAcknowledge: { error in
relayA.publish(topic: randomTopic, payload: payloadA, tag: 0, prompt: false, ttl: 60, 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, tag: 0, onNetworkAcknowledge: { error in
relayB.publish(topic: randomTopic, payload: payloadB, tag: 0, prompt: false, ttl: 60, onNetworkAcknowledge: { error in
XCTAssertNil(error)
})
relayB.subscribe(topic: randomTopic) { error in
Expand Down
54 changes: 24 additions & 30 deletions Sources/Auth/AuthProtocolMethod.swift
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
import Foundation
import WalletConnectNetworking

enum AuthProtocolMethod: String, ProtocolMethod {
case authRequest = "wc_authRequest"
case pairingDelete = "wc_pairingDelete"
case pairingPing = "wc_pairingPing"

var method: String {
return self.rawValue
}

var requestTag: Int {
switch self {
case .authRequest:
return 3000
case .pairingDelete:
return 1000
case .pairingPing:
return 1002
}
}

var responseTag: Int {
switch self {
case .authRequest:
return 3001
case .pairingDelete:
return 1001
case .pairingPing:
return 1003
}
}
struct AuthRequestProtocolMethod: ProtocolMethod {
let method: String = "wc_authRequest"

let requestConfig = RelayConfig(tag: 3000, prompt: true, ttl: 86400)

let responseConfig = RelayConfig(tag: 3001, prompt: false, ttl: 86400)
}


struct PairingPingProtocolMethod: ProtocolMethod {
let method: String = "wc_pairingPing"

let requestConfig = RelayConfig(tag: 1002, prompt: false, ttl: 30)

let responseConfig = RelayConfig(tag: 1003, prompt: false, ttl: 30)
}


struct PairingDeleteProtocolMethod: ProtocolMethod {
let method: String = "wc_pairingDelete"

let requestConfig = RelayConfig(tag: 1000, prompt: false, ttl: 86400)

let responseConfig = RelayConfig(tag: 1001, prompt: false, ttl: 86400)
}
2 changes: 1 addition & 1 deletion Sources/Auth/Services/App/AppRequestService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ actor AppRequestService {
let request = RPCRequest(method: "wc_authRequest", params: params)
try kms.setPublicKey(publicKey: pubKey, for: responseTopic)
logger.debug("AppRequestService: Subscribibg for response topic: \(responseTopic)")
try await networkingInteractor.requestNetworkAck(request, topic: topic, tag: AuthProtocolMethod.authRequest.responseTag)
try await networkingInteractor.requestNetworkAck(request, topic: topic, protocolMethod: AuthRequestProtocolMethod())
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 @@ -32,13 +32,13 @@ class AppRespondSubscriber {
}

private func subscribeForResponse() {
networkingInteractor.responseErrorSubscription(on: AuthProtocolMethod.authRequest)
networkingInteractor.responseErrorSubscription(on: AuthRequestProtocolMethod())
.sink { [unowned self] (payload: ResponseSubscriptionErrorPayload<AuthRequestParams>) in
guard let error = AuthError(code: payload.error.code) else { return }
onResponse?(payload.id, .failure(error))
}.store(in: &publishers)

networkingInteractor.responseSubscription(on: AuthProtocolMethod.authRequest)
networkingInteractor.responseSubscription(on: AuthRequestProtocolMethod())
.sink { [unowned self] (payload: ResponseSubscriptionPayload<AuthRequestParams, Cacao>) in

activatePairingIfNeeded(id: payload.id)
Expand Down
5 changes: 3 additions & 2 deletions Sources/Auth/Services/Common/DeletePairingService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ class DeletePairingService {

func delete(topic: String) async throws {
guard pairingStorage.hasPairing(forTopic: topic) else { throw Errors.pairingNotFound}
let protocolMethod = PairingDeleteProtocolMethod()
let reason = AuthError.userDisconnected
logger.debug("Will delete pairing for reason: message: \(reason.message) code: \(reason.code)")
let request = RPCRequest(method: AuthProtocolMethod.pairingDelete.rawValue, params: reason)
try await networkingInteractor.request(request, topic: topic, tag: AuthProtocolMethod.pairingDelete.requestTag)
let request = RPCRequest(method: protocolMethod.method, params: reason)
try await networkingInteractor.request(request, topic: topic, protocolMethod: protocolMethod)
pairingStorage.delete(topic: topic)
kms.deleteSymmetricKey(for: topic)
networkingInteractor.unsubscribe(topic: topic)
Expand Down
3 changes: 1 addition & 2 deletions Sources/Auth/Services/Wallet/WalletErrorResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ actor WalletErrorResponder {

try kms.setAgreementSecret(keys, topic: topic)

let tag = AuthProtocolMethod.authRequest.responseTag
let envelopeType = Envelope.EnvelopeType.type1(pubKey: keys.publicKey.rawRepresentation)
try await networkingInteractor.respondError(topic: topic, requestId: requestId, tag: tag, reason: error, envelopeType: envelopeType)
try await networkingInteractor.respondError(topic: topic, requestId: requestId, protocolMethod: AuthRequestProtocolMethod(), reason: error, envelopeType: envelopeType)
}

private func getAuthRequestParams(requestId: RPCID) throws -> AuthRequestParams {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Auth/Services/Wallet/WalletRequestSubscriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class WalletRequestSubscriber {
private func subscribeForRequest() {
guard let address = address else { return }

networkingInteractor.requestSubscription(on: AuthProtocolMethod.authRequest)
networkingInteractor.requestSubscription(on: AuthRequestProtocolMethod())
.sink { [unowned self] (payload: RequestSubscriptionPayload<AuthRequestParams>) in
logger.debug("WalletRequestSubscriber: Received request")
guard let message = messageFormatter.formatMessage(from: payload.request.payloadParams, address: address) else {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Auth/Services/Wallet/WalletRespondService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ actor WalletRespondService {
let responseParams = AuthResponseParams(h: header, p: payload, s: signature)

let response = RPCResponse(id: requestId, result: responseParams)
try await networkingInteractor.respond(topic: topic, response: response, tag: AuthProtocolMethod.authRequest.responseTag, envelopeType: .type1(pubKey: keys.publicKey.rawRepresentation))
try await networkingInteractor.respond(topic: topic, response: response, protocolMethod: AuthRequestProtocolMethod(), envelopeType: .type1(pubKey: keys.publicKey.rawRepresentation))
}

func respondError(requestId: RPCID) async throws {
Expand Down
11 changes: 6 additions & 5 deletions Sources/Chat/ProtocolServices/Common/MessagingService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,28 @@ class MessagingService {

func send(topic: String, messageString: String) async throws {
// TODO - manage author account
let protocolMethod = ChatMessageProtocolMethod()
let thread = await threadStore.first {$0.topic == topic}
guard let authorAccount = thread?.selfAccount else { throw Errors.threadDoNotExist}
let timestamp = Int64(Date().timeIntervalSince1970 * 1000)
let message = Message(topic: topic, message: messageString, authorAccount: authorAccount, timestamp: timestamp)
let request = RPCRequest(method: ChatProtocolMethod.message.method, params: message)
try await networkingInteractor.request(request, topic: topic, tag: ChatProtocolMethod.message.requestTag)
let request = RPCRequest(method: protocolMethod.method, params: message)
try await networkingInteractor.request(request, topic: topic, protocolMethod: protocolMethod)
Task(priority: .background) {
await messagesStore.add(message)
onMessage?(message)
}
}

private func setUpResponseHandling() {
networkingInteractor.responseSubscription(on: ChatProtocolMethod.message)
networkingInteractor.responseSubscription(on: ChatMessageProtocolMethod())
.sink { [unowned self] (payload: ResponseSubscriptionPayload<AnyCodable, AnyCodable>) in
logger.debug("Received Message response")
}.store(in: &publishers)
}

private func setUpRequestHandling() {
networkingInteractor.requestSubscription(on: ChatProtocolMethod.message)
networkingInteractor.requestSubscription(on: ChatMessageProtocolMethod())
.sink { [unowned self] (payload: RequestSubscriptionPayload<Message>) in
var message = payload.request
message.topic = payload.topic
Expand All @@ -59,7 +60,7 @@ class MessagingService {

private func handleMessage(_ message: Message, topic: String, requestId: RPCID) {
Task(priority: .background) {
try await networkingInteractor.respondSuccess(topic: topic, requestId: requestId, tag: ChatProtocolMethod.message.responseTag)
try await networkingInteractor.respondSuccess(topic: topic, requestId: requestId, protocolMethod: ChatMessageProtocolMethod())
await messagesStore.add(message)
logger.debug("Received message")
onMessage?(message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class InvitationHandlingService {
}

func accept(inviteId: String) async throws {
let protocolMethod = ChatInviteProtocolMethod()

guard let payload = try invitePayloadStore.get(key: inviteId) else { throw Error.inviteForIdNotFound }

let selfThreadPubKey = try kms.createX25519KeyPair()
Expand All @@ -47,7 +49,7 @@ class InvitationHandlingService {

let response = RPCResponse(id: payload.id, result: inviteResponse)
let responseTopic = try getInviteResponseTopic(requestTopic: payload.topic, invite: payload.request)
try await networkingInteractor.respond(topic: responseTopic, response: response, tag: ChatProtocolMethod.invite.responseTag)
try await networkingInteractor.respond(topic: responseTopic, response: response, protocolMethod: protocolMethod)

let threadAgreementKeys = try kms.performKeyAgreement(selfPublicKey: selfThreadPubKey, peerPublicKey: payload.request.publicKey)
let threadTopic = threadAgreementKeys.derivedTopic()
Expand All @@ -71,13 +73,13 @@ class InvitationHandlingService {

let responseTopic = try getInviteResponseTopic(requestTopic: payload.topic, invite: payload.request)

try await networkingInteractor.respondError(topic: responseTopic, requestId: payload.id, tag: ChatProtocolMethod.invite.responseTag, reason: ChatError.userRejected)
try await networkingInteractor.respondError(topic: responseTopic, requestId: payload.id, protocolMethod: ChatInviteProtocolMethod(), reason: ChatError.userRejected)

invitePayloadStore.delete(forKey: inviteId)
}

private func setUpRequestHandling() {
networkingInteractor.requestSubscription(on: ChatProtocolMethod.invite)
networkingInteractor.requestSubscription(on: ChatInviteProtocolMethod())
.sink { [unowned self] (payload: RequestSubscriptionPayload<Invite>) in
logger.debug("did receive an invite")
invitePayloadStore.set(payload, forKey: payload.request.publicKey)
Expand Down
7 changes: 4 additions & 3 deletions Sources/Chat/ProtocolServices/Inviter/InviteService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class InviteService {

func invite(peerPubKey: String, peerAccount: Account, openingMessage: String, account: Account) async throws {
// TODO ad storage
let protocolMethod = ChatInviteProtocolMethod()
self.peerAccount = peerAccount
let selfPubKeyY = try kms.createX25519KeyPair()
let invite = Invite(message: openingMessage, account: account, publicKey: selfPubKeyY.hexRepresentation)
Expand All @@ -42,21 +43,21 @@ class InviteService {
// overrides on invite toipic
try kms.setSymmetricKey(symKeyI.sharedKey, for: inviteTopic)

let request = RPCRequest(method: ChatProtocolMethod.invite.method, params: invite)
let request = RPCRequest(method: protocolMethod.method, params: invite)

// 2. Proposer subscribes to topic R which is the hash of the derived symKey
let responseTopic = symKeyI.derivedTopic()

try kms.setSymmetricKey(symKeyI.sharedKey, for: responseTopic)

try await networkingInteractor.subscribe(topic: responseTopic)
try await networkingInteractor.request(request, topic: inviteTopic, tag: ChatProtocolMethod.invite.requestTag, envelopeType: .type1(pubKey: selfPubKeyY.rawRepresentation))
try await networkingInteractor.request(request, topic: inviteTopic, protocolMethod: protocolMethod, envelopeType: .type1(pubKey: selfPubKeyY.rawRepresentation))

logger.debug("invite sent on topic: \(inviteTopic)")
}

private func setUpResponseHandling() {
networkingInteractor.responseSubscription(on: ChatProtocolMethod.invite)
networkingInteractor.responseSubscription(on: ChatInviteProtocolMethod())
.sink { [unowned self] (payload: ResponseSubscriptionPayload<Invite, InviteResponse>) in
logger.debug("Invite has been accepted")

Expand Down
46 changes: 16 additions & 30 deletions Sources/Chat/Types/ChatProtocolMethod.swift
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
import Foundation
import WalletConnectNetworking

enum ChatProtocolMethod: ProtocolMethod {
case invite
case message

var requestTag: Int {
switch self {
case .invite:
return 2000
case .message:
return 2002
}
}

var responseTag: Int {
switch self {
case .invite:
return 2001
case .message:
return 2003
}
}

var method: String {
switch self {
case .invite:
return "wc_chatInvite"
case .message:
return "wc_chatMessage"
}
}
struct ChatInviteProtocolMethod: ProtocolMethod {
let method: String = "wc_chatInvite"

let requestConfig = RelayConfig(tag: 2000, prompt: true, ttl: 86400)

let responseConfig = RelayConfig(tag: 2001, prompt: false, ttl: 86400)

}

struct ChatMessageProtocolMethod: ProtocolMethod {
let method: String = "wc_chatMessage"

let requestConfig = RelayConfig(tag: 2002, prompt: true, ttl: 86400)

let responseConfig = RelayConfig(tag: 2003, prompt: false, ttl: 86400)

}
26 changes: 13 additions & 13 deletions Sources/WalletConnectNetworking/NetworkInteracting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ public protocol NetworkInteracting {
var socketConnectionStatusPublisher: AnyPublisher<SocketConnectionStatus, Never> { get }
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
func respondSuccess(topic: String, requestId: RPCID, tag: Int, envelopeType: Envelope.EnvelopeType) async throws
func respondError(topic: String, requestId: RPCID, tag: Int, reason: Reason, envelopeType: Envelope.EnvelopeType) async throws
func request(_ request: RPCRequest, topic: String, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws
func requestNetworkAck(_ request: RPCRequest, topic: String, protocolMethod: ProtocolMethod) async throws
func respond(topic: String, response: RPCResponse, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws
func respondSuccess(topic: String, requestId: RPCID, protocolMethod: ProtocolMethod, envelopeType: Envelope.EnvelopeType) async throws
func respondError(topic: String, requestId: RPCID, protocolMethod: ProtocolMethod, reason: Reason, envelopeType: Envelope.EnvelopeType) async throws

func requestSubscription<Request: Codable>(
on request: ProtocolMethod
Expand All @@ -28,19 +28,19 @@ public protocol NetworkInteracting {
}

extension NetworkInteracting {
public func request(_ request: RPCRequest, topic: String, tag: Int) async throws {
try await self.request(request, topic: topic, tag: tag, envelopeType: .type0)
public func request(_ request: RPCRequest, topic: String, protocolMethod: ProtocolMethod) async throws {
try await self.request(request, topic: topic, protocolMethod: protocolMethod, envelopeType: .type0)
}

public func respond(topic: String, response: RPCResponse, tag: Int) async throws {
try await self.respond(topic: topic, response: response, tag: tag, envelopeType: .type0)
public func respond(topic: String, response: RPCResponse, protocolMethod: ProtocolMethod) async throws {
try await self.respond(topic: topic, response: response, protocolMethod: protocolMethod, envelopeType: .type0)
}

public func respondSuccess(topic: String, requestId: RPCID, tag: Int) async throws {
try await self.respondSuccess(topic: topic, requestId: requestId, tag: tag, envelopeType: .type0)
public func respondSuccess(topic: String, requestId: RPCID, protocolMethod: ProtocolMethod) async throws {
try await self.respondSuccess(topic: topic, requestId: requestId, protocolMethod: protocolMethod, envelopeType: .type0)
}

public func respondError(topic: String, requestId: RPCID, tag: Int, reason: Reason) async throws {
try await self.respondError(topic: topic, requestId: requestId, tag: tag, reason: reason, envelopeType: .type0)
public func respondError(topic: String, requestId: RPCID, protocolMethod: ProtocolMethod, reason: Reason) async throws {
try await self.respondError(topic: topic, requestId: requestId, protocolMethod: protocolMethod, reason: reason, envelopeType: .type0)
}
}
Loading

0 comments on commit 245d100

Please sign in to comment.