MyCrypto/common/utils/localStorage.ts
William O'Beirne db6b737cad Show Recent Txs on Check Tx Page (#1147)
* Save transactions to local storage.

* Checksum more things + reset hash on network change.

* Fix IHexTransaction type, grab from from tx object directly.

* Refactor storage of recent transactions to use redux storage and loading.

* Refactor types to a transactions types file.

* Initial crack at recent transactions tab on account

* Punctuation.

* Transaction Status responsive behavior.

* Refactor transaction helper function out to remove circular dependency.

* Fix typings

* Collapse subtabs to select list when too small.

* s/wallet/address

* Type select onChange

* Get fields from current state if web3 tx
2018-03-14 15:10:14 -05:00

64 lines
1.9 KiB
TypeScript

import { sha256 } from 'ethereumjs-util';
import { State as SwapState } from 'reducers/swap';
import { IWallet, WalletConfig } from 'libs/wallet';
import { AppState } from 'reducers';
export const REDUX_STATE = 'REDUX_STATE';
export function loadState<T>(): T | undefined {
try {
const serializedState = localStorage.getItem(REDUX_STATE);
if (serializedState === null) {
return undefined;
}
return JSON.parse(serializedState || '{}');
} catch (err) {
console.warn(' Warning: corrupted local storage', err);
}
}
export const saveState = (state: any) => {
try {
const serializedState = JSON.stringify(state);
localStorage.setItem(REDUX_STATE, serializedState);
} catch (err) {
console.warn(' Warning: failed to set to local storage', state);
}
};
export type SwapLocalStorage = SwapState;
export function loadStatePropertyOrEmptyObject<T>(key: keyof AppState): T | undefined {
const localStorageState: Partial<AppState> | undefined = loadState();
if (localStorageState) {
if (localStorageState.hasOwnProperty(key)) {
return localStorageState[key] as T;
}
}
return undefined;
}
export function saveWalletConfig(wallet: IWallet, state: Partial<WalletConfig>): WalletConfig {
const oldState = loadWalletConfig(wallet);
const newState = { ...oldState, ...state };
const key = getWalletConfigKey(wallet);
localStorage.setItem(key, JSON.stringify(newState));
return newState;
}
export function loadWalletConfig(wallet: IWallet): WalletConfig {
try {
const key = getWalletConfigKey(wallet);
const state = localStorage.getItem(key);
return state ? JSON.parse(state) : {};
} catch (err) {
console.error('Failed to load wallet state', err);
return {};
}
}
function getWalletConfigKey(wallet: IWallet): string {
const address = wallet.getAddressString();
return sha256(`${address}-mycrypto`).toString('hex');
}