-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathaccount.js
473 lines (355 loc) · 14.7 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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
function updateKV(txt, method, value, success, error) {
if (!isInitialized || offline) return;
if (value == null || value.length == 0) {
makeNotice('error', 'misc-error', txt + ': Invalid value');
return;
}
if (sharedKey == null || sharedKey.length == 0 || sharedKey.length != 36) {
makeNotice('error', 'misc-error', 'Shared key is invalid');
return;
}
value = $.trim(value);
setLoadingText(txt);
$.post("/wallet", { guid: guid, sharedKey: sharedKey, length : (value+'').length, payload : value+'', method : method, format : 'plain' }, function(data) {
makeNotice('success', method + '-success', data);
if (success) success();
})
.error(function(data) {
makeNotice('error', method + '-error', data.responseText);
if (error) error();
});
}
function setDoubleEncryptionButton() {
if (double_encryption) {
$('.double-encryption-off').hide();
$('.double-encryption-on').show();
} else {
$('.double-encryption-on').hide();
$('.double-encryption-off').show();
}
$('#double-password').val('');
$('#double-password2').val('');
}
function setDoubleEncryption(value) {
var panic = function(e) {
console.log('Panic ' + e);
//If we caught an exception here the wallet could be in a inconsistent state
//We probably haven't synced it, so no harm done
//But for now panic!
window.location.reload();
};
try {
if (double_encryption == value)
return;
if (value) {
var tpassword = $('#double-password').val();
var tpassword2 = $('#double-password2').val();
if (tpassword == null || tpassword.length == 0 || tpassword.length < 4 || tpassword.length > 255) {
makeNotice('error', 'misc-error', 'Password must be 4 characters or more in length');
return;
}
if (tpassword != tpassword2) {
makeNotice('error', 'misc-error', 'Passwords do not match.');
return;
}
if (tpassword == password) {
makeNotice('error', 'misc-error', 'Second password should not be the same as your main password.');
return;
}
//Ask the use again before we backup
getSecondPassword(function() {
try {
double_encryption = true;
dpassword = tpassword;
for (var key in addresses) {
var addr = addresses[key];
if (addr.priv != null) {
addr.priv = encodePK(B58.decode(addr.priv));
}
}
//N rounds of SHA 256
var round_data = Crypto.SHA256(sharedKey + dpassword, {asBytes: true});
for (var i = 1; i < pbkdf2_iterations; ++i) {
round_data = Crypto.SHA256(round_data, {asBytes: true});
}
dpasswordhash = Crypto.util.bytesToHex(round_data);
//Clear the password to force the user to login again
//Incase they have forgotten their password already
dpassword = null;
getSecondPassword(function() {
try {
checkAllKeys();
backupWallet('update', function() {
setDoubleEncryptionButton();
}, function() {
panic(e);
});
} catch(e) {
panic(e);
}
}, function() {
panic();
});
} catch(e) {
panic(e);
}
}, function () {
panic();
});
} else {
getSecondPassword(function() {
try {
for (var key in addresses) {
var addr = addresses[key];
if (addr.priv != null) {
addr.priv = decryptPK(addr.priv);
}
}
double_encryption = false;
dpassword = null;
checkAllKeys();
backupWallet('update', function() {
setDoubleEncryptionButton();
}, function() {
panic(e);
});
} catch (e) {
panic(e);
}
}, function() {
panic();
});
}
} catch (e) {
panic(e);
}
}
//Get email address, secret phrase, yubikey etc.
function getAccountInfo() {
setLoadingText('Getting Wallet Info');
$.post("/wallet", { guid: guid, sharedKey: sharedKey, method : 'get-info' }, function(data) {
if (data.email != null) {
$('#wallet-email').val(data.email);
$('.my-email').text(data.email);
}
$('#wallet-phrase').val(data.phrase);
$('#two-factor-select').val(data.auth_type);
$('.two-factor').hide();
$('.two-factor.t'+data.auth_type).show(200);
var notifications_type_el = $('#notifications-type');
notifications_type_el.find(':checkbox').prop("checked", false);
notifications_type_el.find('[class^="type"]').hide();
for (var i in data.notifications_type) {
var type = data.notifications_type[i];
console.log(type);
notifications_type_el.find(':checkbox[value="'+type+'"]').prop("checked", true);
notifications_type_el.find('.type-'+type).show();
}
$('#notifications-confirmations').val(data.notifications_confirmations);
$('#notifications-on').val(data.notifications_on);
if (data.alias != null && data.alias.length > 0) {
$('#wallet-alias').val(data.alias);
$('.alias').text('https://blockchain.info/wallet/'+data.alias);
$('.alias').show(200);
}
loadScript(resource + 'wallet/qr.code.creator.js', function() {
var device_qr = makeQRCode(300, 300, 1 , guid + '|' + sharedKey + '|' + password);
$('#device-qr-code').empty().append(device_qr);
if (data.google_secret_url != null && data.google_secret_url.length > 0) {
var qr = makeQRCode(300, 300, 1 , data.google_secret_url);
$('#wallet-google-qr').empty().append(qr);
}
});
if (data.auto_email_backup == 1)
$('#auto-email-backup').prop("checked", true);
else
$('#auto-email-backup').prop("checked", false);
$('#wallet-http-url').val(data.http_url);
$('#wallet-http-url').val(data.http_url);
$('#wallet-skype').val(data.skype_username);
$('#wallet-yubikey').val(data.yubikey);
$('#password-hint1').val(data.password_hint1);
$('#password-hint2').val(data.password_hint2);
$('#ip-lock').val(data.ip_lock);
$('#my-ip').text(data.my_ip);
if (data.ip_lock_on == 1)
$('#ip-lock-on').prop("checked", true);
else
$('#ip-lock-on').prop("checked", false);
if (data.email_verified == 0) {
$('#verify-email').show();
$('#email-verified').hide();
} else {
$('#verify-email').hide();
$('#email-verified').show();
}
if (data.sms_verified == 0) {
$('.sms-unverified').show();
$('.sms-verified').hide();
} else {
$('.sms-verified').show();
$('.sms-unverified').hide();
}
$.get(resource + 'wallet/country_codes.html').success(function(data) {
$('.wallet-sms-country-codes').html(data);
}).error(function () {
makeNotice('error', 'misc-error', 'Error Downloading SMS Country Codes')
});
})
.error(function(data) {
makeNotice('error', 'misc-error', data.responseText);
});
}
function bindAccountButtons() {
var notifications_type_el = $('#notifications-type');
notifications_type_el.find(':checkbox').unbind().change(function() {
var val = [];
notifications_type_el.find(':checkbox:checked').each(function () {
val.push($(this).val());
});
//If Empty Add Zero Val
if (!val.length) val.push(0);
updateKV('Updating Notifications Type', 'update-notifications-type', val.join('|'));
notifications_type_el.find('.type-'+$(this).val()).toggle();
//Backup the wallet to syn the pub keys
backupWallet();
});
$('#password-hint1').unbind().change(function() {
updateKV('Updating Main Password Hint', 'update-password-hint1', $(this).val());
});
$('#password-hint2').unbind().change(function() {
updateKV('Updating Second Password Hint', 'update-password-hint2', $(this).val());
});
$('#ip-lock-on').unbind().change(function() {
updateKV('Updating IP Lock', 'update-ip-lock-on', $(this).is(':checked'));
});
$('#ip-lock').unbind().change(function() {
updateKV('Updating Locked Ip Addresses', 'update-ip-lock', $(this).val());
});
$('#notifications-on').unbind().change(function() {
updateKV('Updating Notifications Settings', 'update-notifications-on', $(this).val());
});
$('#auto-email-backup').unbind().change(function() {
updateKV('Updating Auto Backup Settings', 'update-auto-email-backup', $(this).is(':checked'));
});
$('#two-factor-select').unbind().change(function() {
var val = parseInt($(this).val());
updateKV('Updating Two Factor Authentication', 'update-auth-type', val, function() {
//For Google Authenticator we need to refetch the account info to fetch the QR Code
if (val == 4) {
getAccountInfo();
}
//Refresh the cache manifest to clear the wallet data
updateCacheManifest();
});
$('.two-factor').hide(200);
$('.two-factor.t'+val).show(200);
});
$('#wallet-email-send').click(function() {
$('#wallet-email').trigger('change');
});
$('#wallet-email').unbind().change(function(e) {
var email = $(this).val();
if (!validateEmail(email)) {
makeNotice('error', 'misc-error', 'Email address is not valid');
return;
}
updateKV('Updating Email', 'update-email', email);
$('#verify-email').show(200);
$('#email-verified').hide();
});
$('#wallet-double-encryption-enable').click(function(e) {
setDoubleEncryption(true);
});
$('#wallet-double-encryption-disable').click(function(e) {
setDoubleEncryption(false);
});
$('#wallet-email-code').unbind().change(function(e) {
if (!isInitialized || offline) return;
var code = $(this).val();
if (code == null || code.length == 0 || code.length > 255) {
makeNotice('error', 'misc-error', 'You must enter a code to verify');
return;
}
code = $.trim(code);
setLoadingText('Verifying Email');
$.post("/wallet", { guid: guid, payload: code, sharedKey: sharedKey, length : code.length, method : 'verify-email' }, function(data) {
makeNotice('success', 'misc-success', data);
$('#verify-email').hide();
$('#email-verified').show(200);
}).error(function(data) {
makeNotice('error', 'misc-error', data.responseText);
$('#verify-email').show(200);
$('#email-verified').hide();
});
});
$('.wallet-sms-code').unbind().change(function(e) {
var code = $(this).val();
if (code == null || code.length == 0 || code.length > 255) {
makeNotice('error', 'misc-error', 'You must enter an SMS code to verify');
return;
}
code = $.trim(code);
setLoadingText('Verifying SMS Code');
$.post("/wallet", { guid: guid, payload: code, sharedKey : sharedKey, length : code.length, method : 'verify-sms' }, function(data) {
makeNotice('success', 'misc-success', data);
$('.sms-unverified').hide();
$('.sms-verified').show(200);
}).error(function(data) {
makeNotice('error', 'misc-error', data.responseText);
$('.sms-verified').hide();
$('.sms-unverified').show(200);
});
});
$('.wallet-sms').unbind().change(function(e) {
var val = $.trim($(this).val());
if (val == null || val.length == 0) {
return;
}
if (val.charAt(0) == '0')
val = val.substring(1);
if (val.charAt(0) != '+')
val = '+' + $('.wallet-sms-country-codes').val() + val;
updateKV('Updating Cell Number', 'update-sms',val);
$('.sms-unverified').show(200);
$('.sms-verified').hide();
});
$('#run-key-check').click(function() {
getSecondPassword(function() {
try {
checkAllKeys(true);
backupWallet();
} catch (e) {
makeNotice('error', 'misc-error', e);
}
});
});
$('#notifications-confirmations').unbind().change(function(e) {
updateKV('Updating Notification Confirmations', 'update-notifications-confirmations', $(this).val());
});
$('#wallet-yubikey').unbind().change(function(e) {
updateKV('Updating Yubikey', 'update-yubikey', $(this).val());
});
$('#wallet-skype').unbind().change(function(e) {
updateKV('Updating Skype Username', 'update-skype', $(this).val());
});
$('#wallet-http-url').unbind().change(function(e) {
updateKV('Updating HTTP url', 'update-http-url', $(this).val());
});
$('#wallet-phrase').unbind().change(function(e) {
var phrase = $(this).val();
if (phrase == null || phrase.length == 0 || phrase.length > 255) {
makeNotice('error', 'misc-error', 'You must enter a secret phrase');
return;
}
updateKV('Updating Secret Phrase', 'update-phrase', phrase);
});
$('#wallet-alias').unbind().change(function(e) {
$(this).val($(this).val().replace(/[\.,\/ #!$%\^&\*;:{}=`~()]/g,""));
if ($(this).val().length > 0) {
$('.alias').fadeIn(200);
$('.alias').text('https://blockchain.info/wallet/'+$(this).val());
}
updateKV('Updating Alias', 'update-alias', $(this).val());
});
}