-
Notifications
You must be signed in to change notification settings - Fork 47
/
state.ts
260 lines (233 loc) · 7.59 KB
/
state.ts
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import {freeze, produce} from 'immer'
export const setupWalletReducer = (
state: SetupWalletState,
action: SetupWalletAction,
) => {
return produce(state, (draft) => {
switch (action.type) {
case SetupWalletActionType.MnemonicChanged:
draft.mnemonic = action.mnemonic
return
case SetupWalletActionType.WalletNameChanged:
draft.walletName = action.walletName
return
case SetupWalletActionType.WalletPasswordChanged:
draft.walletPassword = action.walletPassword
return
case SetupWalletActionType.NetworkIdChanged:
draft.networkId = action.networkId
return
case SetupWalletActionType.WalletImplementationIdChanged:
draft.walletImplementationId = action.walletImplementationId
return
case SetupWalletActionType.PublicKeyHexChanged:
draft.publicKeyHex = action.publicKeyHex
return
case SetupWalletActionType.PathChanged:
draft.path = action.path
return
case SetupWalletActionType.HwDeviceInfoChanged:
draft.hwDeviceInfo = action.hwDeviceInfo
return
case SetupWalletActionType.SetUpTypeChanged:
draft.setUpType = action.setUpType
return
case SetupWalletActionType.MnemonicTypeChanged:
draft.mnemonicType = action.mnemonicType
return
case SetupWalletActionType.UseUSBChanged:
draft.useUSB = action.useUSB
return
case SetupWalletActionType.ShowCreateWalletInfoModalChanged:
draft.showCreateWalletInfoModal = action.showCreateWalletInfoModal
return
case SetupWalletActionType.ShowRestoreWalletInfoModalChanged:
draft.showRestoreWalletInfoModal = action.showRestoreWalletInfoModal
return
case SetupWalletActionType.Reset:
return setupWalletDefaultState
default:
throw new Error(`setupWalletReducer invalid action`)
}
})
}
export const setupWalletDefaultState: Readonly<SetupWalletState> = freeze(
{
mnemonic: '',
walletName: '',
walletPassword: '',
networkId: -1,
walletImplementationId: '',
publicKeyHex: '',
path: [],
hwDeviceInfo: null,
setUpType: null,
mnemonicType: null,
useUSB: false,
showRestoreWalletInfoModal: true,
showCreateWalletInfoModal: true,
},
true,
)
export type SetupWalletState = {
mnemonic: string
walletName: string
walletPassword: string
networkId: NetworkId
walletImplementationId: string
publicKeyHex: string
path: Array<number>
hwDeviceInfo: HWDeviceInfo | null
setUpType: 'restore' | 'create' | 'hw' | null
mnemonicType: 15 | 24 | null
useUSB: boolean
showRestoreWalletInfoModal: boolean
showCreateWalletInfoModal: boolean
}
export enum SetupWalletActionType {
MnemonicChanged = 'mnemonicChanged',
WalletNameChanged = 'walletNameChanged',
WalletPasswordChanged = 'walletPasswordChanged',
NetworkIdChanged = 'networkIdChanged',
WalletImplementationIdChanged = 'walletImplementationIdChanged',
PublicKeyHexChanged = 'publicKeyHexChanged',
PathChanged = 'pathChanged',
HwDeviceInfoChanged = 'hwDeviceInfoChanged',
SetUpTypeChanged = 'setUpTypeChanged',
MnemonicTypeChanged = 'mnemonicTypeChanged',
UseUSBChanged = 'useUSBChanged',
Reset = 'reset',
ShowRestoreWalletInfoModalChanged = 'showRestoreWalletInfoModalChanged',
ShowCreateWalletInfoModalChanged = 'showCreateWalletInfoModalChanged',
}
export type SetupWalletAction =
| {
type: SetupWalletActionType.MnemonicChanged
mnemonic: SetupWalletState['mnemonic']
}
| {
type: SetupWalletActionType.WalletNameChanged
walletName: SetupWalletState['walletName']
}
| {
type: SetupWalletActionType.WalletPasswordChanged
walletPassword: SetupWalletState['walletPassword']
}
| {
type: SetupWalletActionType.NetworkIdChanged
networkId: SetupWalletState['networkId']
}
| {
type: SetupWalletActionType.WalletImplementationIdChanged
walletImplementationId: SetupWalletState['walletImplementationId']
}
| {
type: SetupWalletActionType.PublicKeyHexChanged
publicKeyHex: SetupWalletState['publicKeyHex']
}
| {
type: SetupWalletActionType.PathChanged
path: SetupWalletState['path']
}
| {
type: SetupWalletActionType.HwDeviceInfoChanged
hwDeviceInfo: SetupWalletState['hwDeviceInfo']
}
| {
type: SetupWalletActionType.SetUpTypeChanged
setUpType: SetupWalletState['setUpType']
}
| {
type: SetupWalletActionType.MnemonicTypeChanged
mnemonicType: SetupWalletState['mnemonicType']
}
| {
type: SetupWalletActionType.UseUSBChanged
useUSB: SetupWalletState['useUSB']
}
| {
type: SetupWalletActionType.Reset
}
| {
type: SetupWalletActionType.ShowRestoreWalletInfoModalChanged
showRestoreWalletInfoModal: SetupWalletState['showRestoreWalletInfoModal']
}
| {
type: SetupWalletActionType.ShowCreateWalletInfoModalChanged
showCreateWalletInfoModal: SetupWalletState['showCreateWalletInfoModal']
}
export type SetupWalletActions = {
mnemonicChanged: (mnemonic: SetupWalletState['mnemonic']) => void
walletNameChanged: (walletName: SetupWalletState['walletName']) => void
walletPasswordChanged: (
walletPassword: SetupWalletState['walletPassword'],
) => void
networkIdChanged: (networkId: SetupWalletState['networkId']) => void
walletImplementationIdChanged: (
walletImplementationId: SetupWalletState['walletImplementationId'],
) => void
publicKeyHexChanged: (publicKeyHex: SetupWalletState['publicKeyHex']) => void
pathChanged: (path: SetupWalletState['path']) => void
hwDeviceInfoChanged: (hwDeviceInfo: SetupWalletState['hwDeviceInfo']) => void
setUpTypeChanged: (setUpType: SetupWalletState['setUpType']) => void
mnemonicTypeChanged: (mnemonicType: SetupWalletState['mnemonicType']) => void
useUSBChanged: (useUSB: SetupWalletState['useUSB']) => void
reset: () => void
showRestoreWalletInfoModalChanged: (
showRestoreWalletInfoModal: boolean,
) => void
showCreateWalletInfoModalChanged: (showCreateWalletInfoModal: boolean) => void
}
export type SetupWalletContext = SetupWalletState & SetupWalletActions
export const setupWalletInitialContext: SetupWalletContext = freeze(
{
...setupWalletDefaultState,
mnemonicChanged: missingInit,
walletNameChanged: missingInit,
walletPasswordChanged: missingInit,
networkIdChanged: missingInit,
walletImplementationIdChanged: missingInit,
publicKeyHexChanged: missingInit,
pathChanged: missingInit,
hwDeviceInfoChanged: missingInit,
setUpTypeChanged: missingInit,
mnemonicTypeChanged: missingInit,
useUSBChanged: missingInit,
reset: missingInit,
showRestoreWalletInfoModalChanged: missingInit,
showCreateWalletInfoModalChanged: missingInit,
},
true,
)
/* istanbul ignore next */
function missingInit() {
console.error('[ExchangeContext] missing initialization')
}
export type HWDeviceInfo = {
bip44AccountPublic: string
hwFeatures: HWFeatures
}
type HWFeatures = {
vendor: string
model: string
deviceId: DeviceId | null | undefined
// for establishing a connection through BLE
deviceObj: DeviceObj | null | undefined
// for establishing a connection through USB
serialHex?: string
}
export type DeviceId = string
export type DeviceObj = {
vendorId: number
productId: number
}
export const NETWORK_REGISTRY = {
BYRON_MAINNET: 0,
HASKELL_SHELLEY: 1,
JORMUNGANDR: 100,
// ERGO: 200,
HASKELL_SHELLEY_TESTNET: 300,
UNDEFINED: -1,
SANCHONET: 450,
} as const
export type NetworkId = (typeof NETWORK_REGISTRY)[keyof typeof NETWORK_REGISTRY]