-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Backup.tsx
171 lines (152 loc) · 4.85 KB
/
Backup.tsx
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
// Copyright 2017-2019 @polkadot/app-accounts authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { BareProps, FormProps$Refs, FormProps$Hooks } from '@polkadot/react-components/types';
import FileSaver from 'file-saver';
import React, { useState, useMemo } from 'react';
import { AddressRow, Button, Modal, Password } from '@polkadot/react-components';
import { useForm, usePassword } from '@polkadot/react-hooks';
import keyring from '@polkadot/ui-keyring';
import { useTranslation } from '../translate';
interface Props extends BareProps {
onClose: () => void;
address: string;
}
interface ButtonsProps extends FormProps$Refs {
doBackup: () => void;
isPasswordValid: boolean;
onClose: () => void;
}
interface ContentProps extends FormProps$Hooks {
address: string;
isPasswordTouched: boolean;
isPasswordValid: boolean;
onChangePassword: (password: string) => void;
password: string;
}
export default function ({ address, onClose }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { password, setPassword, ...passwordState } = usePassword();
const { cancelButtonRef, submitButtonRef, onInputEnterKey, onInputEscapeKey } = useForm();
const [isPasswordTouched, setIsPasswordTouched] = useState(false);
const [backupFailed, setBackupFailed] = useState(false);
const isPasswordValid = useMemo(
() => passwordState.isPasswordValid && !backupFailed,
[passwordState.isPasswordValid, backupFailed]
);
function _onChangePassword (value: string): void {
if (!isPasswordTouched) {
setIsPasswordTouched(true);
}
setBackupFailed(false);
setPassword(value);
}
const _doBackup = (): void => {
try {
const addressKeyring = address && keyring.getPair(address);
const json = addressKeyring && keyring.backupAccount(addressKeyring, password);
const blob = new Blob([JSON.stringify(json)], { type: 'application/json; charset=utf-8' });
FileSaver.saveAs(blob, `${address}.json`);
} catch (error) {
setBackupFailed(true);
console.error(error);
return;
}
onClose();
};
return (
<Modal
className='app--accounts-Modal'
dimmer='inverted'
open
>
<Modal.Header>{t('Backup account')}</Modal.Header>
<Content
address={address}
isPasswordTouched={isPasswordTouched}
isPasswordValid={isPasswordValid}
password={password}
onChangePassword={_onChangePassword}
onInputEnterKey={onInputEnterKey}
onInputEscapeKey={onInputEscapeKey}
/>
<Buttons
doBackup={_doBackup}
isPasswordValid={isPasswordValid}
onClose={onClose}
cancelButtonRef={cancelButtonRef}
submitButtonRef={submitButtonRef}
/>
</Modal>
);
}
function Content ({
address,
isPasswordTouched,
isPasswordValid,
password,
onChangePassword,
onInputEnterKey,
onInputEscapeKey
}: ContentProps): React.ReactElement<ContentProps> {
const { t } = useTranslation();
return (
<Modal.Content>
<AddressRow
isInline
value={address}
>
<p>{t('An encrypted backup file will be created once you have pressed the "Download" button. This can be used to re-import your account on any other machine.')}</p>
<p>{t('Save this backup file in a secure location. Additionally, the password associated with this account is needed together with this backup file in order to restore your account.')}</p>
<div>
<Password
autoFocus
help={t('The account password as specified when creating the account. This is used to encrypt the backup file and subsequently decrypt it when restoring the account.')}
isError={isPasswordTouched && !isPasswordValid}
label={t('password')}
onChange={onChangePassword}
onEnter={onInputEnterKey}
onEscape={onInputEscapeKey}
tabIndex={0}
value={password}
/>
</div>
</AddressRow>
</Modal.Content>
);
}
interface ButtonsProps extends FormProps$Refs {
doBackup: () => void;
isPasswordValid: boolean;
onClose: () => void;
}
function Buttons ({
doBackup,
isPasswordValid,
onClose,
submitButtonRef,
cancelButtonRef
}: ButtonsProps): React.ReactElement<ButtonsProps> {
const { t } = useTranslation();
return (
<Modal.Actions>
<Button.Group>
<Button
icon='cancel'
isNegative
label={t('Cancel')}
onClick={onClose}
ref={cancelButtonRef}
/>
<Button.Or />
<Button
icon='download'
isDisabled={!isPasswordValid}
label={t('Download')}
onClick={doBackup}
ref={submitButtonRef}
/>
</Button.Group>
</Modal.Actions>
);
}