Skip to content

Commit

Permalink
Merge pull request #533 from WalletConnect/feature/pair-activate-#525
Browse files Browse the repository at this point in the history
[Pairing] Activate Pair Service
  • Loading branch information
flypaper0 authored Oct 4, 2022
2 parents 9dbbbd0 + 0e7542b commit e862c0b
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Sources/Auth/AuthClientFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public struct AuthClientFactory {
let messageFormatter = SIWEMessageFormatter()
let appRequestService = AppRequestService(networkingInteractor: networkingInteractor, kms: kms, appMetadata: metadata, logger: logger)
let messageSigner = MessageSigner(signer: Signer())
let appRespondSubscriber = AppRespondSubscriber(networkingInteractor: networkingInteractor, logger: logger, rpcHistory: history, signatureVerifier: messageSigner, messageFormatter: messageFormatter)
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)
Expand Down
6 changes: 4 additions & 2 deletions Sources/Auth/Services/App/AppRespondSubscriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class AppRespondSubscriber {
private let rpcHistory: RPCHistory
private let signatureVerifier: MessageSignatureVerifying
private let messageFormatter: SIWEMessageFormatting
private let pairingRegisterer: PairingRegisterer
private var publishers = [AnyCancellable]()

var onResponse: ((_ id: RPCID, _ result: Result<Cacao, AuthError>) -> Void)?
Expand All @@ -19,12 +20,14 @@ class AppRespondSubscriber {
logger: ConsoleLogging,
rpcHistory: RPCHistory,
signatureVerifier: MessageSignatureVerifying,
pairingRegisterer: PairingRegisterer,
messageFormatter: SIWEMessageFormatting) {
self.networkingInteractor = networkingInteractor
self.logger = logger
self.rpcHistory = rpcHistory
self.signatureVerifier = signatureVerifier
self.messageFormatter = messageFormatter
self.pairingRegisterer = pairingRegisterer
subscribeForResponse()
}

Expand All @@ -38,8 +41,7 @@ class AppRespondSubscriber {
networkingInteractor.responseSubscription(on: AuthRequestProtocolMethod())
.sink { [unowned self] (payload: ResponseSubscriptionPayload<AuthRequestParams, Cacao>) in

//TODO - call pairing client to activate
// activatePairingIfNeeded(id: payload.id)
pairingRegisterer.activate(pairingTopic: payload.topic)
networkingInteractor.unsubscribe(topic: payload.topic)

let requestId = payload.id
Expand Down
1 change: 1 addition & 0 deletions Sources/Auth/Services/Wallet/WalletRequestSubscriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class WalletRequestSubscriber {
}
return
}
pairingRegisterer.activate(pairingTopic: payload.topic)
onRequest?(.init(id: payload.id, message: message))
}.store(in: &publishers)
}
Expand Down
8 changes: 7 additions & 1 deletion Sources/WalletConnectPairing/PairingClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class PairingClient: PairingRegisterer {

private let walletPairService: WalletPairService
private let appPairService: AppPairService
private let appPairActivateService: AppPairActivationService
private var pingResponsePublisherSubject = PassthroughSubject<String, Never>()
private let logger: ConsoleLogging
private let pingService: PairingPingService
Expand All @@ -30,6 +31,7 @@ public class PairingClient: PairingRegisterer {
walletPairService: WalletPairService,
deletePairingService: DeletePairingService,
pairingRequestsSubscriber: PairingRequestsSubscriber,
appPairActivateService: AppPairActivationService,
pairingStorage: WCPairingStorage,
cleanupService: CleanupService,
pingService: PairingPingService,
Expand All @@ -43,6 +45,7 @@ public class PairingClient: PairingRegisterer {
self.logger = logger
self.pairingStorage = pairingStorage
self.deletePairingService = deletePairingService
self.appPairActivateService = appPairActivateService
self.cleanupService = cleanupService
self.pingService = pingService
self.pairingRequestsSubscriber = pairingRequestsSubscriber
Expand Down Expand Up @@ -97,7 +100,6 @@ public class PairingClient: PairingRegisterer {

public func disconnect(topic: String) async throws {
try await deletePairingService.delete(topic: topic)

}

public func validatePairingExistance(_ topic: String) throws {
Expand All @@ -109,6 +111,10 @@ public class PairingClient: PairingRegisterer {
return pairingRequestsSubscriber.subscribeForRequest(method)
}

public func activate(pairingTopic: String) {
appPairActivateService.activate(for: pairingTopic)
}

#if DEBUG
/// Delete all stored data such as: pairings, keys
///
Expand Down
2 changes: 2 additions & 0 deletions Sources/WalletConnectPairing/PairingClientFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public struct PairingClientFactory {
let cleanupService = CleanupService(pairingStore: pairingStore, kms: kms)
let deletePairingService = DeletePairingService(networkingInteractor: networkingInteractor, kms: kms, pairingStorage: pairingStore, logger: logger)
let pingService = PairingPingService(pairingStorage: pairingStore, networkingInteractor: networkingInteractor, logger: logger)
let appPairActivateService = AppPairActivationService(pairingStorage: pairingStore, logger: logger)

return PairingClient(
appPairService: appPairService,
Expand All @@ -34,6 +35,7 @@ public struct PairingClientFactory {
walletPairService: walletPairService,
deletePairingService: deletePairingService,
pairingRequestsSubscriber: pairingRequestsSubscriber,
appPairActivateService: appPairActivateService,
pairingStorage: pairingStore,
cleanupService: cleanupService,
pingService: pingService,
Expand Down
6 changes: 5 additions & 1 deletion Sources/WalletConnectPairing/PairingRegisterer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import Combine
import JSONRPC

public protocol PairingRegisterer {
func register<RequestParams>(method: ProtocolMethod) -> AnyPublisher<RequestSubscriptionPayload<RequestParams>, Never> where RequestParams : Codable
func register<RequestParams: Codable>(
method: ProtocolMethod
) -> AnyPublisher<RequestSubscriptionPayload<RequestParams>, Never>

func activate(pairingTopic: String)
func validatePairingExistance(_ topic: String) throws
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Foundation
import Combine
import WalletConnectNetworking
import WalletConnectUtils

final class AppPairActivationService {
private let pairingStorage: PairingStorage
private let logger: ConsoleLogging

init(pairingStorage: PairingStorage, logger: ConsoleLogging) {
self.pairingStorage = pairingStorage
self.logger = logger
}

func activate(for topic: String) {
guard var pairing = pairingStorage.getPairing(forTopic: topic) else {
return logger.error("Pairing not found for topic: \(topic)")
}
if !pairing.active {
pairing.activate()
} else {
try? pairing.updateExpiry()
}
pairingStorage.setPairing(pairing)
}
}

This file was deleted.

4 changes: 4 additions & 0 deletions Tests/AuthTests/AppRespondSubscriberTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ class AppRespondSubscriberTests: XCTestCase {
let prvKey = Data(hex: "462c1dad6832d7d96ccf87bd6a686a4110e114aaaebd5512e552c0e3a87b480f")
var messageSigner: MessageSigner!
var pairingStorage: WCPairingStorageMock!
var pairingRegisterer: PairingRegistererMock<AuthRequestParams>!

override func setUp() {
networkingInteractor = NetworkingInteractorMock()
messageFormatter = SIWEMessageFormatter()
messageSigner = MessageSigner()
rpcHistory = RPCHistoryFactory.createForNetwork(keyValueStorage: RuntimeKeyValueStorage())
pairingStorage = WCPairingStorageMock()
pairingRegisterer = PairingRegistererMock<AuthRequestParams>()
sut = AppRespondSubscriber(
networkingInteractor: networkingInteractor,
logger: ConsoleLoggerMock(),
rpcHistory: rpcHistory,
signatureVerifier: messageSigner,
pairingRegisterer: pairingRegisterer,
messageFormatter: messageFormatter)
}

Expand Down Expand Up @@ -62,6 +65,7 @@ class AppRespondSubscriberTests: XCTestCase {
networkingInteractor.responsePublisherSubject.send((topic, request, response))

wait(for: [messageExpectation], timeout: defaultTimeout)
XCTAssertTrue(pairingRegisterer.isActivateCalled)
XCTAssertEqual(result, .failure(AuthError.messageCompromised))
XCTAssertEqual(messageId, requestId)
}
Expand Down
9 changes: 8 additions & 1 deletion Tests/AuthTests/Mocks/PairingRegistererMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ import WalletConnectPairing
import Combine
import WalletConnectNetworking

struct PairingRegistererMock<RequestParams>: PairingRegisterer where RequestParams : Codable {
class PairingRegistererMock<RequestParams>: PairingRegisterer where RequestParams : Codable {

let subject = PassthroughSubject<RequestSubscriptionPayload<RequestParams>, Never>()

var isActivateCalled: Bool = false

func register<RequestParams>(method: ProtocolMethod) -> AnyPublisher<RequestSubscriptionPayload<RequestParams>, Never> where RequestParams : Decodable, RequestParams : Encodable {
subject.eraseToAnyPublisher() as! AnyPublisher<RequestSubscriptionPayload<RequestParams>, Never>
}

func activate(pairingTopic: String) {
isActivateCalled = true
}

func validatePairingExistance(_ topic: String) throws {

}
}
3 changes: 1 addition & 2 deletions Tests/AuthTests/WalletRequestSubscriberTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ class WalletRequestSubscriberTests: XCTestCase {
messageExpectation.fulfill()
}



let payload = RequestSubscriptionPayload<AuthRequestParams>(id: expectedRequestId, topic: "123", request: AuthRequestParams.stub(id: expectedRequestId))

pairingRegisterer.subject.send(payload)

wait(for: [messageExpectation], timeout: defaultTimeout)
XCTAssertTrue(pairingRegisterer.isActivateCalled)
XCTAssertEqual(message, expectedMessage)
XCTAssertEqual(messageId, expectedRequestId)
}
Expand Down

0 comments on commit e862c0b

Please sign in to comment.