mirror of https://github.com/trezor/trezor-wallet
Merge pull request #495 from trezor/feature/support-import-tool
Feature/support import toolpull/501/head
commit
32072f51a9
@ -0,0 +1,153 @@
|
||||
/* @flow */
|
||||
|
||||
import * as ACCOUNT from 'actions/constants/account';
|
||||
import * as IMPORT from 'actions/constants/importAccount';
|
||||
import * as NOTIFICATION from 'actions/constants/notification';
|
||||
import type { AsyncAction, TrezorDevice, Network, Dispatch, GetState } from 'flowtype';
|
||||
import * as BlockchainActions from 'actions/ethereum/BlockchainActions';
|
||||
import * as LocalStorageActions from 'actions/LocalStorageActions';
|
||||
import TrezorConnect from 'trezor-connect';
|
||||
import { toDecimalAmount } from 'utils/formatUtils';
|
||||
|
||||
export type ImportAccountAction =
|
||||
| {
|
||||
type: typeof IMPORT.START,
|
||||
}
|
||||
| {
|
||||
type: typeof IMPORT.SUCCESS,
|
||||
}
|
||||
| {
|
||||
type: typeof IMPORT.FAIL,
|
||||
error: ?string,
|
||||
};
|
||||
|
||||
export const importAddress = (
|
||||
address: string,
|
||||
network: Network,
|
||||
device: ?TrezorDevice
|
||||
): AsyncAction => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
|
||||
if (!device) return;
|
||||
|
||||
dispatch({
|
||||
type: IMPORT.START,
|
||||
});
|
||||
|
||||
let payload;
|
||||
const index = getState().accounts.filter(
|
||||
a =>
|
||||
a.imported === true &&
|
||||
a.network === network.shortcut &&
|
||||
device &&
|
||||
a.deviceState === device.state
|
||||
).length;
|
||||
|
||||
try {
|
||||
if (network.type === 'ethereum') {
|
||||
const account = await dispatch(
|
||||
BlockchainActions.discoverAccount(device, address, network.shortcut)
|
||||
);
|
||||
|
||||
const empty = account.nonce <= 0 && account.balance === '0';
|
||||
payload = {
|
||||
imported: true,
|
||||
index,
|
||||
network: network.shortcut,
|
||||
deviceID: device.features ? device.features.device_id : '0',
|
||||
deviceState: device.state || '0',
|
||||
accountPath: account.path || [],
|
||||
descriptor: account.descriptor,
|
||||
|
||||
balance: account.balance,
|
||||
availableBalance: account.balance,
|
||||
block: account.block,
|
||||
transactions: account.transactions,
|
||||
empty,
|
||||
|
||||
networkType: 'ethereum',
|
||||
nonce: account.nonce,
|
||||
};
|
||||
dispatch({
|
||||
type: ACCOUNT.CREATE,
|
||||
payload,
|
||||
});
|
||||
dispatch({
|
||||
type: IMPORT.SUCCESS,
|
||||
});
|
||||
dispatch(LocalStorageActions.setImportedAccount(payload));
|
||||
dispatch({
|
||||
type: NOTIFICATION.ADD,
|
||||
payload: {
|
||||
type: 'success',
|
||||
title: 'The account has been successfully imported',
|
||||
cancelable: true,
|
||||
},
|
||||
});
|
||||
} else if (network.type === 'ripple') {
|
||||
const response = await TrezorConnect.rippleGetAccountInfo({
|
||||
account: {
|
||||
descriptor: address,
|
||||
},
|
||||
coin: network.shortcut,
|
||||
});
|
||||
|
||||
// handle TREZOR response error
|
||||
if (!response.success) {
|
||||
throw new Error(response.payload.error);
|
||||
}
|
||||
|
||||
const account = response.payload;
|
||||
const empty = account.sequence <= 0 && account.balance === '0';
|
||||
|
||||
payload = {
|
||||
imported: true,
|
||||
index,
|
||||
network: network.shortcut,
|
||||
deviceID: device.features ? device.features.device_id : '0',
|
||||
deviceState: device.state || '0',
|
||||
accountPath: account.path || [],
|
||||
descriptor: account.descriptor,
|
||||
|
||||
balance: toDecimalAmount(account.balance, network.decimals),
|
||||
availableBalance: toDecimalAmount(account.availableBalance, network.decimals),
|
||||
block: account.block,
|
||||
transactions: account.transactions,
|
||||
empty,
|
||||
|
||||
networkType: 'ripple',
|
||||
sequence: account.sequence,
|
||||
reserve: toDecimalAmount(account.reserve, network.decimals),
|
||||
};
|
||||
dispatch({
|
||||
type: ACCOUNT.CREATE,
|
||||
payload,
|
||||
});
|
||||
dispatch({
|
||||
type: IMPORT.SUCCESS,
|
||||
});
|
||||
dispatch(LocalStorageActions.setImportedAccount(payload));
|
||||
dispatch({
|
||||
type: NOTIFICATION.ADD,
|
||||
payload: {
|
||||
type: 'success',
|
||||
title: 'The account has been successfully imported',
|
||||
cancelable: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
dispatch({
|
||||
type: IMPORT.FAIL,
|
||||
error: error.message,
|
||||
});
|
||||
|
||||
dispatch({
|
||||
type: NOTIFICATION.ADD,
|
||||
payload: {
|
||||
type: 'error',
|
||||
title: 'Import account error',
|
||||
message: error.message,
|
||||
cancelable: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
};
|
@ -0,0 +1,5 @@
|
||||
/* @flow */
|
||||
|
||||
export const START: 'import__account__start' = 'import__account__start';
|
||||
export const SUCCESS: 'import__account__success' = 'import__account__success';
|
||||
export const FAIL: 'import__account__fail' = 'import__account__fail';
|
@ -0,0 +1,43 @@
|
||||
/* @flow */
|
||||
|
||||
import * as IMPORT from 'actions/constants/importAccount';
|
||||
|
||||
import type { Action } from 'flowtype';
|
||||
|
||||
export type ImportState = {
|
||||
loading: boolean,
|
||||
error: ?string,
|
||||
};
|
||||
|
||||
export const initialState: ImportState = {
|
||||
loading: false,
|
||||
error: null,
|
||||
};
|
||||
|
||||
export default (state: ImportState = initialState, action: Action): ImportState => {
|
||||
switch (action.type) {
|
||||
case IMPORT.START:
|
||||
return {
|
||||
...state,
|
||||
loading: true,
|
||||
error: null,
|
||||
};
|
||||
|
||||
case IMPORT.SUCCESS:
|
||||
return {
|
||||
...state,
|
||||
loading: false,
|
||||
error: null,
|
||||
};
|
||||
|
||||
case IMPORT.FAIL:
|
||||
return {
|
||||
...state,
|
||||
loading: false,
|
||||
error: action.error,
|
||||
};
|
||||
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
@ -1,28 +0,0 @@
|
||||
/* @flow */
|
||||
|
||||
import React from 'react';
|
||||
import styled from 'styled-components';
|
||||
import { Button, Link, Icon, H5, icons, colors } from 'trezor-ui-components';
|
||||
import LandingWrapper from 'views/Landing/components/LandingWrapper';
|
||||
|
||||
const Wrapper = styled.div`
|
||||
width: 100%;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
`;
|
||||
|
||||
const Import = () => (
|
||||
<LandingWrapper>
|
||||
<Wrapper>
|
||||
<Icon size={60} color={colors.WARNING_PRIMARY} icon={icons.WARNING} />
|
||||
<H5>Import tool is under construction</H5>
|
||||
<Link to="/">
|
||||
<Button>Take me back</Button>
|
||||
</Link>
|
||||
</Wrapper>
|
||||
</LandingWrapper>
|
||||
);
|
||||
|
||||
export default Import;
|
@ -0,0 +1,105 @@
|
||||
/* @flow */
|
||||
|
||||
import React, { useState } from 'react';
|
||||
import styled from 'styled-components';
|
||||
import { FormattedMessage } from 'react-intl';
|
||||
import { Select, Button, Input, Link, colors } from 'trezor-ui-components';
|
||||
import l10nCommonMessages from 'views/common.messages';
|
||||
import type { Props } from './Container';
|
||||
|
||||
const Wrapper = styled.div`
|
||||
text-align: left;
|
||||
flex-direction: column;
|
||||
display: flex;
|
||||
padding: 24px;
|
||||
min-width: 300px;
|
||||
`;
|
||||
|
||||
const StyledSelect = styled(Select)`
|
||||
min-width: 100px;
|
||||
`;
|
||||
|
||||
const InputRow = styled.div`
|
||||
margin-bottom: 16px;
|
||||
`;
|
||||
|
||||
const Label = styled.div`
|
||||
color: ${colors.TEXT_SECONDARY};
|
||||
padding-bottom: 10px;
|
||||
`;
|
||||
|
||||
const ButtonActions = styled.div`
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
|
||||
justify-content: flex-end;
|
||||
`;
|
||||
|
||||
const ButtonWrapper = styled.div`
|
||||
& + & {
|
||||
margin-left: 10px;
|
||||
}
|
||||
`;
|
||||
|
||||
const Import = (props: Props) => {
|
||||
const [selectedNetwork, setSelectedNetwork] = useState(null);
|
||||
const [address, setAddress] = useState('');
|
||||
|
||||
const { networks } = props.config;
|
||||
return (
|
||||
// <LandingWrapper>
|
||||
<Wrapper>
|
||||
<InputRow>
|
||||
<Label>Select network</Label>
|
||||
<StyledSelect
|
||||
value={selectedNetwork}
|
||||
options={networks
|
||||
.sort((a, b) => a.shortcut.localeCompare(b.shortcut))
|
||||
.map(net => ({
|
||||
label: net.shortcut,
|
||||
value: net,
|
||||
}))}
|
||||
onChange={option => setSelectedNetwork(option)}
|
||||
/>
|
||||
</InputRow>
|
||||
|
||||
<InputRow>
|
||||
<Input
|
||||
topLabel="Address"
|
||||
name="cryptoAddress"
|
||||
value={address}
|
||||
onChange={e => setAddress(e.target.value)}
|
||||
type="text"
|
||||
/>
|
||||
</InputRow>
|
||||
<ButtonActions>
|
||||
<ButtonWrapper>
|
||||
<Link to="/">
|
||||
<Button isWhite>
|
||||
<FormattedMessage {...l10nCommonMessages.TR_CLOSE} />
|
||||
</Button>
|
||||
</Link>
|
||||
</ButtonWrapper>
|
||||
|
||||
<ButtonWrapper>
|
||||
<Button
|
||||
isDisabled={
|
||||
!selectedNetwork || address === '' || props.importAccount.loading
|
||||
}
|
||||
onClick={() =>
|
||||
props.importAddress(
|
||||
address,
|
||||
(selectedNetwork || {}).value,
|
||||
props.device
|
||||
)
|
||||
}
|
||||
>
|
||||
Import
|
||||
</Button>
|
||||
</ButtonWrapper>
|
||||
</ButtonActions>
|
||||
</Wrapper>
|
||||
// </LandingWrapper>
|
||||
);
|
||||
};
|
||||
export default Import;
|
Loading…
Reference in new issue