on 4b0ca21 help sign in
bridge/src/views/Login/Keystore.js
93 lines 2.5 kB history raw
1import React, { useCallback, useMemo } from 'react';
2import { Just } from 'folktale/maybe';
3import { Text, Grid } from 'indigo-react';
4import * as keythereum from 'keythereum';
6import { useWallet } from 'store/wallet';
8import { EthereumWallet } from 'lib/wallet';
9import { WALLET_TYPES } from 'lib/constants';
10import useLoginView from 'lib/useLoginView';
12import { PassphraseInput } from 'form/Inputs';
13import {
14 composeValidator,
15 buildPassphraseValidator,
16 buildUploadValidator,
17} from 'form/validators';
18import UploadInput from 'form/UploadInput';
19import SubmitButton from 'form/SubmitButton';
20import BridgeForm from 'form/BridgeForm';
21import { FORM_ERROR } from 'final-form';
22import FormError from 'form/FormError';
24export default function Keystore({ className, goHome }) {
25 useLoginView(WALLET_TYPES.KEYSTORE);
27 // globals
28 const { setWallet } = useWallet();
30 const validate = useMemo(
31 () =>
32 composeValidator({
33 passphrase: buildPassphraseValidator(),
34 keystore: buildUploadValidator(),
35 }),
36 []
37 );
39 const onSubmit = useCallback(
40 async values => {
41 try {
42 const json = JSON.parse(values.keystore);
43 const privateKey = keythereum.recover(values.passphrase, json);
45 const wallet = new EthereumWallet(privateKey);
46 setWallet(Just(wallet));
47 } catch (error) {
48 console.error(error);
49 return {
50 [FORM_ERROR]:
51 "Couldn't decrypt wallet. You may have entered an incorrect password.",
52 };
53 }
54 },
55 [setWallet]
56 );
58 return (
59 <Grid className={className}>
60 <Grid.Item full as={Text} className="f6 gray4 mb3">
61 Please upload your Ethereum keystore file. If your keystore file is
62 encrypted with a password, you'll also need to enter that below.
63 </Grid.Item>
65 <BridgeForm validate={validate} onSubmit={onSubmit} afterSubmit={goHome}>
66 {({ handleSubmit }) => (
67 <>
68 <Grid.Item
69 full
70 as={UploadInput}
71 name="keystore"
72 label="Upload Keystore file"
73 />
75 <Grid.Item
76 full
77 as={PassphraseInput}
78 className="mt3"
79 name="passphrase"
80 label="Passphrase"
81 />
83 <Grid.Item full as={FormError} />
85 <Grid.Item full as={SubmitButton} handleSubmit={handleSubmit}>
86 Decrypt
87 </Grid.Item>
88 </>
89 )}
90 </BridgeForm>
91 </Grid>
92 );