]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/commitdiff
Merge pull request #271 from cernekee/local
authoriancoleman <1281387+iancoleman@users.noreply.github.com>
Sun, 10 Nov 2019 22:21:07 +0000 (09:21 +1100)
committerGitHub <noreply@github.com>
Sun, 10 Nov 2019 22:21:07 +0000 (09:21 +1100)
Allow converting mnemonic back to raw entropy value

src/js/index.js
src/js/jsbip39.js

index f3302a64bfee229566ff574b82f474ad6db82773..da901f6af65a4739f11796f938ddc6aad7a8d2d8 100644 (file)
         if (phraseChangeTimeoutEvent != null) {
             clearTimeout(phraseChangeTimeoutEvent);
         }
-        phraseChangeTimeoutEvent = setTimeout(phraseChanged, 400);
+        phraseChangeTimeoutEvent = setTimeout(function() {
+            phraseChanged();
+            var entropy = mnemonic.toRawEntropyHex(DOM.phrase.val());
+            if (entropy !== null) {
+                DOM.entropyMnemonicLength.val("raw");
+                DOM.entropy.val(entropy);
+            }
+        }, 400);
     }
 
     function phraseChanged() {
index 3230e3bb46f275356bf3db523d85f66f36d65f3e..9a6e7ec696b3122a7d73ebf4ec070f7621d4c6e8 100644 (file)
@@ -97,22 +97,10 @@ var Mnemonic = function(language) {
     }
 
     self.check = function(mnemonic) {
-        var mnemonic = self.splitWords(mnemonic);
-        if (mnemonic.length == 0 || mnemonic.length % 3 > 0) {
-            return false
-        }
-        // idx = map(lambda x: bin(self.wordlist.index(x))[2:].zfill(11), mnemonic)
-        var idx = [];
-        for (var i=0; i<mnemonic.length; i++) {
-            var word = mnemonic[i];
-            var wordIndex = wordlist.indexOf(word);
-            if (wordIndex == -1) {
-                return false;
-            }
-            var binaryIndex = zfill(wordIndex.toString(2), 11);
-            idx.push(binaryIndex);
+        var b = mnemonicToBinaryString(mnemonic);
+        if (b === null) {
+            return false;
         }
-        var b = idx.join('');
         var l = b.length;
         //d = b[:l / 33 * 32]
         //h = b[-l / 33:]
@@ -128,6 +116,20 @@ var Mnemonic = function(language) {
         return h == nh;
     }
 
+    self.toRawEntropyHex = function(mnemonic) {
+        var b = mnemonicToBinaryString(mnemonic);
+        if (b === null)
+            return null;
+        var d = b.substring(0, b.length / 33 * 32);
+        var nd = binaryStringToWordArray(d);
+
+        var h = "";
+        for (var i=0; i<nd.length; i++) {
+            h += ('0000000' + nd[i].toString(16)).slice(-8);
+        }
+        return h;
+    }
+
     self.toSeed = function(mnemonic, passphrase) {
         passphrase = passphrase || '';
         mnemonic = self.joinWords(self.splitWords(mnemonic)); // removes duplicate blanks
@@ -200,6 +202,25 @@ var Mnemonic = function(language) {
         return a;
     }
 
+    function mnemonicToBinaryString(mnemonic) {
+        var mnemonic = self.splitWords(mnemonic);
+        if (mnemonic.length == 0 || mnemonic.length % 3 > 0) {
+            return null;
+        }
+        // idx = map(lambda x: bin(self.wordlist.index(x))[2:].zfill(11), mnemonic)
+        var idx = [];
+        for (var i=0; i<mnemonic.length; i++) {
+            var word = mnemonic[i];
+            var wordIndex = wordlist.indexOf(word);
+            if (wordIndex == -1) {
+                return null;
+            }
+            var binaryIndex = zfill(wordIndex.toString(2), 11);
+            idx.push(binaryIndex);
+        }
+        return idx.join('');
+    }
+
     // Pad a numeric string on the left with zero digits until the given width
     // is reached.
     // Note this differs to the python implementation because it does not