X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2Fjs%2Findex.js;h=107610886fd24debf758a4f789c7795832653448;hb=0a84fe6ae178e6952c17e4208de8b405e5da55a8;hp=cd7f281b5489f8e1a956a19fb4784e3fa3c1cb8c;hpb=c6624d51f4e5607202e48903352574c47571baab;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FBIP39.git diff --git a/src/js/index.js b/src/js/index.js index cd7f281..1076108 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -24,7 +24,15 @@ DOM.useEntropy = $(".use-entropy"); DOM.entropyContainer = $(".entropy-container"); DOM.entropy = $(".entropy"); - DOM.entropyError = $(".entropy-error"); + DOM.entropyFiltered = DOM.entropyContainer.find(".filtered"); + DOM.entropyType = DOM.entropyContainer.find(".type"); + DOM.entropyStrength = DOM.entropyContainer.find(".strength"); + DOM.entropyEventCount = DOM.entropyContainer.find(".event-count"); + DOM.entropyBits = DOM.entropyContainer.find(".bits"); + DOM.entropyBitsPerEvent = DOM.entropyContainer.find(".bits-per-event"); + DOM.entropyWordCount = DOM.entropyContainer.find(".word-count"); + DOM.entropyBinary = DOM.entropyContainer.find(".binary"); + DOM.entropyMnemonicLength = DOM.entropyContainer.find(".mnemonic-length"); DOM.phrase = $(".phrase"); DOM.passphrase = $(".passphrase"); DOM.generateContainer = $(".generate-container"); @@ -43,7 +51,7 @@ DOM.bip44coin = $("#bip44 .coin"); DOM.bip44account = $("#bip44 .account"); DOM.bip44change = $("#bip44 .change"); - DOM.strength = $(".strength"); + DOM.generatedStrength = $(".generate-container .strength"); DOM.hardenedAddresses = $(".hardened-addresses"); DOM.addresses = $(".addresses"); DOM.rowsToAdd = $(".rows-to-add"); @@ -61,6 +69,7 @@ DOM.network.on("change", networkChanged); DOM.useEntropy.on("change", setEntropyVisibility); DOM.entropy.on("input", delayedEntropyChanged); + DOM.entropyMnemonicLength.on("change", entropyChanged); DOM.phrase.on("input", delayedPhraseChanged); DOM.passphrase.on("input", delayedPhraseChanged); DOM.generate.on("click", generateClicked); @@ -109,6 +118,7 @@ DOM.entropyContainer.addClass("hidden"); DOM.generateContainer.removeClass("hidden"); DOM.phrase.prop("readonly", false); + hidePending(); } } @@ -149,8 +159,31 @@ } function entropyChanged() { + // If blank entropy, clear mnemonic, addresses, errors + if (DOM.entropy.val().trim().length == 0) { + clearDisplay(); + clearEntropyFeedback(); + DOM.phrase.val(""); + showValidationError("Blank entropy"); + return; + } + // Get the current phrase to detect changes + var phrase = DOM.phrase.val(); + // Set the phrase from the entropy setMnemonicFromEntropy(); - phraseChanged(); + // Recalc addresses if the phrase has changed + var newPhrase = DOM.phrase.val(); + if (newPhrase != phrase) { + if (newPhrase.length == 0) { + clearDisplay(); + } + else { + phraseChanged(); + } + } + else { + hidePending(); + } } function delayedRootKeyChanged() { @@ -262,7 +295,7 @@ showValidationError(errorText); return; } - var numWords = parseInt(DOM.strength.val()); + var numWords = parseInt(DOM.generatedStrength.val()); var strength = numWords / 3 * 32; var words = mnemonic.generate(strength); DOM.phrase.val(words); @@ -311,10 +344,13 @@ } function findPhraseErrors(phrase) { - // TODO make this right // Preprocess the words phrase = mnemonic.normalizeString(phrase); var words = phraseToWordArray(phrase); + // Detect blank phrase + if (words.length == 0) { + return "Blank mnemonic"; + } // Check each word for (var i=0; i= 8) { - strength = "a very weak"; - } - if (entropy.hexStr.length >= 12) { - strength = "a weak"; - } - if (entropy.hexStr.length >= 24) { - strength = "a strong"; - } - if (entropy.hexStr.length >= 32) { - strength = "a very strong"; - } - if (entropy.hexStr.length >= 40) { - strength = "an extremely strong"; - } - if (entropy.hexStr.length >=48) { - strength = "an even stronger" + showEntropyFeedback(entropy); + // Use entropy hash if not using raw entropy + var bits = entropy.binaryStr; + var mnemonicLength = DOM.entropyMnemonicLength.val(); + if (mnemonicLength != "raw") { + // Get bits by hashing entropy with SHA256 + var hash = sjcl.hash.sha256.hash(entropy.cleanStr); + var hex = sjcl.codec.hex.fromBits(hash); + bits = BigInteger.parse(hex, 16).toString(2); + for (var i=0; i<256-bits.length; i++) { + bits = "0" + bits; + } + // Truncate hash to suit number of words + mnemonicLength = parseInt(mnemonicLength); + var numberOfBits = 32 * mnemonicLength / 3; + bits = bits.substring(0, numberOfBits); } - var msg = "Have " + entropy.binaryStr.length + " bits of entropy, " + extraChars + " more " + entropy.base.str + " chars required to generate " + strength + " mnemonic: " + entropy.cleanStr; - showEntropyError(msg); // Discard trailing entropy - var hexStr = entropy.hexStr.substring(0, Math.floor(entropy.hexStr.length / 8) * 8); + var bitsToUse = Math.floor(bits.length / 32) * 32; + var binaryStr = bits.substring(0, bitsToUse); // Convert entropy string to numeric array var entropyArr = []; - for (var i=0; i= 64) { + strength = "very weak"; + } + if (entropy.binaryStr.length >= 96) { + strength = "weak"; + } + if (entropy.binaryStr.length >= 128) { + strength = "strong"; + } + if (entropy.binaryStr.length >= 160) { + strength = "very strong"; + } + if (entropy.binaryStr.length >= 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 = entropy.binaryStr.length; + var wordCount = Math.floor(entropy.binaryStr.length / 32) * 3; + DOM.entropyFiltered.text(entropy.cleanStr); + DOM.entropyType.text(entropy.base.str); + DOM.entropyStrength.text(strength); + DOM.entropyEventCount.text(entropy.base.ints.length); + DOM.entropyBits.text(bitsStr); + DOM.entropyWordCount.text(wordCount); + DOM.entropyBinary.text(entropy.binaryStr); + DOM.entropyBitsPerEvent.text(Math.log2(entropy.base.asInt).toFixed(2)); } var networks = [