Skip to content
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

[Dispatching] Native socket support #911

Open
wants to merge 31 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
afce442
Add native WebSocket support
alexander-lsvk Jun 15, 2023
e49086b
Remove test data
alexander-lsvk Jun 15, 2023
e6c609b
Merge remote-tracking branch 'origin/develop' into native-socket
alexander-lsvk Jun 15, 2023
274438e
Fix merge issues
alexander-lsvk Jun 15, 2023
7f57858
Merge branch 'develop' into native-socket
alexander-lsvk Jun 16, 2023
4361cea
Merge branch 'develop' into native-socket
alexander-lsvk Jul 2, 2023
70f2dd3
Fix merge errors
alexander-lsvk Jul 2, 2023
f9b74c8
Fix merge errors
alexander-lsvk Jul 2, 2023
10d8592
Update WebSocketConnecting protocol
alexander-lsvk Jul 5, 2023
78d0544
Merge branch 'develop' into native-socket
alexander-lsvk Jul 5, 2023
a457c5f
Merge branch 'develop' into native-socket
alexander-lsvk Jul 12, 2023
90251da
Fix merge conflicts
alexander-lsvk Jul 12, 2023
9af6061
Update mocks visibility
alexander-lsvk Jul 12, 2023
219a24c
Merge branch 'develop' into native-socket
alexander-lsvk Jul 13, 2023
ac6f2e2
Fix native socket init issue
alexander-lsvk Jul 13, 2023
ab58b21
Fix socket disconnection
alexander-lsvk Jul 14, 2023
2f5b54c
Merge branch 'develop' into native-socket
alexander-lsvk Jul 14, 2023
95176e4
Fix native socket issue
alexander-lsvk Jul 16, 2023
9ace2d0
Add fallback to walletconnect.org
alexander-lsvk Jul 18, 2023
5913314
Fix tests
alexander-lsvk Jul 18, 2023
7c8ca64
Merge branch 'develop' into native-socket
alexander-lsvk Jul 31, 2023
6203975
Support Starscream
alexander-lsvk Sep 7, 2023
2d78626
Merge branch 'develop' into native-socket
alexander-lsvk Sep 7, 2023
1bc4842
Update socket client
alexander-lsvk Sep 12, 2023
2340bb3
Merge branch 'develop' into native-socket
alexander-lsvk Sep 12, 2023
5642411
Update tests
alexander-lsvk Sep 12, 2023
4bd0227
Fix tests
alexander-lsvk Sep 12, 2023
748dc66
Update tests
alexander-lsvk Sep 12, 2023
366a891
Update tests
alexander-lsvk Sep 14, 2023
cf8168e
Update
alexander-lsvk Oct 23, 2023
1ab4dbe
Merge branch 'develop' into native-socket
alexander-lsvk Oct 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Example/DApp/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
private let authCoordinator = AuthCoordinator()

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
Networking.configure(projectId: InputConfig.projectId, socketFactory: DefaultSocketFactory())
Networking.configure(projectId: InputConfig.projectId, socketFactory: WalletConnectSocketClientFactory())
Auth.configure(crypto: DefaultCryptoProvider())

let metadata = AppMetadata(
Expand Down
102 changes: 53 additions & 49 deletions Example/ExampleApp.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

8 changes: 1 addition & 7 deletions Example/IntegrationTests/Auth/AuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,7 @@ final class AuthTests: XCTestCase {
let logger = ConsoleLogger(prefix: prefix, loggingLevel: .debug)
let keyValueStorage = RuntimeKeyValueStorage()
let keychain = KeychainStorageMock()
let relayClient = RelayClientFactory.create(
relayHost: InputConfig.relayHost,
projectId: InputConfig.projectId,
keyValueStorage: keyValueStorage,
keychainStorage: keychain,
socketFactory: DefaultSocketFactory(),
logger: logger)
let relayClient = RelayClientFactory.create(relayHost: InputConfig.relayHost, projectId: InputConfig.projectId, keyValueStorage: RuntimeKeyValueStorage(), keychainStorage: keychain, socketFactory: WalletConnectSocketClientFactory(), logger: logger)

let networkingClient = NetworkingClientFactory.create(
relayClient: relayClient,
Expand Down
8 changes: 1 addition & 7 deletions Example/IntegrationTests/Chat/ChatTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,7 @@ final class ChatTests: XCTestCase {
let logger = ConsoleLogger(prefix: prefix, loggingLevel: .debug)
let keyValueStorage = RuntimeKeyValueStorage()
let keychain = KeychainStorageMock()
let relayClient = RelayClientFactory.create(
relayHost: InputConfig.relayHost,
projectId: InputConfig.projectId,
keyValueStorage: keyValueStorage,
keychainStorage: keychain,
socketFactory: DefaultSocketFactory(),
logger: logger)
let relayClient = RelayClientFactory.create(relayHost: InputConfig.relayHost, projectId: InputConfig.projectId, keyValueStorage: RuntimeKeyValueStorage(), keychainStorage: keychain, socketFactory: WalletConnectSocketClientFactory(), logger: logger)

let networkingInteractor = NetworkingClientFactory.create(
relayClient: relayClient,
Expand Down
2 changes: 1 addition & 1 deletion Example/IntegrationTests/Pairing/PairingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class PairingTests: XCTestCase {
projectId: InputConfig.projectId,
keyValueStorage: RuntimeKeyValueStorage(),
keychainStorage: keychain,
socketFactory: DefaultSocketFactory(),
socketFactory: WalletConnectSocketClientFactory(),
logger: relayLogger)

let networkingClient = NetworkingClientFactory.create(
Expand Down
3 changes: 2 additions & 1 deletion Example/IntegrationTests/Sign/SignClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ final class SignClientTests: XCTestCase {
projectId: InputConfig.projectId,
keyValueStorage: keyValueStorage,
keychainStorage: keychain,
socketFactory: DefaultSocketFactory(),
socketFactory: WalletConnectSocketClientFactory(),
socketConnectionType: .automatic,
logger: logger
)

Expand Down
8 changes: 4 additions & 4 deletions Example/RelayIntegrationTests/RelayClientEndToEndTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ private class RelayKeychainStorageMock: KeychainStorageProtocol {
}

class WebSocketFactoryMock: WebSocketFactory {
private let webSocket: WebSocket
private let webSocket: WebSocketClient

init(webSocket: WebSocket) {
init(webSocket: WebSocketClient) {
self.webSocket = webSocket
}

func create(with url: URL) -> WebSocketConnecting {
return webSocket
return webSocket as! WebSocketConnecting
}
}

Expand Down Expand Up @@ -57,7 +57,7 @@ final class RelayClientEndToEndTests: XCTestCase {
projectId: InputConfig.projectId,
keyValueStorage: keyValueStorage,
keychainStorage: keychain,
socketFactory: DefaultSocketFactory(),
socketFactory: webSocketFactory,
socketConnectionType: .manual,
logger: logger
)
Expand Down
7 changes: 6 additions & 1 deletion Example/Shared/DefaultSocketFactory.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import Foundation

import Starscream
import WalletConnectRelay

extension WebSocket: WebSocketConnecting { }
extension WebSocket: WebSocketConnecting {
public func reconnect() {
connect()
}
}

struct DefaultSocketFactory: WebSocketFactory {
func create(with url: URL) -> WebSocketConnecting {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import Auth
import WalletConnectModal

struct ThirdPartyConfigurator: Configurator {

func configure() {

func configure() {
let metadata = AppMetadata(
name: "Showcase App",
description: "Showcase description",
url: "example.wallet",
icons: ["https://avatars.githubusercontent.com/u/37784886"]
)

Networking.configure(projectId: InputConfig.projectId, socketFactory: DefaultSocketFactory())
Networking.configure(projectId: InputConfig.projectId)
Auth.configure(crypto: DefaultCryptoProvider())
WalletConnectModal.configure(projectId: InputConfig.projectId, metadata: metadata)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import UIKit
import WalletConnectNetworking
import WalletConnectNotify

import Web3Wallet

final class ConfigurationService {

func configure(importAccount: ImportAccount) {
Networking.configure(projectId: InputConfig.projectId, socketFactory: DefaultSocketFactory())
Networking.configure(projectId: InputConfig.projectId, socketFactory: WalletConnectSocketClientFactory())
Networking.instance.setLogging(level: .debug)

let metadata = AppMetadata(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import Foundation

import WalletConnectNetworking
import Web3Wallet

struct ThirdPartyConfigurator: Configurator {

func configure() {

}

private func configureLogging() {
Expand Down
4 changes: 0 additions & 4 deletions Sources/WalletConnectModal/Mocks/Listing+Mocks.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import Foundation

#if DEBUG

extension Listing {
static let stubList: [Listing] = [
Listing(
Expand Down Expand Up @@ -63,5 +61,3 @@ extension Listing {
)
]
}

#endif
3 changes: 2 additions & 1 deletion Sources/WalletConnectModal/Modal/Modal+Previews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class WebSocketMock: WebSocketConnecting {

func connect() {}
func disconnect() {}
func reconnect() {}
func write(string: String, completion: (() -> Void)?) {}
}

Expand Down Expand Up @@ -41,7 +42,7 @@ struct ModalContainerView_Previews: PreviewProvider {
icons: ["https://avatars.githubusercontent.com/u/37784886"]
)

Networking.configure(projectId: projectId, socketFactory: WebSocketFactoryMock())
Networking.configure(projectId: projectId, socketFactory: WalletConnectSocketClientFactory())
WalletConnectModal.configure(projectId: projectId, metadata: metadata)
}

Expand Down
1 change: 0 additions & 1 deletion Sources/WalletConnectModal/Modal/ModalInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ protocol ModalSheetInteractor {
}

final class DefaultModalSheetInteractor: ModalSheetInteractor {

lazy var sessionSettlePublisher: AnyPublisher<Session, Never> = WalletConnectModal.instance.sessionSettlePublisher
lazy var sessionRejectionPublisher: AnyPublisher<(Session.Proposal, Reason), Never> = WalletConnectModal.instance.sessionRejectionPublisher

Expand Down
1 change: 0 additions & 1 deletion Sources/WalletConnectNetworking/Networking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class Networking {
/// - Parameters:
/// - relayHost: relay host
/// - projectId: project id
/// - socketFactory: web socket factory
/// - socketConnectionType: socket connection type
static public func configure(
relayHost: String = "relay.walletconnect.com",
Expand Down
7 changes: 5 additions & 2 deletions Sources/WalletConnectRelay/Dispatching.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ final class Dispatcher: NSObject, Dispatching {
private let logger: ConsoleLogging

private let defaultTimeout: Int = 5

/// The property is used to determine whether relay.walletconnect.org will be used
/// in case relay.walletconnect.com doesn't respond for some reason (most likely due to being blocked in the user's location).
private var fallback = false
Expand Down Expand Up @@ -55,12 +56,12 @@ final class Dispatcher: NSObject, Dispatching {
setUpSocketConnectionObserving()
}

func send(_ string: String, completion: @escaping (Error?) -> Void) {
func send(_ message: String, completion: @escaping (Error?) -> Void) {
guard socket.isConnected else {
completion(NetworkError.webSocketNotConnected)
return
}
socket.write(string: string) {
socket.write(string: message) {
completion(nil)
}
}
Expand Down Expand Up @@ -120,9 +121,11 @@ extension Dispatcher {

private func setUpSocketConnectionObserving() {
socket.onConnect = { [unowned self] in
logger.debug("[WebSocket] - Connected")
self.socketConnectionStatusPublisherSubject.send(.connected)
}
socket.onDisconnect = { [unowned self] error in
logger.debug("[WebSocket] - Disconnected")
self.socketConnectionStatusPublisherSubject.send(.disconnected)
if error != nil {
self.socket.request.url = relayUrlFactory.create(fallback: fallback)
Expand Down
3 changes: 1 addition & 2 deletions Sources/WalletConnectRelay/RelayClientFactory.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//
import Foundation


public struct RelayClientFactory {

public static func create(
Expand All @@ -28,7 +27,6 @@ public struct RelayClientFactory {
)
}


public static func create(
relayHost: String,
projectId: String,
Expand All @@ -50,6 +48,7 @@ public struct RelayClientFactory {
projectId: projectId,
socketAuthenticator: socketAuthenticator
)

let dispatcher = Dispatcher(
socketFactory: socketFactory,
relayUrlFactory: relayUrlFactory,
Expand Down
2 changes: 1 addition & 1 deletion Sources/WalletConnectRelay/RelayURLFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ struct RelayUrlFactory {
private let relayHost: String
private let projectId: String
private let socketAuthenticator: ClientIdAuthenticating

init(
relayHost: String,
projectId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class AutomaticSocketConnectionHandler {
networkMonitor.networkConnectionStatusPublisher.sink { [weak self] networkConnectionStatus in
if networkConnectionStatus == .connected {
self?.reconnectIfNeeded()
} else {
self?.socket.disconnect()
}
}
.store(in: &publishers)
Expand All @@ -65,7 +67,7 @@ class AutomaticSocketConnectionHandler {

private func reconnectIfNeeded() {
if !socket.isConnected {
socket.connect()
socket.reconnect()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ public protocol WebSocketConnecting: AnyObject {
var onDisconnect: ((Error?) -> Void)? { get set }
var onText: ((String) -> Void)? { get set }
var request: URLRequest { get set }

func connect()
func disconnect()
func reconnect()
func write(string: String, completion: (() -> Void)?)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Foundation
import Combine

final class UnfairLock {
private var _lock: UnsafeMutablePointer<os_unfair_lock>

init() {
_lock = UnsafeMutablePointer<os_unfair_lock>.allocate(capacity: 1)
_lock.initialize(to: os_unfair_lock())
}

deinit {
_lock.deallocate()
}

func locked<ReturnValue>(_ f: () throws -> ReturnValue) rethrows -> ReturnValue {
os_unfair_lock_lock(_lock)
defer { os_unfair_lock_unlock(_lock) }
return try f()
}
}
Loading
Loading