2018-02-20 09:30:36 +00:00
|
|
|
/* @flow */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import * as CONNECT from '../actions/constants/TrezorConnect';
|
|
|
|
import * as ADDRESS from '../actions/constants/Address';
|
|
|
|
|
|
|
|
export type Account = {
|
|
|
|
loaded: boolean;
|
2018-03-29 11:38:09 +00:00
|
|
|
+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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const initialState: Array<Account> = [];
|
|
|
|
|
|
|
|
const createAccount = (state: Array<Account>, action: any): Array<Account> => {
|
|
|
|
|
2018-03-29 09:35:27 +00:00
|
|
|
// TODO check with device_id
|
2018-02-20 09:30:36 +00:00
|
|
|
// check if account was created before
|
2018-03-29 11:38:09 +00:00
|
|
|
const exist: ?Account = state.find((account: Account) => account.address === action.address && account.network === action.network && account.deviceID === action.device.features.device_id);
|
|
|
|
console.warn("MAM?", exist, action)
|
2018-02-20 09:30:36 +00:00
|
|
|
if (exist) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
const address: Account = {
|
|
|
|
loaded: false,
|
2018-03-29 11:38:09 +00:00
|
|
|
network: action.network,
|
|
|
|
deviceID: action.device.features.device_id,
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
const newState: Array<Account> = [ ...state ];
|
|
|
|
newState.push(address);
|
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
|
|
|
|
const removeAccounts = (state: Array<Account>, action: any): Array<Account> => {
|
2018-03-29 11:38:09 +00:00
|
|
|
return state.filter(account => account.deviceID !== action.device.features.device_id);
|
2018-02-20 09:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const forgetAccounts = (state: Array<Account>, action: any): Array<Account> => {
|
2018-03-29 11:38:09 +00:00
|
|
|
return state.filter(account => action.accounts.indexOf(account) === -1);
|
2018-02-20 09:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const setBalance = (state: Array<Account>, action: any): Array<Account> => {
|
2018-03-29 11:38:09 +00:00
|
|
|
const index: number = state.findIndex(account => account.address === action.address && account.network === action.network);
|
2018-02-20 09:30:36 +00:00
|
|
|
const newState: Array<Account> = [ ...state ];
|
|
|
|
newState[index].loaded = true;
|
|
|
|
newState[index].balance = action.balance;
|
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
|
|
|
|
const setNonce = (state: Array<Account>, action: any): Array<Account> => {
|
2018-03-29 11:38:09 +00:00
|
|
|
const index: number = state.findIndex(account => account.address === action.address && account.network === action.network);
|
2018-02-20 09:30:36 +00:00
|
|
|
const newState: Array<Account> = [ ...state ];
|
|
|
|
newState[index].loaded = true;
|
|
|
|
newState[index].nonce = action.nonce;
|
|
|
|
return newState;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default (state: Array<Account> = initialState, action: any): Array<Account> => {
|
|
|
|
|
|
|
|
switch (action.type) {
|
|
|
|
|
|
|
|
case ADDRESS.CREATE :
|
|
|
|
return createAccount(state, action);
|
|
|
|
|
|
|
|
case CONNECT.FORGET :
|
|
|
|
case CONNECT.FORGET_SINGLE :
|
|
|
|
return removeAccounts(state, action);
|
|
|
|
|
|
|
|
//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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|