This repository has been archived by the owner on Apr 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 127
/
shared.js
770 lines (598 loc) · 21.1 KB
/
shared.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
var satoshi = 100000000; //One satoshi
var show_adv = false;
var adv_rule;
var symbol_btc = {code : "BTC", symbol : "BTC", name : "Bitcoin", conversion : satoshi, symbolAppearsAfter : true, local : false}; //Default BTC Currency Symbol object
var symbol_local = {"conversion":0,"symbol":"$","name":"U.S. dollar","symbolAppearsAfter":false,"local":true,"code":"USD"}; //Users local currency object
var symbol = symbol_btc; //Active currency object
var root = '/';
var resource = '/Resources/';
var war_checksum;
var min = true; //whether to load minified scripts
var isExtension = false;
var APP_VERSION = '1.0'; //Need some way to set this dynamically
var APP_NAME = 'javascript_web';
var IMPORTED_APP_NAME = 'external'; //Need some way to set this dynamically
var IMPORTED_APP_VERSION = '0';
function stripHTML(input) {
return $.trim($('<div>' + input.replace(/(<([^>]+)>)/ig, "") + '</div>').text());
}
function setLocalSymbol(new_symbol) {
if (!new_symbol) return;
if (symbol === symbol_local) {
symbol_local = new_symbol;
symbol = symbol_local;
calcMoney();
} else {
symbol_local = new_symbol;
}
}
function setBTCSymbol(new_symbol) {
if (!new_symbol) return;
if (symbol === symbol_btc) {
symbol_btc = new_symbol;
symbol = symbol_btc;
calcMoney();
} else {
symbol_btc = new_symbol;
}
}
//Assumes 10px margin (modals)
$.fn.center = function () {
scrollTo(0, 0);
this.css("top", parseInt(Math.max(($(window).height() / 2) - (this.height() / 2), 0)) + "px");
this.css("left", parseInt(Math.max(($(window).width() / 2) - (this.width() / 2), 0)) + "px");
return this;
};
//Ignore Console
if (!window.console) {
var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
"group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
window.console = {};
for (var i = 0; i < names.length; ++i) {
window.console[names[i]] = function() {};
}
}
var ws;
var reconnectInterval;
function webSocketConnect(success) {
try {
function reallyConnect() {
try {
var url = "wss://ws.blockchain.info/inv";
console.log('Connect ' + url);
ws = new WebSocket(url);
if (!ws) {
return;
}
if (success)
success(ws);
} catch (e) {
console.log(e);
}
}
//Updates time last block was received and check for websocket connectivity
function reconnectTimer () {
if (!ws || ws.readyState == WebSocket.CLOSED) {
reallyConnect();
}
}
if (window.WebSocket) {
reallyConnect();
if (!reconnectInterval)
reconnectInterval = setInterval(reconnectTimer, 20000);
}
} catch (e) {
console.log(e);
}
}
function BlockFromJSON(json) {
return {
hash : json.hash,
time : json.time,
blockIndex : json.blockIndex,
height : json.height,
txIndex : json.txIndexes,
totalBTCSent : json.totalBTCSent,
foundBy : json.foundBy,
size : json.size
};
}
function TransactionFromJSON(json) {
return {
hash : json.hash,
size : json.size,
txIndex : json.tx_index,
time : json.time,
inputs : json.inputs,
out : json.out,
blockIndex : json.block_index,
result : json.result,
blockHeight : json.block_height,
balance : json.balance,
double_spend : json.double_spend,
note : json.note,
setConfirmations : function(n_confirmations) {
this.confirmations = n_confirmations;
},
getHTML : function(myAddresses, addresses_book) {
var result = this.result;
var container = $('<div class="txdiv" style="padding-top:10px;"></div>');
container.attr('id', 'tx-'+ this.txIndex);
if (this.note) {
var note = $('<div class="alert note"></div>');
note.text(this.note);
container.append(note);
}
var table = $('<table class="table table-striped" cellpadding="0" cellspacing="0" style="padding:0px;float:left;margin:0px;"></table>');
container.append(table);
var tr = $('<tr></tr>');
table.append(tr);
var th = $('<th colspan="4" align="left"></th>');
tr.append(th);
var a = $('<a target="new" style="font-weight:normal"></a>')
th.append(a);
a.attr('href', root+'tx/'+this.hash);
a.text(this.hash);
var span_right = $('<span style="float:right"></span>');
var can_hide = $('<span class="can-hide" style="font-weight:bold"></span>');
span_right.append(can_hide);
if (this.time > 0) {
var date = new Date(this.time * 1000);
can_hide.text(dateToString(date));
}
th.append(span_right);
var tr = $('<tr></tr>');
table.append(tr);
var td = $('<td width="500px"></td>');
td.addClass('txtd');
if (result < 0) {
td.addClass('hidden-phone');
}
tr.append(td);
if (this.inputs.length > 0) {
for (var i = 0; i < this.inputs.length; i++) {
input = this.inputs[i];
if (input.prev_out == null || input.prev_out.addr == null) {
td.text('No Input (Newly Generated Coins)');
td.append($('<br />'))
} else {
td.append(formatOutput(input.prev_out, myAddresses, addresses_book));
}
}
} else {
td.text('Inputs Error');
td.append($('<br />'));
}
var td = $('<td width="48px" class="hidden-phone" style="padding:4px;text-align:center;vertical-align:middle;"></td>');
tr.append(td);
if (result == null) {
result = 0;
for (var i = 0; i < this.out.length; i++) {
result += this.out[i].value;
}
}
if (result > 0) {
td.append($('<img src="'+resource+'arrow_right_green.png" />'));
} else if (result < 0) {
td.append($('<img src="'+resource+'arrow_right_red.png" />'));
} else {
td.text(' ');
}
var td = $('<td></td>');
tr.append(td);
td.addClass('txtd');
if (result >= 0) {
td.addClass('hidden-phone')
}
var escrow_n = null;
var escrow_addr = null;
for (var i = 0; i < this.out.length; i++) {
var out = this.out[i];
if (out.type > 0 && !out.spent && escrow_n == null) {
var myAddr = myAddresses[out.addr];
if (myAddr == null)
myAddr = myAddresses[out.addr2];
if (myAddr == null)
myAddr = myAddresses[out.addr3];
if (myAddr != null && myAddr.priv != null) {
escrow_n = i;
escrow_addr = myAddr;
}
}
td.append(formatOutput(out, myAddresses, addresses_book));
}
var td = $('<td width="140px" style="text-align:right" class="txtd"></td>');
tr.append(td);
for (var i = 0; i < this.out.length; i++) {
output = this.out[i];
td.append('<span class="hidden-phone">' + formatMoney(output.value, true) +'</span><br />');
}
var span = $('<span style="float:right;padding-bottom:30px;clear:both;"></span>');
container.append(span);
if (this.confirmations == null) {
span.append('<button style="display:none"></button> ');
} else if (this.confirmations == 0) {
span.append('<button class="btn btn-danger">Unconfirmed Transaction!</button> ');
} else if (this.confirmations > 0) {
span.append('<button class="btn btn-primary"></button> ');
span.text(this.confirmations + ' Confirmations');
}
var button = $('<button>' + formatMoney(result, true) + '</button> ');
span.append(button);
if (result > 0) {
button.addClass('btn btn-success cb');
} else if (result < 0) {
button.addClass('btn btn-danger cb');
} else {
button.addClass('btn cb');
}
if (this.double_spend == true) {
span.append('<button class="btn btn-danger">Double Spend</button> ');
}
return container;
}
};
}
Date.prototype.sameDayAs = function(pDate){
return ((this.getFullYear()==pDate.getFullYear())&&(this.getMonth()==pDate.getMonth())&&(this.getDate()==pDate.getDate()));
};
function padStr(i) {
return (i < 10) ? "0" + i : "" + i;
}
function dateToString(d) {
if (d.sameDayAs(new Date())) {
return 'Today ' + padStr(d.getHours()) + ':' + padStr(d.getMinutes()) + ':' + padStr(d.getSeconds());
} else {
return padStr(d.getFullYear()) + '-' + padStr(1 + d.getMonth()) + '-' + padStr(d.getDate()) + ' ' + padStr(d.getHours()) + ':' + padStr(d.getMinutes()) + ':' + padStr(d.getSeconds());
}
}
function formatSatoshi(value, shift, no_comma) {
if (!value)
return '0.00';
var neg = '';
if (value < 0) {
value = -value;
neg = '-';
}
if (!shift) shift = 0;
value = ''+parseInt(value);
//TODO Clean this up
var integerPart = (value.length > (8-shift) ? value.substr(0, value.length-(8-shift)) : '0')
if (!no_comma) integerPart = integerPart.replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
var decimalPart = value.length > (8-shift) ? value.substr(value.length-(8-shift)) : value;
if (decimalPart && decimalPart != 0) {
while (decimalPart.length < (8-shift)) decimalPart = "0"+decimalPart;
decimalPart = decimalPart.replace(/0*$/, '');
while (decimalPart.length < 2) decimalPart += "0";
return neg + integerPart+"."+decimalPart;
}
return neg + integerPart;
}
function convert(x, conversion) {
return (x / conversion).toFixed(2).toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1,");
}
//Convenience format satoshi as BTC value string
function formatBTC(x) {
return formatSymbol(x, symbol_btc);
}
//The current 'shift' value - BTC = 1, mBTC = 3, uBTC = 6
function sShift(symbol) {
return (satoshi / symbol.conversion).toString().length-1;
}
function formatSymbol(x, symbol, html) {
var str;
if (symbol !== symbol_btc) {
str = convert(x, symbol.conversion);
} else {
str = formatSatoshi(x, sShift(symbol))
}
if (html) str = str.replace(/([1-9]\d*\.\d{2}?)(.*)/, "$1<span style=\"font-size:85%;\">$2</span>");
if (symbol.symbolAppearsAfter)
str += ' ' +symbol.symbol;
else
str = symbol.symbol + ' ' + str;
return str;
}
function formatMoney(x, span) {
var str = formatSymbol(x, symbol);
if (span) {
str = '<span data-c="'+x+'">'+str+'</span>';
}
return str;
}
function formatOutput(output, myAddresses, addresses_book) {
function formatOut(addr, out) {
var myAddr = null;
if (myAddresses != null)
myAddr = myAddresses[addr];
if (myAddr != null) {
var lspan = $('<span></span>');
if (myAddr.label != null) {
lspan.text(myAddr.label);
} else {
lspan.text(addr);
}
return lspan;
} else {
var a = $('<a target="new"></a>');
a.attr('href', root + 'address/' + addr);
if (addresses_book && addresses_book[addr]) {
a.text(addresses_book[addr]);
} else if (out.addr_tag) {
var container = $('<span></span>');
a.addClass('tag-address');
a.text(addr);
var span = $('<span class="tag"></span>');
span.text('('+out.addr_tag+') ');
if (out.addr_tag_link) {
var a_tag = $('<a class="external" rel="nofollow" target="new"></a>');
a_tag.attr('href', root + 'r?url='+out.addr_tag_link);
span.append(a_tag);
}
container.append(a);
container.append(span);
return container;
} else {
a.text(addr);
}
return a;
}
}
//total_fees -= output.value;
var span = $('<span></span>');
if (output.type == 0) {
} else if (output.type == 1 || output.type == 2 || output.type == 3) {
span.html('(<font color="red">Escrow</font> ' + output.type + ' of ');
} else {
span.html('<font color="red">Strange</font> ');
}
if (output.addr != null) {
span.append(formatOut(output.addr, output));
}
if (output.addr2 != null) {
span.append(', ');
span.append(formatOut(output.addr2, output));
}
if (output.addr3 != null) {
span.append(', ');
span.append(formatOut(output.addr3, output));
}
if (output.type == 1 || output.type == 2 || output.type == 3) {
span.append(')');
}
span.append('<br />');
return span;
}
function toggleAdv() {
setAdv(!show_adv);
}
function setAdv(isOn) {
show_adv = isOn;
if (adv_rule != null) {
adv_rule.remove();
}
if (show_adv) {
adv_rule = $("<style type='text/css'> .adv{display: inherit;} .basic{display: none;} </style>").appendTo("head");
$('a[class=show_adv]').text('Show Basic');
} else {
adv_rule = $("<style type='text/css'> .adv{display: none;} .basic{display: inherit;} </style>").appendTo("head");
$('a[class=show_adv]').text('Show Advanced');
}
}
function calcMoney() {
$('span[data-c]').each(function() {
$(this).text(formatMoney($(this).data('c')));
})
}
function setupSymbolToggle() {
$('.cb').unbind('click').click(function() {
toggleSymbol();
});
$('span[data-c]').unbind('mouseenter mouseleave').mouseenter(function() {
(function(self) {
if (!self.data('time'))
return;
if (!self.data('tooltip')) {
$.ajax({
timeout: 60000,
type: "GET",
dataType: 'text',
url: root + 'frombtc',
data : {value : self.data('c'), currency : symbol_local.code, time : self.data('time'), textual : true, nosavecurrency : true},
success: function(response) {
if (!response) return;
self.tooltip({
placement : 'bottom',
html : false,
trigger : 'manual',
title : response
});
if (self.is(':hover'))
self.tooltip('show');
},
error : function(e) {
console.log(e);
}
});
} else {
self.tooltip('show');
}
})($(this));
}).mouseleave(function() {
if ($(this).data('tooltip'))
$(this).tooltip('hide');
});
}
function toggleSymbol() {
if (symbol_local && symbol === symbol_btc) {
symbol = symbol_local;
SetCookie('local', 'true');
} else {
symbol = symbol_btc;
SetCookie('local', 'false');
}
$('#currencies').val(symbol.code);
calcMoney();
}
var _sounds = {};
function playSound(id) {
try {
if (!_sounds[id])
_sounds[id] = new Audio(resource+id+'.wav');
_sounds[id].play();
} catch (e) { }
};
function setupToggle() {
$('[class=show_adv]').unbind().click(function() {
toggleAdv();
});
}
function updateQueryString(key, value, url) {
if (!url) url = window.location.href;
var re = new RegExp("([?|&])" + key + "=.*?(&|#|$)(.*)", "gi");
if (re.test(url)) {
if (typeof value !== 'undefined' && value !== null)
return url.replace(re, '$1' + key + "=" + value + '$2$3');
else {
return url.replace(re, '$1$3').replace(/(&|\?)$/, '');
}
}
else {
if (typeof value !== 'undefined' && value !== null) {
var separator = url.indexOf('?') !== -1 ? '&' : '?',
hash = url.split('#');
url = hash[0] + separator + key + '=' + value;
if (hash[1]) url += '#' + hash[1];
return url;
}
else
return url;
}
}
$(document).ready(function() {
var footer = $('.footer');
var obj1 = footer.data('symbol-local');
if (obj1) {
symbol_local = obj1;
}
var obj2 = footer.data('symbol-btc');
if (obj2) {
symbol_btc = obj2;
}
if (symbol_local && getCookie('local') == 'true') {
symbol = symbol_local;
} else {
symbol = symbol_btc;
}
war_checksum = $(document.body).data('war-checksum');
show_adv = getCookie('show_adv');
try {
var language_links = $('#languages_select').find('a');
language_links.click(function(e) {
e.preventDefault();
var new_code = $(this).data('code');
SetCookie('clang', new_code);
var path = window.location.pathname;
language_links.each(function() {
var code = $(this).data('code');
if (path.indexOf('/'+code) == 0) {
path = path.replace('/'+code, '/'+new_code);
return false;
}
});
window.location.href = path;
});
$('#currencies').change(function() {
var val = $(this).val();
if (symbol == null || val != symbol.symbol) {
if (symbol_local != null && val == symbol_local.code) {
toggleSymbol();
} else if (symbol_btc != null && val == symbol_btc.code) {
toggleSymbol();
} else {
document.location.href = updateQueryString('currency', val, document.location.href);
}
}
});
setupSymbolToggle();
setupToggle();
setAdv(show_adv);
} catch (e) {}
});
function loadScript(src, success, error) {
var found = false;
$('script').each(function() {
var attr_src = $(this).attr('src');
if (attr_src && attr_src.replace(/^.*[\\\/]/, '').indexOf(src) == 0) {
success();
found = true;
return false;
}
});
if (found) {
return;
}
console.log('Load ' + src);
var error_fired = false;
var s = document.createElement('script');
s.type = "text/javascript";
s.async = true;
s.src = resource + src + (min ? '.min.js' : '.js') + '?'+war_checksum;
try {
s.addEventListener('error', function(e){ error_fired = true; if (error) error('Error Loading Script. Are You Offline?'); }, false);
s.addEventListener('load', function (e) { if (!error_fired) success(); }, false);
} catch (e) {
//IE 7 & 8 Will throw an exception here
setTimeout(function() {
if (!error_fired) success();
}, 10000);
}
var head = document.getElementsByTagName('head')[0];
head.appendChild(s);
}
function SetCookie(key, value) {
document.cookie = key + "=" + encodeURI(value.toString()) + '; path=/; domain=blockchain.info; max-age=' + (60*60*24*365);
}
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) c_end = document.cookie.length;
return decodeURI(document.cookie.substring(c_start, c_end));
}
}
return "";
}
var MyStore = new function() {
this.put = function(key, value) {
try {
localStorage.setItem(key, value);
} catch(e) {
console.log(e);
}
}
this.get = function(key, callback) {
try {
var result = localStorage.getItem(key);
} catch(e) {
console.log(e);
}
callback(result);
}
this.remove = function(key) {
try {
localStorage.removeItem(key);
} catch(e) {
console.log(e);
}
}
this.clear = function() {
try {
localStorage.clear();
} catch(e) {
console.log(e);
}
}
}