Skip to content

Commit

Permalink
Update request method
Browse files Browse the repository at this point in the history
  • Loading branch information
llbartekll committed Aug 10, 2022
1 parent 94dd0c0 commit ce73441
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions Sources/Auth/AuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import Foundation
import Combine
import JSONRPC
import WalletConnectUtils
import WalletConnectPairing

class AuthClient {
enum Errors: Error {
case malformedPairingURI
case UnknownWalletAddress
case unknownWalletAddress
case noPairingMatchingTopic
}
private var authRequestPublisherSubject = PassthroughSubject<(id: RPCID, message: String), Never>()
public var authRequestPublisher: AnyPublisher<(id: RPCID, message: String), Never> {
Expand All @@ -27,6 +29,8 @@ class AuthClient {
private let walletRequestSubscriber: WalletRequestSubscriber
private let walletRespondService: WalletRespondService
private let cleanupService: CleanupService
private let pairingStorage: WCPairingStorage
public let logger: ConsoleLogging

private var account: Account?

Expand All @@ -38,7 +42,9 @@ class AuthClient {
walletRespondService: WalletRespondService,
account: Account?,
rpcHistory: RPCHistory,
cleanupService: CleanupService) {
cleanupService: CleanupService,
logger: ConsoleLogging,
pairingStorage: WCPairingStorage) {
self.appPairService = appPairService
self.appRequestService = appRequestService
self.walletPairService = walletPairService
Expand All @@ -48,6 +54,8 @@ class AuthClient {
self.account = account
self.rpcHistory = rpcHistory
self.cleanupService = cleanupService
self.logger = logger
self.pairingStorage = pairingStorage

setUpPublishers()
}
Expand All @@ -59,19 +67,29 @@ class AuthClient {
try await walletPairService.pair(pairingURI)
}

public func request(_ params: RequestParams) async throws -> String {
let uri = try await appPairService.create()
try await appRequestService.request(params: params, topic: uri.topic)
return uri.absoluteString
public func request(_ params: RequestParams, topic: String?) async throws -> String? {
logger.debug("Requesting Authentication")
if let topic = topic {
guard pairingStorage.hasPairing(forTopic: topic) else {
throw Errors.noPairingMatchingTopic
}
logger.debug("Requesting on existing pairing")
try await appRequestService.request(params: params, topic: topic)
return nil
} else {
let uri = try await appPairService.create()
try await appRequestService.request(params: params, topic: uri.topic)
return uri.absoluteString
}
}

public func respond(_ params: RespondParams) async throws {
guard let account = account else { throw Errors.UnknownWalletAddress }
guard let account = account else { throw Errors.unknownWalletAddress }
try await walletRespondService.respond(respondParams: params, account: account)
}

public func getPendingRequests() throws -> [AuthRequest] {
guard let account = account else { throw Errors.UnknownWalletAddress }
guard let account = account else { throw Errors.unknownWalletAddress }
let pendingRequests: [AuthRequest] = rpcHistory.getPending()
.filter {$0.request.method == "wc_authRequest"}
.compactMap {
Expand Down

0 comments on commit ce73441

Please sign in to comment.