1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-15 12:59:09 +00:00
trezor-wallet/src/reducers/AccountsReducer.js

131 lines
3.7 KiB
JavaScript
Raw Normal View History

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';
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> => {
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
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];
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
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 => {
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 => {
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:
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);
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
};