]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/BIP39.git/blobdiff - src/js/index.js
Add Elastos
[perso/Immae/Projets/Cryptomonnaies/BIP39.git] / src / js / index.js
index 035747cb0f89f2a415d90336292b5ce6d0e9bb11..52fb3ff94d32f94e739b794d501d03c8162b2e09 100644 (file)
     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()) {
         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) {
                 extendedKey = extendedKey.derive(index);
             }
         }
-        return extendedKey
+        return extendedKey;
     }
 
     function showValidationError(errorText) {
     }
 
     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()) {
         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);
         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);
         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() {
         clearAddressesList();
         var initialAddressCount = parseInt(DOM.rowsToAdd.val());
         displayAddresses(0, initialAddressCount);
+
+        if (isELA()) {
+            displayBip32InfoForELA();
+        }
     }
 
     function displayAddresses(start, total) {
                 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();
                     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
                     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();
                 setHdCoin(161);
             },
         },
+        {
+            name: "ARYA - Aryacoin",
+            onSelect: function() {
+                network = bitcoinjs.bitcoin.networks.aryacoin;
+                setHdCoin(357);
+            },
+        },
         {
             name: "AUR - Auroracoin",
             onSelect: function() {
                 setHdCoin(183);
             },
         },
+        {
+            name: "BSV - BitcoinSV",
+            onSelect: function() {
+                network = bitcoinjs.bitcoin.networks.bitcoinsv;
+                setHdCoin(236);
+            },
+        },
         {
             name: "BTCZ - Bitcoinz",
             onSelect: function() {
                 setHdCoin(78);
             },
         },
+        {
+            name: "ELA - Elastos",
+            onSelect: function () {
+                network = bitcoinjs.bitcoin.networks.elastos;
+                setHdCoin(2305);
+            },
+        },
         {
             name: "ELLA - Ellaism",
             segwitAvailable: false,
                 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() {
             },
         },
                {
-            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",
                 setHdCoin(22);
             },
         },
+        {
+            name: "MONK - Monkey Project",
+            onSelect: function() {
+                network = bitcoinjs.bitcoin.networks.monkeyproject,
+                setHdCoin(214);
+            },
+        },
         {
             name: "MUSIC - Musicoin",
             segwitAvailable: false,
                 setHdCoin(122);
             },
         },
+        {
+            name: "RPD - Rapids",
+            onSelect: function() {
+                network = bitcoinjs.bitcoin.networks.rapids;
+                setHdCoin(320);
+            },
+        },
         {
             name: "RVN - Ravencoin",
             onSelect: function() {
         }
     ]
 
+    // ELA - Elastos functions - begin
+    function displayBip44InfoForELA() {
+        if (!isELA()) {
+            return;
+        }
+
+        var coin = parseIntNoNaN(DOM.bip44coin.val(), 0);
+        var account = parseIntNoNaN(DOM.bip44account.val(), 0);
+
+        // Calculate the account extended keys
+        var accountXprv = elastosjs.getAccountExtendedPrivateKey(seed, coin, account);
+        var accountXpub = elastosjs.getAccountExtendedPublicKey(seed, coin, account);
+
+        // Display the extended keys
+        DOM.bip44accountXprv.val(accountXprv);
+        DOM.bip44accountXpub.val(accountXpub);
+    }
+
+    function displayBip32InfoForELA() {
+        if (!isELA()) {
+            return;
+        }
+
+        var coin = parseIntNoNaN(DOM.bip44coin.val(), 0);
+        var account = parseIntNoNaN(DOM.bip44account.val(), 0);
+        var change = parseIntNoNaN(DOM.bip44change.val(), 0);
+
+        DOM.extendedPrivKey.val(elastosjs.getBip32ExtendedPrivateKey(seed, coin, account, change));
+        DOM.extendedPubKey.val(elastosjs.getBip32ExtendedPublicKey(seed, coin, account, change));
+
+        // Display the addresses and privkeys
+        clearAddressesList();
+        var initialAddressCount = parseInt(DOM.rowsToAdd.val());
+        displayAddresses(0, initialAddressCount);
+    }
+
+    function calcAddressForELA(seed, coin, account, change, index) {
+        if (!isELA()) {
+            return;
+        }
+
+        var publicKey = elastosjs.getDerivedPublicKey(elastosjs.getMasterPublicKey(seed), change, index);
+        return {
+            privateKey: elastosjs.getDerivedPrivateKey(seed, coin, account, change, index),
+            publicKey: publicKey,
+            address: elastosjs.getAddress(publicKey.toString('hex'))
+        };
+    }
+    // ELA - Elastos functions - end
+
     init();
 
 })();