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