navigationHelpers.ts 6.48 KB
Newer Older
1
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
Hanwen Cheng's avatar
Hanwen Cheng committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity.  If not, see <http://www.gnu.org/licenses/>.

17
18
19
20
21
import {
	CommonActions,
	useNavigation,
	useNavigationState
} from '@react-navigation/native';
22
import { StackNavigationProp } from '@react-navigation/stack';
Hanwen Cheng's avatar
Hanwen Cheng committed
23

24
import { Identity } from 'types/identityTypes';
25
import { RootStackParamList } from 'types/routes';
Hanwen Cheng's avatar
Hanwen Cheng committed
26

27
28
29
30
31
type Route = {
	name: keyof RootStackParamList;
	params?: RootStackParamList[keyof RootStackParamList];
};

32
export type GenericNavigationProps<
33
34
35
36
37
	RouteName extends keyof RootStackParamList
> = StackNavigationProp<RootStackParamList, RouteName>;

export const setPin = async <RouteName extends keyof RootStackParamList>(
	navigation: GenericNavigationProps<RouteName>
38
): Promise<string> =>
Hanwen Cheng's avatar
Hanwen Cheng committed
39
	new Promise(resolve => {
40
		navigation.navigate('PinNew', { resolve });
Hanwen Cheng's avatar
Hanwen Cheng committed
41
42
	});

Hanwen Cheng's avatar
Hanwen Cheng committed
43
44
45
export const unlockAndReturnSeed = async <
	RouteName extends keyof RootStackParamList
>(
46
47
48
49
50
51
52
53
54
	navigation: GenericNavigationProps<RouteName>
): Promise<string> =>
	new Promise(resolve => {
		navigation.navigate('PinUnlock', {
			resolve,
			shouldReturnSeed: true
		});
	});

55
56
57
58
59
60
61
62
63
type UnlockWithPassword = (
	nextRoute: (password: string) => Route,
	identity?: Identity
) => Promise<void>;

type UnlockWithoutPassword = (
	nextRoute: Route,
	identity?: Identity
) => Promise<void>;
64
65
66
export const useUnlockSeed = (
	isSeedRefValid: boolean
): {
67
68
69
70
71
72
73
	unlockWithPassword: UnlockWithPassword;
	unlockWithoutPassword: UnlockWithoutPassword;
} => {
	const currentRoutes = useNavigationState(state => state.routes) as Route[];
	const navigation = useNavigation<StackNavigationProp<RootStackParamList>>();
	const resetRoutes = (routes: Route[]): void => {
		const resetAction = CommonActions.reset({
74
			index: routes.length,
75
76
77
78
79
80
81
82
83
84
85
86
87
88
			routes: routes
		});
		navigation.dispatch(resetAction);
	};

	const unlockWithPassword: UnlockWithPassword = async (
		nextRoute,
		identity
	) => {
		const password = await unlockSeedPhraseWithPassword(
			navigation,
			isSeedRefValid,
			identity
		);
89
		const newRoutes = currentRoutes.concat(nextRoute(password));
90
91
92
93
94
95
96
97
		resetRoutes(newRoutes);
	};

	const unlockWithoutPassword: UnlockWithoutPassword = async (
		nextRoute,
		identity
	) => {
		await unlockSeedPhrase(navigation, isSeedRefValid, identity);
98
		const newRoutes = currentRoutes.concat(nextRoute);
99
100
101
102
103
104
		resetRoutes(newRoutes);
	};

	return { unlockWithPassword, unlockWithoutPassword };
};

105
106
107
108
export const unlockSeedPhrase = async <
	RouteName extends keyof RootStackParamList
>(
	navigation: GenericNavigationProps<RouteName>,
109
	isSeedRefValid: boolean,
110
	identity?: Identity
Hanwen Cheng's avatar
Hanwen Cheng committed
111
): Promise<void> =>
Hanwen Cheng's avatar
Hanwen Cheng committed
112
	new Promise(resolve => {
113
114
115
116
117
118
119
120
121
		if (isSeedRefValid) {
			resolve();
		} else {
			navigation.navigate('PinUnlock', {
				identity,
				resolve,
				shouldReturnSeed: false
			});
		}
122
123
124
125
126
127
	});

export const unlockSeedPhraseWithPassword = async <
	RouteName extends keyof RootStackParamList
>(
	navigation: GenericNavigationProps<RouteName>,
128
	isSeedRefValid: boolean,
129
130
131
132
133
	identity?: Identity
): Promise<string> =>
	new Promise(resolve => {
		navigation.navigate('PinUnlockWithPassword', {
			identity,
134
			isSeedRefValid,
135
136
			resolve
		});
Hanwen Cheng's avatar
Hanwen Cheng committed
137
138
	});

139
140
141
142
export const navigateToPathDetails = <
	RouteName extends keyof RootStackParamList
>(
	navigation: GenericNavigationProps<RouteName>,
143
144
145
	networkKey: string,
	path: string
): void => {
146
	const resetAction = CommonActions.reset({
147
		index: 2,
148
149
		routes: [
			{
150
				name: 'Main',
151
152
153
154
155
156
157
158
159
160
161
				params: { isNew: false }
			},
			{
				name: 'PathsList',
				params: { networkKey }
			},
			{
				name: 'PathDetails',
				params: { path }
			}
		]
162
163
164
165
	});
	navigation.dispatch(resetAction);
};

166
167
168
169
export const navigateToLandingPage = <
	RouteName extends keyof RootStackParamList
>(
	navigation: GenericNavigationProps<RouteName>
170
): void => {
171
	const resetAction = CommonActions.reset({
Hanwen Cheng's avatar
Hanwen Cheng committed
172
		index: 0,
173
		routes: [{ name: 'Main' }]
Hanwen Cheng's avatar
Hanwen Cheng committed
174
175
176
177
	});
	navigation.dispatch(resetAction);
};

178
179
180
181
export const navigateToNewIdentityNetwork = <
	RouteName extends keyof RootStackParamList
>(
	navigation: GenericNavigationProps<RouteName>
182
): void => {
183
	const resetAction = CommonActions.reset({
Hanwen Cheng's avatar
Hanwen Cheng committed
184
		index: 0,
185
186
		routes: [
			{
187
				name: 'Main',
188
189
190
				params: { isNew: true }
			}
		]
Hanwen Cheng's avatar
Hanwen Cheng committed
191
192
193
194
	});
	navigation.dispatch(resetAction);
};

195
196
export const resetNavigationTo = <RouteName extends keyof RootStackParamList>(
	navigation: GenericNavigationProps<RouteName>,
197
198
199
	screenName: string,
	params?: any
): void => {
200
	const resetAction = CommonActions.reset({
201
		index: 0,
202
		routes: [{ name: screenName, params }]
Hanwen Cheng's avatar
Hanwen Cheng committed
203
204
205
206
	});
	navigation.dispatch(resetAction);
};

207
208
209
210
export const resetNavigationWithNetworkChooser = <
	RouteName extends keyof RootStackParamList
>(
	navigation: GenericNavigationProps<RouteName>,
211
212
	screenName: string,
	params: object = {},
213
	isNew = false
214
): void => {
215
	const resetAction = CommonActions.reset({
Hanwen Cheng's avatar
Hanwen Cheng committed
216
		index: 1,
217
218
		routes: [
			{
219
				name: 'Main',
220
221
222
223
224
225
226
				params: { isNew }
			},
			{
				name: screenName,
				params: params
			}
		]
Hanwen Cheng's avatar
Hanwen Cheng committed
227
228
229
230
	});
	navigation.dispatch(resetAction);
};

231
232
233
234
export const navigateToSignedMessage = <
	RouteName extends keyof RootStackParamList
>(
	navigation: GenericNavigationProps<RouteName>
235
): void =>
236
237
	resetNavigationWithNetworkChooser(navigation, 'SignedMessage', {
		isNew: true
Hanwen Cheng's avatar
Hanwen Cheng committed
238
	});
239

240
241
export const navigateToSignedTx = <RouteName extends keyof RootStackParamList>(
	navigation: GenericNavigationProps<RouteName>
242
): void =>
243
244
	resetNavigationWithNetworkChooser(navigation, 'SignedTx', { isNew: true });

245
246
export const navigateToPathsList = <RouteName extends keyof RootStackParamList>(
	navigation: GenericNavigationProps<RouteName>,
247
248
	networkKey: string
): void =>
249
250
	resetNavigationWithNetworkChooser(navigation, 'PathsList', { networkKey });

251
252
export const navigateToQrScanner = <RouteName extends keyof RootStackParamList>(
	navigation: GenericNavigationProps<RouteName>
253
): void => resetNavigationWithNetworkChooser(navigation, 'QrScanner');
254

255
256
257
258
export const navigateToLegacyAccountList = <
	RouteName extends keyof RootStackParamList
>(
	navigation: GenericNavigationProps<RouteName>
259
): void => resetNavigationTo(navigation, 'LegacyAccountList');