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