-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
334 lines (289 loc) · 13.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
<html>
<head lang="en">
<meta charset="utf-8" />
<script src="bower_components/jquery/dist/jquery.min.js"></script>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.min.css">
<script src="bower_components/bitcore/bitcore.min.js"></script>
<style src="bower_components/datatables.net/css/jquery.dataTables.min.css"></style>
<script src="bower_components/datatables.net/js/jquery.datatables.min.js"></script>
<style src="bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css"></style>
<script src="bower_components/datatables.net-bs/js/datatables.bootstrap.min.js"></script>
<title>Key scanner</title>
</head>
<body>
<div class="container">
<h1 class="text-center">Key scanner</h1>
<h5 class="text-center">Warning, this tool is for fairly advanced Bitcoin users</h5>
<hr>
<div class="row">
<div class="col-md-12">
<form class="form-horizontal" role="form">
<div class="col-sm-2"></div>
<div class="col-sm-10"></div>
<div class="form-group">
<label for="coin" class="col-sm-2 control-label">Coin</label>
<div class="col-sm-10">
<select id="coin" class="form-control">
<option value="BTC" selected>Bitcoin</option>
<option value="BCH">Bitcoin Cash</option>
</select>
</div>
</div>
<div class="form-group">
<label for="key" class="col-sm-2 control-label">Public or Private BIP32 Key</label>
<div class="col-sm-10">
<textarea id="key" class="form-control"></textarea>
<p>If you need help determining your key from a recovery phrase go <a href="https://iancoleman.github.io/bip39/">here</a></p>
</div>
</div>
<div class="form-group">
<div class="col-sm-2"></div>
<div class="col-sm-10">Google your wallet's derivation path for help</div>
</div>
<div class="form-group">
<label for="startingPath" class="col-sm-2 control-label">Starting Path</label>
<div class="col-sm-1">
<input id="startingPath" class="form-control" value="m"></input>
<a href="https://sevdev.hu/ipns/sevdev.hu/posts/2016-11-16-working-with-bitcoin-hd-wallets.html" target="_blank">More Info</a>
</div>
<label for="autoChangeAddresses" class="col-sm-2 control-label">Auto Append 0,1</label>
<div class="col-sm-1">
<input id="autoChangeAddresses" class="form-control" type="checkbox" value="checked"></input>
</div>
<label for="startingFrom" class="col-sm-2 control-label">Starting From</label>
<div class="col-sm-1">
<input id="startingFrom" class="form-control" value="0"></input>
</div>
<label for="includeEmpty" class="col-sm-2 control-label">Incl. Unused</label>
<div class="col-sm-1">
<input id="includeEmpty" type="checkbox" class="form-control"></input>
</div>
</div>
<div class="form-group">
<label for="searchSpace" class="col-sm-2 control-label">Address Count</label>
<div class="col-sm-2">
<input id="searchSpace" class="form-control" value="5"></input>
</div>
<label for="searchDepth" class="col-sm-2 control-label">Derivation Depth</label>
<div class="col-sm-2">
<input id="searchDepth" class="form-control" value="1"></input>
</div>
<label for="waitTime" class="col-sm-2 control-label">Delay in ms (be nice)</label>
<div class="col-sm-2">
<input id="waitTime" class="form-control" value="100"></input>
</div>
</div>
<div class="form-group">
<div class="col-sm-2">
<span class="input-group-btn">
</span>
</div>
<div class="col-sm-10">
<span class="input-group-btn">
<button id="btnScan" class="btn" type="button">Scan</button>
</span>
</div>
</div>
</form>
</div>
</div>
<div class="row">
<div class="col-md-12">
<h2>Status</h2>
<p>
<span id="status"></span>
</p>
</div>
</div>
<div class="row">
<div class="col-md-12">
<h2>Total Available Bitcoin</h2>
<p>
<span id="total"></span>
</p>
</div>
</div>
<div class="row">
<div class="col-md-12">
<h2>Addresses</h2>
<p>Note these addreses are derived from the
<strong>BIP32 Extended Key</strong>
</p>
<table id="resultsTable" class="table table-striped">
<thead>
<th>
<div class="input-group">Derivation</div>
</th>
<th>
<div class="input-group">Address</div>
</th>
<th>
<div class="input-group">Balance</div>
</th>
<th>
<div class="input-group">Activity</div>
</th>
<th>
<div class="input-group">Public Key</div>
</th>
<th>
<div class="input-group">Private Key</div>
</th>
</thead>
<tbody id="addresses">
</tbody>
</table>
</div>
</div>
</div>
<script type="text/template" id="address-row-template">
<tr>
<td class="derivation"><span></span></td>
<td class="address"><span></span></td>
<td class="balance"><span></span></td>
<td class="activity"><span></span></td>
<td class="publickey"><span></span></td>
<td class="privkey"><span></span></td>
</tr>
</script>
<script>
var bitcore = require('bitcore');
var HDPrivateKey = bitcore.HDPrivateKey;
var HDPublicKey = bitcore.HDPublicKey;
var DOM = {};
DOM.status = $('#status');
DOM.btnScan = $('#btnScan');
DOM.total = $('#total');
var theTable;
function init() {
DOM.btnScan.on('click', scan);
$(document).ready(function () {
theTable = $('#resultsTable').DataTable({
"lengthChange": false,
"paging": false,
"order": [[2, "desc"]]
});
});
}
//we will use this to build up an array of keys to scan, so then we can synchronously scan them
//contents will have properties .path and .key
var keyList;
function scan() {
//grab all the info from the inputs
DOM.autoChangeAddresses = $('#autoChangeAddresses');
DOM.startingPath = $('#startingPath');
DOM.searchDepth = $('#searchDepth');
DOM.waitTime = parseInt($('#waitTime').val());
DOM.includeEmpty = $('#includeEmpty');
DOM.startingFrom = parseInt($('#startingFrom').val());
DOM.searchSpace = $('#searchSpace');
DOM.key = $('#key');
theTable.clear();
keyList = [];
if (DOM.waitTime < 100) {
DOM.waitTime = 100;
}
var keyTxt = DOM.key.val();
DOM.total.html('0');
var key;
if (keyTxt.startsWith('xpub')) {
key = new HDPublicKey(DOM.key.val());
} else if (keyTxt.startsWith('xprv')) {
key = new HDPrivateKey(DOM.key.val());
}
//checkBalance(key, 'm');
var keysPerPath = DOM.searchSpace.val();
var searchDepth = DOM.searchDepth.val();
var startingPath = DOM.startingPath.val();
findInterestingDerivitives(key.derive(startingPath), startingPath, keysPerPath, searchDepth);
//ok now the keylist is filled, we go through and scan them snchronously
scanLoop();
}
async function findInterestingDerivitives(key, startPath, keysPerPath, searchDepth) {
for (var idx = 0; idx < keysPerPath; idx++) {
if (searchDepth == 1 && autoChangeAddresses.checked) {
//this is the last in the depth and auto change address is checked. we need 2 keys. 1 for 0/N and one for 1/N
//key for 0
var derivedKey = key.derive(0).derive(idx + DOM.startingFrom);
//for reporting purposes, not used in calc
var path = startPath + '/0/' + (idx + DOM.startingFrom);
keyList.push({ "path": path, "key": derivedKey });
//key for 1
var derivedKey = key.derive(1).derive(idx + DOM.startingFrom);
//for reporting purposes, not used in calc
var path = startPath + '/1/' + (idx + DOM.startingFrom);
keyList.push({ "path": path, "key": derivedKey });
} else {
//search depth > 1
var derivedKey = key.derive(idx + DOM.startingFrom);
//for reporting purposes, not used in calc
var path = startPath + '/' + (idx + DOM.startingFrom);
keyList.push({ "path": path, "key": derivedKey });
}
if (searchDepth != 1) {
findInterestingDerivitives(derivedKey, path, keysPerPath, searchDepth - 1);
}
if (idx == keysPerPath - 1) {
//we are at the end of the depth
if (searchDepth == 1) return;
//reset start path and depth for next loop due to some bug
startPath = DOM.startingPath.val()
}
}
}
function scanLoop() {
//grab the next key
var nextItem = keyList.shift();
if (!nextItem) return;
var key = nextItem.key;
var path = nextItem.path;
console.log("checking " + path);
var address = key.publicKey.toAddress();
if ($('#coin').val() == "BTC") {
var qry = 'https://insight.bitpay.com/api/addr/' + address;
//var qry = 'https://blockexplorer.com/api/addr/' + address;
} else {
var qry = 'https://bch-bitcore2.trezor.io/api/addr/' + address;
}
$.ajax({
url: qry,
success: function (data) {
reportOnBalance(key, path, Number(data.balance), Number(data.totalReceived), Number(data.totalSent));
sleep(DOM.waitTime);
scanLoop();
},
error: function (ex) {
console.log("$.get failed - " + ex);
scanLoop();
}
});
}
function reportOnBalance(key, path, bal, r, s) {
if (includeEmpty.checked || bal > 0 || r > 0 || s > 0) {
var addr = key.publicKey.toAddress();
var url;
if ($('#coin').val() == "BTC") {
url = '<a target="_blank" href="https://blockchain.info/address/' + addr + '">' + addr + '</a>';
} else {
url = '<a target="_blank" href="https://bch-bitcore2.trezor.io/address/' + addr + '">' + addr + '</a>';
}
var privKey;
try {
privKey = key.privateKey.toWIF();
} catch (e) {
privKey = "N/A";
}
theTable.row.add([path, url, bal, r + s, key.publicKey, privKey]).draw();
DOM.total.html(Number(DOM.total.html()) + bal);
}
DOM.status.text('Checked ' + path);
}
function sleep(ms) {
//return new Promise(resolve => setTimeout(resolve, ms));
var start = new Date().getTime();
while (new Date().getTime() < start + ms);
}
init();
</script>
</body>
</html>