-
Notifications
You must be signed in to change notification settings - Fork 9
/
AddLedgerFactorSource.swift
242 lines (196 loc) · 6.7 KB
/
AddLedgerFactorSource.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import ComposableArchitecture
import SwiftUI
public typealias LedgerDeviceInfo = P2P.ConnectorExtension.Response.LedgerHardwareWallet.Success.GetDeviceInfo
// MARK: - AddLedgerFactorSource
public struct AddLedgerFactorSource: Sendable, FeatureReducer {
// MARK: AddLedgerFactorSource
public struct State: Sendable, Hashable {
public var isWaitingForResponseFromLedger = false
public var unnamedDeviceToAdd: LedgerDeviceInfo?
@PresentationState
public var destination: Destination.State? = nil
public init() {}
}
// MARK: Action
public enum ViewAction: Sendable, Equatable {
case sendAddLedgerRequestButtonTapped
case closeButtonTapped
}
public enum InternalAction: Sendable, Equatable {
case getDeviceInfoResult(TaskResult<LedgerDeviceInfo>)
case alreadyExists(LedgerHardwareWalletFactorSource)
case proceedToNameDevice(LedgerDeviceInfo)
}
public enum DelegateAction: Sendable, Equatable {
case completed(LedgerHardwareWalletFactorSource)
case failedToAddLedger
case dismiss
}
// MARK: Destination
public struct Destination: DestinationReducer {
public enum State: Sendable, Hashable {
case ledgerAlreadyExistsAlert(AlertState<Never>)
case nameLedger(NameLedgerFactorSource.State)
}
public enum Action: Sendable, Equatable {
case ledgerAlreadyExistsAlert(Never)
case nameLedger(NameLedgerFactorSource.Action)
}
public var body: some ReducerOf<Self> {
Scope(state: /State.nameLedger, action: /Action.nameLedger) {
NameLedgerFactorSource()
}
}
}
// MARK: Reduce
@Dependency(\.errorQueue) var errorQueue
@Dependency(\.factorSourcesClient) var factorSourcesClient
@Dependency(\.ledgerHardwareWalletClient) var ledgerHardwareWalletClient
@Dependency(\.radixConnectClient) var radixConnectClient
public init() {}
public var body: some ReducerOf<Self> {
Reduce(core)
.ifLet(destinationPath, action: /Action.destination) {
Destination()
}
}
private let destinationPath: WritableKeyPath<State, PresentationState<Destination.State>> = \.$destination
public func reduce(into state: inout State, viewAction: ViewAction) -> Effect<Action> {
switch viewAction {
case .sendAddLedgerRequestButtonTapped:
sendAddLedgerRequestEffect(&state)
case .closeButtonTapped:
.send(.delegate(.dismiss))
}
}
public func reduce(into state: inout State, internalAction: InternalAction) -> Effect<Action> {
switch internalAction {
case let .getDeviceInfoResult(.success(ledgerDeviceInfo)):
return gotDeviceEffect(ledgerDeviceInfo, in: &state)
case let .getDeviceInfoResult(.failure(error)):
return failedToGetDevice(&state, error: error)
case let .alreadyExists(ledger):
state.destination = .ledgerAlreadyExistsAlert(.ledgerAlreadyExists(ledger))
return .none
case let .proceedToNameDevice(device):
state.destination = .nameLedger(.init(deviceInfo: device))
return .none
}
}
public func reduce(into state: inout State, presentedAction: Destination.Action) -> Effect<Action> {
switch presentedAction {
case let .nameLedger(.delegate(.complete(ledger))):
completeWithLedgerEffect(ledger)
case .nameLedger(.delegate(.failedToCreateLedgerFactorSource)):
.send(.delegate(.failedToAddLedger))
default:
.none
}
}
// MARK: Helper methods
private func sendAddLedgerRequestEffect(_ state: inout State) -> Effect<Action> {
state.isWaitingForResponseFromLedger = true
return .run { send in
let result = await TaskResult {
try await ledgerHardwareWalletClient.getDeviceInfo()
}
await send(.internal(.getDeviceInfoResult(result)))
}
}
private func gotDeviceEffect(_ ledgerDeviceInfo: LedgerDeviceInfo, in state: inout State) -> Effect<Action> {
state.isWaitingForResponseFromLedger = false
loggerGlobal.notice("Successfully received response from CE! \(ledgerDeviceInfo) ✅")
return .run { send in
if let ledger = try await factorSourcesClient.getFactorSource(
id: .init(kind: .ledgerHQHardwareWallet, hash: ledgerDeviceInfo.id.data.data),
as: LedgerHardwareWalletFactorSource.self
) {
await send(.internal(.alreadyExists(ledger)))
} else {
await send(.internal(.proceedToNameDevice(ledgerDeviceInfo)))
}
} catch: { error, _ in
errorQueue.schedule(error)
}
}
private func failedToGetDevice(_ state: inout State, error: Swift.Error) -> Effect<Action> {
state.isWaitingForResponseFromLedger = false
loggerGlobal.error("Failed to get ledger device info: \(error)")
errorQueue.schedule(error)
return .none
}
private func completeWithLedgerEffect(_ ledger: LedgerHardwareWalletFactorSource) -> Effect<Action> {
.run { send in
try await factorSourcesClient.saveFactorSource(ledger.embed())
loggerGlobal.notice("Added Ledger factor source! ✅ ")
await send(.delegate(.completed(ledger)))
} catch: { error, _ in
loggerGlobal.error("Failed to add Factor Source, error: \(error)")
errorQueue.schedule(error)
}
}
}
extension AlertState<Never> {
static func ledgerAlreadyExists(_ ledger: LedgerHardwareWalletFactorSource) -> AlertState {
AlertState {
TextState(L10n.AddLedgerDevice.AlreadyAddedAlert.title)
} message: {
TextState(L10n.AddLedgerDevice.AlreadyAddedAlert.message(ledger.hint.name))
}
}
}
// MARK: - NameLedgerFactorSource
public struct NameLedgerFactorSource: Sendable, FeatureReducer {
public struct State: Sendable, Hashable {
public let deviceInfo: LedgerDeviceInfo
public var ledgerName = ""
public init(deviceInfo: LedgerDeviceInfo) {
self.deviceInfo = deviceInfo
}
public var nameIsValid: Bool {
!ledgerName.isEmpty
}
}
public enum ViewAction: Sendable, Equatable {
case ledgerNameChanged(String)
case confirmNameButtonTapped
}
public enum DelegateAction: Sendable, Equatable {
case complete(LedgerHardwareWalletFactorSource)
case failedToCreateLedgerFactorSource
}
@Dependency(\.errorQueue) var errorQueue
public init() {}
public func reduce(into state: inout State, viewAction: ViewAction) -> Effect<Action> {
switch viewAction {
case let .ledgerNameChanged(name):
state.ledgerName = name
return .none
case .confirmNameButtonTapped:
loggerGlobal.notice("Confirmed ledger name: '\(state.ledgerName)', creating factor source")
do {
let ledger = try LedgerHardwareWalletFactorSource.from(
device: state.deviceInfo,
name: state.ledgerName
)
return .send(.delegate(.complete(ledger)))
} catch {
loggerGlobal.error("Failed to created Ledger FactorSource, error: \(error)")
errorQueue.schedule(error)
return .send(.delegate(.failedToCreateLedgerFactorSource))
}
}
}
}
extension LedgerHardwareWalletFactorSource {
static func from(
device: LedgerDeviceInfo,
name: String
) throws -> Self {
try model(
.init(model: device.model),
name: name,
deviceID: device.id
)
}
}