]>
Commit | Line | Data |
---|---|---|
1 | (function() { | |
2 | ||
3 | var mnemonic = new Mnemonic("english"); | |
4 | var seed = null | |
5 | var bip32RootKey = null; | |
6 | var bip32ExtendedKey = null; | |
7 | var network = bitcoin.networks.bitcoin; | |
8 | var addressRowTemplate = $("#address-row-template"); | |
9 | ||
10 | var showIndex = true; | |
11 | var showAddress = true; | |
12 | var showPrivKey = true; | |
13 | ||
14 | var phraseChangeTimeoutEvent = null; | |
15 | ||
16 | var DOM = {}; | |
17 | DOM.network = $(".network"); | |
18 | DOM.phraseNetwork = $("#network-phrase"); | |
19 | DOM.phrase = $(".phrase"); | |
20 | DOM.passphrase = $(".passphrase"); | |
21 | DOM.generate = $(".generate"); | |
22 | DOM.seed = $(".seed"); | |
23 | DOM.rootKey = $(".root-key"); | |
24 | DOM.extendedPrivKey = $(".extended-priv-key"); | |
25 | DOM.extendedPubKey = $(".extended-pub-key"); | |
26 | DOM.bip32tab = $("#bip32-tab"); | |
27 | DOM.bip44tab = $("#bip44-tab"); | |
28 | DOM.bip32panel = $("#bip32"); | |
29 | DOM.bip44panel = $("#bip44"); | |
30 | DOM.bip32path = $("#bip32-path"); | |
31 | DOM.bip44path = $("#bip44-path"); | |
32 | DOM.bip44purpose = $("#bip44 .purpose"); | |
33 | DOM.bip44coin = $("#bip44 .coin"); | |
34 | DOM.bip44account = $("#bip44 .account"); | |
35 | DOM.bip44change = $("#bip44 .change"); | |
36 | DOM.strength = $(".strength"); | |
37 | DOM.addresses = $(".addresses"); | |
38 | DOM.rowsToAdd = $(".rows-to-add"); | |
39 | DOM.more = $(".more"); | |
40 | DOM.feedback = $(".feedback"); | |
41 | DOM.tab = $(".derivation-type a"); | |
42 | DOM.indexToggle = $(".index-toggle"); | |
43 | DOM.addressToggle = $(".address-toggle"); | |
44 | DOM.privateKeyToggle = $(".private-key-toggle"); | |
45 | ||
46 | var derivationPath = $(".tab-pane.active .path").val(); | |
47 | ||
48 | function init() { | |
49 | // Events | |
50 | DOM.network.on("change", networkChanged); | |
51 | DOM.phrase.on("input", delayedPhraseChanged); | |
52 | DOM.passphrase.on("input", delayedPhraseChanged); | |
53 | DOM.generate.on("click", generateClicked); | |
54 | DOM.more.on("click", showMore); | |
55 | DOM.bip32path.on("input", bip32Changed); | |
56 | DOM.bip44purpose.on("input", bip44Changed); | |
57 | DOM.bip44coin.on("input", bip44Changed); | |
58 | DOM.bip44account.on("input", bip44Changed); | |
59 | DOM.bip44change.on("input", bip44Changed); | |
60 | DOM.tab.on("click", tabClicked); | |
61 | DOM.indexToggle.on("click", toggleIndexes); | |
62 | DOM.addressToggle.on("click", toggleAddresses); | |
63 | DOM.privateKeyToggle.on("click", togglePrivateKeys); | |
64 | disableForms(); | |
65 | hidePending(); | |
66 | hideValidationError(); | |
67 | populateNetworkSelect(); | |
68 | } | |
69 | ||
70 | // Event handlers | |
71 | ||
72 | function networkChanged(e) { | |
73 | var network = e.target.value; | |
74 | networks[network].onSelect(); | |
75 | delayedPhraseChanged(); | |
76 | } | |
77 | ||
78 | function delayedPhraseChanged() { | |
79 | hideValidationError(); | |
80 | showPending(); | |
81 | if (phraseChangeTimeoutEvent != null) { | |
82 | clearTimeout(phraseChangeTimeoutEvent); | |
83 | } | |
84 | phraseChangeTimeoutEvent = setTimeout(phraseChanged, 400); | |
85 | } | |
86 | ||
87 | function phraseChanged() { | |
88 | showPending(); | |
89 | hideValidationError(); | |
90 | // Get the mnemonic phrase | |
91 | var phrase = DOM.phrase.val(); | |
92 | var passphrase = DOM.passphrase.val(); | |
93 | var errorText = findPhraseErrors(phrase); | |
94 | if (errorText) { | |
95 | showValidationError(errorText); | |
96 | return; | |
97 | } | |
98 | // Get the derivation path | |
99 | var errorText = findDerivationPathErrors(); | |
100 | if (errorText) { | |
101 | showValidationError(errorText); | |
102 | return; | |
103 | } | |
104 | // Calculate and display | |
105 | calcBip32Seed(phrase, passphrase, derivationPath); | |
106 | displayBip32Info(); | |
107 | hidePending(); | |
108 | } | |
109 | ||
110 | function generateClicked() { | |
111 | clearDisplay(); | |
112 | showPending(); | |
113 | setTimeout(function() { | |
114 | var phrase = generateRandomPhrase(); | |
115 | if (!phrase) { | |
116 | return; | |
117 | } | |
118 | phraseChanged(); | |
119 | }, 50); | |
120 | } | |
121 | ||
122 | function tabClicked(e) { | |
123 | var activePath = $(e.target.getAttribute("href") + " .path"); | |
124 | derivationPath = activePath.val(); | |
125 | derivationChanged(); | |
126 | } | |
127 | ||
128 | function derivationChanged() { | |
129 | delayedPhraseChanged(); | |
130 | } | |
131 | ||
132 | function bip32Changed() { | |
133 | derivationPath = DOM.bip32path.val(); | |
134 | derivationChanged(); | |
135 | } | |
136 | ||
137 | function bip44Changed() { | |
138 | setBip44DerivationPath(); | |
139 | derivationChanged(); | |
140 | } | |
141 | ||
142 | function toggleIndexes() { | |
143 | showIndex = !showIndex; | |
144 | $("td.index span").toggleClass("invisible"); | |
145 | } | |
146 | ||
147 | function toggleAddresses() { | |
148 | showAddress = !showAddress; | |
149 | $("td.address span").toggleClass("invisible"); | |
150 | } | |
151 | ||
152 | function togglePrivateKeys() { | |
153 | showPrivKey = !showPrivKey; | |
154 | $("td.privkey span").toggleClass("invisible"); | |
155 | } | |
156 | ||
157 | // Private methods | |
158 | ||
159 | function generateRandomPhrase() { | |
160 | if (!hasStrongRandom()) { | |
161 | var errorText = "This browser does not support strong randomness"; | |
162 | showValidationError(errorText); | |
163 | return; | |
164 | } | |
165 | var numWords = parseInt(DOM.strength.val()); | |
166 | var strength = numWords / 3 * 32; | |
167 | var words = mnemonic.generate(strength); | |
168 | DOM.phrase.val(words); | |
169 | return words; | |
170 | } | |
171 | ||
172 | function calcBip32Seed(phrase, passphrase, path) { | |
173 | seed = mnemonic.toSeed(phrase, passphrase); | |
174 | bip32RootKey = bitcoin.HDNode.fromSeedHex(seed, network); | |
175 | bip32ExtendedKey = bip32RootKey; | |
176 | // Derive the key from the path | |
177 | var pathBits = path.split("/"); | |
178 | for (var i=0; i<pathBits.length; i++) { | |
179 | var bit = pathBits[i]; | |
180 | var index = parseInt(bit); | |
181 | if (isNaN(index)) { | |
182 | continue; | |
183 | } | |
184 | var hardened = bit[bit.length-1] == "'"; | |
185 | if (hardened) { | |
186 | bip32ExtendedKey = bip32ExtendedKey.deriveHardened(index); | |
187 | } | |
188 | else { | |
189 | bip32ExtendedKey = bip32ExtendedKey.derive(index); | |
190 | } | |
191 | } | |
192 | } | |
193 | ||
194 | function showValidationError(errorText) { | |
195 | DOM.feedback | |
196 | .text(errorText) | |
197 | .show(); | |
198 | } | |
199 | ||
200 | function hideValidationError() { | |
201 | DOM.feedback | |
202 | .text("") | |
203 | .hide(); | |
204 | } | |
205 | ||
206 | function findPhraseErrors(phrase) { | |
207 | // TODO make this right | |
208 | // Preprocess the words | |
209 | phrase = mnemonic.normalizeString(phrase); | |
210 | var parts = phrase.split(" "); | |
211 | var proper = []; | |
212 | for (var i=0; i<parts.length; i++) { | |
213 | var part = parts[i]; | |
214 | if (part.length > 0) { | |
215 | // TODO check that lowercasing is always valid to do | |
216 | proper.push(part.toLowerCase()); | |
217 | } | |
218 | } | |
219 | // TODO some levenstein on the words | |
220 | var properPhrase = proper.join(' '); | |
221 | // Check the words are valid | |
222 | var isValid = mnemonic.check(properPhrase); | |
223 | if (!isValid) { | |
224 | return "Invalid mnemonic"; | |
225 | } | |
226 | return false; | |
227 | } | |
228 | ||
229 | function findDerivationPathErrors(path) { | |
230 | // TODO | |
231 | return false; | |
232 | } | |
233 | ||
234 | function displayBip32Info() { | |
235 | // Display the key | |
236 | DOM.seed.val(seed); | |
237 | var rootKey = bip32RootKey.toBase58(); | |
238 | DOM.rootKey.val(rootKey); | |
239 | var extendedPrivKey = bip32ExtendedKey.toBase58(); | |
240 | DOM.extendedPrivKey.val(extendedPrivKey); | |
241 | var extendedPubKey = bip32ExtendedKey.toBase58(false); | |
242 | DOM.extendedPubKey.val(extendedPubKey); | |
243 | // Display the addresses and privkeys | |
244 | clearAddressesList(); | |
245 | displayAddresses(0, 20); | |
246 | } | |
247 | ||
248 | function displayAddresses(start, total) { | |
249 | for (var i=0; i<total; i++) { | |
250 | var index = i + start; | |
251 | new TableRow(index); | |
252 | } | |
253 | } | |
254 | ||
255 | function TableRow(index) { | |
256 | ||
257 | function init() { | |
258 | calculateValues(); | |
259 | } | |
260 | ||
261 | function calculateValues() { | |
262 | setTimeout(function() { | |
263 | var key = bip32ExtendedKey.derive(index); | |
264 | var address = key.getAddress().toString(); | |
265 | var privkey = key.privKey.toWIF(network); | |
266 | addAddressToList(index, address, privkey); | |
267 | }, 50) | |
268 | } | |
269 | ||
270 | init(); | |
271 | ||
272 | } | |
273 | ||
274 | function showMore() { | |
275 | var start = DOM.addresses.children().length; | |
276 | var rowsToAdd = parseInt(DOM.rowsToAdd.val()); | |
277 | if (isNaN(rowsToAdd)) { | |
278 | rowsToAdd = 20; | |
279 | DOM.rowsToAdd.val("20"); | |
280 | } | |
281 | if (rowsToAdd > 200) { | |
282 | var msg = "Generating " + rowsToAdd + " rows could take a while. "; | |
283 | msg += "Do you want to continue?"; | |
284 | if (!confirm(msg)) { | |
285 | return; | |
286 | } | |
287 | } | |
288 | displayAddresses(start, rowsToAdd); | |
289 | } | |
290 | ||
291 | function clearDisplay() { | |
292 | clearAddressesList(); | |
293 | clearKey(); | |
294 | hideValidationError(); | |
295 | } | |
296 | ||
297 | function clearAddressesList() { | |
298 | DOM.addresses.empty(); | |
299 | } | |
300 | ||
301 | function clearKey() { | |
302 | DOM.rootKey.val(""); | |
303 | DOM.extendedPrivKey.val(""); | |
304 | DOM.extendedPubKey.val(""); | |
305 | } | |
306 | ||
307 | function addAddressToList(index, address, privkey) { | |
308 | var row = $(addressRowTemplate.html()); | |
309 | // Elements | |
310 | var indexCell = row.find(".index span"); | |
311 | var addressCell = row.find(".address span"); | |
312 | var privkeyCell = row.find(".privkey span"); | |
313 | // Content | |
314 | var indexText = derivationPath + "/" + index; | |
315 | indexCell.text(indexText); | |
316 | addressCell.text(address); | |
317 | privkeyCell.text(privkey); | |
318 | // Visibility | |
319 | if (!showIndex) { | |
320 | indexCell.addClass("invisible"); | |
321 | } | |
322 | if (!showAddress) { | |
323 | addressCell.addClass("invisible"); | |
324 | } | |
325 | if (!showPrivKey) { | |
326 | privkeyCell.addClass("invisible"); | |
327 | } | |
328 | DOM.addresses.append(row); | |
329 | } | |
330 | ||
331 | function hasStrongRandom() { | |
332 | return 'crypto' in window && window['crypto'] !== null; | |
333 | } | |
334 | ||
335 | function disableForms() { | |
336 | $("form").on("submit", function(e) { | |
337 | e.preventDefault(); | |
338 | }); | |
339 | } | |
340 | ||
341 | function setBip44DerivationPath() { | |
342 | var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44); | |
343 | var coin = parseIntNoNaN(DOM.bip44coin.val(), 0); | |
344 | var account = parseIntNoNaN(DOM.bip44account.val(), 0); | |
345 | var change = parseIntNoNaN(DOM.bip44change.val(), 0); | |
346 | var path = "m/"; | |
347 | path += purpose + "'/"; | |
348 | path += coin + "'/"; | |
349 | path += account + "'/"; | |
350 | path += change; | |
351 | DOM.bip44path.val(path); | |
352 | derivationPath = DOM.bip44path.val(); | |
353 | } | |
354 | ||
355 | function parseIntNoNaN(val, defaultVal) { | |
356 | var v = parseInt(val); | |
357 | if (isNaN(v)) { | |
358 | return defaultVal; | |
359 | } | |
360 | return v; | |
361 | } | |
362 | ||
363 | function showPending() { | |
364 | DOM.feedback | |
365 | .text("Calculating...") | |
366 | .show(); | |
367 | } | |
368 | ||
369 | function hidePending() { | |
370 | DOM.feedback | |
371 | .text("") | |
372 | .hide(); | |
373 | } | |
374 | ||
375 | function populateNetworkSelect() { | |
376 | for (var i=0; i<networks.length; i++) { | |
377 | var network = networks[i]; | |
378 | var option = $("<option>"); | |
379 | option.attr("value", i); | |
380 | option.text(network.name); | |
381 | DOM.phraseNetwork.append(option); | |
382 | } | |
383 | } | |
384 | ||
385 | var networks = [ | |
386 | { | |
387 | name: "Bitcoin", | |
388 | onSelect: function() { | |
389 | network = bitcoin.networks.bitcoin; | |
390 | DOM.bip44coin.val(0); | |
391 | }, | |
392 | }, | |
393 | { | |
394 | name: "Bitcoin Testnet", | |
395 | onSelect: function() { | |
396 | network = bitcoin.networks.testnet; | |
397 | DOM.bip44coin.val(1); | |
398 | }, | |
399 | }, | |
400 | { | |
401 | name: "Litecoin", | |
402 | onSelect: function() { | |
403 | network = bitcoin.networks.litecoin; | |
404 | DOM.bip44coin.val(2); | |
405 | }, | |
406 | }, | |
407 | { | |
408 | name: "Dogecoin", | |
409 | onSelect: function() { | |
410 | network = bitcoin.networks.dogecoin; | |
411 | DOM.bip44coin.val(3); | |
412 | }, | |
413 | }, | |
414 | { | |
415 | name: "ShadowCash", | |
416 | onSelect: function() { | |
417 | network = bitcoin.networks.shadow; | |
418 | DOM.bip44coin.val(35); | |
419 | }, | |
420 | }, | |
421 | { | |
422 | name: "ShadowCash Testnet", | |
423 | onSelect: function() { | |
424 | network = bitcoin.networks.shadowtn; | |
425 | DOM.bip44coin.val(1); | |
426 | }, | |
427 | }, | |
428 | { | |
429 | name: "Viacoin", | |
430 | onSelect: function() { | |
431 | network = bitcoin.networks.viacoin; | |
432 | DOM.bip44coin.val(14); | |
433 | }, | |
434 | }, | |
435 | { | |
436 | name: "Viacoin Testnet", | |
437 | onSelect: function() { | |
438 | network = bitcoin.networks.viacointestnet; | |
439 | DOM.bip44coin.val(1); | |
440 | }, | |
441 | }, | |
442 | { | |
443 | name: "Jumbucks", | |
444 | onSelect: function() { | |
445 | network = bitcoin.networks.jumbucks; | |
446 | DOM.bip44coin.val(26); | |
447 | }, | |
448 | }, | |
449 | { | |
450 | name: "CLAM", | |
451 | onSelect: function() { | |
452 | network = bitcoin.networks.clam; | |
453 | DOM.bip44coin.val(23); | |
454 | }, | |
455 | }, | |
456 | ] | |
457 | ||
458 | init(); | |
459 | ||
460 | })(); |