X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2Fjs%2Findex.js;h=0e4cc052a4d704fa749bf9076c9bc68fc5145530;hb=d737abf6809622228faf7d5fe54101e2d87d72a4;hp=f3582b0f17461de4de8ce9d8c37038be0ea2bcc0;hpb=563e401a4f3880da88bfc69cf51be5a1becd4c66;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FBIP39.git diff --git a/src/js/index.js b/src/js/index.js index f3582b0..0e4cc05 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -1,6 +1,8 @@ (function() { - var mnemonic = new Mnemonic("english"); + // mnemonics is populated as required by getLanguage + var mnemonics = { "english": new Mnemonic("english") }; + var mnemonic = mnemonics["english"]; var seed = null var bip32RootKey = null; var bip32ExtendedKey = null; @@ -9,9 +11,11 @@ var showIndex = true; var showAddress = true; + var showPubKey = true; var showPrivKey = true; var phraseChangeTimeoutEvent = null; + var rootKeyChangedTimeoutEvent = null; var DOM = {}; DOM.network = $(".network"); @@ -34,6 +38,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"); @@ -41,7 +46,9 @@ DOM.tab = $(".derivation-type a"); DOM.indexToggle = $(".index-toggle"); DOM.addressToggle = $(".address-toggle"); + DOM.publicKeyToggle = $(".public-key-toggle"); DOM.privateKeyToggle = $(".private-key-toggle"); + DOM.languages = $(".languages a"); function init() { // Events @@ -50,15 +57,19 @@ DOM.passphrase.on("input", delayedPhraseChanged); DOM.generate.on("click", generateClicked); DOM.more.on("click", showMore); - DOM.bip32path.on("input", delayedPhraseChanged); - DOM.bip44purpose.on("input", delayedPhraseChanged); - DOM.bip44coin.on("input", delayedPhraseChanged); - DOM.bip44account.on("input", delayedPhraseChanged); - DOM.bip44change.on("input", delayedPhraseChanged); - DOM.tab.on("click", delayedPhraseChanged); + 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.publicKeyToggle.on("click", togglePublicKeys); DOM.privateKeyToggle.on("click", togglePrivateKeys); + DOM.languages.on("click", languageChanged); disableForms(); hidePending(); hideValidationError(); @@ -68,9 +79,14 @@ // Event handlers function networkChanged(e) { - var network = e.target.value; - networks[network].onSelect(); - delayedPhraseChanged(); + var networkIndex = e.target.value; + networks[networkIndex].onSelect(); + if (seed != null) { + phraseChanged(); + } + else { + rootKeyChanged(); + } } function delayedPhraseChanged() { @@ -85,14 +101,60 @@ function phraseChanged() { showPending(); hideValidationError(); + setMnemonicLanguage(); // Get the mnemonic phrase var phrase = DOM.phrase.val(); - var passphrase = DOM.passphrase.val(); var errorText = findPhraseErrors(phrase); if (errorText) { showValidationError(errorText); return; } + // 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 + calcBip32RootKeyFromBase58(rootKeyBase58); + calcForDerivationPath(); + hidePending(); + } + + function calcForDerivationPath() { + showPending(); + hideValidationError(); // Get the derivation path var derivationPath = getDerivationPath(); var errorText = findDerivationPathErrors(derivationPath); @@ -100,8 +162,7 @@ showValidationError(errorText); return; } - // Calculate and display - calcBip32Seed(phrase, passphrase, derivationPath); + calcBip32ExtendedKey(derivationPath); displayBip32Info(); hidePending(); } @@ -110,6 +171,7 @@ clearDisplay(); showPending(); setTimeout(function() { + setMnemonicLanguage(); var phrase = generateRandomPhrase(); if (!phrase) { return; @@ -118,6 +180,20 @@ }, 50); } + function languageChanged() { + setTimeout(function() { + setMnemonicLanguage(); + if (DOM.phrase.val().length > 0) { + var newPhrase = convertPhraseToNewLanguage(); + DOM.phrase.val(newPhrase); + phraseChanged(); + } + else { + DOM.generate.trigger("click"); + } + }, 50); + } + function toggleIndexes() { showIndex = !showIndex; $("td.index span").toggleClass("invisible"); @@ -128,6 +204,11 @@ $("td.address span").toggleClass("invisible"); } + function togglePublicKeys() { + showPubKey = !showPubKey; + $("td.pubkey span").toggleClass("invisible"); + } + function togglePrivateKeys() { showPrivKey = !showPrivKey; $("td.privkey span").toggleClass("invisible"); @@ -148,9 +229,16 @@ return words; } - function calcBip32Seed(phrase, passphrase, path) { + function calcBip32RootKeyFromSeed(phrase, passphrase) { seed = mnemonic.toSeed(phrase, passphrase); bip32RootKey = bitcoin.HDNode.fromSeedHex(seed, network); + } + + function calcBip32RootKeyFromBase58(rootKeyBase58) { + bip32RootKey = bitcoin.HDNode.fromBase58(rootKeyBase58, network); + } + + function calcBip32ExtendedKey(path) { bip32ExtendedKey = bip32RootKey; // Derive the key from the path var pathBits = path.split("/"); @@ -186,26 +274,19 @@ // TODO make this right // Preprocess the words phrase = mnemonic.normalizeString(phrase); - var parts = phrase.split(" "); - var proper = []; - for (var i=0; i 0) { - // TODO check that lowercasing is always valid to do - proper.push(part.toLowerCase()); - } - } - var properPhrase = proper.join(' '); + var words = phraseToWordArray(phrase); // Check each word - for (var i=0; i 0) { + var words = phraseToWordArray(phrase); + var languageMatches = {}; + for (l in WORDLISTS) { + // Track how many words match in this language + languageMatches[l] = 0; + for (var i=0; i -1; + if (wordInLanguage) { + languageMatches[l]++; + } + } + // Find languages with most word matches. + // This is made difficult due to commonalities between Chinese + // simplified vs traditional. + var mostMatches = 0; + var mostMatchedLanguages = []; + for (var l in languageMatches) { + var numMatches = languageMatches[l]; + if (numMatches > mostMatches) { + mostMatches = numMatches; + mostMatchedLanguages = [l]; + } + else if (numMatches == mostMatches) { + mostMatchedLanguages.push(l); + } + } + } + if (mostMatchedLanguages.length > 0) { + // Use first language and warn if multiple detected + language = mostMatchedLanguages[0]; + if (mostMatchedLanguages.length > 1) { + console.warn("Multiple possible languages"); + console.warn(mostMatchedLanguages); + } + } + } + return language; + } + + function getLanguageFromUrl() { + return window.location.hash.substring(1); + } + + function setMnemonicLanguage() { + var language = getLanguage(); + // Load the bip39 mnemonic generator for this language if required + if (!(language in mnemonics)) { + mnemonics[language] = new Mnemonic(language); + } + mnemonic = mnemonics[language]; + } + + function convertPhraseToNewLanguage() { + var oldLanguage = getLanguageFromPhrase(); + var newLanguage = getLanguageFromUrl(); + var oldPhrase = DOM.phrase.val(); + var oldWords = phraseToWordArray(oldPhrase); + var newWords = []; + for (var i=0; i 0) { + noBlanks.push(word); + } + } + return noBlanks; + } + + // TODO look at jsbip39 - mnemonic.joinWords + function wordArrayToPhrase(words) { + var phrase = words.join(" "); + var language = getLanguageFromPhrase(phrase); + if (language == "japanese") { + phrase = words.join("\u3000"); + } + return phrase; + } + var networks = [ { name: "Bitcoin", @@ -499,6 +721,27 @@ DOM.bip44coin.val(23); }, }, + { + name: "DASH", + onSelect: function() { + network = bitcoin.networks.dash; + DOM.bip44coin.val(5); + }, + }, + { + name: "Namecoin", + onSelect: function() { + network = bitcoin.networks.namecoin; + DOM.bip44coin.val(7); + }, + }, + { + name: "Peercoin", + onSelect: function() { + network = bitcoin.networks.peercoin; + DOM.bip44coin.val(6); + }, + }, ] init();