PathDerivation.tsx 5.24 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
import React, { useRef, useState, useMemo } from 'react';
18

19
import PasswordInput from 'components/PasswordInput';
20
21
import testIDs from 'e2e/testIDs';
import { defaultNetworkKey, UnknownNetworkKeys } from 'constants/networkSpecs';
22
import { Identity } from 'types/identityTypes';
23
import { NavigationAccountIdentityProps } from 'types/props';
24
25
import { withAccountStore } from 'utils/HOC';
import TextInput from 'components/TextInput';
26
27
28
29
30
31
import {
	extractPathId,
	getNetworkKey,
	getNetworkKeyByPathId,
	validateDerivedPath
} from 'utils/identitiesUtils';
32
33
import { unlockSeedPhrase } from 'utils/navigationHelpers';
import { alertDeriveSuccess, alertPathDerivationError } from 'utils/alertUtils';
34
35
36
37
import Separator from 'components/Separator';
import ScreenHeading from 'components/ScreenHeading';
import PathCard from 'components/PathCard';
import { NetworkSelector, NetworkOptions } from 'components/NetworkSelector';
38
import { useSeedRef } from 'utils/seedRefHooks';
39
40
import Button from 'components/Button';
import { KeyboardAwareContainer } from 'modules/unlock/components/Container';
Hanwen Cheng's avatar
Hanwen Cheng committed
41

42
43
44
45
46
47
48
49
50
51
52
function getParentNetworkKey(
	parentPath: string,
	currentIdentity: Identity
): string {
	if (currentIdentity.meta.has(parentPath)) {
		return getNetworkKey(parentPath, currentIdentity);
	}
	const pathId = extractPathId(parentPath);
	return getNetworkKeyByPathId(pathId);
}

53
54
function PathDerivation({
	accounts,
55
56
	navigation,
	route
57
}: NavigationAccountIdentityProps<'PathDerivation'>): React.ReactElement {
58
59
60
61
	const [derivationPath, setDerivationPath] = useState<string>('');
	const [keyPairsName, setKeyPairsName] = useState<string>('');
	const [modalVisible, setModalVisible] = useState<boolean>(false);
	const [password, setPassword] = useState<string>('');
62
	const pathNameInput = useRef<TextInput>(null);
63
	const currentIdentity = accounts.state.currentIdentity;
64
65
66
	const { isSeedRefValid, substrateAddress } = useSeedRef(
		currentIdentity.encryptedSeed
	);
67
	const parentPath = route.params.parentPath;
68
	const parentNetworkKey = useMemo(
69
		() => getParentNetworkKey(parentPath, currentIdentity),
70
		[parentPath, currentIdentity]
71
72
73
74
	);

	const [customNetworkKey, setCustomNetworkKey] = useState(
		parentNetworkKey === UnknownNetworkKeys.UNKNOWN
75
			? defaultNetworkKey
76
			: parentNetworkKey
77
	);
78
79
80
81
82
	const completePath = `${parentPath}${derivationPath}`;
	const enableCustomNetwork = parentPath === '';
	const currentNetworkKey = enableCustomNetwork
		? customNetworkKey
		: parentNetworkKey;
83
	const isPathValid = validateDerivedPath(derivationPath);
Hanwen Cheng's avatar
Hanwen Cheng committed
84

85
	const onPathDerivation = async (): Promise<void> => {
86
		await unlockSeedPhrase(navigation, isSeedRefValid);
87
88
89
		try {
			await accounts.deriveNewPath(
				completePath,
90
				substrateAddress,
91
92
93
94
				currentNetworkKey,
				keyPairsName,
				password
			);
95
96
			alertDeriveSuccess();
			navigation.goBack();
97
98
		} catch (error) {
			alertPathDerivationError(error.message);
Hanwen Cheng's avatar
Hanwen Cheng committed
99
100
101
102
		}
	};

	return (
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
		<KeyboardAwareContainer>
			<ScreenHeading
				title="Derive Account"
				subtitle={parentPath}
				hasSubtitleIcon={true}
			/>
			<TextInput
				autoCompleteType="off"
				autoCorrect={false}
				autoFocus
				error={!isPathValid}
				label="Path"
				onChangeText={setDerivationPath}
				onSubmitEditing={(): void => pathNameInput.current?.input?.focus()}
				placeholder="//hard/soft"
				returnKeyType="next"
				testID={testIDs.PathDerivation.pathInput}
				value={derivationPath}
			/>
			<TextInput
				autoCompleteType="off"
				autoCorrect={false}
				label="Display Name"
				onChangeText={(keyParisName: string): void =>
					setKeyPairsName(keyParisName)
				}
				onSubmitEditing={onPathDerivation}
				ref={pathNameInput}
				returnKeyType="done"
				testID={testIDs.PathDerivation.nameInput}
				value={keyPairsName}
			/>
			{enableCustomNetwork && (
				<NetworkSelector
					networkKey={customNetworkKey}
					setVisible={setModalVisible}
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
			)}
			<Separator style={{ height: 0 }} />
			<PasswordInput
				password={password}
				setPassword={setPassword}
				onSubmitEditing={onPathDerivation}
			/>
			<PathCard
				identity={accounts.state.currentIdentity}
				isPathValid={isPathValid}
				name={keyPairsName}
				path={password === '' ? completePath : `${completePath}///${password}`}
				networkKey={currentNetworkKey}
			/>
			<Button
				disabled={!isPathValid}
				title="Next"
				testID={testIDs.PathDerivation.deriveButton}
				onPress={onPathDerivation}
			/>
			{enableCustomNetwork && (
				<NetworkOptions
					setNetworkKey={setCustomNetworkKey}
					visible={modalVisible}
					setVisible={setModalVisible}
Hanwen Cheng's avatar
Hanwen Cheng committed
165
				/>
166
167
			)}
		</KeyboardAwareContainer>
Hanwen Cheng's avatar
Hanwen Cheng committed
168
169
170
	);
}

171
export default withAccountStore(PathDerivation);