1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-30 12:18:09 +00:00

Merge pull request #502 from trezor/fix/import-tool

Fix/import tool
This commit is contained in:
Vladimir Volek 2019-04-15 13:38:15 +02:00 committed by GitHub
commit 4e6783f51d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 39 deletions

View File

@ -16,7 +16,6 @@ import type {
Account, Account,
} from 'flowtype'; } from 'flowtype';
import type { Discovery, State } from 'reducers/DiscoveryReducer'; import type { Discovery, State } from 'reducers/DiscoveryReducer';
import * as LocalStorageActions from 'actions/LocalStorageActions';
import * as BlockchainActions from './BlockchainActions'; import * as BlockchainActions from './BlockchainActions';
import * as EthereumDiscoveryActions from './ethereum/DiscoveryActions'; import * as EthereumDiscoveryActions from './ethereum/DiscoveryActions';
import * as RippleDiscoveryActions from './ripple/DiscoveryActions'; import * as RippleDiscoveryActions from './ripple/DiscoveryActions';
@ -121,7 +120,6 @@ const start = (device: TrezorDevice, network: string, ignoreCompleted?: boolean)
} }
if (!discoveryProcess) { if (!discoveryProcess) {
dispatch(addImportedAccounts());
dispatch(begin(device, network)); dispatch(begin(device, network));
} else if (discoveryProcess.completed && !ignoreCompleted) { } else if (discoveryProcess.completed && !ignoreCompleted) {
dispatch({ dispatch({
@ -382,17 +380,3 @@ export const addAccount = (): ThunkAction => (dispatch: Dispatch, getState: GetS
if (!selected) return; if (!selected) return;
dispatch(start(selected, getState().router.location.state.network, true)); dispatch(start(selected, getState().router.location.state.network, true));
}; };
export const addImportedAccounts = (): ThunkAction => (dispatch: Dispatch): void => {
// get imported accounts from local storage
const importedAccounts = LocalStorageActions.getImportedAccounts();
if (importedAccounts) {
// create each account
importedAccounts.forEach(account => {
dispatch({
type: ACCOUNT.CREATE,
payload: account,
});
});
}
};

View File

@ -3,7 +3,7 @@
import * as ACCOUNT from 'actions/constants/account'; import * as ACCOUNT from 'actions/constants/account';
import * as IMPORT from 'actions/constants/importAccount'; import * as IMPORT from 'actions/constants/importAccount';
import * as NOTIFICATION from 'actions/constants/notification'; import * as NOTIFICATION from 'actions/constants/notification';
import type { AsyncAction, TrezorDevice, Network, Dispatch, GetState } from 'flowtype'; import type { AsyncAction, Account, TrezorDevice, Network, Dispatch, GetState } from 'flowtype';
import * as BlockchainActions from 'actions/ethereum/BlockchainActions'; import * as BlockchainActions from 'actions/ethereum/BlockchainActions';
import * as LocalStorageActions from 'actions/LocalStorageActions'; import * as LocalStorageActions from 'actions/LocalStorageActions';
import TrezorConnect from 'trezor-connect'; import TrezorConnect from 'trezor-connect';
@ -21,6 +21,15 @@ export type ImportAccountAction =
error: ?string, error: ?string,
}; };
const findIndex = (accounts: Array<Account>, network: Network, device: TrezorDevice): number => {
return accounts.filter(
a =>
a.imported === true &&
a.network === network.shortcut &&
a.deviceID === (device.features || {}).device_id
).length;
};
export const importAddress = ( export const importAddress = (
address: string, address: string,
network: Network, network: Network,
@ -33,20 +42,14 @@ export const importAddress = (
}); });
let payload; let payload;
const index = getState().accounts.filter(
a =>
a.imported === true &&
a.network === network.shortcut &&
device &&
a.deviceState === device.state
).length;
try { try {
if (network.type === 'ethereum') { if (network.type === 'ethereum') {
const account = await dispatch( const account = await dispatch(
BlockchainActions.discoverAccount(device, address, network.shortcut) BlockchainActions.discoverAccount(device, address, network.shortcut)
); );
const index = findIndex(getState().accounts, network, device);
const empty = account.nonce <= 0 && account.balance === '0'; const empty = account.nonce <= 0 && account.balance === '0';
payload = { payload = {
imported: true, imported: true,
@ -97,6 +100,7 @@ export const importAddress = (
const account = response.payload; const account = response.payload;
const empty = account.sequence <= 0 && account.balance === '0'; const empty = account.sequence <= 0 && account.balance === '0';
const index = findIndex(getState().accounts, network, device);
payload = { payload = {
imported: true, imported: true,

View File

@ -237,6 +237,16 @@ const loadStorageData = (): ThunkAction => (dispatch: Dispatch): void => {
}); });
} }
const importedAccounts = getImportedAccounts();
if (importedAccounts) {
importedAccounts.forEach(account => {
dispatch({
type: ACCOUNT.CREATE,
payload: account,
});
});
}
const userTokens: ?string = storageUtils.get(TYPE, KEY_TOKENS); const userTokens: ?string = storageUtils.get(TYPE, KEY_TOKENS);
if (userTokens) { if (userTokens) {
dispatch({ dispatch({

View File

@ -42,23 +42,24 @@ export type State = Array<Account>;
const initialState: State = []; const initialState: State = [];
export const findAccount = (
state: State,
index: number,
deviceState: string,
network: string
): ?Account =>
state.find(a => a.deviceState === deviceState && a.index === index && a.network === network);
export const findDeviceAccounts = ( export const findDeviceAccounts = (
state: State, state: State,
device: TrezorDevice, device: TrezorDevice,
network: string network: string
): Array<Account> => { ): Array<Account> => {
if (network) { if (network) {
return state.filter(addr => addr.deviceState === device.state && addr.network === network); return state.filter(
addr =>
(addr.deviceState === device.state ||
(addr.imported && addr.deviceID === (device.features || {}).device_id)) &&
addr.network === network
);
} }
return state.filter(addr => addr.deviceState === device.state); return state.filter(
addr =>
addr.deviceState === device.state ||
(addr.imported && addr.deviceID === (device.features || {}).device_id)
);
}; };
const createAccount = (state: State, account: Account): State => { const createAccount = (state: State, account: Account): State => {
@ -83,8 +84,16 @@ const createAccount = (state: State, account: Account): State => {
return newState; return newState;
}; };
const removeAccounts = (state: State, device: TrezorDevice): State => const removeAccounts = (
state.filter(account => account.deviceState !== device.state); state: State,
device: TrezorDevice,
keepImportedAccounts = false
): State => {
if (keepImportedAccounts) {
return state.filter(account => account.deviceState !== device.state || account.imported);
}
return state.filter(account => account.deviceState !== device.state);
};
const clear = (state: State, devices: Array<TrezorDevice>): State => { const clear = (state: State, devices: Array<TrezorDevice>): State => {
let newState: State = [...state]; let newState: State = [...state];
@ -114,9 +123,11 @@ export default (state: State = initialState, action: Action): State => {
case CONNECT.FORGET: case CONNECT.FORGET:
case CONNECT.FORGET_SINGLE: case CONNECT.FORGET_SINGLE:
case CONNECT.FORGET_SILENT: case CONNECT.FORGET_SILENT:
case CONNECT.RECEIVE_WALLET_TYPE:
return removeAccounts(state, action.device); return removeAccounts(state, action.device);
case CONNECT.RECEIVE_WALLET_TYPE:
return removeAccounts(state, action.device, true); // removes all accounts except imported ones
case WALLET.CLEAR_UNAVAILABLE_DEVICE_DATA: case WALLET.CLEAR_UNAVAILABLE_DEVICE_DATA:
return clear(state, action.devices); return clear(state, action.devices);

View File

@ -92,7 +92,8 @@ export const getSelectedAccount = (state: State): ?Account => {
return state.accounts.find( return state.accounts.find(
a => a =>
a.imported === isImported && a.imported === isImported &&
a.deviceState === device.state && (a.deviceState === device.state ||
(a.imported && a.deviceID === (device.features || {}).device_id)) &&
a.index === index && a.index === index &&
a.network === locationState.network a.network === locationState.network
); );