X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2Fjs%2Findex.js;h=807230b2130ef6456f01f70d7dc413097663684a;hb=ea2cb91a49d57ee72674223eb3672fcb9a41130c;hp=83221154ab90543c487aa3e00de51baf6e549891;hpb=acf4c9b588c6abcc275b4e46df54248a2c913366;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FBIP39.git diff --git a/src/js/index.js b/src/js/index.js index 8322115..807230b 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; @@ -44,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"); @@ -69,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"); @@ -134,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); @@ -232,7 +238,14 @@ if (phraseChangeTimeoutEvent != null) { clearTimeout(phraseChangeTimeoutEvent); } - phraseChangeTimeoutEvent = setTimeout(phraseChanged, 400); + phraseChangeTimeoutEvent = setTimeout(function() { + phraseChanged(); + var entropy = mnemonic.toRawEntropyBin(DOM.phrase.val()); + if (entropy !== null) { + DOM.entropyMnemonicLength.val("raw"); + DOM.entropy.val(entropy); + } + }, 400); } function phraseChanged() { @@ -251,6 +264,7 @@ calcForDerivationPath(); // Show the word indexes showWordIndexes(); + writeSplitPhrase(phrase); } function tabChanged() { @@ -297,6 +311,7 @@ clearDisplay(); clearEntropyFeedback(); DOM.phrase.val(""); + DOM.phraseSplit.val(""); showValidationError("Blank entropy"); return; } @@ -331,6 +346,7 @@ showPending(); // Clear existing mnemonic and passphrase DOM.phrase.val(""); + DOM.phraseSplit.val(""); DOM.passphrase.val(""); seed = null; if (rootKeyChangedTimeoutEvent != null) { @@ -488,9 +504,16 @@ 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()) { @@ -525,6 +548,41 @@ 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) { @@ -552,7 +610,7 @@ extendedKey = extendedKey.derive(index); } } - return extendedKey + return extendedKey; } function showValidationError(errorText) { @@ -595,6 +653,9 @@ } 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()) { @@ -635,17 +696,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); @@ -745,6 +849,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); @@ -758,9 +870,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() { @@ -816,6 +933,10 @@ clearAddressesList(); var initialAddressCount = parseInt(DOM.rowsToAdd.val()); displayAddresses(0, initialAddressCount); + + if (isELA()) { + displayBip32InfoForELA(); + } } function displayAddresses(start, total) { @@ -888,6 +1009,9 @@ var useUncompressed = useBip38; if (useUncompressed) { 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(); @@ -898,9 +1022,14 @@ 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 @@ -910,14 +1039,7 @@ indexText = indexText + "'"; } // Ethereum values are different - if ((networks[DOM.network.val()].name == "ETH - Ethereum") - || (networks[DOM.network.val()].name == "ETC - Ethereum Classic") - || (networks[DOM.network.val()].name == "PIRL - Pirl") - || (networks[DOM.network.val()].name == "MIX - MIX") - || (networks[DOM.network.val()].name == "MUSIC - Musicoin") - || (networks[DOM.network.val()].name == "POA - Poa") - || (networks[DOM.network.val()].name == "EXP - Expanse") - || (networks[DOM.network.val()].name == "CLO - Callisto")) { + if (networkIsEthereum()) { var privKeyBuffer = keyPair.d.toBuffer(32); privkey = privKeyBuffer.toString('hex'); var addressBuffer = ethUtil.privateToAddress(privKeyBuffer); @@ -927,11 +1049,38 @@ 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(); @@ -942,6 +1091,13 @@ 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) { @@ -960,6 +1116,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(); @@ -1251,6 +1448,49 @@ return phrase; } + function writeSplitPhrase(phrase) { + var wordCount = phrase.split(/\s/g).length; + var left=[]; + for (var i=0;i0) { + groupI=(groupI+1)%3; + seed = seed * 16807 % 2147483647; + var selected=Math.floor(left.length*(seed - 1) / 2147483646); + group[groupI].push(left[selected]); + left.splice(selected,1); + } + var cards=[phrase.split(/\s/g),phrase.split(/\s/g),phrase.split(/\s/g)]; + for (var i=0;i<3;i++) { + for (var ii=0;ii