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

Pairing error response test #535

Merged
merged 10 commits into from
Oct 4, 2022
3 changes: 2 additions & 1 deletion Example/DApp/SceneDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import UIKit
import Starscream
import WalletConnectRelay
import WalletConnectNetworking

extension WebSocket: WebSocketConnecting { }

Expand All @@ -18,7 +19,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
private let authCoordinator = AuthCoordinator()

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
Relay.configure(projectId: InputConfig.projectId, socketFactory: SocketFactory())
Networking.configure(projectId: InputConfig.projectId, socketFactory: SocketFactory())

setupWindow(scene: scene)
}
Expand Down
3 changes: 2 additions & 1 deletion Example/ExampleApp/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import UIKit
import Foundation
import Combine
import WalletConnectSign
import WalletConnectNetworking
import WalletConnectRelay
import Starscream

Expand All @@ -25,7 +26,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
url: "example.wallet",
icons: ["https://avatars.githubusercontent.com/u/37784886"])

Relay.configure(projectId: InputConfig.projectId, socketFactory: SocketFactory())
Networking.configure(projectId: InputConfig.projectId, socketFactory: SocketFactory())
Sign.configure(metadata: metadata)
#if DEBUG
if CommandLine.arguments.contains("-cleanInstall") {
Expand Down
11 changes: 9 additions & 2 deletions Example/IntegrationTests/Auth/AuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import WalletConnectRelay
import Combine
@testable import Auth
import WalletConnectPairing
import WalletConnectNetworking

final class AuthTests: XCTestCase {
var appPairingClient: PairingClient!
Expand All @@ -29,19 +30,25 @@ final class AuthTests: XCTestCase {
let relayClient = RelayClient(relayHost: InputConfig.relayHost, projectId: InputConfig.projectId, keychainStorage: keychain, socketFactory: SocketFactory(), logger: logger)
let keyValueStorage = RuntimeKeyValueStorage()

let networkingClient = NetworkingClientFactory.create(
relayClient: relayClient,
logger: logger,
keychainStorage: keychain,
keyValueStorage: keyValueStorage)

let pairingClient = PairingClientFactory.create(
logger: logger,
keyValueStorage: keyValueStorage,
keychainStorage: keychain,
relayClient: relayClient)
networkingClient: networkingClient)

let authClient = AuthClientFactory.create(
metadata: AppMetadata(name: name, description: "", url: "", icons: [""]),
account: account,
logger: logger,
keyValueStorage: keyValueStorage,
keychainStorage: keychain,
relayClient: relayClient,
networkingClient: networkingClient,
pairingRegisterer: pairingClient)

return (pairingClient, authClient)
Expand Down
125 changes: 102 additions & 23 deletions Example/IntegrationTests/Pairing/PairingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import WalletConnectNetworking
import WalletConnectPush
@testable import WalletConnectPairing


final class PairingTests: XCTestCase {

var appPairingClient: PairingClient!
Expand All @@ -21,51 +20,131 @@ final class PairingTests: XCTestCase {

private var publishers = [AnyCancellable]()

override func setUp() {
(appPairingClient, appPushClient) = makeClients(prefix: "🤖 App")
(walletPairingClient, walletPushClient) = makeClients(prefix: "🐶 Wallet")
}

func makeClients(prefix: String) -> (PairingClient, PushClient) {
let keychain = KeychainStorageMock()
let logger = ConsoleLogger(suffix: prefix, loggingLevel: .debug)
let relayClient = RelayClient(relayHost: InputConfig.relayHost, projectId: InputConfig.projectId, keychainStorage: keychain, socketFactory: SocketFactory(), logger: logger)

let pairingClient = PairingClientFactory.create(logger: logger, keyValueStorage: RuntimeKeyValueStorage(), keychainStorage: keychain, relayClient: relayClient)
let keyValueStorage = RuntimeKeyValueStorage()

let relayLogger = ConsoleLogger(suffix: prefix + " [Relay]", loggingLevel: .debug)
let pairingLogger = ConsoleLogger(suffix: prefix + " [Pairing]", loggingLevel: .debug)
let pushLogger = ConsoleLogger(suffix: prefix + " [Push]", loggingLevel: .debug)
let networkingLogger = ConsoleLogger(suffix: prefix + " [Networking]", loggingLevel: .debug)

let relayClient = RelayClient(
relayHost: InputConfig.relayHost,
projectId: InputConfig.projectId,
keyValueStorage: RuntimeKeyValueStorage(),
keychainStorage: keychain,
socketFactory: SocketFactory(),
logger: relayLogger)

let networkingClient = NetworkingClientFactory.create(
relayClient: relayClient,
logger: networkingLogger,
keychainStorage: keychain,
keyValueStorage: keyValueStorage)

let pairingClient = PairingClientFactory.create(
logger: pairingLogger,
keyValueStorage: keyValueStorage,
keychainStorage: keychain,
networkingClient: networkingClient)

let pushClient = PushClientFactory.create(
logger: pushLogger,
keyValueStorage: keyValueStorage,
keychainStorage: keychain,
networkingClient: networkingClient,
pairingClient: pairingClient)

let pushClient = PushClientFactory.create(logger: logger, keyValueStorage: RuntimeKeyValueStorage(), keychainStorage: keychain, relayClient: relayClient, pairingClient: pairingClient)
return (pairingClient, pushClient)
}

func makePairingClient(prefix: String) -> PairingClient {
let keychain = KeychainStorageMock()
let logger = ConsoleLogger(suffix: prefix, loggingLevel: .debug)
let keyValueStorage = RuntimeKeyValueStorage()

let relayClient = RelayClient(
relayHost: InputConfig.relayHost,
projectId: InputConfig.projectId,
keychainStorage: keychain,
socketFactory: SocketFactory(),
logger: logger)

let networkingClient = NetworkingClientFactory.create(
relayClient: relayClient,
logger: logger,
keychainStorage: keychain,
keyValueStorage: keyValueStorage)

let pairingClient = PairingClientFactory.create(
logger: logger,
keyValueStorage: keyValueStorage,
keychainStorage: keychain,
networkingClient: networkingClient)

return pairingClient
}

func testProposePushOnPairing() async throws {
let exp = expectation(description: "testProposePushOnPairing")
func testProposePushOnPairing() async {
let expectation = expectation(description: "propose push on pairing")

(appPairingClient, appPushClient) = makeClients(prefix: "🤖 App")
(walletPairingClient, walletPushClient) = makeClients(prefix: "🐶 Wallet")

walletPushClient.proposalPublisher.sink { _ in
exp.fulfill()
expectation.fulfill()
}.store(in: &publishers)

let uri = try await appPairingClient.create()
let uri = try! await appPairingClient.create()

try await walletPairingClient.pair(uri: uri)
try! await walletPairingClient.pair(uri: uri)

try await appPushClient.propose(topic: uri.topic)
try! await appPushClient.propose(topic: uri.topic)

wait(for: [exp], timeout: InputConfig.defaultTimeout)
wait(for: [expectation], timeout: InputConfig.defaultTimeout)
}

func testPing() async {
let pingExpectation = expectation(description: "expects ping response")
let expectation = expectation(description: "expects ping response")

(appPairingClient, appPushClient) = makeClients(prefix: "🤖 App")
(walletPairingClient, walletPushClient) = makeClients(prefix: "🐶 Wallet")

let uri = try! await appPairingClient.create()
try! await walletPairingClient.pair(uri: uri)
try! await walletPairingClient.ping(topic: uri.topic)
walletPairingClient.pingResponsePublisher
.sink { topic in
XCTAssertEqual(topic, uri.topic)
pingExpectation.fulfill()
}
.store(in: &publishers)
wait(for: [pingExpectation], timeout: InputConfig.defaultTimeout)
expectation.fulfill()
}.store(in: &publishers)
wait(for: [expectation], timeout: InputConfig.defaultTimeout)
}

func testResponseErrorForMethodUnregistered() async {
(appPairingClient, appPushClient) = makeClients(prefix: "🤖 App")
walletPairingClient = makePairingClient(prefix: "🐶 Wallet")

let expectation = expectation(description: "wallet responds unsupported method for unregistered method")

appPushClient.responsePublisher.sink { (id, response) in
XCTAssertEqual(response, .failure(WalletConnectPairing.PairError(code: 0)!))
expectation.fulfill()
}.store(in: &publishers)

let uri = try! await appPairingClient.create()

try! await walletPairingClient.pair(uri: uri)

try! await appPushClient.propose(topic: uri.topic)

wait(for: [expectation], timeout: InputConfig.defaultTimeout)

}

func testDisconnect() {
//TODO
}
}

9 changes: 8 additions & 1 deletion Example/IntegrationTests/Sign/SignClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import JSONRPC
@testable import WalletConnectSign
@testable import WalletConnectRelay
import WalletConnectPairing
import WalletConnectNetworking

final class SignClientTests: XCTestCase {
var dapp: ClientDelegate!
Expand All @@ -24,11 +25,17 @@ final class SignClientTests: XCTestCase {
)
let keyValueStorage = RuntimeKeyValueStorage()

let networkingClient = NetworkingClientFactory.create(
relayClient: relayClient,
logger: logger,
keychainStorage: keychain,
keyValueStorage: keyValueStorage)

let pairingClient = PairingClientFactory.create(
logger: logger,
keyValueStorage: keyValueStorage,
keychainStorage: keychain,
relayClient: relayClient)
networkingClient: networkingClient)

let client = SignClientFactory.create(
metadata: AppMetadata(name: name, description: "", url: "", icons: [""]),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import WalletConnectRelay
import WalletConnectNetworking
import WalletConnectPairing
import Auth

struct ThirdPartyConfigurator: Configurator {

func configure() {
Relay.configure(projectId: InputConfig.projectId, socketFactory: SocketFactory())
Networking.configure(projectId: InputConfig.projectId, socketFactory: SocketFactory())
Pair.configure(
metadata: AppMetadata(
name: "Showcase App",
Expand Down
4 changes: 2 additions & 2 deletions Sources/Auth/Auth.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
import WalletConnectRelay
import WalletConnectNetworking
import WalletConnectPairing
import Combine

Expand All @@ -24,7 +24,7 @@ public class Auth {
return AuthClientFactory.create(
metadata: config.metadata,
account: config.account,
relayClient: Relay.instance,
networkingClient: Networking.instance,
pairingRegisterer: Pair.instance)
}()

Expand Down
20 changes: 9 additions & 11 deletions Sources/Auth/AuthClientFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,23 @@ import WalletConnectNetworking

public struct AuthClientFactory {

public static func create(metadata: AppMetadata, account: Account?, relayClient: RelayClient, pairingRegisterer: PairingRegisterer) -> AuthClient {
public static func create(metadata: AppMetadata, account: Account?, networkingClient: NetworkingClient, pairingRegisterer: PairingRegisterer) -> AuthClient {
let logger = ConsoleLogger(loggingLevel: .off)
let keyValueStorage = UserDefaults.standard
let keychainStorage = KeychainStorage(serviceIdentifier: "com.walletconnect.sdk")
return AuthClientFactory.create(metadata: metadata, account: account, logger: logger, keyValueStorage: keyValueStorage, keychainStorage: keychainStorage, relayClient: relayClient, pairingRegisterer: pairingRegisterer)
return AuthClientFactory.create(metadata: metadata, account: account, logger: logger, keyValueStorage: keyValueStorage, keychainStorage: keychainStorage, networkingClient: networkingClient, pairingRegisterer: pairingRegisterer)
}

static func create(metadata: AppMetadata, account: Account?, logger: ConsoleLogging, keyValueStorage: KeyValueStorage, keychainStorage: KeychainStorageProtocol, relayClient: RelayClient, pairingRegisterer: PairingRegisterer) -> AuthClient {
static func create(metadata: AppMetadata, account: Account?, logger: ConsoleLogging, keyValueStorage: KeyValueStorage, keychainStorage: KeychainStorageProtocol, networkingClient: NetworkingClient, pairingRegisterer: PairingRegisterer) -> AuthClient {
let kms = KeyManagementService(keychain: keychainStorage)
let serializer = Serializer(kms: kms)
let history = RPCHistoryFactory.createForNetwork(keyValueStorage: keyValueStorage)
let networkingInteractor = NetworkingInteractor(relayClient: relayClient, serializer: serializer, logger: logger, rpcHistory: history)
let messageFormatter = SIWEMessageFormatter()
let appRequestService = AppRequestService(networkingInteractor: networkingInteractor, kms: kms, appMetadata: metadata, logger: logger)
let appRequestService = AppRequestService(networkingInteractor: networkingClient, kms: kms, appMetadata: metadata, logger: logger)
let messageSigner = MessageSigner(signer: Signer())
let appRespondSubscriber = AppRespondSubscriber(networkingInteractor: networkingInteractor, logger: logger, rpcHistory: history, signatureVerifier: messageSigner, pairingRegisterer: pairingRegisterer, messageFormatter: messageFormatter)
let walletErrorResponder = WalletErrorResponder(networkingInteractor: networkingInteractor, logger: logger, kms: kms, rpcHistory: history)
let walletRequestSubscriber = WalletRequestSubscriber(networkingInteractor: networkingInteractor, logger: logger, kms: kms, messageFormatter: messageFormatter, address: account?.address, walletErrorResponder: walletErrorResponder, pairingRegisterer: pairingRegisterer)
let walletRespondService = WalletRespondService(networkingInteractor: networkingInteractor, logger: logger, kms: kms, rpcHistory: history, walletErrorResponder: walletErrorResponder)
let appRespondSubscriber = AppRespondSubscriber(networkingInteractor: networkingClient, logger: logger, rpcHistory: history, signatureVerifier: messageSigner, pairingRegisterer: pairingRegisterer, messageFormatter: messageFormatter)
let walletErrorResponder = WalletErrorResponder(networkingInteractor: networkingClient, logger: logger, kms: kms, rpcHistory: history)
let walletRequestSubscriber = WalletRequestSubscriber(networkingInteractor: networkingClient, logger: logger, kms: kms, messageFormatter: messageFormatter, address: account?.address, walletErrorResponder: walletErrorResponder, pairingRegisterer: pairingRegisterer)
let walletRespondService = WalletRespondService(networkingInteractor: networkingClient, logger: logger, kms: kms, rpcHistory: history, walletErrorResponder: walletErrorResponder)
let pendingRequestsProvider = PendingRequestsProvider(rpcHistory: history)

return AuthClient(appRequestService: appRequestService,
Expand All @@ -35,7 +33,7 @@ public struct AuthClientFactory {
account: account,
pendingRequestsProvider: pendingRequestsProvider,
logger: logger,
socketConnectionStatusPublisher: relayClient.socketConnectionStatusPublisher,
socketConnectionStatusPublisher: networkingClient.socketConnectionStatusPublisher,
pairingRegisterer: pairingRegisterer)
}
}
2 changes: 1 addition & 1 deletion Sources/Chat/ChatClientFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public struct ChatClientFactory {
let topicToRegistryRecordStore = CodableStore<RegistryRecord>(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.topicToInvitationPubKey.rawValue)
let serialiser = Serializer(kms: kms)
let rpcHistory = RPCHistoryFactory.createForNetwork(keyValueStorage: keyValueStorage)
let networkingInteractor = NetworkingInteractor(relayClient: relayClient, serializer: serialiser, logger: logger, rpcHistory: rpcHistory)
let networkingInteractor = NetworkingClient(relayClient: relayClient, serializer: serialiser, logger: logger, rpcHistory: rpcHistory)
let invitePayloadStore = CodableStore<RequestSubscriptionPayload<Invite>>(defaults: keyValueStorage, identifier: StorageDomainIdentifiers.invite.rawValue)
let registryService = RegistryService(registry: registry, networkingInteractor: networkingInteractor, kms: kms, logger: logger, topicToRegistryRecordStore: topicToRegistryRecordStore)
let threadStore = Database<Thread>(keyValueStorage: keyValueStorage, identifier: StorageDomainIdentifiers.threads.rawValue)
Expand Down
2 changes: 1 addition & 1 deletion Sources/WalletConnectNetworking/NetworkInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import WalletConnectRelay
import WalletConnectUtils
import WalletConnectKMS

public class NetworkingInteractor: NetworkInteracting {
public class NetworkingClient: NetworkInteracting {
private var publishers = Set<AnyCancellable>()
private let relayClient: RelayClient
private let serializer: Serializing
Expand Down
46 changes: 46 additions & 0 deletions Sources/WalletConnectNetworking/Networking.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import WalletConnectRelay
import WalletConnectUtils
import WalletConnectKMS
import Foundation

public class Networking {

/// Networking client instance
public static var instance: NetworkingClient = {
guard let config = Networking.config else {
fatalError("Error - you must call Networking.configure(_:) before accessing the shared instance.")
}

return NetworkingClientFactory.create(relayClient: Relay.instance)
}()

private static var config: Config?

private init() { }

/// Networking instance config method
/// - Parameters:
/// - relayHost: relay host
/// - projectId: project id
/// - socketFactory: web socket factory
/// - socketConnectionType: socket connection type
static public func configure(
relayHost: String = "relay.walletconnect.com",
projectId: String,
socketFactory: WebSocketFactory,
socketConnectionType: SocketConnectionType = .automatic
) {
Networking.config = Networking.Config(
relayHost: relayHost,
projectId: projectId,
socketFactory: socketFactory,
socketConnectionType: socketConnectionType
)
Relay.configure(
relayHost: relayHost,
projectId: projectId,
socketFactory: socketFactory,
socketConnectionType: socketConnectionType)
}
}

Loading