diff --git a/src/actions/DiscoveryActions.js b/src/actions/DiscoveryActions.js index 57e97251..22ca82a0 100644 --- a/src/actions/DiscoveryActions.js +++ b/src/actions/DiscoveryActions.js @@ -16,7 +16,6 @@ import type { Account, } from 'flowtype'; import type { Discovery, State } from 'reducers/DiscoveryReducer'; -import * as LocalStorageActions from 'actions/LocalStorageActions'; import * as BlockchainActions from './BlockchainActions'; import * as EthereumDiscoveryActions from './ethereum/DiscoveryActions'; import * as RippleDiscoveryActions from './ripple/DiscoveryActions'; @@ -121,7 +120,6 @@ const start = (device: TrezorDevice, network: string, ignoreCompleted?: boolean) } if (!discoveryProcess) { - dispatch(addImportedAccounts()); dispatch(begin(device, network)); } else if (discoveryProcess.completed && !ignoreCompleted) { dispatch({ @@ -382,17 +380,3 @@ export const addAccount = (): ThunkAction => (dispatch: Dispatch, getState: GetS if (!selected) return; 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, - }); - }); - } -}; diff --git a/src/actions/ImportAccountActions.js b/src/actions/ImportAccountActions.js index 4e9d205f..02a9fae5 100644 --- a/src/actions/ImportAccountActions.js +++ b/src/actions/ImportAccountActions.js @@ -33,16 +33,16 @@ export const importAddress = ( }); let payload; - const index = getState().accounts.filter( - a => a.imported === true && a.network === network.shortcut - ).length; - try { if (network.type === 'ethereum') { const account = await dispatch( BlockchainActions.discoverAccount(device, address, network.shortcut) ); + const index = getState().accounts.filter( + a => a.imported === true && a.network === network.shortcut + ).length; + const empty = account.nonce <= 0 && account.balance === '0'; payload = { imported: true, @@ -93,6 +93,9 @@ export const importAddress = ( const account = response.payload; const empty = account.sequence <= 0 && account.balance === '0'; + const index = getState().accounts.filter( + a => a.imported === true && a.network === network.shortcut + ).length; payload = { imported: true, diff --git a/src/actions/LocalStorageActions.js b/src/actions/LocalStorageActions.js index 2a2a2736..8c0ece33 100644 --- a/src/actions/LocalStorageActions.js +++ b/src/actions/LocalStorageActions.js @@ -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); if (userTokens) { dispatch({ diff --git a/src/reducers/AccountsReducer.js b/src/reducers/AccountsReducer.js index 84008112..f4f5fabe 100644 --- a/src/reducers/AccountsReducer.js +++ b/src/reducers/AccountsReducer.js @@ -90,8 +90,16 @@ const createAccount = (state: State, account: Account): State => { return newState; }; -const removeAccounts = (state: State, device: TrezorDevice): State => - state.filter(account => account.deviceState !== device.state); +const removeAccounts = ( + 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): State => { let newState: State = [...state]; @@ -121,9 +129,11 @@ export default (state: State = initialState, action: Action): State => { case CONNECT.FORGET: case CONNECT.FORGET_SINGLE: case CONNECT.FORGET_SILENT: - case CONNECT.RECEIVE_WALLET_TYPE: 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: return clear(state, action.devices);