mirror of
https://github.com/trezor/trezor-wallet
synced 2025-01-26 07:51:36 +00:00
fix saving and loading imported accounts from localstorage
This commit is contained in:
parent
e95ebe54b6
commit
c2d03631a4
@ -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,
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
@ -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,
|
||||
|
@ -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({
|
||||
|
@ -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<TrezorDevice>): 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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user