-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[Auth] #378 auth client
- Loading branch information
Showing
16 changed files
with
163 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,113 @@ | ||
import Foundation | ||
import Combine | ||
import WalletConnectUtils | ||
import WalletConnectPairing | ||
|
||
class AuthClient { | ||
enum Errors: Error { | ||
case malformedPairingURI | ||
case unknownWalletAddress | ||
case noPairingMatchingTopic | ||
} | ||
private var authRequestPublisherSubject = PassthroughSubject<(id: RPCID, message: String), Never>() | ||
public var authRequestPublisher: AnyPublisher<(id: RPCID, message: String), Never> { | ||
authRequestPublisherSubject.eraseToAnyPublisher() | ||
} | ||
|
||
private var authResponsePublisherSubject = PassthroughSubject<(id: RPCID, cacao: Cacao), Never>() | ||
public var authResponsePublisher: AnyPublisher<(id: RPCID, cacao: Cacao), Never> { | ||
authResponsePublisherSubject.eraseToAnyPublisher() | ||
} | ||
|
||
private let appPairService: AppPairService | ||
private let appRequestService: AuthRequestService | ||
private let appRequestService: AppRequestService | ||
private let appRespondSubscriber: AppRespondSubscriber | ||
|
||
private let walletPairService: WalletPairService | ||
private let walletRequestSubscriber: WalletRequestSubscriber | ||
private let walletRespondService: WalletRespondService | ||
private let cleanupService: CleanupService | ||
private let pairingStorage: WCPairingStorage | ||
private let pendingRequestsProvider: PendingRequestsProvider | ||
public let logger: ConsoleLogging | ||
|
||
private var account: Account? | ||
|
||
init(appPairService: AppPairService, appRequestService: AuthRequestService, walletPairService: WalletPairService) { | ||
init(appPairService: AppPairService, | ||
appRequestService: AppRequestService, | ||
appRespondSubscriber: AppRespondSubscriber, | ||
walletPairService: WalletPairService, | ||
walletRequestSubscriber: WalletRequestSubscriber, | ||
walletRespondService: WalletRespondService, | ||
account: Account?, | ||
pendingRequestsProvider: PendingRequestsProvider, | ||
cleanupService: CleanupService, | ||
logger: ConsoleLogging, | ||
pairingStorage: WCPairingStorage) { | ||
self.appPairService = appPairService | ||
self.appRequestService = appRequestService | ||
self.walletPairService = walletPairService | ||
self.walletRequestSubscriber = walletRequestSubscriber | ||
self.walletRespondService = walletRespondService | ||
self.appRespondSubscriber = appRespondSubscriber | ||
self.account = account | ||
self.pendingRequestsProvider = pendingRequestsProvider | ||
self.cleanupService = cleanupService | ||
self.logger = logger | ||
self.pairingStorage = pairingStorage | ||
|
||
setUpPublishers() | ||
} | ||
|
||
func request(params: RequestParams) async throws -> String { | ||
public func pair(uri: String) async throws { | ||
guard let pairingURI = WalletConnectURI(string: uri) else { | ||
throw Errors.malformedPairingURI | ||
} | ||
try await walletPairService.pair(pairingURI) | ||
} | ||
|
||
public func request(_ params: RequestParams) async throws -> String { | ||
logger.debug("Requesting Authentication") | ||
let uri = try await appPairService.create() | ||
try await appRequestService.request(params: params, topic: uri.topic) | ||
return uri.absoluteString | ||
} | ||
|
||
func pair(uri: String) async throws { | ||
guard let pairingURI = WalletConnectURI(string: uri) else { | ||
throw Errors.malformedPairingURI | ||
public func request(_ params: RequestParams, topic: String) async throws { | ||
logger.debug("Requesting Authentication on existing pairing") | ||
guard pairingStorage.hasPairing(forTopic: topic) else { | ||
throw Errors.noPairingMatchingTopic | ||
} | ||
try await appRequestService.request(params: params, topic: topic) | ||
} | ||
|
||
public func respond(_ params: RespondParams) async throws { | ||
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 } | ||
return try pendingRequestsProvider.getPendingRequests(account: account) | ||
} | ||
|
||
#if DEBUG | ||
/// Delete all stored data sach as: pairings, sessions, keys | ||
/// | ||
/// - Note: Doesn't unsubscribe from topics | ||
public func cleanup() throws { | ||
try cleanupService.cleanup() | ||
} | ||
#endif | ||
|
||
private func setUpPublishers() { | ||
appRespondSubscriber.onResponse = { [unowned self] (id, cacao) in | ||
authResponsePublisherSubject.send((id, cacao)) | ||
} | ||
|
||
walletRequestSubscriber.onRequest = { [unowned self] (id, message) in | ||
authRequestPublisherSubject.send((id, message)) | ||
} | ||
try await walletPairService.pair(pairingURI) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Foundation | ||
import WalletConnectKMS | ||
import WalletConnectUtils | ||
import WalletConnectPairing | ||
|
||
final class CleanupService { | ||
|
||
private let pairingStore: WCPairingStorage | ||
private let kms: KeyManagementServiceProtocol | ||
|
||
init(pairingStore: WCPairingStorage, kms: KeyManagementServiceProtocol, sessionToPairingTopic: CodableStore<String>) { | ||
self.pairingStore = pairingStore | ||
self.kms = kms | ||
} | ||
|
||
func cleanup() throws { | ||
pairingStore.deleteAll() | ||
try kms.deleteAll() | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
Sources/Auth/Services/Wallet/PendingRequestsProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import Foundation | ||
import JSONRPC | ||
import WalletConnectUtils | ||
|
||
class PendingRequestsProvider { | ||
private let rpcHistory: RPCHistory | ||
|
||
init(rpcHistory: RPCHistory) { | ||
self.rpcHistory = rpcHistory | ||
} | ||
|
||
public func getPendingRequests(account: Account) throws -> [AuthRequest] { | ||
let pendingRequests: [AuthRequest] = rpcHistory.getPending() | ||
.filter {$0.request.method == "wc_authRequest"} | ||
.compactMap { | ||
guard let params = try? $0.request.params?.get(AuthRequestParams.self) else {return nil} | ||
let message = SIWEMessageFormatter().formatMessage(from: params.payloadParams, address: account.address) | ||
return AuthRequest(id: $0.request.id!, message: message) | ||
} | ||
return pendingRequests | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import WalletConnectUtils | ||
import Foundation | ||
|
||
typealias Account = WalletConnectUtils.Account |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import Foundation | ||
import JSONRPC | ||
|
||
public typealias RPCID = JSONRPC.RPCID |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Foundation | ||
|
||
public struct AuthRequest: Equatable, Codable { | ||
public let id: RPCID | ||
/// EIP-4361: Sign-In with Ethereum message | ||
public let message: String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters