X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2Fjs%2Findex.js;h=a717a9e45bdd0369004ba521a0a25b008ee67ae9;hb=adc8ce127d4f8ea0d7e5ede6a82c2791d60ff4d2;hp=69f5eab0e7445d24bb9f2624195a6af791771039;hpb=07ac4350eec471d9b7f16182a5009df8b6d8f28e;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FBIP39.git diff --git a/src/js/index.js b/src/js/index.js index 69f5eab..a717a9e 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -1,6 +1,8 @@ (function() { - var mnemonic = new Mnemonic("english"); + // mnemonics is populated as required by getLanguage + var mnemonics = { "english": new Mnemonic("english") }; + var mnemonic = mnemonics["english"]; var seed = null var bip32RootKey = null; var bip32ExtendedKey = null; @@ -9,16 +11,23 @@ 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.entropyError = $(".entropy-error"); DOM.phrase = $(".phrase"); DOM.passphrase = $(".passphrase"); + DOM.generateContainer = $(".generate-container"); DOM.generate = $(".generate"); DOM.seed = $(".seed"); DOM.rootKey = $(".root-key"); @@ -43,11 +52,15 @@ 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.phrase.on("input", delayedPhraseChanged); DOM.passphrase.on("input", delayedPhraseChanged); DOM.generate.on("click", generateClicked); @@ -62,7 +75,9 @@ 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(); hidePending(); hideValidationError(); @@ -82,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(); @@ -94,6 +125,7 @@ function phraseChanged() { showPending(); hideValidationError(); + setMnemonicLanguage(); // Get the mnemonic phrase var phrase = DOM.phrase.val(); var errorText = findPhraseErrors(phrase); @@ -108,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) { @@ -160,9 +229,13 @@ } function generateClicked() { + if (isUsingOwnEntropy()) { + return; + } clearDisplay(); showPending(); setTimeout(function() { + setMnemonicLanguage(); var phrase = generateRandomPhrase(); if (!phrase) { return; @@ -171,6 +244,20 @@ }, 50); } + function languageChanged() { + setTimeout(function() { + setMnemonicLanguage(); + if (DOM.phrase.val().length > 0) { + var newPhrase = convertPhraseToNewLanguage(); + DOM.phrase.val(newPhrase); + phraseChanged(); + } + else { + DOM.generate.trigger("click"); + } + }, 50); + } + function toggleIndexes() { showIndex = !showIndex; $("td.index span").toggleClass("invisible"); @@ -181,6 +268,11 @@ $("td.address span").toggleClass("invisible"); } + function togglePublicKeys() { + showPubKey = !showPubKey; + $("td.pubkey span").toggleClass("invisible"); + } + function togglePrivateKeys() { showPrivKey = !showPrivKey; $("td.privkey span").toggleClass("invisible"); @@ -243,29 +335,25 @@ } function findPhraseErrors(phrase) { - // TODO make this right // Preprocess the words phrase = mnemonic.normalizeString(phrase); - var parts = phrase.split(" "); - var proper = []; - for (var i=0; i 0) { - // TODO check that lowercasing is always valid to do - proper.push(part.toLowerCase()); - } + var words = phraseToWordArray(phrase); + // Detect blank phrase + if (words.length == 0) { + return "Blank mnemonic"; } - var properPhrase = proper.join(' '); // Check each word - for (var i=0; i 0) { + var words = phraseToWordArray(phrase); + var languageMatches = {}; + for (l in WORDLISTS) { + // Track how many words match in this language + languageMatches[l] = 0; + for (var i=0; i -1; + if (wordInLanguage) { + languageMatches[l]++; + } + } + // Find languages with most word matches. + // This is made difficult due to commonalities between Chinese + // simplified vs traditional. + var mostMatches = 0; + var mostMatchedLanguages = []; + for (var l in languageMatches) { + var numMatches = languageMatches[l]; + if (numMatches > mostMatches) { + mostMatches = numMatches; + mostMatchedLanguages = [l]; + } + else if (numMatches == mostMatches) { + mostMatchedLanguages.push(l); + } + } + } + if (mostMatchedLanguages.length > 0) { + // Use first language and warn if multiple detected + language = mostMatchedLanguages[0]; + if (mostMatchedLanguages.length > 1) { + console.warn("Multiple possible languages"); + console.warn(mostMatchedLanguages); + } + } + } + return language; + } + + function getLanguageFromUrl() { + for (var language in WORDLISTS) { + if (window.location.hash.indexOf(language) > -1) { + return language; + } + } + return ""; + } + + function setMnemonicLanguage() { + var language = getLanguage(); + // Load the bip39 mnemonic generator for this language if required + if (!(language in mnemonics)) { + mnemonics[language] = new Mnemonic(language); + } + mnemonic = mnemonics[language]; + } + + function convertPhraseToNewLanguage() { + var oldLanguage = getLanguageFromPhrase(); + var newLanguage = getLanguageFromUrl(); + var oldPhrase = DOM.phrase.val(); + var oldWords = phraseToWordArray(oldPhrase); + var newWords = []; + for (var i=0; i 0) { + noBlanks.push(word); + } + } + return noBlanks; + } + + // TODO look at jsbip39 - mnemonic.joinWords + function wordArrayToPhrase(words) { + var phrase = words.join(" "); + var language = getLanguageFromPhrase(phrase); + if (language == "japanese") { + phrase = words.join("\u3000"); + } + 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