X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2Fjs%2Findex.js;h=b98d9794542168a11403b27cb799fe99421f7b20;hb=44e705cd3119fb0bc1ac72747d33e2b032a73282;hp=8c23e615c26b484201f12af2abf215fa40975064;hpb=491948dbcb6bee740158037a8aefe5096becb76e;p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FBIP39.git diff --git a/src/js/index.js b/src/js/index.js index 8c23e61..b98d979 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; @@ -69,6 +70,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 +136,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); @@ -488,9 +492,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 +536,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) { @@ -595,6 +641,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 +684,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 +837,10 @@ return false; } + function isGRS() { + return networks[DOM.network.val()].name == "GRS - Groestlcoin" || networks[DOM.network.val()].name == "GRS - Groestlcoin Testnet"; + } + function displayBip44Info() { // Get the derivation path for the account var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44); @@ -888,6 +984,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 +997,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,15 +1014,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") - || (networks[DOM.network.val()].name == "DXN - DEXON")) { + if (networkIsEthereum()) { var privKeyBuffer = keyPair.d.toBuffer(32); privkey = privKeyBuffer.toString('hex'); var addressBuffer = ethUtil.privateToAddress(privKeyBuffer); @@ -928,6 +1024,19 @@ 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); @@ -942,6 +1051,11 @@ 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(); @@ -952,6 +1066,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) { @@ -970,6 +1091,34 @@ 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 + } + addAddressToList(indexText, address, pubkey, privkey); if (isLast) { hidePending(); @@ -1481,6 +1630,22 @@ return DOM.bip32tab.hasClass("active"); } + function networkIsEthereum() { + var name = networks[DOM.network.val()].name; + return (name == "ETH - Ethereum") + || (name == "ETC - Ethereum Classic") + || (name == "PIRL - Pirl") + || (name == "MIX - MIX") + || (name == "MUSIC - Musicoin") + || (name == "POA - Poa") + || (name == "EXP - Expanse") + || (name == "CLO - Callisto") + || (name == "DXN - DEXON") + || (name == "ELLA - Ellaism") + || (name == "ESN - Ethersocial Network") + || (name == "VET - VeChain") + } + function networkHasSegwit() { var n = network; if ("baseNetwork" in network) { @@ -1510,10 +1675,30 @@ return DOM.bip141tab.hasClass("active"); } + function setBip44ChangeValue() { + if (isDefaultBip44ChangeValue) { + if (networkIsEthereum()) { + DOM.bip44change.val(""); + } else { + DOM.bip44change.val(0); + } + } + } + + function modifiedDefaultBip44ChangeValue() { + isDefaultBip44ChangeValue = false; + } + + function resetDefaultBip44ChangeValue() { + isDefaultBip44ChangeValue = true; + setBip44ChangeValue(); + } + function setHdCoin(coinValue) { DOM.bip44coin.val(coinValue); DOM.bip49coin.val(coinValue); DOM.bip84coin.val(coinValue); + setBip44ChangeValue(); } function showSegwitAvailable() { @@ -1656,6 +1841,13 @@ setHdCoin(161); }, }, + { + name: "ARYA - Aryacoin", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.aryacoin; + setHdCoin(357); + }, + }, { name: "AUR - Auroracoin", onSelect: function() { @@ -1677,6 +1869,13 @@ setHdCoin(220); }, }, + { + name: "BOLI - Bolivarcoin", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.bolivarcoin; + setHdCoin(278); + }, + }, { name: "BCA - Bitcoin Atom", onSelect: function() { @@ -1718,7 +1917,7 @@ network = bitcoinjs.bitcoin.networks.blocknode; setHdCoin(2941); }, - }, + }, { name: "tBND - Blocknode Testnet", onSelect: function() { @@ -1782,6 +1981,13 @@ setHdCoin(183); }, }, + { + name: "BSV - BitcoinSV", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.bitcoinsv; + setHdCoin(236); + }, + }, { name: "BTCZ - Bitcoinz", onSelect: function() { @@ -1860,6 +2066,13 @@ setHdCoin(71); }, }, + { + name: "CPU - CPUchain", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.cpuchain; + setHdCoin(363); + }, + }, { name: "CRAVE - Crave", onSelect: function() { @@ -1867,6 +2080,13 @@ setHdCoin(186); }, }, + { + name: "CRW - Crown (Legacy)", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.crown; + setHdCoin(72); + }, + }, { name: "CRW - Crown", onSelect: function() { @@ -1874,6 +2094,13 @@ setHdCoin(72); }, }, + { + name: "CSC - CasinoCoin", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.bitcoin; + setHdCoin(359); + }, + }, { name: "DASH - Dash", onSelect: function() { @@ -1930,6 +2157,13 @@ setHdCoin(3); }, }, + { + name: "DOGEt - Dogecoin Testnet", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.dogecointestnet; + setHdCoin(1); + }, + }, { name: "DXN - DEXON", onSelect: function() { @@ -1958,6 +2192,14 @@ setHdCoin(78); }, }, + { + name: "ELLA - Ellaism", + segwitAvailable: false, + onSelect: function() { + network = bitcoinjs.bitcoin.networks.bitcoin; + setHdCoin(163); + }, + }, { name: "EMC2 - Einsteinium", onSelect: function() { @@ -1972,6 +2214,21 @@ setHdCoin(151); }, }, + { + name: "EOS - EOSIO", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.bitcoin; + setHdCoin(194); + }, + }, + { + name: "ESN - Ethersocial Network", + segwitAvailable: false, + onSelect: function() { + network = bitcoinjs.bitcoin.networks.bitcoin; + setHdCoin(31102); + }, + }, { name: "ETC - Ethereum Classic", segwitAvailable: false, @@ -2009,6 +2266,20 @@ setHdCoin(40); }, }, + { + name: "FIX - FIX", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.fix; + setHdCoin(336); + }, + }, + { + name: "FIX - FIX Testnet", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.fixtestnet; + setHdCoin(1); + }, + }, { name: "FJC - Fujicoin", onSelect: function() { @@ -2065,6 +2336,20 @@ setHdCoin(84); }, }, + { + name: "GRS - Groestlcoin", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.groestlcoin; + setHdCoin(17); + }, + }, + { + name: "GRS - Groestlcoin Testnet", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.groestlcointestnet; + setHdCoin(1); + }, + }, { name: "HNC - Helleniccoin", onSelect: function() { @@ -2073,11 +2358,18 @@ }, }, { - name: "HUSH - Hush", + name: "HUSH - Hush (Legacy)", onSelect: function() { network = bitcoinjs.bitcoin.networks.hush; setHdCoin(197); }, + }, + { + name: "HUSH - Hush3", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.hush3; + setHdCoin(197); + }, }, { name: "INSN - Insane", @@ -2167,6 +2459,14 @@ DOM.litecoinLtubContainer.removeClass("hidden"); }, }, + { + name: "LTCt - Litecoin Testnet", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.litecointestnet; + setHdCoin(1); + DOM.litecoinLtubContainer.removeClass("hidden"); + }, + }, { name: "LTZ - LitecoinZ", onSelect: function() { @@ -2217,6 +2517,13 @@ setHdCoin(22); }, }, + { + name: "MONK - Monkey Project", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.monkeyproject, + setHdCoin(214); + }, + }, { name: "MUSIC - Musicoin", segwitAvailable: false, @@ -2323,6 +2630,13 @@ setHdCoin(200); }, }, + { + name: "ONION - DeepOnion", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.deeponion; + setHdCoin(305); + }, + }, { name: "ONX - Onixcoin", onSelect: function() { @@ -2416,6 +2730,13 @@ setHdCoin(122); }, }, + { + name: "RPD - Rapids", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.rapids; + setHdCoin(320); + }, + }, { name: "RVN - Ravencoin", onSelect: function() { @@ -2486,6 +2807,13 @@ setHdCoin(111); }, }, + { + name: "SLP - Simple Ledger Protocol", + onSelect: function() { + DOM.bitcoinCashAddressTypeContainer.removeClass("hidden"); + setHdCoin(245); + }, + }, { name: "SLR - Solarcoin", onSelect: function() { @@ -2549,6 +2877,20 @@ setHdCoin(159); }, }, + { + name: "TWINS - TWINS", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.twins; + setHdCoin(970); + }, + }, + { + name: "TWINS - TWINS Testnet", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.twinstestnet; + setHdCoin(1); + }, + }, { name: "USC - Ultimatesecurecash", onSelect: function() { @@ -2577,6 +2919,13 @@ setHdCoin(33); }, }, + { + name: "VET - VeChain", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.bitcoin; + setHdCoin(818); + }, + }, { name: "VIA - Viacoin", onSelect: function() { @@ -2605,6 +2954,13 @@ setHdCoin(28); }, }, + { + name: "WGR - Wagerr", + onSelect: function() { + network = bitcoinjs.bitcoin.networks.wagerr; + setHdCoin(7825266); + }, + }, { name: "WC - Wincoin", onSelect: function() { @@ -2626,6 +2982,13 @@ setHdCoin(65); }, }, + { + name: "XLM - Stellar", + onSelect: function() { + network = stellarUtil.dummyNetwork; + setHdCoin(148); + }, + }, { name: "XMY - Myriadcoin", onSelect: function() { @@ -2691,7 +3054,7 @@ }, }, { - name: "ZEN - Zencash", + name: "ZEN - Horizen", onSelect: function() { network = bitcoinjs.bitcoin.networks.zencash; setHdCoin(121);