]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/blame - src/js/index.js
Parse extended root key regardless of prefix
[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;
a0091a40 9 var network = bitcoinjs.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
c6624d51 19 var entropyChangeTimeoutEvent = null;
ebd8d4e8 20 var phraseChangeTimeoutEvent = null;
efe41586 21 var rootKeyChangedTimeoutEvent = null;
ebd8d4e8 22
40892aba
IC
23 var generationProcesses = [];
24
ebd8d4e8 25 var DOM = {};
d6cedc94 26 DOM.network = $(".network");
29bf60f5 27 DOM.bip32Client = $("#bip32-client");
d6cedc94 28 DOM.phraseNetwork = $("#network-phrase");
c6624d51
IC
29 DOM.useEntropy = $(".use-entropy");
30 DOM.entropyContainer = $(".entropy-container");
31 DOM.entropy = $(".entropy");
0a84fe6a
IC
32 DOM.entropyFiltered = DOM.entropyContainer.find(".filtered");
33 DOM.entropyType = DOM.entropyContainer.find(".type");
20f459ce 34 DOM.entropyCrackTime = DOM.entropyContainer.find(".crack-time");
0a84fe6a
IC
35 DOM.entropyEventCount = DOM.entropyContainer.find(".event-count");
36 DOM.entropyBits = DOM.entropyContainer.find(".bits");
37 DOM.entropyBitsPerEvent = DOM.entropyContainer.find(".bits-per-event");
38 DOM.entropyWordCount = DOM.entropyContainer.find(".word-count");
39 DOM.entropyBinary = DOM.entropyContainer.find(".binary");
74ab4cbe 40 DOM.entropyWordIndexes = DOM.entropyContainer.find(".word-indexes");
09d63290 41 DOM.entropyChecksum = DOM.entropyContainer.find(".checksum");
0a84fe6a 42 DOM.entropyMnemonicLength = DOM.entropyContainer.find(".mnemonic-length");
645945a0 43 DOM.entropyWeakEntropyOverrideWarning = DOM.entropyContainer.find(".weak-entropy-override-warning");
ee0981f1 44 DOM.entropyFilterWarning = DOM.entropyContainer.find(".filter-warning");
ebd8d4e8 45 DOM.phrase = $(".phrase");
1abcc511 46 DOM.passphrase = $(".passphrase");
c6624d51 47 DOM.generateContainer = $(".generate-container");
ebd8d4e8 48 DOM.generate = $(".generate");
3e0ed16a 49 DOM.seed = $(".seed");
ebd8d4e8 50 DOM.rootKey = $(".root-key");
3abab9b0
IC
51 DOM.litecoinLtubContainer = $(".litecoin-ltub-container");
52 DOM.litecoinUseLtub = $(".litecoin-use-ltub");
ebd8d4e8
IC
53 DOM.extendedPrivKey = $(".extended-priv-key");
54 DOM.extendedPubKey = $(".extended-pub-key");
d6cedc94
IC
55 DOM.bip32tab = $("#bip32-tab");
56 DOM.bip44tab = $("#bip44-tab");
6c08f364 57 DOM.bip49tab = $("#bip49-tab");
4e9b492c 58 DOM.bip84tab = $("#bip84-tab");
c49e8812 59 DOM.bip141tab = $("#bip141-tab");
d6cedc94
IC
60 DOM.bip32panel = $("#bip32");
61 DOM.bip44panel = $("#bip44");
6c08f364 62 DOM.bip49panel = $("#bip49");
ebd8d4e8
IC
63 DOM.bip32path = $("#bip32-path");
64 DOM.bip44path = $("#bip44-path");
65 DOM.bip44purpose = $("#bip44 .purpose");
66 DOM.bip44coin = $("#bip44 .coin");
67 DOM.bip44account = $("#bip44 .account");
c554e6ff
IC
68 DOM.bip44accountXprv = $("#bip44 .account-xprv");
69 DOM.bip44accountXpub = $("#bip44 .account-xpub");
ebd8d4e8 70 DOM.bip44change = $("#bip44 .change");
6c08f364
IC
71 DOM.bip49unavailable = $("#bip49 .unavailable");
72 DOM.bip49available = $("#bip49 .available");
73 DOM.bip49path = $("#bip49-path");
74 DOM.bip49purpose = $("#bip49 .purpose");
75 DOM.bip49coin = $("#bip49 .coin");
76 DOM.bip49account = $("#bip49 .account");
77 DOM.bip49accountXprv = $("#bip49 .account-xprv");
78 DOM.bip49accountXpub = $("#bip49 .account-xpub");
79 DOM.bip49change = $("#bip49 .change");
bddd5d9f
IC
80 DOM.bip84unavailable = $("#bip84 .unavailable");
81 DOM.bip84available = $("#bip84 .available");
4e9b492c
IC
82 DOM.bip84path = $("#bip84-path");
83 DOM.bip84purpose = $("#bip84 .purpose");
84 DOM.bip84coin = $("#bip84 .coin");
85 DOM.bip84account = $("#bip84 .account");
86 DOM.bip84accountXprv = $("#bip84 .account-xprv");
87 DOM.bip84accountXpub = $("#bip84 .account-xpub");
88 DOM.bip84change = $("#bip84 .change");
c49e8812
IC
89 DOM.bip141unavailable = $("#bip141 .unavailable");
90 DOM.bip141available = $("#bip141 .available");
91 DOM.bip141path = $("#bip141-path");
92 DOM.bip141semantics = $(".bip141-semantics");
88df3739 93 DOM.generatedStrength = $(".generate-container .strength");
85c90672 94 DOM.generatedStrengthWarning = $(".generate-container .warning");
146e089e 95 DOM.hardenedAddresses = $(".hardened-addresses");
e0f91e20
IC
96 DOM.bitcoinCashAddressTypeContainer = $(".bch-addr-type-container");
97 DOM.bitcoinCashAddressType = $("[name=bch-addr-type]")
0b39e9dc
IC
98 DOM.useBip38 = $(".use-bip38");
99 DOM.bip38Password = $(".bip38-password");
ebd8d4e8 100 DOM.addresses = $(".addresses");
a78f4e28
IC
101 DOM.csvTab = $("#csv-tab a");
102 DOM.csv = $(".csv");
ebd8d4e8
IC
103 DOM.rowsToAdd = $(".rows-to-add");
104 DOM.more = $(".more");
9183f9f6 105 DOM.moreRowsStartIndex = $(".more-rows-start-index");
ebd8d4e8
IC
106 DOM.feedback = $(".feedback");
107 DOM.tab = $(".derivation-type a");
108 DOM.indexToggle = $(".index-toggle");
109 DOM.addressToggle = $(".address-toggle");
1b12b2f5 110 DOM.publicKeyToggle = $(".public-key-toggle");
ebd8d4e8 111 DOM.privateKeyToggle = $(".private-key-toggle");
5ee7bb9e 112 DOM.languages = $(".languages a");
e00964cc 113 DOM.qrContainer = $(".qr-container");
97811c29 114 DOM.qrHider = DOM.qrContainer.find(".qr-hider");
e00964cc
IC
115 DOM.qrImage = DOM.qrContainer.find(".qr-image");
116 DOM.qrHint = DOM.qrContainer.find(".qr-hint");
117 DOM.showQrEls = $("[data-show-qr]");
ebd8d4e8 118
ebd8d4e8
IC
119 function init() {
120 // Events
85c90672 121 DOM.generatedStrength.on("change", generatedStrengthChanged);
d6cedc94 122 DOM.network.on("change", networkChanged);
29bf60f5 123 DOM.bip32Client.on("change", bip32ClientChanged);
c6624d51
IC
124 DOM.useEntropy.on("change", setEntropyVisibility);
125 DOM.entropy.on("input", delayedEntropyChanged);
3599674d 126 DOM.entropyMnemonicLength.on("change", entropyChanged);
a19a5498
IC
127 DOM.phrase.on("input", delayedPhraseChanged);
128 DOM.passphrase.on("input", delayedPhraseChanged);
ebd8d4e8
IC
129 DOM.generate.on("click", generateClicked);
130 DOM.more.on("click", showMore);
efe41586 131 DOM.rootKey.on("input", delayedRootKeyChanged);
3abab9b0 132 DOM.litecoinUseLtub.on("change", litecoinUseLtubChanged);
efe41586 133 DOM.bip32path.on("input", calcForDerivationPath);
efe41586
IC
134 DOM.bip44account.on("input", calcForDerivationPath);
135 DOM.bip44change.on("input", calcForDerivationPath);
6c08f364
IC
136 DOM.bip49account.on("input", calcForDerivationPath);
137 DOM.bip49change.on("input", calcForDerivationPath);
4e9b492c
IC
138 DOM.bip84account.on("input", calcForDerivationPath);
139 DOM.bip84change.on("input", calcForDerivationPath);
c49e8812
IC
140 DOM.bip141path.on("input", calcForDerivationPath);
141 DOM.bip141semantics.on("change", tabChanged);
93c3ef47 142 DOM.tab.on("shown.bs.tab", tabChanged);
146e089e 143 DOM.hardenedAddresses.on("change", calcForDerivationPath);
0b39e9dc
IC
144 DOM.useBip38.on("change", calcForDerivationPath);
145 DOM.bip38Password.on("change", calcForDerivationPath);
ebd8d4e8
IC
146 DOM.indexToggle.on("click", toggleIndexes);
147 DOM.addressToggle.on("click", toggleAddresses);
1b12b2f5 148 DOM.publicKeyToggle.on("click", togglePublicKeys);
ebd8d4e8 149 DOM.privateKeyToggle.on("click", togglePrivateKeys);
a78f4e28 150 DOM.csvTab.on("click", updateCsv);
5ee7bb9e 151 DOM.languages.on("click", languageChanged);
e0f91e20 152 DOM.bitcoinCashAddressType.on("change", bitcoinCashAddressTypeChange);
e00964cc 153 setQrEvents(DOM.showQrEls);
ebd8d4e8
IC
154 disableForms();
155 hidePending();
156 hideValidationError();
7f15cb6e 157 populateNetworkSelect();
b4fd763c 158 populateClientSelect();
ebd8d4e8
IC
159 }
160
161 // Event handlers
162
85c90672
IC
163 function generatedStrengthChanged() {
164 var strength = parseInt(DOM.generatedStrength.val());
165 if (strength < 12) {
166 DOM.generatedStrengthWarning.removeClass("hidden");
167 }
168 else {
169 DOM.generatedStrengthWarning.addClass("hidden");
170 }
171 }
172
d6cedc94 173 function networkChanged(e) {
6c08f364
IC
174 clearDerivedKeys();
175 clearAddressesList();
3abab9b0 176 DOM.litecoinLtubContainer.addClass("hidden");
e0f91e20 177 DOM.bitcoinCashAddressTypeContainer.addClass("hidden");
54563907 178 var networkIndex = e.target.value;
6c08f364
IC
179 var network = networks[networkIndex];
180 network.onSelect();
c147cb5e 181 adjustNetworkForSegwit();
54563907
IC
182 if (seed != null) {
183 phraseChanged();
184 }
185 else {
186 rootKeyChanged();
187 }
d6cedc94 188 }
29bf60f5
IC
189
190 function bip32ClientChanged(e) {
191 var clientIndex = DOM.bip32Client.val();
192 if (clientIndex == "custom") {
193 DOM.bip32path.prop("readonly", false);
b4fd763c
AG
194 }
195 else {
29bf60f5
IC
196 DOM.bip32path.prop("readonly", true);
197 clients[clientIndex].onSelect();
198 if (seed != null) {
199 phraseChanged();
200 }
201 else {
202 rootKeyChanged();
203 }
b4fd763c
AG
204 }
205 }
d6cedc94 206
c6624d51
IC
207 function setEntropyVisibility() {
208 if (isUsingOwnEntropy()) {
209 DOM.entropyContainer.removeClass("hidden");
210 DOM.generateContainer.addClass("hidden");
211 DOM.phrase.prop("readonly", true);
212 DOM.entropy.focus();
213 entropyChanged();
214 }
215 else {
216 DOM.entropyContainer.addClass("hidden");
217 DOM.generateContainer.removeClass("hidden");
218 DOM.phrase.prop("readonly", false);
057722b0 219 hidePending();
c6624d51
IC
220 }
221 }
222
ebd8d4e8
IC
223 function delayedPhraseChanged() {
224 hideValidationError();
ed6d9d39
IC
225 seed = null;
226 bip32RootKey = null;
227 bip32ExtendedKey = null;
228 clearAddressesList();
ebd8d4e8
IC
229 showPending();
230 if (phraseChangeTimeoutEvent != null) {
231 clearTimeout(phraseChangeTimeoutEvent);
232 }
233 phraseChangeTimeoutEvent = setTimeout(phraseChanged, 400);
234 }
235
236 function phraseChanged() {
237 showPending();
5ee7bb9e 238 setMnemonicLanguage();
ebd8d4e8
IC
239 // Get the mnemonic phrase
240 var phrase = DOM.phrase.val();
241 var errorText = findPhraseErrors(phrase);
242 if (errorText) {
243 showValidationError(errorText);
244 return;
245 }
efe41586
IC
246 // Calculate and display
247 var passphrase = DOM.passphrase.val();
248 calcBip32RootKeyFromSeed(phrase, passphrase);
249 calcForDerivationPath();
74ab4cbe
IC
250 // Show the word indexes
251 showWordIndexes();
efe41586
IC
252 }
253
93c3ef47
IC
254 function tabChanged() {
255 showPending();
0fd67b59 256 adjustNetworkForSegwit();
93c3ef47
IC
257 var phrase = DOM.phrase.val();
258 if (phrase != "") {
259 // Calculate and display for mnemonic
260 var errorText = findPhraseErrors(phrase);
261 if (errorText) {
262 showValidationError(errorText);
263 return;
264 }
265 // Calculate and display
266 var passphrase = DOM.passphrase.val();
267 calcBip32RootKeyFromSeed(phrase, passphrase);
268 }
269 else {
270 // Calculate and display for root key
271 var rootKeyBase58 = DOM.rootKey.val();
272 var errorText = validateRootKey(rootKeyBase58);
273 if (errorText) {
274 showValidationError(errorText);
275 return;
276 }
277 // Calculate and display
278 calcBip32RootKeyFromBase58(rootKeyBase58);
279 }
280 calcForDerivationPath();
281 }
282
c6624d51
IC
283 function delayedEntropyChanged() {
284 hideValidationError();
285 showPending();
286 if (entropyChangeTimeoutEvent != null) {
287 clearTimeout(entropyChangeTimeoutEvent);
288 }
289 entropyChangeTimeoutEvent = setTimeout(entropyChanged, 400);
290 }
291
292 function entropyChanged() {
057722b0
IC
293 // If blank entropy, clear mnemonic, addresses, errors
294 if (DOM.entropy.val().trim().length == 0) {
295 clearDisplay();
0a84fe6a 296 clearEntropyFeedback();
057722b0
IC
297 DOM.phrase.val("");
298 showValidationError("Blank entropy");
299 return;
300 }
301 // Get the current phrase to detect changes
302 var phrase = DOM.phrase.val();
303 // Set the phrase from the entropy
c6624d51 304 setMnemonicFromEntropy();
057722b0
IC
305 // Recalc addresses if the phrase has changed
306 var newPhrase = DOM.phrase.val();
307 if (newPhrase != phrase) {
308 if (newPhrase.length == 0) {
309 clearDisplay();
310 }
311 else {
312 phraseChanged();
313 }
314 }
315 else {
316 hidePending();
317 }
c6624d51
IC
318 }
319
efe41586
IC
320 function delayedRootKeyChanged() {
321 // Warn if there is an existing mnemonic or passphrase.
322 if (DOM.phrase.val().length > 0 || DOM.passphrase.val().length > 0) {
323 if (!confirm("This will clear existing mnemonic and passphrase")) {
324 DOM.rootKey.val(bip32RootKey);
325 return
326 }
327 }
328 hideValidationError();
329 showPending();
330 // Clear existing mnemonic and passphrase
331 DOM.phrase.val("");
332 DOM.passphrase.val("");
333 seed = null;
334 if (rootKeyChangedTimeoutEvent != null) {
335 clearTimeout(rootKeyChangedTimeoutEvent);
336 }
337 rootKeyChangedTimeoutEvent = setTimeout(rootKeyChanged, 400);
338 }
339
340 function rootKeyChanged() {
341 showPending();
342 hideValidationError();
efe41586
IC
343 var rootKeyBase58 = DOM.rootKey.val();
344 var errorText = validateRootKey(rootKeyBase58);
345 if (errorText) {
346 showValidationError(errorText);
347 return;
348 }
349 // Calculate and display
350 calcBip32RootKeyFromBase58(rootKeyBase58);
351 calcForDerivationPath();
efe41586
IC
352 }
353
3abab9b0
IC
354 function litecoinUseLtubChanged() {
355 litecoinUseLtub = DOM.litecoinUseLtub.prop("checked");
356 if (litecoinUseLtub) {
1c2b8c6b 357 network = bitcoinjs.bitcoin.networks.litecoin;
3abab9b0
IC
358 }
359 else {
1c2b8c6b 360 network = bitcoinjs.bitcoin.networks.litecoinXprv;
3abab9b0
IC
361 }
362 phraseChanged();
363 }
364
efe41586 365 function calcForDerivationPath() {
6c08f364 366 clearDerivedKeys();
ba3cb9ec 367 clearAddressesList();
0eda54f5 368 showPending();
0fd67b59
IC
369 // Don't show segwit if it's selected but network doesn't support it
370 if (segwitSelected() && !networkHasSegwit()) {
c147cb5e 371 showSegwitUnavailable();
7733ac32 372 hidePending();
6c08f364
IC
373 return;
374 }
c147cb5e 375 showSegwitAvailable();
ebd8d4e8 376 // Get the derivation path
38523d36
IC
377 var derivationPath = getDerivationPath();
378 var errorText = findDerivationPathErrors(derivationPath);
ebd8d4e8
IC
379 if (errorText) {
380 showValidationError(errorText);
381 return;
382 }
5eaa6877 383 bip32ExtendedKey = calcBip32ExtendedKey(derivationPath);
c554e6ff
IC
384 if (bip44TabSelected()) {
385 displayBip44Info();
386 }
c49e8812 387 else if (bip49TabSelected()) {
6c08f364
IC
388 displayBip49Info();
389 }
4e9b492c
IC
390 else if (bip84TabSelected()) {
391 displayBip84Info();
392 }
ebd8d4e8 393 displayBip32Info();
ebd8d4e8
IC
394 }
395
396 function generateClicked() {
c6624d51
IC
397 if (isUsingOwnEntropy()) {
398 return;
399 }
ebd8d4e8
IC
400 clearDisplay();
401 showPending();
402 setTimeout(function() {
5ee7bb9e 403 setMnemonicLanguage();
ebd8d4e8
IC
404 var phrase = generateRandomPhrase();
405 if (!phrase) {
406 return;
407 }
408 phraseChanged();
409 }, 50);
410 }
411
5ee7bb9e
IC
412 function languageChanged() {
413 setTimeout(function() {
414 setMnemonicLanguage();
415 if (DOM.phrase.val().length > 0) {
416 var newPhrase = convertPhraseToNewLanguage();
417 DOM.phrase.val(newPhrase);
418 phraseChanged();
419 }
420 else {
421 DOM.generate.trigger("click");
422 }
423 }, 50);
424 }
425
e0f91e20 426 function bitcoinCashAddressTypeChange() {
fe8f2d14
IC
427 phraseChanged();
428 }
429
ebd8d4e8 430 function toggleIndexes() {
700901cd 431 showIndex = !showIndex;
ebd8d4e8
IC
432 $("td.index span").toggleClass("invisible");
433 }
434
435 function toggleAddresses() {
700901cd 436 showAddress = !showAddress;
ebd8d4e8
IC
437 $("td.address span").toggleClass("invisible");
438 }
439
1b12b2f5
IC
440 function togglePublicKeys() {
441 showPubKey = !showPubKey;
442 $("td.pubkey span").toggleClass("invisible");
443 }
444
ebd8d4e8 445 function togglePrivateKeys() {
700901cd 446 showPrivKey = !showPrivKey;
ebd8d4e8
IC
447 $("td.privkey span").toggleClass("invisible");
448 }
449
450 // Private methods
451
452 function generateRandomPhrase() {
453 if (!hasStrongRandom()) {
454 var errorText = "This browser does not support strong randomness";
455 showValidationError(errorText);
456 return;
457 }
74ab4cbe 458 // get the amount of entropy to use
88df3739 459 var numWords = parseInt(DOM.generatedStrength.val());
ebd8d4e8 460 var strength = numWords / 3 * 32;
74ab4cbe
IC
461 var buffer = new Uint8Array(strength / 8);
462 // create secure entropy
463 var data = crypto.getRandomValues(buffer);
464 // show the words
465 var words = mnemonic.toMnemonic(data);
ebd8d4e8 466 DOM.phrase.val(words);
74ab4cbe
IC
467 // show the entropy
468 var entropyHex = uint8ArrayToHex(data);
469 DOM.entropy.val(entropyHex);
470 // ensure entropy fields are consistent with what is being displayed
471 DOM.entropyMnemonicLength.val("raw");
ebd8d4e8
IC
472 return words;
473 }
474
efe41586 475 function calcBip32RootKeyFromSeed(phrase, passphrase) {
3e0ed16a 476 seed = mnemonic.toSeed(phrase, passphrase);
a0091a40 477 bip32RootKey = bitcoinjs.bitcoin.HDNode.fromSeedHex(seed, network);
efe41586
IC
478 }
479
480 function calcBip32RootKeyFromBase58(rootKeyBase58) {
6f7fa353
IC
481 // try parsing with various segwit network params since this extended
482 // key may be from any one of them.
483 if (networkHasSegwit()) {
484 var n = network;
485 if ("baseNetwork" in n) {
486 n = bitcoinjs.bitcoin.networks[n.baseNetwork];
487 }
488 // try parsing using base network params
489 try {
490 bip32RootKey = bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n);
491 return;
492 }
493 catch (e) {}
494 // try parsing using p2wpkh params
495 if ("p2wpkh" in n) {
496 try {
497 bip32RootKey = bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n.p2wpkh);
498 return;
499 }
500 catch (e) {}
501 }
502 // try parsing using p2wpkh-in-p2sh network params
503 if ("p2wpkhInP2sh" in n) {
504 try {
505 bip32RootKey = bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n.p2wpkhInP2sh);
506 return;
507 }
508 catch (e) {}
509 }
510 }
511 // try the network params as currently specified
a0091a40 512 bip32RootKey = bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, network);
efe41586
IC
513 }
514
515 function calcBip32ExtendedKey(path) {
0a1f0259
IC
516 // Check there's a root key to derive from
517 if (!bip32RootKey) {
518 return bip32RootKey;
519 }
5eaa6877 520 var extendedKey = bip32RootKey;
ebd8d4e8
IC
521 // Derive the key from the path
522 var pathBits = path.split("/");
523 for (var i=0; i<pathBits.length; i++) {
524 var bit = pathBits[i];
525 var index = parseInt(bit);
526 if (isNaN(index)) {
527 continue;
528 }
529 var hardened = bit[bit.length-1] == "'";
a0091a40 530 var isPriv = !(extendedKey.isNeutered());
ba3cb9ec
IC
531 var invalidDerivationPath = hardened && !isPriv;
532 if (invalidDerivationPath) {
533 extendedKey = null;
534 }
535 else if (hardened) {
5eaa6877 536 extendedKey = extendedKey.deriveHardened(index);
ebd8d4e8
IC
537 }
538 else {
5eaa6877 539 extendedKey = extendedKey.derive(index);
ebd8d4e8
IC
540 }
541 }
5eaa6877 542 return extendedKey
ebd8d4e8
IC
543 }
544
545 function showValidationError(errorText) {
546 DOM.feedback
547 .text(errorText)
548 .show();
549 }
550
551 function hideValidationError() {
552 DOM.feedback
553 .text("")
554 .hide();
555 }
556
557 function findPhraseErrors(phrase) {
ebd8d4e8 558 // Preprocess the words
783981de 559 phrase = mnemonic.normalizeString(phrase);
5ee7bb9e 560 var words = phraseToWordArray(phrase);
057722b0
IC
561 // Detect blank phrase
562 if (words.length == 0) {
563 return "Blank mnemonic";
564 }
563e401a 565 // Check each word
5ee7bb9e
IC
566 for (var i=0; i<words.length; i++) {
567 var word = words[i];
568 var language = getLanguage();
569 if (WORDLISTS[language].indexOf(word) == -1) {
563e401a
IC
570 console.log("Finding closest match to " + word);
571 var nearestWord = findNearestWord(word);
572 return word + " not in wordlist, did you mean " + nearestWord + "?";
573 }
574 }
ebd8d4e8 575 // Check the words are valid
5ee7bb9e 576 var properPhrase = wordArrayToPhrase(words);
ebd8d4e8
IC
577 var isValid = mnemonic.check(properPhrase);
578 if (!isValid) {
579 return "Invalid mnemonic";
580 }
581 return false;
582 }
583
efe41586 584 function validateRootKey(rootKeyBase58) {
6f7fa353
IC
585 // try various segwit network params since this extended key may be from
586 // any one of them.
587 if (networkHasSegwit()) {
588 var n = network;
589 if ("baseNetwork" in n) {
590 n = bitcoinjs.bitcoin.networks[n.baseNetwork];
591 }
592 // try parsing using base network params
593 try {
594 bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n);
595 return "";
596 }
597 catch (e) {}
598 // try parsing using p2wpkh params
599 if ("p2wpkh" in n) {
600 try {
601 bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n.p2wpkh);
602 return "";
603 }
604 catch (e) {}
605 }
606 // try parsing using p2wpkh-in-p2sh network params
607 if ("p2wpkhInP2sh" in n) {
608 try {
609 bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n.p2wpkhInP2sh);
610 return "";
611 }
612 catch (e) {}
613 }
614 }
615 // try the network params as currently specified
efe41586 616 try {
cd7c8327 617 bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, network);
efe41586
IC
618 }
619 catch (e) {
620 return "Invalid root key";
621 }
622 return "";
623 }
624
38523d36 625 function getDerivationPath() {
32fab2c3 626 if (bip44TabSelected()) {
38523d36
IC
627 var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44);
628 var coin = parseIntNoNaN(DOM.bip44coin.val(), 0);
629 var account = parseIntNoNaN(DOM.bip44account.val(), 0);
630 var change = parseIntNoNaN(DOM.bip44change.val(), 0);
631 var path = "m/";
632 path += purpose + "'/";
633 path += coin + "'/";
634 path += account + "'/";
635 path += change;
636 DOM.bip44path.val(path);
637 var derivationPath = DOM.bip44path.val();
638 console.log("Using derivation path from BIP44 tab: " + derivationPath);
639 return derivationPath;
640 }
c49e8812 641 else if (bip49TabSelected()) {
6c08f364
IC
642 var purpose = parseIntNoNaN(DOM.bip49purpose.val(), 49);
643 var coin = parseIntNoNaN(DOM.bip49coin.val(), 0);
644 var account = parseIntNoNaN(DOM.bip49account.val(), 0);
645 var change = parseIntNoNaN(DOM.bip49change.val(), 0);
646 var path = "m/";
647 path += purpose + "'/";
648 path += coin + "'/";
649 path += account + "'/";
650 path += change;
651 DOM.bip49path.val(path);
652 var derivationPath = DOM.bip49path.val();
653 console.log("Using derivation path from BIP49 tab: " + derivationPath);
654 return derivationPath;
655 }
4e9b492c
IC
656 else if (bip84TabSelected()) {
657 var purpose = parseIntNoNaN(DOM.bip84purpose.val(), 84);
658 var coin = parseIntNoNaN(DOM.bip84coin.val(), 0);
659 var account = parseIntNoNaN(DOM.bip84account.val(), 0);
660 var change = parseIntNoNaN(DOM.bip84change.val(), 0);
661 var path = "m/";
662 path += purpose + "'/";
663 path += coin + "'/";
664 path += account + "'/";
665 path += change;
666 DOM.bip84path.val(path);
667 var derivationPath = DOM.bip84path.val();
668 console.log("Using derivation path from BIP84 tab: " + derivationPath);
669 return derivationPath;
670 }
32fab2c3 671 else if (bip32TabSelected()) {
38523d36
IC
672 var derivationPath = DOM.bip32path.val();
673 console.log("Using derivation path from BIP32 tab: " + derivationPath);
674 return derivationPath;
675 }
c49e8812
IC
676 else if (bip141TabSelected()) {
677 var derivationPath = DOM.bip141path.val();
678 console.log("Using derivation path from BIP141 tab: " + derivationPath);
679 return derivationPath;
680 }
38523d36
IC
681 else {
682 console.log("Unknown derivation path");
683 }
684 }
685
ebd8d4e8 686 function findDerivationPathErrors(path) {
30c9e79d
IC
687 // TODO is not perfect but is better than nothing
688 // Inspired by
689 // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#test-vectors
690 // and
691 // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#extended-keys
692 var maxDepth = 255; // TODO verify this!!
693 var maxIndexValue = Math.pow(2, 31); // TODO verify this!!
694 if (path[0] != "m") {
695 return "First character must be 'm'";
696 }
697 if (path.length > 1) {
698 if (path[1] != "/") {
699 return "Separator must be '/'";
700 }
701 var indexes = path.split("/");
702 if (indexes.length > maxDepth) {
703 return "Derivation depth is " + indexes.length + ", must be less than " + maxDepth;
704 }
705 for (var depth = 1; depth<indexes.length; depth++) {
706 var index = indexes[depth];
707 var invalidChars = index.replace(/^[0-9]+'?$/g, "")
708 if (invalidChars.length > 0) {
709 return "Invalid characters " + invalidChars + " found at depth " + depth;
710 }
711 var indexValue = parseInt(index.replace("'", ""));
712 if (isNaN(depth)) {
713 return "Invalid number at depth " + depth;
714 }
715 if (indexValue > maxIndexValue) {
716 return "Value of " + indexValue + " at depth " + depth + " must be less than " + maxIndexValue;
717 }
718 }
719 }
0a1f0259
IC
720 // Check root key exists or else derivation path is useless!
721 if (!bip32RootKey) {
722 return "No root key";
723 }
ba3cb9ec 724 // Check no hardened derivation path when using xpub keys
b18eb97a
IC
725 var hardenedPath = path.indexOf("'") > -1;
726 var hardenedAddresses = bip32TabSelected() && DOM.hardenedAddresses.prop("checked");
727 var hardened = hardenedPath || hardenedAddresses;
a0091a40 728 var isXpubkey = bip32RootKey.isNeutered();
ba3cb9ec
IC
729 if (hardened && isXpubkey) {
730 return "Hardened derivation path is invalid with xpub key";
731 }
ebd8d4e8
IC
732 return false;
733 }
734
c554e6ff
IC
735 function displayBip44Info() {
736 // Get the derivation path for the account
737 var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44);
738 var coin = parseIntNoNaN(DOM.bip44coin.val(), 0);
739 var account = parseIntNoNaN(DOM.bip44account.val(), 0);
740 var path = "m/";
741 path += purpose + "'/";
742 path += coin + "'/";
743 path += account + "'/";
744 // Calculate the account extended keys
745 var accountExtendedKey = calcBip32ExtendedKey(path);
746 var accountXprv = accountExtendedKey.toBase58();
a0091a40 747 var accountXpub = accountExtendedKey.neutered().toBase58();
c554e6ff
IC
748 // Display the extended keys
749 DOM.bip44accountXprv.val(accountXprv);
750 DOM.bip44accountXpub.val(accountXpub);
751 }
752
6c08f364
IC
753 function displayBip49Info() {
754 // Get the derivation path for the account
755 var purpose = parseIntNoNaN(DOM.bip49purpose.val(), 49);
756 var coin = parseIntNoNaN(DOM.bip49coin.val(), 0);
757 var account = parseIntNoNaN(DOM.bip49account.val(), 0);
758 var path = "m/";
759 path += purpose + "'/";
760 path += coin + "'/";
761 path += account + "'/";
762 // Calculate the account extended keys
763 var accountExtendedKey = calcBip32ExtendedKey(path);
764 var accountXprv = accountExtendedKey.toBase58();
765 var accountXpub = accountExtendedKey.neutered().toBase58();
766 // Display the extended keys
767 DOM.bip49accountXprv.val(accountXprv);
768 DOM.bip49accountXpub.val(accountXpub);
769 }
770
4e9b492c
IC
771 function displayBip84Info() {
772 // Get the derivation path for the account
773 var purpose = parseIntNoNaN(DOM.bip84purpose.val(), 84);
774 var coin = parseIntNoNaN(DOM.bip84coin.val(), 0);
775 var account = parseIntNoNaN(DOM.bip84account.val(), 0);
776 var path = "m/";
777 path += purpose + "'/";
778 path += coin + "'/";
779 path += account + "'/";
780 // Calculate the account extended keys
781 var accountExtendedKey = calcBip32ExtendedKey(path);
782 var accountXprv = accountExtendedKey.toBase58();
783 var accountXpub = accountExtendedKey.neutered().toBase58();
784 // Display the extended keys
785 DOM.bip84accountXprv.val(accountXprv);
786 DOM.bip84accountXpub.val(accountXpub);
787 }
788
ebd8d4e8
IC
789 function displayBip32Info() {
790 // Display the key
3e0ed16a 791 DOM.seed.val(seed);
ebd8d4e8
IC
792 var rootKey = bip32RootKey.toBase58();
793 DOM.rootKey.val(rootKey);
ba3cb9ec 794 var xprvkeyB58 = "NA";
a0091a40 795 if (!bip32ExtendedKey.isNeutered()) {
ba3cb9ec
IC
796 xprvkeyB58 = bip32ExtendedKey.toBase58();
797 }
798 var extendedPrivKey = xprvkeyB58;
ebd8d4e8 799 DOM.extendedPrivKey.val(extendedPrivKey);
a0091a40 800 var extendedPubKey = bip32ExtendedKey.neutered().toBase58();
ebd8d4e8
IC
801 DOM.extendedPubKey.val(extendedPubKey);
802 // Display the addresses and privkeys
803 clearAddressesList();
ba678b11
IC
804 var initialAddressCount = parseInt(DOM.rowsToAdd.val());
805 displayAddresses(0, initialAddressCount);
ebd8d4e8
IC
806 }
807
808 function displayAddresses(start, total) {
40892aba
IC
809 generationProcesses.push(new (function() {
810
811 var rows = [];
812
813 this.stop = function() {
814 for (var i=0; i<rows.length; i++) {
815 rows[i].shouldGenerate = false;
816 }
0eda54f5 817 hidePending();
40892aba
IC
818 }
819
820 for (var i=0; i<total; i++) {
821 var index = i + start;
0eda54f5
IC
822 var isLast = i == total - 1;
823 rows.push(new TableRow(index, isLast));
40892aba
IC
824 }
825
826 })());
ebd8d4e8
IC
827 }
828
0fd67b59 829 function segwitSelected() {
4e9b492c 830 return bip49TabSelected() || bip84TabSelected() || bip141TabSelected();
c49e8812
IC
831 }
832
833 function p2wpkhSelected() {
4e9b492c
IC
834 return bip84TabSelected() ||
835 bip141TabSelected() && DOM.bip141semantics.val() == "p2wpkh";
c49e8812
IC
836 }
837
838 function p2wpkhInP2shSelected() {
839 return bip49TabSelected() ||
840 (bip141TabSelected() && DOM.bip141semantics.val() == "p2wpkh-p2sh");
93c3ef47
IC
841 }
842
0eda54f5 843 function TableRow(index, isLast) {
a8c45487 844
40892aba
IC
845 var self = this;
846 this.shouldGenerate = true;
146e089e 847 var useHardenedAddresses = DOM.hardenedAddresses.prop("checked");
0b39e9dc
IC
848 var useBip38 = DOM.useBip38.prop("checked");
849 var bip38password = DOM.bip38Password.val();
0fd67b59
IC
850 var isSegwit = segwitSelected();
851 var segwitAvailable = networkHasSegwit();
c49e8812
IC
852 var isP2wpkh = p2wpkhSelected();
853 var isP2wpkhInP2sh = p2wpkhInP2shSelected();
146e089e 854
a8c45487
IC
855 function init() {
856 calculateValues();
857 }
858
859 function calculateValues() {
860 setTimeout(function() {
40892aba
IC
861 if (!self.shouldGenerate) {
862 return;
863 }
cc61ec30 864 // derive HDkey for this row of the table
a0091a40 865 var key = "NA";
146e089e
IC
866 if (useHardenedAddresses) {
867 key = bip32ExtendedKey.deriveHardened(index);
868 }
869 else {
870 key = bip32ExtendedKey.derive(index);
871 }
cc61ec30
IC
872 // bip38 requires uncompressed keys
873 // see https://github.com/iancoleman/bip39/issues/140#issuecomment-352164035
874 var keyPair = key.keyPair;
875 var useUncompressed = useBip38;
876 if (useUncompressed) {
29c30bf3 877 keyPair = new bitcoinjs.bitcoin.ECPair(keyPair.d, null, { network: network, compressed: false });
cc61ec30
IC
878 }
879 // get address
880 var address = keyPair.getAddress().toString();
881 // get privkey
882 var hasPrivkey = !key.isNeutered();
ba3cb9ec 883 var privkey = "NA";
cc61ec30 884 if (hasPrivkey) {
29c30bf3 885 privkey = keyPair.toWIF();
cc61ec30
IC
886 // BIP38 encode private key if required
887 if (useBip38) {
888 privkey = bitcoinjsBip38.encrypt(keyPair.d.toBuffer(), false, bip38password, function(p) {
889 console.log("Progressed " + p.percent.toFixed(1) + "% for index " + index);
890 });
891 }
ba3cb9ec 892 }
cc61ec30
IC
893 // get pubkey
894 var pubkey = keyPair.getPublicKeyBuffer().toString('hex');
38523d36 895 var indexText = getDerivationPath() + "/" + index;
146e089e
IC
896 if (useHardenedAddresses) {
897 indexText = indexText + "'";
898 }
0edac945 899 // Ethereum values are different
534481b6 900 if (networks[DOM.network.val()].name == "ETH - Ethereum") {
cc61ec30 901 var privKeyBuffer = keyPair.d.toBuffer(32);
0edac945
IC
902 privkey = privKeyBuffer.toString('hex');
903 var addressBuffer = ethUtil.privateToAddress(privKeyBuffer);
49b21f12
IC
904 var hexAddress = addressBuffer.toString('hex');
905 var checksumAddress = ethUtil.toChecksumAddress(hexAddress);
906 address = ethUtil.addHexPrefix(checksumAddress);
d0239db4
IC
907 privkey = ethUtil.addHexPrefix(privkey);
908 pubkey = ethUtil.addHexPrefix(pubkey);
0edac945 909 }
64a7d2aa 910 // Ripple values are different
534481b6 911 if (networks[DOM.network.val()].name == "XRP - Ripple") {
64a7d2aa 912 privkey = convertRipplePriv(privkey);
913 address = convertRippleAdrr(address);
914 }
e0f91e20
IC
915 // Bitcoin Cash address format may vary
916 if (networks[DOM.network.val()].name == "BCH - Bitcoin Cash") {
917 var bchAddrType = DOM.bitcoinCashAddressType.filter(":checked").val();
918 if (bchAddrType == "cashaddr") {
919 address = bchaddr.toCashAddress(address);
920 }
921 else if (bchAddrType == "bitpay") {
922 address = bchaddr.toBitpayAddress(address);
923 }
924 }
0fd67b59
IC
925 // Segwit addresses are different
926 if (isSegwit) {
927 if (!segwitAvailable) {
6c08f364
IC
928 return;
929 }
c49e8812
IC
930 if (isP2wpkh) {
931 var keyhash = bitcoinjs.bitcoin.crypto.hash160(key.getPublicKeyBuffer());
932 var scriptpubkey = bitcoinjs.bitcoin.script.witnessPubKeyHash.output.encode(keyhash);
933 address = bitcoinjs.bitcoin.address.fromOutputScript(scriptpubkey, network)
934 }
935 else if (isP2wpkhInP2sh) {
936 var keyhash = bitcoinjs.bitcoin.crypto.hash160(key.getPublicKeyBuffer());
937 var scriptsig = bitcoinjs.bitcoin.script.witnessPubKeyHash.output.encode(keyhash);
938 var addressbytes = bitcoinjs.bitcoin.crypto.hash160(scriptsig);
939 var scriptpubkey = bitcoinjs.bitcoin.script.scriptHash.output.encode(addressbytes);
940 address = bitcoinjs.bitcoin.address.fromOutputScript(scriptpubkey, network)
941 }
6c08f364 942 }
1b12b2f5 943 addAddressToList(indexText, address, pubkey, privkey);
0eda54f5
IC
944 if (isLast) {
945 hidePending();
a78f4e28 946 updateCsv();
0eda54f5 947 }
a8c45487
IC
948 }, 50)
949 }
950
951 init();
952
953 }
954
ebd8d4e8 955 function showMore() {
ebd8d4e8
IC
956 var rowsToAdd = parseInt(DOM.rowsToAdd.val());
957 if (isNaN(rowsToAdd)) {
958 rowsToAdd = 20;
959 DOM.rowsToAdd.val("20");
960 }
9183f9f6
IC
961 var start = parseInt(DOM.moreRowsStartIndex.val())
962 if (isNaN(start)) {
963 start = lastIndexInTable() + 1;
964 }
965 else {
966 var newStart = start + rowsToAdd;
967 DOM.moreRowsStartIndex.val(newStart);
968 }
ebd8d4e8
IC
969 if (rowsToAdd > 200) {
970 var msg = "Generating " + rowsToAdd + " rows could take a while. ";
971 msg += "Do you want to continue?";
972 if (!confirm(msg)) {
973 return;
974 }
975 }
ebd8d4e8 976 displayAddresses(start, rowsToAdd);
ebd8d4e8
IC
977 }
978
979 function clearDisplay() {
980 clearAddressesList();
fa2e4e93 981 clearKeys();
ebd8d4e8
IC
982 hideValidationError();
983 }
984
985 function clearAddressesList() {
986 DOM.addresses.empty();
a78f4e28 987 DOM.csv.val("");
40892aba
IC
988 stopGenerating();
989 }
990
991 function stopGenerating() {
992 while (generationProcesses.length > 0) {
993 var generation = generationProcesses.shift();
994 generation.stop();
995 }
ebd8d4e8
IC
996 }
997
fa2e4e93
IC
998 function clearKeys() {
999 clearRootKey();
1000 clearDerivedKeys();
1001 }
1002
1003 function clearRootKey() {
ebd8d4e8 1004 DOM.rootKey.val("");
fa2e4e93
IC
1005 }
1006
1007 function clearDerivedKeys() {
ebd8d4e8
IC
1008 DOM.extendedPrivKey.val("");
1009 DOM.extendedPubKey.val("");
fa2e4e93
IC
1010 DOM.bip44accountXprv.val("");
1011 DOM.bip44accountXpub.val("");
ebd8d4e8
IC
1012 }
1013
1b12b2f5 1014 function addAddressToList(indexText, address, pubkey, privkey) {
ebd8d4e8 1015 var row = $(addressRowTemplate.html());
700901cd
IC
1016 // Elements
1017 var indexCell = row.find(".index span");
1018 var addressCell = row.find(".address span");
1b12b2f5 1019 var pubkeyCell = row.find(".pubkey span");
700901cd
IC
1020 var privkeyCell = row.find(".privkey span");
1021 // Content
ae30fed8 1022 indexCell.text(indexText);
700901cd 1023 addressCell.text(address);
1b12b2f5 1024 pubkeyCell.text(pubkey);
700901cd
IC
1025 privkeyCell.text(privkey);
1026 // Visibility
1027 if (!showIndex) {
1028 indexCell.addClass("invisible");
1029 }
1030 if (!showAddress) {
1031 addressCell.addClass("invisible");
1032 }
1b12b2f5
IC
1033 if (!showPubKey) {
1034 pubkeyCell.addClass("invisible");
1035 }
700901cd 1036 if (!showPrivKey) {
6d628db7 1037 privkeyCell.addClass("invisible");
700901cd 1038 }
ebd8d4e8 1039 DOM.addresses.append(row);
e00964cc
IC
1040 var rowShowQrEls = row.find("[data-show-qr]");
1041 setQrEvents(rowShowQrEls);
ebd8d4e8
IC
1042 }
1043
1044 function hasStrongRandom() {
1045 return 'crypto' in window && window['crypto'] !== null;
1046 }
1047
1048 function disableForms() {
1049 $("form").on("submit", function(e) {
1050 e.preventDefault();
1051 });
1052 }
1053
ebd8d4e8
IC
1054 function parseIntNoNaN(val, defaultVal) {
1055 var v = parseInt(val);
1056 if (isNaN(v)) {
1057 return defaultVal;
1058 }
1059 return v;
1060 }
1061
1062 function showPending() {
1063 DOM.feedback
1064 .text("Calculating...")
1065 .show();
1066 }
1067
563e401a 1068 function findNearestWord(word) {
5ee7bb9e
IC
1069 var language = getLanguage();
1070 var words = WORDLISTS[language];
563e401a
IC
1071 var minDistance = 99;
1072 var closestWord = words[0];
1073 for (var i=0; i<words.length; i++) {
1074 var comparedTo = words[i];
6ea15134
IC
1075 if (comparedTo.indexOf(word) == 0) {
1076 return comparedTo;
1077 }
563e401a
IC
1078 var distance = Levenshtein.get(word, comparedTo);
1079 if (distance < minDistance) {
1080 closestWord = comparedTo;
1081 minDistance = distance;
1082 }
1083 }
1084 return closestWord;
1085 }
1086
ebd8d4e8
IC
1087 function hidePending() {
1088 DOM.feedback
1089 .text("")
1090 .hide();
1091 }
1092
7f15cb6e
IC
1093 function populateNetworkSelect() {
1094 for (var i=0; i<networks.length; i++) {
1095 var network = networks[i];
1096 var option = $("<option>");
1097 option.attr("value", i);
1098 option.text(network.name);
7b742f87
IC
1099 if (network.name == "BTC - Bitcoin") {
1100 option.prop("selected", true);
1101 }
7f15cb6e
IC
1102 DOM.phraseNetwork.append(option);
1103 }
1104 }
29bf60f5 1105
b4fd763c
AG
1106 function populateClientSelect() {
1107 for (var i=0; i<clients.length; i++) {
1108 var client = clients[i];
1109 var option = $("<option>");
1110 option.attr("value", i);
1111 option.text(client.name);
29bf60f5 1112 DOM.bip32Client.append(option);
b4fd763c
AG
1113 }
1114 }
7f15cb6e 1115
5ee7bb9e
IC
1116 function getLanguage() {
1117 var defaultLanguage = "english";
1118 // Try to get from existing phrase
1119 var language = getLanguageFromPhrase();
1120 // Try to get from url if not from phrase
1121 if (language.length == 0) {
1122 language = getLanguageFromUrl();
1123 }
1124 // Default to English if no other option
1125 if (language.length == 0) {
1126 language = defaultLanguage;
1127 }
1128 return language;
1129 }
1130
1131 function getLanguageFromPhrase(phrase) {
1132 // Check if how many words from existing phrase match a language.
1133 var language = "";
1134 if (!phrase) {
1135 phrase = DOM.phrase.val();
1136 }
1137 if (phrase.length > 0) {
1138 var words = phraseToWordArray(phrase);
1139 var languageMatches = {};
1140 for (l in WORDLISTS) {
1141 // Track how many words match in this language
1142 languageMatches[l] = 0;
1143 for (var i=0; i<words.length; i++) {
1144 var wordInLanguage = WORDLISTS[l].indexOf(words[i]) > -1;
1145 if (wordInLanguage) {
1146 languageMatches[l]++;
1147 }
1148 }
1149 // Find languages with most word matches.
1150 // This is made difficult due to commonalities between Chinese
1151 // simplified vs traditional.
1152 var mostMatches = 0;
1153 var mostMatchedLanguages = [];
1154 for (var l in languageMatches) {
1155 var numMatches = languageMatches[l];
1156 if (numMatches > mostMatches) {
1157 mostMatches = numMatches;
1158 mostMatchedLanguages = [l];
1159 }
1160 else if (numMatches == mostMatches) {
1161 mostMatchedLanguages.push(l);
1162 }
1163 }
1164 }
1165 if (mostMatchedLanguages.length > 0) {
1166 // Use first language and warn if multiple detected
1167 language = mostMatchedLanguages[0];
1168 if (mostMatchedLanguages.length > 1) {
1169 console.warn("Multiple possible languages");
1170 console.warn(mostMatchedLanguages);
1171 }
1172 }
1173 }
1174 return language;
1175 }
1176
1177 function getLanguageFromUrl() {
c6624d51
IC
1178 for (var language in WORDLISTS) {
1179 if (window.location.hash.indexOf(language) > -1) {
1180 return language;
1181 }
1182 }
1183 return "";
5ee7bb9e
IC
1184 }
1185
1186 function setMnemonicLanguage() {
1187 var language = getLanguage();
1188 // Load the bip39 mnemonic generator for this language if required
1189 if (!(language in mnemonics)) {
1190 mnemonics[language] = new Mnemonic(language);
1191 }
1192 mnemonic = mnemonics[language];
1193 }
1194
1195 function convertPhraseToNewLanguage() {
1196 var oldLanguage = getLanguageFromPhrase();
1197 var newLanguage = getLanguageFromUrl();
1198 var oldPhrase = DOM.phrase.val();
1199 var oldWords = phraseToWordArray(oldPhrase);
1200 var newWords = [];
1201 for (var i=0; i<oldWords.length; i++) {
1202 var oldWord = oldWords[i];
1203 var index = WORDLISTS[oldLanguage].indexOf(oldWord);
1204 var newWord = WORDLISTS[newLanguage][index];
1205 newWords.push(newWord);
1206 }
1207 newPhrase = wordArrayToPhrase(newWords);
1208 return newPhrase;
1209 }
1210
1211 // TODO look at jsbip39 - mnemonic.splitWords
1212 function phraseToWordArray(phrase) {
1213 var words = phrase.split(/\s/g);
1214 var noBlanks = [];
1215 for (var i=0; i<words.length; i++) {
1216 var word = words[i];
1217 if (word.length > 0) {
1218 noBlanks.push(word);
1219 }
1220 }
1221 return noBlanks;
1222 }
1223
1224 // TODO look at jsbip39 - mnemonic.joinWords
1225 function wordArrayToPhrase(words) {
1226 var phrase = words.join(" ");
1227 var language = getLanguageFromPhrase(phrase);
956e44ef 1228 if (language == "japanese") {
5ee7bb9e
IC
1229 phrase = words.join("\u3000");
1230 }
1231 return phrase;
1232 }
1233
c6624d51
IC
1234 function isUsingOwnEntropy() {
1235 return DOM.useEntropy.prop("checked");
1236 }
1237
1238 function setMnemonicFromEntropy() {
0a84fe6a 1239 clearEntropyFeedback();
057722b0 1240 // Get entropy value
c6624d51 1241 var entropyStr = DOM.entropy.val();
057722b0 1242 // Work out minimum base for entropy
c6624d51 1243 var entropy = Entropy.fromString(entropyStr);
057722b0 1244 if (entropy.binaryStr.length == 0) {
c6624d51
IC
1245 return;
1246 }
1247 // Show entropy details
1cf1bbaf 1248 showEntropyFeedback(entropy);
3599674d
IC
1249 // Use entropy hash if not using raw entropy
1250 var bits = entropy.binaryStr;
1251 var mnemonicLength = DOM.entropyMnemonicLength.val();
1252 if (mnemonicLength != "raw") {
1253 // Get bits by hashing entropy with SHA256
1254 var hash = sjcl.hash.sha256.hash(entropy.cleanStr);
1255 var hex = sjcl.codec.hex.fromBits(hash);
1256 bits = BigInteger.parse(hex, 16).toString(2);
53aaab27 1257 while (bits.length % 256 != 0) {
3599674d
IC
1258 bits = "0" + bits;
1259 }
1260 // Truncate hash to suit number of words
1261 mnemonicLength = parseInt(mnemonicLength);
1262 var numberOfBits = 32 * mnemonicLength / 3;
1263 bits = bits.substring(0, numberOfBits);
645945a0
IC
1264 // show warning for weak entropy override
1265 if (mnemonicLength / 3 * 32 > entropy.binaryStr.length) {
1266 DOM.entropyWeakEntropyOverrideWarning.removeClass("hidden");
1267 }
1268 else {
1269 DOM.entropyWeakEntropyOverrideWarning.addClass("hidden");
1270 }
1271 }
1272 else {
1273 // hide warning for weak entropy override
1274 DOM.entropyWeakEntropyOverrideWarning.addClass("hidden");
3599674d 1275 }
c6624d51 1276 // Discard trailing entropy
3599674d 1277 var bitsToUse = Math.floor(bits.length / 32) * 32;
d6fd8ebf
IC
1278 var start = bits.length - bitsToUse;
1279 var binaryStr = bits.substring(start);
c6624d51
IC
1280 // Convert entropy string to numeric array
1281 var entropyArr = [];
adc8ce12
IC
1282 for (var i=0; i<binaryStr.length / 8; i++) {
1283 var byteAsBits = binaryStr.substring(i*8, i*8+8);
1284 var entropyByte = parseInt(byteAsBits, 2);
c6624d51
IC
1285 entropyArr.push(entropyByte)
1286 }
1287 // Convert entropy array to mnemonic
1288 var phrase = mnemonic.toMnemonic(entropyArr);
1289 // Set the mnemonic in the UI
1290 DOM.phrase.val(phrase);
74ab4cbe
IC
1291 // Show the word indexes
1292 showWordIndexes();
09d63290
IC
1293 // Show the checksum
1294 showChecksum();
c6624d51
IC
1295 }
1296
0a84fe6a 1297 function clearEntropyFeedback() {
20f459ce 1298 DOM.entropyCrackTime.text("...");
1cf1bbaf 1299 DOM.entropyType.text("");
0a84fe6a
IC
1300 DOM.entropyWordCount.text("0");
1301 DOM.entropyEventCount.text("0");
1302 DOM.entropyBitsPerEvent.text("0");
1303 DOM.entropyBits.text("0");
1304 DOM.entropyFiltered.html("&nbsp;");
1305 DOM.entropyBinary.html("&nbsp;");
c6624d51
IC
1306 }
1307
1cf1bbaf 1308 function showEntropyFeedback(entropy) {
6422c1cd 1309 var numberOfBits = entropy.binaryStr.length;
20f459ce 1310 var timeToCrack = "unknown";
9bc39377
IC
1311 try {
1312 var z = zxcvbn(entropy.base.parts.join(""));
20f459ce
IC
1313 timeToCrack = z.crack_times_display.offline_fast_hashing_1e10_per_second;
1314 if (z.feedback.warning != "") {
1315 timeToCrack = timeToCrack + " - " + z.feedback.warning;
1316 };
9bc39377
IC
1317 }
1318 catch (e) {
9bc39377
IC
1319 console.log("Error detecting entropy strength with zxcvbn:");
1320 console.log(e);
b299a6a7 1321 }
391c7f26 1322 var entropyTypeStr = getEntropyTypeStr(entropy);
6422c1cd 1323 var wordCount = Math.floor(numberOfBits / 32) * 3;
94959756 1324 var bitsPerEvent = entropy.bitsPerEvent.toFixed(2);
f8ca25c3 1325 var spacedBinaryStr = addSpacesEveryElevenBits(entropy.binaryStr);
b54c1218 1326 DOM.entropyFiltered.html(entropy.cleanHtml);
391c7f26 1327 DOM.entropyType.text(entropyTypeStr);
20f459ce 1328 DOM.entropyCrackTime.text(timeToCrack);
1cf1bbaf 1329 DOM.entropyEventCount.text(entropy.base.ints.length);
6422c1cd 1330 DOM.entropyBits.text(numberOfBits);
0a84fe6a 1331 DOM.entropyWordCount.text(wordCount);
d6cade86 1332 DOM.entropyBinary.text(spacedBinaryStr);
6422c1cd 1333 DOM.entropyBitsPerEvent.text(bitsPerEvent);
ee0981f1
IC
1334 // detect and warn of filtering
1335 var rawNoSpaces = DOM.entropy.val().replace(/\s/g, "");
1336 var cleanNoSpaces = entropy.cleanStr.replace(/\s/g, "");
1337 var isFiltered = rawNoSpaces.length != cleanNoSpaces.length;
1338 if (isFiltered) {
1339 DOM.entropyFilterWarning.removeClass('hidden');
1340 }
1341 else {
1342 DOM.entropyFilterWarning.addClass('hidden');
1343 }
02f05d3e
IC
1344 }
1345
391c7f26
IC
1346 function getEntropyTypeStr(entropy) {
1347 var typeStr = entropy.base.str;
1348 // Add some detail if these are cards
1349 if (entropy.base.asInt == 52) {
1350 var cardDetail = []; // array of message strings
1351 // Detect duplicates
1352 var dupes = [];
1353 var dupeTracker = {};
1354 for (var i=0; i<entropy.base.parts.length; i++) {
1355 var card = entropy.base.parts[i];
5c653a12
IC
1356 var cardUpper = card.toUpperCase();
1357 if (cardUpper in dupeTracker) {
391c7f26
IC
1358 dupes.push(card);
1359 }
5c653a12 1360 dupeTracker[cardUpper] = true;
391c7f26
IC
1361 }
1362 if (dupes.length > 0) {
1363 var dupeWord = "duplicates";
1364 if (dupes.length == 1) {
1365 dupeWord = "duplicate";
1366 }
1367 var msg = dupes.length + " " + dupeWord + ": " + dupes.slice(0,3).join(" ");
1368 if (dupes.length > 3) {
1369 msg += "...";
1370 }
1371 cardDetail.push(msg);
1372 }
1373 // Detect full deck
1374 var uniqueCards = [];
1375 for (var uniqueCard in dupeTracker) {
1376 uniqueCards.push(uniqueCard);
1377 }
1378 if (uniqueCards.length == 52) {
1379 cardDetail.unshift("full deck");
1380 }
bbc29c80
IC
1381 // Detect missing cards
1382 var values = "A23456789TJQK";
1383 var suits = "CDHS";
1384 var missingCards = [];
1385 for (var i=0; i<suits.length; i++) {
1386 for (var j=0; j<values.length; j++) {
1387 var card = values[j] + suits[i];
1388 if (!(card in dupeTracker)) {
1389 missingCards.push(card);
1390 }
1391 }
1392 }
1393 // Display missing cards if six or less, ie clearly going for full deck
1394 if (missingCards.length > 0 && missingCards.length <= 6) {
1395 var msg = missingCards.length + " missing: " + missingCards.slice(0,3).join(" ");
1396 if (missingCards.length > 3) {
1397 msg += "...";
1398 }
1399 cardDetail.push(msg);
1400 }
391c7f26
IC
1401 // Add card details to typeStr
1402 if (cardDetail.length > 0) {
1403 typeStr += " (" + cardDetail.join(", ") + ")";
1404 }
1405 }
1406 return typeStr;
1407 }
1408
e00964cc
IC
1409 function setQrEvents(els) {
1410 els.on("mouseenter", createQr);
1411 els.on("mouseleave", destroyQr);
1412 els.on("click", toggleQr);
1413 }
1414
1415 function createQr(e) {
1416 var content = e.target.textContent || e.target.value;
1417 if (content) {
9225b805
IC
1418 var qrEl = kjua({
1419 text: content,
1420 render: "canvas",
1421 size: 310,
1422 ecLevel: 'H',
1423 });
1424 DOM.qrImage.append(qrEl);
e00964cc 1425 if (!showQr) {
97811c29
IC
1426 DOM.qrHider.addClass("hidden");
1427 }
1428 else {
1429 DOM.qrHider.removeClass("hidden");
e00964cc
IC
1430 }
1431 DOM.qrContainer.removeClass("hidden");
1432 }
1433 }
1434
1435 function destroyQr() {
1436 DOM.qrImage.text("");
1437 DOM.qrContainer.addClass("hidden");
1438 }
1439
1440 function toggleQr() {
1441 showQr = !showQr;
97811c29 1442 DOM.qrHider.toggleClass("hidden");
e00964cc
IC
1443 DOM.qrHint.toggleClass("hidden");
1444 }
1445
32fab2c3
IC
1446 function bip44TabSelected() {
1447 return DOM.bip44tab.hasClass("active");
1448 }
1449
1450 function bip32TabSelected() {
1451 return DOM.bip32tab.hasClass("active");
1452 }
1453
0fd67b59 1454 function networkHasSegwit() {
c147cb5e
IC
1455 var n = network;
1456 if ("baseNetwork" in network) {
1457 n = bitcoinjs.bitcoin.networks[network.baseNetwork];
1458 }
1459 // check if only p2wpkh params are required
1460 if (p2wpkhSelected()) {
1461 return "p2wpkh" in n;
1462 }
1463 // check if only p2wpkh-in-p2sh params are required
1464 else if (p2wpkhInP2shSelected()) {
1465 return "p2wpkhInP2sh" in n;
1466 }
1467 // require both if it's unclear which params are required
1468 return "p2wpkh" in n && "p2wpkhInP2sh" in n;
6c08f364
IC
1469 }
1470
1471 function bip49TabSelected() {
1472 return DOM.bip49tab.hasClass("active");
1473 }
1474
4e9b492c
IC
1475 function bip84TabSelected() {
1476 return DOM.bip84tab.hasClass("active");
1477 }
1478
c49e8812
IC
1479 function bip141TabSelected() {
1480 return DOM.bip141tab.hasClass("active");
1481 }
1482
6c08f364
IC
1483 function setHdCoin(coinValue) {
1484 DOM.bip44coin.val(coinValue);
1485 DOM.bip49coin.val(coinValue);
4e9b492c 1486 DOM.bip84coin.val(coinValue);
6c08f364
IC
1487 }
1488
0fd67b59 1489 function showSegwitAvailable() {
6c08f364
IC
1490 DOM.bip49unavailable.addClass("hidden");
1491 DOM.bip49available.removeClass("hidden");
bddd5d9f
IC
1492 DOM.bip84unavailable.addClass("hidden");
1493 DOM.bip84available.removeClass("hidden");
c49e8812
IC
1494 DOM.bip141unavailable.addClass("hidden");
1495 DOM.bip141available.removeClass("hidden");
6c08f364
IC
1496 }
1497
0fd67b59 1498 function showSegwitUnavailable() {
6c08f364
IC
1499 DOM.bip49available.addClass("hidden");
1500 DOM.bip49unavailable.removeClass("hidden");
bddd5d9f
IC
1501 DOM.bip84available.addClass("hidden");
1502 DOM.bip84unavailable.removeClass("hidden");
c49e8812
IC
1503 DOM.bip141available.addClass("hidden");
1504 DOM.bip141unavailable.removeClass("hidden");
6c08f364
IC
1505 }
1506
0fd67b59
IC
1507 function adjustNetworkForSegwit() {
1508 // If segwit is selected the xpub/xprv prefixes need to be adjusted
93c3ef47
IC
1509 // to avoid accidentally importing BIP49 xpub to BIP44 watch only
1510 // wallet.
1511 // See https://github.com/iancoleman/bip39/issues/125
c49e8812
IC
1512 var segwitNetworks = null;
1513 // if a segwit network is alread selected, need to use base network to
1514 // look up new parameters
1515 if ("baseNetwork" in network) {
1516 network = bitcoinjs.bitcoin.networks[network.baseNetwork];
1517 }
1518 // choose the right segwit params
1519 if (p2wpkhSelected() && "p2wpkh" in network) {
1520 network = network.p2wpkh;
1521 }
1522 else if (p2wpkhInP2shSelected() && "p2wpkhInP2sh" in network) {
1523 network = network.p2wpkhInP2sh;
93c3ef47
IC
1524 }
1525 }
1526
9183f9f6
IC
1527 function lastIndexInTable() {
1528 var pathText = DOM.addresses.find(".index").last().text();
1529 var pathBits = pathText.split("/");
1530 var lastBit = pathBits[pathBits.length-1];
1531 var lastBitClean = lastBit.replace("'", "");
1532 return parseInt(lastBitClean);
1533 }
1534
74ab4cbe
IC
1535 function uint8ArrayToHex(a) {
1536 var s = ""
1537 for (var i=0; i<a.length; i++) {
1538 var h = a[i].toString(16);
1539 while (h.length < 2) {
1540 h = "0" + h;
1541 }
1542 s = s + h;
1543 }
1544 return s;
1545 }
1546
1547 function showWordIndexes() {
1548 var phrase = DOM.phrase.val();
1549 var words = phraseToWordArray(phrase);
1550 var wordIndexes = [];
1551 var language = getLanguage();
1552 for (var i=0; i<words.length; i++) {
1553 var word = words[i];
1554 var wordIndex = WORDLISTS[language].indexOf(word);
1555 wordIndexes.push(wordIndex);
1556 }
1557 var wordIndexesStr = wordIndexes.join(", ");
1558 DOM.entropyWordIndexes.text(wordIndexesStr);
1559 }
a78f4e28 1560
09d63290
IC
1561 function showChecksum() {
1562 var phrase = DOM.phrase.val();
1563 var words = phraseToWordArray(phrase);
1564 var checksumBitlength = words.length / 3;
1565 var checksum = "";
1566 var binaryStr = "";
1567 var language = getLanguage();
1568 for (var i=words.length-1; i>=0; i--) {
1569 var word = words[i];
1570 var wordIndex = WORDLISTS[language].indexOf(word);
1571 var wordBinary = wordIndex.toString(2);
1572 while (wordBinary.length < 11) {
1573 wordBinary = "0" + wordBinary;
1574 }
1575 var binaryStr = wordBinary + binaryStr;
1576 if (binaryStr.length >= checksumBitlength) {
1577 var start = binaryStr.length - checksumBitlength;
1578 var end = binaryStr.length;
1579 checksum = binaryStr.substring(start, end);
f8ca25c3
IC
1580 // add spaces so the last group is 11 bits, not the first
1581 checksum = checksum.split("").reverse().join("")
1582 checksum = addSpacesEveryElevenBits(checksum);
1583 checksum = checksum.split("").reverse().join("")
1584 break;
09d63290
IC
1585 }
1586 }
1587 DOM.entropyChecksum.text(checksum);
1588 }
1589
a78f4e28
IC
1590 function updateCsv() {
1591 var tableCsv = "path,address,public key,private key\n";
1592 var rows = DOM.addresses.find("tr");
1593 for (var i=0; i<rows.length; i++) {
1594 var row = $(rows[i]);
1595 var cells = row.find("td");
1596 for (var j=0; j<cells.length; j++) {
1597 var cell = $(cells[j]);
1598 if (!cell.children().hasClass("invisible")) {
1599 tableCsv = tableCsv + cell.text();
1600 }
1601 if (j != cells.length - 1) {
1602 tableCsv = tableCsv + ",";
1603 }
1604 }
1605 tableCsv = tableCsv + "\n";
1606 }
1607 DOM.csv.val(tableCsv);
1608 }
74ab4cbe 1609
f8ca25c3
IC
1610 function addSpacesEveryElevenBits(binaryStr) {
1611 return binaryStr.match(/.{1,11}/g).join(" ");
1612 }
1613
7f15cb6e 1614 var networks = [
85f762c9 1615 {
1616 name: "AC - Asiacoin",
85f762c9 1617 onSelect: function() {
1618 network = bitcoinjs.bitcoin.networks.asiacoin;
1619 setHdCoin(51);
1620 },
1621 },
1622 {
1623 name: "ACC - Adcoin",
85f762c9 1624 onSelect: function() {
1625 network = bitcoinjs.bitcoin.networks.adcoin;
1626 setHdCoin(161);
1627 },
1628 },
1629 {
1630 name: "AUR - Auroracoin",
85f762c9 1631 onSelect: function() {
1632 network = bitcoinjs.bitcoin.networks.auroracoin;
1633 setHdCoin(85);
1634 },
1635 },
cfc0c4d7 1636 {
1637 name: "AXE - Axe",
cfc0c4d7 1638 onSelect: function() {
1639 network = bitcoinjs.bitcoin.networks.axe;
7d9b5f68 1640 setHdCoin(4242);
cfc0c4d7 1641 },
1642 },
85f762c9 1643 {
1644 name: "BCA - Bitcoin Atom",
85f762c9 1645 onSelect: function() {
1646 network = bitcoinjs.bitcoin.networks.atom;
1647 setHdCoin(185);
1648 },
1649 },
52d698e4 1650 {
2eab7c32 1651 name: "BCH - Bitcoin Cash",
52d698e4 1652 onSelect: function() {
e0f91e20 1653 DOM.bitcoinCashAddressTypeContainer.removeClass("hidden");
2eab7c32 1654 setHdCoin(145);
52d698e4 1655 },
1656 },
daab55dc 1657 {
2eab7c32 1658 name: "BEET - Beetlecoin",
daab55dc 1659 onSelect: function() {
2eab7c32
IC
1660 network = bitcoinjs.bitcoin.networks.beetlecoin;
1661 setHdCoin(800);
daab55dc
IC
1662 },
1663 },
85f762c9 1664 {
1665 name: "BELA - Belacoin",
85f762c9 1666 onSelect: function() {
1667 network = bitcoinjs.bitcoin.networks.belacoin;
1668 setHdCoin(73);
1669 },
1670 },
892ce76f
IC
1671 {
1672 name: "BLK - BlackCoin",
892ce76f
IC
1673 onSelect: function() {
1674 network = bitcoinjs.bitcoin.networks.blackcoin;
1675 setHdCoin(10);
1676 },
1677 },
85f762c9 1678 {
1679 name: "BRIT - Britcoin",
85f762c9 1680 onSelect: function() {
1681 network = bitcoinjs.bitcoin.networks.britcoin;
1682 setHdCoin(70);
1683 },
1684 },
1685 {
1686 name: "BSD - Bitsend",
85f762c9 1687 onSelect: function() {
1688 network = bitcoinjs.bitcoin.networks.bitsend;
1689 setHdCoin(91);
1690 },
1691 },
1692 {
1693 name: "BTA - Bata",
85f762c9 1694 onSelect: function() {
1695 network = bitcoinjs.bitcoin.networks.bata;
1696 setHdCoin(89);
1697 },
1698 },
7f15cb6e 1699 {
534481b6 1700 name: "BTC - Bitcoin",
7a995731 1701 onSelect: function() {
a0091a40 1702 network = bitcoinjs.bitcoin.networks.bitcoin;
6c08f364 1703 setHdCoin(0);
7a995731
IC
1704 },
1705 },
7f15cb6e 1706 {
534481b6 1707 name: "BTC - Bitcoin Testnet",
7a995731 1708 onSelect: function() {
a0091a40 1709 network = bitcoinjs.bitcoin.networks.testnet;
6c08f364 1710 setHdCoin(1);
7a995731
IC
1711 },
1712 },
1e2cc748 1713 {
1714 name: "BTCP - Bitcoin Private",
1e2cc748 1715 onSelect: function() {
1716 network = bitcoinjs.bitcoin.networks.bitcoinprivate;
1717 setHdCoin(183);
1718 },
1719 },
1720 {
1721 name: "BTCZ - Bitcoinz",
1e2cc748 1722 onSelect: function() {
1723 network = bitcoinjs.bitcoin.networks.bitcoinz;
1724 setHdCoin(177);
1725 },
1726 },
39608073
RS
1727 {
1728 name: "BTG - Bitcoin Gold",
39608073
RS
1729 onSelect: function() {
1730 network = bitcoinjs.bitcoin.networks.bgold;
88ae1301 1731 setHdCoin(156);
39608073
RS
1732 },
1733 },
85f762c9 1734 {
1735 name: "BTX - Bitcore",
85f762c9 1736 onSelect: function() {
1737 network = bitcoinjs.bitcoin.networks.bitcore;
1738 setHdCoin(160);
1739 },
1740 },
1741 {
1742 name: "CCN - Cannacoin",
85f762c9 1743 onSelect: function() {
1744 network = bitcoinjs.bitcoin.networks.cannacoin;
1745 setHdCoin(19);
1746 },
1747 },
1748 {
1749 name: "CDN - Canadaecoin",
85f762c9 1750 onSelect: function() {
1751 network = bitcoinjs.bitcoin.networks.canadaecoin;
1752 setHdCoin(34);
1753 },
1754 },
7f15cb6e 1755 {
534481b6 1756 name: "CLAM - Clams",
7a995731 1757 onSelect: function() {
a0091a40 1758 network = bitcoinjs.bitcoin.networks.clam;
6c08f364 1759 setHdCoin(23);
7a995731
IC
1760 },
1761 },
85f762c9 1762 {
1763 name: "CLUB - Clubcoin",
85f762c9 1764 onSelect: function() {
1765 network = bitcoinjs.bitcoin.networks.clubcoin;
1766 setHdCoin(79);
1767 },
1768 },
1769 {
1770 name: "CMP - Compcoin",
85f762c9 1771 onSelect: function() {
1772 network = bitcoinjs.bitcoin.networks.compcoin;
1773 setHdCoin(71);
1774 },
1775 },
1776 {
1777 name: "CRAVE - Crave",
85f762c9 1778 onSelect: function() {
1779 network = bitcoinjs.bitcoin.networks.crave;
1780 setHdCoin(186);
1781 },
1782 },
0921f370 1783 {
1784 name: "CRW - Crown",
1785 onSelect: function() {
6c08f364
IC
1786 network = bitcoinjs.bitcoin.networks.crown;
1787 setHdCoin(72);
0921f370 1788 },
1789 },
7f15cb6e 1790 {
534481b6 1791 name: "DASH - Dash",
e3a9508c 1792 onSelect: function() {
a0091a40 1793 network = bitcoinjs.bitcoin.networks.dash;
6c08f364 1794 setHdCoin(5);
e3a9508c
IC
1795 },
1796 },
c0386f3b 1797 {
534481b6 1798 name: "DASH - Dash Testnet",
c0386f3b 1799 onSelect: function() {
a0091a40 1800 network = bitcoinjs.bitcoin.networks.dashtn;
6c08f364 1801 setHdCoin(1);
c0386f3b
KR
1802 },
1803 },
85f762c9 1804 {
1805 name: "DFC - Defcoin",
85f762c9 1806 onSelect: function() {
1807 network = bitcoinjs.bitcoin.networks.defcoin;
1808 setHdCoin(1337);
1809 },
1810 },
1811 {
1812 name: "DGB - Digibyte",
85f762c9 1813 onSelect: function() {
1814 network = bitcoinjs.bitcoin.networks.digibyte;
1815 setHdCoin(20);
1816 },
1817 },
1818 {
1819 name: "DGC - Digitalcoin",
85f762c9 1820 onSelect: function() {
1821 network = bitcoinjs.bitcoin.networks.digitalcoin;
1822 setHdCoin(18);
1823 },
1824 },
1825 {
1826 name: "DMD - Diamond",
85f762c9 1827 onSelect: function() {
1828 network = bitcoinjs.bitcoin.networks.diamond;
1829 setHdCoin(152);
1830 },
1831 },
40209fd8 1832 {
1833 name: "DNR - Denarius",
40209fd8 1834 onSelect: function() {
1835 network = bitcoinjs.bitcoin.networks.denarius;
50289881 1836 setHdCoin(116);
40209fd8 1837 },
1838 },
e3a9508c 1839 {
534481b6 1840 name: "DOGE - Dogecoin",
1841 onSelect: function() {
6c08f364
IC
1842 network = bitcoinjs.bitcoin.networks.dogecoin;
1843 setHdCoin(3);
534481b6 1844 },
1845 },
85f762c9 1846 {
1847 name: "ECN - Ecoin",
85f762c9 1848 onSelect: function() {
1849 network = bitcoinjs.bitcoin.networks.ecoin;
1850 setHdCoin(115);
1851 },
1852 },
1853 {
1854 name: "EDRC - Edrcoin",
85f762c9 1855 onSelect: function() {
1856 network = bitcoinjs.bitcoin.networks.edrcoin;
1857 setHdCoin(56);
1858 },
1859 },
1860 {
1861 name: "EFL - Egulden",
85f762c9 1862 onSelect: function() {
1863 network = bitcoinjs.bitcoin.networks.egulden;
1864 setHdCoin(78);
1865 },
1866 },
1867 {
1868 name: "EMC2 - Einsteinium",
85f762c9 1869 onSelect: function() {
1870 network = bitcoinjs.bitcoin.networks.einsteinium;
1871 setHdCoin(41);
1872 },
1873 },
1874 {
1875 name: "ERC - Europecoin",
85f762c9 1876 onSelect: function() {
1877 network = bitcoinjs.bitcoin.networks.europecoin;
1878 setHdCoin(151);
1879 },
1880 },
534481b6 1881 {
1882 name: "ETH - Ethereum",
e3a9508c 1883 onSelect: function() {
a0091a40 1884 network = bitcoinjs.bitcoin.networks.bitcoin;
6c08f364 1885 setHdCoin(60);
e3a9508c
IC
1886 },
1887 },
85f762c9 1888 {
1889 name: "EXCL - Exclusivecoin",
85f762c9 1890 onSelect: function() {
1891 network = bitcoinjs.bitcoin.networks.exclusivecoin;
1892 setHdCoin(190);
1893 },
1894 },
6e679905 1895 {
1896 name: "FJC - Fujicoin",
6e679905 1897 onSelect: function() {
1898 network = bitcoinjs.bitcoin.networks.fujicoin;
1899 setHdCoin(75);
1900 },
1901 },
85f762c9 1902 {
1903 name: "FLASH - Flashcoin",
85f762c9 1904 onSelect: function() {
1905 network = bitcoinjs.bitcoin.networks.flashcoin;
1906 setHdCoin(120);
1907 },
1908 },
1909 {
1910 name: "FRST - Firstcoin",
85f762c9 1911 onSelect: function() {
1912 network = bitcoinjs.bitcoin.networks.firstcoin;
1913 setHdCoin(167);
1914 },
1915 },
1916 {
1917 name: "FTC - Feathercoin",
85f762c9 1918 onSelect: function() {
1919 network = bitcoinjs.bitcoin.networks.feathercoin;
1920 setHdCoin(8);
1921 },
1922 },
f88fab20 1923 {
534481b6 1924 name: "GAME - GameCredits",
f88fab20 1925 onSelect: function() {
a0091a40 1926 network = bitcoinjs.bitcoin.networks.game;
6c08f364 1927 setHdCoin(101);
f88fab20 1928 },
1929 },
85f762c9 1930 {
1931 name: "GBX - Gobyte",
85f762c9 1932 onSelect: function() {
1933 network = bitcoinjs.bitcoin.networks.gobyte;
1934 setHdCoin(176);
1935 },
1936 },
1937 {
1938 name: "GCR - GCRCoin",
85f762c9 1939 onSelect: function() {
1940 network = bitcoinjs.bitcoin.networks.gcr;
1941 setHdCoin(79);
1942 },
1943 },
1944 {
1945 name: "GRC - Gridcoin",
85f762c9 1946 onSelect: function() {
1947 network = bitcoinjs.bitcoin.networks.gridcoin;
1948 setHdCoin(84);
1949 },
1950 },
1951 {
1952 name: "HNC - Helleniccoin",
85f762c9 1953 onSelect: function() {
1954 network = bitcoinjs.bitcoin.networks.helleniccoin;
1955 setHdCoin(168);
1956 },
1957 },
1958 {
1959 name: "INSN - Insane",
85f762c9 1960 onSelect: function() {
1961 network = bitcoinjs.bitcoin.networks.insane;
1962 setHdCoin(68);
1963 },
1964 },
1965 {
1966 name: "IOP - Iop",
85f762c9 1967 onSelect: function() {
1968 network = bitcoinjs.bitcoin.networks.iop;
1969 setHdCoin(66);
1970 },
1971 },
1972 {
1973 name: "IXC - Ixcoin",
85f762c9 1974 onSelect: function() {
1975 network = bitcoinjs.bitcoin.networks.ixcoin;
1976 setHdCoin(86);
1977 },
1978 },
a3baa26e 1979 {
534481b6 1980 name: "JBS - Jumbucks",
a3baa26e 1981 onSelect: function() {
a0091a40 1982 network = bitcoinjs.bitcoin.networks.jumbucks;
6c08f364 1983 setHdCoin(26);
a3baa26e
IC
1984 },
1985 },
aab3645f 1986 {
1987 name: "KMD - Komodo",
1988 bip49available: false,
1989 onSelect: function() {
1990 network = bitcoinjs.bitcoin.networks.komodo;
1991 setHdCoin(141);
1992 },
1993 },
0a5d28a7 1994 {
1995 name: "KOBO - Kobocoin",
1996 bip49available: false,
1997 onSelect: function() {
1998 network = bitcoinjs.bitcoin.networks.kobocoin;
1999 setHdCoin(196);
2000 },
2001 },
85f762c9 2002 {
2003 name: "LBC - Library Credits",
85f762c9 2004 onSelect: function() {
2005 network = bitcoinjs.bitcoin.networks.lbry;
2006 setHdCoin(140);
2007 },
2008 },
2009 {
2010 name: "LCC - Litecoincash",
85f762c9 2011 onSelect: function() {
2012 network = bitcoinjs.bitcoin.networks.litecoincash;
2013 setHdCoin(192);
2014 },
2015 },
2016 {
2017 name: "LDCN - Landcoin",
85f762c9 2018 onSelect: function() {
2019 network = bitcoinjs.bitcoin.networks.landcoin;
2020 setHdCoin(63);
2021 },
2022 },
2023 {
2024 name: "LINX - Linx",
85f762c9 2025 onSelect: function() {
2026 network = bitcoinjs.bitcoin.networks.linx;
2027 setHdCoin(114);
2028 },
2029 },
a3baa26e 2030 {
534481b6 2031 name: "LTC - Litecoin",
a3baa26e 2032 onSelect: function() {
a0091a40 2033 network = bitcoinjs.bitcoin.networks.litecoin;
6c08f364 2034 setHdCoin(2);
3abab9b0 2035 DOM.litecoinLtubContainer.removeClass("hidden");
a3baa26e
IC
2036 },
2037 },
85f762c9 2038 {
2039 name: "LYNX - Lynx",
85f762c9 2040 onSelect: function() {
2041 network = bitcoinjs.bitcoin.networks.lynx;
2042 setHdCoin(191);
2043 },
2044 },
56ad9601
JS
2045 {
2046 name: "MAZA - Maza",
56ad9601
JS
2047 onSelect: function() {
2048 network = bitcoinjs.bitcoin.networks.maza;
2049 setHdCoin(13);
2050 },
2051 },
85f762c9 2052 {
2053 name: "MNX - Minexcoin",
85f762c9 2054 onSelect: function() {
2055 network = bitcoinjs.bitcoin.networks.minexcoin;
2056 setHdCoin(182);
2057 },
2058 },
f487fea9
IC
2059 {
2060 name: "MONA - Monacoin",
f487fea9
IC
2061 onSelect: function() {
2062 network = bitcoinjs.bitcoin.networks.monacoin,
2063 setHdCoin(22);
2064 },
2065 },
85f762c9 2066 {
2067 name: "NAV - Navcoin",
85f762c9 2068 onSelect: function() {
2069 network = bitcoinjs.bitcoin.networks.navcoin;
2070 setHdCoin(130);
2071 },
2072 },
db37d639
IC
2073 {
2074 name: "NEBL - Neblio",
db37d639
IC
2075 onSelect: function() {
2076 network = bitcoinjs.bitcoin.networks.neblio;
2077 setHdCoin(146);
2078 },
2079 },
85f762c9 2080 {
2081 name: "NEOS - Neoscoin",
85f762c9 2082 onSelect: function() {
2083 network = bitcoinjs.bitcoin.networks.neoscoin;
2084 setHdCoin(25);
2085 },
2086 },
2087 {
2088 name: "NLG - Gulden",
85f762c9 2089 onSelect: function() {
2090 network = bitcoinjs.bitcoin.networks.gulden;
2091 setHdCoin(87);
2092 },
2093 },
a3baa26e 2094 {
534481b6 2095 name: "NMC - Namecoin",
a3baa26e 2096 onSelect: function() {
a0091a40 2097 network = bitcoinjs.bitcoin.networks.namecoin;
6c08f364 2098 setHdCoin(7);
a3baa26e
IC
2099 },
2100 },
1e2cc748 2101 {
2102 name: "NRG - Energi",
1e2cc748 2103 onSelect: function() {
2104 network = bitcoinjs.bitcoin.networks.energi;
2105 setHdCoin(204);
2106 },
2107 },
85f762c9 2108 {
2109 name: "NRO - Neurocoin",
85f762c9 2110 onSelect: function() {
2111 network = bitcoinjs.bitcoin.networks.neurocoin;
2112 setHdCoin(110);
2113 },
2114 },
2115 {
2116 name: "NSR - Nushares",
85f762c9 2117 onSelect: function() {
2118 network = bitcoinjs.bitcoin.networks.nushares;
2119 setHdCoin(11);
2120 },
2121 },
2122 {
2123 name: "NYC - Newyorkc",
85f762c9 2124 onSelect: function() {
2125 network = bitcoinjs.bitcoin.networks.newyorkc;
2126 setHdCoin(179);
2127 },
2128 },
2129 {
2130 name: "NVC - Novacoin",
85f762c9 2131 onSelect: function() {
2132 network = bitcoinjs.bitcoin.networks.novacoin;
2133 setHdCoin(50);
2134 },
2135 },
2136 {
2137 name: "OK - Okcash",
85f762c9 2138 onSelect: function() {
2139 network = bitcoinjs.bitcoin.networks.okcash;
2140 setHdCoin(69);
2141 },
2142 },
2143 {
2144 name: "OMNI - Omnicore",
85f762c9 2145 onSelect: function() {
2146 network = bitcoinjs.bitcoin.networks.omnicore;
2147 setHdCoin(200);
2148 },
2149 },
66419cf3 2150 {
2151 name: "ONX - Onixcoin",
66419cf3 2152 onSelect: function() {
2153 network = bitcoinjs.bitcoin.networks.onixcoin;
d00c7199 2154 setHdCoin(174);
66419cf3 2155 },
2156 },
85f762c9 2157 {
2158 name: "PINK - Pinkcoin",
85f762c9 2159 onSelect: function() {
2160 network = bitcoinjs.bitcoin.networks.pinkcoin;
2161 setHdCoin(117);
2162 },
2163 },
c0df0189 2164 {
2165 name: "PIVX - PIVX",
c0df0189 2166 onSelect: function() {
2167 network = bitcoinjs.bitcoin.networks.pivx;
2168 setHdCoin(119);
2169 },
2170 },
2171 {
2172 name: "PIVX - PIVX Testnet",
c0df0189 2173 onSelect: function() {
2174 network = bitcoinjs.bitcoin.networks.pivxtestnet;
2175 setHdCoin(1);
2176 },
2177 },
85f762c9 2178 {
2179 name: "POSW - POSWcoin",
85f762c9 2180 onSelect: function() {
2181 network = bitcoinjs.bitcoin.networks.poswcoin;
2182 setHdCoin(47);
2183 },
2184 },
2185 {
2186 name: "POT - Potcoin",
85f762c9 2187 onSelect: function() {
2188 network = bitcoinjs.bitcoin.networks.potcoin;
2189 setHdCoin(81);
2190 },
2191 },
5c434a8a 2192 {
534481b6 2193 name: "PPC - Peercoin",
5c434a8a 2194 onSelect: function() {
a0091a40 2195 network = bitcoinjs.bitcoin.networks.peercoin;
6c08f364 2196 setHdCoin(6);
5c434a8a
CM
2197 },
2198 },
85f762c9 2199 {
2200 name: "PSB - Pesobit",
85f762c9 2201 onSelect: function() {
2202 network = bitcoinjs.bitcoin.networks.pesobit;
2203 setHdCoin(62);
2204 },
2205 },
2206 {
2207 name: "PUT - Putincoin",
85f762c9 2208 onSelect: function() {
2209 network = bitcoinjs.bitcoin.networks.putincoin;
2210 setHdCoin(122);
2211 },
2212 },
2213 {
2214 name: "RBY - Rubycoin",
85f762c9 2215 onSelect: function() {
2216 network = bitcoinjs.bitcoin.networks.rubycoin;
2217 setHdCoin(16);
2218 },
2219 },
2220 {
2221 name: "RDD - Reddcoin",
85f762c9 2222 onSelect: function() {
2223 network = bitcoinjs.bitcoin.networks.reddoin;
2224 setHdCoin(4);
2225 },
2226 },
2227 {
2228 name: "RVR - RevolutionVR",
85f762c9 2229 onSelect: function() {
2230 network = bitcoinjs.bitcoin.networks.revolutionvr;
2231 setHdCoin(129);
2232 },
2233 },
64a7d2aa 2234 {
534481b6 2235 name: "SDC - ShadowCash",
82f91834 2236 onSelect: function() {
a0091a40 2237 network = bitcoinjs.bitcoin.networks.shadow;
6c08f364 2238 setHdCoin(35);
82f91834
DG
2239 },
2240 },
07ac4350 2241 {
534481b6 2242 name: "SDC - ShadowCash Testnet",
07ac4350 2243 onSelect: function() {
a0091a40 2244 network = bitcoinjs.bitcoin.networks.shadowtn;
6c08f364 2245 setHdCoin(1);
07ac4350 2246 },
2247 },
7a5a87a0 2248 {
534481b6 2249 name: "SLM - Slimcoin",
7a5a87a0 2250 onSelect: function() {
a0091a40 2251 network = bitcoinjs.bitcoin.networks.slimcoin;
6c08f364 2252 setHdCoin(63);
7a5a87a0
GH
2253 },
2254 },
2255 {
534481b6 2256 name: "SLM - Slimcoin Testnet",
7a5a87a0 2257 onSelect: function() {
a0091a40 2258 network = bitcoinjs.bitcoin.networks.slimcointn;
6c08f364 2259 setHdCoin(111);
7a5a87a0
GH
2260 },
2261 },
85f762c9 2262 {
2263 name: "SLR - Solarcoin",
85f762c9 2264 onSelect: function() {
2265 network = bitcoinjs.bitcoin.networks.solarcoin;
2266 setHdCoin(58);
2267 },
2268 },
2269 {
2270 name: "SMLY - Smileycoin",
85f762c9 2271 onSelect: function() {
2272 network = bitcoinjs.bitcoin.networks.smileycoin;
2273 setHdCoin(59);
2274 },
2275 },
2276 {
2277 name: "STRAT - Stratis",
85f762c9 2278 onSelect: function() {
2279 network = bitcoinjs.bitcoin.networks.stratis;
2280 setHdCoin(105);
2281 },
2282 },
2283 {
2284 name: "SYS - Syscoin",
85f762c9 2285 onSelect: function() {
2286 network = bitcoinjs.bitcoin.networks.syscoin;
2287 setHdCoin(57);
2288 },
2289 },
2290 {
2291 name: "THC - Hempcoin",
85f762c9 2292 onSelect: function() {
2293 network = bitcoinjs.bitcoin.networks.hempcoin;
2294 setHdCoin(113);
2295 },
2296 },
2297 {
2298 name: "TOA - Toa",
85f762c9 2299 onSelect: function() {
2300 network = bitcoinjs.bitcoin.networks.toa;
2301 setHdCoin(159);
2302 },
2303 },
2304 {
2305 name: "USC - Ultimatesecurecash",
85f762c9 2306 onSelect: function() {
2307 network = bitcoinjs.bitcoin.networks.ultimatesecurecash;
2308 setHdCoin(112);
2309 },
2310 },
8dd28f2c 2311 {
2312 name: "USNBT - NuBits",
8dd28f2c 2313 onSelect: function() {
2314 network = bitcoinjs.bitcoin.networks.nubits;
2315 setHdCoin(12);
2316 },
2317 },
85f762c9 2318 {
2319 name: "UNO - Unobtanium",
85f762c9 2320 onSelect: function() {
2321 network = bitcoinjs.bitcoin.networks.unobtanium;
2322 setHdCoin(92);
2323 },
2324 },
2325 {
2326 name: "VASH - Vpncoin",
85f762c9 2327 onSelect: function() {
2328 network = bitcoinjs.bitcoin.networks.vpncoin;
2329 setHdCoin(33);
2330 },
2331 },
07ac4350 2332 {
534481b6 2333 name: "VIA - Viacoin",
07ac4350 2334 onSelect: function() {
a0091a40 2335 network = bitcoinjs.bitcoin.networks.viacoin;
6c08f364 2336 setHdCoin(14);
07ac4350 2337 },
2338 },
0edac945 2339 {
534481b6 2340 name: "VIA - Viacoin Testnet",
0edac945 2341 onSelect: function() {
a0091a40 2342 network = bitcoinjs.bitcoin.networks.viacointestnet;
6c08f364 2343 setHdCoin(1);
0edac945
IC
2344 },
2345 },
85f762c9 2346 {
2347 name: "VIVO - Vivo",
85f762c9 2348 onSelect: function() {
2349 network = bitcoinjs.bitcoin.networks.vivo;
2350 setHdCoin(166);
2351 },
2352 },
2353 {
2354 name: "VTC - Vertcoin",
85f762c9 2355 onSelect: function() {
2356 network = bitcoinjs.bitcoin.networks.vertcoin;
2357 setHdCoin(28);
2358 },
2359 },
2360 {
2361 name: "WC - Wincoin",
85f762c9 2362 onSelect: function() {
2363 network = bitcoinjs.bitcoin.networks.wincoin;
2364 setHdCoin(181);
2365 },
2366 },
2367 {
2368 name: "XBC - Bitcoinplus",
85f762c9 2369 onSelect: function() {
2370 network = bitcoinjs.bitcoin.networks.bitcoinplus;
2371 setHdCoin(65);
2372 },
2373 },
534481b6 2374 {
7ebdf61c 2375 name: "XMY - Myriadcoin",
534481b6 2376 onSelect: function() {
7ebdf61c
IC
2377 network = bitcoinjs.bitcoin.networks.myriadcoin;
2378 setHdCoin(90);
534481b6 2379 },
adedbf91 2380 },
2381 {
7ebdf61c 2382 name: "XRP - Ripple",
adedbf91 2383 onSelect: function() {
7ebdf61c
IC
2384 network = bitcoinjs.bitcoin.networks.bitcoin;
2385 setHdCoin(144);
adedbf91 2386 },
e1d0cf4f 2387 },
85f762c9 2388 {
2389 name: "XVC - Vcash",
85f762c9 2390 onSelect: function() {
2391 network = bitcoinjs.bitcoin.networks.vcash;
2392 setHdCoin(127);
2393 },
2394 },
2395 {
2396 name: "XVG - Verge",
85f762c9 2397 onSelect: function() {
2398 network = bitcoinjs.bitcoin.networks.verge;
2399 setHdCoin(77);
2400 },
2401 },
2402 {
2403 name: "XWC - Whitecoin",
85f762c9 2404 onSelect: function() {
2405 network = bitcoinjs.bitcoin.networks.whitecoin;
2406 setHdCoin(155);
2407 },
2408 },
2409 {
2410 name: "XZC - Zcoin",
85f762c9 2411 onSelect: function() {
2412 network = bitcoinjs.bitcoin.networks.zcoin;
2413 setHdCoin(136);
2414 },
2415 },
1e2cc748 2416 {
2417 name: "ZCL - Zclassic",
1e2cc748 2418 onSelect: function() {
2419 network = bitcoinjs.bitcoin.networks.zclassic;
2420 setHdCoin(147);
2421 },
2422 },
0702ecd3 2423 {
2424 name: "ZEC - Zcash",
0702ecd3 2425 onSelect: function() {
2426 network = bitcoinjs.bitcoin.networks.zcash;
2427 setHdCoin(133);
2428 },
2429 },
24355c46 2430 {
2431 name: "XUEZ - Xuez",
24355c46 2432 onSelect: function() {
2433 network = bitcoinjs.bitcoin.networks.xuez;
2434 setHdCoin(225);
2435 },
2436 },
1e2cc748 2437 {
2438 name: "ZEN - Zencash",
1e2cc748 2439 onSelect: function() {
2440 network = bitcoinjs.bitcoin.networks.zencash;
2441 setHdCoin(121);
2442 },
2443 },
7f15cb6e 2444 ]
6ee4fb7d 2445
b4fd763c
AG
2446 var clients = [
2447 {
2448 name: "Bitcoin Core",
2449 onSelect: function() {
2450 DOM.bip32path.val("m/0'/0'");
2451 DOM.hardenedAddresses.prop('checked', true);
2452 },
2453 },
2454 {
2455 name: "blockchain.info",
2456 onSelect: function() {
2457 DOM.bip32path.val("m/44'/0'/0'");
2458 DOM.hardenedAddresses.prop('checked', false);
2459 },
2460 },
2461 {
2462 name: "MultiBit HD",
2463 onSelect: function() {
2464 DOM.bip32path.val("m/0'/0");
2465 DOM.hardenedAddresses.prop('checked', false);
2466 },
2467 }
2468 ]
7a995731 2469
ebd8d4e8
IC
2470 init();
2471
2472})();