]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/blame - src/js/index.js
fix: calculate RSK addresses with EIP-155 compliant checksums
[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 }
c2e4f938 1142
a823e526 1143 // RSK values are different
1144 if (networkIsRsk()) {
1145 var pubkeyBuffer = keyPair.getPublicKeyBuffer();
1146 var ethPubkey = libs.ethUtil.importPublic(pubkeyBuffer);
1147 var addressBuffer = libs.ethUtil.publicToAddress(ethPubkey);
1148 var hexAddress = addressBuffer.toString('hex');
1149 // Use chainId based on selected network
1150 // Ref: https://developers.rsk.co/rsk/architecture/account-based/#chainid
1151 var chainId;
1152 var rskNetworkName = networks[DOM.network.val()].name;
1153 switch (rskNetworkName) {
1154 case "R-BTC - RSK":
1155 chainId = 30;
1156 break;
1157 case "tR-BTC - RSK Testnet":
1158 chainId = 31;
1159 break;
1160 default:
1161 chainId = null;
1162 }
1163 var checksumAddress = toChecksumAddressForRsk(hexAddress, chainId);
1164 address = libs.ethUtil.addHexPrefix(checksumAddress);
1165 pubkey = libs.ethUtil.addHexPrefix(pubkey);
1166 if (hasPrivkey) {
1167 privkey = libs.ethUtil.bufferToHex(keyPair.d.toBuffer());
1168 }
1169 }
1170
8ffa333b
SÖ
1171 // Stellar is different
1172 if (networks[DOM.network.val()].name == "XLM - Stellar") {
22c443df
SÖ
1173 var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44);
1174 var coin = parseIntNoNaN(DOM.bip44coin.val(), 0);
1175 var path = "m/";
1176 path += purpose + "'/";
1177 path += coin + "'/" + index + "'";
22f87669 1178 var keypair = libs.stellarUtil.getKeypair(path, seed);
8ffa333b
SÖ
1179 indexText = path;
1180 privkey = keypair.secret();
1181 pubkey = address = keypair.publicKey();
1182 }
7a113003 1183 if ((networks[DOM.network.val()].name == "NAS - Nebulas")) {
7a113003 1184 var privKeyBuffer = keyPair.d.toBuffer(32);
22f87669 1185 var nebulasAccount = libs.nebulas.Account.NewAccount();
491948db 1186 nebulasAccount.setPrivateKey(privKeyBuffer);
1187 address = nebulasAccount.getAddressString();
1188 privkey = nebulasAccount.getPrivateKeyString();
1189 pubkey = nebulasAccount.getPublicKeyString();
7a113003 1190 }
64a7d2aa 1191 // Ripple values are different
534481b6 1192 if (networks[DOM.network.val()].name == "XRP - Ripple") {
64a7d2aa 1193 privkey = convertRipplePriv(privkey);
1194 address = convertRippleAdrr(address);
1195 }
46a5338e
LL
1196 // Jingtum values are different
1197 if (networks[DOM.network.val()].name == "SWTC - Jingtum") {
1198 privkey = convertJingtumPriv(privkey);
1199 address = convertJingtumAdrr(address);
1200 }
45e40c28
C
1201 // CasinoCoin values are different
1202 if (networks[DOM.network.val()].name == "CSC - CasinoCoin") {
1203 privkey = convertCasinoCoinPriv(privkey);
1204 address = convertCasinoCoinAdrr(address);
1205 }
e0f91e20
IC
1206 // Bitcoin Cash address format may vary
1207 if (networks[DOM.network.val()].name == "BCH - Bitcoin Cash") {
1208 var bchAddrType = DOM.bitcoinCashAddressType.filter(":checked").val();
1209 if (bchAddrType == "cashaddr") {
22f87669 1210 address = libs.bchaddr.toCashAddress(address);
e0f91e20
IC
1211 }
1212 else if (bchAddrType == "bitpay") {
22f87669 1213 address = libs.bchaddr.toBitpayAddress(address);
e0f91e20
IC
1214 }
1215 }
d0889ab9 1216 // Bitcoin Cash address format may vary
1217 if (networks[DOM.network.val()].name == "SLP - Simple Ledger Protocol") {
1218 var bchAddrType = DOM.bitcoinCashAddressType.filter(":checked").val();
1219 if (bchAddrType == "cashaddr") {
22f87669 1220 address = libs.bchaddrSlp.toSlpAddress(address);
d0889ab9 1221 }
1222 }
0fd67b59
IC
1223 // Segwit addresses are different
1224 if (isSegwit) {
1225 if (!segwitAvailable) {
6c08f364
IC
1226 return;
1227 }
c49e8812 1228 if (isP2wpkh) {
22f87669
IC
1229 var keyhash = libs.bitcoin.crypto.hash160(key.getPublicKeyBuffer());
1230 var scriptpubkey = libs.bitcoin.script.witnessPubKeyHash.output.encode(keyhash);
1231 address = libs.bitcoin.address.fromOutputScript(scriptpubkey, network)
c49e8812
IC
1232 }
1233 else if (isP2wpkhInP2sh) {
22f87669
IC
1234 var keyhash = libs.bitcoin.crypto.hash160(key.getPublicKeyBuffer());
1235 var scriptsig = libs.bitcoin.script.witnessPubKeyHash.output.encode(keyhash);
1236 var addressbytes = libs.bitcoin.crypto.hash160(scriptsig);
1237 var scriptpubkey = libs.bitcoin.script.scriptHash.output.encode(addressbytes);
1238 address = libs.bitcoin.address.fromOutputScript(scriptpubkey, network)
c49e8812 1239 }
5c203fab 1240 else if (isP2wsh) {
22f87669 1241 // https://github.com/libs.bitcoinjs-lib/blob/v3.3.2/test/integration/addresses.js#L71
f7e9fdf0 1242 // This is a 1-of-1
22f87669
IC
1243 var witnessScript = libs.bitcoin.script.multisig.output.encode(1, [key.getPublicKeyBuffer()]);
1244 var scriptPubKey = libs.bitcoin.script.witnessScriptHash.output.encode(libs.bitcoin.crypto.sha256(witnessScript));
1245 address = libs.bitcoin.address.fromOutputScript(scriptPubKey, network);
5c203fab
IC
1246 }
1247 else if (isP2wshInP2sh) {
22f87669 1248 // https://github.com/libs.bitcoinjs-lib/blob/v3.3.2/test/integration/transactions.js#L183
f7e9fdf0 1249 // This is a 1-of-1
22f87669
IC
1250 var witnessScript = libs.bitcoin.script.multisig.output.encode(1, [key.getPublicKeyBuffer()]);
1251 var redeemScript = libs.bitcoin.script.witnessScriptHash.output.encode(libs.bitcoin.crypto.sha256(witnessScript));
1252 var scriptPubKey = libs.bitcoin.script.scriptHash.output.encode(libs.bitcoin.crypto.hash160(redeemScript));
1253 address = libs.bitcoin.address.fromOutputScript(scriptPubKey, network)
5c203fab 1254 }
6c08f364 1255 }
b85da074 1256
bf13c95f 1257 if ((networks[DOM.network.val()].name == "CRW - Crown")) {
22f87669 1258 address = libs.bitcoin.networks.crown.toNewAddress(address);
bf13c95f 1259 }
b85da074 1260
7d68d547 1261 if (networks[DOM.network.val()].name == "EOS - EOSIO") {
3451d1a8
JU
1262 address = ""
1263 pubkey = eosUtil.bufferToPublic(keyPair.getPublicKeyBuffer());
1264 privkey = eosUtil.bufferToPrivate(keyPair.d.toBuffer(32));
1265 }
82e3938a 1266
76120cb0
H
1267 //Groestlcoin Addresses are different
1268 if(isGRS()) {
1269
1270 if (isSegwit) {
1271 if (!segwitAvailable) {
1272 return;
1273 }
1274 if (isP2wpkh) {
22f87669 1275 address = libs.groestlcoinjs.address.fromOutputScript(scriptpubkey, network)
76120cb0
H
1276 }
1277 else if (isP2wpkhInP2sh) {
22f87669 1278 address = libs.groestlcoinjs.address.fromOutputScript(scriptpubkey, network)
76120cb0 1279 }
75138bee 1280 }
76120cb0
H
1281 //non-segwit addresses are handled by using groestlcoinjs for bip32RootKey
1282 }
1283
36523e0d
NJ
1284 if (isELA()) {
1285 let elaAddress = calcAddressForELA(
1286 seed,
1287 parseIntNoNaN(DOM.bip44coin.val(), 0),
1288 parseIntNoNaN(DOM.bip44account.val(), 0),
1289 parseIntNoNaN(DOM.bip44change.val(), 0),
1290 index
1291 );
1292 address = elaAddress.address;
1293 privkey = elaAddress.privateKey;
1294 pubkey = elaAddress.publicKey;
1295 }
1296
1b12b2f5 1297 addAddressToList(indexText, address, pubkey, privkey);
0eda54f5
IC
1298 if (isLast) {
1299 hidePending();
a78f4e28 1300 updateCsv();
0eda54f5 1301 }
a8c45487
IC
1302 }, 50)
1303 }
1304
1305 init();
1306
1307 }
1308
ebd8d4e8 1309 function showMore() {
ebd8d4e8
IC
1310 var rowsToAdd = parseInt(DOM.rowsToAdd.val());
1311 if (isNaN(rowsToAdd)) {
1312 rowsToAdd = 20;
1313 DOM.rowsToAdd.val("20");
1314 }
9183f9f6
IC
1315 var start = parseInt(DOM.moreRowsStartIndex.val())
1316 if (isNaN(start)) {
1317 start = lastIndexInTable() + 1;
1318 }
1319 else {
1320 var newStart = start + rowsToAdd;
1321 DOM.moreRowsStartIndex.val(newStart);
1322 }
ebd8d4e8
IC
1323 if (rowsToAdd > 200) {
1324 var msg = "Generating " + rowsToAdd + " rows could take a while. ";
1325 msg += "Do you want to continue?";
1326 if (!confirm(msg)) {
1327 return;
1328 }
1329 }
ebd8d4e8 1330 displayAddresses(start, rowsToAdd);
ebd8d4e8
IC
1331 }
1332
1333 function clearDisplay() {
1334 clearAddressesList();
fa2e4e93 1335 clearKeys();
ebd8d4e8
IC
1336 hideValidationError();
1337 }
1338
1339 function clearAddressesList() {
1340 DOM.addresses.empty();
a78f4e28 1341 DOM.csv.val("");
40892aba
IC
1342 stopGenerating();
1343 }
1344
1345 function stopGenerating() {
1346 while (generationProcesses.length > 0) {
1347 var generation = generationProcesses.shift();
1348 generation.stop();
1349 }
ebd8d4e8
IC
1350 }
1351
fa2e4e93
IC
1352 function clearKeys() {
1353 clearRootKey();
1354 clearDerivedKeys();
1355 }
1356
1357 function clearRootKey() {
ebd8d4e8 1358 DOM.rootKey.val("");
fa2e4e93
IC
1359 }
1360
1361 function clearDerivedKeys() {
ebd8d4e8
IC
1362 DOM.extendedPrivKey.val("");
1363 DOM.extendedPubKey.val("");
fa2e4e93
IC
1364 DOM.bip44accountXprv.val("");
1365 DOM.bip44accountXpub.val("");
ebd8d4e8
IC
1366 }
1367
1b12b2f5 1368 function addAddressToList(indexText, address, pubkey, privkey) {
ebd8d4e8 1369 var row = $(addressRowTemplate.html());
700901cd
IC
1370 // Elements
1371 var indexCell = row.find(".index span");
1372 var addressCell = row.find(".address span");
1b12b2f5 1373 var pubkeyCell = row.find(".pubkey span");
700901cd
IC
1374 var privkeyCell = row.find(".privkey span");
1375 // Content
ae30fed8 1376 indexCell.text(indexText);
700901cd 1377 addressCell.text(address);
1b12b2f5 1378 pubkeyCell.text(pubkey);
700901cd
IC
1379 privkeyCell.text(privkey);
1380 // Visibility
1381 if (!showIndex) {
1382 indexCell.addClass("invisible");
1383 }
1384 if (!showAddress) {
1385 addressCell.addClass("invisible");
1386 }
1b12b2f5
IC
1387 if (!showPubKey) {
1388 pubkeyCell.addClass("invisible");
1389 }
700901cd 1390 if (!showPrivKey) {
6d628db7 1391 privkeyCell.addClass("invisible");
700901cd 1392 }
ebd8d4e8 1393 DOM.addresses.append(row);
e00964cc
IC
1394 var rowShowQrEls = row.find("[data-show-qr]");
1395 setQrEvents(rowShowQrEls);
ebd8d4e8
IC
1396 }
1397
1398 function hasStrongRandom() {
1399 return 'crypto' in window && window['crypto'] !== null;
1400 }
1401
1402 function disableForms() {
1403 $("form").on("submit", function(e) {
1404 e.preventDefault();
1405 });
1406 }
1407
ebd8d4e8
IC
1408 function parseIntNoNaN(val, defaultVal) {
1409 var v = parseInt(val);
1410 if (isNaN(v)) {
1411 return defaultVal;
1412 }
1413 return v;
1414 }
1415
1416 function showPending() {
1417 DOM.feedback
1418 .text("Calculating...")
1419 .show();
1420 }
1421
563e401a 1422 function findNearestWord(word) {
5ee7bb9e
IC
1423 var language = getLanguage();
1424 var words = WORDLISTS[language];
563e401a
IC
1425 var minDistance = 99;
1426 var closestWord = words[0];
1427 for (var i=0; i<words.length; i++) {
1428 var comparedTo = words[i];
6ea15134
IC
1429 if (comparedTo.indexOf(word) == 0) {
1430 return comparedTo;
1431 }
22f87669 1432 var distance = libs.levenshtein.get(word, comparedTo);
563e401a
IC
1433 if (distance < minDistance) {
1434 closestWord = comparedTo;
1435 minDistance = distance;
1436 }
1437 }
1438 return closestWord;
1439 }
1440
ebd8d4e8
IC
1441 function hidePending() {
1442 DOM.feedback
1443 .text("")
1444 .hide();
1445 }
1446
7f15cb6e
IC
1447 function populateNetworkSelect() {
1448 for (var i=0; i<networks.length; i++) {
1449 var network = networks[i];
1450 var option = $("<option>");
1451 option.attr("value", i);
1452 option.text(network.name);
7b742f87
IC
1453 if (network.name == "BTC - Bitcoin") {
1454 option.prop("selected", true);
1455 }
7f15cb6e
IC
1456 DOM.phraseNetwork.append(option);
1457 }
1458 }
29bf60f5 1459
b4fd763c
AG
1460 function populateClientSelect() {
1461 for (var i=0; i<clients.length; i++) {
1462 var client = clients[i];
1463 var option = $("<option>");
1464 option.attr("value", i);
1465 option.text(client.name);
29bf60f5 1466 DOM.bip32Client.append(option);
b4fd763c
AG
1467 }
1468 }
7f15cb6e 1469
5ee7bb9e
IC
1470 function getLanguage() {
1471 var defaultLanguage = "english";
1472 // Try to get from existing phrase
1473 var language = getLanguageFromPhrase();
1474 // Try to get from url if not from phrase
1475 if (language.length == 0) {
1476 language = getLanguageFromUrl();
1477 }
1478 // Default to English if no other option
1479 if (language.length == 0) {
1480 language = defaultLanguage;
1481 }
1482 return language;
1483 }
1484
1485 function getLanguageFromPhrase(phrase) {
1486 // Check if how many words from existing phrase match a language.
1487 var language = "";
1488 if (!phrase) {
1489 phrase = DOM.phrase.val();
1490 }
1491 if (phrase.length > 0) {
1492 var words = phraseToWordArray(phrase);
1493 var languageMatches = {};
1494 for (l in WORDLISTS) {
1495 // Track how many words match in this language
1496 languageMatches[l] = 0;
1497 for (var i=0; i<words.length; i++) {
1498 var wordInLanguage = WORDLISTS[l].indexOf(words[i]) > -1;
1499 if (wordInLanguage) {
1500 languageMatches[l]++;
1501 }
1502 }
1503 // Find languages with most word matches.
1504 // This is made difficult due to commonalities between Chinese
1505 // simplified vs traditional.
1506 var mostMatches = 0;
1507 var mostMatchedLanguages = [];
1508 for (var l in languageMatches) {
1509 var numMatches = languageMatches[l];
1510 if (numMatches > mostMatches) {
1511 mostMatches = numMatches;
1512 mostMatchedLanguages = [l];
1513 }
1514 else if (numMatches == mostMatches) {
1515 mostMatchedLanguages.push(l);
1516 }
1517 }
1518 }
1519 if (mostMatchedLanguages.length > 0) {
1520 // Use first language and warn if multiple detected
1521 language = mostMatchedLanguages[0];
1522 if (mostMatchedLanguages.length > 1) {
1523 console.warn("Multiple possible languages");
1524 console.warn(mostMatchedLanguages);
1525 }
1526 }
1527 }
1528 return language;
1529 }
1530
1531 function getLanguageFromUrl() {
c6624d51
IC
1532 for (var language in WORDLISTS) {
1533 if (window.location.hash.indexOf(language) > -1) {
1534 return language;
1535 }
1536 }
1537 return "";
5ee7bb9e
IC
1538 }
1539
1540 function setMnemonicLanguage() {
1541 var language = getLanguage();
1542 // Load the bip39 mnemonic generator for this language if required
1543 if (!(language in mnemonics)) {
1544 mnemonics[language] = new Mnemonic(language);
1545 }
1546 mnemonic = mnemonics[language];
1547 }
1548
1549 function convertPhraseToNewLanguage() {
1550 var oldLanguage = getLanguageFromPhrase();
1551 var newLanguage = getLanguageFromUrl();
1552 var oldPhrase = DOM.phrase.val();
1553 var oldWords = phraseToWordArray(oldPhrase);
1554 var newWords = [];
1555 for (var i=0; i<oldWords.length; i++) {
1556 var oldWord = oldWords[i];
1557 var index = WORDLISTS[oldLanguage].indexOf(oldWord);
1558 var newWord = WORDLISTS[newLanguage][index];
1559 newWords.push(newWord);
1560 }
1561 newPhrase = wordArrayToPhrase(newWords);
1562 return newPhrase;
1563 }
1564
1565 // TODO look at jsbip39 - mnemonic.splitWords
1566 function phraseToWordArray(phrase) {
1567 var words = phrase.split(/\s/g);
1568 var noBlanks = [];
1569 for (var i=0; i<words.length; i++) {
1570 var word = words[i];
1571 if (word.length > 0) {
1572 noBlanks.push(word);
1573 }
1574 }
1575 return noBlanks;
1576 }
1577
1578 // TODO look at jsbip39 - mnemonic.joinWords
1579 function wordArrayToPhrase(words) {
1580 var phrase = words.join(" ");
1581 var language = getLanguageFromPhrase(phrase);
956e44ef 1582 if (language == "japanese") {
5ee7bb9e
IC
1583 phrase = words.join("\u3000");
1584 }
1585 return phrase;
1586 }
96ee8ab0
IC
1587
1588 function writeSplitPhrase(phrase) {
d93f2ba9
IC
1589 var wordCount = phrase.split(/\s/g).length;
1590 var left=[];
1591 for (var i=0;i<wordCount;i++) left.push(i);
1592 var group=[[],[],[]],
1593 groupI=-1;
1594 var seed = Math.abs(sjcl.hash.sha256.hash(phrase)[0])% 2147483647;
1595 while (left.length>0) {
1596 groupI=(groupI+1)%3;
1597 seed = seed * 16807 % 2147483647;
1598 var selected=Math.floor(left.length*(seed - 1) / 2147483646);
1599 group[groupI].push(left[selected]);
1600 left.splice(selected,1);
1601 }
1602 var cards=[phrase.split(/\s/g),phrase.split(/\s/g),phrase.split(/\s/g)];
1603 for (var i=0;i<3;i++) {
1604 for (var ii=0;ii<wordCount/3;ii++) cards[i][group[i][ii]]='XXXX';
1605 cards[i]='Card '+(i+1)+': '+wordArrayToPhrase(cards[i]);
1606 }
f8fae68e 1607 DOM.phraseSplit.val(cards.join("\r\n"));
d93f2ba9
IC
1608 var triesPerSecond=10000000000;
1609 var hackTime=Math.pow(2,wordCount*10/3)/triesPerSecond;
ea2cb91a 1610 var displayRedText = false;
96ee8ab0
IC
1611 if (hackTime<1) {
1612 hackTime="<1 second";
ea2cb91a 1613 displayRedText = true;
96ee8ab0
IC
1614 } else if (hackTime<86400) {
1615 hackTime=Math.floor(hackTime)+" seconds";
ea2cb91a 1616 displayRedText = true;
96ee8ab0
IC
1617 } else if(hackTime<31557600) {
1618 hackTime=Math.floor(hackTime/86400)+" days";
ea2cb91a 1619 displayRedText = true;
96ee8ab0
IC
1620 } else {
1621 hackTime=Math.floor(hackTime/31557600)+" years";
1622 }
1623 DOM.phraseSplitWarn.html("Time to hack with only one card: "+hackTime);
ea2cb91a
IC
1624 if (displayRedText) {
1625 DOM.phraseSplitWarn.addClass("text-danger");
1626 } else {
1627 DOM.phraseSplitWarn.removeClass("text-danger");
1628 }
96ee8ab0 1629 }
5ee7bb9e 1630
c6624d51
IC
1631 function isUsingOwnEntropy() {
1632 return DOM.useEntropy.prop("checked");
1633 }
1634
1635 function setMnemonicFromEntropy() {
0a84fe6a 1636 clearEntropyFeedback();
057722b0 1637 // Get entropy value
c6624d51 1638 var entropyStr = DOM.entropy.val();
057722b0 1639 // Work out minimum base for entropy
516c16d7
IC
1640 var entropy = null;
1641 if (entropyTypeAutoDetect) {
1642 entropy = Entropy.fromString(entropyStr);
1643 }
1644 else {
1645 let base = DOM.entropyTypeInputs.filter(":checked").val();
1646 entropy = Entropy.fromString(entropyStr, base);
1647 }
057722b0 1648 if (entropy.binaryStr.length == 0) {
c6624d51
IC
1649 return;
1650 }
1651 // Show entropy details
1cf1bbaf 1652 showEntropyFeedback(entropy);
3599674d
IC
1653 // Use entropy hash if not using raw entropy
1654 var bits = entropy.binaryStr;
1655 var mnemonicLength = DOM.entropyMnemonicLength.val();
1656 if (mnemonicLength != "raw") {
1657 // Get bits by hashing entropy with SHA256
1658 var hash = sjcl.hash.sha256.hash(entropy.cleanStr);
1659 var hex = sjcl.codec.hex.fromBits(hash);
22f87669 1660 bits = libs.BigInteger.BigInteger.parse(hex, 16).toString(2);
53aaab27 1661 while (bits.length % 256 != 0) {
3599674d
IC
1662 bits = "0" + bits;
1663 }
1664 // Truncate hash to suit number of words
1665 mnemonicLength = parseInt(mnemonicLength);
1666 var numberOfBits = 32 * mnemonicLength / 3;
1667 bits = bits.substring(0, numberOfBits);
645945a0
IC
1668 // show warning for weak entropy override
1669 if (mnemonicLength / 3 * 32 > entropy.binaryStr.length) {
1670 DOM.entropyWeakEntropyOverrideWarning.removeClass("hidden");
1671 }
1672 else {
1673 DOM.entropyWeakEntropyOverrideWarning.addClass("hidden");
1674 }
1675 }
1676 else {
1677 // hide warning for weak entropy override
1678 DOM.entropyWeakEntropyOverrideWarning.addClass("hidden");
3599674d 1679 }
c6624d51 1680 // Discard trailing entropy
3599674d 1681 var bitsToUse = Math.floor(bits.length / 32) * 32;
d6fd8ebf
IC
1682 var start = bits.length - bitsToUse;
1683 var binaryStr = bits.substring(start);
c6624d51
IC
1684 // Convert entropy string to numeric array
1685 var entropyArr = [];
adc8ce12
IC
1686 for (var i=0; i<binaryStr.length / 8; i++) {
1687 var byteAsBits = binaryStr.substring(i*8, i*8+8);
1688 var entropyByte = parseInt(byteAsBits, 2);
c6624d51
IC
1689 entropyArr.push(entropyByte)
1690 }
1691 // Convert entropy array to mnemonic
1692 var phrase = mnemonic.toMnemonic(entropyArr);
1693 // Set the mnemonic in the UI
1694 DOM.phrase.val(phrase);
96ee8ab0 1695 writeSplitPhrase(phrase);
74ab4cbe
IC
1696 // Show the word indexes
1697 showWordIndexes();
09d63290
IC
1698 // Show the checksum
1699 showChecksum();
c6624d51
IC
1700 }
1701
0a84fe6a 1702 function clearEntropyFeedback() {
20f459ce 1703 DOM.entropyCrackTime.text("...");
1cf1bbaf 1704 DOM.entropyType.text("");
0a84fe6a
IC
1705 DOM.entropyWordCount.text("0");
1706 DOM.entropyEventCount.text("0");
1707 DOM.entropyBitsPerEvent.text("0");
1708 DOM.entropyBits.text("0");
1709 DOM.entropyFiltered.html("&nbsp;");
1710 DOM.entropyBinary.html("&nbsp;");
c6624d51
IC
1711 }
1712
1cf1bbaf 1713 function showEntropyFeedback(entropy) {
6422c1cd 1714 var numberOfBits = entropy.binaryStr.length;
20f459ce 1715 var timeToCrack = "unknown";
9bc39377 1716 try {
22f87669 1717 var z = libs.zxcvbn(entropy.base.parts.join(""));
20f459ce
IC
1718 timeToCrack = z.crack_times_display.offline_fast_hashing_1e10_per_second;
1719 if (z.feedback.warning != "") {
1720 timeToCrack = timeToCrack + " - " + z.feedback.warning;
1721 };
9bc39377
IC
1722 }
1723 catch (e) {
9bc39377
IC
1724 console.log("Error detecting entropy strength with zxcvbn:");
1725 console.log(e);
b299a6a7 1726 }
391c7f26 1727 var entropyTypeStr = getEntropyTypeStr(entropy);
516c16d7
IC
1728 DOM.entropyTypeInputs.attr("checked", false);
1729 DOM.entropyTypeInputs.filter("[value='" + entropyTypeStr + "']").attr("checked", true);
6422c1cd 1730 var wordCount = Math.floor(numberOfBits / 32) * 3;
94959756 1731 var bitsPerEvent = entropy.bitsPerEvent.toFixed(2);
f8ca25c3 1732 var spacedBinaryStr = addSpacesEveryElevenBits(entropy.binaryStr);
b54c1218 1733 DOM.entropyFiltered.html(entropy.cleanHtml);
391c7f26 1734 DOM.entropyType.text(entropyTypeStr);
20f459ce 1735 DOM.entropyCrackTime.text(timeToCrack);
1cf1bbaf 1736 DOM.entropyEventCount.text(entropy.base.ints.length);
6422c1cd 1737 DOM.entropyBits.text(numberOfBits);
0a84fe6a 1738 DOM.entropyWordCount.text(wordCount);
d6cade86 1739 DOM.entropyBinary.text(spacedBinaryStr);
6422c1cd 1740 DOM.entropyBitsPerEvent.text(bitsPerEvent);
ee0981f1
IC
1741 // detect and warn of filtering
1742 var rawNoSpaces = DOM.entropy.val().replace(/\s/g, "");
1743 var cleanNoSpaces = entropy.cleanStr.replace(/\s/g, "");
1744 var isFiltered = rawNoSpaces.length != cleanNoSpaces.length;
1745 if (isFiltered) {
1746 DOM.entropyFilterWarning.removeClass('hidden');
1747 }
1748 else {
1749 DOM.entropyFilterWarning.addClass('hidden');
1750 }
02f05d3e
IC
1751 }
1752
391c7f26
IC
1753 function getEntropyTypeStr(entropy) {
1754 var typeStr = entropy.base.str;
1755 // Add some detail if these are cards
1756 if (entropy.base.asInt == 52) {
1757 var cardDetail = []; // array of message strings
1758 // Detect duplicates
1759 var dupes = [];
1760 var dupeTracker = {};
1761 for (var i=0; i<entropy.base.parts.length; i++) {
1762 var card = entropy.base.parts[i];
5c653a12
IC
1763 var cardUpper = card.toUpperCase();
1764 if (cardUpper in dupeTracker) {
391c7f26
IC
1765 dupes.push(card);
1766 }
5c653a12 1767 dupeTracker[cardUpper] = true;
391c7f26
IC
1768 }
1769 if (dupes.length > 0) {
1770 var dupeWord = "duplicates";
1771 if (dupes.length == 1) {
1772 dupeWord = "duplicate";
1773 }
1774 var msg = dupes.length + " " + dupeWord + ": " + dupes.slice(0,3).join(" ");
1775 if (dupes.length > 3) {
1776 msg += "...";
1777 }
1778 cardDetail.push(msg);
1779 }
1780 // Detect full deck
1781 var uniqueCards = [];
1782 for (var uniqueCard in dupeTracker) {
1783 uniqueCards.push(uniqueCard);
1784 }
1785 if (uniqueCards.length == 52) {
1786 cardDetail.unshift("full deck");
1787 }
bbc29c80
IC
1788 // Detect missing cards
1789 var values = "A23456789TJQK";
1790 var suits = "CDHS";
1791 var missingCards = [];
1792 for (var i=0; i<suits.length; i++) {
1793 for (var j=0; j<values.length; j++) {
1794 var card = values[j] + suits[i];
1795 if (!(card in dupeTracker)) {
1796 missingCards.push(card);
1797 }
1798 }
1799 }
1800 // Display missing cards if six or less, ie clearly going for full deck
1801 if (missingCards.length > 0 && missingCards.length <= 6) {
1802 var msg = missingCards.length + " missing: " + missingCards.slice(0,3).join(" ");
1803 if (missingCards.length > 3) {
1804 msg += "...";
1805 }
1806 cardDetail.push(msg);
1807 }
391c7f26
IC
1808 // Add card details to typeStr
1809 if (cardDetail.length > 0) {
1810 typeStr += " (" + cardDetail.join(", ") + ")";
1811 }
1812 }
1813 return typeStr;
1814 }
1815
e00964cc
IC
1816 function setQrEvents(els) {
1817 els.on("mouseenter", createQr);
1818 els.on("mouseleave", destroyQr);
1819 els.on("click", toggleQr);
1820 }
1821
1822 function createQr(e) {
1823 var content = e.target.textContent || e.target.value;
1824 if (content) {
22f87669 1825 var qrEl = libs.kjua({
9225b805
IC
1826 text: content,
1827 render: "canvas",
1828 size: 310,
1829 ecLevel: 'H',
1830 });
1831 DOM.qrImage.append(qrEl);
e00964cc 1832 if (!showQr) {
97811c29
IC
1833 DOM.qrHider.addClass("hidden");
1834 }
1835 else {
1836 DOM.qrHider.removeClass("hidden");
e00964cc
IC
1837 }
1838 DOM.qrContainer.removeClass("hidden");
1839 }
1840 }
1841
1842 function destroyQr() {
1843 DOM.qrImage.text("");
1844 DOM.qrContainer.addClass("hidden");
1845 }
1846
1847 function toggleQr() {
1848 showQr = !showQr;
97811c29 1849 DOM.qrHider.toggleClass("hidden");
e00964cc
IC
1850 DOM.qrHint.toggleClass("hidden");
1851 }
1852
32fab2c3
IC
1853 function bip44TabSelected() {
1854 return DOM.bip44tab.hasClass("active");
1855 }
1856
1857 function bip32TabSelected() {
1858 return DOM.bip32tab.hasClass("active");
1859 }
1860
44e705cd
IB
1861 function networkIsEthereum() {
1862 var name = networks[DOM.network.val()].name;
1863 return (name == "ETH - Ethereum")
1864 || (name == "ETC - Ethereum Classic")
90929701 1865 || (name == "EWT - EnergyWeb")
44e705cd
IB
1866 || (name == "PIRL - Pirl")
1867 || (name == "MIX - MIX")
46a5338e 1868 || (name == "MOAC - MOAC")
44e705cd
IB
1869 || (name == "MUSIC - Musicoin")
1870 || (name == "POA - Poa")
1871 || (name == "EXP - Expanse")
1872 || (name == "CLO - Callisto")
1873 || (name == "DXN - DEXON")
1874 || (name == "ELLA - Ellaism")
1875 || (name == "ESN - Ethersocial Network")
1876 || (name == "VET - VeChain")
8131a071 1877 || (name == "ERE - EtherCore")
44e705cd
IB
1878 }
1879
a823e526 1880 function networkIsRsk() {
1881 var name = networks[DOM.network.val()].name;
1882 return (name == "R-BTC - RSK")
1883 || (name == "tR-BTC - RSK Testnet");
1884 }
1885
0fd67b59 1886 function networkHasSegwit() {
c147cb5e
IC
1887 var n = network;
1888 if ("baseNetwork" in network) {
22f87669 1889 n = libs.bitcoin.networks[network.baseNetwork];
c147cb5e
IC
1890 }
1891 // check if only p2wpkh params are required
1892 if (p2wpkhSelected()) {
1893 return "p2wpkh" in n;
1894 }
1895 // check if only p2wpkh-in-p2sh params are required
1896 else if (p2wpkhInP2shSelected()) {
1897 return "p2wpkhInP2sh" in n;
1898 }
1899 // require both if it's unclear which params are required
1900 return "p2wpkh" in n && "p2wpkhInP2sh" in n;
6c08f364
IC
1901 }
1902
1903 function bip49TabSelected() {
1904 return DOM.bip49tab.hasClass("active");
1905 }
1906
4e9b492c
IC
1907 function bip84TabSelected() {
1908 return DOM.bip84tab.hasClass("active");
1909 }
1910
c49e8812
IC
1911 function bip141TabSelected() {
1912 return DOM.bip141tab.hasClass("active");
1913 }
1914
6c08f364
IC
1915 function setHdCoin(coinValue) {
1916 DOM.bip44coin.val(coinValue);
1917 DOM.bip49coin.val(coinValue);
4e9b492c 1918 DOM.bip84coin.val(coinValue);
6c08f364
IC
1919 }
1920
0fd67b59 1921 function showSegwitAvailable() {
6c08f364
IC
1922 DOM.bip49unavailable.addClass("hidden");
1923 DOM.bip49available.removeClass("hidden");
bddd5d9f
IC
1924 DOM.bip84unavailable.addClass("hidden");
1925 DOM.bip84available.removeClass("hidden");
c49e8812
IC
1926 DOM.bip141unavailable.addClass("hidden");
1927 DOM.bip141available.removeClass("hidden");
6c08f364
IC
1928 }
1929
0fd67b59 1930 function showSegwitUnavailable() {
6c08f364
IC
1931 DOM.bip49available.addClass("hidden");
1932 DOM.bip49unavailable.removeClass("hidden");
bddd5d9f
IC
1933 DOM.bip84available.addClass("hidden");
1934 DOM.bip84unavailable.removeClass("hidden");
c49e8812
IC
1935 DOM.bip141available.addClass("hidden");
1936 DOM.bip141unavailable.removeClass("hidden");
6c08f364
IC
1937 }
1938
0fd67b59
IC
1939 function adjustNetworkForSegwit() {
1940 // If segwit is selected the xpub/xprv prefixes need to be adjusted
93c3ef47
IC
1941 // to avoid accidentally importing BIP49 xpub to BIP44 watch only
1942 // wallet.
1943 // See https://github.com/iancoleman/bip39/issues/125
c49e8812
IC
1944 var segwitNetworks = null;
1945 // if a segwit network is alread selected, need to use base network to
1946 // look up new parameters
1947 if ("baseNetwork" in network) {
22f87669 1948 network = libs.bitcoin.networks[network.baseNetwork];
c49e8812
IC
1949 }
1950 // choose the right segwit params
1951 if (p2wpkhSelected() && "p2wpkh" in network) {
1952 network = network.p2wpkh;
1953 }
1954 else if (p2wpkhInP2shSelected() && "p2wpkhInP2sh" in network) {
1955 network = network.p2wpkhInP2sh;
93c3ef47 1956 }
5c203fab
IC
1957 else if (p2wshSelected() && "p2wsh" in network) {
1958 network = network.p2wsh;
1959 }
1960 else if (p2wshInP2shSelected() && "p2wshInP2sh" in network) {
1961 network = network.p2wshInP2sh;
1962 }
93c3ef47
IC
1963 }
1964
9183f9f6
IC
1965 function lastIndexInTable() {
1966 var pathText = DOM.addresses.find(".index").last().text();
1967 var pathBits = pathText.split("/");
1968 var lastBit = pathBits[pathBits.length-1];
1969 var lastBitClean = lastBit.replace("'", "");
1970 return parseInt(lastBitClean);
1971 }
1972
74ab4cbe
IC
1973 function uint8ArrayToHex(a) {
1974 var s = ""
1975 for (var i=0; i<a.length; i++) {
1976 var h = a[i].toString(16);
1977 while (h.length < 2) {
1978 h = "0" + h;
1979 }
1980 s = s + h;
1981 }
1982 return s;
1983 }
1984
1985 function showWordIndexes() {
1986 var phrase = DOM.phrase.val();
1987 var words = phraseToWordArray(phrase);
1988 var wordIndexes = [];
1989 var language = getLanguage();
1990 for (var i=0; i<words.length; i++) {
1991 var word = words[i];
1992 var wordIndex = WORDLISTS[language].indexOf(word);
1993 wordIndexes.push(wordIndex);
1994 }
1995 var wordIndexesStr = wordIndexes.join(", ");
1996 DOM.entropyWordIndexes.text(wordIndexesStr);
1997 }
a78f4e28 1998
09d63290
IC
1999 function showChecksum() {
2000 var phrase = DOM.phrase.val();
2001 var words = phraseToWordArray(phrase);
2002 var checksumBitlength = words.length / 3;
2003 var checksum = "";
2004 var binaryStr = "";
2005 var language = getLanguage();
2006 for (var i=words.length-1; i>=0; i--) {
2007 var word = words[i];
2008 var wordIndex = WORDLISTS[language].indexOf(word);
2009 var wordBinary = wordIndex.toString(2);
2010 while (wordBinary.length < 11) {
2011 wordBinary = "0" + wordBinary;
2012 }
2013 var binaryStr = wordBinary + binaryStr;
2014 if (binaryStr.length >= checksumBitlength) {
2015 var start = binaryStr.length - checksumBitlength;
2016 var end = binaryStr.length;
2017 checksum = binaryStr.substring(start, end);
f8ca25c3
IC
2018 // add spaces so the last group is 11 bits, not the first
2019 checksum = checksum.split("").reverse().join("")
2020 checksum = addSpacesEveryElevenBits(checksum);
2021 checksum = checksum.split("").reverse().join("")
2022 break;
09d63290
IC
2023 }
2024 }
2025 DOM.entropyChecksum.text(checksum);
2026 }
2027
a78f4e28
IC
2028 function updateCsv() {
2029 var tableCsv = "path,address,public key,private key\n";
2030 var rows = DOM.addresses.find("tr");
2031 for (var i=0; i<rows.length; i++) {
2032 var row = $(rows[i]);
2033 var cells = row.find("td");
2034 for (var j=0; j<cells.length; j++) {
2035 var cell = $(cells[j]);
2036 if (!cell.children().hasClass("invisible")) {
2037 tableCsv = tableCsv + cell.text();
2038 }
2039 if (j != cells.length - 1) {
2040 tableCsv = tableCsv + ",";
2041 }
2042 }
2043 tableCsv = tableCsv + "\n";
2044 }
2045 DOM.csv.val(tableCsv);
2046 }
74ab4cbe 2047
f8ca25c3
IC
2048 function addSpacesEveryElevenBits(binaryStr) {
2049 return binaryStr.match(/.{1,11}/g).join(" ");
2050 }
2051
7f15cb6e 2052 var networks = [
85f762c9 2053 {
2054 name: "AC - Asiacoin",
85f762c9 2055 onSelect: function() {
22f87669 2056 network = libs.bitcoin.networks.asiacoin;
85f762c9 2057 setHdCoin(51);
2058 },
2059 },
2060 {
2061 name: "ACC - Adcoin",
85f762c9 2062 onSelect: function() {
22f87669 2063 network = libs.bitcoin.networks.adcoin;
85f762c9 2064 setHdCoin(161);
2065 },
2066 },
31264e8b
PT
2067 {
2068 name: "ARYA - Aryacoin",
2069 onSelect: function() {
22f87669 2070 network = libs.bitcoin.networks.aryacoin;
31264e8b
PT
2071 setHdCoin(357);
2072 },
2073 },
85f762c9 2074 {
2075 name: "AUR - Auroracoin",
85f762c9 2076 onSelect: function() {
22f87669 2077 network = libs.bitcoin.networks.auroracoin;
85f762c9 2078 setHdCoin(85);
2079 },
2080 },
cfc0c4d7 2081 {
2082 name: "AXE - Axe",
cfc0c4d7 2083 onSelect: function() {
22f87669 2084 network = libs.bitcoin.networks.axe;
7d9b5f68 2085 setHdCoin(4242);
cfc0c4d7 2086 },
96ee8ab0
IC
2087 },
2088 {
45588661
KP
2089 name: "ANON - ANON",
2090 onSelect: function() {
22f87669 2091 network = libs.bitcoin.networks.anon;
45588661
KP
2092 setHdCoin(220);
2093 },
cfc0c4d7 2094 },
3e7b1ac9
JLE
2095 {
2096 name: "BOLI - Bolivarcoin",
2097 onSelect: function() {
22f87669 2098 network = libs.bitcoin.networks.bolivarcoin;
3e7b1ac9
JLE
2099 setHdCoin(278);
2100 },
2101 },
85f762c9 2102 {
2103 name: "BCA - Bitcoin Atom",
85f762c9 2104 onSelect: function() {
22f87669 2105 network = libs.bitcoin.networks.atom;
85f762c9 2106 setHdCoin(185);
2107 },
2108 },
52d698e4 2109 {
2eab7c32 2110 name: "BCH - Bitcoin Cash",
52d698e4 2111 onSelect: function() {
e0f91e20 2112 DOM.bitcoinCashAddressTypeContainer.removeClass("hidden");
2eab7c32 2113 setHdCoin(145);
52d698e4 2114 },
2115 },
daab55dc 2116 {
2eab7c32 2117 name: "BEET - Beetlecoin",
daab55dc 2118 onSelect: function() {
22f87669 2119 network = libs.bitcoin.networks.beetlecoin;
2eab7c32 2120 setHdCoin(800);
daab55dc
IC
2121 },
2122 },
85f762c9 2123 {
2124 name: "BELA - Belacoin",
85f762c9 2125 onSelect: function() {
22f87669 2126 network = libs.bitcoin.networks.belacoin;
85f762c9 2127 setHdCoin(73);
2128 },
2129 },
892ce76f
IC
2130 {
2131 name: "BLK - BlackCoin",
892ce76f 2132 onSelect: function() {
22f87669 2133 network = libs.bitcoin.networks.blackcoin;
892ce76f
IC
2134 setHdCoin(10);
2135 },
2136 },
6f435817
U
2137 {
2138 name: "BND - Blocknode",
2139 onSelect: function() {
22f87669 2140 network = libs.bitcoin.networks.blocknode;
6f435817
U
2141 setHdCoin(2941);
2142 },
d0889ab9 2143 },
96ee8ab0 2144 {
6f435817
U
2145 name: "tBND - Blocknode Testnet",
2146 onSelect: function() {
22f87669 2147 network = libs.bitcoin.networks.blocknode_testnet;
6f435817
U
2148 setHdCoin(1);
2149 },
2150 },
85f762c9 2151 {
2152 name: "BRIT - Britcoin",
85f762c9 2153 onSelect: function() {
22f87669 2154 network = libs.bitcoin.networks.britcoin;
85f762c9 2155 setHdCoin(70);
2156 },
2157 },
2158 {
2159 name: "BSD - Bitsend",
85f762c9 2160 onSelect: function() {
22f87669 2161 network = libs.bitcoin.networks.bitsend;
85f762c9 2162 setHdCoin(91);
2163 },
f22f7677 2164 },
96ee8ab0 2165 {
f22f7677
PC
2166 name: "BST - BlockStamp",
2167 onSelect: function() {
22f87669 2168 network = libs.bitcoin.networks.blockstamp;
f22f7677
PC
2169 setHdCoin(254);
2170 },
85f762c9 2171 },
2172 {
2173 name: "BTA - Bata",
85f762c9 2174 onSelect: function() {
22f87669 2175 network = libs.bitcoin.networks.bata;
85f762c9 2176 setHdCoin(89);
2177 },
2178 },
7f15cb6e 2179 {
534481b6 2180 name: "BTC - Bitcoin",
7a995731 2181 onSelect: function() {
22f87669 2182 network = libs.bitcoin.networks.bitcoin;
6c08f364 2183 setHdCoin(0);
7a995731
IC
2184 },
2185 },
f1224201
IC
2186 {
2187 name: "BTC - Bitcoin RegTest",
2188 onSelect: function() {
22f87669 2189 network = libs.bitcoin.networks.regtest;
f1224201
IC
2190 // Using hd coin value 1 based on bip44_coin_type
2191 // https://github.com/chaintope/bitcoinrb/blob/f1014406f6b8f9b4edcecedc18df70c80df06f11/lib/bitcoin/chainparams/regtest.yml
2192 setHdCoin(1);
2193 },
2194 },
7f15cb6e 2195 {
534481b6 2196 name: "BTC - Bitcoin Testnet",
7a995731 2197 onSelect: function() {
22f87669 2198 network = libs.bitcoin.networks.testnet;
6c08f364 2199 setHdCoin(1);
7a995731 2200 },
8b2a093c 2201 },
96ee8ab0 2202 {
8b2a093c
PC
2203 name: "BITG - Bitcoin Green",
2204 onSelect: function() {
22f87669 2205 network = libs.bitcoin.networks.bitcoingreen;
8b2a093c
PC
2206 setHdCoin(222);
2207 },
7a995731 2208 },
1e2cc748 2209 {
2210 name: "BTCP - Bitcoin Private",
1e2cc748 2211 onSelect: function() {
22f87669 2212 network = libs.bitcoin.networks.bitcoinprivate;
1e2cc748 2213 setHdCoin(183);
2214 },
2215 },
31264e8b
PT
2216 {
2217 name: "BSV - BitcoinSV",
2218 onSelect: function() {
22f87669 2219 network = libs.bitcoin.networks.bitcoinsv;
31264e8b
PT
2220 setHdCoin(236);
2221 },
2222 },
1e2cc748 2223 {
2224 name: "BTCZ - Bitcoinz",
1e2cc748 2225 onSelect: function() {
22f87669 2226 network = libs.bitcoin.networks.bitcoinz;
1e2cc748 2227 setHdCoin(177);
2228 },
2229 },
5ed5f661
D
2230 {
2231 name: "BTDX - BitCloud",
2232 onSelect: function() {
22f87669 2233 network = libs.bitcoin.networks.bitcloud;
5ed5f661
D
2234 setHdCoin(218);
2235 },
2236 },
39608073
RS
2237 {
2238 name: "BTG - Bitcoin Gold",
39608073 2239 onSelect: function() {
22f87669 2240 network = libs.bitcoin.networks.bgold;
88ae1301 2241 setHdCoin(156);
39608073
RS
2242 },
2243 },
85f762c9 2244 {
2245 name: "BTX - Bitcore",
85f762c9 2246 onSelect: function() {
22f87669 2247 network = libs.bitcoin.networks.bitcore;
85f762c9 2248 setHdCoin(160);
2249 },
2250 },
2251 {
2252 name: "CCN - Cannacoin",
85f762c9 2253 onSelect: function() {
22f87669 2254 network = libs.bitcoin.networks.cannacoin;
85f762c9 2255 setHdCoin(19);
2256 },
2257 },
04ec88af
VD
2258 {
2259 name: "CESC - Cryptoescudo",
2260 onSelect: function() {
22f87669 2261 network = libs.bitcoin.networks.cannacoin;
04ec88af
VD
2262 setHdCoin(111);
2263 },
2264 },
85f762c9 2265 {
2266 name: "CDN - Canadaecoin",
85f762c9 2267 onSelect: function() {
22f87669 2268 network = libs.bitcoin.networks.canadaecoin;
85f762c9 2269 setHdCoin(34);
2270 },
2271 },
7f15cb6e 2272 {
534481b6 2273 name: "CLAM - Clams",
7a995731 2274 onSelect: function() {
22f87669 2275 network = libs.bitcoin.networks.clam;
6c08f364 2276 setHdCoin(23);
7a995731
IC
2277 },
2278 },
bb381a72 2279 {
2280 name: "CLO - Callisto",
2281 segwitAvailable: false,
2282 onSelect: function() {
22f87669 2283 network = libs.bitcoin.networks.bitcoin;
bb381a72 2284 setHdCoin(820);
2285 },
2286 },
85f762c9 2287 {
2288 name: "CLUB - Clubcoin",
85f762c9 2289 onSelect: function() {
22f87669 2290 network = libs.bitcoin.networks.clubcoin;
85f762c9 2291 setHdCoin(79);
2292 },
2293 },
2294 {
2295 name: "CMP - Compcoin",
85f762c9 2296 onSelect: function() {
22f87669 2297 network = libs.bitcoin.networks.compcoin;
85f762c9 2298 setHdCoin(71);
2299 },
2300 },
519e9dc7
MKA
2301 {
2302 name: "CPU - CPUchain",
2303 onSelect: function() {
22f87669 2304 network = libs.bitcoin.networks.cpuchain;
519e9dc7
MKA
2305 setHdCoin(363);
2306 },
2307 },
85f762c9 2308 {
2309 name: "CRAVE - Crave",
85f762c9 2310 onSelect: function() {
22f87669 2311 network = libs.bitcoin.networks.crave;
85f762c9 2312 setHdCoin(186);
2313 },
2314 },
bf13c95f
PC
2315 {
2316 name: "CRW - Crown (Legacy)",
2317 onSelect: function() {
22f87669 2318 network = libs.bitcoin.networks.crown;
bf13c95f
PC
2319 setHdCoin(72);
2320 },
2321 },
0921f370 2322 {
2323 name: "CRW - Crown",
2324 onSelect: function() {
22f87669 2325 network = libs.bitcoin.networks.crown;
6c08f364 2326 setHdCoin(72);
0921f370 2327 },
2328 },
45e40c28
C
2329 {
2330 name: "CSC - CasinoCoin",
2331 onSelect: function() {
22f87669 2332 network = libs.bitcoin.networks.bitcoin;
45e40c28
C
2333 setHdCoin(359);
2334 },
2335 },
7f15cb6e 2336 {
534481b6 2337 name: "DASH - Dash",
e3a9508c 2338 onSelect: function() {
22f87669 2339 network = libs.bitcoin.networks.dash;
6c08f364 2340 setHdCoin(5);
e3a9508c
IC
2341 },
2342 },
c0386f3b 2343 {
534481b6 2344 name: "DASH - Dash Testnet",
c0386f3b 2345 onSelect: function() {
22f87669 2346 network = libs.bitcoin.networks.dashtn;
6c08f364 2347 setHdCoin(1);
c0386f3b
KR
2348 },
2349 },
85f762c9 2350 {
2351 name: "DFC - Defcoin",
85f762c9 2352 onSelect: function() {
22f87669 2353 network = libs.bitcoin.networks.defcoin;
85f762c9 2354 setHdCoin(1337);
2355 },
2356 },
2357 {
2358 name: "DGB - Digibyte",
85f762c9 2359 onSelect: function() {
22f87669 2360 network = libs.bitcoin.networks.digibyte;
85f762c9 2361 setHdCoin(20);
2362 },
2363 },
2364 {
2365 name: "DGC - Digitalcoin",
85f762c9 2366 onSelect: function() {
22f87669 2367 network = libs.bitcoin.networks.digitalcoin;
85f762c9 2368 setHdCoin(18);
2369 },
2370 },
2371 {
2372 name: "DMD - Diamond",
85f762c9 2373 onSelect: function() {
22f87669 2374 network = libs.bitcoin.networks.diamond;
85f762c9 2375 setHdCoin(152);
2376 },
2377 },
40209fd8 2378 {
2379 name: "DNR - Denarius",
40209fd8 2380 onSelect: function() {
22f87669 2381 network = libs.bitcoin.networks.denarius;
50289881 2382 setHdCoin(116);
40209fd8 2383 },
2384 },
e3a9508c 2385 {
534481b6 2386 name: "DOGE - Dogecoin",
2387 onSelect: function() {
22f87669 2388 network = libs.bitcoin.networks.dogecoin;
6c08f364 2389 setHdCoin(3);
534481b6 2390 },
2391 },
b85da074
PD
2392 {
2393 name: "DOGEt - Dogecoin Testnet",
2394 onSelect: function() {
22f87669 2395 network = libs.bitcoin.networks.dogecointestnet;
b85da074
PD
2396 setHdCoin(1);
2397 },
2398 },
6cbd3bc8
HL
2399 {
2400 name: "DXN - DEXON",
2401 onSelect: function() {
22f87669 2402 network = libs.bitcoin.networks.bitcoin;
6cbd3bc8
HL
2403 setHdCoin(237);
2404 },
2405 },
85f762c9 2406 {
2407 name: "ECN - Ecoin",
85f762c9 2408 onSelect: function() {
22f87669 2409 network = libs.bitcoin.networks.ecoin;
85f762c9 2410 setHdCoin(115);
2411 },
2412 },
2413 {
2414 name: "EDRC - Edrcoin",
85f762c9 2415 onSelect: function() {
22f87669 2416 network = libs.bitcoin.networks.edrcoin;
85f762c9 2417 setHdCoin(56);
2418 },
2419 },
2420 {
2421 name: "EFL - Egulden",
85f762c9 2422 onSelect: function() {
22f87669 2423 network = libs.bitcoin.networks.egulden;
85f762c9 2424 setHdCoin(78);
2425 },
2885ddd5 2426 },
36523e0d
NJ
2427 {
2428 name: "ELA - Elastos",
2429 onSelect: function () {
22f87669 2430 network = libs.bitcoin.networks.elastos;
36523e0d
NJ
2431 setHdCoin(2305);
2432 },
2433 },
2885ddd5 2434 {
2435 name: "ELLA - Ellaism",
2436 segwitAvailable: false,
2437 onSelect: function() {
22f87669 2438 network = libs.bitcoin.networks.bitcoin;
2885ddd5 2439 setHdCoin(163);
2440 },
85f762c9 2441 },
2442 {
2443 name: "EMC2 - Einsteinium",
85f762c9 2444 onSelect: function() {
22f87669 2445 network = libs.bitcoin.networks.einsteinium;
85f762c9 2446 setHdCoin(41);
2447 },
2448 },
2449 {
2450 name: "ERC - Europecoin",
85f762c9 2451 onSelect: function() {
22f87669 2452 network = libs.bitcoin.networks.europecoin;
85f762c9 2453 setHdCoin(151);
2454 },
2455 },
3451d1a8
JU
2456 {
2457 name: "EOS - EOSIO",
2458 onSelect: function() {
22f87669 2459 network = libs.bitcoin.networks.bitcoin;
3451d1a8
JU
2460 setHdCoin(194);
2461 },
2462 },
8131a071
EC
2463 {
2464 name: "ERE - EtherCore",
2465 segwitAvailable: false,
2466 onSelect: function() {
2467 network = libs.bitcoin.networks.bitcoin;
2468 setHdCoin(466);
2469 },
2470 },
16afe3ec 2471 {
2472 name: "ESN - Ethersocial Network",
2473 segwitAvailable: false,
2474 onSelect: function() {
22f87669 2475 network = libs.bitcoin.networks.bitcoin;
16afe3ec 2476 setHdCoin(31102);
2477 },
2478 },
bb381a72 2479 {
2480 name: "ETC - Ethereum Classic",
2481 segwitAvailable: false,
2482 onSelect: function() {
22f87669 2483 network = libs.bitcoin.networks.bitcoin;
bb381a72 2484 setHdCoin(61);
2485 },
2486 },
534481b6 2487 {
2488 name: "ETH - Ethereum",
e3a9508c 2489 onSelect: function() {
22f87669 2490 network = libs.bitcoin.networks.bitcoin;
6c08f364 2491 setHdCoin(60);
e3a9508c 2492 },
dd99c5fd
GM
2493 },
2494 {
2495 name: "EWT - EnergyWeb",
2496 onSelect: function() {
22f87669 2497 network = libs.bitcoin.networks.bitcoin;
dd99c5fd
GM
2498 setHdCoin(246);
2499 },
2500 },
85f762c9 2501 {
2502 name: "EXCL - Exclusivecoin",
85f762c9 2503 onSelect: function() {
22f87669 2504 network = libs.bitcoin.networks.exclusivecoin;
85f762c9 2505 setHdCoin(190);
2506 },
2507 },
a44c45e3 2508 {
2509 name: "EXCC - ExchangeCoin",
2510 onSelect: function() {
22f87669 2511 network = libs.bitcoin.networks.exchangecoin;
a44c45e3 2512 setHdCoin(0);
2513 },
2514 },
bb381a72 2515 {
2516 name: "EXP - Expanse",
2517 segwitAvailable: false,
2518 onSelect: function() {
22f87669 2519 network = libs.bitcoin.networks.bitcoin;
bb381a72 2520 setHdCoin(40);
2521 },
2522 },
7d4e4cbe 2523 {
2524 name: "FIX - FIX",
2525 onSelect: function() {
22f87669 2526 network = libs.bitcoin.networks.fix;
7d4e4cbe 2527 setHdCoin(336);
2528 },
2529 },
2530 {
2531 name: "FIX - FIX Testnet",
2532 onSelect: function() {
22f87669 2533 network = libs.bitcoin.networks.fixtestnet;
7d4e4cbe 2534 setHdCoin(1);
2535 },
2536 },
6e679905 2537 {
2538 name: "FJC - Fujicoin",
6e679905 2539 onSelect: function() {
22f87669 2540 network = libs.bitcoin.networks.fujicoin;
6e679905 2541 setHdCoin(75);
2542 },
2543 },
85f762c9 2544 {
2545 name: "FLASH - Flashcoin",
85f762c9 2546 onSelect: function() {
22f87669 2547 network = libs.bitcoin.networks.flashcoin;
85f762c9 2548 setHdCoin(120);
2549 },
2550 },
2551 {
2552 name: "FRST - Firstcoin",
85f762c9 2553 onSelect: function() {
22f87669 2554 network = libs.bitcoin.networks.firstcoin;
85f762c9 2555 setHdCoin(167);
2556 },
2557 },
2558 {
2559 name: "FTC - Feathercoin",
85f762c9 2560 onSelect: function() {
22f87669 2561 network = libs.bitcoin.networks.feathercoin;
85f762c9 2562 setHdCoin(8);
2563 },
2564 },
f88fab20 2565 {
534481b6 2566 name: "GAME - GameCredits",
f88fab20 2567 onSelect: function() {
22f87669 2568 network = libs.bitcoin.networks.game;
6c08f364 2569 setHdCoin(101);
f88fab20 2570 },
2571 },
85f762c9 2572 {
2573 name: "GBX - Gobyte",
85f762c9 2574 onSelect: function() {
22f87669 2575 network = libs.bitcoin.networks.gobyte;
85f762c9 2576 setHdCoin(176);
2577 },
2578 },
2579 {
2580 name: "GCR - GCRCoin",
85f762c9 2581 onSelect: function() {
22f87669 2582 network = libs.bitcoin.networks.gcr;
85f762c9 2583 setHdCoin(79);
2584 },
2585 },
2586 {
2587 name: "GRC - Gridcoin",
85f762c9 2588 onSelect: function() {
22f87669 2589 network = libs.bitcoin.networks.gridcoin;
85f762c9 2590 setHdCoin(84);
2591 },
76120cb0
H
2592 },
2593 {
2594 name: "GRS - Groestlcoin",
2595 onSelect: function() {
22f87669 2596 network = libs.bitcoin.networks.groestlcoin;
76120cb0
H
2597 setHdCoin(17);
2598 },
2599 },
2600 {
2601 name: "GRS - Groestlcoin Testnet",
2602 onSelect: function() {
22f87669 2603 network = libs.bitcoin.networks.groestlcointestnet;
76120cb0
H
2604 setHdCoin(1);
2605 },
85f762c9 2606 },
2607 {
2608 name: "HNC - Helleniccoin",
85f762c9 2609 onSelect: function() {
22f87669 2610 network = libs.bitcoin.networks.helleniccoin;
85f762c9 2611 setHdCoin(168);
2612 },
60727c51 2613 },
96ee8ab0 2614 {
318ec4dc 2615 name: "HUSH - Hush (Legacy)",
60727c51 2616 onSelect: function() {
22f87669 2617 network = libs.bitcoin.networks.hush;
60727c51
PC
2618 setHdCoin(197);
2619 },
85f762c9 2620 },
96ee8ab0 2621 {
318ec4dc
PC
2622 name: "HUSH - Hush3",
2623 onSelect: function() {
22f87669 2624 network = libs.bitcoin.networks.hush3;
318ec4dc
PC
2625 setHdCoin(197);
2626 },
2627 },
85f762c9 2628 {
2629 name: "INSN - Insane",
85f762c9 2630 onSelect: function() {
22f87669 2631 network = libs.bitcoin.networks.insane;
85f762c9 2632 setHdCoin(68);
2633 },
2634 },
2635 {
2636 name: "IOP - Iop",
85f762c9 2637 onSelect: function() {
22f87669 2638 network = libs.bitcoin.networks.iop;
85f762c9 2639 setHdCoin(66);
2640 },
2641 },
2642 {
2643 name: "IXC - Ixcoin",
85f762c9 2644 onSelect: function() {
22f87669 2645 network = libs.bitcoin.networks.ixcoin;
85f762c9 2646 setHdCoin(86);
2647 },
2648 },
a3baa26e 2649 {
534481b6 2650 name: "JBS - Jumbucks",
a3baa26e 2651 onSelect: function() {
22f87669 2652 network = libs.bitcoin.networks.jumbucks;
6c08f364 2653 setHdCoin(26);
a3baa26e
IC
2654 },
2655 },
aab3645f 2656 {
2657 name: "KMD - Komodo",
2658 bip49available: false,
2659 onSelect: function() {
22f87669 2660 network = libs.bitcoin.networks.komodo;
aab3645f 2661 setHdCoin(141);
2662 },
2663 },
0a5d28a7 2664 {
2665 name: "KOBO - Kobocoin",
2666 bip49available: false,
2667 onSelect: function() {
22f87669 2668 network = libs.bitcoin.networks.kobocoin;
0a5d28a7 2669 setHdCoin(196);
2670 },
2671 },
85f762c9 2672 {
2673 name: "LBC - Library Credits",
85f762c9 2674 onSelect: function() {
22f87669 2675 network = libs.bitcoin.networks.lbry;
85f762c9 2676 setHdCoin(140);
2677 },
2678 },
2679 {
2680 name: "LCC - Litecoincash",
85f762c9 2681 onSelect: function() {
22f87669 2682 network = libs.bitcoin.networks.litecoincash;
85f762c9 2683 setHdCoin(192);
2684 },
2685 },
2686 {
2687 name: "LDCN - Landcoin",
85f762c9 2688 onSelect: function() {
22f87669 2689 network = libs.bitcoin.networks.landcoin;
85f762c9 2690 setHdCoin(63);
2691 },
2692 },
2693 {
2694 name: "LINX - Linx",
85f762c9 2695 onSelect: function() {
22f87669 2696 network = libs.bitcoin.networks.linx;
85f762c9 2697 setHdCoin(114);
2698 },
2699 },
8ba31a89
JLEP
2700 {
2701 name: "LKR - Lkrcoin",
2702 segwitAvailable: false,
2703 onSelect: function() {
22f87669 2704 network = libs.bitcoin.networks.lkrcoin;
8ba31a89
JLEP
2705 setHdCoin(557);
2706 },
2707 },
a3baa26e 2708 {
534481b6 2709 name: "LTC - Litecoin",
a3baa26e 2710 onSelect: function() {
22f87669 2711 network = libs.bitcoin.networks.litecoin;
6c08f364 2712 setHdCoin(2);
3abab9b0 2713 DOM.litecoinLtubContainer.removeClass("hidden");
a3baa26e
IC
2714 },
2715 },
b85da074
PD
2716 {
2717 name: "LTCt - Litecoin Testnet",
2718 onSelect: function() {
22f87669 2719 network = libs.bitcoin.networks.litecointestnet;
b85da074
PD
2720 setHdCoin(1);
2721 DOM.litecoinLtubContainer.removeClass("hidden");
2722 },
2723 },
d4fcdda9
PC
2724 {
2725 name: "LTZ - LitecoinZ",
2726 onSelect: function() {
22f87669 2727 network = libs.bitcoin.networks.litecoinz;
d4fcdda9
PC
2728 setHdCoin(221);
2729 },
2730 },
85f762c9 2731 {
2732 name: "LYNX - Lynx",
85f762c9 2733 onSelect: function() {
22f87669 2734 network = libs.bitcoin.networks.lynx;
85f762c9 2735 setHdCoin(191);
2736 },
2737 },
56ad9601
JS
2738 {
2739 name: "MAZA - Maza",
56ad9601 2740 onSelect: function() {
22f87669 2741 network = libs.bitcoin.networks.maza;
56ad9601
JS
2742 setHdCoin(13);
2743 },
2744 },
5ed5f661
D
2745 {
2746 name: "MEC - Megacoin",
2747 onSelect: function() {
22f87669 2748 network = libs.bitcoin.networks.megacoin;
5ed5f661
D
2749 setHdCoin(217);
2750 },
2751 },
bb381a72 2752 {
2753 name: "MIX - MIX",
2754 segwitAvailable: false,
2755 onSelect: function() {
22f87669 2756 network = libs.bitcoin.networks.bitcoin;
bb381a72 2757 setHdCoin(76);
2758 },
2759 },
85f762c9 2760 {
2761 name: "MNX - Minexcoin",
85f762c9 2762 onSelect: function() {
22f87669 2763 network = libs.bitcoin.networks.minexcoin;
85f762c9 2764 setHdCoin(182);
2765 },
2766 },
f487fea9
IC
2767 {
2768 name: "MONA - Monacoin",
f487fea9 2769 onSelect: function() {
22f87669 2770 network = libs.bitcoin.networks.monacoin,
f487fea9
IC
2771 setHdCoin(22);
2772 },
2773 },
31264e8b
PT
2774 {
2775 name: "MONK - Monkey Project",
2776 onSelect: function() {
22f87669 2777 network = libs.bitcoin.networks.monkeyproject,
31264e8b
PT
2778 setHdCoin(214);
2779 },
2780 },
46a5338e
LL
2781 {
2782 name: "MOAC - MOAC",
2783 segwitAvailable: false,
2784 onSelect: function() {
2785 network = libs.bitcoin.networks.bitcoin;
2786 setHdCoin(314);
2787 },
2788 },
bb381a72 2789 {
2790 name: "MUSIC - Musicoin",
2791 segwitAvailable: false,
2792 onSelect: function() {
22f87669 2793 network = libs.bitcoin.networks.bitcoin;
bb381a72 2794 setHdCoin(184);
2795 },
2796 },
85f762c9 2797 {
2798 name: "NAV - Navcoin",
85f762c9 2799 onSelect: function() {
22f87669 2800 network = libs.bitcoin.networks.navcoin;
85f762c9 2801 setHdCoin(130);
2802 },
2803 },
7a113003 2804 {
2805 name: "NAS - Nebulas",
2806 onSelect: function() {
22f87669 2807 network = libs.bitcoin.networks.bitcoin;
7a113003 2808 setHdCoin(2718);
2809 },
2810 },
db37d639
IC
2811 {
2812 name: "NEBL - Neblio",
db37d639 2813 onSelect: function() {
22f87669 2814 network = libs.bitcoin.networks.neblio;
db37d639
IC
2815 setHdCoin(146);
2816 },
2817 },
85f762c9 2818 {
2819 name: "NEOS - Neoscoin",
85f762c9 2820 onSelect: function() {
22f87669 2821 network = libs.bitcoin.networks.neoscoin;
85f762c9 2822 setHdCoin(25);
2823 },
2824 },
10980ed6 2825 {
2826 name: "NIX - NIX Platform",
2827 onSelect: function() {
22f87669 2828 network = libs.bitcoin.networks.nix;
10980ed6 2829 setHdCoin(400);
2830 },
2831 },
85f762c9 2832 {
2833 name: "NLG - Gulden",
85f762c9 2834 onSelect: function() {
22f87669 2835 network = libs.bitcoin.networks.gulden;
85f762c9 2836 setHdCoin(87);
2837 },
2838 },
a3baa26e 2839 {
534481b6 2840 name: "NMC - Namecoin",
a3baa26e 2841 onSelect: function() {
22f87669 2842 network = libs.bitcoin.networks.namecoin;
6c08f364 2843 setHdCoin(7);
a3baa26e
IC
2844 },
2845 },
1e2cc748 2846 {
2847 name: "NRG - Energi",
1e2cc748 2848 onSelect: function() {
22f87669 2849 network = libs.bitcoin.networks.energi;
1e2cc748 2850 setHdCoin(204);
2851 },
2852 },
85f762c9 2853 {
2854 name: "NRO - Neurocoin",
85f762c9 2855 onSelect: function() {
22f87669 2856 network = libs.bitcoin.networks.neurocoin;
85f762c9 2857 setHdCoin(110);
2858 },
2859 },
2860 {
2861 name: "NSR - Nushares",
85f762c9 2862 onSelect: function() {
22f87669 2863 network = libs.bitcoin.networks.nushares;
85f762c9 2864 setHdCoin(11);
2865 },
2866 },
2867 {
2868 name: "NYC - Newyorkc",
85f762c9 2869 onSelect: function() {
22f87669 2870 network = libs.bitcoin.networks.newyorkc;
85f762c9 2871 setHdCoin(179);
2872 },
2873 },
2874 {
2875 name: "NVC - Novacoin",
85f762c9 2876 onSelect: function() {
22f87669 2877 network = libs.bitcoin.networks.novacoin;
85f762c9 2878 setHdCoin(50);
2879 },
2880 },
2881 {
2882 name: "OK - Okcash",
85f762c9 2883 onSelect: function() {
22f87669 2884 network = libs.bitcoin.networks.okcash;
85f762c9 2885 setHdCoin(69);
2886 },
2887 },
2888 {
2889 name: "OMNI - Omnicore",
85f762c9 2890 onSelect: function() {
22f87669 2891 network = libs.bitcoin.networks.omnicore;
85f762c9 2892 setHdCoin(200);
2893 },
2894 },
af14981d
A
2895 {
2896 name: "ONION - DeepOnion",
2897 onSelect: function() {
22f87669 2898 network = libs.bitcoin.networks.deeponion;
af14981d
A
2899 setHdCoin(305);
2900 },
2901 },
66419cf3 2902 {
2903 name: "ONX - Onixcoin",
66419cf3 2904 onSelect: function() {
22f87669 2905 network = libs.bitcoin.networks.onixcoin;
d00c7199 2906 setHdCoin(174);
66419cf3 2907 },
38d1b296 2908 },
96ee8ab0 2909 {
38d1b296
PC
2910 name: "PHR - Phore",
2911 onSelect: function() {
22f87669 2912 network = libs.bitcoin.networks.phore;
38d1b296
PC
2913 setHdCoin(444);
2914 },
66419cf3 2915 },
85f762c9 2916 {
2917 name: "PINK - Pinkcoin",
85f762c9 2918 onSelect: function() {
22f87669 2919 network = libs.bitcoin.networks.pinkcoin;
85f762c9 2920 setHdCoin(117);
2921 },
2922 },
bb381a72 2923 {
2924 name: "PIRL - Pirl",
2925 segwitAvailable: false,
2926 onSelect: function() {
22f87669 2927 network = libs.bitcoin.networks.bitcoin;
bb381a72 2928 setHdCoin(164);
2929 },
2930 },
c0df0189 2931 {
2932 name: "PIVX - PIVX",
c0df0189 2933 onSelect: function() {
22f87669 2934 network = libs.bitcoin.networks.pivx;
c0df0189 2935 setHdCoin(119);
2936 },
2937 },
2938 {
2939 name: "PIVX - PIVX Testnet",
c0df0189 2940 onSelect: function() {
22f87669 2941 network = libs.bitcoin.networks.pivxtestnet;
c0df0189 2942 setHdCoin(1);
2943 },
2944 },
bb381a72 2945 {
2946 name: "POA - Poa",
2947 segwitAvailable: false,
2948 onSelect: function() {
22f87669 2949 network = libs.bitcoin.networks.bitcoin;
bb381a72 2950 setHdCoin(178);
2951 },
2952 },
85f762c9 2953 {
2954 name: "POSW - POSWcoin",
85f762c9 2955 onSelect: function() {
22f87669 2956 network = libs.bitcoin.networks.poswcoin;
85f762c9 2957 setHdCoin(47);
2958 },
2959 },
2960 {
2961 name: "POT - Potcoin",
85f762c9 2962 onSelect: function() {
22f87669 2963 network = libs.bitcoin.networks.potcoin;
85f762c9 2964 setHdCoin(81);
2965 },
2966 },
5c434a8a 2967 {
534481b6 2968 name: "PPC - Peercoin",
5c434a8a 2969 onSelect: function() {
22f87669 2970 network = libs.bitcoin.networks.peercoin;
6c08f364 2971 setHdCoin(6);
5c434a8a 2972 },
d2ba3871 2973 },
96ee8ab0 2974 {
d2ba3871
PC
2975 name: "PRJ - ProjectCoin",
2976 onSelect: function() {
22f87669 2977 network = libs.bitcoin.networks.projectcoin;
d2ba3871
PC
2978 setHdCoin(533);
2979 },
5c434a8a 2980 },
85f762c9 2981 {
2982 name: "PSB - Pesobit",
85f762c9 2983 onSelect: function() {
22f87669 2984 network = libs.bitcoin.networks.pesobit;
85f762c9 2985 setHdCoin(62);
2986 },
2987 },
2988 {
2989 name: "PUT - Putincoin",
85f762c9 2990 onSelect: function() {
22f87669 2991 network = libs.bitcoin.networks.putincoin;
85f762c9 2992 setHdCoin(122);
2993 },
2994 },
31264e8b
PT
2995 {
2996 name: "RPD - Rapids",
2997 onSelect: function() {
22f87669 2998 network = libs.bitcoin.networks.rapids;
31264e8b
PT
2999 setHdCoin(320);
3000 },
3001 },
1fe8f519 3002 {
3003 name: "RVN - Ravencoin",
3004 onSelect: function() {
22f87669 3005 network = libs.bitcoin.networks.ravencoin;
1fe8f519 3006 setHdCoin(175);
3007 },
3008 },
e9491c7e 3009 {
b64fb353 3010 name: "R-BTC - RSK",
e9491c7e 3011 onSelect: function() {
b64fb353 3012 network = libs.bitcoin.networks.rsk;
e9491c7e
I
3013 setHdCoin(137);
3014 },
3015 },
3016 {
b64fb353 3017 name: "tR-BTC - RSK Testnet",
e9491c7e 3018 onSelect: function() {
b64fb353 3019 network = libs.bitcoin.networks.rsktestnet;
e9491c7e
I
3020 setHdCoin(37310);
3021 },
3022 },
85f762c9 3023 {
3024 name: "RBY - Rubycoin",
85f762c9 3025 onSelect: function() {
22f87669 3026 network = libs.bitcoin.networks.rubycoin;
85f762c9 3027 setHdCoin(16);
3028 },
3029 },
3030 {
3031 name: "RDD - Reddcoin",
85f762c9 3032 onSelect: function() {
22f87669 3033 network = libs.bitcoin.networks.reddcoin;
85f762c9 3034 setHdCoin(4);
3035 },
3036 },
3037 {
3038 name: "RVR - RevolutionVR",
85f762c9 3039 onSelect: function() {
22f87669 3040 network = libs.bitcoin.networks.revolutionvr;
85f762c9 3041 setHdCoin(129);
3042 },
3043 },
7878bb32 3044 {
d645b982
E
3045 name: "SAFE - Safecoin",
3046 onSelect: function() {
22f87669 3047 network = libs.bitcoin.networks.safecoin;
d645b982
E
3048 setHdCoin(19165);
3049 },
3050 },
96ee8ab0 3051 {
7878bb32
S
3052 name: "SLS - Salus",
3053 onSelect: function() {
22f87669 3054 network = libs.bitcoin.networks.salus;
7878bb32
S
3055 setHdCoin(63);
3056 },
3057 },
64a7d2aa 3058 {
534481b6 3059 name: "SDC - ShadowCash",
82f91834 3060 onSelect: function() {
22f87669 3061 network = libs.bitcoin.networks.shadow;
6c08f364 3062 setHdCoin(35);
82f91834
DG
3063 },
3064 },
07ac4350 3065 {
534481b6 3066 name: "SDC - ShadowCash Testnet",
07ac4350 3067 onSelect: function() {
22f87669 3068 network = libs.bitcoin.networks.shadowtn;
6c08f364 3069 setHdCoin(1);
07ac4350 3070 },
3071 },
7a5a87a0 3072 {
534481b6 3073 name: "SLM - Slimcoin",
7a5a87a0 3074 onSelect: function() {
22f87669 3075 network = libs.bitcoin.networks.slimcoin;
6c08f364 3076 setHdCoin(63);
7a5a87a0
GH
3077 },
3078 },
3079 {
534481b6 3080 name: "SLM - Slimcoin Testnet",
7a5a87a0 3081 onSelect: function() {
22f87669 3082 network = libs.bitcoin.networks.slimcointn;
6c08f364 3083 setHdCoin(111);
7a5a87a0
GH
3084 },
3085 },
d0889ab9 3086 {
3087 name: "SLP - Simple Ledger Protocol",
3088 onSelect: function() {
3089 DOM.bitcoinCashAddressTypeContainer.removeClass("hidden");
3090 setHdCoin(245);
3091 },
3092 },
85f762c9 3093 {
3094 name: "SLR - Solarcoin",
85f762c9 3095 onSelect: function() {
22f87669 3096 network = libs.bitcoin.networks.solarcoin;
85f762c9 3097 setHdCoin(58);
3098 },
3099 },
3100 {
3101 name: "SMLY - Smileycoin",
85f762c9 3102 onSelect: function() {
22f87669 3103 network = libs.bitcoin.networks.smileycoin;
85f762c9 3104 setHdCoin(59);
3105 },
3106 },
ae51db39
B
3107 {
3108 name: "STASH - Stash",
3109 onSelect: function() {
22f87669 3110 network = libs.bitcoin.networks.stash;
ae51db39
B
3111 setHdCoin(0xC0C0);
3112 },
3113 },
3114 {
3115 name: "STASH - Stash Testnet",
3116 onSelect: function() {
22f87669 3117 network = libs.bitcoin.networks.stashtn;
ae51db39
B
3118 setHdCoin(0xCAFE);
3119 },
3120 },
85f762c9 3121 {
3122 name: "STRAT - Stratis",
85f762c9 3123 onSelect: function() {
22f87669 3124 network = libs.bitcoin.networks.stratis;
85f762c9 3125 setHdCoin(105);
3126 },
3127 },
46a5338e
LL
3128 {
3129 name: "SWTC - Jingtum",
3130 onSelect: function() {
3131 network = libs.bitcoin.networks.bitcoin;
3132 setHdCoin(315);
3133 },
3134 },
3b43552c
AD
3135 {
3136 name: "TSTRAT - Stratis Testnet",
3137 onSelect: function() {
22f87669 3138 network = libs.bitcoin.networks.stratistest;
3b43552c
AD
3139 setHdCoin(105);
3140 },
3141 },
85f762c9 3142 {
3143 name: "SYS - Syscoin",
85f762c9 3144 onSelect: function() {
22f87669 3145 network = libs.bitcoin.networks.syscoin;
85f762c9 3146 setHdCoin(57);
3147 },
3148 },
3149 {
3150 name: "THC - Hempcoin",
85f762c9 3151 onSelect: function() {
22f87669 3152 network = libs.bitcoin.networks.hempcoin;
85f762c9 3153 setHdCoin(113);
3154 },
3155 },
75138bee
A
3156 {
3157 name: "THT - Thought",
3158 onSelect: function() {
22f87669 3159 network = libs.bitcoin.networks.thought;
75138bee
A
3160 setHdCoin(1618);
3161 },
3162 },
85f762c9 3163 {
3164 name: "TOA - Toa",
85f762c9 3165 onSelect: function() {
22f87669 3166 network = libs.bitcoin.networks.toa;
85f762c9 3167 setHdCoin(159);
3168 },
3169 },
82e3938a 3170 {
3171 name: "TWINS - TWINS",
3172 onSelect: function() {
22f87669 3173 network = libs.bitcoin.networks.twins;
82e3938a 3174 setHdCoin(970);
3175 },
3176 },
3177 {
3178 name: "TWINS - TWINS Testnet",
3179 onSelect: function() {
22f87669 3180 network = libs.bitcoin.networks.twinstestnet;
82e3938a 3181 setHdCoin(1);
3182 },
3183 },
85f762c9 3184 {
3185 name: "USC - Ultimatesecurecash",
85f762c9 3186 onSelect: function() {
22f87669 3187 network = libs.bitcoin.networks.ultimatesecurecash;
85f762c9 3188 setHdCoin(112);
3189 },
3190 },
8dd28f2c 3191 {
3192 name: "USNBT - NuBits",
8dd28f2c 3193 onSelect: function() {
22f87669 3194 network = libs.bitcoin.networks.nubits;
8dd28f2c 3195 setHdCoin(12);
3196 },
3197 },
85f762c9 3198 {
3199 name: "UNO - Unobtanium",
85f762c9 3200 onSelect: function() {
22f87669 3201 network = libs.bitcoin.networks.unobtanium;
85f762c9 3202 setHdCoin(92);
3203 },
3204 },
3205 {
3206 name: "VASH - Vpncoin",
85f762c9 3207 onSelect: function() {
22f87669 3208 network = libs.bitcoin.networks.vpncoin;
85f762c9 3209 setHdCoin(33);
3210 },
3211 },
c7a1a000
MR
3212 {
3213 name: "VET - VeChain",
3214 onSelect: function() {
22f87669 3215 network = libs.bitcoin.networks.bitcoin;
c7a1a000
MR
3216 setHdCoin(818);
3217 },
3218 },
07ac4350 3219 {
534481b6 3220 name: "VIA - Viacoin",
07ac4350 3221 onSelect: function() {
22f87669 3222 network = libs.bitcoin.networks.viacoin;
6c08f364 3223 setHdCoin(14);
07ac4350 3224 },
3225 },
0edac945 3226 {
534481b6 3227 name: "VIA - Viacoin Testnet",
0edac945 3228 onSelect: function() {
22f87669 3229 network = libs.bitcoin.networks.viacointestnet;
6c08f364 3230 setHdCoin(1);
0edac945
IC
3231 },
3232 },
85f762c9 3233 {
3234 name: "VIVO - Vivo",
85f762c9 3235 onSelect: function() {
22f87669 3236 network = libs.bitcoin.networks.vivo;
85f762c9 3237 setHdCoin(166);
3238 },
3239 },
3240 {
3241 name: "VTC - Vertcoin",
85f762c9 3242 onSelect: function() {
22f87669 3243 network = libs.bitcoin.networks.vertcoin;
85f762c9 3244 setHdCoin(28);
3245 },
3246 },
67f18e2a
C
3247 {
3248 name: "WGR - Wagerr",
3249 onSelect: function() {
22f87669 3250 network = libs.bitcoin.networks.wagerr;
67f18e2a
C
3251 setHdCoin(7825266);
3252 },
3253 },
85f762c9 3254 {
3255 name: "WC - Wincoin",
85f762c9 3256 onSelect: function() {
22f87669 3257 network = libs.bitcoin.networks.wincoin;
85f762c9 3258 setHdCoin(181);
3259 },
2575538b 3260 },
96ee8ab0 3261 {
2575538b
PC
3262 name: "XAX - Artax",
3263 onSelect: function() {
22f87669 3264 network = libs.bitcoin.networks.artax;
2575538b
PC
3265 setHdCoin(219);
3266 },
85f762c9 3267 },
3268 {
3269 name: "XBC - Bitcoinplus",
85f762c9 3270 onSelect: function() {
22f87669 3271 network = libs.bitcoin.networks.bitcoinplus;
85f762c9 3272 setHdCoin(65);
3273 },
3274 },
612d6b42
SÖ
3275 {
3276 name: "XLM - Stellar",
3277 onSelect: function() {
22f87669 3278 network = libs.stellarUtil.dummyNetwork;
612d6b42
SÖ
3279 setHdCoin(148);
3280 },
3281 },
534481b6 3282 {
7ebdf61c 3283 name: "XMY - Myriadcoin",
534481b6 3284 onSelect: function() {
22f87669 3285 network = libs.bitcoin.networks.myriadcoin;
7ebdf61c 3286 setHdCoin(90);
534481b6 3287 },
adedbf91 3288 },
3289 {
7ebdf61c 3290 name: "XRP - Ripple",
adedbf91 3291 onSelect: function() {
22f87669 3292 network = libs.bitcoin.networks.bitcoin;
7ebdf61c 3293 setHdCoin(144);
adedbf91 3294 },
e1d0cf4f 3295 },
85f762c9 3296 {
3297 name: "XVC - Vcash",
85f762c9 3298 onSelect: function() {
22f87669 3299 network = libs.bitcoin.networks.vcash;
85f762c9 3300 setHdCoin(127);
3301 },
3302 },
3303 {
3304 name: "XVG - Verge",
85f762c9 3305 onSelect: function() {
22f87669 3306 network = libs.bitcoin.networks.verge;
85f762c9 3307 setHdCoin(77);
3308 },
3309 },
bb381a72 3310 {
3311 name: "XUEZ - Xuez",
3312 segwitAvailable: false,
3313 onSelect: function() {
22f87669 3314 network = libs.bitcoin.networks.xuez;
bb381a72 3315 setHdCoin(225);
3316 },
3317 },
85f762c9 3318 {
3319 name: "XWC - Whitecoin",
85f762c9 3320 onSelect: function() {
22f87669 3321 network = libs.bitcoin.networks.whitecoin;
85f762c9 3322 setHdCoin(155);
3323 },
3324 },
3325 {
3326 name: "XZC - Zcoin",
85f762c9 3327 onSelect: function() {
22f87669 3328 network = libs.bitcoin.networks.zcoin;
85f762c9 3329 setHdCoin(136);
3330 },
3331 },
1e2cc748 3332 {
3333 name: "ZCL - Zclassic",
1e2cc748 3334 onSelect: function() {
22f87669 3335 network = libs.bitcoin.networks.zclassic;
1e2cc748 3336 setHdCoin(147);
3337 },
3338 },
0702ecd3 3339 {
3340 name: "ZEC - Zcash",
0702ecd3 3341 onSelect: function() {
22f87669 3342 network = libs.bitcoin.networks.zcash;
0702ecd3 3343 setHdCoin(133);
3344 },
3345 },
1e2cc748 3346 {
9cec5dd2 3347 name: "ZEN - Horizen",
1e2cc748 3348 onSelect: function() {
22f87669 3349 network = libs.bitcoin.networks.zencash;
1e2cc748 3350 setHdCoin(121);
3351 },
3352 },
7f15cb6e 3353 ]
6ee4fb7d 3354
b4fd763c
AG
3355 var clients = [
3356 {
3357 name: "Bitcoin Core",
3358 onSelect: function() {
3359 DOM.bip32path.val("m/0'/0'");
3360 DOM.hardenedAddresses.prop('checked', true);
3361 },
3362 },
3363 {
3364 name: "blockchain.info",
3365 onSelect: function() {
3366 DOM.bip32path.val("m/44'/0'/0'");
3367 DOM.hardenedAddresses.prop('checked', false);
3368 },
3369 },
3370 {
3371 name: "MultiBit HD",
3372 onSelect: function() {
3373 DOM.bip32path.val("m/0'/0");
3374 DOM.hardenedAddresses.prop('checked', false);
3375 },
bb381a72 3376 },
3377 {
3378 name: "Coinomi, Ledger",
3379 onSelect: function() {
3380 DOM.bip32path.val("m/44'/"+DOM.bip44coin.val()+"'/0'");
3381 DOM.hardenedAddresses.prop('checked', false);
3382 },
b4fd763c
AG
3383 }
3384 ]
7a995731 3385
a823e526 3386 // RSK - RSK functions - begin
3387 function stripHexPrefix(address) {
3388 if (typeof address !== "string") {
3389 throw new Error("address parameter should be a string.");
3390 }
3391
3392 var hasPrefix = (address.substring(0, 2) === "0x" ||
3393 address.substring(0, 2) === "0X");
3394
3395 return hasPrefix ? address.slice(2) : address;
3396 };
3397
3398 function toChecksumAddressForRsk(address, chainId = null) {
3399 if (typeof address !== "string") {
3400 throw new Error("address parameter should be a string.");
3401 }
3402
3403 if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
3404 throw new Error("Given address is not a valid RSK address: " + address);
3405 }
3406
3407 var stripAddress = stripHexPrefix(address).toLowerCase();
3408 var prefix = chainId != null ? chainId.toString() + "0x" : "";
3409 var keccakHash = libs.ethUtil.keccak256(prefix + stripAddress)
3410 .toString("hex")
3411 .replace(/^0x/i, "");
3412 var checksumAddress = "0x";
3413
3414 for (var i = 0; i < stripAddress.length; i++) {
3415 checksumAddress +=
3416 parseInt(keccakHash[i], 16) >= 8 ?
3417 stripAddress[i].toUpperCase() :
3418 stripAddress[i];
3419 }
3420
3421 return checksumAddress;
3422 }
3423
3424 // RSK - RSK functions - end
3425
36523e0d
NJ
3426 // ELA - Elastos functions - begin
3427 function displayBip44InfoForELA() {
3428 if (!isELA()) {
3429 return;
3430 }
3431
3432 var coin = parseIntNoNaN(DOM.bip44coin.val(), 0);
3433 var account = parseIntNoNaN(DOM.bip44account.val(), 0);
3434
3435 // Calculate the account extended keys
22f87669
IC
3436 var accountXprv = libs.elastosjs.getAccountExtendedPrivateKey(seed, coin, account);
3437 var accountXpub = libs.elastosjs.getAccountExtendedPublicKey(seed, coin, account);
36523e0d
NJ
3438
3439 // Display the extended keys
3440 DOM.bip44accountXprv.val(accountXprv);
3441 DOM.bip44accountXpub.val(accountXpub);
3442 }
3443
3444 function displayBip32InfoForELA() {
3445 if (!isELA()) {
3446 return;
3447 }
3448
3449 var coin = parseIntNoNaN(DOM.bip44coin.val(), 0);
3450 var account = parseIntNoNaN(DOM.bip44account.val(), 0);
3451 var change = parseIntNoNaN(DOM.bip44change.val(), 0);
3452
22f87669
IC
3453 DOM.extendedPrivKey.val(libs.elastosjs.getBip32ExtendedPrivateKey(seed, coin, account, change));
3454 DOM.extendedPubKey.val(libs.elastosjs.getBip32ExtendedPublicKey(seed, coin, account, change));
36523e0d
NJ
3455
3456 // Display the addresses and privkeys
3457 clearAddressesList();
3458 var initialAddressCount = parseInt(DOM.rowsToAdd.val());
3459 displayAddresses(0, initialAddressCount);
3460 }
3461
3462 function calcAddressForELA(seed, coin, account, change, index) {
3463 if (!isELA()) {
3464 return;
3465 }
3466
22f87669 3467 var publicKey = libs.elastosjs.getDerivedPublicKey(libs.elastosjs.getMasterPublicKey(seed), change, index);
36523e0d 3468 return {
22f87669 3469 privateKey: libs.elastosjs.getDerivedPrivateKey(seed, coin, account, change, index),
36523e0d 3470 publicKey: publicKey,
22f87669 3471 address: libs.elastosjs.getAddress(publicKey.toString('hex'))
36523e0d
NJ
3472 };
3473 }
3474 // ELA - Elastos functions - end
3475
ebd8d4e8
IC
3476 init();
3477
3478})();