]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/blobdiff - src/js/index.js
Test suggested word for first four letters of word
[perso/Immae/Projets/Cryptomonnaies/BIP39.git] / src / js / index.js
index 04559d102b0b62d646f79d33fc2c7ce0896acb87..c1989775ede6aa8f9e591d77ed7b66ade282c29d 100644 (file)
@@ -3,7 +3,7 @@
     // mnemonics is populated as required by getLanguage
     var mnemonics = { "english": new Mnemonic("english") };
     var mnemonic = mnemonics["english"];
-    var seed = null
+    var seed = null;
     var bip32RootKey = null;
     var bip32ExtendedKey = null;
     var network = bitcoin.networks.bitcoin;
@@ -13,7 +13,7 @@
     var showAddress = true;
     var showPubKey = true;
     var showPrivKey = true;
-    var showQr = true;
+    var showQr = false;
 
     var entropyChangeTimeoutEvent = null;
     var phraseChangeTimeoutEvent = null;
         // Calculate and display
         calcBip32RootKeyFromBase58(rootKeyBase58);
         calcForDerivationPath();
-        hidePending();
     }
 
     function calcForDerivationPath() {
         showPending();
+        clearAddressesList();
         hideValidationError();
         // Get the derivation path
         var derivationPath = getDerivationPath();
     }
 
     function calcBip32ExtendedKey(path) {
+        // Check there's a root key to derive from
+        if (!bip32RootKey) {
+            return bip32RootKey;
+        }
         var extendedKey = bip32RootKey;
         // Derive the key from the path
         var pathBits = path.split("/");
                 continue;
             }
             var hardened = bit[bit.length-1] == "'";
-            if (hardened) {
+            var isPriv = "privKey" in extendedKey;
+            var invalidDerivationPath = hardened && !isPriv;
+            if (invalidDerivationPath) {
+                extendedKey = null;
+            }
+            else if (hardened) {
                 extendedKey = extendedKey.deriveHardened(index);
             }
             else {
                 }
             }
         }
+        // Check root key exists or else derivation path is useless!
+        if (!bip32RootKey) {
+            return "No root key";
+        }
+        // Check no hardened derivation path when using xpub keys
+        var hardened = path.indexOf("'") > -1;
+        var isXpubkey = !("privKey" in bip32RootKey);
+        if (hardened && isXpubkey) {
+            return "Hardened derivation path is invalid with xpub key";
+        }
         return false;
     }
 
         DOM.seed.val(seed);
         var rootKey = bip32RootKey.toBase58();
         DOM.rootKey.val(rootKey);
-        var extendedPrivKey = bip32ExtendedKey.toBase58();
+        var xprvkeyB58 = "NA";
+        if (bip32ExtendedKey.privKey) {
+            xprvkeyB58 = bip32ExtendedKey.toBase58();
+        }
+        var extendedPrivKey = xprvkeyB58;
         DOM.extendedPrivKey.val(extendedPrivKey);
         var extendedPubKey = bip32ExtendedKey.toBase58(false);
         DOM.extendedPubKey.val(extendedPubKey);
                     key = bip32ExtendedKey.derive(index);
                 }
                 var address = key.getAddress().toString();
-                var privkey = key.privKey.toWIF(network);
+                var privkey = "NA";
+                if (key.privKey) {
+                    privkey = key.privKey.toWIF(network);
+                }
                 var pubkey = key.pubKey.toHex();
                 var indexText = getDerivationPath() + "/" + index;
                 if (useHardenedAddresses) {
         var closestWord = words[0];
         for (var i=0; i<words.length; i++) {
             var comparedTo = words[i];
+            if (comparedTo.indexOf(word) == 0) {
+                return comparedTo;
+            }
             var distance = Levenshtein.get(word, comparedTo);
             if (distance < minDistance) {
                 closestWord = comparedTo;