import { TrezorWallet, TREZOR_MINIMUM_FIRMWARE } from 'libs/wallet'; import React, { PureComponent } from 'react'; import translate, { translateRaw } from 'translations'; import TrezorConnect from 'vendor/trezor-connect'; import DeterministicWalletsModal from './DeterministicWalletsModal'; import './Trezor.scss'; import { Spinner, NewTabLink } from 'components/ui'; import { AppState } from 'reducers'; import { connect } from 'react-redux'; import { SecureWalletName, trezorReferralURL } from 'config'; import { getSingleDPath, getPaths } from 'selectors/config/wallet'; //todo: conflicts with comment in walletDecrypt -> onUnlock method interface OwnProps { onUnlock(param: any): void; } interface StateProps { dPath: DPath; dPaths: DPath[]; } // todo: nearly duplicates ledger component props interface State { publicKey: string; chainCode: string; dPath: string; error: string | null; isLoading: boolean; } type Props = OwnProps & StateProps; class TrezorDecryptClass extends PureComponent { public state: State = { publicKey: '', chainCode: '', dPath: this.props.dPath.value, error: null, isLoading: false }; public componentWillReceiveProps(nextProps: Props) { if (this.props.dPath !== nextProps.dPath) { this.setState({ dPath: nextProps.dPath.value }); } } public render() { const { dPath, publicKey, chainCode, error, isLoading } = this.state; const showErr = error ? 'is-showing' : ''; return (
{translate('Don’t have a TREZOR? Order one now!')}
{error || '-'}
Guide:{' '} How to use TREZOR with MyCrypto
); } private handlePathChange = (dPath: string) => { this.setState({ dPath }); this.handleConnect(dPath); }; private handleConnect = (dPath: string = this.state.dPath): void => { this.setState({ isLoading: true, error: null }); (TrezorConnect as any).getXPubKey( dPath, (res: any) => { if (res.success) { this.setState({ dPath, publicKey: res.publicKey, chainCode: res.chainCode, isLoading: false }); } else { this.setState({ error: res.error, isLoading: false }); } }, TREZOR_MINIMUM_FIRMWARE ); }; private handleCancel = () => { this.reset(); }; private handleUnlock = (address: string, index: number) => { this.props.onUnlock(new TrezorWallet(address, this.state.dPath, index)); this.reset(); }; private handleNullConnect = (): void => this.handleConnect(); private reset() { this.setState({ publicKey: '', chainCode: '', dPath: this.props.dPath.value }); } } function mapStateToProps(state: AppState): StateProps { return { dPath: getSingleDPath(state, SecureWalletName.TREZOR), dPaths: getPaths(state, SecureWalletName.TREZOR) }; } export const TrezorDecrypt = connect(mapStateToProps)(TrezorDecryptClass);