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