1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-12-04 06:08:10 +00:00
trezor-wallet/src/reducers/AccountsReducer.js

119 lines
4.1 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:11:52 +00:00
import * as CONNECT from 'actions/constants/TrezorConnect';
import * as WALLET from 'actions/constants/wallet';
import * as ACCOUNT from 'actions/constants/account';
2018-02-20 09:30:36 +00:00
2018-08-14 12:56:47 +00:00
import type { Action, TrezorDevice } from 'flowtype';
2018-07-30 10:52:13 +00:00
import type {
AccountSetBalanceAction,
2018-07-30 10:52:13 +00:00
AccountSetNonceAction,
2018-08-14 13:11:52 +00:00
} from 'actions/AccountsActions';
2018-04-23 10:20:15 +00:00
2018-02-20 09:30:36 +00:00
export type Account = {
loaded: boolean;
+network: string;
+deviceID: string;
2018-05-02 09:01:08 +00:00
+deviceState: string;
2018-02-20 09:30:36 +00:00
+index: number;
+addressPath: Array<number>;
+address: string;
balance: string;
nonce: number;
2018-09-12 11:25:32 +00:00
block: number;
transactions: number;
2018-02-20 09:30:36 +00:00
}
2018-04-23 10:20:15 +00:00
export type State = Array<Account>;
const initialState: State = [];
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
export const findAccount = (state: State, index: number, deviceState: string, network: string): ?Account => state.find(a => a.deviceState === deviceState && a.index === index && a.network === network);
2018-04-11 13:21:43 +00:00
export const findDeviceAccounts = (state: State, device: TrezorDevice, network: string): Array<Account> => {
if (network) {
2018-07-30 10:52:13 +00:00
return state.filter(addr => addr.deviceState === device.state && addr.network === network);
}
2018-07-30 10:52:13 +00:00
return state.filter(addr => addr.deviceState === device.state);
};
2018-09-12 11:25:32 +00:00
const createAccount = (state: State, account: Account): State => {
// TODO check with device_id
2018-02-20 09:30:36 +00:00
// check if account was created before
2018-09-12 11:25:32 +00:00
const exist: ?Account = state.find(a => a.address === account.address && a.network === account.network && a.deviceState === account.deviceState);
2018-02-20 09:30:36 +00:00
if (exist) {
return state;
}
2018-09-23 06:36:01 +00:00
const newState: State = [...state];
newState.push(account);
2018-02-20 09:30:36 +00:00
return newState;
2018-07-30 10:52:13 +00:00
};
const removeAccounts = (state: State, device: TrezorDevice): State => state.filter(account => account.deviceState !== device.state);
2018-02-20 09:30:36 +00:00
const clear = (state: State, devices: Array<TrezorDevice>): State => {
2018-07-30 10:52:13 +00:00
let newState: State = [...state];
devices.forEach((d) => {
newState = removeAccounts(newState, d);
});
return newState;
2018-07-30 10:52:13 +00:00
};
2018-09-12 11:25:32 +00:00
const updateAccount = (state: State, account: Account): State => {
const index: number = state.findIndex(a => a.address === account.address && a.network === account.network && a.deviceState === account.deviceState);
const newState: State = [...state];
newState[index] = account;
return newState;
2018-09-23 06:36:01 +00:00
};
2018-09-12 11:25:32 +00:00
const setBalance = (state: State, action: AccountSetBalanceAction): State => {
2018-09-12 11:25:32 +00:00
// const index: number = state.findIndex(account => account.address === action.address && account.network === action.network && account.deviceState === action.deviceState);
const index: number = state.findIndex(account => account.address === action.address && account.network === action.network);
2018-07-30 10:52:13 +00:00
const newState: State = [...state];
2018-02-20 09:30:36 +00:00
newState[index].loaded = true;
newState[index].balance = action.balance;
return newState;
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
const setNonce = (state: State, action: AccountSetNonceAction): State => {
const index: number = state.findIndex(account => account.address === action.address && account.network === action.network && account.deviceState === action.deviceState);
2018-07-30 10:52:13 +00:00
const newState: State = [...state];
2018-02-20 09:30:36 +00:00
newState[index].loaded = true;
newState[index].nonce = action.nonce;
return newState;
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
2018-04-23 10:20:15 +00:00
export default (state: State = initialState, action: Action): State => {
2018-02-20 09:30:36 +00:00
switch (action.type) {
2018-07-30 10:52:13 +00:00
case ACCOUNT.CREATE:
2018-09-12 11:25:32 +00:00
return createAccount(state, action.payload);
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
case CONNECT.FORGET:
case CONNECT.FORGET_SINGLE:
case CONNECT.FORGET_SILENT:
case CONNECT.RECEIVE_WALLET_TYPE:
2018-04-23 10:20:15 +00:00
return removeAccounts(state, action.device);
2018-02-20 09:30:36 +00:00
2018-07-30 10:52:13 +00:00
case WALLET.CLEAR_UNAVAILABLE_DEVICE_DATA:
return clear(state, action.devices);
2018-02-20 09:30:36 +00:00
//case CONNECT.FORGET_SINGLE :
// return forgetAccounts(state, action);
2018-09-23 06:36:01 +00:00
case ACCOUNT.UPDATE:
2018-09-12 11:25:32 +00:00
return updateAccount(state, action.payload);
2018-07-30 10:52:13 +00:00
case ACCOUNT.SET_BALANCE:
2018-02-20 09:30:36 +00:00
return setBalance(state, action);
2018-07-30 10:52:13 +00:00
case ACCOUNT.SET_NONCE:
2018-02-20 09:30:36 +00:00
return setNonce(state, action);
2018-07-30 10:52:13 +00:00
case ACCOUNT.FROM_STORAGE:
2018-02-20 09:30:36 +00:00
return action.payload;
default:
return state;
}
2018-07-30 10:52:13 +00:00
};