2018-02-20 09:30:36 +00:00
|
|
|
/* @flow */
|
2018-07-30 10:52:13 +00:00
|
|
|
|
2018-08-14 13:18:16 +00:00
|
|
|
import * as CONNECT from 'actions/constants/TrezorConnect';
|
|
|
|
import * as ACCOUNT from 'actions/constants/account';
|
|
|
|
import * as TOKEN from 'actions/constants/token';
|
|
|
|
import * as DISCOVERY from 'actions/constants/discovery';
|
|
|
|
import * as STORAGE from 'actions/constants/localStorage';
|
|
|
|
import * as PENDING from 'actions/constants/pendingTx';
|
2018-10-17 10:09:25 +00:00
|
|
|
import * as WALLET from 'actions/constants/wallet';
|
2018-09-05 14:17:50 +00:00
|
|
|
import { httpRequest } from 'utils/networkUtils';
|
2018-10-11 16:04:25 +00:00
|
|
|
import * as buildUtils from 'utils/build';
|
2018-10-22 08:04:19 +00:00
|
|
|
import * as storageUtils from 'utils/storage';
|
2019-02-15 19:52:56 +00:00
|
|
|
import * as WalletActions from 'actions/WalletActions';
|
2019-02-18 11:49:27 +00:00
|
|
|
import * as l10nUtils from 'utils/l10n';
|
2018-02-20 09:30:36 +00:00
|
|
|
|
2018-12-21 10:19:40 +00:00
|
|
|
import { getAccountTokens } from 'reducers/utils';
|
2018-10-17 10:09:25 +00:00
|
|
|
import type { Account } from 'reducers/AccountsReducer';
|
|
|
|
import type { Token } from 'reducers/TokensReducer';
|
|
|
|
import type { Discovery } from 'reducers/DiscoveryReducer';
|
|
|
|
|
2018-07-30 10:52:13 +00:00
|
|
|
import type {
|
2018-10-17 10:09:25 +00:00
|
|
|
TrezorDevice,
|
|
|
|
ThunkAction,
|
|
|
|
AsyncAction,
|
|
|
|
GetState,
|
|
|
|
Dispatch,
|
2018-12-19 16:44:19 +00:00
|
|
|
Transaction,
|
2018-08-14 12:56:47 +00:00
|
|
|
} from 'flowtype';
|
2018-10-15 13:44:10 +00:00
|
|
|
import type { Config, Network, TokensCollection } from 'reducers/LocalStorageReducer';
|
2018-04-16 21:19:50 +00:00
|
|
|
|
2018-09-04 15:10:52 +00:00
|
|
|
import Erc20AbiJSON from 'public/data/ERC20Abi.json';
|
|
|
|
import AppConfigJSON from 'public/data/appConfig.json';
|
2018-05-18 16:26:45 +00:00
|
|
|
|
2019-03-04 12:33:02 +00:00
|
|
|
export type StorageAction =
|
|
|
|
| {
|
|
|
|
type: typeof STORAGE.READY,
|
|
|
|
config: Config,
|
|
|
|
tokens: TokensCollection,
|
|
|
|
ERC20Abi: Array<TokensCollection>,
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: typeof STORAGE.SAVE,
|
|
|
|
network: string,
|
|
|
|
}
|
|
|
|
| {
|
|
|
|
type: typeof STORAGE.ERROR,
|
|
|
|
error: string,
|
|
|
|
};
|
2018-04-16 21:19:50 +00:00
|
|
|
|
2018-10-22 08:04:19 +00:00
|
|
|
const TYPE: 'local' = 'local';
|
|
|
|
const { STORAGE_PATH } = storageUtils;
|
|
|
|
const KEY_VERSION: string = `${STORAGE_PATH}version`;
|
|
|
|
const KEY_DEVICES: string = `${STORAGE_PATH}devices`;
|
|
|
|
const KEY_ACCOUNTS: string = `${STORAGE_PATH}accounts`;
|
|
|
|
const KEY_DISCOVERY: string = `${STORAGE_PATH}discovery`;
|
|
|
|
const KEY_TOKENS: string = `${STORAGE_PATH}tokens`;
|
|
|
|
const KEY_PENDING: string = `${STORAGE_PATH}pending`;
|
|
|
|
const KEY_BETA_MODAL: string = '/betaModalPrivacy'; // this key needs to be compatible with "parent" (old) wallet
|
2019-02-06 17:03:19 +00:00
|
|
|
const KEY_LANGUAGE: string = `${STORAGE_PATH}language`;
|
2019-03-11 16:45:35 +00:00
|
|
|
const KEY_LOCAL_CURRENCY: string = `${STORAGE_PATH}localCurrency`;
|
2019-03-14 22:31:19 +00:00
|
|
|
const KEY_HIDE_BALANCE: string = `${STORAGE_PATH}hideBalance`;
|
2018-04-16 21:19:50 +00:00
|
|
|
|
2018-10-17 10:09:25 +00:00
|
|
|
// https://github.com/STRML/react-localstorage/blob/master/react-localstorage.js
|
|
|
|
// or
|
|
|
|
// https://www.npmjs.com/package/redux-react-session
|
2018-04-16 21:19:50 +00:00
|
|
|
|
2019-03-04 12:33:02 +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)), []);
|
|
|
|
|
|
|
|
const findTokens = (accounts: Array<Account>, tokens: Array<Token>): Array<Token> =>
|
|
|
|
accounts.reduce((arr, account) => arr.concat(getAccountTokens(tokens, account)), []);
|
|
|
|
|
|
|
|
const findDiscovery = (
|
|
|
|
devices: Array<TrezorDevice>,
|
|
|
|
discovery: Array<Discovery>
|
|
|
|
): Array<Discovery> =>
|
|
|
|
devices.reduce(
|
|
|
|
(arr, dev) => arr.concat(discovery.filter(d => d.deviceState === dev.state && d.completed)),
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
const findPendingTxs = (
|
|
|
|
accounts: Array<Account>,
|
|
|
|
pending: Array<Transaction>
|
|
|
|
): Array<Transaction> =>
|
|
|
|
accounts.reduce(
|
|
|
|
(result, account) =>
|
|
|
|
result.concat(
|
|
|
|
pending.filter(
|
|
|
|
p => p.descriptor === account.descriptor && p.network === account.network
|
|
|
|
)
|
|
|
|
),
|
|
|
|
[]
|
|
|
|
);
|
2018-07-30 10:52:13 +00:00
|
|
|
|
2018-10-17 10:09:25 +00:00
|
|
|
export const save = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
|
2019-03-04 12:33:02 +00:00
|
|
|
const devices: Array<TrezorDevice> = getState().devices.filter(
|
|
|
|
d => d.features && d.remember === true
|
|
|
|
);
|
2018-10-17 10:09:25 +00:00
|
|
|
const accounts: Array<Account> = findAccounts(devices, getState().accounts);
|
|
|
|
const tokens: Array<Token> = findTokens(accounts, getState().tokens);
|
2018-12-19 16:44:19 +00:00
|
|
|
const pending: Array<Transaction> = findPendingTxs(accounts, getState().pending);
|
2018-10-17 10:09:25 +00:00
|
|
|
const discovery: Array<Discovery> = findDiscovery(devices, getState().discovery);
|
2018-04-16 21:19:50 +00:00
|
|
|
|
2018-10-17 10:09:25 +00:00
|
|
|
// save devices
|
2018-10-22 08:04:19 +00:00
|
|
|
storageUtils.set(TYPE, KEY_DEVICES, JSON.stringify(devices));
|
2018-10-17 10:09:25 +00:00
|
|
|
|
|
|
|
// save already preloaded accounts
|
2018-10-22 08:04:19 +00:00
|
|
|
storageUtils.set(TYPE, KEY_ACCOUNTS, JSON.stringify(accounts));
|
2018-10-17 10:09:25 +00:00
|
|
|
|
|
|
|
// save discovery state
|
2018-10-22 08:04:19 +00:00
|
|
|
storageUtils.set(TYPE, KEY_DISCOVERY, JSON.stringify(discovery));
|
2018-10-17 10:09:25 +00:00
|
|
|
|
|
|
|
// tokens
|
2018-10-22 08:04:19 +00:00
|
|
|
storageUtils.set(TYPE, KEY_TOKENS, JSON.stringify(tokens));
|
2018-10-17 10:09:25 +00:00
|
|
|
|
|
|
|
// pending transactions
|
2018-10-22 08:04:19 +00:00
|
|
|
storageUtils.set(TYPE, KEY_PENDING, JSON.stringify(pending));
|
2018-10-17 10:09:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const update = (event: StorageEvent): ThunkAction => (dispatch: Dispatch): void => {
|
|
|
|
if (!event.newValue) return;
|
|
|
|
|
2018-10-22 08:04:19 +00:00
|
|
|
if (event.key === KEY_DEVICES) {
|
2018-10-17 10:09:25 +00:00
|
|
|
// check if device was added/ removed
|
|
|
|
// const newDevices: Array<TrezorDevice> = JSON.parse(event.newValue);
|
|
|
|
// const myDevices: Array<TrezorDevice> = getState().connect.devices.filter(d => d.features);
|
|
|
|
// if (newDevices.length !== myDevices.length) {
|
|
|
|
// const diff = myDevices.filter(d => newDevices.indexOf(d) < 0)
|
|
|
|
// console.warn("DEV LIST CHANGED!", newDevices.length, myDevices.length, diff)
|
|
|
|
// // check if difference is caused by local device which is not saved
|
|
|
|
// // or device which was saved in other tab
|
|
|
|
// }
|
|
|
|
// const diff = oldDevices.filter(d => newDevices.indexOf())
|
|
|
|
}
|
|
|
|
|
2018-10-22 08:04:19 +00:00
|
|
|
if (event.key === KEY_ACCOUNTS) {
|
2018-10-17 10:09:25 +00:00
|
|
|
dispatch({
|
|
|
|
type: ACCOUNT.FROM_STORAGE,
|
|
|
|
payload: JSON.parse(event.newValue),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-22 08:04:19 +00:00
|
|
|
if (event.key === KEY_TOKENS) {
|
2018-10-17 10:09:25 +00:00
|
|
|
dispatch({
|
|
|
|
type: TOKEN.FROM_STORAGE,
|
|
|
|
payload: JSON.parse(event.newValue),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-22 08:04:19 +00:00
|
|
|
if (event.key === KEY_PENDING) {
|
2018-10-17 10:09:25 +00:00
|
|
|
dispatch({
|
|
|
|
type: PENDING.FROM_STORAGE,
|
|
|
|
payload: JSON.parse(event.newValue),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-22 08:04:19 +00:00
|
|
|
if (event.key === KEY_DISCOVERY) {
|
2018-10-17 10:09:25 +00:00
|
|
|
dispatch({
|
|
|
|
type: DISCOVERY.FROM_STORAGE,
|
|
|
|
payload: JSON.parse(event.newValue),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2018-04-16 21:19:50 +00:00
|
|
|
|
2018-10-17 10:09:25 +00:00
|
|
|
const loadJSON = (): AsyncAction => async (dispatch: Dispatch): Promise<void> => {
|
|
|
|
if (typeof window.localStorage === 'undefined') return;
|
2018-05-17 12:40:38 +00:00
|
|
|
|
2018-10-17 10:09:25 +00:00
|
|
|
try {
|
|
|
|
const config: Config = await httpRequest(AppConfigJSON, 'json');
|
2018-10-11 16:04:25 +00:00
|
|
|
|
2018-12-11 12:25:38 +00:00
|
|
|
// remove testnets from config networks
|
2018-10-17 10:09:25 +00:00
|
|
|
if (!buildUtils.isDev()) {
|
2018-12-11 12:25:38 +00:00
|
|
|
config.networks = config.networks.filter(n => !n.testnet);
|
2018-10-17 10:09:25 +00:00
|
|
|
}
|
2018-10-11 16:04:25 +00:00
|
|
|
|
2018-10-17 10:09:25 +00:00
|
|
|
const ERC20Abi = await httpRequest(Erc20AbiJSON, 'json');
|
2018-03-29 09:35:27 +00:00
|
|
|
|
2019-03-04 12:33:02 +00:00
|
|
|
window.addEventListener('storage', event => {
|
2018-10-17 10:09:25 +00:00
|
|
|
dispatch(update(event));
|
|
|
|
});
|
2018-05-17 12:40:38 +00:00
|
|
|
|
2018-10-17 10:09:25 +00:00
|
|
|
// load tokens
|
2019-03-04 12:33:02 +00:00
|
|
|
const tokens = await config.networks.reduce(
|
|
|
|
async (
|
|
|
|
promise: Promise<TokensCollection>,
|
|
|
|
network: Network
|
|
|
|
): Promise<TokensCollection> => {
|
|
|
|
const collection: TokensCollection = await promise;
|
|
|
|
if (network.tokens) {
|
|
|
|
const json = await httpRequest(network.tokens, 'json');
|
|
|
|
collection[network.shortcut] = json;
|
|
|
|
}
|
|
|
|
return collection;
|
|
|
|
},
|
|
|
|
Promise.resolve({})
|
|
|
|
);
|
2018-02-20 09:30:36 +00:00
|
|
|
|
2018-10-17 10:09:25 +00:00
|
|
|
dispatch({
|
|
|
|
type: STORAGE.READY,
|
|
|
|
config,
|
|
|
|
tokens,
|
|
|
|
ERC20Abi,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
dispatch({
|
|
|
|
type: STORAGE.ERROR,
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2018-02-20 09:30:36 +00:00
|
|
|
|
2018-11-23 12:00:03 +00:00
|
|
|
const VERSION: string = '2';
|
2018-02-20 09:30:36 +00:00
|
|
|
|
2018-10-17 10:09:25 +00:00
|
|
|
const loadStorageData = (): ThunkAction => (dispatch: Dispatch): void => {
|
2018-10-22 08:04:19 +00:00
|
|
|
// validate version
|
|
|
|
const version: ?string = storageUtils.get(TYPE, KEY_VERSION);
|
|
|
|
if (version && version !== VERSION) {
|
|
|
|
storageUtils.clearAll();
|
|
|
|
}
|
|
|
|
storageUtils.set(TYPE, KEY_VERSION, VERSION);
|
|
|
|
|
|
|
|
const devices: ?string = storageUtils.get(TYPE, KEY_DEVICES);
|
2018-10-17 10:09:25 +00:00
|
|
|
if (devices) {
|
|
|
|
dispatch({
|
|
|
|
type: CONNECT.DEVICE_FROM_STORAGE,
|
|
|
|
payload: JSON.parse(devices),
|
|
|
|
});
|
|
|
|
}
|
2018-03-08 16:10:53 +00:00
|
|
|
|
2018-10-22 08:04:19 +00:00
|
|
|
const accounts: ?string = storageUtils.get(TYPE, KEY_ACCOUNTS);
|
2018-10-17 10:09:25 +00:00
|
|
|
if (accounts) {
|
|
|
|
dispatch({
|
|
|
|
type: ACCOUNT.FROM_STORAGE,
|
|
|
|
payload: JSON.parse(accounts),
|
|
|
|
});
|
|
|
|
}
|
2018-07-30 10:52:13 +00:00
|
|
|
|
2018-10-22 08:04:19 +00:00
|
|
|
const userTokens: ?string = storageUtils.get(TYPE, KEY_TOKENS);
|
2018-10-17 10:09:25 +00:00
|
|
|
if (userTokens) {
|
|
|
|
dispatch({
|
|
|
|
type: TOKEN.FROM_STORAGE,
|
|
|
|
payload: JSON.parse(userTokens),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-22 08:04:19 +00:00
|
|
|
const pending: ?string = storageUtils.get(TYPE, KEY_PENDING);
|
2018-10-17 10:09:25 +00:00
|
|
|
if (pending) {
|
|
|
|
dispatch({
|
|
|
|
type: PENDING.FROM_STORAGE,
|
|
|
|
payload: JSON.parse(pending),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-22 08:04:19 +00:00
|
|
|
const discovery: ?string = storageUtils.get(TYPE, KEY_DISCOVERY);
|
2018-10-17 10:09:25 +00:00
|
|
|
if (discovery) {
|
|
|
|
dispatch({
|
|
|
|
type: DISCOVERY.FROM_STORAGE,
|
|
|
|
payload: JSON.parse(discovery),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buildUtils.isDev() || buildUtils.isBeta()) {
|
2019-03-04 12:33:02 +00:00
|
|
|
const betaModal = Object.keys(window.localStorage).find(
|
|
|
|
key => key.indexOf(KEY_BETA_MODAL) >= 0
|
|
|
|
);
|
2018-10-17 10:09:25 +00:00
|
|
|
if (!betaModal) {
|
2018-02-20 09:30:36 +00:00
|
|
|
dispatch({
|
2018-10-17 10:09:25 +00:00
|
|
|
type: WALLET.SHOW_BETA_DISCLAIMER,
|
|
|
|
show: true,
|
2018-07-30 10:52:13 +00:00
|
|
|
});
|
2018-02-20 09:30:36 +00:00
|
|
|
}
|
2018-10-17 10:09:25 +00:00
|
|
|
}
|
2019-02-06 17:03:19 +00:00
|
|
|
|
|
|
|
const language: ?string = storageUtils.get(TYPE, KEY_LANGUAGE);
|
|
|
|
if (language) {
|
2019-02-18 11:49:27 +00:00
|
|
|
dispatch(WalletActions.fetchLocale(JSON.parse(language)));
|
|
|
|
} else {
|
|
|
|
dispatch(WalletActions.fetchLocale(l10nUtils.getInitialLocale()));
|
2019-02-06 17:03:19 +00:00
|
|
|
}
|
2019-03-11 16:45:35 +00:00
|
|
|
|
|
|
|
const localCurrency: ?string = storageUtils.get(TYPE, KEY_LOCAL_CURRENCY);
|
|
|
|
if (localCurrency) {
|
|
|
|
dispatch(WalletActions.setLocalCurrency(JSON.parse(localCurrency)));
|
|
|
|
}
|
2019-03-14 22:31:19 +00:00
|
|
|
|
2019-03-26 09:13:30 +00:00
|
|
|
const hideBalance: ?string = storageUtils.get(TYPE, KEY_HIDE_BALANCE);
|
2019-03-14 22:31:19 +00:00
|
|
|
if (hideBalance) {
|
2019-03-22 09:12:38 +00:00
|
|
|
dispatch(WalletActions.setHideBalance(JSON.parse(hideBalance)));
|
2019-03-14 22:31:19 +00:00
|
|
|
}
|
2018-09-05 14:17:50 +00:00
|
|
|
};
|
2018-05-17 14:02:48 +00:00
|
|
|
|
2018-10-17 10:09:25 +00:00
|
|
|
export const loadData = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
|
|
|
|
dispatch(loadStorageData());
|
|
|
|
|
|
|
|
// stop loading resources and wait for user action
|
|
|
|
if (!getState().wallet.showBetaDisclaimer) {
|
|
|
|
dispatch(loadJSON());
|
2018-02-20 09:30:36 +00:00
|
|
|
}
|
2018-10-17 10:09:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const hideBetaDisclaimer = (): ThunkAction => (dispatch: Dispatch): void => {
|
2018-10-22 08:04:19 +00:00
|
|
|
storageUtils.set(TYPE, KEY_BETA_MODAL, true);
|
2018-10-17 10:09:25 +00:00
|
|
|
dispatch(loadJSON());
|
|
|
|
};
|
2019-02-06 17:03:19 +00:00
|
|
|
|
|
|
|
export const setLanguage = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
|
|
|
|
const { language } = getState().wallet;
|
|
|
|
storageUtils.set(TYPE, KEY_LANGUAGE, JSON.stringify(language));
|
|
|
|
};
|
2019-03-11 16:45:35 +00:00
|
|
|
|
2019-03-14 22:31:19 +00:00
|
|
|
export const setHideBalance = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
|
|
|
|
const { hideBalance } = getState().wallet;
|
2019-03-15 13:35:32 +00:00
|
|
|
storageUtils.set(TYPE, KEY_HIDE_BALANCE, hideBalance);
|
2019-03-14 22:31:19 +00:00
|
|
|
};
|
|
|
|
|
2019-03-11 16:45:35 +00:00
|
|
|
export const setLocalCurrency = (): ThunkAction => (
|
|
|
|
dispatch: Dispatch,
|
|
|
|
getState: GetState
|
|
|
|
): void => {
|
|
|
|
const { localCurrency } = getState().wallet;
|
|
|
|
storageUtils.set(TYPE, KEY_LOCAL_CURRENCY, JSON.stringify(localCurrency));
|
|
|
|
};
|