]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/blobdiff - src/js/index.js
Make CSS and JS path relative
[perso/Immae/Projets/Cryptomonnaies/BIP39.git] / src / js / index.js
index cd3a5063c6e34162b498e9be13769b496a9a7252..9dabe9534677e37c56985a6bfc43366f2c289695 100644 (file)
@@ -3,18 +3,28 @@
     var mnemonic = new Mnemonic("english");
     var bip32RootKey = null;
     var bip32ExtendedKey = null;
-    var network = Bitcoin.networks.bitcoin;
+    var network = bitcoin.networks.bitcoin;
     var addressRowTemplate = $("#address-row-template");
 
+    var showIndex = true;
+    var showAddress = true;
+    var showPrivKey = true;
+
     var phraseChangeTimeoutEvent = null;
 
     var DOM = {};
+    DOM.network = $(".network");
+    DOM.phraseNetwork = $("#network-phrase");
     DOM.phrase = $(".phrase");
     DOM.passphrase = $(".passphrase");
     DOM.generate = $(".generate");
     DOM.rootKey = $(".root-key");
     DOM.extendedPrivKey = $(".extended-priv-key");
     DOM.extendedPubKey = $(".extended-pub-key");
+    DOM.bip32tab = $("#bip32-tab");
+    DOM.bip44tab = $("#bip44-tab");
+    DOM.bip32panel = $("#bip32");
+    DOM.bip44panel = $("#bip44");
     DOM.bip32path = $("#bip32-path");
     DOM.bip44path = $("#bip44-path");
     DOM.bip44purpose = $("#bip44 .purpose");
     DOM.addressToggle = $(".address-toggle");
     DOM.privateKeyToggle = $(".private-key-toggle");
 
-    var derivationPath = DOM.bip44path.val();
+    var derivationPath = $(".tab-pane.active .path").val();
 
     function init() {
         // Events
+        DOM.network.on("change", networkChanged);
         DOM.phrase.on("input", delayedPhraseChanged);
         DOM.passphrase.on("input", delayedPhraseChanged);
         DOM.generate.on("click", generateClicked);
         disableForms();
         hidePending();
         hideValidationError();
+        populateNetworkSelect();
     }
 
     // Event handlers
 
+    function networkChanged(e) {
+        var network = e.target.value;
+        networks[network].onSelect();
+        setBip44DerivationPath();
+        delayedPhraseChanged();
+    }
+
     function delayedPhraseChanged() {
         hideValidationError();
         showPending();
 
     function bip44Changed() {
         setBip44DerivationPath();
-        derivationPath = DOM.bip44path.val();
         derivationChanged();
     }
 
     function toggleIndexes() {
+        showIndex = !showIndex;
         $("td.index span").toggleClass("invisible");
     }
 
     function toggleAddresses() {
+        showAddress = !showAddress;
         $("td.address span").toggleClass("invisible");
     }
 
     function togglePrivateKeys() {
+        showPrivKey = !showPrivKey;
         $("td.privkey span").toggleClass("invisible");
     }
 
             return;
         }
         var numWords = parseInt(DOM.strength.val());
-        // Check strength is an integer
-        if (isNaN(numWords)) {
-            DOM.strength.val("12");
-            numWords = 12;
-        }
-        // Check strength is a multiple of 32, if not round it down
-        if (numWords % 3 != 0) {
-            numWords = Math.floor(numWords / 3) * 3;
-            DOM.strength.val(numWords);
-        }
-        // Check strength is at least 32
-        if (numWords == 0) {
-            numWords = 3;
-            DOM.strength.val(numWords);
-        }
         var strength = numWords / 3 * 32;
         var words = mnemonic.generate(strength);
         DOM.phrase.val(words);
 
     function calcBip32Seed(phrase, passphrase, path) {
         var seed = mnemonic.toSeed(phrase, passphrase);
-        bip32RootKey = Bitcoin.HDNode.fromSeedHex(seed, network);
+        bip32RootKey = bitcoin.HDNode.fromSeedHex(seed, network);
         bip32ExtendedKey = bip32RootKey;
         // Derive the key from the path
         var pathBits = path.split("/");
     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<parts.length; i++) {
 
     function displayAddresses(start, total) {
         for (var i=0; i<total; i++) {
-            var index = i+ start;
-            var key = bip32ExtendedKey.derive(index);
-            var address = key.getAddress().toString();
-            var privkey = key.privKey.toWIF();
-            addAddressToList(index, address, privkey);
+            var index = i + start;
+            new TableRow(index);
         }
     }
 
+    function TableRow(index) {
+
+        function init() {
+            calculateValues();
+        }
+
+        function calculateValues() {
+            setTimeout(function() {
+                var key = bip32ExtendedKey.derive(index);
+                var address = key.getAddress().toString();
+                var privkey = key.privKey.toWIF(network);
+                addAddressToList(index, address, privkey);
+            }, 50)
+        }
+
+        init();
+
+    }
+
     function showMore() {
         var start = DOM.addresses.children().length;
         var rowsToAdd = parseInt(DOM.rowsToAdd.val());
                 return;
             }
         }
-        showPending();
-        setTimeout(function() {
         displayAddresses(start, rowsToAdd);
-        hidePending();
-        }, 50);
     }
 
     function clearDisplay() {
 
     function addAddressToList(index, address, privkey) {
         var row = $(addressRowTemplate.html());
-        row.find(".index span").text(index);
-        row.find(".address span").text(address);
-        row.find(".privkey span").text(privkey);
+        // Elements
+        var indexCell = row.find(".index span");
+        var addressCell = row.find(".address span");
+        var privkeyCell = row.find(".privkey span");
+        // Content
+        var indexText = derivationPath + "/" + index;
+        indexCell.text(indexText);
+        addressCell.text(address);
+        privkeyCell.text(privkey);
+        // Visibility
+        if (!showIndex) {
+            indexCell.addClass("invisible");
+        }
+        if (!showAddress) {
+            addressCell.addClass("invisible");
+        }
+        if (!showPrivKey) {
+            privkeyCell.addClass("invisible");
+        }
         DOM.addresses.append(row);
     }
 
         path += account + "'/";
         path += change;
         DOM.bip44path.val(path);
+        derivationPath = DOM.bip44path.val();
     }
 
     function parseIntNoNaN(val, defaultVal) {
             .hide();
     }
 
+    function populateNetworkSelect() {
+        for (var i=0; i<networks.length; i++) {
+            var network = networks[i];
+            var option = $("<option>");
+            option.attr("value", i);
+            option.text(network.name);
+            DOM.phraseNetwork.append(option);
+        }
+    }
+
+    var networks = [
+        {
+            name: "Bitcoin",
+            onSelect: function() {
+                network = bitcoin.networks.bitcoin;
+                DOM.bip44coin.val(0);
+            },
+        },
+        {
+            name: "Bitcoin Testnet",
+            onSelect: function() {
+                network = bitcoin.networks.testnet;
+                DOM.bip44coin.val(1);
+            },
+        },
+        {
+            name: "Litecoin",
+            onSelect: function() {
+                network = bitcoin.networks.litecoin;
+                DOM.bip44coin.val(2);
+            },
+        },
+        {
+            name: "Dogecoin",
+            onSelect: function() {
+                network = bitcoin.networks.dogecoin;
+                DOM.bip44coin.val(3);
+            },
+        },
+        {
+            name: "ShadowCash",
+            onSelect: function() {
+                network = bitcoin.networks.shadow;
+                DOM.bip44coin.val(35);
+            },
+        },
+        {
+            name: "ShadowCash Testnet",
+            onSelect: function() {
+                network = bitcoin.networks.shadowtn;
+                DOM.bip44coin.val(1);
+            },
+        },
+        {
+            name: "Viacoin",
+            onSelect: function() {
+                network = bitcoin.networks.viacoin;
+                DOM.bip44coin.val(14);
+            },
+        },
+        {
+            name: "Viacoin Testnet",
+            onSelect: function() {
+                network = bitcoin.networks.viacointestnet;
+                DOM.bip44coin.val(1);
+            },
+        },
+        {
+            name: "Jumbucks",
+            onSelect: function() {
+                network = bitcoin.networks.jumbucks;
+                DOM.bip44coin.val(26);
+            },
+        },
+        {
+            name: "CLAM",
+            onSelect: function() {
+                network = bitcoin.networks.clam;
+                DOM.bip44coin.val(23);
+            },
+        },
+    ]
+
     init();
 
 })();