]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/blobdiff - src/js/index.js
Show entropy from PRNG and word indexes
[perso/Immae/Projets/Cryptomonnaies/BIP39.git] / src / js / index.js
index 261a6d19c43005d77809bae797f7f1726fdbee0e..58090c34a410638579d133e8d1b961212fbeb971 100644 (file)
@@ -37,6 +37,7 @@
     DOM.entropyBitsPerEvent = DOM.entropyContainer.find(".bits-per-event");
     DOM.entropyWordCount = DOM.entropyContainer.find(".word-count");
     DOM.entropyBinary = DOM.entropyContainer.find(".binary");
+    DOM.entropyWordIndexes = DOM.entropyContainer.find(".word-indexes");
     DOM.entropyMnemonicLength = DOM.entropyContainer.find(".mnemonic-length");
     DOM.entropyFilterWarning = DOM.entropyContainer.find(".filter-warning");
     DOM.phrase = $(".phrase");
         var passphrase = DOM.passphrase.val();
         calcBip32RootKeyFromSeed(phrase, passphrase);
         calcForDerivationPath();
+        // Show the word indexes
+        showWordIndexes();
     }
 
     function tabChanged() {
             showValidationError(errorText);
             return;
         }
+        // get the amount of entropy to use
         var numWords = parseInt(DOM.generatedStrength.val());
         var strength = numWords / 3 * 32;
-        var words = mnemonic.generate(strength);
+        var buffer = new Uint8Array(strength / 8);
+        // create secure entropy
+        var data = crypto.getRandomValues(buffer);
+        // show the words
+        var words = mnemonic.toMnemonic(data);
         DOM.phrase.val(words);
+        // show the entropy
+        var entropyHex = uint8ArrayToHex(data);
+        DOM.entropy.val(entropyHex);
+        // ensure entropy fields are consistent with what is being displayed
+        DOM.entropyMnemonicLength.val("raw");
         return words;
     }
 
         var phrase = mnemonic.toMnemonic(entropyArr);
         // Set the mnemonic in the UI
         DOM.phrase.val(phrase);
+        // Show the word indexes
+        showWordIndexes();
     }
 
     function clearEntropyFeedback() {
         return parseInt(lastBitClean);
     }
 
+    function uint8ArrayToHex(a) {
+        var s = ""
+        for (var i=0; i<a.length; i++) {
+            var h = a[i].toString(16);
+            while (h.length < 2) {
+                h = "0" + h;
+            }
+            s = s + h;
+        }
+        return s;
+    }
+
+    function showWordIndexes() {
+        var phrase = DOM.phrase.val();
+        var words = phraseToWordArray(phrase);
+        var wordIndexes = [];
+        var language = getLanguage();
+        for (var i=0; i<words.length; i++) {
+            var word = words[i];
+            var wordIndex = WORDLISTS[language].indexOf(word);
+            wordIndexes.push(wordIndex);
+        }
+        var wordIndexesStr = wordIndexes.join(", ");
+        DOM.entropyWordIndexes.text(wordIndexesStr);
+    }
+
     var networks = [
         {
             name: "BCH - Bitcoin Cash",