Remove Monkey network, test was failing.
[perso/Immae/Projets/Cryptomonnaies/BIP39.git] / src / js / index.js
1 (function() {
2
3 // mnemonics is populated as required by getLanguage
4 var mnemonics = { "english": new Mnemonic("english") };
5 var mnemonic = mnemonics["english"];
6 var seed = null;
7 var bip32RootKey = null;
8 var bip32ExtendedKey = null;
9 var network = bitcoinjs.bitcoin.networks.bitcoin;
10 var addressRowTemplate = $("#address-row-template");
11
12 var showIndex = true;
13 var showAddress = true;
14 var showPubKey = true;
15 var showPrivKey = true;
16 var showQr = false;
17 var litecoinUseLtub = true;
18
19 var entropyChangeTimeoutEvent = null;
20 var phraseChangeTimeoutEvent = null;
21 var rootKeyChangedTimeoutEvent = null;
22
23 var generationProcesses = [];
24
25 var DOM = {};
26 DOM.privacyScreenToggle = $(".privacy-screen-toggle");
27 DOM.network = $(".network");
28 DOM.bip32Client = $("#bip32-client");
29 DOM.phraseNetwork = $("#network-phrase");
30 DOM.useEntropy = $(".use-entropy");
31 DOM.entropyContainer = $(".entropy-container");
32 DOM.entropy = $(".entropy");
33 DOM.entropyFiltered = DOM.entropyContainer.find(".filtered");
34 DOM.entropyType = DOM.entropyContainer.find(".type");
35 DOM.entropyCrackTime = DOM.entropyContainer.find(".crack-time");
36 DOM.entropyEventCount = DOM.entropyContainer.find(".event-count");
37 DOM.entropyBits = DOM.entropyContainer.find(".bits");
38 DOM.entropyBitsPerEvent = DOM.entropyContainer.find(".bits-per-event");
39 DOM.entropyWordCount = DOM.entropyContainer.find(".word-count");
40 DOM.entropyBinary = DOM.entropyContainer.find(".binary");
41 DOM.entropyWordIndexes = DOM.entropyContainer.find(".word-indexes");
42 DOM.entropyChecksum = DOM.entropyContainer.find(".checksum");
43 DOM.entropyMnemonicLength = DOM.entropyContainer.find(".mnemonic-length");
44 DOM.entropyWeakEntropyOverrideWarning = DOM.entropyContainer.find(".weak-entropy-override-warning");
45 DOM.entropyFilterWarning = DOM.entropyContainer.find(".filter-warning");
46 DOM.phrase = $(".phrase");
47 DOM.passphrase = $(".passphrase");
48 DOM.generateContainer = $(".generate-container");
49 DOM.generate = $(".generate");
50 DOM.seed = $(".seed");
51 DOM.rootKey = $(".root-key");
52 DOM.litecoinLtubContainer = $(".litecoin-ltub-container");
53 DOM.litecoinUseLtub = $(".litecoin-use-ltub");
54 DOM.extendedPrivKey = $(".extended-priv-key");
55 DOM.extendedPubKey = $(".extended-pub-key");
56 DOM.bip32tab = $("#bip32-tab");
57 DOM.bip44tab = $("#bip44-tab");
58 DOM.bip49tab = $("#bip49-tab");
59 DOM.bip84tab = $("#bip84-tab");
60 DOM.bip141tab = $("#bip141-tab");
61 DOM.bip32panel = $("#bip32");
62 DOM.bip44panel = $("#bip44");
63 DOM.bip49panel = $("#bip49");
64 DOM.bip32path = $("#bip32-path");
65 DOM.bip44path = $("#bip44-path");
66 DOM.bip44purpose = $("#bip44 .purpose");
67 DOM.bip44coin = $("#bip44 .coin");
68 DOM.bip44account = $("#bip44 .account");
69 DOM.bip44accountXprv = $("#bip44 .account-xprv");
70 DOM.bip44accountXpub = $("#bip44 .account-xpub");
71 DOM.bip44change = $("#bip44 .change");
72 DOM.bip49unavailable = $("#bip49 .unavailable");
73 DOM.bip49available = $("#bip49 .available");
74 DOM.bip49path = $("#bip49-path");
75 DOM.bip49purpose = $("#bip49 .purpose");
76 DOM.bip49coin = $("#bip49 .coin");
77 DOM.bip49account = $("#bip49 .account");
78 DOM.bip49accountXprv = $("#bip49 .account-xprv");
79 DOM.bip49accountXpub = $("#bip49 .account-xpub");
80 DOM.bip49change = $("#bip49 .change");
81 DOM.bip84unavailable = $("#bip84 .unavailable");
82 DOM.bip84available = $("#bip84 .available");
83 DOM.bip84path = $("#bip84-path");
84 DOM.bip84purpose = $("#bip84 .purpose");
85 DOM.bip84coin = $("#bip84 .coin");
86 DOM.bip84account = $("#bip84 .account");
87 DOM.bip84accountXprv = $("#bip84 .account-xprv");
88 DOM.bip84accountXpub = $("#bip84 .account-xpub");
89 DOM.bip84change = $("#bip84 .change");
90 DOM.bip141unavailable = $("#bip141 .unavailable");
91 DOM.bip141available = $("#bip141 .available");
92 DOM.bip141path = $("#bip141-path");
93 DOM.bip141semantics = $(".bip141-semantics");
94 DOM.generatedStrength = $(".generate-container .strength");
95 DOM.generatedStrengthWarning = $(".generate-container .warning");
96 DOM.hardenedAddresses = $(".hardened-addresses");
97 DOM.bitcoinCashAddressTypeContainer = $(".bch-addr-type-container");
98 DOM.bitcoinCashAddressType = $("[name=bch-addr-type]")
99 DOM.useBip38 = $(".use-bip38");
100 DOM.bip38Password = $(".bip38-password");
101 DOM.addresses = $(".addresses");
102 DOM.csvTab = $("#csv-tab a");
103 DOM.csv = $(".csv");
104 DOM.rowsToAdd = $(".rows-to-add");
105 DOM.more = $(".more");
106 DOM.moreRowsStartIndex = $(".more-rows-start-index");
107 DOM.feedback = $(".feedback");
108 DOM.tab = $(".derivation-type a");
109 DOM.indexToggle = $(".index-toggle");
110 DOM.addressToggle = $(".address-toggle");
111 DOM.publicKeyToggle = $(".public-key-toggle");
112 DOM.privateKeyToggle = $(".private-key-toggle");
113 DOM.languages = $(".languages a");
114 DOM.qrContainer = $(".qr-container");
115 DOM.qrHider = DOM.qrContainer.find(".qr-hider");
116 DOM.qrImage = DOM.qrContainer.find(".qr-image");
117 DOM.qrHint = DOM.qrContainer.find(".qr-hint");
118 DOM.showQrEls = $("[data-show-qr]");
119
120 function init() {
121 // Events
122 DOM.privacyScreenToggle.on("change", privacyScreenToggled);
123 DOM.generatedStrength.on("change", generatedStrengthChanged);
124 DOM.network.on("change", networkChanged);
125 DOM.bip32Client.on("change", bip32ClientChanged);
126 DOM.useEntropy.on("change", setEntropyVisibility);
127 DOM.entropy.on("input", delayedEntropyChanged);
128 DOM.entropyMnemonicLength.on("change", entropyChanged);
129 DOM.phrase.on("input", delayedPhraseChanged);
130 DOM.passphrase.on("input", delayedPhraseChanged);
131 DOM.generate.on("click", generateClicked);
132 DOM.more.on("click", showMore);
133 DOM.rootKey.on("input", delayedRootKeyChanged);
134 DOM.litecoinUseLtub.on("change", litecoinUseLtubChanged);
135 DOM.bip32path.on("input", calcForDerivationPath);
136 DOM.bip44account.on("input", calcForDerivationPath);
137 DOM.bip44change.on("input", calcForDerivationPath);
138 DOM.bip49account.on("input", calcForDerivationPath);
139 DOM.bip49change.on("input", calcForDerivationPath);
140 DOM.bip84account.on("input", calcForDerivationPath);
141 DOM.bip84change.on("input", calcForDerivationPath);
142 DOM.bip141path.on("input", calcForDerivationPath);
143 DOM.bip141semantics.on("change", tabChanged);
144 DOM.tab.on("shown.bs.tab", tabChanged);
145 DOM.hardenedAddresses.on("change", calcForDerivationPath);
146 DOM.useBip38.on("change", calcForDerivationPath);
147 DOM.bip38Password.on("change", calcForDerivationPath);
148 DOM.indexToggle.on("click", toggleIndexes);
149 DOM.addressToggle.on("click", toggleAddresses);
150 DOM.publicKeyToggle.on("click", togglePublicKeys);
151 DOM.privateKeyToggle.on("click", togglePrivateKeys);
152 DOM.csvTab.on("click", updateCsv);
153 DOM.languages.on("click", languageChanged);
154 DOM.bitcoinCashAddressType.on("change", bitcoinCashAddressTypeChange);
155 setQrEvents(DOM.showQrEls);
156 disableForms();
157 hidePending();
158 hideValidationError();
159 populateNetworkSelect();
160 populateClientSelect();
161 }
162
163 // Event handlers
164
165 function generatedStrengthChanged() {
166 var strength = parseInt(DOM.generatedStrength.val());
167 if (strength < 12) {
168 DOM.generatedStrengthWarning.removeClass("hidden");
169 }
170 else {
171 DOM.generatedStrengthWarning.addClass("hidden");
172 }
173 }
174
175 function networkChanged(e) {
176 clearDerivedKeys();
177 clearAddressesList();
178 DOM.litecoinLtubContainer.addClass("hidden");
179 DOM.bitcoinCashAddressTypeContainer.addClass("hidden");
180 var networkIndex = e.target.value;
181 var network = networks[networkIndex];
182 network.onSelect();
183 adjustNetworkForSegwit();
184 if (seed != null) {
185 phraseChanged();
186 }
187 else {
188 rootKeyChanged();
189 }
190 }
191
192 function bip32ClientChanged(e) {
193 var clientIndex = DOM.bip32Client.val();
194 if (clientIndex == "custom") {
195 DOM.bip32path.prop("readonly", false);
196 }
197 else {
198 DOM.bip32path.prop("readonly", true);
199 clients[clientIndex].onSelect();
200 if (seed != null) {
201 phraseChanged();
202 }
203 else {
204 rootKeyChanged();
205 }
206 }
207 }
208
209 function setEntropyVisibility() {
210 if (isUsingOwnEntropy()) {
211 DOM.entropyContainer.removeClass("hidden");
212 DOM.generateContainer.addClass("hidden");
213 DOM.phrase.prop("readonly", true);
214 DOM.entropy.focus();
215 entropyChanged();
216 }
217 else {
218 DOM.entropyContainer.addClass("hidden");
219 DOM.generateContainer.removeClass("hidden");
220 DOM.phrase.prop("readonly", false);
221 hidePending();
222 }
223 }
224
225 function delayedPhraseChanged() {
226 hideValidationError();
227 seed = null;
228 bip32RootKey = null;
229 bip32ExtendedKey = null;
230 clearAddressesList();
231 showPending();
232 if (phraseChangeTimeoutEvent != null) {
233 clearTimeout(phraseChangeTimeoutEvent);
234 }
235 phraseChangeTimeoutEvent = setTimeout(phraseChanged, 400);
236 }
237
238 function phraseChanged() {
239 showPending();
240 setMnemonicLanguage();
241 // Get the mnemonic phrase
242 var phrase = DOM.phrase.val();
243 var errorText = findPhraseErrors(phrase);
244 if (errorText) {
245 showValidationError(errorText);
246 return;
247 }
248 // Calculate and display
249 var passphrase = DOM.passphrase.val();
250 calcBip32RootKeyFromSeed(phrase, passphrase);
251 calcForDerivationPath();
252 // Show the word indexes
253 showWordIndexes();
254 }
255
256 function tabChanged() {
257 showPending();
258 adjustNetworkForSegwit();
259 var phrase = DOM.phrase.val();
260 if (phrase != "") {
261 // Calculate and display for mnemonic
262 var errorText = findPhraseErrors(phrase);
263 if (errorText) {
264 showValidationError(errorText);
265 return;
266 }
267 // Calculate and display
268 var passphrase = DOM.passphrase.val();
269 calcBip32RootKeyFromSeed(phrase, passphrase);
270 }
271 else {
272 // Calculate and display for root key
273 var rootKeyBase58 = DOM.rootKey.val();
274 var errorText = validateRootKey(rootKeyBase58);
275 if (errorText) {
276 showValidationError(errorText);
277 return;
278 }
279 // Calculate and display
280 calcBip32RootKeyFromBase58(rootKeyBase58);
281 }
282 calcForDerivationPath();
283 }
284
285 function delayedEntropyChanged() {
286 hideValidationError();
287 showPending();
288 if (entropyChangeTimeoutEvent != null) {
289 clearTimeout(entropyChangeTimeoutEvent);
290 }
291 entropyChangeTimeoutEvent = setTimeout(entropyChanged, 400);
292 }
293
294 function entropyChanged() {
295 // If blank entropy, clear mnemonic, addresses, errors
296 if (DOM.entropy.val().trim().length == 0) {
297 clearDisplay();
298 clearEntropyFeedback();
299 DOM.phrase.val("");
300 showValidationError("Blank entropy");
301 return;
302 }
303 // Get the current phrase to detect changes
304 var phrase = DOM.phrase.val();
305 // Set the phrase from the entropy
306 setMnemonicFromEntropy();
307 // Recalc addresses if the phrase has changed
308 var newPhrase = DOM.phrase.val();
309 if (newPhrase != phrase) {
310 if (newPhrase.length == 0) {
311 clearDisplay();
312 }
313 else {
314 phraseChanged();
315 }
316 }
317 else {
318 hidePending();
319 }
320 }
321
322 function delayedRootKeyChanged() {
323 // Warn if there is an existing mnemonic or passphrase.
324 if (DOM.phrase.val().length > 0 || DOM.passphrase.val().length > 0) {
325 if (!confirm("This will clear existing mnemonic and passphrase")) {
326 DOM.rootKey.val(bip32RootKey);
327 return
328 }
329 }
330 hideValidationError();
331 showPending();
332 // Clear existing mnemonic and passphrase
333 DOM.phrase.val("");
334 DOM.passphrase.val("");
335 seed = null;
336 if (rootKeyChangedTimeoutEvent != null) {
337 clearTimeout(rootKeyChangedTimeoutEvent);
338 }
339 rootKeyChangedTimeoutEvent = setTimeout(rootKeyChanged, 400);
340 }
341
342 function rootKeyChanged() {
343 showPending();
344 hideValidationError();
345 var rootKeyBase58 = DOM.rootKey.val();
346 var errorText = validateRootKey(rootKeyBase58);
347 if (errorText) {
348 showValidationError(errorText);
349 return;
350 }
351 // Calculate and display
352 calcBip32RootKeyFromBase58(rootKeyBase58);
353 calcForDerivationPath();
354 }
355
356 function litecoinUseLtubChanged() {
357 litecoinUseLtub = DOM.litecoinUseLtub.prop("checked");
358 if (litecoinUseLtub) {
359 network = bitcoinjs.bitcoin.networks.litecoin;
360 }
361 else {
362 network = bitcoinjs.bitcoin.networks.litecoinXprv;
363 }
364 phraseChanged();
365 }
366
367 function calcForDerivationPath() {
368 clearDerivedKeys();
369 clearAddressesList();
370 showPending();
371 // Don't show segwit if it's selected but network doesn't support it
372 if (segwitSelected() && !networkHasSegwit()) {
373 showSegwitUnavailable();
374 hidePending();
375 return;
376 }
377 showSegwitAvailable();
378 // Get the derivation path
379 var derivationPath = getDerivationPath();
380 var errorText = findDerivationPathErrors(derivationPath);
381 if (errorText) {
382 showValidationError(errorText);
383 return;
384 }
385 bip32ExtendedKey = calcBip32ExtendedKey(derivationPath);
386 if (bip44TabSelected()) {
387 displayBip44Info();
388 }
389 else if (bip49TabSelected()) {
390 displayBip49Info();
391 }
392 else if (bip84TabSelected()) {
393 displayBip84Info();
394 }
395 displayBip32Info();
396 }
397
398 function generateClicked() {
399 if (isUsingOwnEntropy()) {
400 return;
401 }
402 clearDisplay();
403 showPending();
404 setTimeout(function() {
405 setMnemonicLanguage();
406 var phrase = generateRandomPhrase();
407 if (!phrase) {
408 return;
409 }
410 phraseChanged();
411 }, 50);
412 }
413
414 function languageChanged() {
415 setTimeout(function() {
416 setMnemonicLanguage();
417 if (DOM.phrase.val().length > 0) {
418 var newPhrase = convertPhraseToNewLanguage();
419 DOM.phrase.val(newPhrase);
420 phraseChanged();
421 }
422 else {
423 DOM.generate.trigger("click");
424 }
425 }, 50);
426 }
427
428 function bitcoinCashAddressTypeChange() {
429 phraseChanged();
430 }
431
432 function toggleIndexes() {
433 showIndex = !showIndex;
434 $("td.index span").toggleClass("invisible");
435 }
436
437 function toggleAddresses() {
438 showAddress = !showAddress;
439 $("td.address span").toggleClass("invisible");
440 }
441
442 function togglePublicKeys() {
443 showPubKey = !showPubKey;
444 $("td.pubkey span").toggleClass("invisible");
445 }
446
447 function togglePrivateKeys() {
448 showPrivKey = !showPrivKey;
449 $("td.privkey span").toggleClass("invisible");
450 }
451
452 function privacyScreenToggled() {
453 // private-data contains elements added to DOM at runtime
454 // so catch all by adding visual privacy class to the root of the DOM
455 if (DOM.privacyScreenToggle.prop("checked")) {
456 $("body").addClass("visual-privacy");
457 }
458 else {
459 $("body").removeClass("visual-privacy");
460 }
461 }
462
463 // Private methods
464
465 function generateRandomPhrase() {
466 if (!hasStrongRandom()) {
467 var errorText = "This browser does not support strong randomness";
468 showValidationError(errorText);
469 return;
470 }
471 // get the amount of entropy to use
472 var numWords = parseInt(DOM.generatedStrength.val());
473 var strength = numWords / 3 * 32;
474 var buffer = new Uint8Array(strength / 8);
475 // create secure entropy
476 var data = crypto.getRandomValues(buffer);
477 // show the words
478 var words = mnemonic.toMnemonic(data);
479 DOM.phrase.val(words);
480 // show the entropy
481 var entropyHex = uint8ArrayToHex(data);
482 DOM.entropy.val(entropyHex);
483 // ensure entropy fields are consistent with what is being displayed
484 DOM.entropyMnemonicLength.val("raw");
485 return words;
486 }
487
488 function calcBip32RootKeyFromSeed(phrase, passphrase) {
489 seed = mnemonic.toSeed(phrase, passphrase);
490 bip32RootKey = bitcoinjs.bitcoin.HDNode.fromSeedHex(seed, network);
491 }
492
493 function calcBip32RootKeyFromBase58(rootKeyBase58) {
494 // try parsing with various segwit network params since this extended
495 // key may be from any one of them.
496 if (networkHasSegwit()) {
497 var n = network;
498 if ("baseNetwork" in n) {
499 n = bitcoinjs.bitcoin.networks[n.baseNetwork];
500 }
501 // try parsing using base network params
502 try {
503 bip32RootKey = bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n);
504 return;
505 }
506 catch (e) {}
507 // try parsing using p2wpkh params
508 if ("p2wpkh" in n) {
509 try {
510 bip32RootKey = bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n.p2wpkh);
511 return;
512 }
513 catch (e) {}
514 }
515 // try parsing using p2wpkh-in-p2sh network params
516 if ("p2wpkhInP2sh" in n) {
517 try {
518 bip32RootKey = bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n.p2wpkhInP2sh);
519 return;
520 }
521 catch (e) {}
522 }
523 }
524 // try the network params as currently specified
525 bip32RootKey = bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, network);
526 }
527
528 function calcBip32ExtendedKey(path) {
529 // Check there's a root key to derive from
530 if (!bip32RootKey) {
531 return bip32RootKey;
532 }
533 var extendedKey = bip32RootKey;
534 // Derive the key from the path
535 var pathBits = path.split("/");
536 for (var i=0; i<pathBits.length; i++) {
537 var bit = pathBits[i];
538 var index = parseInt(bit);
539 if (isNaN(index)) {
540 continue;
541 }
542 var hardened = bit[bit.length-1] == "'";
543 var isPriv = !(extendedKey.isNeutered());
544 var invalidDerivationPath = hardened && !isPriv;
545 if (invalidDerivationPath) {
546 extendedKey = null;
547 }
548 else if (hardened) {
549 extendedKey = extendedKey.deriveHardened(index);
550 }
551 else {
552 extendedKey = extendedKey.derive(index);
553 }
554 }
555 return extendedKey
556 }
557
558 function showValidationError(errorText) {
559 DOM.feedback
560 .text(errorText)
561 .show();
562 }
563
564 function hideValidationError() {
565 DOM.feedback
566 .text("")
567 .hide();
568 }
569
570 function findPhraseErrors(phrase) {
571 // Preprocess the words
572 phrase = mnemonic.normalizeString(phrase);
573 var words = phraseToWordArray(phrase);
574 // Detect blank phrase
575 if (words.length == 0) {
576 return "Blank mnemonic";
577 }
578 // Check each word
579 for (var i=0; i<words.length; i++) {
580 var word = words[i];
581 var language = getLanguage();
582 if (WORDLISTS[language].indexOf(word) == -1) {
583 console.log("Finding closest match to " + word);
584 var nearestWord = findNearestWord(word);
585 return word + " not in wordlist, did you mean " + nearestWord + "?";
586 }
587 }
588 // Check the words are valid
589 var properPhrase = wordArrayToPhrase(words);
590 var isValid = mnemonic.check(properPhrase);
591 if (!isValid) {
592 return "Invalid mnemonic";
593 }
594 return false;
595 }
596
597 function validateRootKey(rootKeyBase58) {
598 // try various segwit network params since this extended key may be from
599 // any one of them.
600 if (networkHasSegwit()) {
601 var n = network;
602 if ("baseNetwork" in n) {
603 n = bitcoinjs.bitcoin.networks[n.baseNetwork];
604 }
605 // try parsing using base network params
606 try {
607 bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n);
608 return "";
609 }
610 catch (e) {}
611 // try parsing using p2wpkh params
612 if ("p2wpkh" in n) {
613 try {
614 bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n.p2wpkh);
615 return "";
616 }
617 catch (e) {}
618 }
619 // try parsing using p2wpkh-in-p2sh network params
620 if ("p2wpkhInP2sh" in n) {
621 try {
622 bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n.p2wpkhInP2sh);
623 return "";
624 }
625 catch (e) {}
626 }
627 }
628 // try the network params as currently specified
629 try {
630 bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, network);
631 }
632 catch (e) {
633 return "Invalid root key";
634 }
635 return "";
636 }
637
638 function getDerivationPath() {
639 if (bip44TabSelected()) {
640 var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44);
641 var coin = parseIntNoNaN(DOM.bip44coin.val(), 0);
642 var account = parseIntNoNaN(DOM.bip44account.val(), 0);
643 var change = parseIntNoNaN(DOM.bip44change.val(), 0);
644 var path = "m/";
645 path += purpose + "'/";
646 path += coin + "'/";
647 path += account + "'/";
648 path += change;
649 DOM.bip44path.val(path);
650 var derivationPath = DOM.bip44path.val();
651 console.log("Using derivation path from BIP44 tab: " + derivationPath);
652 return derivationPath;
653 }
654 else if (bip49TabSelected()) {
655 var purpose = parseIntNoNaN(DOM.bip49purpose.val(), 49);
656 var coin = parseIntNoNaN(DOM.bip49coin.val(), 0);
657 var account = parseIntNoNaN(DOM.bip49account.val(), 0);
658 var change = parseIntNoNaN(DOM.bip49change.val(), 0);
659 var path = "m/";
660 path += purpose + "'/";
661 path += coin + "'/";
662 path += account + "'/";
663 path += change;
664 DOM.bip49path.val(path);
665 var derivationPath = DOM.bip49path.val();
666 console.log("Using derivation path from BIP49 tab: " + derivationPath);
667 return derivationPath;
668 }
669 else if (bip84TabSelected()) {
670 var purpose = parseIntNoNaN(DOM.bip84purpose.val(), 84);
671 var coin = parseIntNoNaN(DOM.bip84coin.val(), 0);
672 var account = parseIntNoNaN(DOM.bip84account.val(), 0);
673 var change = parseIntNoNaN(DOM.bip84change.val(), 0);
674 var path = "m/";
675 path += purpose + "'/";
676 path += coin + "'/";
677 path += account + "'/";
678 path += change;
679 DOM.bip84path.val(path);
680 var derivationPath = DOM.bip84path.val();
681 console.log("Using derivation path from BIP84 tab: " + derivationPath);
682 return derivationPath;
683 }
684 else if (bip32TabSelected()) {
685 var derivationPath = DOM.bip32path.val();
686 console.log("Using derivation path from BIP32 tab: " + derivationPath);
687 return derivationPath;
688 }
689 else if (bip141TabSelected()) {
690 var derivationPath = DOM.bip141path.val();
691 console.log("Using derivation path from BIP141 tab: " + derivationPath);
692 return derivationPath;
693 }
694 else {
695 console.log("Unknown derivation path");
696 }
697 }
698
699 function findDerivationPathErrors(path) {
700 // TODO is not perfect but is better than nothing
701 // Inspired by
702 // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#test-vectors
703 // and
704 // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#extended-keys
705 var maxDepth = 255; // TODO verify this!!
706 var maxIndexValue = Math.pow(2, 31); // TODO verify this!!
707 if (path[0] != "m") {
708 return "First character must be 'm'";
709 }
710 if (path.length > 1) {
711 if (path[1] != "/") {
712 return "Separator must be '/'";
713 }
714 var indexes = path.split("/");
715 if (indexes.length > maxDepth) {
716 return "Derivation depth is " + indexes.length + ", must be less than " + maxDepth;
717 }
718 for (var depth = 1; depth<indexes.length; depth++) {
719 var index = indexes[depth];
720 var invalidChars = index.replace(/^[0-9]+'?$/g, "")
721 if (invalidChars.length > 0) {
722 return "Invalid characters " + invalidChars + " found at depth " + depth;
723 }
724 var indexValue = parseInt(index.replace("'", ""));
725 if (isNaN(depth)) {
726 return "Invalid number at depth " + depth;
727 }
728 if (indexValue > maxIndexValue) {
729 return "Value of " + indexValue + " at depth " + depth + " must be less than " + maxIndexValue;
730 }
731 }
732 }
733 // Check root key exists or else derivation path is useless!
734 if (!bip32RootKey) {
735 return "No root key";
736 }
737 // Check no hardened derivation path when using xpub keys
738 var hardenedPath = path.indexOf("'") > -1;
739 var hardenedAddresses = bip32TabSelected() && DOM.hardenedAddresses.prop("checked");
740 var hardened = hardenedPath || hardenedAddresses;
741 var isXpubkey = bip32RootKey.isNeutered();
742 if (hardened && isXpubkey) {
743 return "Hardened derivation path is invalid with xpub key";
744 }
745 return false;
746 }
747
748 function displayBip44Info() {
749 // Get the derivation path for the account
750 var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44);
751 var coin = parseIntNoNaN(DOM.bip44coin.val(), 0);
752 var account = parseIntNoNaN(DOM.bip44account.val(), 0);
753 var path = "m/";
754 path += purpose + "'/";
755 path += coin + "'/";
756 path += account + "'/";
757 // Calculate the account extended keys
758 var accountExtendedKey = calcBip32ExtendedKey(path);
759 var accountXprv = accountExtendedKey.toBase58();
760 var accountXpub = accountExtendedKey.neutered().toBase58();
761 // Display the extended keys
762 DOM.bip44accountXprv.val(accountXprv);
763 DOM.bip44accountXpub.val(accountXpub);
764 }
765
766 function displayBip49Info() {
767 // Get the derivation path for the account
768 var purpose = parseIntNoNaN(DOM.bip49purpose.val(), 49);
769 var coin = parseIntNoNaN(DOM.bip49coin.val(), 0);
770 var account = parseIntNoNaN(DOM.bip49account.val(), 0);
771 var path = "m/";
772 path += purpose + "'/";
773 path += coin + "'/";
774 path += account + "'/";
775 // Calculate the account extended keys
776 var accountExtendedKey = calcBip32ExtendedKey(path);
777 var accountXprv = accountExtendedKey.toBase58();
778 var accountXpub = accountExtendedKey.neutered().toBase58();
779 // Display the extended keys
780 DOM.bip49accountXprv.val(accountXprv);
781 DOM.bip49accountXpub.val(accountXpub);
782 }
783
784 function displayBip84Info() {
785 // Get the derivation path for the account
786 var purpose = parseIntNoNaN(DOM.bip84purpose.val(), 84);
787 var coin = parseIntNoNaN(DOM.bip84coin.val(), 0);
788 var account = parseIntNoNaN(DOM.bip84account.val(), 0);
789 var path = "m/";
790 path += purpose + "'/";
791 path += coin + "'/";
792 path += account + "'/";
793 // Calculate the account extended keys
794 var accountExtendedKey = calcBip32ExtendedKey(path);
795 var accountXprv = accountExtendedKey.toBase58();
796 var accountXpub = accountExtendedKey.neutered().toBase58();
797 // Display the extended keys
798 DOM.bip84accountXprv.val(accountXprv);
799 DOM.bip84accountXpub.val(accountXpub);
800 }
801
802 function displayBip32Info() {
803 // Display the key
804 DOM.seed.val(seed);
805 var rootKey = bip32RootKey.toBase58();
806 DOM.rootKey.val(rootKey);
807 var xprvkeyB58 = "NA";
808 if (!bip32ExtendedKey.isNeutered()) {
809 xprvkeyB58 = bip32ExtendedKey.toBase58();
810 }
811 var extendedPrivKey = xprvkeyB58;
812 DOM.extendedPrivKey.val(extendedPrivKey);
813 var extendedPubKey = bip32ExtendedKey.neutered().toBase58();
814 DOM.extendedPubKey.val(extendedPubKey);
815 // Display the addresses and privkeys
816 clearAddressesList();
817 var initialAddressCount = parseInt(DOM.rowsToAdd.val());
818 displayAddresses(0, initialAddressCount);
819 }
820
821 function displayAddresses(start, total) {
822 generationProcesses.push(new (function() {
823
824 var rows = [];
825
826 this.stop = function() {
827 for (var i=0; i<rows.length; i++) {
828 rows[i].shouldGenerate = false;
829 }
830 hidePending();
831 }
832
833 for (var i=0; i<total; i++) {
834 var index = i + start;
835 var isLast = i == total - 1;
836 rows.push(new TableRow(index, isLast));
837 }
838
839 })());
840 }
841
842 function segwitSelected() {
843 return bip49TabSelected() || bip84TabSelected() || bip141TabSelected();
844 }
845
846 function p2wpkhSelected() {
847 return bip84TabSelected() ||
848 bip141TabSelected() && DOM.bip141semantics.val() == "p2wpkh";
849 }
850
851 function p2wpkhInP2shSelected() {
852 return bip49TabSelected() ||
853 (bip141TabSelected() && DOM.bip141semantics.val() == "p2wpkh-p2sh");
854 }
855
856 function TableRow(index, isLast) {
857
858 var self = this;
859 this.shouldGenerate = true;
860 var useHardenedAddresses = DOM.hardenedAddresses.prop("checked");
861 var useBip38 = DOM.useBip38.prop("checked");
862 var bip38password = DOM.bip38Password.val();
863 var isSegwit = segwitSelected();
864 var segwitAvailable = networkHasSegwit();
865 var isP2wpkh = p2wpkhSelected();
866 var isP2wpkhInP2sh = p2wpkhInP2shSelected();
867
868 function init() {
869 calculateValues();
870 }
871
872 function calculateValues() {
873 setTimeout(function() {
874 if (!self.shouldGenerate) {
875 return;
876 }
877 // derive HDkey for this row of the table
878 var key = "NA";
879 if (useHardenedAddresses) {
880 key = bip32ExtendedKey.deriveHardened(index);
881 }
882 else {
883 key = bip32ExtendedKey.derive(index);
884 }
885 // bip38 requires uncompressed keys
886 // see https://github.com/iancoleman/bip39/issues/140#issuecomment-352164035
887 var keyPair = key.keyPair;
888 var useUncompressed = useBip38;
889 if (useUncompressed) {
890 keyPair = new bitcoinjs.bitcoin.ECPair(keyPair.d, null, { network: network, compressed: false });
891 }
892 // get address
893 var address = keyPair.getAddress().toString();
894 // get privkey
895 var hasPrivkey = !key.isNeutered();
896 var privkey = "NA";
897 if (hasPrivkey) {
898 privkey = keyPair.toWIF();
899 // BIP38 encode private key if required
900 if (useBip38) {
901 privkey = bitcoinjsBip38.encrypt(keyPair.d.toBuffer(), false, bip38password, function(p) {
902 console.log("Progressed " + p.percent.toFixed(1) + "% for index " + index);
903 });
904 }
905 }
906 // get pubkey
907 var pubkey = keyPair.getPublicKeyBuffer().toString('hex');
908 var indexText = getDerivationPath() + "/" + index;
909 if (useHardenedAddresses) {
910 indexText = indexText + "'";
911 }
912 // Ethereum values are different
913 if ((networks[DOM.network.val()].name == "ETH - Ethereum")
914 || (networks[DOM.network.val()].name == "ETC - Ethereum Classic")
915 || (networks[DOM.network.val()].name == "PIRL - Pirl")
916 || (networks[DOM.network.val()].name == "MIX - MIX")
917 || (networks[DOM.network.val()].name == "MUSIC - Musicoin")
918 || (networks[DOM.network.val()].name == "POA - Poa")
919 || (networks[DOM.network.val()].name == "EXP - Expanse")
920 || (networks[DOM.network.val()].name == "CLO - Callisto")
921 || (networks[DOM.network.val()].name == "DXN - DEXON")
922 || (networks[DOM.network.val()].name == "ELLA - Ellaism")
923 || (networks[DOM.network.val()].name == "ESN - Ethersocial Network")
924 ) {
925 var privKeyBuffer = keyPair.d.toBuffer(32);
926 privkey = privKeyBuffer.toString('hex');
927 var addressBuffer = ethUtil.privateToAddress(privKeyBuffer);
928 var hexAddress = addressBuffer.toString('hex');
929 var checksumAddress = ethUtil.toChecksumAddress(hexAddress);
930 address = ethUtil.addHexPrefix(checksumAddress);
931 privkey = ethUtil.addHexPrefix(privkey);
932 pubkey = ethUtil.addHexPrefix(pubkey);
933 }
934
935 // Stellar is different
936 if (networks[DOM.network.val()].name == "XLM - Stellar") {
937 var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44);
938 var coin = parseIntNoNaN(DOM.bip44coin.val(), 0);
939 var path = "m/";
940 path += purpose + "'/";
941 path += coin + "'/" + index + "'";
942 var keypair = stellarUtil.getKeypair(path, seed);
943 indexText = path;
944 privkey = keypair.secret();
945 pubkey = address = keypair.publicKey();
946 }
947 if ((networks[DOM.network.val()].name == "NAS - Nebulas")) {
948 var NasAccount = require("nebulas-account");
949 var privKeyBuffer = keyPair.d.toBuffer(32);
950 var nebulasAccount = new NasAccount();
951 nebulasAccount.setPrivateKey(privKeyBuffer);
952 address = nebulasAccount.getAddressString();
953 privkey = nebulasAccount.getPrivateKeyString();
954 pubkey = nebulasAccount.getPublicKeyString();
955 }
956 // Ripple values are different
957 if (networks[DOM.network.val()].name == "XRP - Ripple") {
958 privkey = convertRipplePriv(privkey);
959 address = convertRippleAdrr(address);
960 }
961 // Bitcoin Cash address format may vary
962 if (networks[DOM.network.val()].name == "BCH - Bitcoin Cash") {
963 var bchAddrType = DOM.bitcoinCashAddressType.filter(":checked").val();
964 if (bchAddrType == "cashaddr") {
965 address = bchaddr.toCashAddress(address);
966 }
967 else if (bchAddrType == "bitpay") {
968 address = bchaddr.toBitpayAddress(address);
969 }
970 }
971 // Bitcoin Cash address format may vary
972 if (networks[DOM.network.val()].name == "SLP - Simple Ledger Protocol") {
973 var bchAddrType = DOM.bitcoinCashAddressType.filter(":checked").val();
974 if (bchAddrType == "cashaddr") {
975 address = bchaddr.toSlpAddress(address);
976 }
977 }
978 // Segwit addresses are different
979 if (isSegwit) {
980 if (!segwitAvailable) {
981 return;
982 }
983 if (isP2wpkh) {
984 var keyhash = bitcoinjs.bitcoin.crypto.hash160(key.getPublicKeyBuffer());
985 var scriptpubkey = bitcoinjs.bitcoin.script.witnessPubKeyHash.output.encode(keyhash);
986 address = bitcoinjs.bitcoin.address.fromOutputScript(scriptpubkey, network)
987 }
988 else if (isP2wpkhInP2sh) {
989 var keyhash = bitcoinjs.bitcoin.crypto.hash160(key.getPublicKeyBuffer());
990 var scriptsig = bitcoinjs.bitcoin.script.witnessPubKeyHash.output.encode(keyhash);
991 var addressbytes = bitcoinjs.bitcoin.crypto.hash160(scriptsig);
992 var scriptpubkey = bitcoinjs.bitcoin.script.scriptHash.output.encode(addressbytes);
993 address = bitcoinjs.bitcoin.address.fromOutputScript(scriptpubkey, network)
994 }
995 }
996
997 if ((networks[DOM.network.val()].name == "CRW - Crown")) {
998 address = bitcoinjs.bitcoin.networks.crown.toNewAddress(address);
999 }
1000
1001 if (networks[DOM.network.val()].name == "EOS - EOSIO") {
1002 address = ""
1003 pubkey = eosUtil.bufferToPublic(keyPair.getPublicKeyBuffer());
1004 privkey = eosUtil.bufferToPrivate(keyPair.d.toBuffer(32));
1005 }
1006
1007 addAddressToList(indexText, address, pubkey, privkey);
1008 if (isLast) {
1009 hidePending();
1010 updateCsv();
1011 }
1012 }, 50)
1013 }
1014
1015 init();
1016
1017 }
1018
1019 function showMore() {
1020 var rowsToAdd = parseInt(DOM.rowsToAdd.val());
1021 if (isNaN(rowsToAdd)) {
1022 rowsToAdd = 20;
1023 DOM.rowsToAdd.val("20");
1024 }
1025 var start = parseInt(DOM.moreRowsStartIndex.val())
1026 if (isNaN(start)) {
1027 start = lastIndexInTable() + 1;
1028 }
1029 else {
1030 var newStart = start + rowsToAdd;
1031 DOM.moreRowsStartIndex.val(newStart);
1032 }
1033 if (rowsToAdd > 200) {
1034 var msg = "Generating " + rowsToAdd + " rows could take a while. ";
1035 msg += "Do you want to continue?";
1036 if (!confirm(msg)) {
1037 return;
1038 }
1039 }
1040 displayAddresses(start, rowsToAdd);
1041 }
1042
1043 function clearDisplay() {
1044 clearAddressesList();
1045 clearKeys();
1046 hideValidationError();
1047 }
1048
1049 function clearAddressesList() {
1050 DOM.addresses.empty();
1051 DOM.csv.val("");
1052 stopGenerating();
1053 }
1054
1055 function stopGenerating() {
1056 while (generationProcesses.length > 0) {
1057 var generation = generationProcesses.shift();
1058 generation.stop();
1059 }
1060 }
1061
1062 function clearKeys() {
1063 clearRootKey();
1064 clearDerivedKeys();
1065 }
1066
1067 function clearRootKey() {
1068 DOM.rootKey.val("");
1069 }
1070
1071 function clearDerivedKeys() {
1072 DOM.extendedPrivKey.val("");
1073 DOM.extendedPubKey.val("");
1074 DOM.bip44accountXprv.val("");
1075 DOM.bip44accountXpub.val("");
1076 }
1077
1078 function addAddressToList(indexText, address, pubkey, privkey) {
1079 var row = $(addressRowTemplate.html());
1080 // Elements
1081 var indexCell = row.find(".index span");
1082 var addressCell = row.find(".address span");
1083 var pubkeyCell = row.find(".pubkey span");
1084 var privkeyCell = row.find(".privkey span");
1085 // Content
1086 indexCell.text(indexText);
1087 addressCell.text(address);
1088 pubkeyCell.text(pubkey);
1089 privkeyCell.text(privkey);
1090 // Visibility
1091 if (!showIndex) {
1092 indexCell.addClass("invisible");
1093 }
1094 if (!showAddress) {
1095 addressCell.addClass("invisible");
1096 }
1097 if (!showPubKey) {
1098 pubkeyCell.addClass("invisible");
1099 }
1100 if (!showPrivKey) {
1101 privkeyCell.addClass("invisible");
1102 }
1103 DOM.addresses.append(row);
1104 var rowShowQrEls = row.find("[data-show-qr]");
1105 setQrEvents(rowShowQrEls);
1106 }
1107
1108 function hasStrongRandom() {
1109 return 'crypto' in window && window['crypto'] !== null;
1110 }
1111
1112 function disableForms() {
1113 $("form").on("submit", function(e) {
1114 e.preventDefault();
1115 });
1116 }
1117
1118 function parseIntNoNaN(val, defaultVal) {
1119 var v = parseInt(val);
1120 if (isNaN(v)) {
1121 return defaultVal;
1122 }
1123 return v;
1124 }
1125
1126 function showPending() {
1127 DOM.feedback
1128 .text("Calculating...")
1129 .show();
1130 }
1131
1132 function findNearestWord(word) {
1133 var language = getLanguage();
1134 var words = WORDLISTS[language];
1135 var minDistance = 99;
1136 var closestWord = words[0];
1137 for (var i=0; i<words.length; i++) {
1138 var comparedTo = words[i];
1139 if (comparedTo.indexOf(word) == 0) {
1140 return comparedTo;
1141 }
1142 var distance = Levenshtein.get(word, comparedTo);
1143 if (distance < minDistance) {
1144 closestWord = comparedTo;
1145 minDistance = distance;
1146 }
1147 }
1148 return closestWord;
1149 }
1150
1151 function hidePending() {
1152 DOM.feedback
1153 .text("")
1154 .hide();
1155 }
1156
1157 function populateNetworkSelect() {
1158 for (var i=0; i<networks.length; i++) {
1159 var network = networks[i];
1160 var option = $("<option>");
1161 option.attr("value", i);
1162 option.text(network.name);
1163 if (network.name == "BTC - Bitcoin") {
1164 option.prop("selected", true);
1165 }
1166 DOM.phraseNetwork.append(option);
1167 }
1168 }
1169
1170 function populateClientSelect() {
1171 for (var i=0; i<clients.length; i++) {
1172 var client = clients[i];
1173 var option = $("<option>");
1174 option.attr("value", i);
1175 option.text(client.name);
1176 DOM.bip32Client.append(option);
1177 }
1178 }
1179
1180 function getLanguage() {
1181 var defaultLanguage = "english";
1182 // Try to get from existing phrase
1183 var language = getLanguageFromPhrase();
1184 // Try to get from url if not from phrase
1185 if (language.length == 0) {
1186 language = getLanguageFromUrl();
1187 }
1188 // Default to English if no other option
1189 if (language.length == 0) {
1190 language = defaultLanguage;
1191 }
1192 return language;
1193 }
1194
1195 function getLanguageFromPhrase(phrase) {
1196 // Check if how many words from existing phrase match a language.
1197 var language = "";
1198 if (!phrase) {
1199 phrase = DOM.phrase.val();
1200 }
1201 if (phrase.length > 0) {
1202 var words = phraseToWordArray(phrase);
1203 var languageMatches = {};
1204 for (l in WORDLISTS) {
1205 // Track how many words match in this language
1206 languageMatches[l] = 0;
1207 for (var i=0; i<words.length; i++) {
1208 var wordInLanguage = WORDLISTS[l].indexOf(words[i]) > -1;
1209 if (wordInLanguage) {
1210 languageMatches[l]++;
1211 }
1212 }
1213 // Find languages with most word matches.
1214 // This is made difficult due to commonalities between Chinese
1215 // simplified vs traditional.
1216 var mostMatches = 0;
1217 var mostMatchedLanguages = [];
1218 for (var l in languageMatches) {
1219 var numMatches = languageMatches[l];
1220 if (numMatches > mostMatches) {
1221 mostMatches = numMatches;
1222 mostMatchedLanguages = [l];
1223 }
1224 else if (numMatches == mostMatches) {
1225 mostMatchedLanguages.push(l);
1226 }
1227 }
1228 }
1229 if (mostMatchedLanguages.length > 0) {
1230 // Use first language and warn if multiple detected
1231 language = mostMatchedLanguages[0];
1232 if (mostMatchedLanguages.length > 1) {
1233 console.warn("Multiple possible languages");
1234 console.warn(mostMatchedLanguages);
1235 }
1236 }
1237 }
1238 return language;
1239 }
1240
1241 function getLanguageFromUrl() {
1242 for (var language in WORDLISTS) {
1243 if (window.location.hash.indexOf(language) > -1) {
1244 return language;
1245 }
1246 }
1247 return "";
1248 }
1249
1250 function setMnemonicLanguage() {
1251 var language = getLanguage();
1252 // Load the bip39 mnemonic generator for this language if required
1253 if (!(language in mnemonics)) {
1254 mnemonics[language] = new Mnemonic(language);
1255 }
1256 mnemonic = mnemonics[language];
1257 }
1258
1259 function convertPhraseToNewLanguage() {
1260 var oldLanguage = getLanguageFromPhrase();
1261 var newLanguage = getLanguageFromUrl();
1262 var oldPhrase = DOM.phrase.val();
1263 var oldWords = phraseToWordArray(oldPhrase);
1264 var newWords = [];
1265 for (var i=0; i<oldWords.length; i++) {
1266 var oldWord = oldWords[i];
1267 var index = WORDLISTS[oldLanguage].indexOf(oldWord);
1268 var newWord = WORDLISTS[newLanguage][index];
1269 newWords.push(newWord);
1270 }
1271 newPhrase = wordArrayToPhrase(newWords);
1272 return newPhrase;
1273 }
1274
1275 // TODO look at jsbip39 - mnemonic.splitWords
1276 function phraseToWordArray(phrase) {
1277 var words = phrase.split(/\s/g);
1278 var noBlanks = [];
1279 for (var i=0; i<words.length; i++) {
1280 var word = words[i];
1281 if (word.length > 0) {
1282 noBlanks.push(word);
1283 }
1284 }
1285 return noBlanks;
1286 }
1287
1288 // TODO look at jsbip39 - mnemonic.joinWords
1289 function wordArrayToPhrase(words) {
1290 var phrase = words.join(" ");
1291 var language = getLanguageFromPhrase(phrase);
1292 if (language == "japanese") {
1293 phrase = words.join("\u3000");
1294 }
1295 return phrase;
1296 }
1297
1298 function isUsingOwnEntropy() {
1299 return DOM.useEntropy.prop("checked");
1300 }
1301
1302 function setMnemonicFromEntropy() {
1303 clearEntropyFeedback();
1304 // Get entropy value
1305 var entropyStr = DOM.entropy.val();
1306 // Work out minimum base for entropy
1307 var entropy = Entropy.fromString(entropyStr);
1308 if (entropy.binaryStr.length == 0) {
1309 return;
1310 }
1311 // Show entropy details
1312 showEntropyFeedback(entropy);
1313 // Use entropy hash if not using raw entropy
1314 var bits = entropy.binaryStr;
1315 var mnemonicLength = DOM.entropyMnemonicLength.val();
1316 if (mnemonicLength != "raw") {
1317 // Get bits by hashing entropy with SHA256
1318 var hash = sjcl.hash.sha256.hash(entropy.cleanStr);
1319 var hex = sjcl.codec.hex.fromBits(hash);
1320 bits = BigInteger.parse(hex, 16).toString(2);
1321 while (bits.length % 256 != 0) {
1322 bits = "0" + bits;
1323 }
1324 // Truncate hash to suit number of words
1325 mnemonicLength = parseInt(mnemonicLength);
1326 var numberOfBits = 32 * mnemonicLength / 3;
1327 bits = bits.substring(0, numberOfBits);
1328 // show warning for weak entropy override
1329 if (mnemonicLength / 3 * 32 > entropy.binaryStr.length) {
1330 DOM.entropyWeakEntropyOverrideWarning.removeClass("hidden");
1331 }
1332 else {
1333 DOM.entropyWeakEntropyOverrideWarning.addClass("hidden");
1334 }
1335 }
1336 else {
1337 // hide warning for weak entropy override
1338 DOM.entropyWeakEntropyOverrideWarning.addClass("hidden");
1339 }
1340 // Discard trailing entropy
1341 var bitsToUse = Math.floor(bits.length / 32) * 32;
1342 var start = bits.length - bitsToUse;
1343 var binaryStr = bits.substring(start);
1344 // Convert entropy string to numeric array
1345 var entropyArr = [];
1346 for (var i=0; i<binaryStr.length / 8; i++) {
1347 var byteAsBits = binaryStr.substring(i*8, i*8+8);
1348 var entropyByte = parseInt(byteAsBits, 2);
1349 entropyArr.push(entropyByte)
1350 }
1351 // Convert entropy array to mnemonic
1352 var phrase = mnemonic.toMnemonic(entropyArr);
1353 // Set the mnemonic in the UI
1354 DOM.phrase.val(phrase);
1355 // Show the word indexes
1356 showWordIndexes();
1357 // Show the checksum
1358 showChecksum();
1359 }
1360
1361 function clearEntropyFeedback() {
1362 DOM.entropyCrackTime.text("...");
1363 DOM.entropyType.text("");
1364 DOM.entropyWordCount.text("0");
1365 DOM.entropyEventCount.text("0");
1366 DOM.entropyBitsPerEvent.text("0");
1367 DOM.entropyBits.text("0");
1368 DOM.entropyFiltered.html("&nbsp;");
1369 DOM.entropyBinary.html("&nbsp;");
1370 }
1371
1372 function showEntropyFeedback(entropy) {
1373 var numberOfBits = entropy.binaryStr.length;
1374 var timeToCrack = "unknown";
1375 try {
1376 var z = zxcvbn(entropy.base.parts.join(""));
1377 timeToCrack = z.crack_times_display.offline_fast_hashing_1e10_per_second;
1378 if (z.feedback.warning != "") {
1379 timeToCrack = timeToCrack + " - " + z.feedback.warning;
1380 };
1381 }
1382 catch (e) {
1383 console.log("Error detecting entropy strength with zxcvbn:");
1384 console.log(e);
1385 }
1386 var entropyTypeStr = getEntropyTypeStr(entropy);
1387 var wordCount = Math.floor(numberOfBits / 32) * 3;
1388 var bitsPerEvent = entropy.bitsPerEvent.toFixed(2);
1389 var spacedBinaryStr = addSpacesEveryElevenBits(entropy.binaryStr);
1390 DOM.entropyFiltered.html(entropy.cleanHtml);
1391 DOM.entropyType.text(entropyTypeStr);
1392 DOM.entropyCrackTime.text(timeToCrack);
1393 DOM.entropyEventCount.text(entropy.base.ints.length);
1394 DOM.entropyBits.text(numberOfBits);
1395 DOM.entropyWordCount.text(wordCount);
1396 DOM.entropyBinary.text(spacedBinaryStr);
1397 DOM.entropyBitsPerEvent.text(bitsPerEvent);
1398 // detect and warn of filtering
1399 var rawNoSpaces = DOM.entropy.val().replace(/\s/g, "");
1400 var cleanNoSpaces = entropy.cleanStr.replace(/\s/g, "");
1401 var isFiltered = rawNoSpaces.length != cleanNoSpaces.length;
1402 if (isFiltered) {
1403 DOM.entropyFilterWarning.removeClass('hidden');
1404 }
1405 else {
1406 DOM.entropyFilterWarning.addClass('hidden');
1407 }
1408 }
1409
1410 function getEntropyTypeStr(entropy) {
1411 var typeStr = entropy.base.str;
1412 // Add some detail if these are cards
1413 if (entropy.base.asInt == 52) {
1414 var cardDetail = []; // array of message strings
1415 // Detect duplicates
1416 var dupes = [];
1417 var dupeTracker = {};
1418 for (var i=0; i<entropy.base.parts.length; i++) {
1419 var card = entropy.base.parts[i];
1420 var cardUpper = card.toUpperCase();
1421 if (cardUpper in dupeTracker) {
1422 dupes.push(card);
1423 }
1424 dupeTracker[cardUpper] = true;
1425 }
1426 if (dupes.length > 0) {
1427 var dupeWord = "duplicates";
1428 if (dupes.length == 1) {
1429 dupeWord = "duplicate";
1430 }
1431 var msg = dupes.length + " " + dupeWord + ": " + dupes.slice(0,3).join(" ");
1432 if (dupes.length > 3) {
1433 msg += "...";
1434 }
1435 cardDetail.push(msg);
1436 }
1437 // Detect full deck
1438 var uniqueCards = [];
1439 for (var uniqueCard in dupeTracker) {
1440 uniqueCards.push(uniqueCard);
1441 }
1442 if (uniqueCards.length == 52) {
1443 cardDetail.unshift("full deck");
1444 }
1445 // Detect missing cards
1446 var values = "A23456789TJQK";
1447 var suits = "CDHS";
1448 var missingCards = [];
1449 for (var i=0; i<suits.length; i++) {
1450 for (var j=0; j<values.length; j++) {
1451 var card = values[j] + suits[i];
1452 if (!(card in dupeTracker)) {
1453 missingCards.push(card);
1454 }
1455 }
1456 }
1457 // Display missing cards if six or less, ie clearly going for full deck
1458 if (missingCards.length > 0 && missingCards.length <= 6) {
1459 var msg = missingCards.length + " missing: " + missingCards.slice(0,3).join(" ");
1460 if (missingCards.length > 3) {
1461 msg += "...";
1462 }
1463 cardDetail.push(msg);
1464 }
1465 // Add card details to typeStr
1466 if (cardDetail.length > 0) {
1467 typeStr += " (" + cardDetail.join(", ") + ")";
1468 }
1469 }
1470 return typeStr;
1471 }
1472
1473 function setQrEvents(els) {
1474 els.on("mouseenter", createQr);
1475 els.on("mouseleave", destroyQr);
1476 els.on("click", toggleQr);
1477 }
1478
1479 function createQr(e) {
1480 var content = e.target.textContent || e.target.value;
1481 if (content) {
1482 var qrEl = kjua({
1483 text: content,
1484 render: "canvas",
1485 size: 310,
1486 ecLevel: 'H',
1487 });
1488 DOM.qrImage.append(qrEl);
1489 if (!showQr) {
1490 DOM.qrHider.addClass("hidden");
1491 }
1492 else {
1493 DOM.qrHider.removeClass("hidden");
1494 }
1495 DOM.qrContainer.removeClass("hidden");
1496 }
1497 }
1498
1499 function destroyQr() {
1500 DOM.qrImage.text("");
1501 DOM.qrContainer.addClass("hidden");
1502 }
1503
1504 function toggleQr() {
1505 showQr = !showQr;
1506 DOM.qrHider.toggleClass("hidden");
1507 DOM.qrHint.toggleClass("hidden");
1508 }
1509
1510 function bip44TabSelected() {
1511 return DOM.bip44tab.hasClass("active");
1512 }
1513
1514 function bip32TabSelected() {
1515 return DOM.bip32tab.hasClass("active");
1516 }
1517
1518 function networkHasSegwit() {
1519 var n = network;
1520 if ("baseNetwork" in network) {
1521 n = bitcoinjs.bitcoin.networks[network.baseNetwork];
1522 }
1523 // check if only p2wpkh params are required
1524 if (p2wpkhSelected()) {
1525 return "p2wpkh" in n;
1526 }
1527 // check if only p2wpkh-in-p2sh params are required
1528 else if (p2wpkhInP2shSelected()) {
1529 return "p2wpkhInP2sh" in n;
1530 }
1531 // require both if it's unclear which params are required
1532 return "p2wpkh" in n && "p2wpkhInP2sh" in n;
1533 }
1534
1535 function bip49TabSelected() {
1536 return DOM.bip49tab.hasClass("active");
1537 }
1538
1539 function bip84TabSelected() {
1540 return DOM.bip84tab.hasClass("active");
1541 }
1542
1543 function bip141TabSelected() {
1544 return DOM.bip141tab.hasClass("active");
1545 }
1546
1547 function setHdCoin(coinValue) {
1548 DOM.bip44coin.val(coinValue);
1549 DOM.bip49coin.val(coinValue);
1550 DOM.bip84coin.val(coinValue);
1551 }
1552
1553 function showSegwitAvailable() {
1554 DOM.bip49unavailable.addClass("hidden");
1555 DOM.bip49available.removeClass("hidden");
1556 DOM.bip84unavailable.addClass("hidden");
1557 DOM.bip84available.removeClass("hidden");
1558 DOM.bip141unavailable.addClass("hidden");
1559 DOM.bip141available.removeClass("hidden");
1560 }
1561
1562 function showSegwitUnavailable() {
1563 DOM.bip49available.addClass("hidden");
1564 DOM.bip49unavailable.removeClass("hidden");
1565 DOM.bip84available.addClass("hidden");
1566 DOM.bip84unavailable.removeClass("hidden");
1567 DOM.bip141available.addClass("hidden");
1568 DOM.bip141unavailable.removeClass("hidden");
1569 }
1570
1571 function adjustNetworkForSegwit() {
1572 // If segwit is selected the xpub/xprv prefixes need to be adjusted
1573 // to avoid accidentally importing BIP49 xpub to BIP44 watch only
1574 // wallet.
1575 // See https://github.com/iancoleman/bip39/issues/125
1576 var segwitNetworks = null;
1577 // if a segwit network is alread selected, need to use base network to
1578 // look up new parameters
1579 if ("baseNetwork" in network) {
1580 network = bitcoinjs.bitcoin.networks[network.baseNetwork];
1581 }
1582 // choose the right segwit params
1583 if (p2wpkhSelected() && "p2wpkh" in network) {
1584 network = network.p2wpkh;
1585 }
1586 else if (p2wpkhInP2shSelected() && "p2wpkhInP2sh" in network) {
1587 network = network.p2wpkhInP2sh;
1588 }
1589 }
1590
1591 function lastIndexInTable() {
1592 var pathText = DOM.addresses.find(".index").last().text();
1593 var pathBits = pathText.split("/");
1594 var lastBit = pathBits[pathBits.length-1];
1595 var lastBitClean = lastBit.replace("'", "");
1596 return parseInt(lastBitClean);
1597 }
1598
1599 function uint8ArrayToHex(a) {
1600 var s = ""
1601 for (var i=0; i<a.length; i++) {
1602 var h = a[i].toString(16);
1603 while (h.length < 2) {
1604 h = "0" + h;
1605 }
1606 s = s + h;
1607 }
1608 return s;
1609 }
1610
1611 function showWordIndexes() {
1612 var phrase = DOM.phrase.val();
1613 var words = phraseToWordArray(phrase);
1614 var wordIndexes = [];
1615 var language = getLanguage();
1616 for (var i=0; i<words.length; i++) {
1617 var word = words[i];
1618 var wordIndex = WORDLISTS[language].indexOf(word);
1619 wordIndexes.push(wordIndex);
1620 }
1621 var wordIndexesStr = wordIndexes.join(", ");
1622 DOM.entropyWordIndexes.text(wordIndexesStr);
1623 }
1624
1625 function showChecksum() {
1626 var phrase = DOM.phrase.val();
1627 var words = phraseToWordArray(phrase);
1628 var checksumBitlength = words.length / 3;
1629 var checksum = "";
1630 var binaryStr = "";
1631 var language = getLanguage();
1632 for (var i=words.length-1; i>=0; i--) {
1633 var word = words[i];
1634 var wordIndex = WORDLISTS[language].indexOf(word);
1635 var wordBinary = wordIndex.toString(2);
1636 while (wordBinary.length < 11) {
1637 wordBinary = "0" + wordBinary;
1638 }
1639 var binaryStr = wordBinary + binaryStr;
1640 if (binaryStr.length >= checksumBitlength) {
1641 var start = binaryStr.length - checksumBitlength;
1642 var end = binaryStr.length;
1643 checksum = binaryStr.substring(start, end);
1644 // add spaces so the last group is 11 bits, not the first
1645 checksum = checksum.split("").reverse().join("")
1646 checksum = addSpacesEveryElevenBits(checksum);
1647 checksum = checksum.split("").reverse().join("")
1648 break;
1649 }
1650 }
1651 DOM.entropyChecksum.text(checksum);
1652 }
1653
1654 function updateCsv() {
1655 var tableCsv = "path,address,public key,private key\n";
1656 var rows = DOM.addresses.find("tr");
1657 for (var i=0; i<rows.length; i++) {
1658 var row = $(rows[i]);
1659 var cells = row.find("td");
1660 for (var j=0; j<cells.length; j++) {
1661 var cell = $(cells[j]);
1662 if (!cell.children().hasClass("invisible")) {
1663 tableCsv = tableCsv + cell.text();
1664 }
1665 if (j != cells.length - 1) {
1666 tableCsv = tableCsv + ",";
1667 }
1668 }
1669 tableCsv = tableCsv + "\n";
1670 }
1671 DOM.csv.val(tableCsv);
1672 }
1673
1674 function addSpacesEveryElevenBits(binaryStr) {
1675 return binaryStr.match(/.{1,11}/g).join(" ");
1676 }
1677
1678 var networks = [
1679 {
1680 name: "AC - Asiacoin",
1681 onSelect: function() {
1682 network = bitcoinjs.bitcoin.networks.asiacoin;
1683 setHdCoin(51);
1684 },
1685 },
1686 {
1687 name: "ACC - Adcoin",
1688 onSelect: function() {
1689 network = bitcoinjs.bitcoin.networks.adcoin;
1690 setHdCoin(161);
1691 },
1692 },
1693 {
1694 name: "AUR - Auroracoin",
1695 onSelect: function() {
1696 network = bitcoinjs.bitcoin.networks.auroracoin;
1697 setHdCoin(85);
1698 },
1699 },
1700 {
1701 name: "AXE - Axe",
1702 onSelect: function() {
1703 network = bitcoinjs.bitcoin.networks.axe;
1704 setHdCoin(4242);
1705 },
1706 },
1707 {
1708 name: "ANON - ANON",
1709 onSelect: function() {
1710 network = bitcoinjs.bitcoin.networks.anon;
1711 setHdCoin(220);
1712 },
1713 },
1714 {
1715 name: "BOLI - Bolivarcoin",
1716 onSelect: function() {
1717 network = bitcoinjs.bitcoin.networks.bolivarcoin;
1718 setHdCoin(278);
1719 },
1720 },
1721 {
1722 name: "BCA - Bitcoin Atom",
1723 onSelect: function() {
1724 network = bitcoinjs.bitcoin.networks.atom;
1725 setHdCoin(185);
1726 },
1727 },
1728 {
1729 name: "BCH - Bitcoin Cash",
1730 onSelect: function() {
1731 DOM.bitcoinCashAddressTypeContainer.removeClass("hidden");
1732 setHdCoin(145);
1733 },
1734 },
1735 {
1736 name: "BEET - Beetlecoin",
1737 onSelect: function() {
1738 network = bitcoinjs.bitcoin.networks.beetlecoin;
1739 setHdCoin(800);
1740 },
1741 },
1742 {
1743 name: "BELA - Belacoin",
1744 onSelect: function() {
1745 network = bitcoinjs.bitcoin.networks.belacoin;
1746 setHdCoin(73);
1747 },
1748 },
1749 {
1750 name: "BLK - BlackCoin",
1751 onSelect: function() {
1752 network = bitcoinjs.bitcoin.networks.blackcoin;
1753 setHdCoin(10);
1754 },
1755 },
1756 {
1757 name: "BND - Blocknode",
1758 onSelect: function() {
1759 network = bitcoinjs.bitcoin.networks.blocknode;
1760 setHdCoin(2941);
1761 },
1762 },
1763 {
1764 name: "tBND - Blocknode Testnet",
1765 onSelect: function() {
1766 network = bitcoinjs.bitcoin.networks.blocknode_testnet;
1767 setHdCoin(1);
1768 },
1769 },
1770 {
1771 name: "BRIT - Britcoin",
1772 onSelect: function() {
1773 network = bitcoinjs.bitcoin.networks.britcoin;
1774 setHdCoin(70);
1775 },
1776 },
1777 {
1778 name: "BSD - Bitsend",
1779 onSelect: function() {
1780 network = bitcoinjs.bitcoin.networks.bitsend;
1781 setHdCoin(91);
1782 },
1783 },
1784 {
1785 name: "BST - BlockStamp",
1786 onSelect: function() {
1787 network = bitcoinjs.bitcoin.networks.blockstamp;
1788 setHdCoin(254);
1789 },
1790 },
1791 {
1792 name: "BTA - Bata",
1793 onSelect: function() {
1794 network = bitcoinjs.bitcoin.networks.bata;
1795 setHdCoin(89);
1796 },
1797 },
1798 {
1799 name: "BTC - Bitcoin",
1800 onSelect: function() {
1801 network = bitcoinjs.bitcoin.networks.bitcoin;
1802 setHdCoin(0);
1803 },
1804 },
1805 {
1806 name: "BTC - Bitcoin Testnet",
1807 onSelect: function() {
1808 network = bitcoinjs.bitcoin.networks.testnet;
1809 setHdCoin(1);
1810 },
1811 },
1812 {
1813 name: "BITG - Bitcoin Green",
1814 onSelect: function() {
1815 network = bitcoinjs.bitcoin.networks.bitcoingreen;
1816 setHdCoin(222);
1817 },
1818 },
1819 {
1820 name: "BTCP - Bitcoin Private",
1821 onSelect: function() {
1822 network = bitcoinjs.bitcoin.networks.bitcoinprivate;
1823 setHdCoin(183);
1824 },
1825 },
1826 {
1827 name: "BTCZ - Bitcoinz",
1828 onSelect: function() {
1829 network = bitcoinjs.bitcoin.networks.bitcoinz;
1830 setHdCoin(177);
1831 },
1832 },
1833 {
1834 name: "BTDX - BitCloud",
1835 onSelect: function() {
1836 network = bitcoinjs.bitcoin.networks.bitcloud;
1837 setHdCoin(218);
1838 },
1839 },
1840 {
1841 name: "BTG - Bitcoin Gold",
1842 onSelect: function() {
1843 network = bitcoinjs.bitcoin.networks.bgold;
1844 setHdCoin(156);
1845 },
1846 },
1847 {
1848 name: "BTX - Bitcore",
1849 onSelect: function() {
1850 network = bitcoinjs.bitcoin.networks.bitcore;
1851 setHdCoin(160);
1852 },
1853 },
1854 {
1855 name: "CCN - Cannacoin",
1856 onSelect: function() {
1857 network = bitcoinjs.bitcoin.networks.cannacoin;
1858 setHdCoin(19);
1859 },
1860 },
1861 {
1862 name: "CESC - Cryptoescudo",
1863 onSelect: function() {
1864 network = bitcoinjs.bitcoin.networks.cannacoin;
1865 setHdCoin(111);
1866 },
1867 },
1868 {
1869 name: "CDN - Canadaecoin",
1870 onSelect: function() {
1871 network = bitcoinjs.bitcoin.networks.canadaecoin;
1872 setHdCoin(34);
1873 },
1874 },
1875 {
1876 name: "CLAM - Clams",
1877 onSelect: function() {
1878 network = bitcoinjs.bitcoin.networks.clam;
1879 setHdCoin(23);
1880 },
1881 },
1882 {
1883 name: "CLO - Callisto",
1884 segwitAvailable: false,
1885 onSelect: function() {
1886 network = bitcoinjs.bitcoin.networks.bitcoin;
1887 setHdCoin(820);
1888 },
1889 },
1890 {
1891 name: "CLUB - Clubcoin",
1892 onSelect: function() {
1893 network = bitcoinjs.bitcoin.networks.clubcoin;
1894 setHdCoin(79);
1895 },
1896 },
1897 {
1898 name: "CMP - Compcoin",
1899 onSelect: function() {
1900 network = bitcoinjs.bitcoin.networks.compcoin;
1901 setHdCoin(71);
1902 },
1903 },
1904 {
1905 name: "CRAVE - Crave",
1906 onSelect: function() {
1907 network = bitcoinjs.bitcoin.networks.crave;
1908 setHdCoin(186);
1909 },
1910 },
1911 {
1912 name: "CRW - Crown (Legacy)",
1913 onSelect: function() {
1914 network = bitcoinjs.bitcoin.networks.crown;
1915 setHdCoin(72);
1916 },
1917 },
1918 {
1919 name: "CRW - Crown",
1920 onSelect: function() {
1921 network = bitcoinjs.bitcoin.networks.crown;
1922 setHdCoin(72);
1923 },
1924 },
1925 {
1926 name: "DASH - Dash",
1927 onSelect: function() {
1928 network = bitcoinjs.bitcoin.networks.dash;
1929 setHdCoin(5);
1930 },
1931 },
1932 {
1933 name: "DASH - Dash Testnet",
1934 onSelect: function() {
1935 network = bitcoinjs.bitcoin.networks.dashtn;
1936 setHdCoin(1);
1937 },
1938 },
1939 {
1940 name: "DFC - Defcoin",
1941 onSelect: function() {
1942 network = bitcoinjs.bitcoin.networks.defcoin;
1943 setHdCoin(1337);
1944 },
1945 },
1946 {
1947 name: "DGB - Digibyte",
1948 onSelect: function() {
1949 network = bitcoinjs.bitcoin.networks.digibyte;
1950 setHdCoin(20);
1951 },
1952 },
1953 {
1954 name: "DGC - Digitalcoin",
1955 onSelect: function() {
1956 network = bitcoinjs.bitcoin.networks.digitalcoin;
1957 setHdCoin(18);
1958 },
1959 },
1960 {
1961 name: "DMD - Diamond",
1962 onSelect: function() {
1963 network = bitcoinjs.bitcoin.networks.diamond;
1964 setHdCoin(152);
1965 },
1966 },
1967 {
1968 name: "DNR - Denarius",
1969 onSelect: function() {
1970 network = bitcoinjs.bitcoin.networks.denarius;
1971 setHdCoin(116);
1972 },
1973 },
1974 {
1975 name: "DOGE - Dogecoin",
1976 onSelect: function() {
1977 network = bitcoinjs.bitcoin.networks.dogecoin;
1978 setHdCoin(3);
1979 },
1980 },
1981 {
1982 name: "DOGEt - Dogecoin Testnet",
1983 onSelect: function() {
1984 network = bitcoinjs.bitcoin.networks.dogecointestnet;
1985 setHdCoin(1);
1986 },
1987 },
1988 {
1989 name: "DXN - DEXON",
1990 onSelect: function() {
1991 network = bitcoinjs.bitcoin.networks.bitcoin;
1992 setHdCoin(237);
1993 },
1994 },
1995 {
1996 name: "ECN - Ecoin",
1997 onSelect: function() {
1998 network = bitcoinjs.bitcoin.networks.ecoin;
1999 setHdCoin(115);
2000 },
2001 },
2002 {
2003 name: "EDRC - Edrcoin",
2004 onSelect: function() {
2005 network = bitcoinjs.bitcoin.networks.edrcoin;
2006 setHdCoin(56);
2007 },
2008 },
2009 {
2010 name: "EFL - Egulden",
2011 onSelect: function() {
2012 network = bitcoinjs.bitcoin.networks.egulden;
2013 setHdCoin(78);
2014 },
2015 },
2016 {
2017 name: "ELLA - Ellaism",
2018 segwitAvailable: false,
2019 onSelect: function() {
2020 network = bitcoinjs.bitcoin.networks.bitcoin;
2021 setHdCoin(163);
2022 },
2023 },
2024 {
2025 name: "EMC2 - Einsteinium",
2026 onSelect: function() {
2027 network = bitcoinjs.bitcoin.networks.einsteinium;
2028 setHdCoin(41);
2029 },
2030 },
2031 {
2032 name: "ERC - Europecoin",
2033 onSelect: function() {
2034 network = bitcoinjs.bitcoin.networks.europecoin;
2035 setHdCoin(151);
2036 },
2037 },
2038 {
2039 name: "EOS - EOSIO",
2040 onSelect: function() {
2041 network = bitcoinjs.bitcoin.networks.bitcoin;
2042 setHdCoin(194);
2043 },
2044 },
2045 {
2046 name: "ESN - Ethersocial Network",
2047 segwitAvailable: false,
2048 onSelect: function() {
2049 network = bitcoinjs.bitcoin.networks.bitcoin;
2050 setHdCoin(31102);
2051 },
2052 },
2053 {
2054 name: "ETC - Ethereum Classic",
2055 segwitAvailable: false,
2056 onSelect: function() {
2057 network = bitcoinjs.bitcoin.networks.bitcoin;
2058 setHdCoin(61);
2059 },
2060 },
2061 {
2062 name: "ETH - Ethereum",
2063 onSelect: function() {
2064 network = bitcoinjs.bitcoin.networks.bitcoin;
2065 setHdCoin(60);
2066 },
2067 },
2068 {
2069 name: "EXCL - Exclusivecoin",
2070 onSelect: function() {
2071 network = bitcoinjs.bitcoin.networks.exclusivecoin;
2072 setHdCoin(190);
2073 },
2074 },
2075 {
2076 name: "EXCC - ExchangeCoin",
2077 onSelect: function() {
2078 network = bitcoinjs.bitcoin.networks.exchangecoin;
2079 setHdCoin(0);
2080 },
2081 },
2082 {
2083 name: "EXP - Expanse",
2084 segwitAvailable: false,
2085 onSelect: function() {
2086 network = bitcoinjs.bitcoin.networks.bitcoin;
2087 setHdCoin(40);
2088 },
2089 },
2090 {
2091 name: "FIX - FIX",
2092 onSelect: function() {
2093 network = bitcoinjs.bitcoin.networks.fix;
2094 setHdCoin(336);
2095 },
2096 },
2097 {
2098 name: "FIX - FIX Testnet",
2099 onSelect: function() {
2100 network = bitcoinjs.bitcoin.networks.fixtestnet;
2101 setHdCoin(1);
2102 },
2103 },
2104 {
2105 name: "FJC - Fujicoin",
2106 onSelect: function() {
2107 network = bitcoinjs.bitcoin.networks.fujicoin;
2108 setHdCoin(75);
2109 },
2110 },
2111 {
2112 name: "FLASH - Flashcoin",
2113 onSelect: function() {
2114 network = bitcoinjs.bitcoin.networks.flashcoin;
2115 setHdCoin(120);
2116 },
2117 },
2118 {
2119 name: "FRST - Firstcoin",
2120 onSelect: function() {
2121 network = bitcoinjs.bitcoin.networks.firstcoin;
2122 setHdCoin(167);
2123 },
2124 },
2125 {
2126 name: "FTC - Feathercoin",
2127 onSelect: function() {
2128 network = bitcoinjs.bitcoin.networks.feathercoin;
2129 setHdCoin(8);
2130 },
2131 },
2132 {
2133 name: "GAME - GameCredits",
2134 onSelect: function() {
2135 network = bitcoinjs.bitcoin.networks.game;
2136 setHdCoin(101);
2137 },
2138 },
2139 {
2140 name: "GBX - Gobyte",
2141 onSelect: function() {
2142 network = bitcoinjs.bitcoin.networks.gobyte;
2143 setHdCoin(176);
2144 },
2145 },
2146 {
2147 name: "GCR - GCRCoin",
2148 onSelect: function() {
2149 network = bitcoinjs.bitcoin.networks.gcr;
2150 setHdCoin(79);
2151 },
2152 },
2153 {
2154 name: "GRC - Gridcoin",
2155 onSelect: function() {
2156 network = bitcoinjs.bitcoin.networks.gridcoin;
2157 setHdCoin(84);
2158 },
2159 },
2160 {
2161 name: "HNC - Helleniccoin",
2162 onSelect: function() {
2163 network = bitcoinjs.bitcoin.networks.helleniccoin;
2164 setHdCoin(168);
2165 },
2166 },
2167 {
2168 name: "HUSH - Hush",
2169 onSelect: function() {
2170 network = bitcoinjs.bitcoin.networks.hush;
2171 setHdCoin(197);
2172 },
2173 },
2174 {
2175 name: "INSN - Insane",
2176 onSelect: function() {
2177 network = bitcoinjs.bitcoin.networks.insane;
2178 setHdCoin(68);
2179 },
2180 },
2181 {
2182 name: "IOP - Iop",
2183 onSelect: function() {
2184 network = bitcoinjs.bitcoin.networks.iop;
2185 setHdCoin(66);
2186 },
2187 },
2188 {
2189 name: "IXC - Ixcoin",
2190 onSelect: function() {
2191 network = bitcoinjs.bitcoin.networks.ixcoin;
2192 setHdCoin(86);
2193 },
2194 },
2195 {
2196 name: "JBS - Jumbucks",
2197 onSelect: function() {
2198 network = bitcoinjs.bitcoin.networks.jumbucks;
2199 setHdCoin(26);
2200 },
2201 },
2202 {
2203 name: "KMD - Komodo",
2204 bip49available: false,
2205 onSelect: function() {
2206 network = bitcoinjs.bitcoin.networks.komodo;
2207 setHdCoin(141);
2208 },
2209 },
2210 {
2211 name: "KOBO - Kobocoin",
2212 bip49available: false,
2213 onSelect: function() {
2214 network = bitcoinjs.bitcoin.networks.kobocoin;
2215 setHdCoin(196);
2216 },
2217 },
2218 {
2219 name: "LBC - Library Credits",
2220 onSelect: function() {
2221 network = bitcoinjs.bitcoin.networks.lbry;
2222 setHdCoin(140);
2223 },
2224 },
2225 {
2226 name: "LCC - Litecoincash",
2227 onSelect: function() {
2228 network = bitcoinjs.bitcoin.networks.litecoincash;
2229 setHdCoin(192);
2230 },
2231 },
2232 {
2233 name: "LDCN - Landcoin",
2234 onSelect: function() {
2235 network = bitcoinjs.bitcoin.networks.landcoin;
2236 setHdCoin(63);
2237 },
2238 },
2239 {
2240 name: "LINX - Linx",
2241 onSelect: function() {
2242 network = bitcoinjs.bitcoin.networks.linx;
2243 setHdCoin(114);
2244 },
2245 },
2246 {
2247 name: "LKR - Lkrcoin",
2248 segwitAvailable: false,
2249 onSelect: function() {
2250 network = bitcoinjs.bitcoin.networks.lkrcoin;
2251 setHdCoin(557);
2252 },
2253 },
2254 {
2255 name: "LTC - Litecoin",
2256 onSelect: function() {
2257 network = bitcoinjs.bitcoin.networks.litecoin;
2258 setHdCoin(2);
2259 DOM.litecoinLtubContainer.removeClass("hidden");
2260 },
2261 },
2262 {
2263 name: "LTCt - Litecoin Testnet",
2264 onSelect: function() {
2265 network = bitcoinjs.bitcoin.networks.litecointestnet;
2266 setHdCoin(1);
2267 DOM.litecoinLtubContainer.removeClass("hidden");
2268 },
2269 },
2270 {
2271 name: "LTZ - LitecoinZ",
2272 onSelect: function() {
2273 network = bitcoinjs.bitcoin.networks.litecoinz;
2274 setHdCoin(221);
2275 },
2276 },
2277 {
2278 name: "LYNX - Lynx",
2279 onSelect: function() {
2280 network = bitcoinjs.bitcoin.networks.lynx;
2281 setHdCoin(191);
2282 },
2283 },
2284 {
2285 name: "MAZA - Maza",
2286 onSelect: function() {
2287 network = bitcoinjs.bitcoin.networks.maza;
2288 setHdCoin(13);
2289 },
2290 },
2291 {
2292 name: "MEC - Megacoin",
2293 onSelect: function() {
2294 network = bitcoinjs.bitcoin.networks.megacoin;
2295 setHdCoin(217);
2296 },
2297 },
2298 {
2299 name: "MIX - MIX",
2300 segwitAvailable: false,
2301 onSelect: function() {
2302 network = bitcoinjs.bitcoin.networks.bitcoin;
2303 setHdCoin(76);
2304 },
2305 },
2306 {
2307 name: "MNX - Minexcoin",
2308 onSelect: function() {
2309 network = bitcoinjs.bitcoin.networks.minexcoin;
2310 setHdCoin(182);
2311 },
2312 },
2313 {
2314 name: "MONA - Monacoin",
2315 onSelect: function() {
2316 network = bitcoinjs.bitcoin.networks.monacoin,
2317 setHdCoin(22);
2318 },
2319 },
2320 {
2321 name: "MUSIC - Musicoin",
2322 segwitAvailable: false,
2323 onSelect: function() {
2324 network = bitcoinjs.bitcoin.networks.bitcoin;
2325 setHdCoin(184);
2326 },
2327 },
2328 {
2329 name: "NAV - Navcoin",
2330 onSelect: function() {
2331 network = bitcoinjs.bitcoin.networks.navcoin;
2332 setHdCoin(130);
2333 },
2334 },
2335 {
2336 name: "NAS - Nebulas",
2337 onSelect: function() {
2338 network = bitcoinjs.bitcoin.networks.bitcoin;
2339 setHdCoin(2718);
2340 },
2341 },
2342 {
2343 name: "NEBL - Neblio",
2344 onSelect: function() {
2345 network = bitcoinjs.bitcoin.networks.neblio;
2346 setHdCoin(146);
2347 },
2348 },
2349 {
2350 name: "NEOS - Neoscoin",
2351 onSelect: function() {
2352 network = bitcoinjs.bitcoin.networks.neoscoin;
2353 setHdCoin(25);
2354 },
2355 },
2356 {
2357 name: "NIX - NIX Platform",
2358 onSelect: function() {
2359 network = bitcoinjs.bitcoin.networks.nix;
2360 setHdCoin(400);
2361 },
2362 },
2363 {
2364 name: "NLG - Gulden",
2365 onSelect: function() {
2366 network = bitcoinjs.bitcoin.networks.gulden;
2367 setHdCoin(87);
2368 },
2369 },
2370 {
2371 name: "NMC - Namecoin",
2372 onSelect: function() {
2373 network = bitcoinjs.bitcoin.networks.namecoin;
2374 setHdCoin(7);
2375 },
2376 },
2377 {
2378 name: "NRG - Energi",
2379 onSelect: function() {
2380 network = bitcoinjs.bitcoin.networks.energi;
2381 setHdCoin(204);
2382 },
2383 },
2384 {
2385 name: "NRO - Neurocoin",
2386 onSelect: function() {
2387 network = bitcoinjs.bitcoin.networks.neurocoin;
2388 setHdCoin(110);
2389 },
2390 },
2391 {
2392 name: "NSR - Nushares",
2393 onSelect: function() {
2394 network = bitcoinjs.bitcoin.networks.nushares;
2395 setHdCoin(11);
2396 },
2397 },
2398 {
2399 name: "NYC - Newyorkc",
2400 onSelect: function() {
2401 network = bitcoinjs.bitcoin.networks.newyorkc;
2402 setHdCoin(179);
2403 },
2404 },
2405 {
2406 name: "NVC - Novacoin",
2407 onSelect: function() {
2408 network = bitcoinjs.bitcoin.networks.novacoin;
2409 setHdCoin(50);
2410 },
2411 },
2412 {
2413 name: "OK - Okcash",
2414 onSelect: function() {
2415 network = bitcoinjs.bitcoin.networks.okcash;
2416 setHdCoin(69);
2417 },
2418 },
2419 {
2420 name: "OMNI - Omnicore",
2421 onSelect: function() {
2422 network = bitcoinjs.bitcoin.networks.omnicore;
2423 setHdCoin(200);
2424 },
2425 },
2426 {
2427 name: "ONION - DeepOnion",
2428 onSelect: function() {
2429 network = bitcoinjs.bitcoin.networks.deeponion;
2430 setHdCoin(305);
2431 },
2432 },
2433 {
2434 name: "ONX - Onixcoin",
2435 onSelect: function() {
2436 network = bitcoinjs.bitcoin.networks.onixcoin;
2437 setHdCoin(174);
2438 },
2439 },
2440 {
2441 name: "PHR - Phore",
2442 onSelect: function() {
2443 network = bitcoinjs.bitcoin.networks.phore;
2444 setHdCoin(444);
2445 },
2446 },
2447 {
2448 name: "PINK - Pinkcoin",
2449 onSelect: function() {
2450 network = bitcoinjs.bitcoin.networks.pinkcoin;
2451 setHdCoin(117);
2452 },
2453 },
2454 {
2455 name: "PIRL - Pirl",
2456 segwitAvailable: false,
2457 onSelect: function() {
2458 network = bitcoinjs.bitcoin.networks.bitcoin;
2459 setHdCoin(164);
2460 },
2461 },
2462 {
2463 name: "PIVX - PIVX",
2464 onSelect: function() {
2465 network = bitcoinjs.bitcoin.networks.pivx;
2466 setHdCoin(119);
2467 },
2468 },
2469 {
2470 name: "PIVX - PIVX Testnet",
2471 onSelect: function() {
2472 network = bitcoinjs.bitcoin.networks.pivxtestnet;
2473 setHdCoin(1);
2474 },
2475 },
2476 {
2477 name: "POA - Poa",
2478 segwitAvailable: false,
2479 onSelect: function() {
2480 network = bitcoinjs.bitcoin.networks.bitcoin;
2481 setHdCoin(178);
2482 },
2483 },
2484 {
2485 name: "POSW - POSWcoin",
2486 onSelect: function() {
2487 network = bitcoinjs.bitcoin.networks.poswcoin;
2488 setHdCoin(47);
2489 },
2490 },
2491 {
2492 name: "POT - Potcoin",
2493 onSelect: function() {
2494 network = bitcoinjs.bitcoin.networks.potcoin;
2495 setHdCoin(81);
2496 },
2497 },
2498 {
2499 name: "PPC - Peercoin",
2500 onSelect: function() {
2501 network = bitcoinjs.bitcoin.networks.peercoin;
2502 setHdCoin(6);
2503 },
2504 },
2505 {
2506 name: "PRJ - ProjectCoin",
2507 onSelect: function() {
2508 network = bitcoinjs.bitcoin.networks.projectcoin;
2509 setHdCoin(533);
2510 },
2511 },
2512 {
2513 name: "PSB - Pesobit",
2514 onSelect: function() {
2515 network = bitcoinjs.bitcoin.networks.pesobit;
2516 setHdCoin(62);
2517 },
2518 },
2519 {
2520 name: "PUT - Putincoin",
2521 onSelect: function() {
2522 network = bitcoinjs.bitcoin.networks.putincoin;
2523 setHdCoin(122);
2524 },
2525 },
2526 {
2527 name: "RVN - Ravencoin",
2528 onSelect: function() {
2529 network = bitcoinjs.bitcoin.networks.ravencoin;
2530 setHdCoin(175);
2531 },
2532 },
2533 {
2534 name: "RBY - Rubycoin",
2535 onSelect: function() {
2536 network = bitcoinjs.bitcoin.networks.rubycoin;
2537 setHdCoin(16);
2538 },
2539 },
2540 {
2541 name: "RDD - Reddcoin",
2542 onSelect: function() {
2543 network = bitcoinjs.bitcoin.networks.reddcoin;
2544 setHdCoin(4);
2545 },
2546 },
2547 {
2548 name: "RVR - RevolutionVR",
2549 onSelect: function() {
2550 network = bitcoinjs.bitcoin.networks.revolutionvr;
2551 setHdCoin(129);
2552 },
2553 },
2554 {
2555 name: "SAFE - Safecoin",
2556 onSelect: function() {
2557 network = bitcoinjs.bitcoin.networks.safecoin;
2558 setHdCoin(19165);
2559 },
2560 },
2561 {
2562 name: "SLS - Salus",
2563 onSelect: function() {
2564 network = bitcoinjs.bitcoin.networks.salus;
2565 setHdCoin(63);
2566 },
2567 },
2568 {
2569 name: "SDC - ShadowCash",
2570 onSelect: function() {
2571 network = bitcoinjs.bitcoin.networks.shadow;
2572 setHdCoin(35);
2573 },
2574 },
2575 {
2576 name: "SDC - ShadowCash Testnet",
2577 onSelect: function() {
2578 network = bitcoinjs.bitcoin.networks.shadowtn;
2579 setHdCoin(1);
2580 },
2581 },
2582 {
2583 name: "SLM - Slimcoin",
2584 onSelect: function() {
2585 network = bitcoinjs.bitcoin.networks.slimcoin;
2586 setHdCoin(63);
2587 },
2588 },
2589 {
2590 name: "SLM - Slimcoin Testnet",
2591 onSelect: function() {
2592 network = bitcoinjs.bitcoin.networks.slimcointn;
2593 setHdCoin(111);
2594 },
2595 },
2596 {
2597 name: "SLP - Simple Ledger Protocol",
2598 onSelect: function() {
2599 DOM.bitcoinCashAddressTypeContainer.removeClass("hidden");
2600 setHdCoin(245);
2601 },
2602 },
2603 {
2604 name: "SLR - Solarcoin",
2605 onSelect: function() {
2606 network = bitcoinjs.bitcoin.networks.solarcoin;
2607 setHdCoin(58);
2608 },
2609 },
2610 {
2611 name: "SMLY - Smileycoin",
2612 onSelect: function() {
2613 network = bitcoinjs.bitcoin.networks.smileycoin;
2614 setHdCoin(59);
2615 },
2616 },
2617 {
2618 name: "STASH - Stash",
2619 onSelect: function() {
2620 network = bitcoinjs.bitcoin.networks.stash;
2621 setHdCoin(0xC0C0);
2622 },
2623 },
2624 {
2625 name: "STASH - Stash Testnet",
2626 onSelect: function() {
2627 network = bitcoinjs.bitcoin.networks.stashtn;
2628 setHdCoin(0xCAFE);
2629 },
2630 },
2631 {
2632 name: "STRAT - Stratis",
2633 onSelect: function() {
2634 network = bitcoinjs.bitcoin.networks.stratis;
2635 setHdCoin(105);
2636 },
2637 },
2638 {
2639 name: "TSTRAT - Stratis Testnet",
2640 onSelect: function() {
2641 network = bitcoinjs.bitcoin.networks.stratistest;
2642 setHdCoin(105);
2643 },
2644 },
2645 {
2646 name: "SYS - Syscoin",
2647 onSelect: function() {
2648 network = bitcoinjs.bitcoin.networks.syscoin;
2649 setHdCoin(57);
2650 },
2651 },
2652 {
2653 name: "THC - Hempcoin",
2654 onSelect: function() {
2655 network = bitcoinjs.bitcoin.networks.hempcoin;
2656 setHdCoin(113);
2657 },
2658 },
2659 {
2660 name: "TOA - Toa",
2661 onSelect: function() {
2662 network = bitcoinjs.bitcoin.networks.toa;
2663 setHdCoin(159);
2664 },
2665 },
2666 {
2667 name: "TWINS - TWINS",
2668 onSelect: function() {
2669 network = bitcoinjs.bitcoin.networks.twins;
2670 setHdCoin(970);
2671 },
2672 },
2673 {
2674 name: "TWINS - TWINS Testnet",
2675 onSelect: function() {
2676 network = bitcoinjs.bitcoin.networks.twinstestnet;
2677 setHdCoin(1);
2678 },
2679 },
2680 {
2681 name: "USC - Ultimatesecurecash",
2682 onSelect: function() {
2683 network = bitcoinjs.bitcoin.networks.ultimatesecurecash;
2684 setHdCoin(112);
2685 },
2686 },
2687 {
2688 name: "USNBT - NuBits",
2689 onSelect: function() {
2690 network = bitcoinjs.bitcoin.networks.nubits;
2691 setHdCoin(12);
2692 },
2693 },
2694 {
2695 name: "UNO - Unobtanium",
2696 onSelect: function() {
2697 network = bitcoinjs.bitcoin.networks.unobtanium;
2698 setHdCoin(92);
2699 },
2700 },
2701 {
2702 name: "VASH - Vpncoin",
2703 onSelect: function() {
2704 network = bitcoinjs.bitcoin.networks.vpncoin;
2705 setHdCoin(33);
2706 },
2707 },
2708 {
2709 name: "VIA - Viacoin",
2710 onSelect: function() {
2711 network = bitcoinjs.bitcoin.networks.viacoin;
2712 setHdCoin(14);
2713 },
2714 },
2715 {
2716 name: "VIA - Viacoin Testnet",
2717 onSelect: function() {
2718 network = bitcoinjs.bitcoin.networks.viacointestnet;
2719 setHdCoin(1);
2720 },
2721 },
2722 {
2723 name: "VIVO - Vivo",
2724 onSelect: function() {
2725 network = bitcoinjs.bitcoin.networks.vivo;
2726 setHdCoin(166);
2727 },
2728 },
2729 {
2730 name: "VTC - Vertcoin",
2731 onSelect: function() {
2732 network = bitcoinjs.bitcoin.networks.vertcoin;
2733 setHdCoin(28);
2734 },
2735 },
2736 {
2737 name: "WC - Wincoin",
2738 onSelect: function() {
2739 network = bitcoinjs.bitcoin.networks.wincoin;
2740 setHdCoin(181);
2741 },
2742 },
2743 {
2744 name: "XAX - Artax",
2745 onSelect: function() {
2746 network = bitcoinjs.bitcoin.networks.artax;
2747 setHdCoin(219);
2748 },
2749 },
2750 {
2751 name: "XBC - Bitcoinplus",
2752 onSelect: function() {
2753 network = bitcoinjs.bitcoin.networks.bitcoinplus;
2754 setHdCoin(65);
2755 },
2756 },
2757 {
2758 name: "XLM - Stellar",
2759 onSelect: function() {
2760 network = stellarUtil.dummyNetwork;
2761 setHdCoin(148);
2762 },
2763 },
2764 {
2765 name: "XMY - Myriadcoin",
2766 onSelect: function() {
2767 network = bitcoinjs.bitcoin.networks.myriadcoin;
2768 setHdCoin(90);
2769 },
2770 },
2771 {
2772 name: "XRP - Ripple",
2773 onSelect: function() {
2774 network = bitcoinjs.bitcoin.networks.bitcoin;
2775 setHdCoin(144);
2776 },
2777 },
2778 {
2779 name: "XVC - Vcash",
2780 onSelect: function() {
2781 network = bitcoinjs.bitcoin.networks.vcash;
2782 setHdCoin(127);
2783 },
2784 },
2785 {
2786 name: "XVG - Verge",
2787 onSelect: function() {
2788 network = bitcoinjs.bitcoin.networks.verge;
2789 setHdCoin(77);
2790 },
2791 },
2792 {
2793 name: "XUEZ - Xuez",
2794 segwitAvailable: false,
2795 onSelect: function() {
2796 network = bitcoinjs.bitcoin.networks.xuez;
2797 setHdCoin(225);
2798 },
2799 },
2800 {
2801 name: "XWC - Whitecoin",
2802 onSelect: function() {
2803 network = bitcoinjs.bitcoin.networks.whitecoin;
2804 setHdCoin(155);
2805 },
2806 },
2807 {
2808 name: "XZC - Zcoin",
2809 onSelect: function() {
2810 network = bitcoinjs.bitcoin.networks.zcoin;
2811 setHdCoin(136);
2812 },
2813 },
2814 {
2815 name: "ZCL - Zclassic",
2816 onSelect: function() {
2817 network = bitcoinjs.bitcoin.networks.zclassic;
2818 setHdCoin(147);
2819 },
2820 },
2821 {
2822 name: "ZEC - Zcash",
2823 onSelect: function() {
2824 network = bitcoinjs.bitcoin.networks.zcash;
2825 setHdCoin(133);
2826 },
2827 },
2828 {
2829 name: "ZEN - Zencash",
2830 onSelect: function() {
2831 network = bitcoinjs.bitcoin.networks.zencash;
2832 setHdCoin(121);
2833 },
2834 },
2835 ]
2836
2837 var clients = [
2838 {
2839 name: "Bitcoin Core",
2840 onSelect: function() {
2841 DOM.bip32path.val("m/0'/0'");
2842 DOM.hardenedAddresses.prop('checked', true);
2843 },
2844 },
2845 {
2846 name: "blockchain.info",
2847 onSelect: function() {
2848 DOM.bip32path.val("m/44'/0'/0'");
2849 DOM.hardenedAddresses.prop('checked', false);
2850 },
2851 },
2852 {
2853 name: "MultiBit HD",
2854 onSelect: function() {
2855 DOM.bip32path.val("m/0'/0");
2856 DOM.hardenedAddresses.prop('checked', false);
2857 },
2858 },
2859 {
2860 name: "Coinomi, Ledger",
2861 onSelect: function() {
2862 DOM.bip32path.val("m/44'/"+DOM.bip44coin.val()+"'/0'");
2863 DOM.hardenedAddresses.prop('checked', false);
2864 },
2865 }
2866 ]
2867
2868 init();
2869
2870 })();