Skip to content

Commit

Permalink
add pair to sample apps and fix integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
llbartekll committed Sep 30, 2022
1 parent 10c13c5 commit 6d8f836
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 61 deletions.
4 changes: 3 additions & 1 deletion Example/DApp/Auth/AuthViewModel.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import UIKit
import Combine
import Auth
import WalletConnectPairing

final class AuthViewModel: ObservableObject {

Expand All @@ -27,7 +28,8 @@ final class AuthViewModel: ObservableObject {
func setupInitialState() async throws {
state = .none
uri = nil
uri = try await Auth.instance.request(.stub()).absoluteString
let uri = try! await Pair.instance.create()
try await Auth.instance.request(.stub(), topic: uri.topic)
}

func copyDidPressed() {
Expand Down
122 changes: 64 additions & 58 deletions Example/IntegrationTests/Auth/AuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,62 +59,68 @@ final class AuthTests: XCTestCase {
wait(for: [requestExpectation], timeout: InputConfig.defaultTimeout)
}

// func testRespondSuccess() async {
// let responseExpectation = expectation(description: "successful response delivered")
// let uri = try! await app.request(RequestParams.stub())
// try! await wallet.pair(uri: uri)
// walletAuthClient.authRequestPublisher.sink { [unowned self] request in
// Task(priority: .high) {
// let signature = try! MessageSigner().sign(message: request.message, privateKey: prvKey)
// try! await walletAuthClient.respond(requestId: request.id, signature: signature)
// }
// }
// .store(in: &publishers)
// appAuthClient.authResponsePublisher.sink { (_, result) in
// guard case .success = result else { XCTFail(); return }
// responseExpectation.fulfill()
// }
// .store(in: &publishers)
// wait(for: [responseExpectation], timeout: InputConfig.defaultTimeout)
// }
//
// func testUserRespondError() async {
// let responseExpectation = expectation(description: "error response delivered")
// let uri = try! await app.request(RequestParams.stub())
// try! await wallet.pair(uri: uri)
// walletAuthClient.authRequestPublisher.sink { [unowned self] request in
// Task(priority: .high) {
// try! await walletAuthClient.reject(requestId: request.id)
// }
// }
// .store(in: &publishers)
// appAuthClient.authResponsePublisher.sink { (_, result) in
// guard case .failure(let error) = result else { XCTFail(); return }
// XCTAssertEqual(error, .userRejeted)
// responseExpectation.fulfill()
// }
// .store(in: &publishers)
// wait(for: [responseExpectation], timeout: InputConfig.defaultTimeout)
// }
//
// func testRespondSignatureVerificationFailed() async {
// let responseExpectation = expectation(description: "invalid signature response delivered")
// let uri = try! await app.request(RequestParams.stub())
// try! await wallet.pair(uri: uri)
// walletAuthClient.authRequestPublisher.sink { [unowned self] request in
// Task(priority: .high) {
// let invalidSignature = "438effc459956b57fcd9f3dac6c675f9cee88abf21acab7305e8e32aa0303a883b06dcbd956279a7a2ca21ffa882ff55cc22e8ab8ec0f3fe90ab45f306938cfa1b"
// let cacaoSignature = CacaoSignature(t: "eip191", s: invalidSignature)
// try! await walletAuthClient.respond(requestId: request.id, signature: cacaoSignature)
// }
// }
// .store(in: &publishers)
// appAuthClient.authResponsePublisher.sink { (_, result) in
// guard case .failure(let error) = result else { XCTFail(); return }
// XCTAssertEqual(error, .signatureVerificationFailed)
// responseExpectation.fulfill()
// }
// .store(in: &publishers)
// wait(for: [responseExpectation], timeout: InputConfig.defaultTimeout)
// }
func testRespondSuccess() async {
let responseExpectation = expectation(description: "successful response delivered")
let uri = try! await appPairingClient.create()
try! await appAuthClient.request(RequestParams.stub(), topic: uri.topic)

try! await walletPairingClient.pair(uri: uri)
walletAuthClient.authRequestPublisher.sink { [unowned self] request in
Task(priority: .high) {
let signature = try! MessageSigner().sign(message: request.message, privateKey: prvKey)
try! await walletAuthClient.respond(requestId: request.id, signature: signature)
}
}
.store(in: &publishers)
appAuthClient.authResponsePublisher.sink { (_, result) in
guard case .success = result else { XCTFail(); return }
responseExpectation.fulfill()
}
.store(in: &publishers)
wait(for: [responseExpectation], timeout: InputConfig.defaultTimeout)
}

func testUserRespondError() async {
let responseExpectation = expectation(description: "error response delivered")
let uri = try! await appPairingClient.create()
try! await appAuthClient.request(RequestParams.stub(), topic: uri.topic)

try! await walletPairingClient.pair(uri: uri)
walletAuthClient.authRequestPublisher.sink { [unowned self] request in
Task(priority: .high) {
try! await walletAuthClient.reject(requestId: request.id)
}
}
.store(in: &publishers)
appAuthClient.authResponsePublisher.sink { (_, result) in
guard case .failure(let error) = result else { XCTFail(); return }
XCTAssertEqual(error, .userRejeted)
responseExpectation.fulfill()
}
.store(in: &publishers)
wait(for: [responseExpectation], timeout: InputConfig.defaultTimeout)
}

func testRespondSignatureVerificationFailed() async {
let responseExpectation = expectation(description: "invalid signature response delivered")
let uri = try! await appPairingClient.create()
try! await appAuthClient.request(RequestParams.stub(), topic: uri.topic)

try! await walletPairingClient.pair(uri: uri)
walletAuthClient.authRequestPublisher.sink { [unowned self] request in
Task(priority: .high) {
let invalidSignature = "438effc459956b57fcd9f3dac6c675f9cee88abf21acab7305e8e32aa0303a883b06dcbd956279a7a2ca21ffa882ff55cc22e8ab8ec0f3fe90ab45f306938cfa1b"
let cacaoSignature = CacaoSignature(t: "eip191", s: invalidSignature)
try! await walletAuthClient.respond(requestId: request.id, signature: cacaoSignature)
}
}
.store(in: &publishers)
appAuthClient.authResponsePublisher.sink { (_, result) in
guard case .failure(let error) = result else { XCTFail(); return }
XCTAssertEqual(error, .signatureVerificationFailed)
responseExpectation.fulfill()
}
.store(in: &publishers)
wait(for: [responseExpectation], timeout: InputConfig.defaultTimeout)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import UIKit
import Auth
import WalletConnectPairing

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

Expand Down Expand Up @@ -30,7 +31,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {

let uri = context.url.absoluteString.replacingOccurrences(of: "showcase://wc?uri=", with: "")
Task {
try await Auth.instance.pair(uri: WalletConnectURI(string: uri)!)
try await Pair.instance.pair(uri: WalletConnectURI(string: uri)!)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Combine
import Auth
import WalletConnectPairing

final class WalletInteractor {

func pair(uri: WalletConnectURI) async throws {
try await Auth.instance.pair(uri: uri)
try await Pair.instance.pair(uri: uri)
}

var requestPublisher: AnyPublisher<AuthRequest, Never> {
Expand Down

0 comments on commit 6d8f836

Please sign in to comment.