X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2Fjs%2Findex.js;h=68f1cdaa17e0b6213d057db7184aa263e8fa3f29;hb=146e089e28fbda936955ce74fc1f2214418633c5;hp=f8b67616c530af1ae84c873a7302f6d59781ac20;hpb=a8c454871dafb83d47f65367ef46e64e4e47589f;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FBIP39.git diff --git a/src/js/index.js b/src/js/index.js index f8b6761..68f1cda 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -1,20 +1,33 @@ (function() { var mnemonic = new Mnemonic("english"); + var seed = null 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 rootKeyChangedTimeoutEvent = null; var DOM = {}; + DOM.network = $(".network"); + DOM.phraseNetwork = $("#network-phrase"); DOM.phrase = $(".phrase"); DOM.passphrase = $(".passphrase"); DOM.generate = $(".generate"); + DOM.seed = $(".seed"); 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"); @@ -22,6 +35,7 @@ DOM.bip44account = $("#bip44 .account"); DOM.bip44change = $("#bip44 .change"); DOM.strength = $(".strength"); + DOM.hardenedAddresses = $(".hardened-addresses"); DOM.addresses = $(".addresses"); DOM.rowsToAdd = $(".rows-to-add"); DOM.more = $(".more"); @@ -31,30 +45,38 @@ DOM.addressToggle = $(".address-toggle"); DOM.privateKeyToggle = $(".private-key-toggle"); - var derivationPath = DOM.bip44path.val(); - function init() { // Events + DOM.network.on("change", networkChanged); DOM.phrase.on("input", delayedPhraseChanged); DOM.passphrase.on("input", delayedPhraseChanged); DOM.generate.on("click", generateClicked); DOM.more.on("click", showMore); - DOM.bip32path.on("input", bip32Changed); - DOM.bip44purpose.on("input", bip44Changed); - DOM.bip44coin.on("input", bip44Changed); - DOM.bip44account.on("input", bip44Changed); - DOM.bip44change.on("input", bip44Changed); - DOM.tab.on("click", tabClicked); + DOM.rootKey.on("input", delayedRootKeyChanged); + DOM.bip32path.on("input", calcForDerivationPath); + DOM.bip44purpose.on("input", calcForDerivationPath); + DOM.bip44coin.on("input", calcForDerivationPath); + DOM.bip44account.on("input", calcForDerivationPath); + DOM.bip44change.on("input", calcForDerivationPath); + DOM.tab.on("shown.bs.tab", calcForDerivationPath); + DOM.hardenedAddresses.on("change", calcForDerivationPath); DOM.indexToggle.on("click", toggleIndexes); DOM.addressToggle.on("click", toggleAddresses); DOM.privateKeyToggle.on("click", togglePrivateKeys); disableForms(); hidePending(); hideValidationError(); + populateNetworkSelect(); } // Event handlers + function networkChanged(e) { + var network = e.target.value; + networks[network].onSelect(); + displayBip32Info(); + } + function delayedPhraseChanged() { hideValidationError(); showPending(); @@ -69,20 +91,65 @@ hideValidationError(); // Get the mnemonic phrase var phrase = DOM.phrase.val(); - var passphrase = DOM.passphrase.val(); var errorText = findPhraseErrors(phrase); if (errorText) { showValidationError(errorText); return; } - // Get the derivation path - var errorText = findDerivationPathErrors(); + // Calculate and display + var passphrase = DOM.passphrase.val(); + calcBip32RootKeyFromSeed(phrase, passphrase); + calcForDerivationPath(); + hidePending(); + } + + function delayedRootKeyChanged() { + // Warn if there is an existing mnemonic or passphrase. + if (DOM.phrase.val().length > 0 || DOM.passphrase.val().length > 0) { + if (!confirm("This will clear existing mnemonic and passphrase")) { + DOM.rootKey.val(bip32RootKey); + return + } + } + hideValidationError(); + showPending(); + // Clear existing mnemonic and passphrase + DOM.phrase.val(""); + DOM.passphrase.val(""); + seed = null; + if (rootKeyChangedTimeoutEvent != null) { + clearTimeout(rootKeyChangedTimeoutEvent); + } + rootKeyChangedTimeoutEvent = setTimeout(rootKeyChanged, 400); + } + + function rootKeyChanged() { + showPending(); + hideValidationError(); + // Validate the root key TODO + var rootKeyBase58 = DOM.rootKey.val(); + var errorText = validateRootKey(rootKeyBase58); if (errorText) { showValidationError(errorText); return; } // Calculate and display - calcBip32Seed(phrase, passphrase, derivationPath); + calcBip32RootKeyFromBase58(rootKeyBase58); + calcForDerivationPath(); + hidePending(); + } + + function calcForDerivationPath() { + showPending(); + hideValidationError(); + // Get the derivation path + var derivationPath = getDerivationPath(); + var errorText = findDerivationPathErrors(derivationPath); + if (errorText) { + showValidationError(errorText); + return; + } + calcBip32ExtendedKey(derivationPath); displayBip32Info(); hidePending(); } @@ -99,36 +166,18 @@ }, 50); } - function tabClicked(e) { - var activePath = $(e.target.getAttribute("href") + " .path"); - derivationPath = activePath.val(); - derivationChanged(); - } - - function derivationChanged() { - delayedPhraseChanged(); - } - - function bip32Changed() { - derivationPath = DOM.bip32path.val(); - derivationChanged(); - } - - 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"); } @@ -141,30 +190,22 @@ 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); return words; } - function calcBip32Seed(phrase, passphrase, path) { - var seed = mnemonic.toSeed(phrase, passphrase); - bip32RootKey = Bitcoin.HDNode.fromSeedHex(seed, network); + function calcBip32RootKeyFromSeed(phrase, passphrase) { + seed = mnemonic.toSeed(phrase, passphrase); + bip32RootKey = bitcoin.HDNode.fromSeedHex(seed, network); + } + + function calcBip32RootKeyFromBase58(rootKeyBase58) { + bip32RootKey = bitcoin.HDNode.fromBase58(rootKeyBase58); + } + + function calcBip32ExtendedKey(path) { bip32ExtendedKey = bip32RootKey; // Derive the key from the path var pathBits = path.split("/"); @@ -209,8 +250,16 @@ proper.push(part.toLowerCase()); } } - // TODO some levenstein on the words var properPhrase = proper.join(' '); + // Check each word + for (var i=0; i 1) { + if (path[1] != "/") { + return "Separator must be '/'"; + } + var indexes = path.split("/"); + if (indexes.length > maxDepth) { + return "Derivation depth is " + indexes.length + ", must be less than " + maxDepth; + } + for (var depth = 1; depth 0) { + return "Invalid characters " + invalidChars + " found at depth " + depth; + } + var indexValue = parseInt(index.replace("'", "")); + if (isNaN(depth)) { + return "Invalid number at depth " + depth; + } + if (indexValue > maxIndexValue) { + return "Value of " + indexValue + " at depth " + depth + " must be less than " + maxIndexValue; + } + } + } return false; } function displayBip32Info() { // Display the key + DOM.seed.val(seed); var rootKey = bip32RootKey.toBase58(); DOM.rootKey.val(rootKey); var extendedPrivKey = bip32ExtendedKey.toBase58(); @@ -246,16 +364,28 @@ function TableRow(index) { + var useHardenedAddresses = DOM.hardenedAddresses.prop("checked"); + function init() { calculateValues(); } function calculateValues() { setTimeout(function() { - var key = bip32ExtendedKey.derive(index); + var key = ""; + if (useHardenedAddresses) { + key = bip32ExtendedKey.deriveHardened(index); + } + else { + key = bip32ExtendedKey.derive(index); + } var address = key.getAddress().toString(); var privkey = key.privKey.toWIF(network); - addAddressToList(index, address, privkey); + var indexText = getDerivationPath() + "/" + index; + if (useHardenedAddresses) { + indexText = indexText + "'"; + } + addAddressToList(indexText, address, privkey); }, 50) } @@ -296,11 +426,26 @@ DOM.extendedPubKey.val(""); } - function addAddressToList(index, address, privkey) { + function addAddressToList(indexText, 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 + 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); } @@ -314,19 +459,6 @@ }); } - function setBip44DerivationPath() { - var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44); - var coin = parseIntNoNaN(DOM.bip44coin.val(), 0); - var account = parseIntNoNaN(DOM.bip44account.val(), 0); - var change = parseIntNoNaN(DOM.bip44change.val(), 0); - var path = "m/"; - path += purpose + "'/"; - path += coin + "'/"; - path += account + "'/"; - path += change; - DOM.bip44path.val(path); - } - function parseIntNoNaN(val, defaultVal) { var v = parseInt(val); if (isNaN(v)) { @@ -341,12 +473,110 @@ .show(); } + function findNearestWord(word) { + var words = WORDLISTS["english"]; + var minDistance = 99; + var closestWord = words[0]; + for (var i=0; i"); + 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(); })();