Skip to content

Commit

Permalink
Merge pull request #23 from reown-com/develop
Browse files Browse the repository at this point in the history
1.0.2
  • Loading branch information
llbartekll authored Oct 2, 2024
2 parents e1d0f89 + 64cebec commit b29d9c1
Show file tree
Hide file tree
Showing 11 changed files with 264 additions and 238 deletions.
2 changes: 0 additions & 2 deletions Example/IntegrationTests/Sign/SignClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ final class SignClientTests: XCTestCase {
}

override func tearDown() {
// Synchronously wait for 0.1 seconds
Thread.sleep(forTimeInterval: 0.1)

// Now set properties to nil
dapp = nil
Expand Down
81 changes: 49 additions & 32 deletions Example/RelayIntegrationTests/RelayClientEndToEndTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class WebSocketFactoryMock: WebSocketFactory {
final class RelayClientEndToEndTests: XCTestCase {

private var publishers = Set<AnyCancellable>()
private var relayA: RelayClient!
private var relayB: RelayClient!

func makeRelayClient(prefix: String) -> RelayClient {
let keyValueStorage = RuntimeKeyValueStorage()
Expand Down Expand Up @@ -81,33 +83,43 @@ final class RelayClientEndToEndTests: XCTestCase {
}

override func tearDown() {
Thread.sleep(forTimeInterval: 1.0)
relayA = nil
relayB = nil
super.tearDown()
}

func testSubscribe() {
let relayClient = makeRelayClient(prefix: "")

try! relayClient.connect()
let subscribeExpectation = expectation(description: "subscribe call succeeds")
subscribeExpectation.assertForOverFulfill = true
relayClient.socketConnectionStatusPublisher.sink { status in
if status == .connected {
Task(priority: .high) { try await relayClient.subscribe(topic: "ecb78f2df880c43d3418ddbf871092b847801932e21765b250cc50b9e96a9131") }
subscribeExpectation.fulfill()
}
}.store(in: &publishers)

wait(for: [subscribeExpectation], timeout: InputConfig.defaultTimeout)
}
// func testSubscribe() {
// relayA = makeRelayClient(prefix: "")
//
// do {
// try relayA.connect()
// } catch {
// XCTFail("Failed to connect: \(error)")
// }
//
// let subscribeExpectation = expectation(description: "subscribe call succeeds")
// subscribeExpectation.assertForOverFulfill = true
// relayA.socketConnectionStatusPublisher.sink { [weak self] status in
// guard let self = self else {return}
// if status == .connected {
// Task(priority: .high) { try await self.relayA.subscribe(topic: "ecb78f2df880c43d3418ddbf871092b847801932e21765b250cc50b9e96a9131") }
// subscribeExpectation.fulfill()
// }
// }.store(in: &publishers)
//
// wait(for: [subscribeExpectation], timeout: InputConfig.defaultTimeout)
// }

func testEndToEndPayload() {
let relayA = makeRelayClient(prefix: "⚽️ A ")
let relayB = makeRelayClient(prefix: "🏀 B ")

try! relayA.connect()
try! relayB.connect()

relayA = makeRelayClient(prefix: "⚽️ A ")
relayB = makeRelayClient(prefix: "🏀 B ")

do {
try relayA.connect()
try relayB.connect()
} catch {
XCTFail("Failed to connect: \(error)")
}
let randomTopic = String.randomTopic()
let payloadA = "A"
let payloadB = "B"
Expand All @@ -122,31 +134,36 @@ final class RelayClientEndToEndTests: XCTestCase {
expectationA.assertForOverFulfill = false
expectationB.assertForOverFulfill = false

relayA.messagePublisher.sink { topic, payload, _, _ in
relayA.messagePublisher.sink { [weak self] topic, payload, _, _ in
guard let self = self else { return }
(subscriptionATopic, subscriptionAPayload) = (topic, payload)
expectationA.fulfill()
}.store(in: &publishers)

relayB.messagePublisher.sink { topic, payload, _, _ in
relayB.messagePublisher.sink { [weak self] topic, payload, _, _ in
guard let self = self else { return }
(subscriptionBTopic, subscriptionBPayload) = (topic, payload)
Task(priority: .high) {
sleep(1)
try await relayB.publish(topic: randomTopic, payload: payloadB, tag: 0, prompt: false, ttl: 60)
try await self.relayB.publish(topic: randomTopic, payload: payloadB, tag: 0, prompt: false, ttl: 60)
}
expectationB.fulfill()
}.store(in: &publishers)

relayA.socketConnectionStatusPublisher.sink { status in
guard status == .connected else {return}
relayA.socketConnectionStatusPublisher.sink { [weak self] status in
guard let self = self else { return }
guard status == .connected else { return }
Task(priority: .high) {
try await relayA.subscribe(topic: randomTopic)
try await relayA.publish(topic: randomTopic, payload: payloadA, tag: 0, prompt: false, ttl: 60)
try await self.relayA.subscribe(topic: randomTopic)
try await self.relayA.publish(topic: randomTopic, payload: payloadA, tag: 0, prompt: false, ttl: 60)
}
}.store(in: &publishers)
relayB.socketConnectionStatusPublisher.sink { status in
guard status == .connected else {return}

relayB.socketConnectionStatusPublisher.sink { [weak self] status in
guard let self = self else { return }
guard status == .connected else { return }
Task(priority: .high) {
try await relayB.subscribe(topic: randomTopic)
try await self.relayB.subscribe(topic: randomTopic)
}
}.store(in: &publishers)

Expand Down
11 changes: 11 additions & 0 deletions Sources/WalletConnectPairing/PairingClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,23 @@ public class PairingClient: PairingRegisterer, PairingInteracting, PairingClient
self.pairingsProvider = pairingsProvider
self.pairingStateProvider = pairingStateProvider
setUpExpiration()
subscribePairingTopics()
}

private func setUpExpiration() {
expirationService.setupExpirationHandling()
}

private func subscribePairingTopics() {
let topics = pairingStorage
.getAll()
.filter{!$0.requestReceived}
.map{$0.topic}
Task(priority: .background) {
try await networkingInteractor.batchSubscribe(topics: topics)
}
}

/// For wallet to establish a pairing
/// Wallet should call this function in order to accept peer's pairing proposal and be able to subscribe for future requests.
/// - Parameter uri: Pairing URI that is commonly presented as a QR code by a dapp or delivered with universal linking.
Expand Down
41 changes: 6 additions & 35 deletions Sources/WalletConnectRelay/BundleFinder.swift
Original file line number Diff line number Diff line change
@@ -1,38 +1,9 @@
import class Foundation.Bundle

private class BundleFinder {}
import Foundation

extension Foundation.Bundle {
/// Returns the resource bundle associated with the current Swift module.
static var resourceBundle: Bundle = {
let bundleName = "reown_WalletConnectRelay"

let candidates = [
// Bundle should be present here when the package is linked into an App.
Bundle.main.resourceURL,

// Bundle should be present here when the package is linked into a framework.
Bundle(for: BundleFinder.self).resourceURL,

// For command-line tools.
Bundle.main.bundleURL,

// One of these should be used when building SwiftUI Previews
Bundle(for: BundleFinder.self).resourceURL?
.deletingLastPathComponent()
.deletingLastPathComponent()
.deletingLastPathComponent(),
Bundle(for: BundleFinder.self).resourceURL?
.deletingLastPathComponent()
.deletingLastPathComponent()
]

for candidate in candidates {
let bundlePath = candidate?.appendingPathComponent(bundleName + ".bundle")
if let bundle = bundlePath.flatMap(Bundle.init(url:)) {
return bundle
}
}
fatalError("unable to find bundle named WalletConnect_WalletConnectRelay")
}()
extension Bundle {
// This will provide access to the resources for the WalletConnectRelay module
static var resourceBundle: Bundle {
return Bundle.module
}
}
32 changes: 22 additions & 10 deletions Sources/WalletConnectRelay/EnvironmentInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,32 @@ public enum EnvironmentInfo {
"reown-swift-v\(packageVersion)"
}

// This method reads the package version from the "PackageConfig.json" file in the bundle
public static var packageVersion: String {
let configURL = Bundle.resourceBundle.url(forResource: "PackageConfig", withExtension: "json")!
let jsonData = try! Data(contentsOf: configURL)
let config = try! JSONDecoder().decode(PackageConfig.self, from: jsonData)
return config.version
guard let configURL = Bundle.resourceBundle.url(forResource: "PackageConfig", withExtension: "json") else {
fatalError("Unable to find PackageConfig.json in the resource bundle")
}

do {
let jsonData = try Data(contentsOf: configURL)
let config = try JSONDecoder().decode(PackageConfig.self, from: jsonData)
return config.version
} catch {
fatalError("Failed to load and decode PackageConfig.json: \(error)")
}
}

public static var operatingSystem: String {
#if os(iOS)
#if os(iOS)
return "\(UIDevice.current.systemName)-\(UIDevice.current.systemVersion)"
#elseif os(macOS)
return "macOS-\(ProcessInfo.processInfo.operatingSystemVersion)"
#elseif os(tvOS)
return "tvOS-\(ProcessInfo.processInfo.operatingSystemVersion)"
#endif
#elseif os(macOS)
let version = ProcessInfo.processInfo.operatingSystemVersion
return "macOS-\(version.majorVersion).\(version.minorVersion).\(version.patchVersion)"
#elseif os(tvOS)
let version = ProcessInfo.processInfo.operatingSystemVersion
return "tvOS-\(version.majorVersion).\(version.minorVersion).\(version.patchVersion)"
#else
return "unknownOS"
#endif
}
}
2 changes: 1 addition & 1 deletion Sources/WalletConnectRelay/PackageConfig.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version": "1.0.1"}
{"version": "1.0.2"}
7 changes: 4 additions & 3 deletions Sources/WalletConnectRelay/RelayClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,12 @@ public final class RelayClient {

private func setupConnectionSubscriptions() {
socketConnectionStatusPublisher
.sink { [unowned self] status in
.sink { [weak self] status in
guard let self = self else { return }
guard status == .connected else { return }
let topics = subscriptionsTracker.getTopics()
let topics = self.subscriptionsTracker.getTopics()
Task(priority: .high) {
try await batchSubscribe(topics: topics)
try await self.batchSubscribe(topics: topics)
}
}
.store(in: &publishers)
Expand Down
Loading

0 comments on commit b29d9c1

Please sign in to comment.