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

110 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-02-20 09:30:36 +00:00
/* @flow */
'use strict';
import * as CONNECT from '../actions/constants/TrezorConnect';
2018-04-11 10:13:38 +00:00
import * as ADDRESS from '../actions/constants/address';
2018-02-20 09:30:36 +00:00
2018-04-23 10:20:15 +00:00
import type { Action, TrezorDevice } from '../flowtype';
import type {
AddressCreateAction,
AddressSetBalanceAction,
AddressSetNonceAction
} from '../actions/AddressActions';
2018-02-20 09:30:36 +00:00
export type Account = {
loaded: boolean;
+network: string;
+deviceID: string;
+deviceState: ?string;
2018-02-20 09:30:36 +00:00
+index: number;
+addressPath: Array<number>;
+address: string;
balance: string;
nonce: number;
}
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-04-23 10:20:15 +00:00
export const findAccount = (state: State, index: number, deviceState: string, network: string): ?Account => {
2018-04-11 13:21:43 +00:00
return state.find(a => a.deviceState === deviceState && a.index === index && a.network === network);
}
2018-04-23 10:20:15 +00:00
const createAccount = (state: State, action: AddressCreateAction): State => {
2018-02-20 09:30:36 +00:00
// TODO check with device_id
2018-02-20 09:30:36 +00:00
// check if account was created before
2018-04-23 10:20:15 +00:00
const exist: ?Account = state.find((account: Account) => account.address === action.address && account.network === action.network && action.device.features && account.deviceID === action.device.features.device_id);
2018-02-20 09:30:36 +00:00
if (exist) {
return state;
}
const address: Account = {
loaded: false,
network: action.network,
2018-04-23 10:20:15 +00:00
deviceID: action.device.features ? action.device.features.device_id : '0',
deviceState: action.device.state,
2018-02-20 09:30:36 +00:00
index: action.index,
addressPath: action.path,
address: action.address,
balance: '0',
nonce: 0,
}
2018-04-23 10:20:15 +00:00
const newState: State = [ ...state ];
2018-02-20 09:30:36 +00:00
newState.push(address);
return newState;
}
2018-04-23 10:20:15 +00:00
const removeAccounts = (state: State, device: TrezorDevice): State => {
return state.filter(account => device.features && account.deviceID !== device.features.device_id);
2018-02-20 09:30:36 +00:00
}
2018-04-23 10:20:15 +00:00
// const forgetAccounts = (state: State, action: any): State => {
// return state.filter(account => action.accounts.indexOf(account) === -1);
// }
2018-02-20 09:30:36 +00:00
2018-04-23 10:20:15 +00:00
const setBalance = (state: State, action: AddressSetBalanceAction): State => {
const index: number = state.findIndex(account => account.address === action.address && account.network === action.network);
2018-04-23 10:20:15 +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-04-23 10:20:15 +00:00
const setNonce = (state: State, action: AddressSetNonceAction): State => {
const index: number = state.findIndex(account => account.address === action.address && account.network === action.network);
2018-04-23 10:20:15 +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-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) {
case ADDRESS.CREATE :
return createAccount(state, action);
case CONNECT.FORGET :
case CONNECT.FORGET_SINGLE :
2018-04-23 10:20:15 +00:00
return removeAccounts(state, action.device);
2018-02-20 09:30:36 +00:00
//case CONNECT.FORGET_SINGLE :
// return forgetAccounts(state, action);
case ADDRESS.SET_BALANCE :
return setBalance(state, action);
case ADDRESS.SET_NONCE :
return setNonce(state, action);
case ADDRESS.FROM_STORAGE :
return action.payload;
default:
return state;
}
}