Skip to content

Commit

Permalink
Merge pull request #731 from WalletConnect/w3w-new-methods
Browse files Browse the repository at this point in the history
Expose new methods to web3wallet
  • Loading branch information
llbartekll authored Feb 24, 2023
2 parents c69aca1 + 53d2830 commit 89fab4c
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 2 deletions.
1 change: 1 addition & 0 deletions Sources/WalletConnectPairing/PairingClientProtocol.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
public protocol PairingClientProtocol {
func pair(uri: WalletConnectURI) async throws
func disconnect(topic: String) async throws
func getPairings() -> [Pairing]
}
5 changes: 5 additions & 0 deletions Sources/WalletConnectSign/Sign/SignClientProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ public protocol SignClientProtocol {
var sessionProposalPublisher: AnyPublisher<Session.Proposal, Never> { get }
var sessionRequestPublisher: AnyPublisher<Request, Never> { get }
var sessionsPublisher: AnyPublisher<[Session], Never> { get }
var socketConnectionStatusPublisher: AnyPublisher<SocketConnectionStatus, Never> { get }
var sessionSettlePublisher: AnyPublisher<Session, Never> { get }
var sessionDeletePublisher: AnyPublisher<(String, Reason), Never> { get }
var sessionResponsePublisher: AnyPublisher<Response, Never> { get }

func approve(proposalId: String, namespaces: [String: SessionNamespace]) async throws
func reject(proposalId: String, reason: RejectionReason) async throws
Expand All @@ -15,6 +19,7 @@ public protocol SignClientProtocol {
func pair(uri: WalletConnectURI) async throws
func disconnect(topic: String) async throws
func getSessions() -> [Session]
func cleanup() async throws

func getPendingRequests(topic: String?) -> [Request]
func getSessionRequestRecord(id: RPCID) -> Request?
Expand Down
37 changes: 37 additions & 0 deletions Sources/Web3Wallet/Web3WalletClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,32 @@ public class Web3WalletClient {
public var sessionsPublisher: AnyPublisher<[Session], Never> {
signClient.sessionsPublisher.eraseToAnyPublisher()
}

/// Publisher that sends web socket connection status
public var socketConnectionStatusPublisher: AnyPublisher<SocketConnectionStatus, Never> {
signClient.socketConnectionStatusPublisher.eraseToAnyPublisher()
}

/// Publisher that sends session when one is settled
///
/// Event is emited on proposer and responder client when both communicating peers have successfully established a session.
public var sessionSettlePublisher: AnyPublisher<Session, Never> {
signClient.sessionSettlePublisher.eraseToAnyPublisher()
}

/// Publisher that sends deleted session topic
///
/// Event can be emited on any type of the client.
public var sessionDeletePublisher: AnyPublisher<(String, Reason), Never> {
signClient.sessionDeletePublisher.eraseToAnyPublisher()
}

/// Publisher that sends response for session request
///
/// In most cases that event will be emited on dApp client.
public var sessionResponsePublisher: AnyPublisher<Response, Never> {
signClient.sessionResponsePublisher.eraseToAnyPublisher()
}

// MARK: - Private Properties
private let authClient: AuthClientProtocol
Expand Down Expand Up @@ -176,4 +202,15 @@ public class Web3WalletClient {
public func getPendingRequests() throws -> [AuthRequest] {
try authClient.getPendingRequests()
}

/// Delete all stored data such as: pairings, sessions, keys
///
/// - Note: Will unsubscribe from all topics
public func cleanup() async throws {
try await signClient.cleanup()
}

public func getPairings() -> [Pairing] {
return pairingClient.getPairings()
}
}
4 changes: 4 additions & 0 deletions Tests/Web3WalletTests/Mocks/PairingClientMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ final class PairingClientMock: PairingClientProtocol {
func disconnect(topic: String) async throws {
disconnectPairingCalled = true
}

func getPairings() -> [Pairing] {
return [Pairing(topic: "", peer: nil, expiryDate: Date())]
}
}
12 changes: 12 additions & 0 deletions Tests/Web3WalletTests/Mocks/ReasonMock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Foundation
import WalletConnectSign

struct ReasonMock: WalletConnectNetworking.Reason {
var code: Int {
return 0
}

var message: String {
return "error"
}
}
29 changes: 27 additions & 2 deletions Tests/Web3WalletTests/Mocks/SignClientMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import Combine

@testable import WalletConnectSign


final class SignClientMock: SignClientProtocol {


var approveCalled = false
var rejectCalled = false
var updateCalled = false
Expand All @@ -14,9 +13,11 @@ final class SignClientMock: SignClientProtocol {
var emitCalled = false
var pairCalled = false
var disconnectCalled = false
var cleanupCalled = false

private let metadata = AppMetadata(name: "", description: "", url: "", icons: [])
private let request = WalletConnectSign.Request(id: .left(""), topic: "", method: "", params: "", chainId: Blockchain("eip155:1")!, expiry: nil)
private let response = WalletConnectSign.Response(id: RPCID(1234567890123456789), topic: "", chainId: "", result: .response(AnyCodable(any: "")))

var sessionProposalPublisher: AnyPublisher<WalletConnectSign.Session.Proposal, Never> {
let proposer = Participant(publicKey: "", metadata: metadata)
Expand All @@ -42,6 +43,26 @@ final class SignClientMock: SignClientProtocol {
.eraseToAnyPublisher()
}

var socketConnectionStatusPublisher: AnyPublisher<WalletConnectRelay.SocketConnectionStatus, Never> {
return Result.Publisher(.connected)
.eraseToAnyPublisher()
}

var sessionSettlePublisher: AnyPublisher<WalletConnectSign.Session, Never> {
return Result.Publisher(Session(topic: "", pairingTopic: "", peer: metadata, namespaces: [:], expiryDate: Date()))
.eraseToAnyPublisher()
}

var sessionDeletePublisher: AnyPublisher<(String, WalletConnectNetworking.Reason), Never> {
return Result.Publisher(("topic", ReasonMock()))
.eraseToAnyPublisher()
}

var sessionResponsePublisher: AnyPublisher<WalletConnectSign.Response, Never> {
return Result.Publisher(.success(response))
.eraseToAnyPublisher()
}

func approve(proposalId: String, namespaces: [String : WalletConnectSign.SessionNamespace]) async throws {
approveCalled = true
}
Expand Down Expand Up @@ -85,4 +106,8 @@ final class SignClientMock: SignClientProtocol {
func getSessionRequestRecord(id: JSONRPC.RPCID) -> WalletConnectSign.Request? {
return request
}

func cleanup() async throws {
cleanupCalled = true
}
}
69 changes: 69 additions & 0 deletions Tests/Web3WalletTests/Web3WalletTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,66 @@ final class Web3WalletTests: XCTestCase {
}
}

func testSocketConnectionStatusCalled() {
var success = false
web3WalletClient.socketConnectionStatusPublisher.sink { value in
success = true
XCTAssertTrue(true)
}
.store(in: &disposeBag)

let expectation = expectation(description: "Fail after 0.1s timeout")
let result = XCTWaiter.wait(for: [expectation], timeout: 0.1)
if result == XCTWaiter.Result.timedOut && success == false {
XCTFail()
}
}

func testSessionSettleCalled() {
var success = false
web3WalletClient.sessionSettlePublisher.sink { value in
success = true
XCTAssertTrue(true)
}
.store(in: &disposeBag)

let expectation = expectation(description: "Fail after 0.1s timeout")
let result = XCTWaiter.wait(for: [expectation], timeout: 0.1)
if result == XCTWaiter.Result.timedOut && success == false {
XCTFail()
}
}

func testSessionDeleteCalled() {
var success = false
web3WalletClient.sessionDeletePublisher.sink { value in
success = true
XCTAssertTrue(true)
}
.store(in: &disposeBag)

let expectation = expectation(description: "Fail after 0.1s timeout")
let result = XCTWaiter.wait(for: [expectation], timeout: 0.1)
if result == XCTWaiter.Result.timedOut && success == false {
XCTFail()
}
}

func testSessionResponseCalled() {
var success = false
web3WalletClient.sessionResponsePublisher.sink { value in
success = true
XCTAssertTrue(true)
}
.store(in: &disposeBag)

let expectation = expectation(description: "Fail after 0.1s timeout")
let result = XCTWaiter.wait(for: [expectation], timeout: 0.1)
if result == XCTWaiter.Result.timedOut && success == false {
XCTFail()
}
}

func testApproveCalled() async {
try! await web3WalletClient.approve(proposalId: "", namespaces: [:])
XCTAssertTrue(signClient.approveCalled)
Expand Down Expand Up @@ -194,4 +254,13 @@ final class Web3WalletTests: XCTestCase {
let pendingRequests = try! web3WalletClient.getPendingRequests()
XCTAssertEqual(1, pendingRequests.count)
}

func testCleanupCalled() async {
try! await web3WalletClient.cleanup()
XCTAssertTrue(signClient.cleanupCalled)
}

func testGetPairingsNotEmpty() async {
XCTAssertEqual(1, web3WalletClient.getPairings().count)
}
}

0 comments on commit 89fab4c

Please sign in to comment.