Skip to content

Commit

Permalink
Merge pull request #79 from WalletConnect/#26-push-notification-support
Browse files Browse the repository at this point in the history
#26 push notification support
  • Loading branch information
llbartekll committed Feb 18, 2022
2 parents 921d6d5 + 3f7899f commit 01e523d
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 54 deletions.
38 changes: 0 additions & 38 deletions .swiftpm/xcode/xcshareddata/xcschemes/WalletConnect.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -76,34 +76,6 @@
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "KMS"
BuildableName = "KMS"
BlueprintName = "KMS"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "WalletConnectKMS"
BuildableName = "WalletConnectKMS"
BlueprintName = "WalletConnectKMS"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
Expand Down Expand Up @@ -152,16 +124,6 @@
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "KMSTests"
BuildableName = "KMSTests"
BlueprintName = "KMSTests"
ReferencedContainer = "container:">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
<BuildableReference
Expand Down
1 change: 1 addition & 0 deletions Sources/Relayer/RelayJSONRPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum RelayJSONRPC {
let topic: String
let message: String
let ttl: Int
let prompt: Bool?
}

struct SubscribeParams: Codable, Equatable {
Expand Down
10 changes: 7 additions & 3 deletions Sources/Relayer/Relayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ public final class Relayer {
try dispatcher.disconnect(closeCode: closeCode)
}

@discardableResult public func publish(topic: String, payload: String, completion: @escaping ((Error?) -> ())) -> 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<RelayJSONRPC.PublishParams>(method: RelayJSONRPC.Method.publish.rawValue, params: params)
let requestJson = try! request.json()
logger.debug("waku: Publishing Payload on Topic: \(topic)")
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Sources/WalletConnect/Relay/NetworkRelaying.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions Sources/WalletConnect/Relay/WalletConnectRelay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -110,7 +111,7 @@ class WalletConnectRelay: WalletConnectRelaying {
_ = try jsonRpcHistory.resolve(response: response)
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) {
Expand Down Expand Up @@ -216,6 +217,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
Expand Down
4 changes: 2 additions & 2 deletions Tests/IntegrationTests/ClientTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!

Expand Down
10 changes: 6 additions & 4 deletions Tests/RelayerTests/RelayerEndToEndTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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()

Expand Down
4 changes: 3 additions & 1 deletion Tests/WalletConnectTests/Mocks/MockedNetworkRelayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ class MockedNetworkRelayer: NetworkRelaying {
var onConnect: (() -> ())?
var onMessage: ((String, String) -> ())?
var error: Error?
func publish(topic: String, payload: String, completion: @escaping ((Error?) -> ())) -> Int64 {
var prompt = false
func publish(topic: String, payload: String, prompt: Bool, completion: @escaping ((Error?) -> ())) -> Int64 {
self.prompt = prompt
completion(error)
return 0
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/WalletConnectTests/Mocks/SerializerMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
24 changes: 22 additions & 2 deletions Tests/WalletConnectTests/WCRelayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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 =
Expand Down

0 comments on commit 01e523d

Please sign in to comment.