on c19b2a8 help sign in
bridge/src/views/Login/PrivateKey.js
63 lines 1.7 kB history raw
1import React, { useCallback, useMemo } from 'react';
2import { Just, Nothing } from 'folktale/maybe';
3import { Grid } from 'indigo-react';
5import { useWallet } from 'store/wallet';
7import { EthereumWallet, WALLET_TYPES, stripHexPrefix } from 'lib/wallet';
8import useLoginView from 'lib/useLoginView';
10import FormError from 'form/FormError';
11import BridgeForm from 'form/BridgeForm';
12import { PrivateKeyInput } from 'form/Inputs';
13import { composeValidator, buildPrivateKeyValidator } from 'form/validators';
15import SubmitButton from 'form/SubmitButton';
17export default function PrivateKey({ className, goHome }) {
18 useLoginView(WALLET_TYPES.PRIVATE_KEY);
20 const { setWallet } = useWallet();
22 const validate = useMemo(
23 () =>
24 composeValidator({
25 privateKey: buildPrivateKeyValidator(64),
26 }),
27 []
28 );
30 const onValues = useCallback(
31 ({ valid, values }) => {
32 if (valid) {
33 const sec = Buffer.from(stripHexPrefix(values.privateKey), 'hex');
34 const newWallet = new EthereumWallet(sec);
35 setWallet(Just(newWallet));
36 } else {
37 setWallet(Nothing());
38 }
39 },
40 [setWallet]
41 );
43 return (
44 <Grid className={className}>
45 <BridgeForm validate={validate} onValues={onValues} afterSubmit={goHome}>
46 {({ handleSubmit }) => (
47 <>
48 <Grid.Item
49 full
50 as={PrivateKeyInput}
51 name="privateKey"
52 label="Private key"
53 />
54 <Grid.Item full as={FormError} />
55 <Grid.Item full as={SubmitButton} handleSubmit={handleSubmit}>
56 Continue
57 </Grid.Item>
58 </>
59 )}
60 </BridgeForm>
61 </Grid>
62 );