X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2Fjs%2Findex.js;h=89f8d0dd006053d7f86233f74d313fce00127680;hb=02f05d3e467ba8ccf3fca2811eda847fc71e511f;hp=c5f6c111fa61e4c38695939a1106a8afcb94396d;hpb=5ee7bb9ec28b9bbfc72cb703ac9a01f5af5ee7d9;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FBIP39.git diff --git a/src/js/index.js b/src/js/index.js index c5f6c11..89f8d0d 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -11,16 +11,31 @@ var showIndex = true; var showAddress = true; + var showPubKey = true; var showPrivKey = true; + var entropyChangeTimeoutEvent = null; var phraseChangeTimeoutEvent = null; var rootKeyChangedTimeoutEvent = null; var DOM = {}; DOM.network = $(".network"); DOM.phraseNetwork = $("#network-phrase"); + DOM.useEntropy = $(".use-entropy"); + DOM.entropyContainer = $(".entropy-container"); + DOM.entropy = $(".entropy"); + 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"); DOM.generate = $(".generate"); DOM.seed = $(".seed"); DOM.rootKey = $(".root-key"); @@ -36,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"); @@ -45,12 +60,16 @@ DOM.tab = $(".derivation-type a"); DOM.indexToggle = $(".index-toggle"); DOM.addressToggle = $(".address-toggle"); + DOM.publicKeyToggle = $(".public-key-toggle"); DOM.privateKeyToggle = $(".private-key-toggle"); DOM.languages = $(".languages a"); function init() { // Events 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); @@ -65,6 +84,7 @@ DOM.hardenedAddresses.on("change", calcForDerivationPath); DOM.indexToggle.on("click", toggleIndexes); DOM.addressToggle.on("click", toggleAddresses); + DOM.publicKeyToggle.on("click", togglePublicKeys); DOM.privateKeyToggle.on("click", togglePrivateKeys); DOM.languages.on("click", languageChanged); disableForms(); @@ -86,6 +106,22 @@ } } + function setEntropyVisibility() { + if (isUsingOwnEntropy()) { + DOM.entropyContainer.removeClass("hidden"); + DOM.generateContainer.addClass("hidden"); + DOM.phrase.prop("readonly", true); + DOM.entropy.focus(); + entropyChanged(); + } + else { + DOM.entropyContainer.addClass("hidden"); + DOM.generateContainer.removeClass("hidden"); + DOM.phrase.prop("readonly", false); + hidePending(); + } + } + function delayedPhraseChanged() { hideValidationError(); showPending(); @@ -113,6 +149,43 @@ hidePending(); } + function delayedEntropyChanged() { + hideValidationError(); + showPending(); + if (entropyChangeTimeoutEvent != null) { + clearTimeout(entropyChangeTimeoutEvent); + } + entropyChangeTimeoutEvent = setTimeout(entropyChanged, 400); + } + + 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(); + // 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() { // Warn if there is an existing mnemonic or passphrase. if (DOM.phrase.val().length > 0 || DOM.passphrase.val().length > 0) { @@ -165,6 +238,9 @@ } function generateClicked() { + if (isUsingOwnEntropy()) { + return; + } clearDisplay(); showPending(); setTimeout(function() { @@ -201,6 +277,11 @@ $("td.address span").toggleClass("invisible"); } + function togglePublicKeys() { + showPubKey = !showPubKey; + $("td.pubkey span").toggleClass("invisible"); + } + function togglePrivateKeys() { showPrivKey = !showPrivKey; $("td.privkey span").toggleClass("invisible"); @@ -214,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); @@ -263,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 -1) { + return language; + } + } + return ""; } function setMnemonicLanguage() { @@ -636,6 +731,134 @@ return phrase; } + function isUsingOwnEntropy() { + return DOM.useEntropy.prop("checked"); + } + + function setMnemonicFromEntropy() { + clearEntropyFeedback(); + // Get entropy value + var entropyStr = DOM.entropy.val(); + // Work out minimum base for entropy + var entropy = Entropy.fromString(entropyStr); + if (entropy.binaryStr.length == 0) { + return; + } + // Show entropy details + 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); + } + // Discard trailing entropy + var bitsToUse = Math.floor(bits.length / 32) * 32; + var start = bits.length - bitsToUse; + var binaryStr = bits.substring(start); + // 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 = getNumberOfEntropyBits(entropy); + var wordCount = Math.floor(entropy.binaryStr.length / 32) * 3; + DOM.entropyFiltered.html(entropy.cleanHtml); + 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)); + } + + 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 + } + + // 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)); + } + return f; + } + var networks = [ { name: "Bitcoin",