mirror of
https://github.com/trezor/trezor-wallet
synced 2025-02-04 20:30:58 +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,
|
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,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
@ -33,16 +33,16 @@ export const importAddress = (
|
|||||||
});
|
});
|
||||||
|
|
||||||
let payload;
|
let payload;
|
||||||
const index = getState().accounts.filter(
|
|
||||||
a => a.imported === true && a.network === network.shortcut
|
|
||||||
).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 = getState().accounts.filter(
|
||||||
|
a => a.imported === true && a.network === network.shortcut
|
||||||
|
).length;
|
||||||
|
|
||||||
const empty = account.nonce <= 0 && account.balance === '0';
|
const empty = account.nonce <= 0 && account.balance === '0';
|
||||||
payload = {
|
payload = {
|
||||||
imported: true,
|
imported: true,
|
||||||
@ -93,6 +93,9 @@ 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 = getState().accounts.filter(
|
||||||
|
a => a.imported === true && a.network === network.shortcut
|
||||||
|
).length;
|
||||||
|
|
||||||
payload = {
|
payload = {
|
||||||
imported: true,
|
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);
|
const userTokens: ?string = storageUtils.get(TYPE, KEY_TOKENS);
|
||||||
if (userTokens) {
|
if (userTokens) {
|
||||||
dispatch({
|
dispatch({
|
||||||
|
@ -90,8 +90,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];
|
||||||
@ -121,9 +129,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);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user