PinUnlock.tsx 2.91 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
// 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/>.

import React from 'react';

18
import { KeyboardAwareContainer } from 'modules/unlock/components/Container';
19
20
21
22
23
24
import PinInput from 'modules/unlock/components/PinInput';
import { usePinState } from 'modules/unlock/hooks';
import t from 'modules/unlock/strings';
import { getSubtitle, onPinInputChange } from 'modules/unlock/utils';
import testIDs from 'e2e/testIDs';
import ScreenHeading from 'components/ScreenHeading';
25
import { NavigationTargetIdentityProps } from 'types/props';
26
import { debounce } from 'utils/debounce';
27
28
29
import { withAccountStore, withTargetIdentity } from 'utils/HOC';
import { unlockIdentitySeedWithReturn } from 'utils/identitiesUtils';
import { useSeedRef } from 'utils/seedRefHooks';
30
31

function PinUnlock({
32
	targetIdentity,
33
	route
34
}: NavigationTargetIdentityProps<'PinUnlock'>): React.ReactElement {
35
	const [state, updateState, resetState] = usePinState();
36
	const { createSeedRef } = useSeedRef(targetIdentity.encryptedSeed);
37

38
	async function submit(pin: string): Promise<void> {
39
40
		if (pin.length >= 6 && targetIdentity) {
			try {
41
				if (route.params.shouldReturnSeed) {
Hanwen Cheng's avatar
Hanwen Cheng committed
42
					const resolveSeedPhrase = route.params.resolve;
43
44
45
46
47
48
					const seedPhrase = await unlockIdentitySeedWithReturn(
						pin,
						targetIdentity,
						createSeedRef
					);
					resetState();
Hanwen Cheng's avatar
Hanwen Cheng committed
49
					resolveSeedPhrase(seedPhrase);
50
				} else {
Hanwen Cheng's avatar
Hanwen Cheng committed
51
					const resolve = route.params.resolve;
52
53
54
55
					await createSeedRef(pin);
					resetState();
					resolve();
				}
56
			} catch (e) {
57
				updateState({ pin, pinMismatch: true });
58
59
			}
		} else {
60
			updateState({ pin, pinTooShort: true });
61
62
		}
	}
63
64
65
66
67
68
69

	const onPinInput = (pin: string): void => {
		onPinInputChange('pin', updateState)(pin);
		const debounceSubmit = debounce(() => submit(pin), 500);
		debounceSubmit();
	};

70
	return (
71
72
73
74
75
		<KeyboardAwareContainer
			contentContainerStyle={{
				flexGrow: 1
			}}
		>
76
77
78
79
80
81
82
83
84
85
			<ScreenHeading
				title={t.title.pinUnlock}
				error={state.pinMismatch || state.pinTooShort}
				subtitle={getSubtitle(state, true)}
			/>
			<PinInput
				label={t.pinLabel}
				autoFocus
				testID={testIDs.IdentityPin.unlockPinInput}
				returnKeyType="done"
86
				onChangeText={onPinInput}
87
88
				value={state.pin}
			/>
89
		</KeyboardAwareContainer>
90
91
92
	);
}

93
export default withAccountStore(withTargetIdentity(PinUnlock));