forked from bitrequest/bitrequest.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassets_js_bitrequest_bip39.js
More file actions
1973 lines (1854 loc) · 65.8 KB
/
assets_js_bitrequest_bip39.js
File metadata and controls
1973 lines (1854 loc) · 65.8 KB
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const glob_test_phrase = "army van defense carry jealous true garbage claim echo media make crunch", // random phrase used for test derive
glob_expected_seed = "5b56c417303faa3fcba7e57400e120a0ca83ec5a4fc9ffba757fbe63fbd77a89a1a3be4c67196f57c39a88b76373733891bfaba16ed27a813ceed498804c0570", // expected seed used for test derive
glob_expected_address = "1HQ3rb7nyLPrjnuW85MUknPekwkn7poAUm", // expected addres used for test derive
glob_expected_bech32 = "bc1qg0azlj4w2lrq8jssrrz6eprt2fe7f7edm4vpd5", // expected bech32 addres used for test derive
glob_expected_ltc_address = "LZakyXotaE29Pehw21SoPuU832UhvJp4LG",
glob_expected_bch_cashaddr = "qp5p0eur784pk8wxy2kzlz3ctnq5whfnuqqpp78u22",
glob_expected_doge_address = "DKvWg8UhQSycj1J8QVxeBDkRpbjDkw3DiW",
glob_expected_eth_address = "0x2161DedC3Be05B7Bb5aa16154BcbD254E9e9eb68",
glob_c_derive = {
"bitcoin": true,
"litecoin": true,
"dogecoin": true,
"nano": true,
"monero": true,
"ethereum": true,
"bitcoin-cash": true,
"nimiq": false,
"kaspa": false
},
glob_can_xpub = {
"bitcoin": true,
"litecoin": true,
"dogecoin": true,
"nano": false,
"monero": false,
"ethereum": true,
"bitcoin-cash": true,
"nimiq": false,
"kaspa": false
};
let glob_test_derive = true,
glob_phrasearray,
glob_has_bigint = false,
glob_phraseverified;
$(document).ready(function() {
hasbigint();
//istrial
//bipv_pass
test_bip39();
//bip39_fail
//derive_fail
//derive_xpub_fail
//test_derivation
// Check derivationsn
//check_derivations
//active_xpub
//has_xpub
//is_xpub
//cxpub
//getbip32dat
//hasbip32
// Bip 39 seed generation
make_seed();
restore_seed();
restore_seed_verify();
//get_seedid
//manage_bip32
submit_disclaimer();
//bip39
// Seed panel nav
got_it();
seed_back1();
seed_back2();
//seed_nav
//ls_phrase_obj
//ls_phrase_obj_parsed
//seed_decrypt
backup_continue();
//check_phrase
//get_phrase
//checkmnemonic
//missing_words
//verify_phrase
//shuffleArray
verify_words();
//move_seed_cb
continue_seed();
skip_verify();
//finish_seed
//seed_callback
//deactivate_xpubs
// Test triggers
//derive_addone_trigger();
//derive_addone
//get_latest_index
//key_cc
//key_cc_xpub
//get_rootkey
//derive_all_init
//derive_all
//derive_add_address
//derive_data
//derive_obj
//ch_pending
//get_uniques
copy_phrase();
show_phrase();
//show_phrase_callback
delete_phrase_trigger();
//delete_phrase_verify
// Bip 32 Key derivation
//hmac_encrypt
//toseed
//parse_seed
//newseed
//to_mnemonic
//zfill
// bip32 Derivation
//objectify_extended
//derive_x
//ckd
//keypair_array
//ext_keys
//xpub_obj
//b58c_x_payload
//format_keys
//xpub_prefix
// Phrase info
phrase_info();
//phrase_info_pu
//compatible_wallets
//w_icon
phrase_coin_info();
toggle_dpaths();
//pi_show
test_derive_next();
test_derive_prev();
//test_derive_function
phrase_moreinfo();
phrase_showxp();
});
function hasbigint() {
try {
if (typeof BigInt("1") == "bigint") {
glob_has_bigint = true;
}
} catch (err) {
console.log(err.message);
}
}
function istrial() {
let trialp = br_get_local("tp");
if (trialp) {
let twelvehours = 43200000;
if ((now() - parseFloat(trialp)) < twelvehours) {
return true;
}
}
return false;
}
// Reminder to write down secret phrase
function bipv_pass() {
if (glob_hasbip === true) {
if (glob_bipv === true) {
return true;
}
let used_addresses = filter_all_addressli("seedid", glob_bipid).filter(".used");
if (istrial() === true) {
if (used_addresses.length > 1) {
manage_bip32({
"type": "popup"
});
}
if (used_addresses.length > 2) {
return false;
}
} else {
manage_bip32({
"type": "popup"
});
if (used_addresses.length > 0) {
return false;
}
}
}
return true;
}
// dependencies check
function test_bip39() {
if (crypto === undefined) { // test for window.crypto
bip39_fail();
glob_test_derive = false;
}
if (glob_has_bigint === false) { // test for js BigInt
bip39_fail();
glob_test_derive = false;
}
let k_str = glob_expected_seed.slice(0, 32),
enc_test = aes_enc(glob_test_phrase, k_str),
dec_test = aes_dec(enc_test, k_str);
if (glob_test_phrase != dec_test) { // test encryption
bip39_fail();
glob_test_derive = false;
}
if (toseed(glob_test_phrase) != glob_expected_seed || test_derivation() === false) {
derive_fail(["bitcoin", "litecoin", "dogecoin", "ethereum", "bitcoin-cash"]);
glob_c_derive.bitcoin = false,
glob_c_derive.litecoin = false,
glob_c_derive.dogecoin = false,
glob_c_derive.ethereum = false,
glob_c_derive["bitcoin-cash"] = false;
}
if (bech32_check() === false) { // test for bech32 Derivation
derive_fail(["bitcoin"]);
glob_c_derive.bitcoin = false;
}
if (cashaddr_check() === false) { // test for BCH cashaddr Derivation
derive_fail(["bitcoin-cash"]);
glob_c_derive["bitcoin-cash"] = false;
}
if (nano_check() === false) { // test for nano Derivation
derive_fail(["nano"]);
glob_c_derive.nano = false;
}
if (xmr_check() === false) { // test for xmr Derivation
derive_fail(["monero"]);
glob_c_derive.monero = false;
}
// check xpub derivation
if (xpub_check() === false) { // test for btc xpub derivation
derive_xpub_fail(["bitcoin", "litecoin", "dogecoin", "bitcoin-cash"]);
glob_can_xpub.bitcoin = false,
glob_can_xpub.litecoin = false,
glob_can_xpub.dogecoin = false,
glob_can_xpub["bitcoin-cash"] = false;
}
if (eth_xpub_check() === false) { // test for ethereum xpub derivation
derive_xpub_fail(["ethereum"]);
glob_can_xpub.ethereum = false;
}
}
function bip39_fail() {
glob_body.addClass("nobip");
}
function derive_fail(arr) {
setTimeout(function() {
$.each(arr, function(i, val) {
$("#" + val + "_settings").addClass("no_derive");
});
}, 500)
}
function derive_xpub_fail(arr) {
setTimeout(function() {
$.each(arr, function(i, val) {
$("#" + val + "_settings").addClass("no_xpub");
});
}, 500)
}
// test derivations
function test_derivation() {
let currency = "bitcoin",
test_rootkey = get_rootkey(glob_expected_seed),
bip32dat = getbip32dat(currency),
dx_dat = {
"dpath": "m/44'/0'/0'/0/0",
"key": test_rootkey.slice(0, 64),
"cc": test_rootkey.slice(64)
},
x_keys_dat = derive_x(dx_dat),
key_object = format_keys(glob_expected_seed, x_keys_dat, bip32dat, 0, currency);
if (key_object.address == glob_expected_address) {
return true;
}
return false;
}
function bech32_check() {
let bip84_pub = "03bb4a626f63436a64d7cf1e441713cc964c0d53289a5b17acb1b9c262be57cb17",
bip84_bech32 = pub_to_address_bech32("bc", bip84_pub);
if (glob_expected_bech32 == bip84_bech32) {
return true;
}
return false;
}
function cashaddr_check() {
let bch_legacy = "1AVPurYZinnctgGPiXziwU6PuyZKX5rYZU",
bch_cashaddr = pub_to_cashaddr(bch_legacy);
if (glob_expected_bch_cashaddr == bch_cashaddr) {
return true;
}
return false;
}
function nano_check() {
let expected_nano_address = "nano_1mbtirc4x3kixfy5wufxaqakd3gbojpn6gpmk6kjiyngnjwgy6yty3txgztq",
xnano_address = NanocurrencyWeb.wallet.accounts(glob_expected_seed, 0, 0)[0].address;
if (expected_nano_address == xnano_address) {
return true;
}
return false;
}
function xmr_check() { // https://coinomi.github.io/tools/bip39/
let expected_xmr_address = "477h3C6E6C4VLMR36bQL3yLcA8Aq3jts1AHLzm5QXipDdXVCYPnKEvUKykh2GTYqkkeQoTEhWpzvVQ4rMgLM1YpeD6qdHbS",
ssk = get_ssk(glob_expected_seed, true),
xko = xmr_getpubs(ssk, 0);
if (xko.address == expected_xmr_address) {
return true;
}
return false;
}
function xpub_check() {
let currency = "bitcoin",
xpub_keycc = key_cc_xpub("xpub6Cy7dUR4ZKF22HEuVq7epRgRsoXfL2MK1RE81CSvp1ZySySoYGXk5PUY9y9Cc5ExpnSwXyimQAsVhyyPDNDrfj4xjDsKZJNYgsHXoEPNCYQ"),
dx_dat = {
"dpath": "M/0/0",
"key": xpub_keycc.key,
"cc": xpub_keycc.cc,
"vb": xpub_keycc.version
},
x_keys_dat = derive_x(dx_dat),
bip32dat = getbip32dat(currency),
key_object = format_keys(null, x_keys_dat, bip32dat, 0, currency),
xpub_address = key_object.address,
xpub_wildcard_address = "bc1qk0wlvl4xh3eqe5szqyrlcj4ws8633vz0vhhywl"; // wildcard for bech32 Xpubs (Zpub)
if (xpub_address == glob_expected_address || xpub_address == xpub_wildcard_address) {
return true;
}
return false;
}
function eth_xpub_check() {
let eth_pub = "03c026c4b041059c84a187252682b6f80cbbe64eb81497111ab6914b050a8936fd",
eth_address = pub_to_eth_address(eth_pub);
if (glob_expected_eth_address == eth_address) {
return true;
}
return false;
}
// Check derivationsn
function check_derivations(currency) {
if (glob_test_derive === true && glob_c_derive[currency]) {
let activepub = active_xpub(currency);
if (cxpub(currency) && activepub) {
return "xpub";
}
if (glob_hasbip === true) {
return "seed";
}
}
return false;
}
function active_xpub(currency) {
let haspub = has_xpub(currency)
if (haspub) {
if (haspub.selected === true) {
return haspub;
}
}
return false;
}
function has_xpub(currency) {
let ispub = is_xpub(currency);
if (ispub) {
if (ispub.key) {
return ispub;
}
}
return false;
}
function is_xpub(currency) {
if (cxpub(currency)) {
let xpubli_dat = cs_node(currency, "Xpub", true);
if (xpubli_dat) {
return xpubli_dat;
}
}
return false;
}
function cxpub(currency) {
if (glob_can_xpub[currency]) {
return true;
}
return false;
}
function getbip32dat(currency) {
let xpub_dat = cs_node(currency, "Xpub", true);
if (xpub_dat && xpub_dat.active === true) {
return xpub_dat;
}
let coindata = getcoinconfig(currency);
if (coindata) {
let xpubdat = coindata.settings.Xpub;
if (xpubdat && xpubdat.active) {
return xpubdat;
}
}
return false;
}
function hasbip32(currency) {
let coindata = getcoinconfig(currency);
if (coindata) {
let settings = coindata.settings;
if (settings) {
let xpub = settings.Xpub;
if (xpub) {
if (xpub.active) {
return true;
}
}
}
}
return false;
}
// Bip 39 seed generation
function make_seed() {
$(document).on("click", "#option_makeseed", function() {
let currency = $(this).attr("data-currency");
if (glob_hasbip === true) {
topnotify(translate("alreadyhavesecretphrase"));
return
}
canceldialog();
manage_bip32({
"type": currency,
"edit": true
});
})
}
function restore_seed() {
$(document).on("click", "#rest_seed, .applist.pobox li.seedu .address .srcicon", function() {
if (is_viewonly() === true) {
vu_block();
return false;
}
if (glob_hasbip === true) {
return false;
}
let result = confirm(translate("resoresecretphrase") + "?");
if (result === true) {
let seedid = $(this).attr("data-seedid");
canceloptions();
canceldialog();
bip39({
"type": "restore",
"edit": true,
"seedid": seedid
});
$("#seed_step2").addClass("restore");
seed_nav(2);
$("#bip39phrase").focus();
}
})
}
function restore_seed_verify() {
$(document).on("click", "#restore_seed", function() {
if (glob_hasbip === true) {
return false;
}
glob_phrasearray = null,
glob_phraseverified = false;
let phrase = get_phrase(),
verify = check_phrase(phrase);
if (verify === true) {
let seedid = $(this).attr("data-seedid"),
words = phrase.split(" "),
phraseid = get_seedid(words);
if (seedid == phraseid) {
glob_phrasearray = words,
glob_phraseverified = true;
$("#seed_steps").addClass("checked");
finish_seed();
return
}
shake($("#bip39phrase"));
topnotify(translate("wrongsecretphrase"));
return
}
topnotify(verify);
})
}
function get_seedid(words) {
return hmacsha(btoa(JSON.stringify(words)), "sha256").slice(0, 8);
}
function manage_bip32(dat) {
if (glob_hasbip === true) {
bip39(dat);
return
}
let data = br_dobj(dat, true),
ddat = [{
"div": {
"class": "popform",
"content": [{
"div": {
"class": "inputwrap",
"content": "<p>" + translate("cannotbespend") + "</p>"
},
}]
}
},
{
"div": {
"id": "pk_confirm",
"class": "noselect",
"content": [{
"div": {
"id": "pk_confirmwrap",
"class": "cb_wrap",
"attr": {
"data-checked": "false"
},
"content": [{
"span": {
"class": "checkbox"
}
}]
},
"span": {
"content": translate("understandandok")
}
}]
}
},
{
"input": {
"class": "submit",
"attr": {
"type": "submit",
"value": "ok"
}
}
}
],
content = $(template_dialog({
"id": "disclaimer_dialog",
"icon": "icon-warning",
"title": translate("disclaimer"),
"elements": ddat
})).data(data);
if ($("#option_makeseed").length) {
canceldialog();
setTimeout(function() {
popdialog(content, "triggersubmit");
}, 1000);
} else {
popdialog(content, "triggersubmit");
}
}
function submit_disclaimer() {
$(document).on("click", "#disclaimer_dialog input.submit", function(e) {
e.preventDefault();
let disclaimer_dialog = $("#disclaimer_dialog"),
data = disclaimer_dialog.data(),
pk_checkbox = disclaimer_dialog.find("#pk_confirmwrap"),
pk_checked = pk_checkbox.data("checked");
if (pk_checked == true) {
canceldialog();
bip39(data);
} else {
popnotify("error", translate("consent"));
}
})
}
function bip39(dat) {
glob_phraseverified = false;
let data = br_dobj(dat, true),
phrase_obj = ls_phrase_obj(),
edit = (data && data.edit) ? true : false,
dtype = (data && data.type) ? data.type : null,
restore = (dtype == "restore" && edit === true),
type = (glob_hasbip === true) ? (glob_bipv === true) ? "bipsavedbu" : "bipsaved" : "nobip",
step = (type == "nobip") ? 1 : (type == "bipsavedbu") ? 2 : 3,
spclass = (type == "nobip") ? " showphrase" : " hidephrase",
savedseed = (glob_hasbip === true) ? (phrase_obj) ? phrase_obj.pob.join(" ") : false : false,
seed = (restore) ? "" : (savedseed) ? savedseed : newseed(12),
remindp = (dtype == "restore") ? "<p>" + translate("overwritten") + "</p>" : "<p>" + translate("pleaseverify") + "</p>",
verifyheader = (dtype == "restore") ? translate("verifycurrent") : translate("verifybackup"),
save_str = (restore) ? translate("entersecretphrase") : translate("writedownsecretphrase"),
verify_str = (restore) ? "<div id='restore_seed' class='button' data-seedid='" + data.seedid + "'>" + translate("restorebttn") + "</div>" : "<div id='cfbu2' class='button'>" + translate("ivebackeditup") + "</div>",
markup = $("<div id='seed_steps' class='panel" + step + "' data-goal='" + dtype + "'>\
<div id='seed_step1' class='seed_step'>\
<div class='ss_header'>\
<div class='icon-cross ssnav'></div>\
</div>\
<div class='ss_content flex'>\
<div class='ss_content_box'>\
<h2 style='color:#eeac57'>" + translate("important") + "</h2>\
<p><strong>" + translate("abouttobecome") + "</strong><br/>" + translate("inthenextscreen") + " <strong style='color:#eeac57'>" + translate("makesure") + "</strong></p>\
<p><strong>" + translate("ifyouloseyourdevice") + "</strong></p>\
<p class='p_warning' style='text-transform:uppercase'><strong>" + translate("ifyouloseyourphrase") + "</strong></p>\
</div>\
</div>\
<div class='ss_footer'>\
<div id='cfbu1' class='button'>" + translate("understand") + "</div>\
</div>\
</div>\
<div id='seed_step2' class='seed_step'>\
<div class='ss_header'>\
<div class='icon-arrow-left2 ssnav'></div>\
<div class='icon-cross ssnav'></div>\
</div>\
<div class='ss_content flex'>\
<div id='phrase_cb' class='ss_content_box" + spclass + "'>\
<h2 id='showphrase'><span class='icon-eye-blocked eye'></span><span class='icon-eye eye'></span>" + translate("bip39_passphrase") + ":</h2>\
<p>" + save_str + "</p>\
<div id='phrasewrap'>\
<div id='bip39phrase' contenteditable='" + edit + "' autocomplete='off' autocorrect='off' autocapitalize='off' spellcheck='false' lang='en' class='noselect'>" + seed + "</div>\
<div id='phrase_actions'>\
<div id='copyphrase' class='button'>" + translate("copy") + "</div>\
<div id='phrase_info' title='seed info'><span class='icon-info'></span></div>\
</div>\
<div id='phraseblur'></div>\
</div>\
</div>\
</div>\
<div class='ss_footer'>" + verify_str + "</div>\
</div>\
<div id='seed_step3' class='seed_step'>\
<div class='ss_header'>\
<div class='icon-arrow-left2 ssnav'></div>\
</div>\
<div class='ss_content flex'>\
<div class='ss_content_box'><h2>" + verifyheader + "</h2><p id='reminder_seed_backup'>" + translate("congratulations") + "<br/></p>\
<p id='gpp'>" + translate("withgreatpower") + "<br/><strong>" + translate("remember") + "</strong></p>" + remindp + "<div id='seed_verify_box'>\
</div>\
<div id='cfbu3_w'>\
<div id='cfbu3' class='button'>" + translate("idothislater") + "</div>\
</div>\
</div>\
</div>\
<div class='ss_footer'>\
<div id='continue_seed' class='button'>Continue</div>\
</div>\
</div>\
</div>").data(data);
$("#seed_panel").html(markup).addClass(type);
glob_body.addClass("seed_dialog");
if (step === 3) {
verify_phrase(seed.split(" "), 3);
}
wake();
}
// Seed panel nav
function got_it() {
$(document).on("click", "#cfbu1", function() {
seed_nav(2);
})
}
function seed_back1() {
$(document).on("click", "#seed_steps #seed_step2 .ss_header .icon-arrow-left2", function() {
seed_nav(1);
})
}
function seed_back2() {
$(document).on("click", "#seed_steps #seed_step3 .ss_header .icon-arrow-left2, #seed_steps #seed_step3 #toseed", function() {
seed_nav(2);
$("#seed_step3").removeClass("delete verify");
})
}
function seed_nav(index) {
$("#seed_steps").attr("class", "panel" + index);
}
function ls_phrase_obj() {
if (glob_bipobj) {
return ls_phrase_obj_parsed(glob_bipobj);
}
return false;
}
function ls_phrase_obj_parsed(obj) {
let phrasedat = obj.dat,
pdat_enc = obj.datenc;
if (pdat_enc) {
let pdat_dec = s_decode(pdat_enc),
pdat_dec_dat = pdat_dec.dat;
if (pdat_dec_dat) {
phrasedat = pdat_dec_dat;
}
}
if (phrasedat) {
let datparse = JSON.parse(atob(phrasedat));
return {
"pid": datparse.pid,
"pob": JSON.parse(atob(datparse.pob))
}
}
return false;
}
function seed_decrypt(pin) {
if (glob_bipobj) {
let pdat_enc = glob_bipobj.datenc;
if (pdat_enc) {
let pdat_dec = s_decode(pdat_enc, pin),
pdat_dec_dat = pdat_dec.dat;
if (pdat_dec_dat) {
return JSON.parse(atob(pdat_dec_dat));
}
}
let pdat_dat = glob_bipobj.dat;
if (pdat_dat) {
return JSON.parse(atob(pdat_dat));
}
}
return false;
}
function backup_continue() {
$(document).on("click", "#cfbu2", function() {
glob_phrasearray = null,
glob_phraseverified = false;
let phrase = get_phrase(),
verify = check_phrase(phrase);
if (verify === true) {
let words = phrase.split(" ");
verify_phrase(words, 3);
glob_phrasearray = words;
$("#seed_steps").removeClass("checked");
$("#seed_step3").addClass("verify");
seed_nav(3);
}
topnotify(verify);
})
}
function check_phrase(phrase) {
let cleanphrase = get_phrase(),
words = phrase.split(" "),
phraselength = words.length;
if (phraselength < 2) {
return translate("emptyphrase");
}
if (phraselength === 12) {
if (checkmnemonic(phrase) === false) {
let missing_word = missing_words(words);
if (missing_word) {
return translate("notinwordlist", {
"missing_word": missing_word
});
}
return translate("notbip39compatible");
}
return true;
}
return translate("mustbe12characters");
}
function get_phrase() {
return cleanstring($("#bip39phrase").text());
}
function checkmnemonic(mnemonic) {
let b = mnemonicToBinaryString(mnemonic);
if (b === null) {
return false;
}
let l = b.length,
d = b.substring(0, l / 33 * 32),
h = b.substring(l - l / 33, l),
nd = binaryStringToWordArray(d),
ndHash = sjcl.hash.sha256.hash(nd),
ndHex = sjcl.codec.hex.fromBits(ndHash),
ndBstr = zfill(hexStringToBinaryString(ndHex), 256),
nh = ndBstr.substring(0, l / 33);
return h == nh;
}
function missing_words(words) {
let missing;
$.each(words, function(i, word) {
if (wordlist.indexOf(word) == -1) {
missing = word;
return
}
});
return missing;
}
function verify_phrase(words, count) {
let wordindex = [];
$.each(words, function(i, word) {
wordobject = {
"word": word,
"index": i + 1
}
wordindex.push(wordobject);
});
let shuffled_words = shuffleArray(wordindex),
trimmed_sw = shuffled_words.slice(0, count),
verify_box = $("#seed_verify_box");
verify_box.html("");
$.each(trimmed_sw, function(i, word_obj) {
let word = word_obj.word,
index = word_obj.index,
af_attr = (i === 0) ? " autofocus" : "",
input = "<div class='checkword_box uncheck'><input type='text' placeholder='" + translate("word") + " #" + index + "' data-word='" + word + "'" + af_attr + " autocorrect='off' autocapitalize='none'/><span class='icon-checkmark'></span></div>";
verify_box.append(input);
});
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
let j = Math.floor(Math.random() * (i + 1)),
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
function verify_words() {
$(document).on("input", "#seed_verify_box input", function(e) {
let thisinput = $(this),
cw_box = thisinput.closest(".checkword_box"),
thisword = thisinput.data("word"),
thisval = thisinput.val();
if (thisval == thisword) {
thisinput.blur();
cw_box.removeClass("uncheck");
let unchecked = $("#seed_verify_box").find(".uncheck"),
uclength = unchecked.length;
if (uclength > 0) {
let first_uncheck = unchecked.first().find("input");
setTimeout(function() {
first_uncheck.focus().val("");
}, 500);
return
}
let step3 = $("#seed_step3");
if (step3.hasClass("delete")) {
let result = confirm(translate("areyousuredfp"));
if (result === true) {
br_remove_local("bpdat");
let initdat = br_get_local("init", true),
iodat = br_dobj(initdat, true);
iodat.bipv = "no";
delete iodat.bipv;
br_set_local("init", iodat, true);
glob_hasbip = false;
glob_bipv = false;
glob_bipid = false;
move_seed_cb();
hide_seed_panel();
notify(translate("secretphrasedeleted"));
}
return
}
if (step3.hasClass("replace")) {
let result = confirm(translate("restoresecretphrasefrombackup"));
if (result === true) {
let bu_dat = $("#seed_steps").data().dat;
restore_callback(bu_dat, true);
}
return
}
glob_phraseverified = true,
$("#seed_steps").addClass("checked");
finish_seed();
return
}
cw_box.addClass("uncheck");
});
}
function move_seed_cb() {
$.each(glob_br_config.bitrequest_coin_data, function(i, coinconfig) {
let currency = coinconfig.currency,
bip32 = coinconfig.settings.Xpub;
if (bip32.active) {
let addresslist = get_addresslist(currency);
addresslist.children(".adli").each(function(i) {
let this_li = $(this);
if (this_li.hasClass("seed")) {
let seedid = this_li.data("seedid");
if (seedid == glob_bipid) {
this_li.removeClass("seedu").addClass("seedv").attr("data-checked", "true").data("checked", true);
} else {
this_li.removeClass("seedv").addClass("seedu").attr("data-checked", "false").data("checked", false);
}
}
});
saveaddresses(currency, false);
check_currency(currency);
}
});
}
function continue_seed() {
$(document).on("click", "#continue_seed", function() {
finish_seed();
})
}
function skip_verify() {
$(document).on("click", "#cfbu3", function() {
let content = "<h2><span class='icon-warning' style='color:#B33A3A'></span>" + translate("continueatownrisk") + "</h2><p><strong>" + translate("ifyouloseyourdevice") + "</strong></p>";
popdialog(content, "finish_seed");
})
}
function finish_seed() {
canceldialog();
if (haspin(true) === true) {
seed_callback();
return
}
let cb = {
"func": seed_callback
},
content = pinpanel("", cb);
showoptions(content, "pin");
}
function seed_callback() {
if (glob_hasbip === true) {} else {
let seed_object = {},
seed_string = btoa(JSON.stringify(glob_phrasearray)),
phraseid = hmacsha(seed_string, "sha256").slice(0, 8);
seed_object.pid = phraseid;
seed_object.pob = seed_string;
let savedat = {
"id": phraseid,
"dat": null
};
br_set_local("bpdat", savedat, true);
br_set_local("tp", now());
glob_bipobj = savedat,
glob_hasbip = true,
glob_bipid = phraseid;
notify("🎉 " + translate("congratulations") + " 🎉");
let seedid = phraseid,
savedseed = glob_phrasearray.join(" ");
if (glob_body.hasClass("showstartpage")) {
derive_all_init(savedseed, seedid);
openpage("?p=home", "home", "loadpage");
let currency = $("#seed_steps").attr("data-goal"),
homeli = get_homeli(currency);
homeli.find(".rq_icon").trigger("click");
} else {
let derivations = filter_all_addressli("seedid", seedid);
if (derivations.length > 0) {
move_seed_cb();
}
deactivate_xpubs();
derive_all(savedseed, seedid);
savecurrencies(true);
}
enc_s(seed_object);
}
if (glob_phraseverified === true) {
// save as verified
let initdat = br_get_local("init", true),
iodat = br_dobj(initdat, true);
iodat.bipv = "yes";
br_set_local("init", iodat, true);
glob_bipv = true;
topnotify(translate("passphraseverified"));
} else {
notify(translate("backupasap"));
}
hide_seed_panel();
}