X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FBIP39.git;a=blobdiff_plain;f=src%2Fjs%2Findex.js;h=cd7f281b5489f8e1a956a19fb4784e3fa3c1cb8c;hp=0e4cc052a4d704fa749bf9076c9bc68fc5145530;hb=c6624d51f4e5607202e48903352574c47571baab;hpb=d737abf6809622228faf7d5fe54101e2d87d72a4 diff --git a/src/js/index.js b/src/js/index.js index 0e4cc05..cd7f281 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"); @@ -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,21 @@ } } + 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); + } + } + function delayedPhraseChanged() { hideValidationError(); showPending(); @@ -116,6 +139,20 @@ hidePending(); } + function delayedEntropyChanged() { + hideValidationError(); + showPending(); + if (entropyChangeTimeoutEvent != null) { + clearTimeout(entropyChangeTimeoutEvent); + } + entropyChangeTimeoutEvent = setTimeout(entropyChanged, 400); + } + + function entropyChanged() { + setMnemonicFromEntropy(); + phraseChanged(); + } + function delayedRootKeyChanged() { // Warn if there is an existing mnemonic or passphrase. if (DOM.phrase.val().length > 0 || DOM.passphrase.val().length > 0) { @@ -168,6 +205,9 @@ } function generateClicked() { + if (isUsingOwnEntropy()) { + return; + } clearDisplay(); showPending(); setTimeout(function() { @@ -599,7 +639,12 @@ } function getLanguageFromUrl() { - return window.location.hash.substring(1); + for (var language in WORDLISTS) { + if (window.location.hash.indexOf(language) > -1) { + return language; + } + } + return ""; } function setMnemonicLanguage() { @@ -650,6 +695,65 @@ return phrase; } + function isUsingOwnEntropy() { + return DOM.useEntropy.prop("checked"); + } + + function setMnemonicFromEntropy() { + hideEntropyError(); + // Work out minimum base for entropy + var entropyStr = DOM.entropy.val(); + var entropy = Entropy.fromString(entropyStr); + if (entropy.hexStr.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 strength = "an extremely weak"; + if (entropy.hexStr.length >= 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" + } + 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); + // Convert entropy string to numeric array + var entropyArr = []; + for (var i=0; i