From 878cd602722ac5ccc45b09556cc1b765ee63187d Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Tue, 15 Feb 2022 13:11:41 +0100 Subject: [PATCH 1/3] add prompt flag, fix tests --- .../xcschemes/WalletConnect.xcscheme | 40 +------------------ Sources/Relayer/RelayJSONRPC.swift | 1 + Sources/Relayer/Relayer.swift | 10 +++-- .../WalletConnect/Relay/NetworkRelaying.swift | 2 +- .../Relay/WalletConnectRelay.swift | 14 ++++++- Tests/IntegrationTests/ClientTest.swift | 4 +- Tests/RelayerTests/RelayerEndToEndTests.swift | 10 +++-- .../Mocks/MockedNetworkRelayer.swift | 2 +- 8 files changed, 31 insertions(+), 52 deletions(-) diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/WalletConnect.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/WalletConnect.xcscheme index 5bb6db710..5b80153d8 100644 --- a/.swiftpm/xcode/xcshareddata/xcschemes/WalletConnect.xcscheme +++ b/.swiftpm/xcode/xcshareddata/xcschemes/WalletConnect.xcscheme @@ -76,34 +76,6 @@ ReferencedContainer = "container:"> - - - - - - - - - - - - + skipped = "YES"> ())) -> Int64 { - let params = RelayJSONRPC.PublishParams(topic: topic, message: payload, ttl: defaultTtl) + @discardableResult public func publish( + topic: String, + payload: String, + prompt: Bool = false, + completion: @escaping ((Error?) -> ())) -> Int64 { + let params = RelayJSONRPC.PublishParams(topic: topic, message: payload, ttl: defaultTtl, prompt: prompt) let request = JSONRPCRequest(method: RelayJSONRPC.Method.publish.rawValue, params: params) let requestJson = try! request.json() logger.debug("waku: Publishing Payload on Topic: \(topic)") @@ -209,7 +213,7 @@ public final class Relayer { } } - static private func makeRelayUrl(host: String, projectId: String) -> URL { + static func makeRelayUrl(host: String, projectId: String) -> URL { var components = URLComponents() components.scheme = "wss" components.host = host diff --git a/Sources/WalletConnect/Relay/NetworkRelaying.swift b/Sources/WalletConnect/Relay/NetworkRelaying.swift index 93f9a2b9b..b8139cdde 100644 --- a/Sources/WalletConnect/Relay/NetworkRelaying.swift +++ b/Sources/WalletConnect/Relay/NetworkRelaying.swift @@ -10,7 +10,7 @@ protocol NetworkRelaying { func connect() throws func disconnect(closeCode: URLSessionWebSocketTask.CloseCode) throws /// - returns: request id - @discardableResult func publish(topic: String, payload: String, completion: @escaping ((Error?)->())) -> Int64 + @discardableResult func publish(topic: String, payload: String, prompt: Bool, completion: @escaping ((Error?)->())) -> Int64 /// - returns: request id @discardableResult func subscribe(topic: String, completion: @escaping (Error?)->()) -> Int64 /// - returns: request id diff --git a/Sources/WalletConnect/Relay/WalletConnectRelay.swift b/Sources/WalletConnect/Relay/WalletConnectRelay.swift index 19451eb77..036ecd96f 100644 --- a/Sources/WalletConnect/Relay/WalletConnectRelay.swift +++ b/Sources/WalletConnect/Relay/WalletConnectRelay.swift @@ -77,7 +77,8 @@ class WalletConnectRelay: WalletConnectRelaying { do { try jsonRpcHistory.set(topic: topic, request: payload, chainId: getChainId(payload)) let message = try serializer.serialize(topic: topic, encodable: payload) - networkRelayer.publish(topic: topic, payload: message) { [weak self] error in + let prompt = shouldPrompt(payload.method) + networkRelayer.publish(topic: topic, payload: message, prompt: prompt) { [weak self] error in guard let self = self else {return} if let error = error { self.logger.error(error) @@ -111,7 +112,7 @@ class WalletConnectRelay: WalletConnectRelaying { let message = try serializer.serialize(topic: topic, encodable: response.value) logger.debug("Responding....topic: \(topic)") - networkRelayer.publish(topic: topic, payload: message) { error in + networkRelayer.publish(topic: topic, payload: message, prompt: false) { error in completion(error) } } catch WalletConnectError.internal(.jsonRpcDuplicateDetected) { @@ -217,6 +218,15 @@ class WalletConnectRelay: WalletConnectRelaying { } } + private func shouldPrompt(_ method: WCRequest.Method) -> Bool { + switch method { + case .sessionPayload, .pairingPayload: + return true + default: + return false + } + } + func getChainId(_ request: WCRequest) -> String? { guard case let .sessionPayload(payload) = request.params else {return nil} return payload.chainId diff --git a/Tests/IntegrationTests/ClientTest.swift b/Tests/IntegrationTests/ClientTest.swift index 667db180d..885050949 100644 --- a/Tests/IntegrationTests/ClientTest.swift +++ b/Tests/IntegrationTests/ClientTest.swift @@ -16,8 +16,8 @@ final class ClientTests: XCTestCase { let defaultTimeout: TimeInterval = 5.0 - let relayHost = "staging.walletconnect.org" - let projectId = "" + let relayHost = "relay.dev.walletconnect.com" + let projectId = "52af113ee0c1e1a20f4995730196c13e" var proposer: ClientDelegate! var responder: ClientDelegate! diff --git a/Tests/RelayerTests/RelayerEndToEndTests.swift b/Tests/RelayerTests/RelayerEndToEndTests.swift index 2e60956e5..e246ec0b6 100644 --- a/Tests/RelayerTests/RelayerEndToEndTests.swift +++ b/Tests/RelayerTests/RelayerEndToEndTests.swift @@ -8,13 +8,15 @@ import TestingUtils final class RelayerEndToEndTests: XCTestCase { - let url = URL(string: "wss://staging.walletconnect.org")! + let relayHost = "relay.dev.walletconnect.com" + let projectId = "52af113ee0c1e1a20f4995730196c13e" private var publishers = [AnyCancellable]() - func makeRelayer(_ uniqueIdentifier: String = "") -> Relayer { + func makeRelayer() -> Relayer { let logger = ConsoleLogger() let socketConnectionObserver = SocketConnectionObserver() let urlSession = URLSession(configuration: .default, delegate: socketConnectionObserver, delegateQueue: OperationQueue()) + let url = Relayer.makeRelayUrl(host: relayHost, projectId: projectId) let socket = WebSocketSession(session: urlSession, url: url) let dispatcher = Dispatcher(socket: socket, socketConnectionObserver: socketConnectionObserver, socketConnectionHandler: ManualSocketConnectionHandler(socket: socket)) return Relayer(dispatcher: dispatcher, logger: logger, keyValueStorage: RuntimeKeyValueStorage()) @@ -32,8 +34,8 @@ final class RelayerEndToEndTests: XCTestCase { } func testEndToEndPayload() { - let relayA = makeRelayer("A") - let relayB = makeRelayer("B") + let relayA = makeRelayer() + let relayB = makeRelayer() try! relayA.connect() try! relayB.connect() diff --git a/Tests/WalletConnectTests/Mocks/MockedNetworkRelayer.swift b/Tests/WalletConnectTests/Mocks/MockedNetworkRelayer.swift index 8729aedae..6f76ddeff 100644 --- a/Tests/WalletConnectTests/Mocks/MockedNetworkRelayer.swift +++ b/Tests/WalletConnectTests/Mocks/MockedNetworkRelayer.swift @@ -6,7 +6,7 @@ class MockedNetworkRelayer: NetworkRelaying { var onConnect: (() -> ())? var onMessage: ((String, String) -> ())? var error: Error? - func publish(topic: String, payload: String, completion: @escaping ((Error?) -> ())) -> Int64 { + func publish(topic: String, payload: String, prompt: Bool, completion: @escaping ((Error?) -> ())) -> Int64 { completion(error) return 0 } From 1e5bbd48e9f96c0bd461ae561554b23d14c0ffc7 Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Tue, 15 Feb 2022 13:13:02 +0100 Subject: [PATCH 2/3] add kms tests for wc scheme --- .swiftpm/xcode/xcshareddata/xcschemes/WalletConnect.xcscheme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/WalletConnect.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/WalletConnect.xcscheme index 5b80153d8..70e18c35a 100644 --- a/.swiftpm/xcode/xcshareddata/xcschemes/WalletConnect.xcscheme +++ b/.swiftpm/xcode/xcshareddata/xcschemes/WalletConnect.xcscheme @@ -125,7 +125,7 @@ + skipped = "NO"> Date: Tue, 15 Feb 2022 13:28:07 +0100 Subject: [PATCH 3/3] Add prompt tests --- .../Mocks/MockedNetworkRelayer.swift | 2 ++ .../Mocks/SerializerMock.swift | 2 +- Tests/WalletConnectTests/WCRelayTests.swift | 24 +++++++++++++++++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Tests/WalletConnectTests/Mocks/MockedNetworkRelayer.swift b/Tests/WalletConnectTests/Mocks/MockedNetworkRelayer.swift index 6f76ddeff..ca163e2d3 100644 --- a/Tests/WalletConnectTests/Mocks/MockedNetworkRelayer.swift +++ b/Tests/WalletConnectTests/Mocks/MockedNetworkRelayer.swift @@ -6,7 +6,9 @@ class MockedNetworkRelayer: NetworkRelaying { var onConnect: (() -> ())? var onMessage: ((String, String) -> ())? var error: Error? + var prompt = false func publish(topic: String, payload: String, prompt: Bool, completion: @escaping ((Error?) -> ())) -> Int64 { + self.prompt = prompt completion(error) return 0 } diff --git a/Tests/WalletConnectTests/Mocks/SerializerMock.swift b/Tests/WalletConnectTests/Mocks/SerializerMock.swift index b018a2b96..381c97e78 100644 --- a/Tests/WalletConnectTests/Mocks/SerializerMock.swift +++ b/Tests/WalletConnectTests/Mocks/SerializerMock.swift @@ -8,7 +8,7 @@ import WalletConnectUtils class SerializerMock: Serializing { var deserialized: Any! - var serialized: String! + var serialized: String = "" func serialize(topic: String, encodable: Encodable) throws -> String { try serialize(json: try encodable.json(), agreementKeys: AgreementSecret(sharedSecret: Data(), publicKey: AgreementPrivateKey().publicKey)) diff --git a/Tests/WalletConnectTests/WCRelayTests.swift b/Tests/WalletConnectTests/WCRelayTests.swift index 17acb7662..5422bc07b 100644 --- a/Tests/WalletConnectTests/WCRelayTests.swift +++ b/Tests/WalletConnectTests/WCRelayTests.swift @@ -71,8 +71,20 @@ class WalletConnectRelayTests: XCTestCase { waitForExpectations(timeout: 0.01, handler: nil) } - func testRequestCompletesWithError() { - //todo + func testPromptOnSessionPayload() { + let topic = "fefc3dc39cacbc562ed58f92b296e2d65a6b07ef08992b93db5b3cb86280635a" + let request = getWCSessionPayloadRequest() + networkRelayer.prompt = false + wcRelay.request(topic: topic, payload: request) { _ in } + XCTAssertTrue(networkRelayer.prompt) + } + + func testNoPromptOnSessionUpgrade() { + let topic = "fefc3dc39cacbc562ed58f92b296e2d65a6b07ef08992b93db5b3cb86280635a" + let request = getWCSessionUpgrade() + networkRelayer.prompt = false + wcRelay.request(topic: topic, payload: request) { _ in } + XCTAssertTrue(networkRelayer.prompt) } } @@ -89,6 +101,14 @@ extension WalletConnectRelayTests { let wcRequest = WCRequest(id: wcRequestId, method: WCRequest.Method.sessionPayload, params: params) return wcRequest } + + func getWCSessionUpgrade() -> WCRequest { + let wcRequestId: Int64 = 123456 + let sessionUpgradeParams = SessionType.UpgradeParams(permissions: SessionPermissions(blockchain: SessionPermissions.Blockchain(chains: []), jsonrpc: SessionPermissions.JSONRPC(methods: []), notifications: SessionPermissions.Notifications(types: []))) + let params = WCRequest.Params.sessionUpgrade(sessionUpgradeParams) + let wcRequest = WCRequest(id: wcRequestId, method: WCRequest.Method.sessionPayload, params: params) + return wcRequest + } } fileprivate let testPayload =