on d760cbf help sign in
bridge/src/views/Login/Keystore.js
99 lines 2.6 kB history raw
1import React, { useState } from 'react';
2import { Just, Nothing } from 'folktale/maybe';
3import { P, Grid, Input, ErrorText } from 'indigo-react';
4import * as keythereum from 'keythereum';
6import { useWallet } from 'store/wallet';
8import { usePassphraseInput } from 'lib/useInputs';
9import * as need from 'lib/need';
10import { EthereumWallet, WALLET_TYPES } from 'lib/wallet';
11import useLoginView from 'lib/useLoginView';
13import { ForwardButton } from 'components/Buttons';
14import UploadButton from 'components/UploadButton';
16export default function Keystore({ className }) {
17 useLoginView(WALLET_TYPES.KEYSTORE);
19 // globals
20 const { setWallet } = useWallet();
22 const [error, setError] = useState();
23 // inputs
24 // keystore: Maybe<String>
25 const [keystore, setKeystore] = useState(Nothing());
26 const [passphraseInput, { data: passphrase }] = usePassphraseInput({
27 name: 'password',
28 label: 'Keystore password',
29 autoFocus: true,
30 });
32 const constructWallet = () => {
33 try {
34 const text = need.keystore(keystore);
36 const json = JSON.parse(text);
37 const privateKey = keythereum.recover(passphrase, json);
39 const newWallet = new EthereumWallet(privateKey);
40 setError();
41 setWallet(Just(newWallet));
42 } catch (err) {
43 setError(
44 "Couldn't decrypt wallet. You may have entered an incorrect password."
45 );
46 setWallet(Nothing());
47 }
48 };
50 const handleKeystoreUpload = element => {
51 const file = element.files.item(0);
52 const reader = new FileReader();
54 reader.onload = e => {
55 const keystore = e.target.result;
56 setKeystore(Just(keystore));
57 };
59 const failure = _ => {
60 setError('There was a problem uploading your Keystore file');
61 };
63 reader.onerror = failure;
64 reader.onabort = failure;
66 reader.readAsText(file);
67 };
69 return (
70 <Grid className={className}>
71 <Grid.Item full as={P}>
72 Please upload your Ethereum keystore file. If your keystore file is
73 encrypted with a password, you'll also need to enter that below.
74 </Grid.Item>
76 <Grid.Item full as={UploadButton} onChange={handleKeystoreUpload}>
77 Upload Keystore file
78 </Grid.Item>
80 {error && (
81 <Grid.Item full as={ErrorText} className="mt1">
82 {error}
83 </Grid.Item>
84 )}
86 <Grid.Item full as={Input} className="mt3" {...passphraseInput} />
88 <Grid.Item
89 full
90 as={ForwardButton}
91 solid
92 className="mt3"
93 disabled={Nothing.hasInstance(keystore)}
94 onClick={constructWallet}>
95 Decrypt
96 </Grid.Item>
97 </Grid>
98 );