X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FBIP39.git;a=blobdiff_plain;f=src%2Fjs%2Findex.js;h=535cac690658da9e52ab59477da574b53f8d1173;hp=9b8432a4d64686dfbf45cdaf6f8d2ed275c85bd6;hb=e2186682ac0cfa1f301c226408cdfb438f7b4d6e;hpb=a0091a40c46d1ece61193ee84c0a1b7de771e447 diff --git a/src/js/index.js b/src/js/index.js index 9b8432a..9578146 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -14,6 +14,7 @@ var showPubKey = true; var showPrivKey = true; var showQr = false; + var litecoinUseLtub = true; var entropyChangeTimeoutEvent = null; var phraseChangeTimeoutEvent = null; @@ -22,6 +23,7 @@ var generationProcesses = []; var DOM = {}; + DOM.privacyScreenToggle = $(".privacy-screen-toggle"); DOM.network = $(".network"); DOM.bip32Client = $("#bip32-client"); DOM.phraseNetwork = $("#network-phrase"); @@ -36,19 +38,29 @@ DOM.entropyBitsPerEvent = DOM.entropyContainer.find(".bits-per-event"); DOM.entropyWordCount = DOM.entropyContainer.find(".word-count"); DOM.entropyBinary = DOM.entropyContainer.find(".binary"); + DOM.entropyWordIndexes = DOM.entropyContainer.find(".word-indexes"); + DOM.entropyChecksum = DOM.entropyContainer.find(".checksum"); DOM.entropyMnemonicLength = DOM.entropyContainer.find(".mnemonic-length"); + DOM.entropyWeakEntropyOverrideWarning = DOM.entropyContainer.find(".weak-entropy-override-warning"); + DOM.entropyFilterWarning = DOM.entropyContainer.find(".filter-warning"); DOM.phrase = $(".phrase"); DOM.passphrase = $(".passphrase"); DOM.generateContainer = $(".generate-container"); DOM.generate = $(".generate"); DOM.seed = $(".seed"); DOM.rootKey = $(".root-key"); + DOM.litecoinLtubContainer = $(".litecoin-ltub-container"); + DOM.litecoinUseLtub = $(".litecoin-use-ltub"); DOM.extendedPrivKey = $(".extended-priv-key"); DOM.extendedPubKey = $(".extended-pub-key"); DOM.bip32tab = $("#bip32-tab"); DOM.bip44tab = $("#bip44-tab"); + DOM.bip49tab = $("#bip49-tab"); + DOM.bip84tab = $("#bip84-tab"); + DOM.bip141tab = $("#bip141-tab"); DOM.bip32panel = $("#bip32"); DOM.bip44panel = $("#bip44"); + DOM.bip49panel = $("#bip49"); DOM.bip32path = $("#bip32-path"); DOM.bip44path = $("#bip44-path"); DOM.bip44purpose = $("#bip44 .purpose"); @@ -57,11 +69,41 @@ DOM.bip44accountXprv = $("#bip44 .account-xprv"); DOM.bip44accountXpub = $("#bip44 .account-xpub"); DOM.bip44change = $("#bip44 .change"); + DOM.bip49unavailable = $("#bip49 .unavailable"); + DOM.bip49available = $("#bip49 .available"); + DOM.bip49path = $("#bip49-path"); + DOM.bip49purpose = $("#bip49 .purpose"); + DOM.bip49coin = $("#bip49 .coin"); + DOM.bip49account = $("#bip49 .account"); + DOM.bip49accountXprv = $("#bip49 .account-xprv"); + DOM.bip49accountXpub = $("#bip49 .account-xpub"); + DOM.bip49change = $("#bip49 .change"); + DOM.bip84unavailable = $("#bip84 .unavailable"); + DOM.bip84available = $("#bip84 .available"); + DOM.bip84path = $("#bip84-path"); + DOM.bip84purpose = $("#bip84 .purpose"); + DOM.bip84coin = $("#bip84 .coin"); + DOM.bip84account = $("#bip84 .account"); + DOM.bip84accountXprv = $("#bip84 .account-xprv"); + DOM.bip84accountXpub = $("#bip84 .account-xpub"); + DOM.bip84change = $("#bip84 .change"); + DOM.bip141unavailable = $("#bip141 .unavailable"); + DOM.bip141available = $("#bip141 .available"); + DOM.bip141path = $("#bip141-path"); + DOM.bip141semantics = $(".bip141-semantics"); DOM.generatedStrength = $(".generate-container .strength"); + DOM.generatedStrengthWarning = $(".generate-container .warning"); DOM.hardenedAddresses = $(".hardened-addresses"); + DOM.bitcoinCashAddressTypeContainer = $(".bch-addr-type-container"); + DOM.bitcoinCashAddressType = $("[name=bch-addr-type]") + DOM.useBip38 = $(".use-bip38"); + DOM.bip38Password = $(".bip38-password"); DOM.addresses = $(".addresses"); + DOM.csvTab = $("#csv-tab a"); + DOM.csv = $(".csv"); DOM.rowsToAdd = $(".rows-to-add"); DOM.more = $(".more"); + DOM.moreRowsStartIndex = $(".more-rows-start-index"); DOM.feedback = $(".feedback"); DOM.tab = $(".derivation-type a"); DOM.indexToggle = $(".index-toggle"); @@ -77,6 +119,8 @@ function init() { // Events + DOM.privacyScreenToggle.on("change", privacyScreenToggled); + DOM.generatedStrength.on("change", generatedStrengthChanged); DOM.network.on("change", networkChanged); DOM.bip32Client.on("change", bip32ClientChanged); DOM.useEntropy.on("change", setEntropyVisibility); @@ -87,18 +131,27 @@ DOM.generate.on("click", generateClicked); DOM.more.on("click", showMore); DOM.rootKey.on("input", delayedRootKeyChanged); + DOM.litecoinUseLtub.on("change", litecoinUseLtubChanged); 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.bip49account.on("input", calcForDerivationPath); + DOM.bip49change.on("input", calcForDerivationPath); + DOM.bip84account.on("input", calcForDerivationPath); + DOM.bip84change.on("input", calcForDerivationPath); + DOM.bip141path.on("input", calcForDerivationPath); + DOM.bip141semantics.on("change", tabChanged); + DOM.tab.on("shown.bs.tab", tabChanged); DOM.hardenedAddresses.on("change", calcForDerivationPath); + DOM.useBip38.on("change", calcForDerivationPath); + DOM.bip38Password.on("change", calcForDerivationPath); DOM.indexToggle.on("click", toggleIndexes); DOM.addressToggle.on("click", toggleAddresses); DOM.publicKeyToggle.on("click", togglePublicKeys); DOM.privateKeyToggle.on("click", togglePrivateKeys); + DOM.csvTab.on("click", updateCsv); DOM.languages.on("click", languageChanged); + DOM.bitcoinCashAddressType.on("change", bitcoinCashAddressTypeChange); setQrEvents(DOM.showQrEls); disableForms(); hidePending(); @@ -109,9 +162,25 @@ // Event handlers + function generatedStrengthChanged() { + var strength = parseInt(DOM.generatedStrength.val()); + if (strength < 12) { + DOM.generatedStrengthWarning.removeClass("hidden"); + } + else { + DOM.generatedStrengthWarning.addClass("hidden"); + } + } + function networkChanged(e) { + clearDerivedKeys(); + clearAddressesList(); + DOM.litecoinLtubContainer.addClass("hidden"); + DOM.bitcoinCashAddressTypeContainer.addClass("hidden"); var networkIndex = e.target.value; - networks[networkIndex].onSelect(); + var network = networks[networkIndex]; + network.onSelect(); + adjustNetworkForSegwit(); if (seed != null) { phraseChanged(); } @@ -155,6 +224,10 @@ function delayedPhraseChanged() { hideValidationError(); + seed = null; + bip32RootKey = null; + bip32ExtendedKey = null; + clearAddressesList(); showPending(); if (phraseChangeTimeoutEvent != null) { clearTimeout(phraseChangeTimeoutEvent); @@ -164,7 +237,6 @@ function phraseChanged() { showPending(); - hideValidationError(); setMnemonicLanguage(); // Get the mnemonic phrase var phrase = DOM.phrase.val(); @@ -177,7 +249,37 @@ var passphrase = DOM.passphrase.val(); calcBip32RootKeyFromSeed(phrase, passphrase); calcForDerivationPath(); - hidePending(); + // Show the word indexes + showWordIndexes(); + } + + function tabChanged() { + showPending(); + adjustNetworkForSegwit(); + var phrase = DOM.phrase.val(); + if (phrase != "") { + // Calculate and display for mnemonic + var errorText = findPhraseErrors(phrase); + if (errorText) { + showValidationError(errorText); + return; + } + // Calculate and display + var passphrase = DOM.passphrase.val(); + calcBip32RootKeyFromSeed(phrase, passphrase); + } + else { + // Calculate and display for root key + var rootKeyBase58 = DOM.rootKey.val(); + var errorText = validateRootKey(rootKeyBase58); + if (errorText) { + showValidationError(errorText); + return; + } + // Calculate and display + calcBip32RootKeyFromBase58(rootKeyBase58); + } + calcForDerivationPath(); } function delayedEntropyChanged() { @@ -240,7 +342,6 @@ function rootKeyChanged() { showPending(); hideValidationError(); - // Validate the root key TODO var rootKeyBase58 = DOM.rootKey.val(); var errorText = validateRootKey(rootKeyBase58); if (errorText) { @@ -252,10 +353,28 @@ calcForDerivationPath(); } + function litecoinUseLtubChanged() { + litecoinUseLtub = DOM.litecoinUseLtub.prop("checked"); + if (litecoinUseLtub) { + network = bitcoinjs.bitcoin.networks.litecoin; + } + else { + network = bitcoinjs.bitcoin.networks.litecoinXprv; + } + phraseChanged(); + } + function calcForDerivationPath() { - showPending(); + clearDerivedKeys(); clearAddressesList(); - hideValidationError(); + showPending(); + // Don't show segwit if it's selected but network doesn't support it + if (segwitSelected() && !networkHasSegwit()) { + showSegwitUnavailable(); + hidePending(); + return; + } + showSegwitAvailable(); // Get the derivation path var derivationPath = getDerivationPath(); var errorText = findDerivationPathErrors(derivationPath); @@ -267,8 +386,13 @@ if (bip44TabSelected()) { displayBip44Info(); } + else if (bip49TabSelected()) { + displayBip49Info(); + } + else if (bip84TabSelected()) { + displayBip84Info(); + } displayBip32Info(); - hidePending(); } function generateClicked() { @@ -301,6 +425,10 @@ }, 50); } + function bitcoinCashAddressTypeChange() { + phraseChanged(); + } + function toggleIndexes() { showIndex = !showIndex; $("td.index span").toggleClass("invisible"); @@ -321,6 +449,17 @@ $("td.privkey span").toggleClass("invisible"); } + function privacyScreenToggled() { + // private-data contains elements added to DOM at runtime + // so catch all by adding visual privacy class to the root of the DOM + if (DOM.privacyScreenToggle.prop("checked")) { + $("body").addClass("visual-privacy"); + } + else { + $("body").removeClass("visual-privacy"); + } + } + // Private methods function generateRandomPhrase() { @@ -329,10 +468,20 @@ showValidationError(errorText); return; } + // get the amount of entropy to use var numWords = parseInt(DOM.generatedStrength.val()); var strength = numWords / 3 * 32; - var words = mnemonic.generate(strength); + var buffer = new Uint8Array(strength / 8); + // create secure entropy + var data = crypto.getRandomValues(buffer); + // show the words + var words = mnemonic.toMnemonic(data); DOM.phrase.val(words); + // show the entropy + var entropyHex = uint8ArrayToHex(data); + DOM.entropy.val(entropyHex); + // ensure entropy fields are consistent with what is being displayed + DOM.entropyMnemonicLength.val("raw"); return words; } @@ -342,6 +491,37 @@ } function calcBip32RootKeyFromBase58(rootKeyBase58) { + // try parsing with various segwit network params since this extended + // key may be from any one of them. + if (networkHasSegwit()) { + var n = network; + if ("baseNetwork" in n) { + n = bitcoinjs.bitcoin.networks[n.baseNetwork]; + } + // try parsing using base network params + try { + bip32RootKey = bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n); + return; + } + catch (e) {} + // try parsing using p2wpkh params + if ("p2wpkh" in n) { + try { + bip32RootKey = bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n.p2wpkh); + return; + } + catch (e) {} + } + // try parsing using p2wpkh-in-p2sh network params + if ("p2wpkhInP2sh" in n) { + try { + bip32RootKey = bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n.p2wpkhInP2sh); + return; + } + catch (e) {} + } + } + // try the network params as currently specified bip32RootKey = bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, network); } @@ -415,8 +595,39 @@ } function validateRootKey(rootKeyBase58) { + // try various segwit network params since this extended key may be from + // any one of them. + if (networkHasSegwit()) { + var n = network; + if ("baseNetwork" in n) { + n = bitcoinjs.bitcoin.networks[n.baseNetwork]; + } + // try parsing using base network params + try { + bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n); + return ""; + } + catch (e) {} + // try parsing using p2wpkh params + if ("p2wpkh" in n) { + try { + bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n.p2wpkh); + return ""; + } + catch (e) {} + } + // try parsing using p2wpkh-in-p2sh network params + if ("p2wpkhInP2sh" in n) { + try { + bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, n.p2wpkhInP2sh); + return ""; + } + catch (e) {} + } + } + // try the network params as currently specified try { - bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58); + bitcoinjs.bitcoin.HDNode.fromBase58(rootKeyBase58, network); } catch (e) { return "Invalid root key"; @@ -440,11 +651,46 @@ console.log("Using derivation path from BIP44 tab: " + derivationPath); return derivationPath; } + else if (bip49TabSelected()) { + var purpose = parseIntNoNaN(DOM.bip49purpose.val(), 49); + var coin = parseIntNoNaN(DOM.bip49coin.val(), 0); + var account = parseIntNoNaN(DOM.bip49account.val(), 0); + var change = parseIntNoNaN(DOM.bip49change.val(), 0); + var path = "m/"; + path += purpose + "'/"; + path += coin + "'/"; + path += account + "'/"; + path += change; + DOM.bip49path.val(path); + var derivationPath = DOM.bip49path.val(); + console.log("Using derivation path from BIP49 tab: " + derivationPath); + return derivationPath; + } + else if (bip84TabSelected()) { + var purpose = parseIntNoNaN(DOM.bip84purpose.val(), 84); + var coin = parseIntNoNaN(DOM.bip84coin.val(), 0); + var account = parseIntNoNaN(DOM.bip84account.val(), 0); + var change = parseIntNoNaN(DOM.bip84change.val(), 0); + var path = "m/"; + path += purpose + "'/"; + path += coin + "'/"; + path += account + "'/"; + path += change; + DOM.bip84path.val(path); + var derivationPath = DOM.bip84path.val(); + console.log("Using derivation path from BIP84 tab: " + derivationPath); + return derivationPath; + } else if (bip32TabSelected()) { var derivationPath = DOM.bip32path.val(); console.log("Using derivation path from BIP32 tab: " + derivationPath); return derivationPath; } + else if (bip141TabSelected()) { + var derivationPath = DOM.bip141path.val(); + console.log("Using derivation path from BIP141 tab: " + derivationPath); + return derivationPath; + } else { console.log("Unknown derivation path"); } @@ -489,7 +735,9 @@ return "No root key"; } // Check no hardened derivation path when using xpub keys - var hardened = path.indexOf("'") > -1; + var hardenedPath = path.indexOf("'") > -1; + var hardenedAddresses = bip32TabSelected() && DOM.hardenedAddresses.prop("checked"); + var hardened = hardenedPath || hardenedAddresses; var isXpubkey = bip32RootKey.isNeutered(); if (hardened && isXpubkey) { return "Hardened derivation path is invalid with xpub key"; @@ -515,6 +763,42 @@ DOM.bip44accountXpub.val(accountXpub); } + function displayBip49Info() { + // Get the derivation path for the account + var purpose = parseIntNoNaN(DOM.bip49purpose.val(), 49); + var coin = parseIntNoNaN(DOM.bip49coin.val(), 0); + var account = parseIntNoNaN(DOM.bip49account.val(), 0); + var path = "m/"; + path += purpose + "'/"; + path += coin + "'/"; + path += account + "'/"; + // Calculate the account extended keys + var accountExtendedKey = calcBip32ExtendedKey(path); + var accountXprv = accountExtendedKey.toBase58(); + var accountXpub = accountExtendedKey.neutered().toBase58(); + // Display the extended keys + DOM.bip49accountXprv.val(accountXprv); + DOM.bip49accountXpub.val(accountXpub); + } + + function displayBip84Info() { + // Get the derivation path for the account + var purpose = parseIntNoNaN(DOM.bip84purpose.val(), 84); + var coin = parseIntNoNaN(DOM.bip84coin.val(), 0); + var account = parseIntNoNaN(DOM.bip84account.val(), 0); + var path = "m/"; + path += purpose + "'/"; + path += coin + "'/"; + path += account + "'/"; + // Calculate the account extended keys + var accountExtendedKey = calcBip32ExtendedKey(path); + var accountXprv = accountExtendedKey.toBase58(); + var accountXpub = accountExtendedKey.neutered().toBase58(); + // Display the extended keys + DOM.bip84accountXprv.val(accountXprv); + DOM.bip84accountXpub.val(accountXpub); + } + function displayBip32Info() { // Display the key DOM.seed.val(seed); @@ -530,7 +814,8 @@ DOM.extendedPubKey.val(extendedPubKey); // Display the addresses and privkeys clearAddressesList(); - displayAddresses(0, 20); + var initialAddressCount = parseInt(DOM.rowsToAdd.val()); + displayAddresses(0, initialAddressCount); } function displayAddresses(start, total) { @@ -542,21 +827,43 @@ for (var i=0; i 200) { var msg = "Generating " + rowsToAdd + " rows could take a while. "; msg += "Do you want to continue?"; @@ -627,12 +1042,13 @@ function clearDisplay() { clearAddressesList(); - clearKey(); + clearKeys(); hideValidationError(); } function clearAddressesList() { DOM.addresses.empty(); + DOM.csv.val(""); stopGenerating(); } @@ -643,10 +1059,20 @@ } } - function clearKey() { + function clearKeys() { + clearRootKey(); + clearDerivedKeys(); + } + + function clearRootKey() { DOM.rootKey.val(""); + } + + function clearDerivedKeys() { DOM.extendedPrivKey.val(""); DOM.extendedPubKey.val(""); + DOM.bip44accountXprv.val(""); + DOM.bip44accountXpub.val(""); } function addAddressToList(indexText, address, pubkey, privkey) { @@ -734,6 +1160,9 @@ var option = $("