X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FBIP39.git;a=blobdiff_plain;f=src%2Fjs%2Findex.js;h=6367c0197fc985bc17d96a8ae15e9387effb89c8;hp=e064185f411124f18f78316ca72114b5fc24898d;hb=32fab2c39507357909438c2be8cd100c6539038f;hpb=391c7f267f6be3356d0bffa74e2726245b5f45b2 diff --git a/src/js/index.js b/src/js/index.js index e064185..6367c01 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -13,6 +13,7 @@ var showAddress = true; var showPubKey = true; var showPrivKey = true; + var showQr = true; var entropyChangeTimeoutEvent = null; var phraseChangeTimeoutEvent = null; @@ -63,6 +64,10 @@ DOM.publicKeyToggle = $(".public-key-toggle"); DOM.privateKeyToggle = $(".private-key-toggle"); DOM.languages = $(".languages a"); + DOM.qrContainer = $(".qr-container"); + DOM.qrImage = DOM.qrContainer.find(".qr-image"); + DOM.qrHint = DOM.qrContainer.find(".qr-hint"); + DOM.showQrEls = $("[data-show-qr]"); function init() { // Events @@ -87,6 +92,7 @@ DOM.publicKeyToggle.on("click", togglePublicKeys); DOM.privateKeyToggle.on("click", togglePrivateKeys); DOM.languages.on("click", languageChanged); + setQrEvents(DOM.showQrEls); disableForms(); hidePending(); hideValidationError(); @@ -232,7 +238,7 @@ showValidationError(errorText); return; } - calcBip32ExtendedKey(derivationPath); + bip32ExtendedKey = calcBip32ExtendedKey(derivationPath); displayBip32Info(); hidePending(); } @@ -312,7 +318,7 @@ } function calcBip32ExtendedKey(path) { - bip32ExtendedKey = bip32RootKey; + var extendedKey = bip32RootKey; // Derive the key from the path var pathBits = path.split("/"); for (var i=0; i= 64) { + if (numberOfBits >= 64) { strength = "very weak"; } - if (entropy.binaryStr.length >= 96) { + if (numberOfBits >= 96) { strength = "weak"; } - if (entropy.binaryStr.length >= 128) { + if (numberOfBits >= 128) { strength = "strong"; } - if (entropy.binaryStr.length >= 160) { + if (numberOfBits >= 160) { strength = "very strong"; } - if (entropy.binaryStr.length >= 192) { + if (numberOfBits >= 192) { strength = "extremely strong"; } // If time to crack is less than one day, and password is considered // strong or better based on the number of bits, rename strength to // 'easily cracked'. - var z = zxcvbn(entropy.cleanStr); - var timeToCrack = z.crack_times_seconds.offline_fast_hashing_1e10_per_second; - if (timeToCrack < 86400 && entropy.binaryStr.length >= 128) { - strength = "easily cracked"; - if (z.feedback.warning != "") { - strength = strength + " - " + z.feedback.warning; - }; - } - var bitsStr = getNumberOfEntropyBits(entropy); - var wordCount = Math.floor(entropy.binaryStr.length / 32) * 3; + try { + var z = zxcvbn(entropy.base.parts.join("")); + var timeToCrack = z.crack_times_seconds.offline_fast_hashing_1e10_per_second; + if (timeToCrack < 86400 && entropy.binaryStr.length >= 128) { + strength = "easily cracked"; + if (z.feedback.warning != "") { + strength = strength + " - " + z.feedback.warning; + }; + } + } + catch (e) { + strength = "unknown"; + console.log("Error detecting entropy strength with zxcvbn:"); + console.log(e); + } var entropyTypeStr = getEntropyTypeStr(entropy); + var wordCount = Math.floor(numberOfBits / 32) * 3; + var bitsPerEvent = entropy.bitsPerEvent.toFixed(2); DOM.entropyFiltered.html(entropy.cleanHtml); DOM.entropyType.text(entropyTypeStr); DOM.entropyStrength.text(strength); DOM.entropyEventCount.text(entropy.base.ints.length); - DOM.entropyBits.text(bitsStr); + DOM.entropyBits.text(numberOfBits); DOM.entropyWordCount.text(wordCount); DOM.entropyBinary.text(entropy.binaryStr); - DOM.entropyBitsPerEvent.text(Math.log2(entropy.base.asInt).toFixed(2)); - } - - function getNumberOfEntropyBits(entropy) { - var bitsStr = entropy.binaryStr.length.toString(); - // If using cards, assume they are not reused, thus additional entropy - // decreases as more cards are used. This means entropy is measured - // using n!, not base^n. - // eg the second last card can be only one of two, not one of fifty two - // so the added entropy for that card is only one bit at most - if (entropy.base.asInt == 52) { - var totalCombos = factorial(52); - var remainingCards = 52 - entropy.base.parts.length; - var remainingCombos = factorial(remainingCards); - var currentCombos = totalCombos.divide(remainingCombos); - bitsStr = currentCombos.toString(2).length.toString(); - } - return bitsStr + DOM.entropyBitsPerEvent.text(bitsPerEvent); } function getEntropyTypeStr(entropy) { @@ -858,10 +858,11 @@ var dupeTracker = {}; for (var i=0; i 0) { var dupeWord = "duplicates"; @@ -882,6 +883,26 @@ if (uniqueCards.length == 52) { cardDetail.unshift("full deck"); } + // Detect missing cards + var values = "A23456789TJQK"; + var suits = "CDHS"; + var missingCards = []; + for (var i=0; i 0 && missingCards.length <= 6) { + var msg = missingCards.length + " missing: " + missingCards.slice(0,3).join(" "); + if (missingCards.length > 3) { + msg += "..."; + } + cardDetail.push(msg); + } // Add card details to typeStr if (cardDetail.length > 0) { typeStr += " (" + cardDetail.join(", ") + ")"; @@ -890,16 +911,41 @@ return typeStr; } - // Depends on BigInteger - function factorial(n) { - if (n == 0) { - return 1; - } - f = BigInteger.ONE; - for (var i=1; i<=n; i++) { - f = f.multiply(new BigInteger(i)); + function setQrEvents(els) { + els.on("mouseenter", createQr); + els.on("mouseleave", destroyQr); + els.on("click", toggleQr); + } + + function createQr(e) { + var content = e.target.textContent || e.target.value; + if (content) { + var size = 130; + DOM.qrImage.qrcode({width: size, height: size, text: content}); + if (!showQr) { + DOM.qrImage.addClass("hidden"); + } + DOM.qrContainer.removeClass("hidden"); } - return f; + } + + function destroyQr() { + DOM.qrImage.text(""); + DOM.qrContainer.addClass("hidden"); + } + + function toggleQr() { + showQr = !showQr; + DOM.qrImage.toggleClass("hidden"); + DOM.qrHint.toggleClass("hidden"); + } + + function bip44TabSelected() { + return DOM.bip44tab.hasClass("active"); + } + + function bip32TabSelected() { + return DOM.bip32tab.hasClass("active"); } var networks = [