-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathuseAddCoinAccount.ts
342 lines (293 loc) · 11.2 KB
/
useAddCoinAccount.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import { useMemo, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { A, pipe } from '@mobily/ts-belt';
import { isRejected } from '@reduxjs/toolkit';
import { CommonActions, useNavigation } from '@react-navigation/native';
import {
AccountType,
NetworkSymbol,
networks,
NORMAL_ACCOUNT_TYPE,
} from '@suite-common/wallet-config';
import { Account } from '@suite-common/wallet-types';
import {
AccountsRootState,
DeviceRootState,
LIMIT,
selectDevice,
selectDeviceAccounts,
selectIsDeviceInViewOnlyMode,
accountsActions,
} from '@suite-common/wallet-core';
import {
addAndDiscoverNetworkAccountThunk,
selectDeviceEnabledDiscoveryNetworkSymbols,
selectDiscoveryNetworkSymbols,
} from '@suite-native/discovery';
import { TxKeyPath, useTranslate } from '@suite-native/intl';
import {
RootStackParamList,
AddCoinAccountStackRoutes,
AddCoinAccountStackParamList,
StackToStackCompositeNavigationProps,
RootStackRoutes,
AddCoinFlowType,
AppTabsRoutes,
} from '@suite-native/navigation';
import { useAccountAlerts } from '@suite-native/accounts';
import { useAddCoinAccountAlerts } from './useAddCoinAccountAlerts';
export type AddCoinAccountNavigationProps = StackToStackCompositeNavigationProps<
AddCoinAccountStackParamList,
AddCoinAccountStackRoutes.AddCoinAccount,
RootStackParamList
>;
export const accountTypeTranslationKeys: Record<
Exclude<AccountType, 'coinjoin' | 'imported' | 'ledger'>,
{ titleKey: TxKeyPath; subtitleKey: TxKeyPath; descKey: TxKeyPath }
> = {
normal: {
titleKey: 'moduleAddAccounts.selectAccountTypeScreen.accountTypes.normal.title',
subtitleKey: 'moduleAddAccounts.selectAccountTypeScreen.accountTypes.normal.subtitle',
descKey: 'moduleAddAccounts.selectAccountTypeScreen.accountTypes.normal.desc',
},
taproot: {
titleKey: 'moduleAddAccounts.selectAccountTypeScreen.accountTypes.taproot.title',
subtitleKey: 'moduleAddAccounts.selectAccountTypeScreen.accountTypes.taproot.subtitle',
descKey: 'moduleAddAccounts.selectAccountTypeScreen.accountTypes.taproot.desc',
},
segwit: {
titleKey: 'moduleAddAccounts.selectAccountTypeScreen.accountTypes.segwit.title',
subtitleKey: 'moduleAddAccounts.selectAccountTypeScreen.accountTypes.segwit.subtitle',
descKey: 'moduleAddAccounts.selectAccountTypeScreen.accountTypes.segwit.desc',
},
legacy: {
titleKey: 'moduleAddAccounts.selectAccountTypeScreen.accountTypes.legacy.title',
subtitleKey: 'moduleAddAccounts.selectAccountTypeScreen.accountTypes.legacy.subtitle',
descKey: 'moduleAddAccounts.selectAccountTypeScreen.accountTypes.legacy.desc',
},
};
export const useAddCoinAccount = () => {
const dispatch = useDispatch();
const { translate } = useTranslate();
const supportedNetworkSymbols = useSelector(selectDiscoveryNetworkSymbols);
const deviceAccounts = useSelector((state: AccountsRootState & DeviceRootState) =>
selectDeviceAccounts(state),
);
const device = useSelector(selectDevice);
const isDeviceInViewOnlyMode = useSelector(selectIsDeviceInViewOnlyMode);
const enabledDiscoveryNetworkSymbols = useSelector(selectDeviceEnabledDiscoveryNetworkSymbols);
const navigation = useNavigation<AddCoinAccountNavigationProps>();
const { showViewOnlyAddAccountAlert } = useAccountAlerts();
const {
showTooManyAccountsAlert,
showAnotherEmptyAccountAlert,
showGeneralErrorAlert,
showPassphraseAuthAlert,
} = useAddCoinAccountAlerts();
const [networkSymbolWithTypeToBeAdded, setNetworkSymbolWithTypeToBeAdded] = useState<
[NetworkSymbol, AccountType] | null
>(null);
const availableNetworkAccountTypes = useMemo(() => {
// first account type for every network is set to normal and represents default type
const availableTypes: Map<NetworkSymbol, [AccountType, ...AccountType[]]> = new Map();
Object.keys(networks).forEach(symbol => {
// for Cardano and Ethereum only allow latest account type and coinjoin and ledger are not supported
const types = Object.keys(networks[symbol].accountTypes).filter(
t => !['coinjoin', 'imported', 'ledger'].includes(t),
) as AccountType[];
availableTypes.set(symbol as NetworkSymbol, [
NORMAL_ACCOUNT_TYPE,
// For Cardano and EVMs allow only normal account type
...(['ada', 'eth', 'pol', 'bnb', 'sol', 'op'].includes(symbol) ? [] : types),
]);
});
return availableTypes;
}, []);
const getAvailableAccountTypesForNetworkSymbol = ({
networkSymbol,
}: {
networkSymbol: NetworkSymbol;
}) => availableNetworkAccountTypes.get(networkSymbol) ?? [NORMAL_ACCOUNT_TYPE];
const getAccountTypeToBeAddedName = () =>
networkSymbolWithTypeToBeAdded
? translate(accountTypeTranslationKeys[networkSymbolWithTypeToBeAdded[1]].titleKey)
: '';
const setDefaultAccountToBeAdded = (networkSymbol: NetworkSymbol) => {
const defaultType = getAvailableAccountTypesForNetworkSymbol({ networkSymbol })[0];
setNetworkSymbolWithTypeToBeAdded([networkSymbol, defaultType]);
};
const navigateToSuccessorScreen = ({
flowType,
networkSymbol,
accountType,
accountIndex,
}: {
flowType: AddCoinFlowType;
networkSymbol: NetworkSymbol;
accountType: AccountType;
accountIndex: number;
}) => {
switch (flowType) {
case 'accounts':
navigation.replace(RootStackRoutes.AccountDetail, {
networkSymbol,
accountType,
accountIndex,
closeActionType: 'close',
});
break;
case 'home':
case 'receive':
navigation.replace(RootStackRoutes.ReceiveModal, {
networkSymbol,
accountType,
accountIndex,
closeActionType: 'close',
});
break;
}
};
const clearNetworkWithTypeToBeAdded = () => {
setNetworkSymbolWithTypeToBeAdded(null);
};
const checkCanAddAccount = (accounts: Account[]) => {
if (isDeviceInViewOnlyMode) {
showViewOnlyAddAccountAlert();
return false;
}
// Do not allow adding more than 10 accounts of the same type
if (accounts.filter(account => account.visible).length >= LIMIT) {
showTooManyAccountsAlert();
return false;
}
// Do not allow showing another empty account if there is already one
const hasVisibleEmptyAccount = accounts.some(account => account.empty && account.visible);
if (hasVisibleEmptyAccount) {
showAnotherEmptyAccountAlert();
return false;
}
return true;
};
const handleAccountTypeSelection = flowType => {
if (networkSymbolWithTypeToBeAdded) {
clearNetworkWithTypeToBeAdded();
navigation.navigate(AddCoinAccountStackRoutes.SelectAccountType, {
accountType: networkSymbolWithTypeToBeAdded[1] ?? NORMAL_ACCOUNT_TYPE,
networkSymbol: networkSymbolWithTypeToBeAdded[0],
flowType,
});
}
};
const addCoinAccount = async ({
networkSymbol,
flowType,
accountType = NORMAL_ACCOUNT_TYPE,
}: {
networkSymbol: NetworkSymbol;
flowType: AddCoinFlowType;
accountType?: AccountType;
}) => {
clearNetworkWithTypeToBeAdded();
if (!device?.state) {
showGeneralErrorAlert();
return;
}
const accounts = deviceAccounts.filter(
account => account.symbol === networkSymbol && account.accountType === accountType,
);
const firstHiddenEmptyAccount = pipe(
accounts,
A.filter(account => account.empty && !account.visible),
A.sortBy(account => account.index),
A.head,
);
const canAddAccount = checkCanAddAccount(accounts);
if (!canAddAccount) {
return;
}
const network = networks[networkSymbol];
//If the account already exists, but is invisible, make it visible
if (firstHiddenEmptyAccount) {
dispatch(accountsActions.changeAccountVisibility(firstHiddenEmptyAccount));
}
navigateToSuccessorScreen({
flowType,
networkSymbol,
accountType,
accountIndex: firstHiddenEmptyAccount?.index ?? accounts.length,
});
const newAccountResult = await dispatch(
addAndDiscoverNetworkAccountThunk({
network,
accountType,
deviceState: device.state.staticSessionId!,
}),
);
if (
!firstHiddenEmptyAccount && // Do not show error if we are just making the first hidden empty account visible
isRejected(newAccountResult)
) {
let screen = AppTabsRoutes.HomeStack;
if (flowType === 'accounts') {
screen = AppTabsRoutes.AccountsStack;
} else if (flowType === 'receive') {
screen = AppTabsRoutes.ReceiveStack;
}
if (newAccountResult.payload === 'Passphrase is incorrect') {
showPassphraseAuthAlert();
} else {
showGeneralErrorAlert();
}
navigation.dispatch(
CommonActions.reset({
index: 0,
routes: [
{
name: RootStackRoutes.AppTabs,
params: {
screen,
},
},
],
}),
);
}
};
const onSelectedNetworkItem = ({
networkSymbol,
flowType,
}: {
networkSymbol: NetworkSymbol;
flowType: AddCoinFlowType;
}) => {
if (isDeviceInViewOnlyMode) {
showViewOnlyAddAccountAlert();
return;
}
if (!enabledDiscoveryNetworkSymbols.includes(networkSymbol)) {
clearNetworkWithTypeToBeAdded();
navigation.replace(AddCoinAccountStackRoutes.AddCoinDiscoveryRunning, {
networkSymbol,
flowType,
});
return;
}
const types = getAvailableAccountTypesForNetworkSymbol({ networkSymbol });
if (types.length > 1) {
setDefaultAccountToBeAdded(networkSymbol);
} else {
addCoinAccount({ networkSymbol, flowType });
}
};
return {
supportedNetworkSymbols,
onSelectedNetworkItem,
getAvailableAccountTypesForNetworkSymbol,
addCoinAccount,
navigateToSuccessorScreen,
networkSymbolWithTypeToBeAdded,
clearNetworkWithTypeToBeAdded,
getAccountTypeToBeAddedName,
handleAccountTypeSelection,
};
};