2018-02-20 09:30:36 +00:00
|
|
|
/* @flow */
|
2018-07-30 10:52:13 +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-12-21 09:58:53 +00:00
|
|
|
|
|
|
|
type AccountCommon = {
|
|
|
|
+imported: boolean,
|
|
|
|
+index: number,
|
|
|
|
+network: string, // network id (shortcut)
|
|
|
|
+deviceID: string, // empty for imported accounts
|
|
|
|
+deviceState: string, // empty for imported accounts
|
|
|
|
+accountPath: Array<number>, // empty for imported accounts
|
|
|
|
+descriptor: string, // address or xpub
|
|
|
|
|
|
|
|
balance: string,
|
|
|
|
availableBalance: string, // balance - pending
|
|
|
|
block: number, // last known (synchronized) block
|
|
|
|
empty: boolean, // account without transactions
|
|
|
|
|
|
|
|
transactions: number, // deprecated
|
|
|
|
};
|
|
|
|
|
2019-03-04 12:33:02 +00:00
|
|
|
export type Account =
|
|
|
|
| (AccountCommon & {
|
|
|
|
networkType: 'ethereum',
|
|
|
|
nonce: number,
|
|
|
|
})
|
|
|
|
| (AccountCommon & {
|
|
|
|
networkType: 'ripple',
|
|
|
|
sequence: number,
|
|
|
|
reserve: string,
|
|
|
|
})
|
|
|
|
| (AccountCommon & {
|
|
|
|
networkType: 'bitcoin',
|
|
|
|
addressIndex: 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
|
|
|
|
2019-03-04 12:33:02 +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);
|
|
|
|
|
|
|
|
export const findDeviceAccounts = (
|
|
|
|
state: State,
|
|
|
|
device: TrezorDevice,
|
|
|
|
network: string
|
|
|
|
): Array<Account> => {
|
2018-05-11 16:29:27 +00:00
|
|
|
if (network) {
|
2018-07-30 10:52:13 +00:00
|
|
|
return state.filter(addr => addr.deviceState === device.state && addr.network === network);
|
2018-05-11 16:29:27 +00:00
|
|
|
}
|
2018-07-30 10:52:13 +00:00
|
|
|
return state.filter(addr => addr.deviceState === device.state);
|
|
|
|
};
|
2018-05-11 16:29:27 +00:00
|
|
|
|
2018-09-12 11:25:32 +00:00
|
|
|
const createAccount = (state: State, account: Account): State => {
|
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
|
2019-03-04 12:33:02 +00:00
|
|
|
const exist: ?Account = state.find(
|
|
|
|
a =>
|
|
|
|
a.descriptor === account.descriptor &&
|
|
|
|
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];
|
2018-05-11 14:45:10 +00:00
|
|
|
newState.push(account);
|
2018-02-20 09:30:36 +00:00
|
|
|
return newState;
|
2018-07-30 10:52:13 +00:00
|
|
|
};
|
|
|
|
|
2019-03-04 12:33:02 +00:00
|
|
|
const removeAccounts = (state: State, device: TrezorDevice): State =>
|
|
|
|
state.filter(account => account.deviceState !== device.state);
|
2018-02-20 09:30:36 +00:00
|
|
|
|
2018-05-28 17:42:03 +00:00
|
|
|
const clear = (state: State, devices: Array<TrezorDevice>): State => {
|
2018-07-30 10:52:13 +00:00
|
|
|
let newState: State = [...state];
|
2019-03-04 12:33:02 +00:00
|
|
|
devices.forEach(d => {
|
2018-05-28 17:42:03 +00:00
|
|
|
newState = removeAccounts(newState, d);
|
|
|
|
});
|
|
|
|
return newState;
|
2018-07-30 10:52:13 +00:00
|
|
|
};
|
2018-05-28 17:42:03 +00:00
|
|
|
|
2018-09-12 11:25:32 +00:00
|
|
|
const updateAccount = (state: State, account: Account): State => {
|
2019-03-04 12:33:02 +00:00
|
|
|
const index: number = state.findIndex(
|
|
|
|
a =>
|
|
|
|
a.descriptor === account.descriptor &&
|
|
|
|
a.network === account.network &&
|
|
|
|
a.deviceState === account.deviceState
|
|
|
|
);
|
2018-09-12 11:25:32 +00:00
|
|
|
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
|
|
|
|
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:
|
2018-10-08 09:59:10 +00:00
|
|
|
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:
|
2018-05-28 17:42:03 +00:00
|
|
|
return clear(state, action.devices);
|
|
|
|
|
2019-03-04 12:33:02 +00:00
|
|
|
//case CONNECT.FORGET_SINGLE :
|
|
|
|
// return forgetAccounts(state, action);
|
2018-02-20 09:30:36 +00:00
|
|
|
|
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.FROM_STORAGE:
|
2018-02-20 09:30:36 +00:00
|
|
|
return action.payload;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2019-03-04 12:33:02 +00:00
|
|
|
};
|