]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/blame - src/js/index.js
Validate root key correctly for alternate networks
[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;
3abab9b0 17 var litecoinUseLtub = false;
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");
40 DOM.entropyMnemonicLength = DOM.entropyContainer.find(".mnemonic-length");
ebd8d4e8 41 DOM.phrase = $(".phrase");
1abcc511 42 DOM.passphrase = $(".passphrase");
c6624d51 43 DOM.generateContainer = $(".generate-container");
ebd8d4e8 44 DOM.generate = $(".generate");
3e0ed16a 45 DOM.seed = $(".seed");
ebd8d4e8 46 DOM.rootKey = $(".root-key");
3abab9b0
IC
47 DOM.litecoinLtubContainer = $(".litecoin-ltub-container");
48 DOM.litecoinUseLtub = $(".litecoin-use-ltub");
ebd8d4e8
IC
49 DOM.extendedPrivKey = $(".extended-priv-key");
50 DOM.extendedPubKey = $(".extended-pub-key");
d6cedc94
IC
51 DOM.bip32tab = $("#bip32-tab");
52 DOM.bip44tab = $("#bip44-tab");
6c08f364 53 DOM.bip49tab = $("#bip49-tab");
d6cedc94
IC
54 DOM.bip32panel = $("#bip32");
55 DOM.bip44panel = $("#bip44");
6c08f364 56 DOM.bip49panel = $("#bip49");
ebd8d4e8
IC
57 DOM.bip32path = $("#bip32-path");
58 DOM.bip44path = $("#bip44-path");
59 DOM.bip44purpose = $("#bip44 .purpose");
60 DOM.bip44coin = $("#bip44 .coin");
61 DOM.bip44account = $("#bip44 .account");
c554e6ff
IC
62 DOM.bip44accountXprv = $("#bip44 .account-xprv");
63 DOM.bip44accountXpub = $("#bip44 .account-xpub");
ebd8d4e8 64 DOM.bip44change = $("#bip44 .change");
6c08f364
IC
65 DOM.bip49unavailable = $("#bip49 .unavailable");
66 DOM.bip49available = $("#bip49 .available");
67 DOM.bip49path = $("#bip49-path");
68 DOM.bip49purpose = $("#bip49 .purpose");
69 DOM.bip49coin = $("#bip49 .coin");
70 DOM.bip49account = $("#bip49 .account");
71 DOM.bip49accountXprv = $("#bip49 .account-xprv");
72 DOM.bip49accountXpub = $("#bip49 .account-xpub");
73 DOM.bip49change = $("#bip49 .change");
88df3739 74 DOM.generatedStrength = $(".generate-container .strength");
146e089e 75 DOM.hardenedAddresses = $(".hardened-addresses");
ebd8d4e8
IC
76 DOM.addresses = $(".addresses");
77 DOM.rowsToAdd = $(".rows-to-add");
78 DOM.more = $(".more");
79 DOM.feedback = $(".feedback");
80 DOM.tab = $(".derivation-type a");
81 DOM.indexToggle = $(".index-toggle");
82 DOM.addressToggle = $(".address-toggle");
1b12b2f5 83 DOM.publicKeyToggle = $(".public-key-toggle");
ebd8d4e8 84 DOM.privateKeyToggle = $(".private-key-toggle");
5ee7bb9e 85 DOM.languages = $(".languages a");
e00964cc 86 DOM.qrContainer = $(".qr-container");
97811c29 87 DOM.qrHider = DOM.qrContainer.find(".qr-hider");
e00964cc
IC
88 DOM.qrImage = DOM.qrContainer.find(".qr-image");
89 DOM.qrHint = DOM.qrContainer.find(".qr-hint");
90 DOM.showQrEls = $("[data-show-qr]");
ebd8d4e8 91
ebd8d4e8
IC
92 function init() {
93 // Events
d6cedc94 94 DOM.network.on("change", networkChanged);
29bf60f5 95 DOM.bip32Client.on("change", bip32ClientChanged);
c6624d51
IC
96 DOM.useEntropy.on("change", setEntropyVisibility);
97 DOM.entropy.on("input", delayedEntropyChanged);
3599674d 98 DOM.entropyMnemonicLength.on("change", entropyChanged);
a19a5498
IC
99 DOM.phrase.on("input", delayedPhraseChanged);
100 DOM.passphrase.on("input", delayedPhraseChanged);
ebd8d4e8
IC
101 DOM.generate.on("click", generateClicked);
102 DOM.more.on("click", showMore);
efe41586 103 DOM.rootKey.on("input", delayedRootKeyChanged);
3abab9b0 104 DOM.litecoinUseLtub.on("change", litecoinUseLtubChanged);
efe41586 105 DOM.bip32path.on("input", calcForDerivationPath);
efe41586
IC
106 DOM.bip44account.on("input", calcForDerivationPath);
107 DOM.bip44change.on("input", calcForDerivationPath);
6c08f364
IC
108 DOM.bip49account.on("input", calcForDerivationPath);
109 DOM.bip49change.on("input", calcForDerivationPath);
efe41586 110 DOM.tab.on("shown.bs.tab", calcForDerivationPath);
146e089e 111 DOM.hardenedAddresses.on("change", calcForDerivationPath);
ebd8d4e8
IC
112 DOM.indexToggle.on("click", toggleIndexes);
113 DOM.addressToggle.on("click", toggleAddresses);
1b12b2f5 114 DOM.publicKeyToggle.on("click", togglePublicKeys);
ebd8d4e8 115 DOM.privateKeyToggle.on("click", togglePrivateKeys);
5ee7bb9e 116 DOM.languages.on("click", languageChanged);
e00964cc 117 setQrEvents(DOM.showQrEls);
ebd8d4e8
IC
118 disableForms();
119 hidePending();
120 hideValidationError();
7f15cb6e 121 populateNetworkSelect();
b4fd763c 122 populateClientSelect();
ebd8d4e8
IC
123 }
124
125 // Event handlers
126
d6cedc94 127 function networkChanged(e) {
6c08f364
IC
128 clearDerivedKeys();
129 clearAddressesList();
3abab9b0 130 DOM.litecoinLtubContainer.addClass("hidden");
54563907 131 var networkIndex = e.target.value;
6c08f364
IC
132 var network = networks[networkIndex];
133 network.onSelect();
0cda44d5
IC
134 if (network.p2wpkhNestedInP2shAvailable) {
135 showP2wpkhNestedInP2shAvailable();
6c08f364
IC
136 }
137 else {
0cda44d5 138 showP2wpkhNestedInP2shUnavailable();
6c08f364 139 }
54563907
IC
140 if (seed != null) {
141 phraseChanged();
142 }
143 else {
144 rootKeyChanged();
145 }
d6cedc94 146 }
29bf60f5
IC
147
148 function bip32ClientChanged(e) {
149 var clientIndex = DOM.bip32Client.val();
150 if (clientIndex == "custom") {
151 DOM.bip32path.prop("readonly", false);
b4fd763c
AG
152 }
153 else {
29bf60f5
IC
154 DOM.bip32path.prop("readonly", true);
155 clients[clientIndex].onSelect();
156 if (seed != null) {
157 phraseChanged();
158 }
159 else {
160 rootKeyChanged();
161 }
b4fd763c
AG
162 }
163 }
d6cedc94 164
c6624d51
IC
165 function setEntropyVisibility() {
166 if (isUsingOwnEntropy()) {
167 DOM.entropyContainer.removeClass("hidden");
168 DOM.generateContainer.addClass("hidden");
169 DOM.phrase.prop("readonly", true);
170 DOM.entropy.focus();
171 entropyChanged();
172 }
173 else {
174 DOM.entropyContainer.addClass("hidden");
175 DOM.generateContainer.removeClass("hidden");
176 DOM.phrase.prop("readonly", false);
057722b0 177 hidePending();
c6624d51
IC
178 }
179 }
180
ebd8d4e8
IC
181 function delayedPhraseChanged() {
182 hideValidationError();
ed6d9d39
IC
183 seed = null;
184 bip32RootKey = null;
185 bip32ExtendedKey = null;
186 clearAddressesList();
ebd8d4e8
IC
187 showPending();
188 if (phraseChangeTimeoutEvent != null) {
189 clearTimeout(phraseChangeTimeoutEvent);
190 }
191 phraseChangeTimeoutEvent = setTimeout(phraseChanged, 400);
192 }
193
194 function phraseChanged() {
195 showPending();
5ee7bb9e 196 setMnemonicLanguage();
ebd8d4e8
IC
197 // Get the mnemonic phrase
198 var phrase = DOM.phrase.val();
199 var errorText = findPhraseErrors(phrase);
200 if (errorText) {
201 showValidationError(errorText);
202 return;
203 }
efe41586
IC
204 // Calculate and display
205 var passphrase = DOM.passphrase.val();
206 calcBip32RootKeyFromSeed(phrase, passphrase);
207 calcForDerivationPath();
efe41586
IC
208 }
209
c6624d51
IC
210 function delayedEntropyChanged() {
211 hideValidationError();
212 showPending();
213 if (entropyChangeTimeoutEvent != null) {
214 clearTimeout(entropyChangeTimeoutEvent);
215 }
216 entropyChangeTimeoutEvent = setTimeout(entropyChanged, 400);
217 }
218
219 function entropyChanged() {
057722b0
IC
220 // If blank entropy, clear mnemonic, addresses, errors
221 if (DOM.entropy.val().trim().length == 0) {
222 clearDisplay();
0a84fe6a 223 clearEntropyFeedback();
057722b0
IC
224 DOM.phrase.val("");
225 showValidationError("Blank entropy");
226 return;
227 }
228 // Get the current phrase to detect changes
229 var phrase = DOM.phrase.val();
230 // Set the phrase from the entropy
c6624d51 231 setMnemonicFromEntropy();
057722b0
IC
232 // Recalc addresses if the phrase has changed
233 var newPhrase = DOM.phrase.val();
234 if (newPhrase != phrase) {
235 if (newPhrase.length == 0) {
236 clearDisplay();
237 }
238 else {
239 phraseChanged();
240 }
241 }
242 else {
243 hidePending();
244 }
c6624d51
IC
245 }
246
efe41586
IC
247 function delayedRootKeyChanged() {
248 // Warn if there is an existing mnemonic or passphrase.
249 if (DOM.phrase.val().length > 0 || DOM.passphrase.val().length > 0) {
250 if (!confirm("This will clear existing mnemonic and passphrase")) {
251 DOM.rootKey.val(bip32RootKey);
252 return
253 }
254 }
255 hideValidationError();
256 showPending();
257 // Clear existing mnemonic and passphrase
258 DOM.phrase.val("");
259 DOM.passphrase.val("");
260 seed = null;
261 if (rootKeyChangedTimeoutEvent != null) {
262 clearTimeout(rootKeyChangedTimeoutEvent);
263 }
264 rootKeyChangedTimeoutEvent = setTimeout(rootKeyChanged, 400);
265 }
266
267 function rootKeyChanged() {
268 showPending();
269 hideValidationError();
efe41586
IC
270 var rootKeyBase58 = DOM.rootKey.val();
271 var errorText = validateRootKey(rootKeyBase58);
272 if (errorText) {
273 showValidationError(errorText);
274 return;
275 }
276 // Calculate and display
277 calcBip32RootKeyFromBase58(rootKeyBase58);
278 calcForDerivationPath();
efe41586
IC
279 }
280
3abab9b0
IC
281 function litecoinUseLtubChanged() {
282 litecoinUseLtub = DOM.litecoinUseLtub.prop("checked");
283 if (litecoinUseLtub) {
284 network = bitcoinjs.bitcoin.networks.litecoinLtub;
285 }
286 else {
287 network = bitcoinjs.bitcoin.networks.litecoin;
288 }
289 phraseChanged();
290 }
291
efe41586 292 function calcForDerivationPath() {
6c08f364 293 clearDerivedKeys();
ba3cb9ec 294 clearAddressesList();
0eda54f5 295 showPending();
6c08f364
IC
296 // Don't show bip49 if it's selected but network doesn't support it
297 if (bip49TabSelected() && !networkHasBip49()) {
298 return;
299 }
ebd8d4e8 300 // Get the derivation path
38523d36
IC
301 var derivationPath = getDerivationPath();
302 var errorText = findDerivationPathErrors(derivationPath);
ebd8d4e8
IC
303 if (errorText) {
304 showValidationError(errorText);
305 return;
306 }
5eaa6877 307 bip32ExtendedKey = calcBip32ExtendedKey(derivationPath);
c554e6ff
IC
308 if (bip44TabSelected()) {
309 displayBip44Info();
310 }
6c08f364
IC
311 if (bip49TabSelected()) {
312 displayBip49Info();
313 }
ebd8d4e8 314 displayBip32Info();
ebd8d4e8
IC
315 }
316
317 function generateClicked() {
c6624d51
IC
318 if (isUsingOwnEntropy()) {
319 return;
320 }
ebd8d4e8
IC
321 clearDisplay();
322 showPending();
323 setTimeout(function() {
5ee7bb9e 324 setMnemonicLanguage();
ebd8d4e8
IC
325 var phrase = generateRandomPhrase();
326 if (!phrase) {
327 return;
328 }
329 phraseChanged();
330 }, 50);
331 }
332
5ee7bb9e
IC
333 function languageChanged() {
334 setTimeout(function() {
335 setMnemonicLanguage();
336 if (DOM.phrase.val().length > 0) {
337 var newPhrase = convertPhraseToNewLanguage();
338 DOM.phrase.val(newPhrase);
339 phraseChanged();
340 }
341 else {
342 DOM.generate.trigger("click");
343 }
344 }, 50);
345 }
346
ebd8d4e8 347 function toggleIndexes() {
700901cd 348 showIndex = !showIndex;
ebd8d4e8
IC
349 $("td.index span").toggleClass("invisible");
350 }
351
352 function toggleAddresses() {
700901cd 353 showAddress = !showAddress;
ebd8d4e8
IC
354 $("td.address span").toggleClass("invisible");
355 }
356
1b12b2f5
IC
357 function togglePublicKeys() {
358 showPubKey = !showPubKey;
359 $("td.pubkey span").toggleClass("invisible");
360 }
361
ebd8d4e8 362 function togglePrivateKeys() {
700901cd 363 showPrivKey = !showPrivKey;
ebd8d4e8
IC
364 $("td.privkey span").toggleClass("invisible");
365 }
366
367 // Private methods
368
369 function generateRandomPhrase() {
370 if (!hasStrongRandom()) {
371 var errorText = "This browser does not support strong randomness";
372 showValidationError(errorText);
373 return;
374 }
88df3739 375 var numWords = parseInt(DOM.generatedStrength.val());
ebd8d4e8
IC
376 var strength = numWords / 3 * 32;
377 var words = mnemonic.generate(strength);
378 DOM.phrase.val(words);
379 return words;
380 }
381
efe41586 382 function calcBip32RootKeyFromSeed(phrase, passphrase) {
3e0ed16a 383 seed = mnemonic.toSeed(phrase, passphrase);
a0091a40 384 bip32RootKey = bitcoinjs.bitcoin.HDNode.fromSeedHex(seed, network);
efe41586
IC
385 }
386
387 function calcBip32RootKeyFromBase58(rootKeyBase58) {
a0091a40 388 bip32RootKey = bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, network);
efe41586
IC
389 }
390
391 function calcBip32ExtendedKey(path) {
0a1f0259
IC
392 // Check there's a root key to derive from
393 if (!bip32RootKey) {
394 return bip32RootKey;
395 }
5eaa6877 396 var extendedKey = bip32RootKey;
ebd8d4e8
IC
397 // Derive the key from the path
398 var pathBits = path.split("/");
399 for (var i=0; i<pathBits.length; i++) {
400 var bit = pathBits[i];
401 var index = parseInt(bit);
402 if (isNaN(index)) {
403 continue;
404 }
405 var hardened = bit[bit.length-1] == "'";
a0091a40 406 var isPriv = !(extendedKey.isNeutered());
ba3cb9ec
IC
407 var invalidDerivationPath = hardened && !isPriv;
408 if (invalidDerivationPath) {
409 extendedKey = null;
410 }
411 else if (hardened) {
5eaa6877 412 extendedKey = extendedKey.deriveHardened(index);
ebd8d4e8
IC
413 }
414 else {
5eaa6877 415 extendedKey = extendedKey.derive(index);
ebd8d4e8
IC
416 }
417 }
5eaa6877 418 return extendedKey
ebd8d4e8
IC
419 }
420
421 function showValidationError(errorText) {
422 DOM.feedback
423 .text(errorText)
424 .show();
425 }
426
427 function hideValidationError() {
428 DOM.feedback
429 .text("")
430 .hide();
431 }
432
433 function findPhraseErrors(phrase) {
ebd8d4e8 434 // Preprocess the words
783981de 435 phrase = mnemonic.normalizeString(phrase);
5ee7bb9e 436 var words = phraseToWordArray(phrase);
057722b0
IC
437 // Detect blank phrase
438 if (words.length == 0) {
439 return "Blank mnemonic";
440 }
563e401a 441 // Check each word
5ee7bb9e
IC
442 for (var i=0; i<words.length; i++) {
443 var word = words[i];
444 var language = getLanguage();
445 if (WORDLISTS[language].indexOf(word) == -1) {
563e401a
IC
446 console.log("Finding closest match to " + word);
447 var nearestWord = findNearestWord(word);
448 return word + " not in wordlist, did you mean " + nearestWord + "?";
449 }
450 }
ebd8d4e8 451 // Check the words are valid
5ee7bb9e 452 var properPhrase = wordArrayToPhrase(words);
ebd8d4e8
IC
453 var isValid = mnemonic.check(properPhrase);
454 if (!isValid) {
455 return "Invalid mnemonic";
456 }
457 return false;
458 }
459
efe41586
IC
460 function validateRootKey(rootKeyBase58) {
461 try {
cd7c8327 462 bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, network);
efe41586
IC
463 }
464 catch (e) {
465 return "Invalid root key";
466 }
467 return "";
468 }
469
38523d36 470 function getDerivationPath() {
32fab2c3 471 if (bip44TabSelected()) {
38523d36
IC
472 var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44);
473 var coin = parseIntNoNaN(DOM.bip44coin.val(), 0);
474 var account = parseIntNoNaN(DOM.bip44account.val(), 0);
475 var change = parseIntNoNaN(DOM.bip44change.val(), 0);
476 var path = "m/";
477 path += purpose + "'/";
478 path += coin + "'/";
479 path += account + "'/";
480 path += change;
481 DOM.bip44path.val(path);
482 var derivationPath = DOM.bip44path.val();
483 console.log("Using derivation path from BIP44 tab: " + derivationPath);
484 return derivationPath;
485 }
6c08f364
IC
486 if (bip49TabSelected()) {
487 var purpose = parseIntNoNaN(DOM.bip49purpose.val(), 49);
488 var coin = parseIntNoNaN(DOM.bip49coin.val(), 0);
489 var account = parseIntNoNaN(DOM.bip49account.val(), 0);
490 var change = parseIntNoNaN(DOM.bip49change.val(), 0);
491 var path = "m/";
492 path += purpose + "'/";
493 path += coin + "'/";
494 path += account + "'/";
495 path += change;
496 DOM.bip49path.val(path);
497 var derivationPath = DOM.bip49path.val();
498 console.log("Using derivation path from BIP49 tab: " + derivationPath);
499 return derivationPath;
500 }
32fab2c3 501 else if (bip32TabSelected()) {
38523d36
IC
502 var derivationPath = DOM.bip32path.val();
503 console.log("Using derivation path from BIP32 tab: " + derivationPath);
504 return derivationPath;
505 }
506 else {
507 console.log("Unknown derivation path");
508 }
509 }
510
ebd8d4e8 511 function findDerivationPathErrors(path) {
30c9e79d
IC
512 // TODO is not perfect but is better than nothing
513 // Inspired by
514 // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#test-vectors
515 // and
516 // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#extended-keys
517 var maxDepth = 255; // TODO verify this!!
518 var maxIndexValue = Math.pow(2, 31); // TODO verify this!!
519 if (path[0] != "m") {
520 return "First character must be 'm'";
521 }
522 if (path.length > 1) {
523 if (path[1] != "/") {
524 return "Separator must be '/'";
525 }
526 var indexes = path.split("/");
527 if (indexes.length > maxDepth) {
528 return "Derivation depth is " + indexes.length + ", must be less than " + maxDepth;
529 }
530 for (var depth = 1; depth<indexes.length; depth++) {
531 var index = indexes[depth];
532 var invalidChars = index.replace(/^[0-9]+'?$/g, "")
533 if (invalidChars.length > 0) {
534 return "Invalid characters " + invalidChars + " found at depth " + depth;
535 }
536 var indexValue = parseInt(index.replace("'", ""));
537 if (isNaN(depth)) {
538 return "Invalid number at depth " + depth;
539 }
540 if (indexValue > maxIndexValue) {
541 return "Value of " + indexValue + " at depth " + depth + " must be less than " + maxIndexValue;
542 }
543 }
544 }
0a1f0259
IC
545 // Check root key exists or else derivation path is useless!
546 if (!bip32RootKey) {
547 return "No root key";
548 }
ba3cb9ec 549 // Check no hardened derivation path when using xpub keys
b18eb97a
IC
550 var hardenedPath = path.indexOf("'") > -1;
551 var hardenedAddresses = bip32TabSelected() && DOM.hardenedAddresses.prop("checked");
552 var hardened = hardenedPath || hardenedAddresses;
a0091a40 553 var isXpubkey = bip32RootKey.isNeutered();
ba3cb9ec
IC
554 if (hardened && isXpubkey) {
555 return "Hardened derivation path is invalid with xpub key";
556 }
ebd8d4e8
IC
557 return false;
558 }
559
c554e6ff
IC
560 function displayBip44Info() {
561 // Get the derivation path for the account
562 var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44);
563 var coin = parseIntNoNaN(DOM.bip44coin.val(), 0);
564 var account = parseIntNoNaN(DOM.bip44account.val(), 0);
565 var path = "m/";
566 path += purpose + "'/";
567 path += coin + "'/";
568 path += account + "'/";
569 // Calculate the account extended keys
570 var accountExtendedKey = calcBip32ExtendedKey(path);
571 var accountXprv = accountExtendedKey.toBase58();
a0091a40 572 var accountXpub = accountExtendedKey.neutered().toBase58();
c554e6ff
IC
573 // Display the extended keys
574 DOM.bip44accountXprv.val(accountXprv);
575 DOM.bip44accountXpub.val(accountXpub);
576 }
577
6c08f364
IC
578 function displayBip49Info() {
579 // Get the derivation path for the account
580 var purpose = parseIntNoNaN(DOM.bip49purpose.val(), 49);
581 var coin = parseIntNoNaN(DOM.bip49coin.val(), 0);
582 var account = parseIntNoNaN(DOM.bip49account.val(), 0);
583 var path = "m/";
584 path += purpose + "'/";
585 path += coin + "'/";
586 path += account + "'/";
587 // Calculate the account extended keys
588 var accountExtendedKey = calcBip32ExtendedKey(path);
589 var accountXprv = accountExtendedKey.toBase58();
590 var accountXpub = accountExtendedKey.neutered().toBase58();
591 // Display the extended keys
592 DOM.bip49accountXprv.val(accountXprv);
593 DOM.bip49accountXpub.val(accountXpub);
594 }
595
ebd8d4e8
IC
596 function displayBip32Info() {
597 // Display the key
3e0ed16a 598 DOM.seed.val(seed);
ebd8d4e8
IC
599 var rootKey = bip32RootKey.toBase58();
600 DOM.rootKey.val(rootKey);
ba3cb9ec 601 var xprvkeyB58 = "NA";
a0091a40 602 if (!bip32ExtendedKey.isNeutered()) {
ba3cb9ec
IC
603 xprvkeyB58 = bip32ExtendedKey.toBase58();
604 }
605 var extendedPrivKey = xprvkeyB58;
ebd8d4e8 606 DOM.extendedPrivKey.val(extendedPrivKey);
a0091a40 607 var extendedPubKey = bip32ExtendedKey.neutered().toBase58();
ebd8d4e8
IC
608 DOM.extendedPubKey.val(extendedPubKey);
609 // Display the addresses and privkeys
610 clearAddressesList();
611 displayAddresses(0, 20);
612 }
613
614 function displayAddresses(start, total) {
40892aba
IC
615 generationProcesses.push(new (function() {
616
617 var rows = [];
618
619 this.stop = function() {
620 for (var i=0; i<rows.length; i++) {
621 rows[i].shouldGenerate = false;
622 }
0eda54f5 623 hidePending();
40892aba
IC
624 }
625
626 for (var i=0; i<total; i++) {
627 var index = i + start;
0eda54f5
IC
628 var isLast = i == total - 1;
629 rows.push(new TableRow(index, isLast));
40892aba
IC
630 }
631
632 })());
ebd8d4e8
IC
633 }
634
0eda54f5 635 function TableRow(index, isLast) {
a8c45487 636
40892aba
IC
637 var self = this;
638 this.shouldGenerate = true;
146e089e 639 var useHardenedAddresses = DOM.hardenedAddresses.prop("checked");
0cda44d5
IC
640 var isP2wpkhNestedInP2sh = bip49TabSelected();
641 var p2wpkhNestedInP2shAvailable = networkHasBip49();
146e089e 642
a8c45487
IC
643 function init() {
644 calculateValues();
645 }
646
647 function calculateValues() {
648 setTimeout(function() {
40892aba
IC
649 if (!self.shouldGenerate) {
650 return;
651 }
a0091a40 652 var key = "NA";
146e089e
IC
653 if (useHardenedAddresses) {
654 key = bip32ExtendedKey.deriveHardened(index);
655 }
656 else {
657 key = bip32ExtendedKey.derive(index);
658 }
a8c45487 659 var address = key.getAddress().toString();
ba3cb9ec 660 var privkey = "NA";
a0091a40
IC
661 if (!key.isNeutered()) {
662 privkey = key.keyPair.toWIF(network);
ba3cb9ec 663 }
a0091a40 664 var pubkey = key.getPublicKeyBuffer().toString('hex');
38523d36 665 var indexText = getDerivationPath() + "/" + index;
146e089e
IC
666 if (useHardenedAddresses) {
667 indexText = indexText + "'";
668 }
0edac945 669 // Ethereum values are different
534481b6 670 if (networks[DOM.network.val()].name == "ETH - Ethereum") {
a0091a40 671 var privKeyBuffer = key.keyPair.d.toBuffer();
0edac945
IC
672 privkey = privKeyBuffer.toString('hex');
673 var addressBuffer = ethUtil.privateToAddress(privKeyBuffer);
49b21f12
IC
674 var hexAddress = addressBuffer.toString('hex');
675 var checksumAddress = ethUtil.toChecksumAddress(hexAddress);
676 address = ethUtil.addHexPrefix(checksumAddress);
d0239db4
IC
677 privkey = ethUtil.addHexPrefix(privkey);
678 pubkey = ethUtil.addHexPrefix(pubkey);
0edac945 679 }
64a7d2aa 680 // Ripple values are different
534481b6 681 if (networks[DOM.network.val()].name == "XRP - Ripple") {
64a7d2aa 682 privkey = convertRipplePriv(privkey);
683 address = convertRippleAdrr(address);
684 }
6c08f364 685 // BIP49 addresses are different
0cda44d5
IC
686 if (isP2wpkhNestedInP2sh) {
687 if (!p2wpkhNestedInP2shAvailable) {
6c08f364
IC
688 return;
689 }
690 var keyhash = bitcoinjs.bitcoin.crypto.hash160(key.getPublicKeyBuffer());
691 var scriptsig = bitcoinjs.bitcoin.script.witnessPubKeyHash.output.encode(keyhash);
692 var addressbytes = bitcoinjs.bitcoin.crypto.hash160(scriptsig);
693 var scriptpubkey = bitcoinjs.bitcoin.script.scriptHash.output.encode(addressbytes);
694 address = bitcoinjs.bitcoin.address.fromOutputScript(scriptpubkey, network)
695 }
1b12b2f5 696 addAddressToList(indexText, address, pubkey, privkey);
0eda54f5
IC
697 if (isLast) {
698 hidePending();
699 }
a8c45487
IC
700 }, 50)
701 }
702
703 init();
704
705 }
706
ebd8d4e8
IC
707 function showMore() {
708 var start = DOM.addresses.children().length;
709 var rowsToAdd = parseInt(DOM.rowsToAdd.val());
710 if (isNaN(rowsToAdd)) {
711 rowsToAdd = 20;
712 DOM.rowsToAdd.val("20");
713 }
714 if (rowsToAdd > 200) {
715 var msg = "Generating " + rowsToAdd + " rows could take a while. ";
716 msg += "Do you want to continue?";
717 if (!confirm(msg)) {
718 return;
719 }
720 }
ebd8d4e8 721 displayAddresses(start, rowsToAdd);
ebd8d4e8
IC
722 }
723
724 function clearDisplay() {
725 clearAddressesList();
fa2e4e93 726 clearKeys();
ebd8d4e8
IC
727 hideValidationError();
728 }
729
730 function clearAddressesList() {
731 DOM.addresses.empty();
40892aba
IC
732 stopGenerating();
733 }
734
735 function stopGenerating() {
736 while (generationProcesses.length > 0) {
737 var generation = generationProcesses.shift();
738 generation.stop();
739 }
ebd8d4e8
IC
740 }
741
fa2e4e93
IC
742 function clearKeys() {
743 clearRootKey();
744 clearDerivedKeys();
745 }
746
747 function clearRootKey() {
ebd8d4e8 748 DOM.rootKey.val("");
fa2e4e93
IC
749 }
750
751 function clearDerivedKeys() {
ebd8d4e8
IC
752 DOM.extendedPrivKey.val("");
753 DOM.extendedPubKey.val("");
fa2e4e93
IC
754 DOM.bip44accountXprv.val("");
755 DOM.bip44accountXpub.val("");
ebd8d4e8
IC
756 }
757
1b12b2f5 758 function addAddressToList(indexText, address, pubkey, privkey) {
ebd8d4e8 759 var row = $(addressRowTemplate.html());
700901cd
IC
760 // Elements
761 var indexCell = row.find(".index span");
762 var addressCell = row.find(".address span");
1b12b2f5 763 var pubkeyCell = row.find(".pubkey span");
700901cd
IC
764 var privkeyCell = row.find(".privkey span");
765 // Content
ae30fed8 766 indexCell.text(indexText);
700901cd 767 addressCell.text(address);
1b12b2f5 768 pubkeyCell.text(pubkey);
700901cd
IC
769 privkeyCell.text(privkey);
770 // Visibility
771 if (!showIndex) {
772 indexCell.addClass("invisible");
773 }
774 if (!showAddress) {
775 addressCell.addClass("invisible");
776 }
1b12b2f5
IC
777 if (!showPubKey) {
778 pubkeyCell.addClass("invisible");
779 }
700901cd 780 if (!showPrivKey) {
6d628db7 781 privkeyCell.addClass("invisible");
700901cd 782 }
ebd8d4e8 783 DOM.addresses.append(row);
e00964cc
IC
784 var rowShowQrEls = row.find("[data-show-qr]");
785 setQrEvents(rowShowQrEls);
ebd8d4e8
IC
786 }
787
788 function hasStrongRandom() {
789 return 'crypto' in window && window['crypto'] !== null;
790 }
791
792 function disableForms() {
793 $("form").on("submit", function(e) {
794 e.preventDefault();
795 });
796 }
797
ebd8d4e8
IC
798 function parseIntNoNaN(val, defaultVal) {
799 var v = parseInt(val);
800 if (isNaN(v)) {
801 return defaultVal;
802 }
803 return v;
804 }
805
806 function showPending() {
807 DOM.feedback
808 .text("Calculating...")
809 .show();
810 }
811
563e401a 812 function findNearestWord(word) {
5ee7bb9e
IC
813 var language = getLanguage();
814 var words = WORDLISTS[language];
563e401a
IC
815 var minDistance = 99;
816 var closestWord = words[0];
817 for (var i=0; i<words.length; i++) {
818 var comparedTo = words[i];
6ea15134
IC
819 if (comparedTo.indexOf(word) == 0) {
820 return comparedTo;
821 }
563e401a
IC
822 var distance = Levenshtein.get(word, comparedTo);
823 if (distance < minDistance) {
824 closestWord = comparedTo;
825 minDistance = distance;
826 }
827 }
828 return closestWord;
829 }
830
ebd8d4e8
IC
831 function hidePending() {
832 DOM.feedback
833 .text("")
834 .hide();
835 }
836
7f15cb6e
IC
837 function populateNetworkSelect() {
838 for (var i=0; i<networks.length; i++) {
839 var network = networks[i];
840 var option = $("<option>");
841 option.attr("value", i);
842 option.text(network.name);
7b742f87
IC
843 if (network.name == "BTC - Bitcoin") {
844 option.prop("selected", true);
845 }
7f15cb6e
IC
846 DOM.phraseNetwork.append(option);
847 }
848 }
29bf60f5 849
b4fd763c
AG
850 function populateClientSelect() {
851 for (var i=0; i<clients.length; i++) {
852 var client = clients[i];
853 var option = $("<option>");
854 option.attr("value", i);
855 option.text(client.name);
29bf60f5 856 DOM.bip32Client.append(option);
b4fd763c
AG
857 }
858 }
7f15cb6e 859
5ee7bb9e
IC
860 function getLanguage() {
861 var defaultLanguage = "english";
862 // Try to get from existing phrase
863 var language = getLanguageFromPhrase();
864 // Try to get from url if not from phrase
865 if (language.length == 0) {
866 language = getLanguageFromUrl();
867 }
868 // Default to English if no other option
869 if (language.length == 0) {
870 language = defaultLanguage;
871 }
872 return language;
873 }
874
875 function getLanguageFromPhrase(phrase) {
876 // Check if how many words from existing phrase match a language.
877 var language = "";
878 if (!phrase) {
879 phrase = DOM.phrase.val();
880 }
881 if (phrase.length > 0) {
882 var words = phraseToWordArray(phrase);
883 var languageMatches = {};
884 for (l in WORDLISTS) {
885 // Track how many words match in this language
886 languageMatches[l] = 0;
887 for (var i=0; i<words.length; i++) {
888 var wordInLanguage = WORDLISTS[l].indexOf(words[i]) > -1;
889 if (wordInLanguage) {
890 languageMatches[l]++;
891 }
892 }
893 // Find languages with most word matches.
894 // This is made difficult due to commonalities between Chinese
895 // simplified vs traditional.
896 var mostMatches = 0;
897 var mostMatchedLanguages = [];
898 for (var l in languageMatches) {
899 var numMatches = languageMatches[l];
900 if (numMatches > mostMatches) {
901 mostMatches = numMatches;
902 mostMatchedLanguages = [l];
903 }
904 else if (numMatches == mostMatches) {
905 mostMatchedLanguages.push(l);
906 }
907 }
908 }
909 if (mostMatchedLanguages.length > 0) {
910 // Use first language and warn if multiple detected
911 language = mostMatchedLanguages[0];
912 if (mostMatchedLanguages.length > 1) {
913 console.warn("Multiple possible languages");
914 console.warn(mostMatchedLanguages);
915 }
916 }
917 }
918 return language;
919 }
920
921 function getLanguageFromUrl() {
c6624d51
IC
922 for (var language in WORDLISTS) {
923 if (window.location.hash.indexOf(language) > -1) {
924 return language;
925 }
926 }
927 return "";
5ee7bb9e
IC
928 }
929
930 function setMnemonicLanguage() {
931 var language = getLanguage();
932 // Load the bip39 mnemonic generator for this language if required
933 if (!(language in mnemonics)) {
934 mnemonics[language] = new Mnemonic(language);
935 }
936 mnemonic = mnemonics[language];
937 }
938
939 function convertPhraseToNewLanguage() {
940 var oldLanguage = getLanguageFromPhrase();
941 var newLanguage = getLanguageFromUrl();
942 var oldPhrase = DOM.phrase.val();
943 var oldWords = phraseToWordArray(oldPhrase);
944 var newWords = [];
945 for (var i=0; i<oldWords.length; i++) {
946 var oldWord = oldWords[i];
947 var index = WORDLISTS[oldLanguage].indexOf(oldWord);
948 var newWord = WORDLISTS[newLanguage][index];
949 newWords.push(newWord);
950 }
951 newPhrase = wordArrayToPhrase(newWords);
952 return newPhrase;
953 }
954
955 // TODO look at jsbip39 - mnemonic.splitWords
956 function phraseToWordArray(phrase) {
957 var words = phrase.split(/\s/g);
958 var noBlanks = [];
959 for (var i=0; i<words.length; i++) {
960 var word = words[i];
961 if (word.length > 0) {
962 noBlanks.push(word);
963 }
964 }
965 return noBlanks;
966 }
967
968 // TODO look at jsbip39 - mnemonic.joinWords
969 function wordArrayToPhrase(words) {
970 var phrase = words.join(" ");
971 var language = getLanguageFromPhrase(phrase);
972 if (language == "japanese") {
973 phrase = words.join("\u3000");
974 }
975 return phrase;
976 }
977
c6624d51
IC
978 function isUsingOwnEntropy() {
979 return DOM.useEntropy.prop("checked");
980 }
981
982 function setMnemonicFromEntropy() {
0a84fe6a 983 clearEntropyFeedback();
057722b0 984 // Get entropy value
c6624d51 985 var entropyStr = DOM.entropy.val();
057722b0 986 // Work out minimum base for entropy
c6624d51 987 var entropy = Entropy.fromString(entropyStr);
057722b0 988 if (entropy.binaryStr.length == 0) {
c6624d51
IC
989 return;
990 }
991 // Show entropy details
1cf1bbaf 992 showEntropyFeedback(entropy);
3599674d
IC
993 // Use entropy hash if not using raw entropy
994 var bits = entropy.binaryStr;
995 var mnemonicLength = DOM.entropyMnemonicLength.val();
996 if (mnemonicLength != "raw") {
997 // Get bits by hashing entropy with SHA256
998 var hash = sjcl.hash.sha256.hash(entropy.cleanStr);
999 var hex = sjcl.codec.hex.fromBits(hash);
1000 bits = BigInteger.parse(hex, 16).toString(2);
53aaab27 1001 while (bits.length % 256 != 0) {
3599674d
IC
1002 bits = "0" + bits;
1003 }
1004 // Truncate hash to suit number of words
1005 mnemonicLength = parseInt(mnemonicLength);
1006 var numberOfBits = 32 * mnemonicLength / 3;
1007 bits = bits.substring(0, numberOfBits);
1008 }
c6624d51 1009 // Discard trailing entropy
3599674d 1010 var bitsToUse = Math.floor(bits.length / 32) * 32;
d6fd8ebf
IC
1011 var start = bits.length - bitsToUse;
1012 var binaryStr = bits.substring(start);
c6624d51
IC
1013 // Convert entropy string to numeric array
1014 var entropyArr = [];
adc8ce12
IC
1015 for (var i=0; i<binaryStr.length / 8; i++) {
1016 var byteAsBits = binaryStr.substring(i*8, i*8+8);
1017 var entropyByte = parseInt(byteAsBits, 2);
c6624d51
IC
1018 entropyArr.push(entropyByte)
1019 }
1020 // Convert entropy array to mnemonic
1021 var phrase = mnemonic.toMnemonic(entropyArr);
1022 // Set the mnemonic in the UI
1023 DOM.phrase.val(phrase);
1024 }
1025
0a84fe6a 1026 function clearEntropyFeedback() {
20f459ce 1027 DOM.entropyCrackTime.text("...");
1cf1bbaf 1028 DOM.entropyType.text("");
0a84fe6a
IC
1029 DOM.entropyWordCount.text("0");
1030 DOM.entropyEventCount.text("0");
1031 DOM.entropyBitsPerEvent.text("0");
1032 DOM.entropyBits.text("0");
1033 DOM.entropyFiltered.html("&nbsp;");
1034 DOM.entropyBinary.html("&nbsp;");
c6624d51
IC
1035 }
1036
1cf1bbaf 1037 function showEntropyFeedback(entropy) {
6422c1cd 1038 var numberOfBits = entropy.binaryStr.length;
20f459ce 1039 var timeToCrack = "unknown";
9bc39377
IC
1040 try {
1041 var z = zxcvbn(entropy.base.parts.join(""));
20f459ce
IC
1042 timeToCrack = z.crack_times_display.offline_fast_hashing_1e10_per_second;
1043 if (z.feedback.warning != "") {
1044 timeToCrack = timeToCrack + " - " + z.feedback.warning;
1045 };
9bc39377
IC
1046 }
1047 catch (e) {
9bc39377
IC
1048 console.log("Error detecting entropy strength with zxcvbn:");
1049 console.log(e);
b299a6a7 1050 }
391c7f26 1051 var entropyTypeStr = getEntropyTypeStr(entropy);
6422c1cd 1052 var wordCount = Math.floor(numberOfBits / 32) * 3;
94959756 1053 var bitsPerEvent = entropy.bitsPerEvent.toFixed(2);
b54c1218 1054 DOM.entropyFiltered.html(entropy.cleanHtml);
391c7f26 1055 DOM.entropyType.text(entropyTypeStr);
20f459ce 1056 DOM.entropyCrackTime.text(timeToCrack);
1cf1bbaf 1057 DOM.entropyEventCount.text(entropy.base.ints.length);
6422c1cd 1058 DOM.entropyBits.text(numberOfBits);
0a84fe6a
IC
1059 DOM.entropyWordCount.text(wordCount);
1060 DOM.entropyBinary.text(entropy.binaryStr);
6422c1cd 1061 DOM.entropyBitsPerEvent.text(bitsPerEvent);
02f05d3e
IC
1062 }
1063
391c7f26
IC
1064 function getEntropyTypeStr(entropy) {
1065 var typeStr = entropy.base.str;
1066 // Add some detail if these are cards
1067 if (entropy.base.asInt == 52) {
1068 var cardDetail = []; // array of message strings
1069 // Detect duplicates
1070 var dupes = [];
1071 var dupeTracker = {};
1072 for (var i=0; i<entropy.base.parts.length; i++) {
1073 var card = entropy.base.parts[i];
5c653a12
IC
1074 var cardUpper = card.toUpperCase();
1075 if (cardUpper in dupeTracker) {
391c7f26
IC
1076 dupes.push(card);
1077 }
5c653a12 1078 dupeTracker[cardUpper] = true;
391c7f26
IC
1079 }
1080 if (dupes.length > 0) {
1081 var dupeWord = "duplicates";
1082 if (dupes.length == 1) {
1083 dupeWord = "duplicate";
1084 }
1085 var msg = dupes.length + " " + dupeWord + ": " + dupes.slice(0,3).join(" ");
1086 if (dupes.length > 3) {
1087 msg += "...";
1088 }
1089 cardDetail.push(msg);
1090 }
1091 // Detect full deck
1092 var uniqueCards = [];
1093 for (var uniqueCard in dupeTracker) {
1094 uniqueCards.push(uniqueCard);
1095 }
1096 if (uniqueCards.length == 52) {
1097 cardDetail.unshift("full deck");
1098 }
bbc29c80
IC
1099 // Detect missing cards
1100 var values = "A23456789TJQK";
1101 var suits = "CDHS";
1102 var missingCards = [];
1103 for (var i=0; i<suits.length; i++) {
1104 for (var j=0; j<values.length; j++) {
1105 var card = values[j] + suits[i];
1106 if (!(card in dupeTracker)) {
1107 missingCards.push(card);
1108 }
1109 }
1110 }
1111 // Display missing cards if six or less, ie clearly going for full deck
1112 if (missingCards.length > 0 && missingCards.length <= 6) {
1113 var msg = missingCards.length + " missing: " + missingCards.slice(0,3).join(" ");
1114 if (missingCards.length > 3) {
1115 msg += "...";
1116 }
1117 cardDetail.push(msg);
1118 }
391c7f26
IC
1119 // Add card details to typeStr
1120 if (cardDetail.length > 0) {
1121 typeStr += " (" + cardDetail.join(", ") + ")";
1122 }
1123 }
1124 return typeStr;
1125 }
1126
e00964cc
IC
1127 function setQrEvents(els) {
1128 els.on("mouseenter", createQr);
1129 els.on("mouseleave", destroyQr);
1130 els.on("click", toggleQr);
1131 }
1132
1133 function createQr(e) {
1134 var content = e.target.textContent || e.target.value;
1135 if (content) {
1136 var size = 130;
1137 DOM.qrImage.qrcode({width: size, height: size, text: content});
1138 if (!showQr) {
97811c29
IC
1139 DOM.qrHider.addClass("hidden");
1140 }
1141 else {
1142 DOM.qrHider.removeClass("hidden");
e00964cc
IC
1143 }
1144 DOM.qrContainer.removeClass("hidden");
1145 }
1146 }
1147
1148 function destroyQr() {
1149 DOM.qrImage.text("");
1150 DOM.qrContainer.addClass("hidden");
1151 }
1152
1153 function toggleQr() {
1154 showQr = !showQr;
97811c29 1155 DOM.qrHider.toggleClass("hidden");
e00964cc
IC
1156 DOM.qrHint.toggleClass("hidden");
1157 }
1158
32fab2c3
IC
1159 function bip44TabSelected() {
1160 return DOM.bip44tab.hasClass("active");
1161 }
1162
1163 function bip32TabSelected() {
1164 return DOM.bip32tab.hasClass("active");
1165 }
1166
6c08f364 1167 function networkHasBip49() {
0cda44d5 1168 return networks[DOM.network.val()].p2wpkhNestedInP2shAvailable;
6c08f364
IC
1169 }
1170
1171 function bip49TabSelected() {
1172 return DOM.bip49tab.hasClass("active");
1173 }
1174
1175 function setHdCoin(coinValue) {
1176 DOM.bip44coin.val(coinValue);
1177 DOM.bip49coin.val(coinValue);
1178 }
1179
0cda44d5 1180 function showP2wpkhNestedInP2shAvailable() {
6c08f364
IC
1181 DOM.bip49unavailable.addClass("hidden");
1182 DOM.bip49available.removeClass("hidden");
1183 }
1184
0cda44d5 1185 function showP2wpkhNestedInP2shUnavailable() {
6c08f364
IC
1186 DOM.bip49available.addClass("hidden");
1187 DOM.bip49unavailable.removeClass("hidden");
1188 }
1189
7f15cb6e 1190 var networks = [
daab55dc
IC
1191 {
1192 name: "BCH - Bitcoin Cash",
0cda44d5 1193 p2wpkhNestedInP2shAvailable: false,
daab55dc
IC
1194 onSelect: function() {
1195 network = bitcoinjs.bitcoin.networks.bitcoin;
1196 setHdCoin(145);
1197 },
1198 },
7f15cb6e 1199 {
534481b6 1200 name: "BTC - Bitcoin",
0cda44d5 1201 p2wpkhNestedInP2shAvailable: true,
7a995731 1202 onSelect: function() {
a0091a40 1203 network = bitcoinjs.bitcoin.networks.bitcoin;
6c08f364 1204 setHdCoin(0);
7a995731
IC
1205 },
1206 },
7f15cb6e 1207 {
534481b6 1208 name: "BTC - Bitcoin Testnet",
0cda44d5 1209 p2wpkhNestedInP2shAvailable: true,
7a995731 1210 onSelect: function() {
a0091a40 1211 network = bitcoinjs.bitcoin.networks.testnet;
6c08f364 1212 setHdCoin(1);
7a995731
IC
1213 },
1214 },
7f15cb6e 1215 {
534481b6 1216 name: "CLAM - Clams",
0cda44d5 1217 p2wpkhNestedInP2shAvailable: false,
7a995731 1218 onSelect: function() {
a0091a40 1219 network = bitcoinjs.bitcoin.networks.clam;
6c08f364 1220 setHdCoin(23);
7a995731
IC
1221 },
1222 },
0921f370 1223 {
1224 name: "CRW - Crown",
0cda44d5 1225 p2wpkhNestedInP2shAvailable: false,
0921f370 1226 onSelect: function() {
6c08f364
IC
1227 network = bitcoinjs.bitcoin.networks.crown;
1228 setHdCoin(72);
0921f370 1229 },
1230 },
7f15cb6e 1231 {
534481b6 1232 name: "DASH - Dash",
0cda44d5 1233 p2wpkhNestedInP2shAvailable: false,
e3a9508c 1234 onSelect: function() {
a0091a40 1235 network = bitcoinjs.bitcoin.networks.dash;
6c08f364 1236 setHdCoin(5);
e3a9508c
IC
1237 },
1238 },
c0386f3b 1239 {
534481b6 1240 name: "DASH - Dash Testnet",
0cda44d5 1241 p2wpkhNestedInP2shAvailable: false,
c0386f3b 1242 onSelect: function() {
a0091a40 1243 network = bitcoinjs.bitcoin.networks.dashtn;
6c08f364 1244 setHdCoin(1);
c0386f3b
KR
1245 },
1246 },
e3a9508c 1247 {
534481b6 1248 name: "DOGE - Dogecoin",
0cda44d5 1249 p2wpkhNestedInP2shAvailable: false,
534481b6 1250 onSelect: function() {
6c08f364
IC
1251 network = bitcoinjs.bitcoin.networks.dogecoin;
1252 setHdCoin(3);
534481b6 1253 },
1254 },
1255 {
1256 name: "ETH - Ethereum",
0cda44d5 1257 p2wpkhNestedInP2shAvailable: false,
e3a9508c 1258 onSelect: function() {
a0091a40 1259 network = bitcoinjs.bitcoin.networks.bitcoin;
6c08f364 1260 setHdCoin(60);
e3a9508c
IC
1261 },
1262 },
f88fab20 1263 {
534481b6 1264 name: "GAME - GameCredits",
0cda44d5 1265 p2wpkhNestedInP2shAvailable: false,
f88fab20 1266 onSelect: function() {
a0091a40 1267 network = bitcoinjs.bitcoin.networks.game;
6c08f364 1268 setHdCoin(101);
f88fab20 1269 },
1270 },
a3baa26e 1271 {
534481b6 1272 name: "JBS - Jumbucks",
0cda44d5 1273 p2wpkhNestedInP2shAvailable: false,
a3baa26e 1274 onSelect: function() {
a0091a40 1275 network = bitcoinjs.bitcoin.networks.jumbucks;
6c08f364 1276 setHdCoin(26);
a3baa26e
IC
1277 },
1278 },
1279 {
534481b6 1280 name: "LTC - Litecoin",
0cda44d5 1281 p2wpkhNestedInP2shAvailable: false,
a3baa26e 1282 onSelect: function() {
a0091a40 1283 network = bitcoinjs.bitcoin.networks.litecoin;
6c08f364 1284 setHdCoin(2);
3abab9b0 1285 DOM.litecoinLtubContainer.removeClass("hidden");
a3baa26e
IC
1286 },
1287 },
56ad9601
JS
1288 {
1289 name: "MAZA - Maza",
0cda44d5 1290 p2wpkhNestedInP2shAvailable: false,
56ad9601
JS
1291 onSelect: function() {
1292 network = bitcoinjs.bitcoin.networks.maza;
1293 setHdCoin(13);
1294 },
1295 },
1296
a3baa26e 1297 {
534481b6 1298 name: "NMC - Namecoin",
0cda44d5 1299 p2wpkhNestedInP2shAvailable: false,
a3baa26e 1300 onSelect: function() {
a0091a40 1301 network = bitcoinjs.bitcoin.networks.namecoin;
6c08f364 1302 setHdCoin(7);
a3baa26e
IC
1303 },
1304 },
c0df0189 1305 {
1306 name: "PIVX - PIVX",
0cda44d5 1307 p2wpkhNestedInP2shAvailable: false,
c0df0189 1308 onSelect: function() {
1309 network = bitcoinjs.bitcoin.networks.pivx;
1310 setHdCoin(119);
1311 },
1312 },
1313 {
1314 name: "PIVX - PIVX Testnet",
0cda44d5 1315 p2wpkhNestedInP2shAvailable: false,
c0df0189 1316 onSelect: function() {
1317 network = bitcoinjs.bitcoin.networks.pivxtestnet;
1318 setHdCoin(1);
1319 },
1320 },
5c434a8a 1321 {
534481b6 1322 name: "PPC - Peercoin",
0cda44d5 1323 p2wpkhNestedInP2shAvailable: false,
5c434a8a 1324 onSelect: function() {
a0091a40 1325 network = bitcoinjs.bitcoin.networks.peercoin;
6c08f364 1326 setHdCoin(6);
5c434a8a
CM
1327 },
1328 },
64a7d2aa 1329 {
534481b6 1330 name: "SDC - ShadowCash",
0cda44d5 1331 p2wpkhNestedInP2shAvailable: false,
82f91834 1332 onSelect: function() {
a0091a40 1333 network = bitcoinjs.bitcoin.networks.shadow;
6c08f364 1334 setHdCoin(35);
82f91834
DG
1335 },
1336 },
07ac4350 1337 {
534481b6 1338 name: "SDC - ShadowCash Testnet",
0cda44d5 1339 p2wpkhNestedInP2shAvailable: false,
07ac4350 1340 onSelect: function() {
a0091a40 1341 network = bitcoinjs.bitcoin.networks.shadowtn;
6c08f364 1342 setHdCoin(1);
07ac4350 1343 },
1344 },
7a5a87a0 1345 {
534481b6 1346 name: "SLM - Slimcoin",
0cda44d5 1347 p2wpkhNestedInP2shAvailable: false,
7a5a87a0 1348 onSelect: function() {
a0091a40 1349 network = bitcoinjs.bitcoin.networks.slimcoin;
6c08f364 1350 setHdCoin(63);
7a5a87a0
GH
1351 },
1352 },
1353 {
534481b6 1354 name: "SLM - Slimcoin Testnet",
0cda44d5 1355 p2wpkhNestedInP2shAvailable: false,
7a5a87a0 1356 onSelect: function() {
a0091a40 1357 network = bitcoinjs.bitcoin.networks.slimcointn;
6c08f364 1358 setHdCoin(111);
7a5a87a0
GH
1359 },
1360 },
07ac4350 1361 {
534481b6 1362 name: "VIA - Viacoin",
0cda44d5 1363 p2wpkhNestedInP2shAvailable: false,
07ac4350 1364 onSelect: function() {
a0091a40 1365 network = bitcoinjs.bitcoin.networks.viacoin;
6c08f364 1366 setHdCoin(14);
07ac4350 1367 },
1368 },
0edac945 1369 {
534481b6 1370 name: "VIA - Viacoin Testnet",
0cda44d5 1371 p2wpkhNestedInP2shAvailable: false,
0edac945 1372 onSelect: function() {
a0091a40 1373 network = bitcoinjs.bitcoin.networks.viacointestnet;
6c08f364 1374 setHdCoin(1);
0edac945
IC
1375 },
1376 },
534481b6 1377 {
7ebdf61c 1378 name: "XMY - Myriadcoin",
0cda44d5 1379 p2wpkhNestedInP2shAvailable: false,
534481b6 1380 onSelect: function() {
7ebdf61c
IC
1381 network = bitcoinjs.bitcoin.networks.myriadcoin;
1382 setHdCoin(90);
534481b6 1383 },
adedbf91 1384 },
1385 {
7ebdf61c 1386 name: "XRP - Ripple",
0cda44d5 1387 p2wpkhNestedInP2shAvailable: false,
adedbf91 1388 onSelect: function() {
7ebdf61c
IC
1389 network = bitcoinjs.bitcoin.networks.bitcoin;
1390 setHdCoin(144);
adedbf91 1391 },
534481b6 1392 }
7f15cb6e 1393 ]
6ee4fb7d 1394
b4fd763c
AG
1395 var clients = [
1396 {
1397 name: "Bitcoin Core",
1398 onSelect: function() {
1399 DOM.bip32path.val("m/0'/0'");
1400 DOM.hardenedAddresses.prop('checked', true);
1401 },
1402 },
1403 {
1404 name: "blockchain.info",
1405 onSelect: function() {
1406 DOM.bip32path.val("m/44'/0'/0'");
1407 DOM.hardenedAddresses.prop('checked', false);
1408 },
1409 },
1410 {
1411 name: "MultiBit HD",
1412 onSelect: function() {
1413 DOM.bip32path.val("m/0'/0");
1414 DOM.hardenedAddresses.prop('checked', false);
1415 },
1416 }
1417 ]
7a995731 1418
ebd8d4e8
IC
1419 init();
1420
1421})();