on 4b0ca21 help sign in
bridge/src/views/Login/PrivateKey.js
65 lines 1.8 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 } from 'lib/wallet';
8import { stripHexPrefix } from 'lib/utils/address';
9import { WALLET_TYPES } from 'lib/constants';
10import useLoginView from 'lib/useLoginView';
12import FormError from 'form/FormError';
13import BridgeForm from 'form/BridgeForm';
14import { PrivateKeyInput } from 'form/Inputs';
15import { composeValidator, buildPrivateKeyValidator } from 'form/validators';
17import SubmitButton from 'form/SubmitButton';
19export default function PrivateKey({ className, goHome }) {
20 useLoginView(WALLET_TYPES.PRIVATE_KEY);
22 const { setWallet } = useWallet();
24 const validate = useMemo(
25 () =>
26 composeValidator({
27 privateKey: buildPrivateKeyValidator(64),
28 }),
29 []
30 );
32 const onValues = useCallback(
33 ({ valid, values }) => {
34 if (valid) {
35 const sec = Buffer.from(stripHexPrefix(values.privateKey), 'hex');
36 const newWallet = new EthereumWallet(sec);
37 setWallet(Just(newWallet));
38 } else {
39 setWallet(Nothing());
40 }
41 },
42 [setWallet]
43 );
45 return (
46 <Grid className={className}>
47 <BridgeForm validate={validate} onValues={onValues} afterSubmit={goHome}>
48 {({ handleSubmit }) => (
49 <>
50 <Grid.Item
51 full
52 as={PrivateKeyInput}
53 name="privateKey"
54 label="Private key"
55 />
56 <Grid.Item full as={FormError} />
57 <Grid.Item full as={SubmitButton} handleSubmit={handleSubmit}>
58 Login
59 </Grid.Item>
60 </>
61 )}
62 </BridgeForm>
63 </Grid>
64 );