-
Notifications
You must be signed in to change notification settings - Fork 9
/
SignWithFactorSourcesOfKindLedger.swift
72 lines (62 loc) · 2.06 KB
/
SignWithFactorSourcesOfKindLedger.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import ComposableArchitecture
import SwiftUI
// MARK: - SignWithFactorSourcesOfKindLedger
public struct SignWithFactorSourcesOfKindLedger: SignWithFactorSourcesOfKindReducer {
public typealias Factor = LedgerHardwareWalletFactorSource
public typealias State = SignWithFactorSourcesOfKindState<Factor>
public enum ViewAction: SignWithFactorSourcesOfKindViewActionProtocol {
case onFirstTask
case retryButtonTapped
}
public enum InternalAction: SignWithFactorSourcesOfKindInternalActionProtocol {
case signingWithFactor(SigningFactor)
}
public enum DelegateAction: SignWithFactorSourcesOfKindDelegateActionProtocol {
case done(signingFactors: NonEmpty<Set<SigningFactor>>, signatures: Set<SignatureOfEntity>)
case failedToSign(SigningFactor)
}
@Dependency(\.ledgerHardwareWalletClient) var ledgerHardwareWalletClient
@Dependency(\.appPreferencesClient) var appPreferencesClient
public init() {}
public func reduce(into state: inout State, viewAction: ViewAction) -> Effect<Action> {
switch viewAction {
case .onFirstTask:
signWithSigningFactors(of: state)
case .retryButtonTapped:
signWithSigningFactors(of: state)
}
}
public func reduce(
into state: inout State,
internalAction: InternalAction
) -> Effect<Action> {
switch internalAction {
case let .signingWithFactor(currentLedger):
state.currentSigningFactor = currentLedger
return .none
}
}
public func sign(
signers: SigningFactor.Signers,
factor ledger: Factor,
state: State
) async throws -> Set<SignatureOfEntity> {
switch state.signingPurposeWithPayload {
case let .signTransaction(_, intent, _):
try await ledgerHardwareWalletClient.signTransaction(.init(
ledger: ledger,
signers: signers,
transactionIntent: intent,
displayHashOnLedgerDisplay: false
))
case let .signAuth(authToSign):
try await ledgerHardwareWalletClient.signAuthChallenge(.init(
ledger: ledger,
signers: signers,
challenge: authToSign.input.challenge,
origin: authToSign.input.origin,
dAppDefinitionAddress: authToSign.input.dAppDefinitionAddress
))
}
}
}