-
Notifications
You must be signed in to change notification settings - Fork 0
/
tool.00.xpub.insight.html
275 lines (240 loc) · 5.79 KB
/
tool.00.xpub.insight.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
<html>
<body>
<script src="js/modules.js"></script>
<script>
var bitcoin = modules.bitcoin;
//var Buffer = modules.buffer.Buffer;
//var Bip38 = modules.bip38;
//var bip38 = new Bip38()
</script>
1) Enter xpub key : <br>
<input id="xpubInput" type="text" style="width:940px"><br>
Or load from file : <input type="file" onchange="fileSelected(this.files)"><br>
<br>
2) Choose server to check for used addresses.
<form id="sourceSelect"></form>
3) How far to search before giving up : <input id="searchDepth" type="text" value="2"><br>
<br>
4) <button id="searchButton" onclick="search()">Search</button> and be patient might take a while. Ctrl-Shift-J (chrome) or Ctrl-Shift-K (firefox) to view the console logs.<br>
<br>
<button id="saveButton" disabled onclick="saveAddressFile()">Save Addresses</button> <a></a>
<table>
<tbody id="addressTable">
<tr><td>Name</td><td>Address</td></tr>
</tbody>
</table>
<script>
var xpubInput = document.getElementById('xpubInput');
var searchDepth = document.getElementById('searchDepth');
var searchButton = document.getElementById('searchButton');
var saveButton = document.getElementById('saveButton');
var addressTable = document.getElementById('addressTable');
var insightSites = [
'blockexplorer.com',
'insight.bitpay.com',
'localbitcoinschain.com',
// 'search.bitaccess.ca',
];
var sourceSelect = document.getElementById('sourceSelect');
sourceSelect.insertAdjacentHTML('beforeend', '<input type="radio" name="source" value="random" checked>random<br>');
for (var i = 0; i < insightSites.length; i++)
{
var site = insightSites[i];
sourceSelect.insertAdjacentHTML('beforeend', '<input type="radio" name="source" value="' + site + '">' + site + '<br>');
}
sourceSelect.insertAdjacentHTML('beforeend', '<input type="radio" name="source" value="custom">private insight server: <input type="text" id="insightURL">');
function fileSelected(files)
{
if (files.length != 1)
{
return;
}
var reader = new FileReader();
reader.onload = function ()
{
var start = reader.result.indexOf('xpub');
var xpub = reader.result.substr(start);
var end = xpub.search('[^123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz]');
xpub = xpub.substr(0,end);
xpubInput.value = xpub;
}
reader.readAsText(files[0]);
}
function generateLink(address)
{
var value = sourceSelect.elements['source'].value
var link = 'https://';
switch (value)
{
case 'random':
link += insightSites[Math.trunc(Math.random()*insightSites.length)];
break;
case 'custom':
link += document.getElementById('insightURL').value;
break;
default:
link += value;
}
link += '/api/addr/' + address +'/totalReceived';
return link;
}
function trGetCol(tr, index)
{
while (index >= tr.children.length)
{
var td = document.createElement('td');
tr.appendChild(td);
}
return tr.children[index];
}
function bindAddressToTable(address)
{
var tr = document.createElement('tr');
trGetCol(tr, 0).textContent = address.name;
trGetCol(tr, 1).textContent = address.address;
addressTable.appendChild(tr);
}
var addresses = []
var aSave = document.querySelector('a');
aSave.download = 'addresses.json';
function saveAddressFile()
{
aSave.textContent = 'addresses.json';
//aSave.href = 'data:text/json;charset=UTF-8,' + encodeURIComponent(JSON.stringify(addresses));
aSave.href = 'data:application/octet-stream,' + encodeURIComponent(JSON.stringify(addresses));
aSave.click();
}
function onDone()
{
saveButton.disabled = false;
for (var i = 0; i < addresses.length; i++)
{
bindAddressToTable(addresses[i]);
}
}
var curAddress;
var curLink;
var curPath;
var curPathStr;
var curDepth;
var maxDepth;
function onAddressLoad()
{
//console.log('onAddressLoad');
var value = parseInt(this.responseText);
if (isNaN(value))
{
alert('bad value returned from link "' + curLink + '"');
return;
}
if (value > 0)
{
addresses.push({
name : curPathStr,
address : curAddress,
});
nextSearch(true);
return;
}
nextSearch(false);
}
function checkAddress()
{
//console.log('checkAddress');
// debug logging
curPathStr = ''
var next = curPath;
while (next.parent)
{
curPathStr = '/' + next.index.toString() + curPathStr;
next = next.parent;
}
curPathStr = 'm' + curPathStr;
//
curAddress = curPath.node.getAddress();
curLink = generateLink(curAddress);
var req = new XMLHttpRequest();
req.open( "GET", curLink, true );
req.onload = onAddressLoad;
req.send();
}
function nextSearch(success)
{
console.log(curPathStr + (success? ' success' : 'failed'));
if (success)
{
// check sibling
curPath.index++;
curPath.node = curPath.parent.node.derive(curPath.index);
curPath.success = true;
}
else if (curPath.index < maxDepth && curDepth < maxDepth)
{
// check children
var nextPath = {
index : 0,
node : curPath.node.derive(0),
success : false,
parent : curPath,
}
curPath = nextPath;
curDepth++;
}
else
{
curPath = curPath.parent;
if (!curPath.parent)
{
onDone();
return;
}
curDepth--;
curPath.index++;
if (curPath.index > maxDepth)
{
onDone();
return;
}
curPath.node = curPath.parent.node.derive(curPath.index);
}
checkAddress();
}
function search()
{
console.log('search');
maxDepth = parseInt(searchDepth.value);
if (isNaN(maxDepth))
{
alert('Search depth value is not a number! with : ' + searchDepth.value);
return;
}
if (maxDepth < 1)
{
alert('Search depth value is less than 1! with : ' + searchDepth.value);
return;
}
var xpub = xpubInput.value;
try {
var node = new bitcoin.HDNode.fromBase58(xpub);
}
catch (e)
{
alert('Failed to parse xpub. Error : ' + e.message);
return;
}
addresses = []
curPathStr = 'm/0'; // debug
curDepth = 0;
curPath = {
index : 0,
node : node.derive(0),
success : false,
parent : {
node : node,
}
}
checkAddress();
}
</script>
</body>
</html>