X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2Fjs%2Findex.js;h=45db7b139861814e603903c68c5a583c5dd4fa6e;hb=88df3739e7e8b26b461a73fe3874b195e5b03fec;hp=0e4cc052a4d704fa749bf9076c9bc68fc5145530;hpb=1b12b2f5f12b32a2ed6e69ff77f10b889b383e97;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FBIP39.git diff --git a/src/js/index.js b/src/js/index.js index 0e4cc05..45db7b1 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -14,14 +14,20 @@ 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.entropyError = $(".entropy-error"); DOM.phrase = $(".phrase"); DOM.passphrase = $(".passphrase"); + DOM.generateContainer = $(".generate-container"); DOM.generate = $(".generate"); DOM.seed = $(".seed"); DOM.rootKey = $(".root-key"); @@ -37,7 +43,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"); @@ -53,6 +59,8 @@ function init() { // Events DOM.network.on("change", networkChanged); + DOM.useEntropy.on("change", setEntropyVisibility); + DOM.entropy.on("input", delayedEntropyChanged); DOM.phrase.on("input", delayedPhraseChanged); DOM.passphrase.on("input", delayedPhraseChanged); DOM.generate.on("click", generateClicked); @@ -89,6 +97,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(); @@ -116,6 +140,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(); + hideEntropyError(); + 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) { @@ -168,6 +229,9 @@ } function generateClicked() { + if (isUsingOwnEntropy()) { + return; + } clearDisplay(); showPending(); setTimeout(function() { @@ -222,7 +286,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); @@ -271,10 +335,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() { @@ -650,6 +722,69 @@ return phrase; } + function isUsingOwnEntropy() { + return DOM.useEntropy.prop("checked"); + } + + function setMnemonicFromEntropy() { + hideEntropyError(); + // 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 + var extraBits = 32 - (entropy.binaryStr.length % 32); + var extraChars = Math.ceil(extraBits * Math.log(2) / Math.log(entropy.base.asInt)); + var words = Math.floor(entropy.binaryStr.length / 32) * 3; + var strength = "an extremely weak"; + if (words >= 3) { + strength = "a very weak"; + } + if (words >= 6) { + strength = "a weak"; + } + if (words >= 9) { + strength = "a strong"; + } + if (words >= 12) { + strength = "a very strong"; + } + if (words >= 15) { + strength = "an extremely strong"; + } + if (words >= 18) { + strength = "an even stronger" + } + 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 bitsToUse = Math.floor(entropy.binaryStr.length / 32) * 32; + var binaryStr = entropy.binaryStr.substring(0, bitsToUse); + // Convert entropy string to numeric array + var entropyArr = []; + for (var i=0; i