-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.html
338 lines (300 loc) · 14.4 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>XCP Opreturn Builder for Electrum</title>
<link rel="stylesheet" href="css/main.css">
<script src="db/asset_longnames.js"></script>
<script src="db/assets_div.js"></script>
<script src="db/assets_indiv.js"></script>
<script src="js/xcp/hex.js"></script>
<script src="js/xcp/assets.js"></script>
<script src="js/xcp/build_tx_msg.js"></script>
<script src="js/xcp/decode_tx_msg.js"></script>
<script src="js/xcp/display.js"></script>
<script src="js/xcp/rc4.js"></script>
<script src="js/xcp/validate_address.js"></script>
<script src="js/lib/bitcoinjs-lib.js"></script>
<script src="js/lib/bitcoinjs-message.js"></script>
<script src="js/lib/jquery-3.4.1.min.js"></script>
<script src="js/lib/lozad.min.js"></script>
<script src="js/lib/sha256.js"></script>
<script src="js/lib/btc.js"></script>
<script>
/* testing:
1AwS3wRFNCoymKs69BXjAA4VfgWvuKvx4j too many balances to show all
*/
const TO_RECEIVER = 0.00005420; //BTC dust sent along asset
const forms = [
['Send', 'send.html'],
['Sell (Dispenser)', 'dispenser.html'],
['Sweep', 'sweep.html'],
['Broadcast Text', 'broadcast_text.html'],
['Broadcast File (CIP33)', 'cip33_broadcast.html'],
['Issue Token w/ File (CIP33)', 'cip33_issuance.html']
];
for (let i = 0; i < forms.length; i++) {
forms[i].push('<a href="'+forms[i][1]+'{params}">'+forms[i][0]+'</a>');
}
let address = '';
let num_balances = 0;
let balances = [];
let num_ownerships = 0;
let ownerships = [];
let utxos = [];
function prepare() {
//Get url parameter 'address'
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
if (urlParams.has('address') == false) {
let header = wallet_header
header = header.replace('{address}', addr_short(address));
header = header.replace('{utxo}', '');
let forms_html = '';
for (let i = 0; i < forms.length; i++) {
forms_html += '<br>• '+forms[i][2].replace('{params}','');
}
let tip = '<div style="white-space:pre;">Tip: Set your address as url parameter to view token balances,<br> e.g. <i>index.html?address=1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa</i></div>';
document.getElementById('content').innerHTML = header + '' + forms_html + '<br><br>' + tip;
return;
}
address = urlParams.get('address');
if (valid_btc_addr(address, 'bitcoin') == false) {
document.getElementById('content').innerHTML = 'The provided address `'+address+'` is not a valid Bitcoin address.';
return;
}
if (valid_xcp_addr(address, 'bitcoin') == false) {
document.getElementById('content').innerHTML = 'The provided address `'+address+'` is not supported by Counterparty.';
return;
}
//All good, go ahead
get_api();
}
//API functions rely on 3rd party services
function get_api() {
//pushes values to three arrays:
// balances[] is an array where each balance is an object {asset, longname, displayname, quantity(sats), divisibility}
// ownerships[] is an array where each asset is an object {asset, longname, displayname, description, divisibility}
// utxos[] is an array of [id, sats]
//also assigns values to num_balances and num_ownerships (XCP counts as a balance, BTC does not)
//Notes: First call gives number of balances and ownerships to expect.
// We only load the first page, so in case a very large number not all will show.
// Id and quantity are strings to prevent > max_int issues.
// Quantity is always satoshis. Converting to human readable requires divisibility ('div' or 'indiv').
// Longname applies only to subassets, which both have a numeric name (Axxx) and longname (TOKEN.Sub).
// All other assets have empty ('') longname.
// For ownerships there's not one specific api call, so we use `issuances` to try
// to get current ownerships. This fails to show ownerships that have been transferred to the address.
$.getJSON( "https://xchain.io/api/address/" + address, function( data ) {
num_balances = data.assets.held;
num_ownerships = data.assets.owned;
//balances = [];
//balances.length = num_balances;
//ownerships = [];
//ownerships.length = num_ownerships;
console.log('Xchain api 1/4 loaded. Balances = ' + num_balances + '. Ownerships = ' + num_ownerships + '.');
$.getJSON( "https://xchain.io/api/balances/" + address, function( data ) {
//let xcp_balance = "0"; //error if address not used (assume any error means 0 XCP)
let i = -1;
$.each( data.data, function( key, val ) {
i += 1;
//let balance = {};
let quant = val.quantity;
let divisbility = '';
if (quant.includes('.')) { //divisible
divisbility = 'div';
//sanity check. Xchain always uses 8 decimals for divisible assets
if (quant.split('.')[1].length != 8) {
throw new Error('API unexpected result. Expected 8 decimals. https://xchain.io/api/balances/' + address);
}
quant = quant.replace('.','').replace(/^0+/, ''); //to get quant in sats .. 0.00042000 -> 42000
} else {
divisbility = 'indiv';
}
//Api returns name but not ID for regular assets (get from asset_id() function)
// name and implicitly the ID for numeric assets
// name (longname) and ID for subassets (id is wrong so use numeric name without A)
//Leave blank ID for regular assets
let asset_name = val.asset;
let asset_longname = val.asset_longname;
let asset_diplayname = asset_name;
if (asset_longname != '') {
asset_diplayname = asset_longname;
}
if (asset_name == 'XCP') {
num_balances += 1;
}
//balance.name = asset_name;
//balance.longname = asset_longname;
//balance.divisbility = divisbility;
balances.push({asset: asset_name, longname: asset_longname, displayname: asset_diplayname, quantity: quant, divisibility: divisbility});
});
console.log('Xchain api 2/4 loaded. (' + balances.length + ' token balances)');
let issuances = [];
$.getJSON( "https://xchain.io/api/issuances/" + address, function( data ) {
//issuances are soreted with newest first
// only care about last valid where new`issuer` is the same address
$.each( data.data, function( key, val ) {
if (val.status == 'valid') {
issuances.push([val.asset, val.asset_longname, val.issuer, val.divisible, val.description]);
}
});
i = -1;
for (let j = 0; j < issuances.length; j++) {
let asset_name = issuances[j][0];
let asset_longname = issuances[j][1];
let asset_diplayname = asset_name;
if (asset_longname != '') {
asset_diplayname = asset_longname;
}
let description = issuances[j][4];
//ignore if this is not the last issuance (ie first in array)
let asset_count = 0;
for (let k = 0; k < j; k++) {
if (issuances[k][0] == asset_name) asset_count += 1;
}
if (asset_count > 0) continue;
//ignore if asset was transferred to someone else
if (issuances[j][2] != address) continue;
//conclude address owns this asset
i += 1;
let divisbility = '';
if (issuances[j][3] == true) {
divisbility = 'div';
} else {
divisbility = 'indiv';
}
ownerships.push({asset: asset_name, longname: asset_longname, displayname: asset_diplayname, desciption: description, divisbility: divisbility});
}
console.log('Xchain api 3/4 loaded. (' + ownerships.length + ' asset ownerships)');
displayBalances();
//utxos =[];
$.getJSON( "https://xchain.io/api/utxos/" + address, function( data ) {
$.each( data.data, function( key, val ) {
utxos.push({id: val.txid, quantity: val.value});
});
console.log('Xchin api 4/4 loaded. (' + utxos.length + ' UTXOs)');
displayBalances();
});
});
});
});
}
function displayBalances() {
let html = '';
let url_send = 'asset={asset}&longname={longname}&max={max}&div={div}&address={addr}&utxo={utxo}';
//UTXOs
let utxo = '';
//utxos.sort((a, b) => parseFloat(a.id) - parseFloat(b.id));
utxos.sort((a, b) => a.id.localeCompare(b.id));
let btc_balance = 0;
for (let i = 0; i < utxos.length; i++) {
btc_balance += utxos[i].quantity;
}
if (utxos[0]) utxo = utxos[0].id;
//Display names
for (let i = 0; i < balances.length; i++) {
if (balances[i].longname == '') {
balances[i].displayname = balances[i].asset;
} else {
balances[i].displayname = balances[i].longname;
}
}
//Send links
let xcp_balance = 0;
let send_link_xcp = '';
let send_links = [];
for (let i = 0; i < balances.length; i++) {
if (balances[i].quantity == 0 || utxo == '') {
send_links.push('');
} else {
let send_link = url_send;
send_link = send_link.replace('{asset}', balances[i].asset);
send_link = send_link.replace('{longname}', balances[i].longname);
send_link = send_link.replace('{max}', balances[i].quantity);
send_link = send_link.replace('{div}', balances[i].divisibility);
send_link = send_link.replace('{addr}', address);
send_link = send_link.replace('{utxo}', utxo);
send_links.push('<a href="send.html?'+send_link+'">Send</a> | <a href="dispenser.html?'+send_link+'">Sell</a>');
if (balances[i].asset == 'XCP') {
xcp_balance = balances[i].quantity;
send_link_xcp = '<a href="send.html?'+send_link+'">Send</a> | <a href="dispenser.html?'+send_link+'">Sell</a>';
}
}
}
//Header
html += '<h1>'+address+'</h1>';
html += '<p>Token balances and ownerships.<p>';
let header = wallet_header
header = header.replace('{address}', addr_short(address));
if (utxo == '') {
header = header.replace('{utxo}', '');
} else {
header = header.replace('{utxo}', 'UTXO ' +utxo.slice(0, 4));
}
//Token balances
html += '<table>';
html += '<colgroup><col style="width:30%"><col style="width:40%"><col style="width:30%"></colgroup>';
html += '<tr><th>Tokens</th><th></th><th></th></tr>';
html += '<tr><td>BTC</td><td class="cell_right">'+balance_format(btc_balance, 'div')+'</td><td></td></tr>';
html += '<tr><td>XCP</td><td class="cell_right">'+balance_format(xcp_balance, 'div')+'</td><td class="cell_right">'+send_link_xcp+'</td></tr>';
for (let i = 0; i < balances.length; i++) {
if (balances[i].asset != 'XCP') {
html += '<tr><td>'+balances[i].displayname+'</td><td class="cell_right">'+sat_to_displayx(balances[i].quantity, balances[i].divisibility)+'</td><td class="cell_right">'+send_links[i]+'</td></tr>';
}
}
if (balances.length == num_balances + 1 && xcp_balance > 0) {
html += '<tr><td colspan="3"><i>'+(num_balances+1)+' token balances</i></td></tr>';
} else if (balances.length == num_balances) {
html += '<tr><td colspan="3"><i>'+num_balances+' token balances</i></td></tr>';
} else {
html += '<tr><td colspan="3"><i>Only '+balances.length+' of '+num_balances+' tokens displayed due to api limitations.</i></td></tr>';
}
html += '</table>';
//Asset ownerships
html += '<table>';
html += '<colgroup><col style="width:30%"><col style="width:40%"><col style="width:30%"></colgroup>';
html += '<tr><th>Asset Ownerships</th><th></th><th></th></tr>';
for (let i = 0; i < ownerships.length; i++) {
html += '<tr><td>'+ownerships[i].displayname+'</td><td>'+ownerships[i].desciption+'</td><td class="cell_right">EDIT TRANSFER</td></tr>';
}
if (ownerships.length == num_ownerships) {
html += '<tr><td colspan="3"><i>'+num_ownerships+' asset ownerships</i></td></tr>';
} else {
html += '<tr><td colspan="3"><i>Only '+ownerships.length+' of '+num_ownerships+' asset ownerships displayed due to api limitations.</i></td></tr>';
}
html += '</table>';
//Input coin (utxo)
html += '<table>';
html += '<tr><th>Input Coin (UTXO)</th></tr>';
if (utxo != '') {
html += '<tr><td>'+utxo+'</td></tr>';
} else {
html += '<tr><td>N/A</td></tr>';
}
html += '</table>';
//List of forms
let params = '?address=' + address;
if (utxo != '') {
params += '&utxo=' + utxo;
}
params += '&xcp=' + xcp_balance;
html += '<table>';
html += '<tr><th>Address Actions</th></tr>';
for (let i = 0; i < forms.length; i++) {
html += '<tr><td>• '+forms[i][2].replaceAll('{params}',params)+'</td></tr>';
}
html += '</table>';
//console.log(html);
document.getElementById('content').innerHTML = header + html + wallet_footer;
}
</script>
</head>
<body onload="prepare()">
<div id="content">
<div id="output">
<p>Loading..</p>
</div>
</div>
</body>
</html>