-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.js
1332 lines (1184 loc) · 53.3 KB
/
main.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
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
/* _____ _ _ _ ___ ___ __ ___
/ ____| (_) | | | | |__ \ / _ \/_ |/ _ \
| | ___ _ __ _ _ _ __ _ __ _| |__ | |_ ) | | | || | (_) |
| | / _ \| '_ \| | | | '__| |/ _` | '_ \| __| / /| | | || |> _ <
| |___| (_) | |_) | |_| | | | | (_| | | | | |_ / /_| |_| || | (_) |
\_____\___/| .__/ \__, |_| |_|\__, |_| |_|\__| |____|\___/ |_|\___/
| | __/ | __/ |
|_| |___/ |___/
Copyright 2018 Matthew Cornelisse.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in the
Software without restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the
following conditions:
The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
"use strict";
(function(window,document,undefined){
const MAX_REQUESTS=4; //max number of concurent requests to explorer server
const MAX_UNUSED=20; //bip39 giveup point recommend 20
const SEND_MIN=0.0007; //minimum amount that can be sent to an address
const DUST_VALUE=0.00007;
const ASSET_SERVER='http://createdigiassets.com:8090/';
/* _ _____ ____ _ _ _____ _
| |/ ____|/ __ \| \ | | | __ \ | |
| | (___ | | | | \| | | |__) |___ __ _ _ _ ___ ___| |_
_ | |\___ \| | | | . ` | | _ // _ \/ _` | | | |/ _ \/ __| __|
| |__| |____) | |__| | |\ | | | \ \ __/ (_| | |_| | __/\__ \ |_
\____/|_____/ \____/|_| \_| |_| \_\___|\__, |\__,_|\___||___/\__|
| |
|_|
Has been moved to xmr.js
*/
xmr.setMax(MAX_REQUESTS);
var setServer=function() {
xmr.setServer(document.getElementById('server').value);
}
document.getElementById('server').addEventListener('change',setServer);
setServer();
/*_ ___ _ _____ _
\ \ / (_) | | / ____| | |
\ \ /\ / / _ _ __ __| | _____ __ | (___ _ _ ___| |_ ___ _ __ ___
\ \/ \/ / | | '_ \ / _` |/ _ \ \ /\ / / \___ \| | | / __| __/ _ \ '_ ` _ \
\ /\ / | | | | | (_| | (_) \ V V / ____) | |_| \__ \ || __/ | | | | |
\/ \/ |_|_| |_|\__,_|\___/ \_/\_/ |_____/ \__, |___/\__\___|_| |_| |_|
__/ |
|___/
*/
var domShadow=document['getElementById']('shadow');
var closeWindows=function(shadow) {
var windows=document['getElementsByClassName']('window'); //get all windows
for (var i=0; i<windows['length']; i++) { //go through each window
windows[i]['style']['display']='none'; //make window invisible
}
if (shadow===true) domShadow['style']['display']='none'; //close shadow if set
}
var openWindow=function(windowType) {
closeWindows(); //close all windows
document['getElementById']("window_"+windowType)['style']['display']='block';//open the window associated with button pressed
domShadow['style']['display']='block'; //open shadow
}
var domClose=document.getElementsByClassName("close"); //get all dom items using class next
for (var i=0; i<domClose.length; i++) { //go through each dom element with class "next"
domClose[i].addEventListener('click', function() {closeWindows(true)}, false); //attach click listener to execute closeWindows function
}
/*____ _ ___ _ _ ____ _ _
| _ \ | | / / \ | | | | | _ \ | | | |
| |_) | __ _ ___| | __ / /| \| | _____ _| |_ | |_) |_ _| |_| |_ ___ _ __
| _ < / _` |/ __| |/ / / / | . ` |/ _ \ \/ / __| | _ <| | | | __| __/ _ \| '_ \
| |_) | (_| | (__| < / / | |\ | __/> <| |_ | |_) | |_| | |_| || (_) | | | |
|____/ \__,_|\___|_|\_\/_/ |_| \_|\___/_/\_\\__| |____/ \__,_|\__|\__\___/|_| |_|
To use add clickable item to dom with
class="next"
page="pages div id"
val="optional string to send to function"
Add a div with class="page" to represent the page data
If you want code to be executed when the button is clicked add
$PAGE["page_div_id"]={
valid: function(val) {
return new Promise(function(resolve, reject) { //return promise since execution is asyncronous
});
},
load: function(val) {
//code goes here on page loaded by next button
},
reload: function(val) {
//code goes here on page loaded by back button
}
}
*/
var error=function(e) {
openWindow('error');
console.log(e);
document.getElementById('errorMessage').innerHTML=e;
}
var domNext=document.getElementsByClassName("next"); //get all dom items using class next
var domBack=document.getElementsByClassName("back"); //get all dom items using class next
var domPage=document.getElementsByClassName("page"); //get all dom items using class page
var $PAGE=[]; //make up an array for page functions
var loadPage=function(page) { //function to be executed when something with class "next" is clicked
/* *********************
* 1) Intitialisation *
********************* */
var val,next=true,emptyFunc=function(){return new Promise(function(resolve,reject) {resolve()})}; //define default values if being called directly
if (typeof page!="string") { //if page is not a string then it is called by a link so get values
page=this.getAttribute("page"); //get next page name
val=this.getAttribute("val"); //get optional link value to pass to functions
next=(this.getAttribute("class")=="next"); //see if moving forward or backwards
}
var pageCode=$PAGE[page]||{}; //get page code object if doesn't exist create an empty one
/* **********************
* 2) Validation *
********************** */
var validate=function() {
if (next) { //see if moving to next page because we don't validate fields on back
setTimeout(function(){
(pageCode.valid||emptyFunc)(val).then(load,error); //moving forward so run validation script if any
},10);
} else {
load(); //moving backwards so skip to load
}
}
/* **********************
* 3) Custom Load Code *
********************** */
var load=function() {
setTimeout(function(){
((next?pageCode.load:pageCode.reload)||emptyFunc)(val).then(show,error);//executes page code if exists
},10);
};
/* **********************
* 4) Show Page *
********************** */
var show=function() { //no delay because this won't take long
for (var i=0; i<domPage.length; i++) //go through each element with class "page"
domPage[i].style.display = 'none'; //hide them
document.getElementById(page).style.display = 'block'; //make desired page visible
}
/* *********************
* 0) Start *
********************* */
validate(); //start process
};
for (var i=0; i<domNext.length; i++) { //go through each dom element with class "next"
domNext[i].addEventListener('click', loadPage, false); //attach click listener to execute loadPage function
}
for (var i=0; i<domBack.length; i++) { //go through each dom element with class "back"
domBack[i].addEventListener('click', loadPage, false); //attach click listener to execute loadPage function
}
/* **************************************************************
HANDLE AddressBox
************************************************************** */
var checkAddressValid=function(domItem) {
var newAddress=domItem.innerHTML.trim().replace(/<[^>]*>?/gm,''); //get the new address to add
var html=newAddress;
if (!DigiByte.Address.isValid(newAddress)) {
if (newAddress.toUpperCase().substr(0,4)=='DGB1') {
//find error information
var a=bech32Check(newAddress,'dgb');
if (a.error!=null) {
//handle errors
html='';
for (var p = 0; p < newAddress.length; ++p) {
if (a.pos.indexOf(p) != -1) {
html += '<a style="color:red">' + newAddress.charAt(p) + '</a>';
} else {
html += newAddress.charAt(p);
}
}
}
} else {
html='<a style="color:red">' + newAddress + '</a>';
}
}
//save html out
domItem.innerHTML=html;
}
var domAddressBox=document.getElementsByClassName("addressBox"); //get all dom items using class next
for (var i=0; i<domAddressBox.length; i++) { //go through each dom element with class "next"
(function(domItem) {
var addressBoxTimeoutEvent=null;
domItem.addEventListener('input', function() {
if (addressBoxTimeoutEvent != null) {
clearTimeout(addressBoxTimeoutEvent);
}
addressBoxTimeoutEvent = setTimeout(function() {
checkAddressValid(domItem);
},2000);
}, false); //attach click listener to execute loadPage function
})(domAddressBox[i]);
}
/*_ __ _____
| |/ / | __ \
| ' / ___ _ _ ___ | |__) |_ _ __ _ ___
| < / _ \ | | / __| | ___/ _` |/ _` |/ _ \
| . \ __/ |_| \__ \ | | | (_| | (_| | __/
|_|\_\___|\__, |___/ |_| \__,_|\__, |\___|
__/ | __/ |
|___/ |___/
*/
var checkOldGoWallet=function(data) {
return new Promise(function(resolve,reject) { //return promise since execution is asyncronous
//decode data and verify data
var jsonData=JSON.parse(data);
if (jsonData===null) return reject();
//get keys from file
openWindow("password");
var tryPasswords=function() {
var passwords=document.getElementById("passwords").value.split("\n");
for (var pass of passwords) {
try {
var decoded=JSON.parse(sjcl.decrypt(pass, data));
return resolve(decoded["xPrivKey"]);
} catch(e) {
}
}
openWindow("password");
}
document.getElementById("passwordTry").addEventListener('click',function() {
openWindow("wait");
setTimeout(tryPasswords,10);
});
document.getElementById("passwordFail").addEventListener('click',reject);
});
}
var checkOldWallet=function(data) {
return new Promise(function(resolve,reject) { //return promise since execution is asyncronous
//check if old wallet
var lines=data.split("\n"); //split file to lines
var badLines=0; //initialize bad lines variable
var encoded=''; //initialize encoded variable
for (var line of lines) { //get each line of file
line=line.trim(); //remove excess from line
encoded+=line; //save encoded data
var l=line.length; //get length of line
if ((l!=76)&&(l!=0)) badLines++; //if length is not 0 or 76 characters its a bad line
}
if (badLines>1) return reject(); //if more then 1 line that is wrong length then cancel
if (lines.length<4) return reject(); //if file is to short cancel
//get keys from file
openWindow("password");
var tryPasswords=function() {
var passwords=document.getElementById("passwords").value.split("\n");
for (var pass of passwords) {
try {
var data=GibberishAES.dec(encoded,pass.trim()).split("\n");//decode data
var keys=[]; //initialize keys array
for (var line of data) { //go through each line of the data
line=line.trim(); //remove white space
if ((line.length>0) && (line[0]!='#')) {//only process lines with keys on it
keys.push(line.split(" ")[0]); //split out key
}
}
resolve(keys); //return keys
} catch(e) {
}
}
openWindow("password");
}
document.getElementById("passwordTry").addEventListener('click',function() {
openWindow("wait");
setTimeout(tryPasswords,10);
});
document.getElementById("passwordFail").addEventListener('click',reject);
});
}
document.getElementById('keysFile').addEventListener('change',function(e) {
var file=document.getElementById('keysFile').files[0];
if (file) {
var reader=new FileReader();
reader.readAsText(file,"UTF-8");
reader.onload=function(evt) {
var data=evt.target.result;
checkOldWallet(data).then(function(keys) {
//returns array of keys
document.getElementById("wif").value=keys.join(" ");
loadPage("pageBalances");
}, function() {
checkOldGoWallet(data).then(function(xprv) {
document.getElementById("wif").value=xprv;
loadPage("pageBalances");
}, function() {
//see if contains plain text keysFile
document.getElementById("wif").value=data;
closeWindows(true);
});
});
}
reader.onerror = function() {
error("Couldn't Load File");
}
}
});
/*____ _ _____
| _ \ | | | __ \
| |_) | __ _| | __ _ _ __ ___ ___ | |__) |_ _ __ _ ___
| _ < / _` | |/ _` | '_ \ / __/ _ \ | ___/ _` |/ _` |/ _ \
| |_) | (_| | | (_| | | | | (_| __/ | | | (_| | (_| | __/
|____/ \__,_|_|\__,_|_| |_|\___\___| |_| \__,_|\__, |\___|
__/ |
|___/
*/
var exportKeys=function() {
openWindow('wait');
var txt='data:text/csv;charset=utf-8,';
for (var address in accountData) {
txt+=address+','+accountData[address].private+"\r\n";
}
txt.slice(-2);
document.getElementById('exportLink').setAttribute('href',encodeURI(txt));
openWindow('export');
}
document.getElementById('export').addEventListener('click',exportKeys);
var getDataFromXPrv=function(xprv) {
return new Promise(function(resolve, reject) { //return promise since execution is asyncronous
/* ********************
* 1) Show Wait Window *
******************** */
var showWait=function() {
openWindow("wait");
setTimeout(checkPath,10);
};
/* ***********************************
* 2) Start check of derivative paths *
*********************************** */
var tests=[];
var checkPath=function() {
for (var di=0;di<2;di++) {
tests.push({
"hdKey": bip39.getHDKeyFromXPrv(xprv,"m/44'/0'/0'/"+di),
"start": "D",
"max": 0,
"giveUp": MAX_UNUSED,
"type": (di==0?'input':'change')
});
}
for (var testIndex in tests) { //go through each app test one at a time
for (var i=0;i<MAX_UNUSED;i++) add(testIndex); //add first MAX_UNUSED addresses to buffer if it should
}
};
/* **********************
* 3) Buffer Get *
********************** */
var data={};
var buffer=[];
var active=0;
var add=function(testIndex) {
var test=tests[testIndex];
var keyI=test.max++;
var keyPair=test.hdKey.derive(keyI).keyPair;
buffer.push({
"test":testIndex,
"index":keyI,
"address":keyPair.getAddress(test.start),
"private":keyPair.toWIF(keyI)
});
get();
}
var get=function() {
if (active>=MAX_REQUESTS) return; //don't start if already max active
if (buffer.length==0) {
if (active==0) return resolve(data); //found everything so resolve promise
return; //no room to get another so cancel
}
active++; //set that one more request is active
var curData=buffer.shift(); //remove first element from array
xmr.getJSON('addr/'+curData.address).then(function(reqData) {//make request
active--; //remove as active request
var test=tests[curData.test]; //get app being tested
if (reqData.txApperances!=0) { //see if ay transactions done on address
test.giveUp=MAX_UNUSED; //if used then reset giveup counter
data[curData.address]={ //store returned data
"type": test.type,
"balance":reqData.balance,
"private":curData.private
};
}
get(); //start next buffer read
if (--test.giveUp>0) add(curData.test); //if we haven't failed enough times to giveup then add another attempt
},reject);
}
/* *******************
* 0) Start *
******************* */
showWait();
});
}
var getDataFromSeed=function(seedPhrase) {
return new Promise(function(resolve, reject) { //return promise since execution is asyncronous
/*Resolve:
{
address: {
type: input/change,
balance: dgb,
private: private key
},
address2:...
}
Reject: string
*/
seedPhrase=seedPhrase.join(" "); //recombine seed phrase into a string
var keyData={};
/* ************************
* Check Seed Phrase *
************************ */
var errorText=bip39.findPhraseErrors(seedPhrase);
if (errorText!==false) {
return reject(errorText);
}
/* ************************
* 1) Show BIP39 Window *
************************ */
var showBIP39=function() {
openWindow("paths");
var html='<div class="pathsRow"><div class="pathsHead">Derivative Path</div><div class="pathsHead">Status</div></div>';
for (var i in appTests) {
var testData=appTests[i];
testData.scan=false; //initailise scan flag to false
html+='<div class="pathsRow"><div class="pathsCell">'+testData.name+' Input</div><div class="pathsCell" id="path'+i+'0">Testing</div></div>';
html+='<div class="pathsRow"><div class="pathsCell">'+testData.name+' Change</div><div class="pathsCell" id="path'+i+'1">Testing</div></div>';
}
html+='<div class="pathsRow"><div class="pathsCell">Common DigiID Paths</div><div class="pathsCell" id="pathDigiID">Testing</div></div>';
document.getElementById('pathsTable').innerHTML=html;
setTimeout(quickCheck,10);
}
/* **************************
* 2) Create quick app check *
* scans first 2 incoming, *
* and 2 change addresses *
************************** */
var active=1; //initialise active count as 1 because DigiID requests are not done as part of normal buffer
var updateCount=function(test,count) {
test.dom.innerHTML='Found: '+count+(count>0?'<img class="bip39dots" src="dots.gif">':'<div class="bip39dots"></div>') ;
}
var tests=[];
var hdKey;
var quickCheck=function() {
//generate requests for known apps
var lastMaster='';
var req=[]; //initialise reqeust list
for (var testIndex in appTests) { //go through each app test one at a time
var testData=appTests[testIndex];
for (var di=0;di<2;di++) { //check incoming/change
if (testData.master!=lastMaster) { //speed up processing by only reseting master when needed
bip39.rebuild(testData.master);
lastMaster=testData.master;
hdKey=bip39.getHDKey(seedPhrase);
}
var test={
"hdKey": hdKey.derivePath(testData.derivation+'/'+di),
"failed": 0,
"start": testData.start,
"scanned": 0,
"max": 0,
"giveUp": MAX_UNUSED,
"type": (di==0?'input':'change'),
"dom": document.getElementById('path'+testIndex+di)
};
for (var keyI=0;keyI<2;keyI++) {
var address=test.hdKey.derive(keyI).keyPair.getAddress(test.start);//get address for test
req.push('addr/'+address); //get address for test and save request
}
tests.push(test);
}
}
//generate requests for DigiByte core mobile DigiID
var digiIDPKeys={};
bip39.rebuild('DigiByte seed');
for (var path of sitePaths) {
var keyPair=bip39.getHDKey(seedPhrase).derivePath(path).keyPair; //get key pair for specific site
var address=keyPair.getAddress(); //get address for site
digiIDPKeys[address]=keyPair.toWIF(); //store private key for address in case we need it
req.push('addr/'+address); //save request
}
//generate requests for all others DigiID
bip39.rebuild('Bitcoin seed');
for (var path of sitePaths) {
var keyPair=bip39.getHDKey(seedPhrase).derivePath(path).keyPair; //get key pair for specific site
var address=keyPair.getAddress(); //get address for site
digiIDPKeys[address]=keyPair.toWIF(); //store private key for address in case we need it
req.push('addr/'+address); //save request
}
//make requests and process results
var found=false;
var digiIDfound=0;
var domDigiIDpath=document.getElementById("pathDigiID");
xmr.getJSON(req,"",function(data,index,url) { //make requests of server
if (index/2>=tests.length) {
//DigiID tests
if(data["txApperances"]>0) { //check if address was used
updateCount({"dom":domDigiIDpath},++digiIDfound); //update DigiID count found
found=true; //enable found
keyData[data.addrStr]={ //store returned data
"type": "DigiID",
"balance":data.balance,
"private":digiIDPKeys[data.addrStr]
};
}
} else {
//app tests
var testIndex=Math.floor(index/2); //get index of test
if(data["txApperances"]>0) { //check if address was used
tests[testIndex].failed=0; //reset failed counter
found=true; //enable found
updateCount(tests[testIndex],1); //update table to show we found at least 1
for (var i=0;i<MAX_UNUSED;i++) add(testIndex); //add first MAX_UNUSED addresses to buffer if it should
} else {
tests[testIndex].failed--; //mark test as failed
if (tests[testIndex].failed==-2) { //check if both tests for app failed.
updateCount(tests[testIndex],0); //update table to show we didn't find any
}
}
}
}).then(function(reqResponses) { //execute once all requests have been processed
if (!found) return reject("No transactions found for known apps");
if (--active==0) return resolve(keyData); //found everything so resolve promise
},reject);
}
/* *************************************************
* 3) Full scan of all paths that have transactions *
************************************************* */
var buffer=[];
var add=function(testIndex) {
var test=tests[testIndex];
var keyI=test.max++;
var keyPair=test.hdKey.derive(keyI).keyPair;
buffer.push({
"test":testIndex,
"index":keyI,
"address":keyPair.getAddress(test.start),
"private":keyPair.toWIF()
});
get();
}
var get=function() {
if (active>=MAX_REQUESTS) return; //don't start if already max active
if (buffer.length==0) {
if (active==0) return resolve(keyData); //found everything so resolve promise
return; //no room to get another so cancel
}
active++; //set that one more request is active
var curData=buffer.shift(); //remove first element from array
xmr.getJSON('addr/'+curData.address).then(function(reqData) {//make request
active--; //remove as active request
var test=tests[curData.test]; //get app being tested
if (reqData.txApperances!=0) { //see if ay transactions done on address
test.giveUp=MAX_UNUSED; //if used then reset giveup counter
updateCount(test,++test.scanned); //update count scanned
keyData[curData.address]={ //store returned data
"type": test.type,
"balance":reqData.balance,
"private":curData.private
};
}
get(); //start next buffer read
if (--test.giveUp>0) add(curData.test); //if we haven't failed enough times to giveup then add another attempt
},reject);
}
/* *********************************
* 0) Start *
********************************* */
showBIP39();
});
}
var getDataFromKeys=function(keys) {
return new Promise(function(resolve, reject) { //return promise since execution is asyncronous
/* ************************
* 1) Show getting balance *
************************ */
var showGetting=function() {
openWindow("bal");
document.getElementById("keyCount").innerHTML=keys.length; //show how many keys there are to search
setTimeout(getBalance,10); //allow stuff to show before executing next section
}
/* ************************
* 2) Get balance *
************************ */
var getBalance=function() {
/* *********************************
* 2a) generate server request list *
* computes the public key and makes*
* request for data asociated with *
* public key. Private keys do not *
* leave your computer *
********************************* */
var reqs=[];
var data={};
for (var key of keys) { //go through each private key and look up its value
var publicAddress=DigiByte.PrivateKey.toAddress(key); //computepublic address
reqs.push("addr/"+publicAddress); //add request for data about public key
data[publicAddress]={
"private":key, //temporarily store private key incase user wants to export to file. Requires user request.
"type":"input" //manually entered private keys are always considered input
};
}
/* *********************************
* 2b) execute all requests do 5 at *
* a time to make fast but not so *
* fast server black lists us *
********************************* */
xmr.getJSON(reqs).then(function(reqResponses) { //request the value of each address
for (var addressData of reqResponses) { //go through each response and get the data
var publicAddress=addressData["addrStr"]; //get the public address asociated with request
data[publicAddress]["balance"]=addressData["balance"];//store balance
}
resolve(data);
},reject);
}
/* ***********************
* 0) Start *
*********************** */
showGetting(); //now that functions are defined start execution
});
}
var privateKeys;
var isSeedPhrase;
var isXPrv;
var accountData={}; //initialise private key list(never leaves this page or stored)
$PAGE["pageBalances"]={
valid: function() { //function executes to validate key inputs returns false if no errors.
return new Promise(function(resolve, reject) { //return promise since execution is asyncronous
openWindow("wait"); //open generic wait screen so people know we are doing something
/* ******************
* 1) Process Input *
****************** */
privateKeys=document.getElementById("wif").value.trim(); //get what user typed into input box
if (privateKeys=="") return reject("No Input Provided"); //check that something was entered
privateKeys=privateKeys.replace(/\s/g,","); //replace all white space with ,
privateKeys=privateKeys.split(",").filter(function(e){return e});//split up into array of keys and remove duplicates
/* **********************
* 2) See if Seed Phrase *
********************** */
isSeedPhrase=false; //initialise seed phrase check
if ((privateKeys.length%3==0)&&(privateKeys.length>11)&&(privateKeys.length<25)) { //find if 12,15,18,21,24
if (privateKeys[0].length<20) { //check if short word or long private key
if (bip39.getHDKey(privateKeys.join(' '))!==false) {//check if valid seed phrase(will put any errors to console)
isSeedPhrase=true; //is likely seed phrase
return resolve(); //mark as valid
}
}
}
isXPrv=false;
if ((privateKeys.length==1)&&(privateKeys[0].substr(0,4)=="xprv")) {
isXPrv=true;
return resolve();
}
/* ****************************************************
* 3) Check if valid private keys and decode encrypted *
**************************************************** */
var i=privateKeys.length;
var testNext=function() {
i--;
if (privateKeys[i].substr(0,2)=="6P") { //see if key is encrypted
document.getElementById("password2_key").innerHTML=privateKeys[i];
openWindow("password2");
var tryPasswords=function() {
var passwords=document.getElementById("passwords2").value.split("\n");
var last=passwords.length-1;
var passI=-1;
function tryNextPass() {
var pass=passwords[++passI];
bip38decode(privateKeys[i],pass,function(percent) {
var pdone=Math.round((100*passI+percent)/(passwords.length))+"%";
document.getElementById("progress").innerHTML=pdone;
}).then(function(pKey){
privateKeys[i]=pKey;
i++;
testNext();
},function() {
if (passI!=last) {
tryNextPass();
} else {
openWindow("password2");
}
});
}
tryNextPass();
}
document.getElementById("passwordTry2").addEventListener('click',function() {
openWindow("progress");
setTimeout(tryPasswords,10);
});
document.getElementById("passwordFail2").addEventListener('click',reject);
} else {
if (!DigiByte.PrivateKey.isValid(privateKeys[i])) //looks to see if key is valid
return reject("Invalid Private Key: "+privateKeys[i]);//if not valid then return error message(doesn't bother checking rest of keys)
if (i==0) {
resolve();
} else {
testNext();
}
}
}
testNext();
});
},
load: function() { //function executes when page is loaded by next button
return new Promise(function(resolve, reject) { //return promise since execution is asyncronous
/* *****************************************
* 1) Determine what type of keys inputed *
***************************************** */
var decode=function() {
if (isXPrv) {
getDataFromXPrv(privateKeys[0]).then(finish,reject);
} else {
(isSeedPhrase?getDataFromSeed:getDataFromKeys)(privateKeys).then(finish,reject); //executes apropraite data collection helper
}
}
/* *************************
* 2) Process all addresses *
************************* */
var finish=function(data) {
/*
data={
address: {
type: input/change,
balance: dgb,
private: private key
},
address2:...
}
*/
/* *********************
* Save new keys *
********************* */
for (var publicAddress in data) {
accountData[publicAddress]=data[publicAddress];
}
/* *************************
* Update balance page html *
************************* */
var fundsTotal=0;
var html='<div class="balanceRow"><div class="balanceHead colType">Type</div><div class="balanceHead">Addresses</div><div class="balanceHead">Value</div></div>';
for (var publiAddress in accountData) {
var data=accountData[publiAddress];
html+='<div class="balanceRow"><div class="balanceCellAddress colType">'+data.type+'</div><div class="balanceCellAddress">'+publiAddress+'</div><div class="balanceCellValue">'+data.balance.toFixed(8)+' DGB</div></div>'; //create table row
fundsTotal+=data.balance;
}
document.getElementById("balanceTable").innerHTML=html; //write html code to dom
document.getElementById("balanceTotal").innerHTML=fundsTotal.toFixed(8);//write total balance found
closeWindows(true); //close any open windows and remove shadow
resolve(); //resolve the promise(we are done)
}
/* **************
* 0) Initialize *
************** */
decode();
});
}
}
/*_____ _ _ _ _____
| __ \ (_) (_) | | | __ \
| |__) |___ ___ _ _ __ _ ___ _ __ | |_ ___ | |__) |_ _ __ _ ___
| _ // _ \/ __| | '_ \| |/ _ \ '_ \| __/ __| | ___/ _` |/ _` |/ _ \
| | \ \ __/ (__| | |_) | | __/ | | | |_\__ \ | | | (_| | (_| | __/
|_| \_\___|\___|_| .__/|_|\___|_| |_|\__|___/ |_| \__,_|\__, |\___|
| | __/ |
|_| |___/
*/
var transaction=new DigiByte.Transaction();
transaction.setChange('D9RVKBjzRvnzUUTHxZBNZ3kfAHrmci1v76');
transaction.setSplit(true);
$PAGE["pageRecipients"]={
load: function(){
return new Promise(function(resolve, reject) { //return promise since execution is asyncronous
var unspendable=0;
/* ***********************************************
* 1) Show getting utxo window *
*********************************************** */
var showGettingUTXOs=function() {
openWindow('utxos');
setTimeout(document.getElementById('assetUse').checked?getAssetUTXOs:getUTXOs,10);
}
/* ***********************************************
* 2a) Get all UTXO for address with non 0 balance*
*********************************************** */
var fundsTotal=0;
var getUTXOs=function() {
var req={}; //create object to store requests
for (var address in accountData) {
if (accountData[address].balance>0)
req[address]="addr/"+address+"/utxo";
}
xmr.getJSON(req,"",function(data,address){
for (var utxo of data) {
if (
(utxo["confirmationsFromCache"])&& //no recent coinbase transactions
(utxo["amount"]>DUST_VALUE) //dont include DigiAssets
) {
fundsTotal+=utxo["amount"];
transaction.addIn(utxo,accountData[address].private);
} else if (utxo["confirmationsFromCache"]) {
unspendable+=utxo["amount"];
accountData[address].balance-=utxo["amount"]; //reduce funds if any utxo where unspendable
}
}
}).then(finish,reject);
}
/* ***********************************************
* 2b) Check all UTXOs for assets and move assets to new address*
*********************************************** */
var usableUtxos=[];
var sweepableUtxos=[];
var usableAssets=[];
var getAssetUTXOs=function() {
xmr.setServer(ASSET_SERVER);
var req={}; //create object to store requests
for (var address in accountData) {
if (accountData[address].balance>0)
req[address]="addressinfo/"+address;
}
xmr.getJSON(req,"",function(data,address){
for (var utxo of data['utxos']) {
if (!utxo["used"]) {
if (utxo['assets'].length==0) {
if (utxo['value']>700) {
console.log(utxo);
if ((utxo['value']>2500)&&(utxo['address'][0]=="D")) {
utxo["n"]=utxo["index"];
usableUtxos.push(utxo);
} else {
sweepableUtxos.push(utxo);
}
} else {
accountData[address].balance-=utxo["value"];
}
} else {
//assets
usableAssets.push({
"address": utxo['address'],
"txid": utxo['txid'],
"vout": utxo['index'],
"scriptPubKey":utxo['scriptPubKey']['hex'],
"amount": utxo['value'],
"assets": utxo['assets']
});
}
}
}
}).then(getAssetMeta,reject);
}
var getAssetMeta=function() {
if (usableAssets.length>0) {
var req=[]; //create object to store requests
for (var index in usableAssets) {
var asset=usableAssets[index]['assets'][0];
req[index]="assetmetadata/"+asset['assetId']+"/"+usableAssets[index]['txid']+":"+usableAssets[index]['vout'];
}
//txid:vout
xmr.getJSON(req,"",function(data,index){
usableAssets[index]["metaData"]=data['metadataOfIssuence']['data'];
}).then(sendAssets,reject);
} else {
//process as non asset job(temp work around)
xmr.setServer(document.getElementById('server').value);
getUTXOs();
}
}
var sendAssets=function() {
//process assets
var finishAssets=function(){
/*
//process any remaining utxo so can be processed by main sweep
for (var index in usableUtxos) {
var reformatedUTXO={
"address": usableUtxos[index]['address'],
"txid": usableUtxos[index]['txid'],
"vout": usableUtxos[index]['index'],
"scriptPubKey":usableUtxos[index]['scriptPubKey']['hex'],
"amount": usableUtxos[index]['value']/100000000