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