1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-24 17:28:10 +00:00
trezor-wallet/src/services/LocalStorageService.js

121 lines
4.4 KiB
JavaScript
Raw Normal View History

2018-02-20 09:30:36 +00:00
/* @flow */
2018-07-30 10:52:13 +00:00
import { DEVICE } from 'trezor-connect';
2018-08-14 13:11:52 +00:00
import * as LocalStorageActions from 'actions/LocalStorageActions';
2018-09-06 15:04:28 +00:00
// import * as WalletActions from 'actions/WalletActions';
2018-02-20 09:30:36 +00:00
2018-08-14 13:11:52 +00:00
import * as CONNECT from 'actions/constants/TrezorConnect';
import * as TOKEN from 'actions/constants/token';
import * as ACCOUNT from 'actions/constants/account';
import * as DISCOVERY from 'actions/constants/discovery';
import * as SEND from 'actions/constants/send';
import * as PENDING from 'actions/constants/pendingTx';
2018-08-14 13:18:16 +00:00
import { findAccountTokens } from 'reducers/TokensReducer';
2018-04-16 21:19:50 +00:00
2018-07-30 10:52:13 +00:00
import type {
2018-04-16 21:19:50 +00:00
Middleware,
MiddlewareAPI,
MiddlewareDispatch,
Dispatch,
Action,
2018-07-30 10:52:13 +00:00
GetState,
2018-09-06 15:04:28 +00:00
TrezorDevice,
2018-08-14 12:56:47 +00:00
} from 'flowtype';
2018-04-16 21:19:50 +00:00
2018-08-14 13:18:16 +00:00
import type { Account } from 'reducers/AccountsReducer';
import type { Token } from 'reducers/TokensReducer';
import type { PendingTx } from 'reducers/PendingTxReducer';
import type { Discovery } from 'reducers/DiscoveryReducer';
2018-02-20 09:30:36 +00:00
// https://github.com/STRML/react-localstorage/blob/master/react-localstorage.js
// or
// https://www.npmjs.com/package/redux-react-session
2018-07-30 10:52:13 +00:00
const findAccounts = (devices: Array<TrezorDevice>, accounts: Array<Account>): Array<Account> => devices.reduce((arr, dev) => arr.concat(accounts.filter(a => a.deviceState === dev.state)), []);
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
const findTokens = (accounts: Array<Account>, tokens: Array<Token>): Array<Token> => accounts.reduce((arr, account) => arr.concat(findAccountTokens(tokens, account)), []);
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
const findDiscovery = (devices: Array<TrezorDevice>, discovery: Array<Discovery>): Array<Discovery> => devices.reduce((arr, dev) => arr.concat(discovery.filter(a => a.deviceState === dev.state && a.publicKey.length > 0)), []);
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
const findPendingTxs = (accounts: Array<Account>, pending: Array<PendingTx>): Array<PendingTx> => accounts.reduce((result, account) => result.concat(pending.filter(p => p.address === account.address && p.network === account.network)), []);
2018-03-08 16:10:53 +00:00
2018-04-16 21:19:50 +00:00
const save = (dispatch: Dispatch, getState: GetState): void => {
const devices: Array<TrezorDevice> = getState().devices.filter(d => d.features && d.remember === true);
2018-04-16 21:19:50 +00:00
const accounts: Array<Account> = findAccounts(devices, getState().accounts);
const tokens: Array<Token> = findTokens(accounts, getState().tokens);
const pending: Array<PendingTx> = findPendingTxs(accounts, getState().pending);
const discovery: Array<Discovery> = findDiscovery(devices, getState().discovery);
2018-02-20 09:30:36 +00:00
// save devices
2018-07-30 10:52:13 +00:00
dispatch(LocalStorageActions.save('devices', JSON.stringify(devices)));
2018-02-20 09:30:36 +00:00
// save already preloaded accounts
2018-07-30 10:52:13 +00:00
dispatch(LocalStorageActions.save('accounts', JSON.stringify(accounts)));
2018-02-20 09:30:36 +00:00
// save discovery state
2018-07-30 10:52:13 +00:00
dispatch(LocalStorageActions.save('discovery', JSON.stringify(discovery)));
2018-02-20 09:30:36 +00:00
// tokens
2018-07-30 10:52:13 +00:00
dispatch(LocalStorageActions.save('tokens', JSON.stringify(tokens)));
2018-03-08 16:10:53 +00:00
// pending transactions
2018-07-30 10:52:13 +00:00
dispatch(LocalStorageActions.save('pending', JSON.stringify(pending)));
};
2018-02-20 09:30:36 +00:00
2018-04-16 21:19:50 +00:00
const LocalStorageService: Middleware = (api: MiddlewareAPI) => (next: MiddlewareDispatch) => (action: Action): Action => {
2018-05-18 17:46:35 +00:00
// Application live cycle starts here
2018-05-22 17:42:03 +00:00
// if (action.type === LOCATION_CHANGE) {
// const { location } = api.getState().router;
// if (!location) {
// api.dispatch( WalletActions.init() );
// // load data from config.json and local storage
// api.dispatch( LocalStorageActions.loadData() );
// }
// }
2018-02-20 09:30:36 +00:00
next(action);
switch (action.type) {
// first time saving
2018-07-30 10:52:13 +00:00
case CONNECT.REMEMBER:
2018-04-16 21:19:50 +00:00
save(api.dispatch, api.getState);
2018-07-30 10:52:13 +00:00
break;
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
case TOKEN.ADD:
case TOKEN.REMOVE:
case TOKEN.SET_BALANCE:
2018-04-16 21:19:50 +00:00
save(api.dispatch, api.getState);
2018-07-30 10:52:13 +00:00
break;
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
case ACCOUNT.CREATE:
case ACCOUNT.SET_BALANCE:
case ACCOUNT.SET_NONCE:
2018-04-16 21:19:50 +00:00
save(api.dispatch, api.getState);
2018-07-30 10:52:13 +00:00
break;
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
case DISCOVERY.START:
case DISCOVERY.STOP:
case DISCOVERY.COMPLETE:
2018-04-16 21:19:50 +00:00
save(api.dispatch, api.getState);
2018-07-30 10:52:13 +00:00
break;
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
case CONNECT.FORGET:
case CONNECT.FORGET_SINGLE:
case DEVICE.CHANGED:
case DEVICE.DISCONNECT:
case CONNECT.AUTH_DEVICE:
2018-04-16 21:19:50 +00:00
save(api.dispatch, api.getState);
2018-07-30 10:52:13 +00:00
break;
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
case SEND.TX_COMPLETE:
case PENDING.TX_RESOLVED:
case PENDING.TX_NOT_FOUND:
2018-04-16 21:19:50 +00:00
save(api.dispatch, api.getState);
2018-07-30 10:52:13 +00:00
break;
2018-02-20 09:30:36 +00:00
}
2018-04-16 21:19:50 +00:00
return action;
2018-02-20 09:30:36 +00:00
};
export default LocalStorageService;