1import React, { useCallback, useMemo } from 'react';
2import { Just } from 'folktale/maybe';
3import * as bip32 from 'bip32';
4import { times } from 'lodash';
5import TrezorConnect from 'trezor-connect';
6import * as secp256k1 from 'secp256k1';
7import { Text, Grid, H5, CheckboxInput, SelectInput } from 'indigo-react';
9import { useWallet } from 'store/wallet';
11import { TREZOR_PATH } from 'lib/trezor';
12import { WALLET_TYPES } from 'lib/wallet';
13import useLoginView from 'lib/useLoginView';
17 buildCheckboxValidator,
20} from 'form/validators';
21import BridgeForm from 'form/BridgeForm';
22import Condition from 'form/Condition';
23import FormError from 'form/FormError';
25import SubmitButton from 'form/SubmitButton';
26import { FORM_ERROR } from 'final-form';
27import { HdPathInput } from 'form/Inputs';
29const ACCOUNT_OPTIONS = times(20, i => ({
30 text: `Account #${i + 1}`,
34// see Ledger.js for context — Trezor is basicaly Ledger with less complexity
35export default function Trezor({ className, goHome }) {
36 useLoginView(WALLET_TYPES.TREZOR);
38 const { setWallet, setWalletHdPath } = useWallet();
40 const validate = useMemo(
43 useCustomPath: buildCheckboxValidator(),
44 account: buildSelectValidator(ACCOUNT_OPTIONS),
45 hdPath: buildHdPathValidator(),
50 const onSubmit = useCallback(
52 TrezorConnect.manifest({
53 email: 'bridge-trezor@urbit.org',
54 appUrl: 'https://github.com/urbit/bridge',
57 const { success, payload } = await TrezorConnect.getPublicKey({
62 return { [FORM_ERROR]: 'Failed to authenticate with your Trezor.' };
65 const publicKey = Buffer.from(payload.publicKey, 'hex');
66 const chainCode = Buffer.from(payload.chainCode, 'hex');
67 const pub = secp256k1.publicKeyConvert(publicKey, true);
68 const hd = bip32.fromPublicKey(pub, chainCode);
71 setWalletHdPath(values.hdPath);
73 [setWallet, setWalletHdPath]
76 const onValues = useCallback(({ valid, values, form }) => {
81 if (!values.useCustomPath) {
82 form.change('hdPath', TREZOR_PATH.replace(/x/g, values.account));
86 const initialValues = useMemo(
89 hdPath: TREZOR_PATH.replace(/x/g, ACCOUNT_OPTIONS[0].value),
90 account: ACCOUNT_OPTIONS[0].value,
96 <Grid className={className}>
97 <Grid.Item full as={H5}>
98 Authenticate With Your Trezor
101 <Grid.Item full as={Text} className="f6 mb3">
102 Connect and authenticate to your Trezor. If you'd like to use a custom
103 derivation path, you may enter it below.
111 initialValues={initialValues}>
112 {({ handleSubmit }) => (
114 <Condition when="useCustomPath" is={true}>
115 <Grid.Item full as={HdPathInput} name="hdPath" label="HD Path" />
118 <Condition when="useCustomPath" is={false}>
124 placeholder="Choose account..."
125 options={ACCOUNT_OPTIONS}
134 label="Custom HD Path"
137 <Grid.Item full as={FormError} />
139 <Grid.Item full as={SubmitButton} handleSubmit={handleSubmit}>
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list