/
account.js
258 lines (240 loc) · 7.93 KB
/
account.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import i18next from 'i18next';
import actionTypes from '../constants/actions';
import { getAccount, setSecondPassphrase } from '../utils/api/account';
import { registerDelegate, getDelegate } from '../utils/api/delegate';
import { getTransactions } from '../utils/api/transactions';
import { getBlocks } from '../utils/api/blocks';
import { updateTransactions } from './transactions';
import { delegateRegisteredFailure } from './delegate';
import { secondPassphraseRegisteredFailure } from './secondPassphrase';
import { liskAPIClientUpdate } from './peers';
import { getTimeOffset } from '../utils/hacks';
import Fees from '../constants/fees';
import transactionTypes from '../constants/transactionTypes';
import { updateWallet } from './wallets';
import accountConfig from '../constants/account';
import { loginType } from '../constants/hwConstants';
import { errorToastDisplayed } from './toaster';
import { tokenMap } from '../constants/tokens';
import { getConnectionErrorMessage } from './network/lsk';
/**
* Trigger this action to remove passphrase from account object
*
* @param {Object} data - account data
* @returns {Object} - Action object
*/
export const removePassphrase = data => ({
data,
type: actionTypes.removePassphrase,
});
/**
* Trigger this action to update the account object
* while already logged in
*
* @param {Object} data - account data
* @returns {Object} - Action object
*/
export const accountUpdated = data => ({
data,
type: actionTypes.accountUpdated,
});
/**
* Trigger this action to log out of the account
* while already logged in
*
* @returns {Object} - Action object
*/
export const accountLoggedOut = () => ({
type: actionTypes.accountLoggedOut,
});
/**
* Trigger this action to login to an account
* The login middleware triggers this action
*
* @param {Object} data - account data
* @returns {Object} - Action object
*/
export const accountLoggedIn = data => ({
type: actionTypes.accountLoggedIn,
data,
});
export const accountLoading = () => ({
type: actionTypes.accountLoading,
});
export const passphraseUsed = data => ({
type: actionTypes.passphraseUsed,
data,
});
export const delegateStatsLoaded = data => ({
type: actionTypes.delegateStatsLoaded,
data,
});
/**
*
*/
export const secondPassphraseRegistered = ({ secondPassphrase, account, passphrase }) =>
(dispatch, getState) => {
const liskAPIClient = getState().peers.liskAPIClient;
const timeOffset = getTimeOffset(getState());
setSecondPassphrase(liskAPIClient, secondPassphrase, account.publicKey, passphrase, timeOffset)
.then((data) => {
dispatch({
data: {
id: data.id,
senderPublicKey: account.publicKey,
senderId: account.address,
amount: 0,
fee: Fees.setSecondPassphrase,
type: transactionTypes.setSecondPassphrase,
},
type: actionTypes.addPendingTransaction,
});
}).catch((error) => {
const text = (error && error.message) ? error.message : i18next.t('An error occurred while registering your second passphrase. Please try again.');
dispatch(secondPassphraseRegisteredFailure({ text }));
});
dispatch(passphraseUsed(passphrase));
};
export const updateDelegateAccount = ({ publicKey }) =>
(dispatch, getState) => {
const liskAPIClient = getState().peers.liskAPIClient;
return getDelegate(liskAPIClient, { publicKey })
.then((response) => {
dispatch(accountUpdated({
...getState().account.info.LSK,
delegate: response.data[0],
isDelegate: true,
}));
});
};
/**
*
*/
export const delegateRegistered = ({
account, passphrase, username, secondPassphrase,
}) =>
(dispatch, getState) => {
const timeOffset = getTimeOffset(getState());
const liskAPIClient = getState().peers.liskAPIClient;
registerDelegate(liskAPIClient, username, passphrase, secondPassphrase, timeOffset)
.then((data) => {
// dispatch to add to pending transaction
dispatch({
data: {
id: data.id,
senderPublicKey: account.publicKey,
senderId: account.address,
username,
amount: 0,
fee: Fees.registerDelegate,
type: transactionTypes.registerDelegate,
},
type: actionTypes.addPendingTransaction,
});
})
.catch((error) => {
dispatch(delegateRegisteredFailure(error));
});
dispatch(passphraseUsed(passphrase));
};
export const loadDelegate = ({ publicKey }) =>
(dispatch, getState) => {
const liskAPIClient = getState().peers.liskAPIClient;
getDelegate(liskAPIClient, { publicKey }).then((response) => {
dispatch({
data: {
delegate: response.delegate,
},
type: actionTypes.updateDelegate,
});
});
};
export const updateTransactionsIfNeeded = ({ transactions, account }, windowFocus) =>
(dispatch) => {
const hasRecentTransactions = txs => (
txs.confirmed.filter(tx => tx.confirmations < 1000).length !== 0 ||
txs.pending.length !== 0
);
if (windowFocus || hasRecentTransactions(transactions)) {
const { filters } = transactions;
const address = transactions.account ? transactions.account.address : account.address;
dispatch(updateTransactions({
pendingTransactions: transactions.pending,
address,
filters,
}));
}
};
export const accountDataUpdated = ({
account, windowIsFocused, transactions,
}) =>
(dispatch, getState) => {
const networkConfig = getState().network;
getAccount({ networkConfig, address: account.address }).then((result) => {
if (result.balance !== account.balance) {
dispatch(updateTransactionsIfNeeded(
{
transactions,
account,
},
!windowIsFocused,
));
}
dispatch(accountUpdated(result));
dispatch(updateWallet(result, getState().peers));
dispatch(liskAPIClientUpdate({ online: true }));
}).catch((res) => {
dispatch(liskAPIClientUpdate({ online: false, code: res.error.code }));
});
};
export const updateAccountDelegateStats = account =>
async (dispatch, getState) => {
const liskAPIClient = getState().peers.liskAPIClient;
const { address, publicKey } = account;
const networkConfig = getState().network;
const token = tokenMap.LSK.key;
const transaction = await getTransactions({
token, networkConfig, address, limit: 1, type: transactionTypes.registerDelegate,
});
const block = await getBlocks(liskAPIClient, { generatorPublicKey: publicKey, limit: 1 });
dispatch(delegateStatsLoaded({
lastBlock: (block.data[0] && block.data[0].timestamp) || '-',
txDelegateRegister: transaction.data[0],
}));
};
export const login = ({ passphrase, publicKey, hwInfo }) => async (dispatch, getState) => {
const networkConfig = getState().network;
dispatch(accountLoading());
await getAccount({
token: tokenMap.LSK.key, networkConfig, publicKey, passphrase,
}).then(async (accountData) => {
const expireTime = (passphrase && getState().settings.autoLog) ?
Date.now() + accountConfig.lockDuration : 0;
dispatch(accountLoggedIn({
...accountData, // TODO remove this after all components are updated to use "info"
passphrase,
loginType: hwInfo ? loginType.ledger : loginType.normal,
hwInfo: hwInfo || {},
expireTime,
info: {
LSK: accountData,
},
}));
// TODO remove this condition with enabling BTC feature
// istanbul ignore else
if (localStorage.getItem('btc')) {
await getAccount({
token: tokenMap.BTC.key, networkConfig, passphrase,
}).then((btcAccountData) => {
dispatch(accountUpdated(btcAccountData));
});
/*
*/
}
}).catch((error) => {
dispatch(errorToastDisplayed({
label: getConnectionErrorMessage(error),
}));
dispatch(accountLoggedOut());
});
};