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