X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2Fjs%2Findex.js;h=1fc0586106a5dd3b36b62478f2ef9aefd9285995;hb=0a23f51792722f094328d695242556c4c0195a8b;hp=758b8402b3ddd8983e0eaf5089a6452d990bd3b6;hpb=584dbddd37c762244f17a423a31573436d6dde11;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FBIP39.git diff --git a/src/js/index.js b/src/js/index.js index 758b840..9807449 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -15,6 +15,7 @@ var showPrivKey = true; var showQr = false; var litecoinUseLtub = true; + var isDefaultBip44ChangeValue = true; var entropyChangeTimeoutEvent = null; var phraseChangeTimeoutEvent = null; @@ -23,6 +24,7 @@ var generationProcesses = []; var DOM = {}; + DOM.privacyScreenToggle = $(".privacy-screen-toggle"); DOM.network = $(".network"); DOM.bip32Client = $("#bip32-client"); DOM.phraseNetwork = $("#network-phrase"); @@ -43,6 +45,8 @@ DOM.entropyWeakEntropyOverrideWarning = DOM.entropyContainer.find(".weak-entropy-override-warning"); DOM.entropyFilterWarning = DOM.entropyContainer.find(".filter-warning"); DOM.phrase = $(".phrase"); + DOM.splitPhrase = $(".phraseSplit"); + DOM.phraseSplitWarn = $(".phraseSplitWarn"); DOM.passphrase = $(".passphrase"); DOM.generateContainer = $(".generate-container"); DOM.generate = $(".generate"); @@ -68,6 +72,7 @@ DOM.bip44accountXprv = $("#bip44 .account-xprv"); DOM.bip44accountXpub = $("#bip44 .account-xpub"); DOM.bip44change = $("#bip44 .change"); + DOM.defaultBip44ChangeValue = $("#bip44 .default-bip44-change-value"); DOM.bip49unavailable = $("#bip49 .unavailable"); DOM.bip49available = $("#bip49 .available"); DOM.bip49path = $("#bip49-path"); @@ -77,6 +82,8 @@ 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"); @@ -91,8 +98,8 @@ DOM.generatedStrength = $(".generate-container .strength"); DOM.generatedStrengthWarning = $(".generate-container .warning"); DOM.hardenedAddresses = $(".hardened-addresses"); - DOM.useBitpayAddressesContainer = $(".use-bitpay-addresses-container"); - DOM.useBitpayAddresses = $(".use-bitpay-addresses"); + DOM.bitcoinCashAddressTypeContainer = $(".bch-addr-type-container"); + DOM.bitcoinCashAddressType = $("[name=bch-addr-type]") DOM.useBip38 = $(".use-bip38"); DOM.bip38Password = $(".bip38-password"); DOM.addresses = $(".addresses"); @@ -116,6 +123,7 @@ function init() { // Events + DOM.privacyScreenToggle.on("change", privacyScreenToggled); DOM.generatedStrength.on("change", generatedStrengthChanged); DOM.network.on("change", networkChanged); DOM.bip32Client.on("change", bip32ClientChanged); @@ -130,7 +138,9 @@ DOM.litecoinUseLtub.on("change", litecoinUseLtubChanged); DOM.bip32path.on("input", calcForDerivationPath); DOM.bip44account.on("input", calcForDerivationPath); + DOM.bip44change.on("input", modifiedDefaultBip44ChangeValue); DOM.bip44change.on("input", calcForDerivationPath); + DOM.defaultBip44ChangeValue.on("click", resetDefaultBip44ChangeValue); DOM.bip49account.on("input", calcForDerivationPath); DOM.bip49change.on("input", calcForDerivationPath); DOM.bip84account.on("input", calcForDerivationPath); @@ -147,7 +157,7 @@ DOM.privateKeyToggle.on("click", togglePrivateKeys); DOM.csvTab.on("click", updateCsv); DOM.languages.on("click", languageChanged); - DOM.useBitpayAddresses.on("change", useBitpayAddressesChange); + DOM.bitcoinCashAddressType.on("change", bitcoinCashAddressTypeChange); setQrEvents(DOM.showQrEls); disableForms(); hidePending(); @@ -172,17 +182,11 @@ clearDerivedKeys(); clearAddressesList(); DOM.litecoinLtubContainer.addClass("hidden"); - DOM.useBitpayAddressesContainer.addClass("hidden"); + DOM.bitcoinCashAddressTypeContainer.addClass("hidden"); var networkIndex = e.target.value; var network = networks[networkIndex]; network.onSelect(); - if (network.segwitAvailable) { - adjustNetworkForSegwit(); - showSegwitAvailable(); - } - else { - showSegwitUnavailable(); - } + adjustNetworkForSegwit(); if (seed != null) { phraseChanged(); } @@ -234,7 +238,14 @@ if (phraseChangeTimeoutEvent != null) { clearTimeout(phraseChangeTimeoutEvent); } - phraseChangeTimeoutEvent = setTimeout(phraseChanged, 400); + phraseChangeTimeoutEvent = setTimeout(function() { + phraseChanged(); + var entropy = mnemonic.toRawEntropyHex(DOM.phrase.val()); + if (entropy !== null) { + DOM.entropyMnemonicLength.val("raw"); + DOM.entropy.val(entropy); + } + }, 400); } function phraseChanged() { @@ -299,6 +310,7 @@ clearDisplay(); clearEntropyFeedback(); DOM.phrase.val(""); + DOM.phraseSplit.val(""); showValidationError("Blank entropy"); return; } @@ -333,6 +345,7 @@ showPending(); // Clear existing mnemonic and passphrase DOM.phrase.val(""); + DOM.phraseSplit.val(""); DOM.passphrase.val(""); seed = null; if (rootKeyChangedTimeoutEvent != null) { @@ -372,8 +385,11 @@ 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); @@ -416,6 +432,7 @@ if (DOM.phrase.val().length > 0) { var newPhrase = convertPhraseToNewLanguage(); DOM.phrase.val(newPhrase); + writeSplitPhrase(newPhrase); phraseChanged(); } else { @@ -424,8 +441,7 @@ }, 50); } - function useBitpayAddressesChange() { - setBitcoinCashNetworkValues(); + function bitcoinCashAddressTypeChange() { phraseChanged(); } @@ -449,6 +465,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() { @@ -466,6 +493,7 @@ // show the words var words = mnemonic.toMnemonic(data); DOM.phrase.val(words); + writeSplitPhrase(words); // show the entropy var entropyHex = uint8ArrayToHex(data); DOM.entropy.val(entropyHex); @@ -477,12 +505,85 @@ function calcBip32RootKeyFromSeed(phrase, passphrase) { seed = mnemonic.toSeed(phrase, passphrase); bip32RootKey = bitcoinjs.bitcoin.HDNode.fromSeedHex(seed, network); + if(isGRS()) + bip32RootKey = groestlcoinjs.HDNode.fromSeedHex(seed, network); + } function calcBip32RootKeyFromBase58(rootKeyBase58) { + if(isGRS()) { + calcBip32RootKeyFromBase58GRS(rootKeyBase58); + return; + } + // 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); } + function calcBip32RootKeyFromBase58GRS(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 = groestlcoinjs.HDNode.fromBase58(rootKeyBase58, n); + return; + } + catch (e) {} + // try parsing using p2wpkh params + if ("p2wpkh" in n) { + try { + bip32RootKey = groestlcoinjs.HDNode.fromBase58(rootKeyBase58, n.p2wpkh); + return; + } + catch (e) {} + } + // try parsing using p2wpkh-in-p2sh network params + if ("p2wpkhInP2sh" in n) { + try { + bip32RootKey = groestlcoinjs.HDNode.fromBase58(rootKeyBase58, n.p2wpkhInP2sh); + return; + } + catch (e) {} + } + } + // try the network params as currently specified + bip32RootKey = groestlcoinjs.HDNode.fromBase58(rootKeyBase58, network); + } + function calcBip32ExtendedKey(path) { // Check there's a root key to derive from if (!bip32RootKey) { @@ -510,7 +611,7 @@ extendedKey = extendedKey.derive(index); } } - return extendedKey + return extendedKey; } function showValidationError(errorText) { @@ -553,6 +654,40 @@ } function validateRootKey(rootKeyBase58) { + if(isGRS()) + return validateRootKeyGRS(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, network); } @@ -562,17 +697,60 @@ return ""; } + function validateRootKeyGRS(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 { + groestlcoinjs.HDNode.fromBase58(rootKeyBase58, n); + return ""; + } + catch (e) {} + // try parsing using p2wpkh params + if ("p2wpkh" in n) { + try { + groestlcoinjs.HDNode.fromBase58(rootKeyBase58, n.p2wpkh); + return ""; + } + catch (e) {} + } + // try parsing using p2wpkh-in-p2sh network params + if ("p2wpkhInP2sh" in n) { + try { + groestlcoinjs.HDNode.fromBase58(rootKeyBase58, n.p2wpkhInP2sh); + return ""; + } + catch (e) {} + } + } + // try the network params as currently specified + try { + groestlcoinjs.HDNode.fromBase58(rootKeyBase58, network); + } + catch (e) { + return "Invalid root key"; + } + return ""; + } + function getDerivationPath() { if (bip44TabSelected()) { 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; + var change = parseIntNoNaN(DOM.bip44change.val(), ""); + var path = "m"; + path += "/" + purpose + "'"; + path += "/" + coin + "'"; + path += "/" + account + "'"; + if (change !== "") { + path += "/" + change; + } DOM.bip44path.val(path); var derivationPath = DOM.bip44path.val(); console.log("Using derivation path from BIP44 tab: " + derivationPath); @@ -672,6 +850,14 @@ return false; } + function isGRS() { + return networks[DOM.network.val()].name == "GRS - Groestlcoin" || networks[DOM.network.val()].name == "GRS - Groestlcoin Testnet"; + } + + function isELA() { + return networks[DOM.network.val()].name == "ELA - Elastos" + } + function displayBip44Info() { // Get the derivation path for the account var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44); @@ -685,9 +871,14 @@ var accountExtendedKey = calcBip32ExtendedKey(path); var accountXprv = accountExtendedKey.toBase58(); var accountXpub = accountExtendedKey.neutered().toBase58(); + // Display the extended keys DOM.bip44accountXprv.val(accountXprv); DOM.bip44accountXpub.val(accountXpub); + + if (isELA()) { + displayBip44InfoForELA(); + } } function displayBip49Info() { @@ -743,6 +934,10 @@ clearAddressesList(); var initialAddressCount = parseInt(DOM.rowsToAdd.val()); displayAddresses(0, initialAddressCount); + + if (isELA()) { + displayBip32InfoForELA(); + } } function displayAddresses(start, total) { @@ -814,7 +1009,10 @@ var keyPair = key.keyPair; var useUncompressed = useBip38; if (useUncompressed) { - keyPair = new bitcoinjs.bitcoin.ECPair(keyPair.d, null, { compressed: false }); + keyPair = new bitcoinjs.bitcoin.ECPair(keyPair.d, null, { network: network, compressed: false }); + if(isGRS()) + keyPair = new groestlcoinjs.ECPair(keyPair.d, null, { network: network, compressed: false }); + } // get address var address = keyPair.getAddress().toString(); @@ -822,12 +1020,17 @@ var hasPrivkey = !key.isNeutered(); var privkey = "NA"; if (hasPrivkey) { - privkey = keyPair.toWIF(network); + privkey = keyPair.toWIF(); // BIP38 encode private key if required if (useBip38) { - privkey = bitcoinjsBip38.encrypt(keyPair.d.toBuffer(), false, bip38password, function(p) { - console.log("Progressed " + p.percent.toFixed(1) + "% for index " + index); - }); + if(isGRS()) + privkey = groestlcoinjsBip38.encrypt(keyPair.d.toBuffer(), false, bip38password, function(p) { + console.log("Progressed " + p.percent.toFixed(1) + "% for index " + index); + }, null, networks[DOM.network.val()].name.includes("Testnet")); + else + privkey = bitcoinjsBip38.encrypt(keyPair.d.toBuffer(), false, bip38password, function(p) { + console.log("Progressed " + p.percent.toFixed(1) + "% for index " + index); + }); } } // get pubkey @@ -837,7 +1040,7 @@ indexText = indexText + "'"; } // Ethereum values are different - if (networks[DOM.network.val()].name == "ETH - Ethereum") { + if (networkIsEthereum()) { var privKeyBuffer = keyPair.d.toBuffer(32); privkey = privKeyBuffer.toString('hex'); var addressBuffer = ethUtil.privateToAddress(privKeyBuffer); @@ -847,11 +1050,55 @@ privkey = ethUtil.addHexPrefix(privkey); pubkey = ethUtil.addHexPrefix(pubkey); } + + // Stellar is different + if (networks[DOM.network.val()].name == "XLM - Stellar") { + var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44); + var coin = parseIntNoNaN(DOM.bip44coin.val(), 0); + var path = "m/"; + path += purpose + "'/"; + path += coin + "'/" + index + "'"; + var keypair = stellarUtil.getKeypair(path, seed); + indexText = path; + privkey = keypair.secret(); + pubkey = address = keypair.publicKey(); + } + if ((networks[DOM.network.val()].name == "NAS - Nebulas")) { + var NasAccount = require("nebulas-account"); + var privKeyBuffer = keyPair.d.toBuffer(32); + var nebulasAccount = new NasAccount(); + nebulasAccount.setPrivateKey(privKeyBuffer); + address = nebulasAccount.getAddressString(); + privkey = nebulasAccount.getPrivateKeyString(); + pubkey = nebulasAccount.getPublicKeyString(); + } // Ripple values are different if (networks[DOM.network.val()].name == "XRP - Ripple") { privkey = convertRipplePriv(privkey); address = convertRippleAdrr(address); } + // CasinoCoin values are different + if (networks[DOM.network.val()].name == "CSC - CasinoCoin") { + privkey = convertCasinoCoinPriv(privkey); + address = convertCasinoCoinAdrr(address); + } + // Bitcoin Cash address format may vary + if (networks[DOM.network.val()].name == "BCH - Bitcoin Cash") { + var bchAddrType = DOM.bitcoinCashAddressType.filter(":checked").val(); + if (bchAddrType == "cashaddr") { + address = bchaddr.toCashAddress(address); + } + else if (bchAddrType == "bitpay") { + address = bchaddr.toBitpayAddress(address); + } + } + // Bitcoin Cash address format may vary + if (networks[DOM.network.val()].name == "SLP - Simple Ledger Protocol") { + var bchAddrType = DOM.bitcoinCashAddressType.filter(":checked").val(); + if (bchAddrType == "cashaddr") { + address = bchaddr.toSlpAddress(address); + } + } // Segwit addresses are different if (isSegwit) { if (!segwitAvailable) { @@ -870,6 +1117,47 @@ address = bitcoinjs.bitcoin.address.fromOutputScript(scriptpubkey, network) } } + + if ((networks[DOM.network.val()].name == "CRW - Crown")) { + address = bitcoinjs.bitcoin.networks.crown.toNewAddress(address); + } + + if (networks[DOM.network.val()].name == "EOS - EOSIO") { + address = "" + pubkey = eosUtil.bufferToPublic(keyPair.getPublicKeyBuffer()); + privkey = eosUtil.bufferToPrivate(keyPair.d.toBuffer(32)); + } + + //Groestlcoin Addresses are different + if(isGRS()) { + + if (isSegwit) { + if (!segwitAvailable) { + return; + } + if (isP2wpkh) { + address = groestlcoinjs.address.fromOutputScript(scriptpubkey, network) + } + else if (isP2wpkhInP2sh) { + address = groestlcoinjs.address.fromOutputScript(scriptpubkey, network) + } + } + //non-segwit addresses are handled by using groestlcoinjs for bip32RootKey + } + + if (isELA()) { + let elaAddress = calcAddressForELA( + seed, + parseIntNoNaN(DOM.bip44coin.val(), 0), + parseIntNoNaN(DOM.bip44account.val(), 0), + parseIntNoNaN(DOM.bip44change.val(), 0), + index + ); + address = elaAddress.address; + privkey = elaAddress.privateKey; + pubkey = elaAddress.publicKey; + } + addAddressToList(indexText, address, pubkey, privkey); if (isLast) { hidePending(); @@ -1155,11 +1443,45 @@ function wordArrayToPhrase(words) { var phrase = words.join(" "); var language = getLanguageFromPhrase(phrase); - if (language == "japanese" || language == "korean") { + if (language == "japanese") { phrase = words.join("\u3000"); } return phrase; } + + function writeSplitPhrase(phrase) { + var wordCount = phrase.split(/\s/g).length; //get number of words in phrase + var left=[]; //initialize array of indexs + for (var i=0;i0) { //while indexs left + groupI=(groupI+1)%3; //get next group to insert index into + seed = seed * 16807 % 2147483647; //change random value.(simple predicatable random number generator works well for this use) + var selected=Math.floor(left.length*(seed - 1) / 2147483646); //get index in left we will use for this group + group[groupI].push(left[selected]); //add index to group + left.splice(selected,1); //remove selected index + } + var cards=[phrase.split(/\s/g),phrase.split(/\s/g),phrase.split(/\s/g)];//make array of cards + for (var i=0;i<3;i++) { //go through each card + for (var ii=0;ii