forked from rotki/rotki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userunlock.ts
355 lines (330 loc) · 14.4 KB
/
userunlock.ts
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import {form_entry} from './elements';
import {setup_client_auditor, setupMessageRetrieval, showError, showInfo, suggest_element, unsuggest_element, showAction} from './utils';
import {set_ui_main_currency} from './topmenu';
import {get_total_assets_value, total_table_add_balances} from './balances_table';
import {create_box, create_or_reload_dashboard} from './dashboard';
import {create_task} from './monitor';
import {query_exchange_balances_async} from './exchange';
import {settings} from './settings';
import {UnlockResult} from './model/action-result';
import {service} from './rotkehlchen_service';
import {shell} from 'electron';
function verify_userpass(username: string, password: string) {
if (!username) {
$.alert('Please provide a user name');
return false;
}
if (!/^[0-9a-zA-Z_.-]+$/.test(username)) {
$.alert('A username must contain only alphanumeric characters and have no spaces');
return false;
}
if (!password) {
$.alert('Please provide a password');
return false;
}
return true;
}
function ask_permission(msg: string, username: string, password: string, create_true: boolean, api_key: string, api_secret: string) {
$.confirm({
title: 'Sync Permission Required',
content: msg,
buttons: {
yes: {
text: 'Yes',
btnClass: 'btn-blue',
action: function() {
unlock_async(username, password, create_true, 'yes', api_key, api_secret);
}
},
no: {
text: 'No',
btnClass: 'btn-red',
action: function() {
unlock_async(username, password, create_true, 'no', api_key, api_secret);
}
}
}
});
}
/**
* Registers a handler for pressing Enter Key on form entry buttons
*
* @param content JquerySelectorResult A JQuery selector with the content of the form
* @param id string The id of the form entry field at which pressing enter should work
* @param button_text string The text of the form submit button to activate
*/
function register_enter_keypress(content: JQuery.PlainObject, id: string, button_text: string) {
$(`#${id}`).on('keypress', function(e: JQuery.Event) {
// if the user submits the form by pressing enter in the last field.
if (e.keyCode === 13 || e.which === 13) {
e.preventDefault();
content.find(`button:contains("${button_text}")`).trigger('click'); // reference the button and click it
}
});
}
function prompt_new_account() {
let content_str = '';
content_str += form_entry('User Name', 'user_name_entry', '', 'A name for your user -- only used locally');
content_str += form_entry('Password', 'password_entry', '', 'Password to encrypt your data with', 'password');
content_str += form_entry('Repeat Password', 'repeat_password_entry', '', 'Repeat Password', 'password');
content_str += form_entry('API KEY', 'api_key_entry', '', 'Optional: Only for premium users', '');
content_str += form_entry('API SECRET', 'api_secret_entry', '', 'Optional: Only for premium users', '');
$.confirm({
title: 'Create New Account',
content: content_str,
buttons: {
formSubmit: {
text: 'Create',
btnClass: 'btn-blue',
action: () => {
const $content = $(document);
const username = $content.find('#user_name_entry').val() as string;
const password = $content.find('#password_entry').val() as string;
const password2 = $content.find('#repeat_password_entry').val() as string;
const api_key = $content.find('#api_key_entry').val() as string;
const api_secret = $content.find('#api_secret_entry').val() as string;
if (!verify_userpass(username, password)) {
return false;
}
if (password !== password2) {
$.alert('The given passwords don\'t match');
return false;
}
unlock_user(username, password, true, 'unknown', api_key, api_secret);
return true;
}
},
cancel: function() {
prompt_sign_in();
}
},
onContentReady: function() {
const $content = $(document);
$content.find('form').on('submit', (e: JQuery.TriggeredEvent) => {
// if the user submits the form by pressing enter in the field.
e.preventDefault();
$(e.target).trigger('click'); // reference the button and click it
});
register_enter_keypress($content, 'repeat_password_entry', 'Create');
register_enter_keypress($content, 'api_secret_entry', 'Create');
}
});
}
export function prompt_sign_in() {
let content_str = '';
content_str += form_entry('User Name', 'username_entry');
content_str += form_entry('Password', 'password_entry', '', '', 'password');
$.confirm({
title: 'Sign In',
content: content_str,
buttons: {
formSubmit: {
text: 'Sign In',
btnClass: 'btn-blue',
action: () => {
const $content = $(document);
const username = $content.find('#username_entry').val() as string;
const password = $content.find('#password_entry').val() as string;
if (!verify_userpass(username, password)) {
return false;
}
unlock_user(username, password, false, 'unknown');
return true;
}
},
newAccount: {
text: 'Create New Account',
btnClass: 'btn-blue',
action: () => {
prompt_new_account();
}
}
},
onContentReady: () => {
const $content = $(document);
$content.find('form').on('submit', function(e: JQuery.TriggeredEvent) {
e.preventDefault();
$(e.target).trigger('click'); // reference the button and click it
});
register_enter_keypress($content, 'password_entry', 'Sign In');
}
});
}
let GLOBAL_UNLOCK_DEFERRED: JQuery.Deferred<any> | null = null;
function unlock_async(
username: string,
password: string,
create_true: boolean,
sync_approval: string,
api_key: string,
api_secret: string
): JQuery.Promise<UnlockResult> {
let deferred: JQuery.Deferred<UnlockResult>;
if (!GLOBAL_UNLOCK_DEFERRED) {
console.log('At unlock_async start, creating new deferred object');
deferred = $.Deferred();
GLOBAL_UNLOCK_DEFERRED = deferred;
} else {
console.log('At unlock_async start, using global deferred object');
deferred = GLOBAL_UNLOCK_DEFERRED;
}
service.unlock_user(username, password, create_true, sync_approval, api_key, api_secret).then(res => {
if (!res.result) {
if (res.permission_needed) {
deferred.notify(res.message);
} else {
deferred.reject(res.message);
}
return;
}
deferred.resolve(res);
}).catch((reason: Error) => {
deferred.reject(reason);
});
return deferred.promise();
}
function unlock_user(
username: string,
password: string,
create_true: boolean,
sync_approval: string,
api_key: string = '',
api_secret: string = ''
) {
$.alert({
content: function() {
const self = this as Alert;
return unlock_async(username, password, create_true, sync_approval, api_key, api_secret).done(
(response: UnlockResult) => {
const db_settings = response.settings;
if (!db_settings) {
self.setType('red');
self.setTitle('Sign In Failed');
self.setContentAppend('<div>main_currency not returned from db_settings</div>');
GLOBAL_UNLOCK_DEFERRED = null;
return;
}
self.setType('green');
self.setTitle('Successful Sign In');
self.setContentAppend(`<div>Welcome ${username}!</div>`);
(self.buttons.ok as ConfirmButton).keys = ['enter'];
$('#welcome_text').html(`Welcome ${username}!`);
setup_client_auditor();
setupMessageRetrieval();
settings.user_logged = true;
settings.has_premium = response.premium;
if (db_settings.premium_should_sync) {
settings.premium_should_sync = db_settings.premium_should_sync;
} else {
settings.premium_should_sync = false;
}
const new_main = db_settings.main_currency;
// Before any other calls happen let's make sure we got the
// exchange rates so that everything can be shown to the user
// in their desired currency. Empty list argument means to
// query all fiat currency pairs
service.get_fiat_exchange_rates().then(result => {
const rates = result.exchange_rates;
for (const asset in rates) {
if (!rates.hasOwnProperty(asset)) {
continue;
}
settings.usd_to_fiat_exchange_rates[asset] = parseFloat(rates[asset]);
}
set_ui_main_currency(new_main);
settings.floating_precision = db_settings.ui_floating_precision;
settings.historical_data_start = db_settings.historical_data_start;
settings.eth_rpc_endpoint = db_settings.eth_rpc_endpoint;
settings.include_crypto2crypto = db_settings.include_crypto2crypto;
settings.include_gas_costs = db_settings.include_gas_costs;
settings.taxfree_after_period = db_settings.taxfree_after_period;
settings.balance_save_frequency = db_settings.balance_save_frequency;
settings.last_balance_save = db_settings.last_balance_save;
settings.anonymized_logs = db_settings.anonymized_logs;
settings.date_display_format = db_settings.date_display_format;
const is_new_user = create_true && api_key === '';
const exchanges = response.exchanges;
if (!exchanges) {
return;
}
load_dashboard_after_unlock(exchanges, is_new_user);
}).catch((reason: Error) => {
showError('Connectivity Error', `Failed to acquire fiat to USD exchange rates: ${reason.message}`);
});
GLOBAL_UNLOCK_DEFERRED = null;
}).progress((msg: string) => {
ask_permission(msg, username, password, create_true, api_key, api_secret);
}).fail((error: Error) => {
self.setType('red');
self.setTitle('Sign In Failed');
self.setContentAppend(`<div>${error}</div>`);
// @ts-ignore
self.buttons.ok.action = () => prompt_sign_in();
GLOBAL_UNLOCK_DEFERRED = null;
});
}
});
}
function load_dashboard_after_unlock(exchanges: string[], is_new_user: boolean) {
for (let i = 0; i < exchanges.length; i++) {
const exx = exchanges[i];
settings.connected_exchanges.push(exx);
query_exchange_balances_async(exx, true);
}
if (!is_new_user) {
if (!settings.has_premium) {
showAction(
'Upgrade to Premium',
`Rotki is opensource software and needs your support! Please
consider upgrading to premium by purchasing a premium subscription.
This way you can help us further develop the software and you can
also enjoy additional premium-only features.`,
'Upgrade',
function() {shell.openExternal('https://rotkehlchen.io/products/'); }
);
}
get_blockchain_total();
get_banks_total();
} else {
showInfo(
'Welcome to Rotki!',
'It appears this is your first time using the program. ' +
'Follow the suggestions to integrate with some exchanges or manually input data.'
);
suggest_element('#user-dropdown', 'click_user_dropdown');
$('#user-dropdown').click(() => {
if (settings.start_suggestion === 'click_user_dropdown') {
unsuggest_element('#user-dropdown');
suggest_element('#user_settings_button', 'click_user_settings');
}
});
}
create_or_reload_dashboard();
}
function get_blockchain_total() {
service.query_blockchain_balances_async().then(result => {
console.log(`Blockchain balances returned task id ${result.task_id}`);
create_task(result.task_id, 'query_blockchain_balances_async', 'Query Blockchain Balances', true, true);
}).catch((reason: Error) => {
console.log(`Error at querying blockchain balances: ${reason}`);
});
}
function get_banks_total() {
service.query_fiat_balances().then(result => {
const fiat_total = get_total_assets_value(result);
console.log(`query fiat balances result is: ${JSON.stringify(result, null, 4)}`);
console.log(`Fiat total is: ${fiat_total}`);
if (fiat_total !== 0.0) {
create_box(
'banks_box',
'fa-university',
fiat_total,
settings.main_currency.icon
);
total_table_add_balances('banks', result);
}
}).catch((reason: Error) => {
console.log(`Error at querying fiat balances`);
console.error(reason);
});
}