Merge pull request #352 from PavlosTze/bsv-rpd-monkey-arya
[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 || (networks[DOM.network.val()].name == "VET - VeChain")
925 ) {
926 var privKeyBuffer = keyPair.d.toBuffer(32);
927 privkey = privKeyBuffer.toString('hex');
928 var addressBuffer = ethUtil.privateToAddress(privKeyBuffer);
929 var hexAddress = addressBuffer.toString('hex');
930 var checksumAddress = ethUtil.toChecksumAddress(hexAddress);
931 address = ethUtil.addHexPrefix(checksumAddress);
932 privkey = ethUtil.addHexPrefix(privkey);
933 pubkey = ethUtil.addHexPrefix(pubkey);
934 }
935
936 // Stellar is different
937 if (networks[DOM.network.val()].name == "XLM - Stellar") {
938 var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44);
939 var coin = parseIntNoNaN(DOM.bip44coin.val(), 0);
940 var path = "m/";
941 path += purpose + "'/";
942 path += coin + "'/" + index + "'";
943 var keypair = stellarUtil.getKeypair(path, seed);
944 indexText = path;
945 privkey = keypair.secret();
946 pubkey = address = keypair.publicKey();
947 }
948 if ((networks[DOM.network.val()].name == "NAS - Nebulas")) {
949 var NasAccount = require("nebulas-account");
950 var privKeyBuffer = keyPair.d.toBuffer(32);
951 var nebulasAccount = new NasAccount();
952 nebulasAccount.setPrivateKey(privKeyBuffer);
953 address = nebulasAccount.getAddressString();
954 privkey = nebulasAccount.getPrivateKeyString();
955 pubkey = nebulasAccount.getPublicKeyString();
956 }
957 // Ripple values are different
958 if (networks[DOM.network.val()].name == "XRP - Ripple") {
959 privkey = convertRipplePriv(privkey);
960 address = convertRippleAdrr(address);
961 }
962 // CasinoCoin values are different
963 if (networks[DOM.network.val()].name == "CSC - CasinoCoin") {
964 privkey = convertCasinoCoinPriv(privkey);
965 address = convertCasinoCoinAdrr(address);
966 }
967 // Bitcoin Cash address format may vary
968 if (networks[DOM.network.val()].name == "BCH - Bitcoin Cash") {
969 var bchAddrType = DOM.bitcoinCashAddressType.filter(":checked").val();
970 if (bchAddrType == "cashaddr") {
971 address = bchaddr.toCashAddress(address);
972 }
973 else if (bchAddrType == "bitpay") {
974 address = bchaddr.toBitpayAddress(address);
975 }
976 }
977 // Bitcoin Cash address format may vary
978 if (networks[DOM.network.val()].name == "SLP - Simple Ledger Protocol") {
979 var bchAddrType = DOM.bitcoinCashAddressType.filter(":checked").val();
980 if (bchAddrType == "cashaddr") {
981 address = bchaddr.toSlpAddress(address);
982 }
983 }
984 // Segwit addresses are different
985 if (isSegwit) {
986 if (!segwitAvailable) {
987 return;
988 }
989 if (isP2wpkh) {
990 var keyhash = bitcoinjs.bitcoin.crypto.hash160(key.getPublicKeyBuffer());
991 var scriptpubkey = bitcoinjs.bitcoin.script.witnessPubKeyHash.output.encode(keyhash);
992 address = bitcoinjs.bitcoin.address.fromOutputScript(scriptpubkey, network)
993 }
994 else if (isP2wpkhInP2sh) {
995 var keyhash = bitcoinjs.bitcoin.crypto.hash160(key.getPublicKeyBuffer());
996 var scriptsig = bitcoinjs.bitcoin.script.witnessPubKeyHash.output.encode(keyhash);
997 var addressbytes = bitcoinjs.bitcoin.crypto.hash160(scriptsig);
998 var scriptpubkey = bitcoinjs.bitcoin.script.scriptHash.output.encode(addressbytes);
999 address = bitcoinjs.bitcoin.address.fromOutputScript(scriptpubkey, network)
1000 }
1001 }
1002
1003 if ((networks[DOM.network.val()].name == "CRW - Crown")) {
1004 address = bitcoinjs.bitcoin.networks.crown.toNewAddress(address);
1005 }
1006
1007 if (networks[DOM.network.val()].name == "EOS - EOSIO") {
1008 address = ""
1009 pubkey = eosUtil.bufferToPublic(keyPair.getPublicKeyBuffer());
1010 privkey = eosUtil.bufferToPrivate(keyPair.d.toBuffer(32));
1011 }
1012
1013 addAddressToList(indexText, address, pubkey, privkey);
1014 if (isLast) {
1015 hidePending();
1016 updateCsv();
1017 }
1018 }, 50)
1019 }
1020
1021 init();
1022
1023 }
1024
1025 function showMore() {
1026 var rowsToAdd = parseInt(DOM.rowsToAdd.val());
1027 if (isNaN(rowsToAdd)) {
1028 rowsToAdd = 20;
1029 DOM.rowsToAdd.val("20");
1030 }
1031 var start = parseInt(DOM.moreRowsStartIndex.val())
1032 if (isNaN(start)) {
1033 start = lastIndexInTable() + 1;
1034 }
1035 else {
1036 var newStart = start + rowsToAdd;
1037 DOM.moreRowsStartIndex.val(newStart);
1038 }
1039 if (rowsToAdd > 200) {
1040 var msg = "Generating " + rowsToAdd + " rows could take a while. ";
1041 msg += "Do you want to continue?";
1042 if (!confirm(msg)) {
1043 return;
1044 }
1045 }
1046 displayAddresses(start, rowsToAdd);
1047 }
1048
1049 function clearDisplay() {
1050 clearAddressesList();
1051 clearKeys();
1052 hideValidationError();
1053 }
1054
1055 function clearAddressesList() {
1056 DOM.addresses.empty();
1057 DOM.csv.val("");
1058 stopGenerating();
1059 }
1060
1061 function stopGenerating() {
1062 while (generationProcesses.length > 0) {
1063 var generation = generationProcesses.shift();
1064 generation.stop();
1065 }
1066 }
1067
1068 function clearKeys() {
1069 clearRootKey();
1070 clearDerivedKeys();
1071 }
1072
1073 function clearRootKey() {
1074 DOM.rootKey.val("");
1075 }
1076
1077 function clearDerivedKeys() {
1078 DOM.extendedPrivKey.val("");
1079 DOM.extendedPubKey.val("");
1080 DOM.bip44accountXprv.val("");
1081 DOM.bip44accountXpub.val("");
1082 }
1083
1084 function addAddressToList(indexText, address, pubkey, privkey) {
1085 var row = $(addressRowTemplate.html());
1086 // Elements
1087 var indexCell = row.find(".index span");
1088 var addressCell = row.find(".address span");
1089 var pubkeyCell = row.find(".pubkey span");
1090 var privkeyCell = row.find(".privkey span");
1091 // Content
1092 indexCell.text(indexText);
1093 addressCell.text(address);
1094 pubkeyCell.text(pubkey);
1095 privkeyCell.text(privkey);
1096 // Visibility
1097 if (!showIndex) {
1098 indexCell.addClass("invisible");
1099 }
1100 if (!showAddress) {
1101 addressCell.addClass("invisible");
1102 }
1103 if (!showPubKey) {
1104 pubkeyCell.addClass("invisible");
1105 }
1106 if (!showPrivKey) {
1107 privkeyCell.addClass("invisible");
1108 }
1109 DOM.addresses.append(row);
1110 var rowShowQrEls = row.find("[data-show-qr]");
1111 setQrEvents(rowShowQrEls);
1112 }
1113
1114 function hasStrongRandom() {
1115 return 'crypto' in window && window['crypto'] !== null;
1116 }
1117
1118 function disableForms() {
1119 $("form").on("submit", function(e) {
1120 e.preventDefault();
1121 });
1122 }
1123
1124 function parseIntNoNaN(val, defaultVal) {
1125 var v = parseInt(val);
1126 if (isNaN(v)) {
1127 return defaultVal;
1128 }
1129 return v;
1130 }
1131
1132 function showPending() {
1133 DOM.feedback
1134 .text("Calculating...")
1135 .show();
1136 }
1137
1138 function findNearestWord(word) {
1139 var language = getLanguage();
1140 var words = WORDLISTS[language];
1141 var minDistance = 99;
1142 var closestWord = words[0];
1143 for (var i=0; i<words.length; i++) {
1144 var comparedTo = words[i];
1145 if (comparedTo.indexOf(word) == 0) {
1146 return comparedTo;
1147 }
1148 var distance = Levenshtein.get(word, comparedTo);
1149 if (distance < minDistance) {
1150 closestWord = comparedTo;
1151 minDistance = distance;
1152 }
1153 }
1154 return closestWord;
1155 }
1156
1157 function hidePending() {
1158 DOM.feedback
1159 .text("")
1160 .hide();
1161 }
1162
1163 function populateNetworkSelect() {
1164 for (var i=0; i<networks.length; i++) {
1165 var network = networks[i];
1166 var option = $("<option>");
1167 option.attr("value", i);
1168 option.text(network.name);
1169 if (network.name == "BTC - Bitcoin") {
1170 option.prop("selected", true);
1171 }
1172 DOM.phraseNetwork.append(option);
1173 }
1174 }
1175
1176 function populateClientSelect() {
1177 for (var i=0; i<clients.length; i++) {
1178 var client = clients[i];
1179 var option = $("<option>");
1180 option.attr("value", i);
1181 option.text(client.name);
1182 DOM.bip32Client.append(option);
1183 }
1184 }
1185
1186 function getLanguage() {
1187 var defaultLanguage = "english";
1188 // Try to get from existing phrase
1189 var language = getLanguageFromPhrase();
1190 // Try to get from url if not from phrase
1191 if (language.length == 0) {
1192 language = getLanguageFromUrl();
1193 }
1194 // Default to English if no other option
1195 if (language.length == 0) {
1196 language = defaultLanguage;
1197 }
1198 return language;
1199 }
1200
1201 function getLanguageFromPhrase(phrase) {
1202 // Check if how many words from existing phrase match a language.
1203 var language = "";
1204 if (!phrase) {
1205 phrase = DOM.phrase.val();
1206 }
1207 if (phrase.length > 0) {
1208 var words = phraseToWordArray(phrase);
1209 var languageMatches = {};
1210 for (l in WORDLISTS) {
1211 // Track how many words match in this language
1212 languageMatches[l] = 0;
1213 for (var i=0; i<words.length; i++) {
1214 var wordInLanguage = WORDLISTS[l].indexOf(words[i]) > -1;
1215 if (wordInLanguage) {
1216 languageMatches[l]++;
1217 }
1218 }
1219 // Find languages with most word matches.
1220 // This is made difficult due to commonalities between Chinese
1221 // simplified vs traditional.
1222 var mostMatches = 0;
1223 var mostMatchedLanguages = [];
1224 for (var l in languageMatches) {
1225 var numMatches = languageMatches[l];
1226 if (numMatches > mostMatches) {
1227 mostMatches = numMatches;
1228 mostMatchedLanguages = [l];
1229 }
1230 else if (numMatches == mostMatches) {
1231 mostMatchedLanguages.push(l);
1232 }
1233 }
1234 }
1235 if (mostMatchedLanguages.length > 0) {
1236 // Use first language and warn if multiple detected
1237 language = mostMatchedLanguages[0];
1238 if (mostMatchedLanguages.length > 1) {
1239 console.warn("Multiple possible languages");
1240 console.warn(mostMatchedLanguages);
1241 }
1242 }
1243 }
1244 return language;
1245 }
1246
1247 function getLanguageFromUrl() {
1248 for (var language in WORDLISTS) {
1249 if (window.location.hash.indexOf(language) > -1) {
1250 return language;
1251 }
1252 }
1253 return "";
1254 }
1255
1256 function setMnemonicLanguage() {
1257 var language = getLanguage();
1258 // Load the bip39 mnemonic generator for this language if required
1259 if (!(language in mnemonics)) {
1260 mnemonics[language] = new Mnemonic(language);
1261 }
1262 mnemonic = mnemonics[language];
1263 }
1264
1265 function convertPhraseToNewLanguage() {
1266 var oldLanguage = getLanguageFromPhrase();
1267 var newLanguage = getLanguageFromUrl();
1268 var oldPhrase = DOM.phrase.val();
1269 var oldWords = phraseToWordArray(oldPhrase);
1270 var newWords = [];
1271 for (var i=0; i<oldWords.length; i++) {
1272 var oldWord = oldWords[i];
1273 var index = WORDLISTS[oldLanguage].indexOf(oldWord);
1274 var newWord = WORDLISTS[newLanguage][index];
1275 newWords.push(newWord);
1276 }
1277 newPhrase = wordArrayToPhrase(newWords);
1278 return newPhrase;
1279 }
1280
1281 // TODO look at jsbip39 - mnemonic.splitWords
1282 function phraseToWordArray(phrase) {
1283 var words = phrase.split(/\s/g);
1284 var noBlanks = [];
1285 for (var i=0; i<words.length; i++) {
1286 var word = words[i];
1287 if (word.length > 0) {
1288 noBlanks.push(word);
1289 }
1290 }
1291 return noBlanks;
1292 }
1293
1294 // TODO look at jsbip39 - mnemonic.joinWords
1295 function wordArrayToPhrase(words) {
1296 var phrase = words.join(" ");
1297 var language = getLanguageFromPhrase(phrase);
1298 if (language == "japanese") {
1299 phrase = words.join("\u3000");
1300 }
1301 return phrase;
1302 }
1303
1304 function isUsingOwnEntropy() {
1305 return DOM.useEntropy.prop("checked");
1306 }
1307
1308 function setMnemonicFromEntropy() {
1309 clearEntropyFeedback();
1310 // Get entropy value
1311 var entropyStr = DOM.entropy.val();
1312 // Work out minimum base for entropy
1313 var entropy = Entropy.fromString(entropyStr);
1314 if (entropy.binaryStr.length == 0) {
1315 return;
1316 }
1317 // Show entropy details
1318 showEntropyFeedback(entropy);
1319 // Use entropy hash if not using raw entropy
1320 var bits = entropy.binaryStr;
1321 var mnemonicLength = DOM.entropyMnemonicLength.val();
1322 if (mnemonicLength != "raw") {
1323 // Get bits by hashing entropy with SHA256
1324 var hash = sjcl.hash.sha256.hash(entropy.cleanStr);
1325 var hex = sjcl.codec.hex.fromBits(hash);
1326 bits = BigInteger.parse(hex, 16).toString(2);
1327 while (bits.length % 256 != 0) {
1328 bits = "0" + bits;
1329 }
1330 // Truncate hash to suit number of words
1331 mnemonicLength = parseInt(mnemonicLength);
1332 var numberOfBits = 32 * mnemonicLength / 3;
1333 bits = bits.substring(0, numberOfBits);
1334 // show warning for weak entropy override
1335 if (mnemonicLength / 3 * 32 > entropy.binaryStr.length) {
1336 DOM.entropyWeakEntropyOverrideWarning.removeClass("hidden");
1337 }
1338 else {
1339 DOM.entropyWeakEntropyOverrideWarning.addClass("hidden");
1340 }
1341 }
1342 else {
1343 // hide warning for weak entropy override
1344 DOM.entropyWeakEntropyOverrideWarning.addClass("hidden");
1345 }
1346 // Discard trailing entropy
1347 var bitsToUse = Math.floor(bits.length / 32) * 32;
1348 var start = bits.length - bitsToUse;
1349 var binaryStr = bits.substring(start);
1350 // Convert entropy string to numeric array
1351 var entropyArr = [];
1352 for (var i=0; i<binaryStr.length / 8; i++) {
1353 var byteAsBits = binaryStr.substring(i*8, i*8+8);
1354 var entropyByte = parseInt(byteAsBits, 2);
1355 entropyArr.push(entropyByte)
1356 }
1357 // Convert entropy array to mnemonic
1358 var phrase = mnemonic.toMnemonic(entropyArr);
1359 // Set the mnemonic in the UI
1360 DOM.phrase.val(phrase);
1361 // Show the word indexes
1362 showWordIndexes();
1363 // Show the checksum
1364 showChecksum();
1365 }
1366
1367 function clearEntropyFeedback() {
1368 DOM.entropyCrackTime.text("...");
1369 DOM.entropyType.text("");
1370 DOM.entropyWordCount.text("0");
1371 DOM.entropyEventCount.text("0");
1372 DOM.entropyBitsPerEvent.text("0");
1373 DOM.entropyBits.text("0");
1374 DOM.entropyFiltered.html("&nbsp;");
1375 DOM.entropyBinary.html("&nbsp;");
1376 }
1377
1378 function showEntropyFeedback(entropy) {
1379 var numberOfBits = entropy.binaryStr.length;
1380 var timeToCrack = "unknown";
1381 try {
1382 var z = zxcvbn(entropy.base.parts.join(""));
1383 timeToCrack = z.crack_times_display.offline_fast_hashing_1e10_per_second;
1384 if (z.feedback.warning != "") {
1385 timeToCrack = timeToCrack + " - " + z.feedback.warning;
1386 };
1387 }
1388 catch (e) {
1389 console.log("Error detecting entropy strength with zxcvbn:");
1390 console.log(e);
1391 }
1392 var entropyTypeStr = getEntropyTypeStr(entropy);
1393 var wordCount = Math.floor(numberOfBits / 32) * 3;
1394 var bitsPerEvent = entropy.bitsPerEvent.toFixed(2);
1395 var spacedBinaryStr = addSpacesEveryElevenBits(entropy.binaryStr);
1396 DOM.entropyFiltered.html(entropy.cleanHtml);
1397 DOM.entropyType.text(entropyTypeStr);
1398 DOM.entropyCrackTime.text(timeToCrack);
1399 DOM.entropyEventCount.text(entropy.base.ints.length);
1400 DOM.entropyBits.text(numberOfBits);
1401 DOM.entropyWordCount.text(wordCount);
1402 DOM.entropyBinary.text(spacedBinaryStr);
1403 DOM.entropyBitsPerEvent.text(bitsPerEvent);
1404 // detect and warn of filtering
1405 var rawNoSpaces = DOM.entropy.val().replace(/\s/g, "");
1406 var cleanNoSpaces = entropy.cleanStr.replace(/\s/g, "");
1407 var isFiltered = rawNoSpaces.length != cleanNoSpaces.length;
1408 if (isFiltered) {
1409 DOM.entropyFilterWarning.removeClass('hidden');
1410 }
1411 else {
1412 DOM.entropyFilterWarning.addClass('hidden');
1413 }
1414 }
1415
1416 function getEntropyTypeStr(entropy) {
1417 var typeStr = entropy.base.str;
1418 // Add some detail if these are cards
1419 if (entropy.base.asInt == 52) {
1420 var cardDetail = []; // array of message strings
1421 // Detect duplicates
1422 var dupes = [];
1423 var dupeTracker = {};
1424 for (var i=0; i<entropy.base.parts.length; i++) {
1425 var card = entropy.base.parts[i];
1426 var cardUpper = card.toUpperCase();
1427 if (cardUpper in dupeTracker) {
1428 dupes.push(card);
1429 }
1430 dupeTracker[cardUpper] = true;
1431 }
1432 if (dupes.length > 0) {
1433 var dupeWord = "duplicates";
1434 if (dupes.length == 1) {
1435 dupeWord = "duplicate";
1436 }
1437 var msg = dupes.length + " " + dupeWord + ": " + dupes.slice(0,3).join(" ");
1438 if (dupes.length > 3) {
1439 msg += "...";
1440 }
1441 cardDetail.push(msg);
1442 }
1443 // Detect full deck
1444 var uniqueCards = [];
1445 for (var uniqueCard in dupeTracker) {
1446 uniqueCards.push(uniqueCard);
1447 }
1448 if (uniqueCards.length == 52) {
1449 cardDetail.unshift("full deck");
1450 }
1451 // Detect missing cards
1452 var values = "A23456789TJQK";
1453 var suits = "CDHS";
1454 var missingCards = [];
1455 for (var i=0; i<suits.length; i++) {
1456 for (var j=0; j<values.length; j++) {
1457 var card = values[j] + suits[i];
1458 if (!(card in dupeTracker)) {
1459 missingCards.push(card);
1460 }
1461 }
1462 }
1463 // Display missing cards if six or less, ie clearly going for full deck
1464 if (missingCards.length > 0 && missingCards.length <= 6) {
1465 var msg = missingCards.length + " missing: " + missingCards.slice(0,3).join(" ");
1466 if (missingCards.length > 3) {
1467 msg += "...";
1468 }
1469 cardDetail.push(msg);
1470 }
1471 // Add card details to typeStr
1472 if (cardDetail.length > 0) {
1473 typeStr += " (" + cardDetail.join(", ") + ")";
1474 }
1475 }
1476 return typeStr;
1477 }
1478
1479 function setQrEvents(els) {
1480 els.on("mouseenter", createQr);
1481 els.on("mouseleave", destroyQr);
1482 els.on("click", toggleQr);
1483 }
1484
1485 function createQr(e) {
1486 var content = e.target.textContent || e.target.value;
1487 if (content) {
1488 var qrEl = kjua({
1489 text: content,
1490 render: "canvas",
1491 size: 310,
1492 ecLevel: 'H',
1493 });
1494 DOM.qrImage.append(qrEl);
1495 if (!showQr) {
1496 DOM.qrHider.addClass("hidden");
1497 }
1498 else {
1499 DOM.qrHider.removeClass("hidden");
1500 }
1501 DOM.qrContainer.removeClass("hidden");
1502 }
1503 }
1504
1505 function destroyQr() {
1506 DOM.qrImage.text("");
1507 DOM.qrContainer.addClass("hidden");
1508 }
1509
1510 function toggleQr() {
1511 showQr = !showQr;
1512 DOM.qrHider.toggleClass("hidden");
1513 DOM.qrHint.toggleClass("hidden");
1514 }
1515
1516 function bip44TabSelected() {
1517 return DOM.bip44tab.hasClass("active");
1518 }
1519
1520 function bip32TabSelected() {
1521 return DOM.bip32tab.hasClass("active");
1522 }
1523
1524 function networkHasSegwit() {
1525 var n = network;
1526 if ("baseNetwork" in network) {
1527 n = bitcoinjs.bitcoin.networks[network.baseNetwork];
1528 }
1529 // check if only p2wpkh params are required
1530 if (p2wpkhSelected()) {
1531 return "p2wpkh" in n;
1532 }
1533 // check if only p2wpkh-in-p2sh params are required
1534 else if (p2wpkhInP2shSelected()) {
1535 return "p2wpkhInP2sh" in n;
1536 }
1537 // require both if it's unclear which params are required
1538 return "p2wpkh" in n && "p2wpkhInP2sh" in n;
1539 }
1540
1541 function bip49TabSelected() {
1542 return DOM.bip49tab.hasClass("active");
1543 }
1544
1545 function bip84TabSelected() {
1546 return DOM.bip84tab.hasClass("active");
1547 }
1548
1549 function bip141TabSelected() {
1550 return DOM.bip141tab.hasClass("active");
1551 }
1552
1553 function setHdCoin(coinValue) {
1554 DOM.bip44coin.val(coinValue);
1555 DOM.bip49coin.val(coinValue);
1556 DOM.bip84coin.val(coinValue);
1557 }
1558
1559 function showSegwitAvailable() {
1560 DOM.bip49unavailable.addClass("hidden");
1561 DOM.bip49available.removeClass("hidden");
1562 DOM.bip84unavailable.addClass("hidden");
1563 DOM.bip84available.removeClass("hidden");
1564 DOM.bip141unavailable.addClass("hidden");
1565 DOM.bip141available.removeClass("hidden");
1566 }
1567
1568 function showSegwitUnavailable() {
1569 DOM.bip49available.addClass("hidden");
1570 DOM.bip49unavailable.removeClass("hidden");
1571 DOM.bip84available.addClass("hidden");
1572 DOM.bip84unavailable.removeClass("hidden");
1573 DOM.bip141available.addClass("hidden");
1574 DOM.bip141unavailable.removeClass("hidden");
1575 }
1576
1577 function adjustNetworkForSegwit() {
1578 // If segwit is selected the xpub/xprv prefixes need to be adjusted
1579 // to avoid accidentally importing BIP49 xpub to BIP44 watch only
1580 // wallet.
1581 // See https://github.com/iancoleman/bip39/issues/125
1582 var segwitNetworks = null;
1583 // if a segwit network is alread selected, need to use base network to
1584 // look up new parameters
1585 if ("baseNetwork" in network) {
1586 network = bitcoinjs.bitcoin.networks[network.baseNetwork];
1587 }
1588 // choose the right segwit params
1589 if (p2wpkhSelected() && "p2wpkh" in network) {
1590 network = network.p2wpkh;
1591 }
1592 else if (p2wpkhInP2shSelected() && "p2wpkhInP2sh" in network) {
1593 network = network.p2wpkhInP2sh;
1594 }
1595 }
1596
1597 function lastIndexInTable() {
1598 var pathText = DOM.addresses.find(".index").last().text();
1599 var pathBits = pathText.split("/");
1600 var lastBit = pathBits[pathBits.length-1];
1601 var lastBitClean = lastBit.replace("'", "");
1602 return parseInt(lastBitClean);
1603 }
1604
1605 function uint8ArrayToHex(a) {
1606 var s = ""
1607 for (var i=0; i<a.length; i++) {
1608 var h = a[i].toString(16);
1609 while (h.length < 2) {
1610 h = "0" + h;
1611 }
1612 s = s + h;
1613 }
1614 return s;
1615 }
1616
1617 function showWordIndexes() {
1618 var phrase = DOM.phrase.val();
1619 var words = phraseToWordArray(phrase);
1620 var wordIndexes = [];
1621 var language = getLanguage();
1622 for (var i=0; i<words.length; i++) {
1623 var word = words[i];
1624 var wordIndex = WORDLISTS[language].indexOf(word);
1625 wordIndexes.push(wordIndex);
1626 }
1627 var wordIndexesStr = wordIndexes.join(", ");
1628 DOM.entropyWordIndexes.text(wordIndexesStr);
1629 }
1630
1631 function showChecksum() {
1632 var phrase = DOM.phrase.val();
1633 var words = phraseToWordArray(phrase);
1634 var checksumBitlength = words.length / 3;
1635 var checksum = "";
1636 var binaryStr = "";
1637 var language = getLanguage();
1638 for (var i=words.length-1; i>=0; i--) {
1639 var word = words[i];
1640 var wordIndex = WORDLISTS[language].indexOf(word);
1641 var wordBinary = wordIndex.toString(2);
1642 while (wordBinary.length < 11) {
1643 wordBinary = "0" + wordBinary;
1644 }
1645 var binaryStr = wordBinary + binaryStr;
1646 if (binaryStr.length >= checksumBitlength) {
1647 var start = binaryStr.length - checksumBitlength;
1648 var end = binaryStr.length;
1649 checksum = binaryStr.substring(start, end);
1650 // add spaces so the last group is 11 bits, not the first
1651 checksum = checksum.split("").reverse().join("")
1652 checksum = addSpacesEveryElevenBits(checksum);
1653 checksum = checksum.split("").reverse().join("")
1654 break;
1655 }
1656 }
1657 DOM.entropyChecksum.text(checksum);
1658 }
1659
1660 function updateCsv() {
1661 var tableCsv = "path,address,public key,private key\n";
1662 var rows = DOM.addresses.find("tr");
1663 for (var i=0; i<rows.length; i++) {
1664 var row = $(rows[i]);
1665 var cells = row.find("td");
1666 for (var j=0; j<cells.length; j++) {
1667 var cell = $(cells[j]);
1668 if (!cell.children().hasClass("invisible")) {
1669 tableCsv = tableCsv + cell.text();
1670 }
1671 if (j != cells.length - 1) {
1672 tableCsv = tableCsv + ",";
1673 }
1674 }
1675 tableCsv = tableCsv + "\n";
1676 }
1677 DOM.csv.val(tableCsv);
1678 }
1679
1680 function addSpacesEveryElevenBits(binaryStr) {
1681 return binaryStr.match(/.{1,11}/g).join(" ");
1682 }
1683
1684 var networks = [
1685 {
1686 name: "AC - Asiacoin",
1687 onSelect: function() {
1688 network = bitcoinjs.bitcoin.networks.asiacoin;
1689 setHdCoin(51);
1690 },
1691 },
1692 {
1693 name: "ACC - Adcoin",
1694 onSelect: function() {
1695 network = bitcoinjs.bitcoin.networks.adcoin;
1696 setHdCoin(161);
1697 },
1698 },
1699 {
1700 name: "ARYA - Aryacoin",
1701 onSelect: function() {
1702 network = bitcoinjs.bitcoin.networks.aryacoin;
1703 setHdCoin(357);
1704 },
1705 },
1706 {
1707 name: "AUR - Auroracoin",
1708 onSelect: function() {
1709 network = bitcoinjs.bitcoin.networks.auroracoin;
1710 setHdCoin(85);
1711 },
1712 },
1713 {
1714 name: "AXE - Axe",
1715 onSelect: function() {
1716 network = bitcoinjs.bitcoin.networks.axe;
1717 setHdCoin(4242);
1718 },
1719 },
1720 {
1721 name: "ANON - ANON",
1722 onSelect: function() {
1723 network = bitcoinjs.bitcoin.networks.anon;
1724 setHdCoin(220);
1725 },
1726 },
1727 {
1728 name: "BOLI - Bolivarcoin",
1729 onSelect: function() {
1730 network = bitcoinjs.bitcoin.networks.bolivarcoin;
1731 setHdCoin(278);
1732 },
1733 },
1734 {
1735 name: "BCA - Bitcoin Atom",
1736 onSelect: function() {
1737 network = bitcoinjs.bitcoin.networks.atom;
1738 setHdCoin(185);
1739 },
1740 },
1741 {
1742 name: "BCH - Bitcoin Cash",
1743 onSelect: function() {
1744 DOM.bitcoinCashAddressTypeContainer.removeClass("hidden");
1745 setHdCoin(145);
1746 },
1747 },
1748 {
1749 name: "BEET - Beetlecoin",
1750 onSelect: function() {
1751 network = bitcoinjs.bitcoin.networks.beetlecoin;
1752 setHdCoin(800);
1753 },
1754 },
1755 {
1756 name: "BELA - Belacoin",
1757 onSelect: function() {
1758 network = bitcoinjs.bitcoin.networks.belacoin;
1759 setHdCoin(73);
1760 },
1761 },
1762 {
1763 name: "BLK - BlackCoin",
1764 onSelect: function() {
1765 network = bitcoinjs.bitcoin.networks.blackcoin;
1766 setHdCoin(10);
1767 },
1768 },
1769 {
1770 name: "BND - Blocknode",
1771 onSelect: function() {
1772 network = bitcoinjs.bitcoin.networks.blocknode;
1773 setHdCoin(2941);
1774 },
1775 },
1776 {
1777 name: "tBND - Blocknode Testnet",
1778 onSelect: function() {
1779 network = bitcoinjs.bitcoin.networks.blocknode_testnet;
1780 setHdCoin(1);
1781 },
1782 },
1783 {
1784 name: "BRIT - Britcoin",
1785 onSelect: function() {
1786 network = bitcoinjs.bitcoin.networks.britcoin;
1787 setHdCoin(70);
1788 },
1789 },
1790 {
1791 name: "BSD - Bitsend",
1792 onSelect: function() {
1793 network = bitcoinjs.bitcoin.networks.bitsend;
1794 setHdCoin(91);
1795 },
1796 },
1797 {
1798 name: "BST - BlockStamp",
1799 onSelect: function() {
1800 network = bitcoinjs.bitcoin.networks.blockstamp;
1801 setHdCoin(254);
1802 },
1803 },
1804 {
1805 name: "BTA - Bata",
1806 onSelect: function() {
1807 network = bitcoinjs.bitcoin.networks.bata;
1808 setHdCoin(89);
1809 },
1810 },
1811 {
1812 name: "BTC - Bitcoin",
1813 onSelect: function() {
1814 network = bitcoinjs.bitcoin.networks.bitcoin;
1815 setHdCoin(0);
1816 },
1817 },
1818 {
1819 name: "BTC - Bitcoin Testnet",
1820 onSelect: function() {
1821 network = bitcoinjs.bitcoin.networks.testnet;
1822 setHdCoin(1);
1823 },
1824 },
1825 {
1826 name: "BITG - Bitcoin Green",
1827 onSelect: function() {
1828 network = bitcoinjs.bitcoin.networks.bitcoingreen;
1829 setHdCoin(222);
1830 },
1831 },
1832 {
1833 name: "BTCP - Bitcoin Private",
1834 onSelect: function() {
1835 network = bitcoinjs.bitcoin.networks.bitcoinprivate;
1836 setHdCoin(183);
1837 },
1838 },
1839 {
1840 name: "BSV - BitcoinSV",
1841 onSelect: function() {
1842 network = bitcoinjs.bitcoin.networks.bitcoinsv;
1843 setHdCoin(236);
1844 },
1845 },
1846 {
1847 name: "BTCZ - Bitcoinz",
1848 onSelect: function() {
1849 network = bitcoinjs.bitcoin.networks.bitcoinz;
1850 setHdCoin(177);
1851 },
1852 },
1853 {
1854 name: "BTDX - BitCloud",
1855 onSelect: function() {
1856 network = bitcoinjs.bitcoin.networks.bitcloud;
1857 setHdCoin(218);
1858 },
1859 },
1860 {
1861 name: "BTG - Bitcoin Gold",
1862 onSelect: function() {
1863 network = bitcoinjs.bitcoin.networks.bgold;
1864 setHdCoin(156);
1865 },
1866 },
1867 {
1868 name: "BTX - Bitcore",
1869 onSelect: function() {
1870 network = bitcoinjs.bitcoin.networks.bitcore;
1871 setHdCoin(160);
1872 },
1873 },
1874 {
1875 name: "CCN - Cannacoin",
1876 onSelect: function() {
1877 network = bitcoinjs.bitcoin.networks.cannacoin;
1878 setHdCoin(19);
1879 },
1880 },
1881 {
1882 name: "CESC - Cryptoescudo",
1883 onSelect: function() {
1884 network = bitcoinjs.bitcoin.networks.cannacoin;
1885 setHdCoin(111);
1886 },
1887 },
1888 {
1889 name: "CDN - Canadaecoin",
1890 onSelect: function() {
1891 network = bitcoinjs.bitcoin.networks.canadaecoin;
1892 setHdCoin(34);
1893 },
1894 },
1895 {
1896 name: "CLAM - Clams",
1897 onSelect: function() {
1898 network = bitcoinjs.bitcoin.networks.clam;
1899 setHdCoin(23);
1900 },
1901 },
1902 {
1903 name: "CLO - Callisto",
1904 segwitAvailable: false,
1905 onSelect: function() {
1906 network = bitcoinjs.bitcoin.networks.bitcoin;
1907 setHdCoin(820);
1908 },
1909 },
1910 {
1911 name: "CLUB - Clubcoin",
1912 onSelect: function() {
1913 network = bitcoinjs.bitcoin.networks.clubcoin;
1914 setHdCoin(79);
1915 },
1916 },
1917 {
1918 name: "CMP - Compcoin",
1919 onSelect: function() {
1920 network = bitcoinjs.bitcoin.networks.compcoin;
1921 setHdCoin(71);
1922 },
1923 },
1924 {
1925 name: "CPU - CPUchain",
1926 onSelect: function() {
1927 network = bitcoinjs.bitcoin.networks.cpuchain;
1928 setHdCoin(363);
1929 },
1930 },
1931 {
1932 name: "CRAVE - Crave",
1933 onSelect: function() {
1934 network = bitcoinjs.bitcoin.networks.crave;
1935 setHdCoin(186);
1936 },
1937 },
1938 {
1939 name: "CRW - Crown (Legacy)",
1940 onSelect: function() {
1941 network = bitcoinjs.bitcoin.networks.crown;
1942 setHdCoin(72);
1943 },
1944 },
1945 {
1946 name: "CRW - Crown",
1947 onSelect: function() {
1948 network = bitcoinjs.bitcoin.networks.crown;
1949 setHdCoin(72);
1950 },
1951 },
1952 {
1953 name: "CSC - CasinoCoin",
1954 onSelect: function() {
1955 network = bitcoinjs.bitcoin.networks.bitcoin;
1956 setHdCoin(359);
1957 },
1958 },
1959 {
1960 name: "DASH - Dash",
1961 onSelect: function() {
1962 network = bitcoinjs.bitcoin.networks.dash;
1963 setHdCoin(5);
1964 },
1965 },
1966 {
1967 name: "DASH - Dash Testnet",
1968 onSelect: function() {
1969 network = bitcoinjs.bitcoin.networks.dashtn;
1970 setHdCoin(1);
1971 },
1972 },
1973 {
1974 name: "DFC - Defcoin",
1975 onSelect: function() {
1976 network = bitcoinjs.bitcoin.networks.defcoin;
1977 setHdCoin(1337);
1978 },
1979 },
1980 {
1981 name: "DGB - Digibyte",
1982 onSelect: function() {
1983 network = bitcoinjs.bitcoin.networks.digibyte;
1984 setHdCoin(20);
1985 },
1986 },
1987 {
1988 name: "DGC - Digitalcoin",
1989 onSelect: function() {
1990 network = bitcoinjs.bitcoin.networks.digitalcoin;
1991 setHdCoin(18);
1992 },
1993 },
1994 {
1995 name: "DMD - Diamond",
1996 onSelect: function() {
1997 network = bitcoinjs.bitcoin.networks.diamond;
1998 setHdCoin(152);
1999 },
2000 },
2001 {
2002 name: "DNR - Denarius",
2003 onSelect: function() {
2004 network = bitcoinjs.bitcoin.networks.denarius;
2005 setHdCoin(116);
2006 },
2007 },
2008 {
2009 name: "DOGE - Dogecoin",
2010 onSelect: function() {
2011 network = bitcoinjs.bitcoin.networks.dogecoin;
2012 setHdCoin(3);
2013 },
2014 },
2015 {
2016 name: "DOGEt - Dogecoin Testnet",
2017 onSelect: function() {
2018 network = bitcoinjs.bitcoin.networks.dogecointestnet;
2019 setHdCoin(1);
2020 },
2021 },
2022 {
2023 name: "DXN - DEXON",
2024 onSelect: function() {
2025 network = bitcoinjs.bitcoin.networks.bitcoin;
2026 setHdCoin(237);
2027 },
2028 },
2029 {
2030 name: "ECN - Ecoin",
2031 onSelect: function() {
2032 network = bitcoinjs.bitcoin.networks.ecoin;
2033 setHdCoin(115);
2034 },
2035 },
2036 {
2037 name: "EDRC - Edrcoin",
2038 onSelect: function() {
2039 network = bitcoinjs.bitcoin.networks.edrcoin;
2040 setHdCoin(56);
2041 },
2042 },
2043 {
2044 name: "EFL - Egulden",
2045 onSelect: function() {
2046 network = bitcoinjs.bitcoin.networks.egulden;
2047 setHdCoin(78);
2048 },
2049 },
2050 {
2051 name: "ELLA - Ellaism",
2052 segwitAvailable: false,
2053 onSelect: function() {
2054 network = bitcoinjs.bitcoin.networks.bitcoin;
2055 setHdCoin(163);
2056 },
2057 },
2058 {
2059 name: "EMC2 - Einsteinium",
2060 onSelect: function() {
2061 network = bitcoinjs.bitcoin.networks.einsteinium;
2062 setHdCoin(41);
2063 },
2064 },
2065 {
2066 name: "ERC - Europecoin",
2067 onSelect: function() {
2068 network = bitcoinjs.bitcoin.networks.europecoin;
2069 setHdCoin(151);
2070 },
2071 },
2072 {
2073 name: "EOS - EOSIO",
2074 onSelect: function() {
2075 network = bitcoinjs.bitcoin.networks.bitcoin;
2076 setHdCoin(194);
2077 },
2078 },
2079 {
2080 name: "ESN - Ethersocial Network",
2081 segwitAvailable: false,
2082 onSelect: function() {
2083 network = bitcoinjs.bitcoin.networks.bitcoin;
2084 setHdCoin(31102);
2085 },
2086 },
2087 {
2088 name: "ETC - Ethereum Classic",
2089 segwitAvailable: false,
2090 onSelect: function() {
2091 network = bitcoinjs.bitcoin.networks.bitcoin;
2092 setHdCoin(61);
2093 },
2094 },
2095 {
2096 name: "ETH - Ethereum",
2097 onSelect: function() {
2098 network = bitcoinjs.bitcoin.networks.bitcoin;
2099 setHdCoin(60);
2100 },
2101 },
2102 {
2103 name: "EXCL - Exclusivecoin",
2104 onSelect: function() {
2105 network = bitcoinjs.bitcoin.networks.exclusivecoin;
2106 setHdCoin(190);
2107 },
2108 },
2109 {
2110 name: "EXCC - ExchangeCoin",
2111 onSelect: function() {
2112 network = bitcoinjs.bitcoin.networks.exchangecoin;
2113 setHdCoin(0);
2114 },
2115 },
2116 {
2117 name: "EXP - Expanse",
2118 segwitAvailable: false,
2119 onSelect: function() {
2120 network = bitcoinjs.bitcoin.networks.bitcoin;
2121 setHdCoin(40);
2122 },
2123 },
2124 {
2125 name: "FIX - FIX",
2126 onSelect: function() {
2127 network = bitcoinjs.bitcoin.networks.fix;
2128 setHdCoin(336);
2129 },
2130 },
2131 {
2132 name: "FIX - FIX Testnet",
2133 onSelect: function() {
2134 network = bitcoinjs.bitcoin.networks.fixtestnet;
2135 setHdCoin(1);
2136 },
2137 },
2138 {
2139 name: "FJC - Fujicoin",
2140 onSelect: function() {
2141 network = bitcoinjs.bitcoin.networks.fujicoin;
2142 setHdCoin(75);
2143 },
2144 },
2145 {
2146 name: "FLASH - Flashcoin",
2147 onSelect: function() {
2148 network = bitcoinjs.bitcoin.networks.flashcoin;
2149 setHdCoin(120);
2150 },
2151 },
2152 {
2153 name: "FRST - Firstcoin",
2154 onSelect: function() {
2155 network = bitcoinjs.bitcoin.networks.firstcoin;
2156 setHdCoin(167);
2157 },
2158 },
2159 {
2160 name: "FTC - Feathercoin",
2161 onSelect: function() {
2162 network = bitcoinjs.bitcoin.networks.feathercoin;
2163 setHdCoin(8);
2164 },
2165 },
2166 {
2167 name: "GAME - GameCredits",
2168 onSelect: function() {
2169 network = bitcoinjs.bitcoin.networks.game;
2170 setHdCoin(101);
2171 },
2172 },
2173 {
2174 name: "GBX - Gobyte",
2175 onSelect: function() {
2176 network = bitcoinjs.bitcoin.networks.gobyte;
2177 setHdCoin(176);
2178 },
2179 },
2180 {
2181 name: "GCR - GCRCoin",
2182 onSelect: function() {
2183 network = bitcoinjs.bitcoin.networks.gcr;
2184 setHdCoin(79);
2185 },
2186 },
2187 {
2188 name: "GRC - Gridcoin",
2189 onSelect: function() {
2190 network = bitcoinjs.bitcoin.networks.gridcoin;
2191 setHdCoin(84);
2192 },
2193 },
2194 {
2195 name: "HNC - Helleniccoin",
2196 onSelect: function() {
2197 network = bitcoinjs.bitcoin.networks.helleniccoin;
2198 setHdCoin(168);
2199 },
2200 },
2201 {
2202 name: "HUSH - Hush (Legacy)",
2203 onSelect: function() {
2204 network = bitcoinjs.bitcoin.networks.hush;
2205 setHdCoin(197);
2206 },
2207 },
2208 {
2209 name: "HUSH - Hush3",
2210 onSelect: function() {
2211 network = bitcoinjs.bitcoin.networks.hush3;
2212 setHdCoin(197);
2213 },
2214 },
2215 {
2216 name: "INSN - Insane",
2217 onSelect: function() {
2218 network = bitcoinjs.bitcoin.networks.insane;
2219 setHdCoin(68);
2220 },
2221 },
2222 {
2223 name: "IOP - Iop",
2224 onSelect: function() {
2225 network = bitcoinjs.bitcoin.networks.iop;
2226 setHdCoin(66);
2227 },
2228 },
2229 {
2230 name: "IXC - Ixcoin",
2231 onSelect: function() {
2232 network = bitcoinjs.bitcoin.networks.ixcoin;
2233 setHdCoin(86);
2234 },
2235 },
2236 {
2237 name: "JBS - Jumbucks",
2238 onSelect: function() {
2239 network = bitcoinjs.bitcoin.networks.jumbucks;
2240 setHdCoin(26);
2241 },
2242 },
2243 {
2244 name: "KMD - Komodo",
2245 bip49available: false,
2246 onSelect: function() {
2247 network = bitcoinjs.bitcoin.networks.komodo;
2248 setHdCoin(141);
2249 },
2250 },
2251 {
2252 name: "KOBO - Kobocoin",
2253 bip49available: false,
2254 onSelect: function() {
2255 network = bitcoinjs.bitcoin.networks.kobocoin;
2256 setHdCoin(196);
2257 },
2258 },
2259 {
2260 name: "LBC - Library Credits",
2261 onSelect: function() {
2262 network = bitcoinjs.bitcoin.networks.lbry;
2263 setHdCoin(140);
2264 },
2265 },
2266 {
2267 name: "LCC - Litecoincash",
2268 onSelect: function() {
2269 network = bitcoinjs.bitcoin.networks.litecoincash;
2270 setHdCoin(192);
2271 },
2272 },
2273 {
2274 name: "LDCN - Landcoin",
2275 onSelect: function() {
2276 network = bitcoinjs.bitcoin.networks.landcoin;
2277 setHdCoin(63);
2278 },
2279 },
2280 {
2281 name: "LINX - Linx",
2282 onSelect: function() {
2283 network = bitcoinjs.bitcoin.networks.linx;
2284 setHdCoin(114);
2285 },
2286 },
2287 {
2288 name: "LKR - Lkrcoin",
2289 segwitAvailable: false,
2290 onSelect: function() {
2291 network = bitcoinjs.bitcoin.networks.lkrcoin;
2292 setHdCoin(557);
2293 },
2294 },
2295 {
2296 name: "LTC - Litecoin",
2297 onSelect: function() {
2298 network = bitcoinjs.bitcoin.networks.litecoin;
2299 setHdCoin(2);
2300 DOM.litecoinLtubContainer.removeClass("hidden");
2301 },
2302 },
2303 {
2304 name: "LTCt - Litecoin Testnet",
2305 onSelect: function() {
2306 network = bitcoinjs.bitcoin.networks.litecointestnet;
2307 setHdCoin(1);
2308 DOM.litecoinLtubContainer.removeClass("hidden");
2309 },
2310 },
2311 {
2312 name: "LTZ - LitecoinZ",
2313 onSelect: function() {
2314 network = bitcoinjs.bitcoin.networks.litecoinz;
2315 setHdCoin(221);
2316 },
2317 },
2318 {
2319 name: "LYNX - Lynx",
2320 onSelect: function() {
2321 network = bitcoinjs.bitcoin.networks.lynx;
2322 setHdCoin(191);
2323 },
2324 },
2325 {
2326 name: "MAZA - Maza",
2327 onSelect: function() {
2328 network = bitcoinjs.bitcoin.networks.maza;
2329 setHdCoin(13);
2330 },
2331 },
2332 {
2333 name: "MEC - Megacoin",
2334 onSelect: function() {
2335 network = bitcoinjs.bitcoin.networks.megacoin;
2336 setHdCoin(217);
2337 },
2338 },
2339 {
2340 name: "MIX - MIX",
2341 segwitAvailable: false,
2342 onSelect: function() {
2343 network = bitcoinjs.bitcoin.networks.bitcoin;
2344 setHdCoin(76);
2345 },
2346 },
2347 {
2348 name: "MNX - Minexcoin",
2349 onSelect: function() {
2350 network = bitcoinjs.bitcoin.networks.minexcoin;
2351 setHdCoin(182);
2352 },
2353 },
2354 {
2355 name: "MONA - Monacoin",
2356 onSelect: function() {
2357 network = bitcoinjs.bitcoin.networks.monacoin,
2358 setHdCoin(22);
2359 },
2360 },
2361 {
2362 name: "MONK - Monkey Project",
2363 onSelect: function() {
2364 network = bitcoinjs.bitcoin.networks.monkeyproject,
2365 setHdCoin(214);
2366 },
2367 },
2368 {
2369 name: "MUSIC - Musicoin",
2370 segwitAvailable: false,
2371 onSelect: function() {
2372 network = bitcoinjs.bitcoin.networks.bitcoin;
2373 setHdCoin(184);
2374 },
2375 },
2376 {
2377 name: "NAV - Navcoin",
2378 onSelect: function() {
2379 network = bitcoinjs.bitcoin.networks.navcoin;
2380 setHdCoin(130);
2381 },
2382 },
2383 {
2384 name: "NAS - Nebulas",
2385 onSelect: function() {
2386 network = bitcoinjs.bitcoin.networks.bitcoin;
2387 setHdCoin(2718);
2388 },
2389 },
2390 {
2391 name: "NEBL - Neblio",
2392 onSelect: function() {
2393 network = bitcoinjs.bitcoin.networks.neblio;
2394 setHdCoin(146);
2395 },
2396 },
2397 {
2398 name: "NEOS - Neoscoin",
2399 onSelect: function() {
2400 network = bitcoinjs.bitcoin.networks.neoscoin;
2401 setHdCoin(25);
2402 },
2403 },
2404 {
2405 name: "NIX - NIX Platform",
2406 onSelect: function() {
2407 network = bitcoinjs.bitcoin.networks.nix;
2408 setHdCoin(400);
2409 },
2410 },
2411 {
2412 name: "NLG - Gulden",
2413 onSelect: function() {
2414 network = bitcoinjs.bitcoin.networks.gulden;
2415 setHdCoin(87);
2416 },
2417 },
2418 {
2419 name: "NMC - Namecoin",
2420 onSelect: function() {
2421 network = bitcoinjs.bitcoin.networks.namecoin;
2422 setHdCoin(7);
2423 },
2424 },
2425 {
2426 name: "NRG - Energi",
2427 onSelect: function() {
2428 network = bitcoinjs.bitcoin.networks.energi;
2429 setHdCoin(204);
2430 },
2431 },
2432 {
2433 name: "NRO - Neurocoin",
2434 onSelect: function() {
2435 network = bitcoinjs.bitcoin.networks.neurocoin;
2436 setHdCoin(110);
2437 },
2438 },
2439 {
2440 name: "NSR - Nushares",
2441 onSelect: function() {
2442 network = bitcoinjs.bitcoin.networks.nushares;
2443 setHdCoin(11);
2444 },
2445 },
2446 {
2447 name: "NYC - Newyorkc",
2448 onSelect: function() {
2449 network = bitcoinjs.bitcoin.networks.newyorkc;
2450 setHdCoin(179);
2451 },
2452 },
2453 {
2454 name: "NVC - Novacoin",
2455 onSelect: function() {
2456 network = bitcoinjs.bitcoin.networks.novacoin;
2457 setHdCoin(50);
2458 },
2459 },
2460 {
2461 name: "OK - Okcash",
2462 onSelect: function() {
2463 network = bitcoinjs.bitcoin.networks.okcash;
2464 setHdCoin(69);
2465 },
2466 },
2467 {
2468 name: "OMNI - Omnicore",
2469 onSelect: function() {
2470 network = bitcoinjs.bitcoin.networks.omnicore;
2471 setHdCoin(200);
2472 },
2473 },
2474 {
2475 name: "ONION - DeepOnion",
2476 onSelect: function() {
2477 network = bitcoinjs.bitcoin.networks.deeponion;
2478 setHdCoin(305);
2479 },
2480 },
2481 {
2482 name: "ONX - Onixcoin",
2483 onSelect: function() {
2484 network = bitcoinjs.bitcoin.networks.onixcoin;
2485 setHdCoin(174);
2486 },
2487 },
2488 {
2489 name: "PHR - Phore",
2490 onSelect: function() {
2491 network = bitcoinjs.bitcoin.networks.phore;
2492 setHdCoin(444);
2493 },
2494 },
2495 {
2496 name: "PINK - Pinkcoin",
2497 onSelect: function() {
2498 network = bitcoinjs.bitcoin.networks.pinkcoin;
2499 setHdCoin(117);
2500 },
2501 },
2502 {
2503 name: "PIRL - Pirl",
2504 segwitAvailable: false,
2505 onSelect: function() {
2506 network = bitcoinjs.bitcoin.networks.bitcoin;
2507 setHdCoin(164);
2508 },
2509 },
2510 {
2511 name: "PIVX - PIVX",
2512 onSelect: function() {
2513 network = bitcoinjs.bitcoin.networks.pivx;
2514 setHdCoin(119);
2515 },
2516 },
2517 {
2518 name: "PIVX - PIVX Testnet",
2519 onSelect: function() {
2520 network = bitcoinjs.bitcoin.networks.pivxtestnet;
2521 setHdCoin(1);
2522 },
2523 },
2524 {
2525 name: "POA - Poa",
2526 segwitAvailable: false,
2527 onSelect: function() {
2528 network = bitcoinjs.bitcoin.networks.bitcoin;
2529 setHdCoin(178);
2530 },
2531 },
2532 {
2533 name: "POSW - POSWcoin",
2534 onSelect: function() {
2535 network = bitcoinjs.bitcoin.networks.poswcoin;
2536 setHdCoin(47);
2537 },
2538 },
2539 {
2540 name: "POT - Potcoin",
2541 onSelect: function() {
2542 network = bitcoinjs.bitcoin.networks.potcoin;
2543 setHdCoin(81);
2544 },
2545 },
2546 {
2547 name: "PPC - Peercoin",
2548 onSelect: function() {
2549 network = bitcoinjs.bitcoin.networks.peercoin;
2550 setHdCoin(6);
2551 },
2552 },
2553 {
2554 name: "PRJ - ProjectCoin",
2555 onSelect: function() {
2556 network = bitcoinjs.bitcoin.networks.projectcoin;
2557 setHdCoin(533);
2558 },
2559 },
2560 {
2561 name: "PSB - Pesobit",
2562 onSelect: function() {
2563 network = bitcoinjs.bitcoin.networks.pesobit;
2564 setHdCoin(62);
2565 },
2566 },
2567 {
2568 name: "PUT - Putincoin",
2569 onSelect: function() {
2570 network = bitcoinjs.bitcoin.networks.putincoin;
2571 setHdCoin(122);
2572 },
2573 },
2574 {
2575 name: "RPD - Rapids",
2576 onSelect: function() {
2577 network = bitcoinjs.bitcoin.networks.rapids;
2578 setHdCoin(320);
2579 },
2580 },
2581 {
2582 name: "RVN - Ravencoin",
2583 onSelect: function() {
2584 network = bitcoinjs.bitcoin.networks.ravencoin;
2585 setHdCoin(175);
2586 },
2587 },
2588 {
2589 name: "RBY - Rubycoin",
2590 onSelect: function() {
2591 network = bitcoinjs.bitcoin.networks.rubycoin;
2592 setHdCoin(16);
2593 },
2594 },
2595 {
2596 name: "RDD - Reddcoin",
2597 onSelect: function() {
2598 network = bitcoinjs.bitcoin.networks.reddcoin;
2599 setHdCoin(4);
2600 },
2601 },
2602 {
2603 name: "RVR - RevolutionVR",
2604 onSelect: function() {
2605 network = bitcoinjs.bitcoin.networks.revolutionvr;
2606 setHdCoin(129);
2607 },
2608 },
2609 {
2610 name: "SAFE - Safecoin",
2611 onSelect: function() {
2612 network = bitcoinjs.bitcoin.networks.safecoin;
2613 setHdCoin(19165);
2614 },
2615 },
2616 {
2617 name: "SLS - Salus",
2618 onSelect: function() {
2619 network = bitcoinjs.bitcoin.networks.salus;
2620 setHdCoin(63);
2621 },
2622 },
2623 {
2624 name: "SDC - ShadowCash",
2625 onSelect: function() {
2626 network = bitcoinjs.bitcoin.networks.shadow;
2627 setHdCoin(35);
2628 },
2629 },
2630 {
2631 name: "SDC - ShadowCash Testnet",
2632 onSelect: function() {
2633 network = bitcoinjs.bitcoin.networks.shadowtn;
2634 setHdCoin(1);
2635 },
2636 },
2637 {
2638 name: "SLM - Slimcoin",
2639 onSelect: function() {
2640 network = bitcoinjs.bitcoin.networks.slimcoin;
2641 setHdCoin(63);
2642 },
2643 },
2644 {
2645 name: "SLM - Slimcoin Testnet",
2646 onSelect: function() {
2647 network = bitcoinjs.bitcoin.networks.slimcointn;
2648 setHdCoin(111);
2649 },
2650 },
2651 {
2652 name: "SLP - Simple Ledger Protocol",
2653 onSelect: function() {
2654 DOM.bitcoinCashAddressTypeContainer.removeClass("hidden");
2655 setHdCoin(245);
2656 },
2657 },
2658 {
2659 name: "SLR - Solarcoin",
2660 onSelect: function() {
2661 network = bitcoinjs.bitcoin.networks.solarcoin;
2662 setHdCoin(58);
2663 },
2664 },
2665 {
2666 name: "SMLY - Smileycoin",
2667 onSelect: function() {
2668 network = bitcoinjs.bitcoin.networks.smileycoin;
2669 setHdCoin(59);
2670 },
2671 },
2672 {
2673 name: "STASH - Stash",
2674 onSelect: function() {
2675 network = bitcoinjs.bitcoin.networks.stash;
2676 setHdCoin(0xC0C0);
2677 },
2678 },
2679 {
2680 name: "STASH - Stash Testnet",
2681 onSelect: function() {
2682 network = bitcoinjs.bitcoin.networks.stashtn;
2683 setHdCoin(0xCAFE);
2684 },
2685 },
2686 {
2687 name: "STRAT - Stratis",
2688 onSelect: function() {
2689 network = bitcoinjs.bitcoin.networks.stratis;
2690 setHdCoin(105);
2691 },
2692 },
2693 {
2694 name: "TSTRAT - Stratis Testnet",
2695 onSelect: function() {
2696 network = bitcoinjs.bitcoin.networks.stratistest;
2697 setHdCoin(105);
2698 },
2699 },
2700 {
2701 name: "SYS - Syscoin",
2702 onSelect: function() {
2703 network = bitcoinjs.bitcoin.networks.syscoin;
2704 setHdCoin(57);
2705 },
2706 },
2707 {
2708 name: "THC - Hempcoin",
2709 onSelect: function() {
2710 network = bitcoinjs.bitcoin.networks.hempcoin;
2711 setHdCoin(113);
2712 },
2713 },
2714 {
2715 name: "TOA - Toa",
2716 onSelect: function() {
2717 network = bitcoinjs.bitcoin.networks.toa;
2718 setHdCoin(159);
2719 },
2720 },
2721 {
2722 name: "TWINS - TWINS",
2723 onSelect: function() {
2724 network = bitcoinjs.bitcoin.networks.twins;
2725 setHdCoin(970);
2726 },
2727 },
2728 {
2729 name: "TWINS - TWINS Testnet",
2730 onSelect: function() {
2731 network = bitcoinjs.bitcoin.networks.twinstestnet;
2732 setHdCoin(1);
2733 },
2734 },
2735 {
2736 name: "USC - Ultimatesecurecash",
2737 onSelect: function() {
2738 network = bitcoinjs.bitcoin.networks.ultimatesecurecash;
2739 setHdCoin(112);
2740 },
2741 },
2742 {
2743 name: "USNBT - NuBits",
2744 onSelect: function() {
2745 network = bitcoinjs.bitcoin.networks.nubits;
2746 setHdCoin(12);
2747 },
2748 },
2749 {
2750 name: "UNO - Unobtanium",
2751 onSelect: function() {
2752 network = bitcoinjs.bitcoin.networks.unobtanium;
2753 setHdCoin(92);
2754 },
2755 },
2756 {
2757 name: "VASH - Vpncoin",
2758 onSelect: function() {
2759 network = bitcoinjs.bitcoin.networks.vpncoin;
2760 setHdCoin(33);
2761 },
2762 },
2763 {
2764 name: "VET - VeChain",
2765 onSelect: function() {
2766 network = bitcoinjs.bitcoin.networks.bitcoin;
2767 setHdCoin(818);
2768 },
2769 },
2770 {
2771 name: "VIA - Viacoin",
2772 onSelect: function() {
2773 network = bitcoinjs.bitcoin.networks.viacoin;
2774 setHdCoin(14);
2775 },
2776 },
2777 {
2778 name: "VIA - Viacoin Testnet",
2779 onSelect: function() {
2780 network = bitcoinjs.bitcoin.networks.viacointestnet;
2781 setHdCoin(1);
2782 },
2783 },
2784 {
2785 name: "VIVO - Vivo",
2786 onSelect: function() {
2787 network = bitcoinjs.bitcoin.networks.vivo;
2788 setHdCoin(166);
2789 },
2790 },
2791 {
2792 name: "VTC - Vertcoin",
2793 onSelect: function() {
2794 network = bitcoinjs.bitcoin.networks.vertcoin;
2795 setHdCoin(28);
2796 },
2797 },
2798 {
2799 name: "WGR - Wagerr",
2800 onSelect: function() {
2801 network = bitcoinjs.bitcoin.networks.wagerr;
2802 setHdCoin(7825266);
2803 },
2804 },
2805 {
2806 name: "WC - Wincoin",
2807 onSelect: function() {
2808 network = bitcoinjs.bitcoin.networks.wincoin;
2809 setHdCoin(181);
2810 },
2811 },
2812 {
2813 name: "XAX - Artax",
2814 onSelect: function() {
2815 network = bitcoinjs.bitcoin.networks.artax;
2816 setHdCoin(219);
2817 },
2818 },
2819 {
2820 name: "XBC - Bitcoinplus",
2821 onSelect: function() {
2822 network = bitcoinjs.bitcoin.networks.bitcoinplus;
2823 setHdCoin(65);
2824 },
2825 },
2826 {
2827 name: "XLM - Stellar",
2828 onSelect: function() {
2829 network = stellarUtil.dummyNetwork;
2830 setHdCoin(148);
2831 },
2832 },
2833 {
2834 name: "XMY - Myriadcoin",
2835 onSelect: function() {
2836 network = bitcoinjs.bitcoin.networks.myriadcoin;
2837 setHdCoin(90);
2838 },
2839 },
2840 {
2841 name: "XRP - Ripple",
2842 onSelect: function() {
2843 network = bitcoinjs.bitcoin.networks.bitcoin;
2844 setHdCoin(144);
2845 },
2846 },
2847 {
2848 name: "XVC - Vcash",
2849 onSelect: function() {
2850 network = bitcoinjs.bitcoin.networks.vcash;
2851 setHdCoin(127);
2852 },
2853 },
2854 {
2855 name: "XVG - Verge",
2856 onSelect: function() {
2857 network = bitcoinjs.bitcoin.networks.verge;
2858 setHdCoin(77);
2859 },
2860 },
2861 {
2862 name: "XUEZ - Xuez",
2863 segwitAvailable: false,
2864 onSelect: function() {
2865 network = bitcoinjs.bitcoin.networks.xuez;
2866 setHdCoin(225);
2867 },
2868 },
2869 {
2870 name: "XWC - Whitecoin",