]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/blobdiff - src/js/jsbip39.js
Add immae_config.yaml
[perso/Immae/Projets/Cryptomonnaies/BIP39.git] / src / js / jsbip39.js
index 80ce656583dda9cb8a726ebe28986c50f6df77a4..1e52d9d93296681f47b78b127904d0d2f1e7fbd3 100644 (file)
@@ -97,22 +97,10 @@ var Mnemonic = function(language) {
     }
 
     self.check = function(mnemonic) {
-        var mnemonic = self.splitWords(mnemonic);
-        if (mnemonic.length % 3 > 0) {
-            return false
+        var b = mnemonicToBinaryString(mnemonic);
+        if (b === null) {
+            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 = idx.join('');
         var l = b.length;
         //d = b[:l / 33 * 32]
         //h = b[-l / 33:]
@@ -128,12 +116,33 @@ 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.toRawEntropyBin = function(mnemonic) {
+        var b = mnemonicToBinaryString(mnemonic);
+        var d = b.substring(0, b.length / 33 * 32);
+        return d;
+    }
+
     self.toSeed = function(mnemonic, passphrase) {
         passphrase = passphrase || '';
-        mnemonic = self.joinWords(self.splitWords(self.normalizeString(mnemonic))); // removes blanks
+        mnemonic = self.joinWords(self.splitWords(mnemonic)); // removes duplicate blanks
+        var mnemonicNormalized = self.normalizeString(mnemonic);
         passphrase = self.normalizeString(passphrase)
         passphrase = "mnemonic" + passphrase;
-        var mnemonicBits = sjcl.codec.utf8String.toBits(mnemonic);
+        var mnemonicBits = sjcl.codec.utf8String.toBits(mnemonicNormalized);
         var passphraseBits = sjcl.codec.utf8String.toBits(passphrase);
         var result = sjcl.misc.pbkdf2(mnemonicBits, passphraseBits, PBKDF2_ROUNDS, 512, hmacSHA512);
         var hashHex = sjcl.codec.hex.fromBits(result);
@@ -155,14 +164,7 @@ var Mnemonic = function(language) {
     }
 
     self.normalizeString = function(str) {
-        if (typeof str.normalize == "function") {
-            return str.normalize("NFKD");
-        }
-        else {
-            // TODO decide how to handle this in the future.
-            // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/normalize
-            return str;
-        }
+        return str.normalize("NFKD");
     }
 
     function byteArrayToWordArray(data) {
@@ -206,6 +208,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