1
0
mirror of https://github.com/trezor/trezor-wallet synced 2025-02-11 23:52:47 +00:00
trezor-wallet/src/actions/LocalStorageActions.js

228 lines
6.8 KiB
JavaScript
Raw Normal View History

2018-02-20 09:30:36 +00:00
/* @flow */
2018-07-30 10:52:13 +00:00
2018-02-20 09:30:36 +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';
import { JSONRequest, httpRequest } from 'utils/networkUtils';
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
import type {
ThunkAction, AsyncAction, GetState, Dispatch, TrezorDevice,
2018-08-14 12:56:47 +00:00
} from 'flowtype';
2018-08-14 13:18:16 +00:00
import type { Config, Coin, TokensCollection } from 'reducers/LocalStorageReducer';
2018-04-16 21:19:50 +00:00
2018-08-14 12:56:47 +00:00
import AppConfigJSON from 'data/appConfig.json';
import Erc20AbiJSON from 'data/ERC20Abi.json';
2018-04-16 21:19:50 +00:00
export type StorageAction = {
type: typeof STORAGE.READY,
2018-04-23 10:20:15 +00:00
config: Config,
tokens: TokensCollection,
ERC20Abi: Array<Object>
2018-04-16 21:19:50 +00:00
} | {
type: typeof STORAGE.SAVE,
network: string,
} | {
type: typeof STORAGE.ERROR,
error: string,
};
2018-07-30 10:52:13 +00:00
export const loadData = (): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
// check if local storage is available
// let available: boolean = true;
// if (typeof window.localStorage === 'undefined') {
// available = false;
// } else {
// try {
// window.localStorage.setItem('ethereum_wallet', true);
// } catch (error) {
// available = false;
// }
// }
dispatch(loadTokensFromJSON());
};
2018-02-20 09:30:36 +00:00
2018-04-16 21:19:50 +00:00
// const parseConfig = (json: JSON): Config => {
// if (json['coins']) {
// }
// for (let key in json) {
// if (key === 'coins') {
// }
// }
// const coins: Array<Object> = json.coins || [];
// if ("coins" in json){
// json.coins
// }
// if (!json.hasOwnProperty("fiatValueTickers")) throw new Error(`Property "fiatValueTickers" is missing in appConfig.json`);
// if (json.config && json.hasOwnProperty('coins') && Array.isArray(json.coins)) {
// json.coins.map(c => {
// return {
2018-07-30 10:52:13 +00:00
2018-04-16 21:19:50 +00:00
// }
// })
// } else {
// throw new Error(`Property "coins" is missing in appConfig.json`);
// }
2018-07-30 10:52:13 +00:00
2018-04-16 21:19:50 +00:00
// return {
// coins: [],
// fiatValueTickers: []
// }
// }
export function loadTokensFromJSON(): AsyncAction {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
2018-05-17 12:40:38 +00:00
if (typeof window.localStorage === 'undefined') return;
2018-02-20 09:30:36 +00:00
try {
const config: Config = await httpRequest(AppConfigJSON, 'json');
const ERC20Abi = await httpRequest(Erc20AbiJSON, 'json');
2018-07-30 10:52:13 +00:00
window.addEventListener('storage', (event) => {
dispatch(update(event));
});
2018-05-17 12:40:38 +00:00
// load tokens
2018-04-16 21:19:50 +00:00
const tokens = await config.coins.reduce(async (promise: Promise<TokensCollection>, coin: Coin): Promise<TokensCollection> => {
const collection: TokensCollection = await promise;
const json = await httpRequest(coin.tokens, 'json');
2018-07-30 10:52:13 +00:00
collection[coin.network] = json;
return collection;
}, Promise.resolve({}));
2018-02-20 09:30:36 +00:00
const devices: ?string = get('devices');
if (devices) {
dispatch({
type: CONNECT.DEVICE_FROM_STORAGE,
2018-07-30 10:52:13 +00:00
payload: JSON.parse(devices),
});
2018-02-20 09:30:36 +00:00
}
const accounts: ?string = get('accounts');
if (accounts) {
dispatch({
type: ACCOUNT.FROM_STORAGE,
2018-07-30 10:52:13 +00:00
payload: JSON.parse(accounts),
});
2018-02-20 09:30:36 +00:00
}
const userTokens: ?string = get('tokens');
if (userTokens) {
2018-02-20 09:30:36 +00:00
dispatch({
type: TOKEN.FROM_STORAGE,
2018-07-30 10:52:13 +00:00
payload: JSON.parse(userTokens),
});
2018-02-20 09:30:36 +00:00
}
2018-03-08 16:10:53 +00:00
const pending: ?string = get('pending');
if (pending) {
dispatch({
2018-04-23 10:20:15 +00:00
type: PENDING.FROM_STORAGE,
2018-07-30 10:52:13 +00:00
payload: JSON.parse(pending),
});
2018-03-08 16:10:53 +00:00
}
2018-02-20 09:30:36 +00:00
const discovery: ?string = get('discovery');
if (discovery) {
dispatch({
type: DISCOVERY.FROM_STORAGE,
2018-07-30 10:52:13 +00:00
payload: JSON.parse(discovery),
});
2018-02-20 09:30:36 +00:00
}
2018-07-30 10:52:13 +00:00
2018-02-20 09:30:36 +00:00
dispatch({
type: STORAGE.READY,
config,
tokens,
2018-07-30 10:52:13 +00:00
ERC20Abi,
});
} catch (error) {
2018-02-20 09:30:36 +00:00
dispatch({
type: STORAGE.ERROR,
2018-07-30 10:52:13 +00:00
error,
});
2018-02-20 09:30:36 +00:00
}
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
}
2018-05-17 12:40:38 +00:00
export function update(event: StorageEvent): AsyncAction {
return async (dispatch: Dispatch, getState: GetState): Promise<void> => {
if (!event.newValue) return;
if (event.key === 'devices') {
// 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())
}
if (event.key === 'accounts') {
dispatch({
type: ACCOUNT.FROM_STORAGE,
2018-07-30 10:52:13 +00:00
payload: JSON.parse(event.newValue),
});
}
if (event.key === 'tokens') {
2018-05-17 12:40:38 +00:00
dispatch({
type: TOKEN.FROM_STORAGE,
2018-07-30 10:52:13 +00:00
payload: JSON.parse(event.newValue),
2018-05-17 12:40:38 +00:00
});
}
if (event.key === 'pending') {
dispatch({
type: PENDING.FROM_STORAGE,
2018-07-30 10:52:13 +00:00
payload: JSON.parse(event.newValue),
});
}
if (event.key === 'discovery') {
2018-05-25 09:20:12 +00:00
dispatch({
type: DISCOVERY.FROM_STORAGE,
2018-07-30 10:52:13 +00:00
payload: JSON.parse(event.newValue),
2018-05-25 09:20:12 +00:00
});
}
2018-07-30 10:52:13 +00:00
};
2018-05-17 12:40:38 +00:00
}
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
export const save = (key: string, value: string): ThunkAction => (dispatch: Dispatch, getState: GetState): void => {
if (typeof window.localStorage !== 'undefined') {
try {
window.localStorage.setItem(key, value);
} catch (error) {
// available = false;
console.error(`Local Storage ERROR: ${error}`);
2018-02-20 09:30:36 +00:00
}
}
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
export const get = (key: string): ?string => {
2018-05-17 12:40:38 +00:00
try {
return window.localStorage.getItem(key);
} catch (error) {
// available = false;
return null;
2018-02-20 09:30:36 +00:00
}
2018-07-30 10:52:13 +00:00
};