diff --git a/assets/eco-scripts/accounts_helper.js b/assets/eco-scripts/accounts_helper.js index 8e9a8422..a5de3edb 100644 --- a/assets/eco-scripts/accounts_helper.js +++ b/assets/eco-scripts/accounts_helper.js @@ -1,22 +1,22 @@ // searchAddrIndexFromAddressPrivateKeyWifiLabelEncrypted -function searchWalletID(baseValue) { - for (iToFind = 0; iToFind < ECO_WALLET.length; ++iToFind) { - if (ECO_WALLET[iToFind].account._encrypted != null) - if (ECO_WALLET[iToFind].account.encrypted == baseValue) +function searchWalletID(BASE_ARRAY_SEARCH, baseValue) { + for (iToFind = 0; iToFind < BASE_ARRAY_SEARCH.length; ++iToFind) { + if (BASE_ARRAY_SEARCH[iToFind].account._encrypted != null) + if (BASE_ARRAY_SEARCH[iToFind].account.encrypted == baseValue) return iToFind; - if (ECO_WALLET[iToFind].account.address == baseValue) + if (BASE_ARRAY_SEARCH[iToFind].account.address == baseValue) return iToFind; - if (ECO_WALLET[iToFind].label == baseValue) + if (BASE_ARRAY_SEARCH[iToFind].label == baseValue) return iToFind; - if (ECO_WALLET[iToFind].account._privateKey != null) - if (ECO_WALLET[iToFind].account.privateKey == baseValue) + if (BASE_ARRAY_SEARCH[iToFind].account._privateKey != null) + if (BASE_ARRAY_SEARCH[iToFind].account.privateKey == baseValue) return iToFind; - if (ECO_WALLET[iToFind].account._WIF != null) - if (ECO_WALLET[iToFind].account.WIF == baseValue) + if (BASE_ARRAY_SEARCH[iToFind].account._WIF != null) + if (BASE_ARRAY_SEARCH[iToFind].account.WIF == baseValue) return iToFind; } diff --git a/assets/eco-scripts/consensus-info.js b/assets/eco-scripts/consensus-info.js index 91d6ae92..d39160df 100644 --- a/assets/eco-scripts/consensus-info.js +++ b/assets/eco-scripts/consensus-info.js @@ -168,8 +168,7 @@ function createMultiSigFromNextValidators() { label: "CN-MultiSig", print: true }); - ECO_WALLET = DEFAULT_WALLET; - ECO_WALLET = ECO_WALLET.concat(ECO_EXTRA_ACCOUNTS); + ECO_WALLET = DEFAULT_WALLET.concat(ECO_EXTRA_ACCOUNTS); drawPopulateAllWalletAccountsInfo(); }, "json" // The format the response should be in diff --git a/assets/eco-scripts/global_var.js b/assets/eco-scripts/global_var.js index 7e96b8b4..b1464e4a 100644 --- a/assets/eco-scripts/global_var.js +++ b/assets/eco-scripts/global_var.js @@ -124,4 +124,6 @@ var RELAYED_TXS = []; /* ACE SESSIONS */ var aceEditor; -var openedSessions = new Map(); \ No newline at end of file +var openedSessions = new Map(); + +var MASTER_KEY_WALLET = ""; \ No newline at end of file diff --git a/assets/eco-scripts/wallet_addNewAccounts.js b/assets/eco-scripts/wallet_addNewAccounts.js new file mode 100644 index 00000000..4af97546 --- /dev/null +++ b/assets/eco-scripts/wallet_addNewAccounts.js @@ -0,0 +1,236 @@ +function togglePasswordSwal() { + const passwordField = $('#input-newaccount-password-1'); + const passwordFieldType = passwordField.attr('type'); + passwordField.attr('type', passwordFieldType === 'password' ? 'text' : 'password'); +} + +//First verifications for adding new wallet +function addWalletFromForm() { + var type = $("#type_to_register")[0].value; + var keyToAdd = $("#accountToAddInfo")[0].value; + var labelToAdd = $("#accountLabelToAddInfo")[0].value; + if (labelToAdd === "") + labelToAdd = "ImportedWallet_From_" + type; + + if (keyToAdd == "") { + swal2Simple("Error when adding " + type, "Empty key", 5500, "error"); + return false; + } + if (type == 'address' && !Neon.wallet.isAddress(keyToAdd)) { + swal2Simple("Error when adding " + type, "Not valid", 5500, "error"); + return false; + } + + if (type == 'scripthash' && !Neon.wallet.isScriptHash(keyToAdd)) { + swal2Simple("Error when adding " + type, "Not valid", 5500, "error"); + return false; + } + + if (type == 'publickey' && !Neon.wallet.isPublicKey(keyToAdd)) { + swal2Simple("Error when adding " + type, "Not valid", 5500, "error"); + return false; + } + + if (type == 'wif' && !Neon.wallet.isWIF(keyToAdd)) { + swal2Simple("Error when adding " + type, "Not valid", 5500, "error"); + return false; + } + + if (type == 'privatekey' && !Neon.wallet.isPrivateKey(keyToAdd)) { + swal2Simple("Error when adding " + type, "Not valid", 5500, "error"); + return false; + } + + if (["publickey", "address", "scripthash"].includes(type)) { + var accountToAdd = new Neon.wallet.Account(keyToAdd); + addAccountAndDraw(accountToAdd, labelToAdd); + } + + if (type == 'multisig') { + var accountToAdd = getAccountFromMultiSigVerification(keyToAdd); + addAccountAndDraw(accountToAdd, labelToAdd); + } + + // These are more complex because requires encrypting data and setting a master key + if (["privatekey", "wif"].includes(type)) { + var accountToAdd = new Neon.wallet.Account(keyToAdd); + addSafeAccount(accountToAdd, labelToAdd) + } + + // Even more complex because requires first decrypting + if (type == 'encryptedkey') { + tryToDecrypt(keyToAdd, labelToAdd); + } +} + +function addAccountAndDraw(accountToAdd, labelToAdd) { + if (addToWallet(accountToAdd, labelToAdd)) + drawPopulateAllWalletAccountsInfo(); +} + +function addSafeAccount(accountToAdd, labelToAdd) { + //Asks for password if privatekey or wif or encrypted + if (MASTER_KEY_WALLET != "") { + swal2Simple("You already have a master key", "Check on config tab", 5500, "success"); + addAccountAndDraw(accountToAdd, labelToAdd); + } else { + setMasterKey(() => { + console.log("dasidhsi") + addAccountAndDraw(accountToAdd, labelToAdd); + }, "Adding new account."); + } +} + +function setMasterKey(callback, labelToAdd) { + var serializedHTML = + '
' + + '' + + '
' + + '' + + '
' + + '
' + + '' + + '
' + + '
'; + + const swalWithBootstrapButtons = Swal.mixin({ + customClass: { + confirmButton: "btn btn-success", + }, + buttonsStyling: false + }); + + swalWithBootstrapButtons.fire({ + title: "Enter your password for local storage (MasterKey) - " + labelToAdd, + html: serializedHTML, + color: "#00AF92", + background: "#263A40", + confirmButtonText: "Confirm password", + preConfirm: () => { + var pass1 = document.getElementById("input-newaccount-password-1").value; + var pass2 = document.getElementById("input-newaccount-password-2").value; + if (pass1 !== "" && pass1 == pass2) { + MASTER_KEY_WALLET = pass1; + return true; + } + else { + Swal.update({ footer: "Password should be the same." }) + return false; + } + } + }).then((result) => { + if (result.isConfirmed) { + callback(); + }else{ + swal2Simple("Be careful!", "A password is needed for this action.", 0, "error"); + } + }); +} + + +function tryToDecrypt(keyToAdd, labelToAdd) { + var serializedHTML = + '
' + + '' + + '
' + + '
' + + '' + + '
' + + '
'; + + const swalWithBootstrapButtons = Swal.mixin({ + customClass: { + confirmButton: "btn btn-success", + }, + buttonsStyling: false + }); + + swalWithBootstrapButtons.fire({ + title: "Enter your decription key. After that the MasterKey will be used to re-encrypt.", + html: serializedHTML, + color: "#00AF92", + background: "#263A40", + confirmButtonText: "Decrypt", + preConfirm: async () => { + return new Promise(async (resolve) => { + var pass = document.getElementById("input-newaccount-password-1").value; + var accountToAdd = new Neon.wallet.Account(keyToAdd); + try { + Swal.update({ footer: "Decrypting..." }); + const decryptedAccount = await accountToAdd.decrypt(pass); + console.log("Decrypted"); + addSafeAccount(decryptedAccount, labelToAdd); + resolve(true); // Resolve with true if decryption is successful + } catch (err) { + console.error(err); + Swal.update({ footer: "Decryption error. Password should be wrong." }); + resolve(false); // Resolve with false if decryption fails + } + }); + } + }).then((result) => { + }); +} + +function addContractToWallet(scriptHashToAdd) { + var accountToAdd; + if (scriptHashToAdd != '') { + accountToAdd = new Neon.wallet.Account(scriptHashToAdd); + labelToAdd = scriptHashToAdd.slice(0, 3) + "..." + scriptHashToAdd.slice(-3) + if (addToWallet(accountToAdd, labelToAdd)) + drawPopulateAllWalletAccountsInfo(); + + $('.nav a[href="#nav-wallet"]').tab('show'); + } else + console.log("Nothing to add. Scripthash looks to be empty!"); +} + +//TODO Add support for adding multisig and specialSC +function addToWallet(accountToAdd, labelToAdd, verificationScriptToAdd = "") { + if (!accountToAdd.isMultiSig) { + if (checkIfAccountAlreadyBelongsToEcoWallet(accountToAdd.address)) + return false; + } else { + // Checks for multisig + var vsToAdd = accountToAdd.contract.script; + if (vsToAdd == '') { + alert("Verification script is empty for this multisig!"); + return false; + } + + if (accountToAdd.address != toBase58(getScriptHashFromAVM(vsToAdd))) { + alert("Error on converting verification script to base58"); + return false; + } + + if (checkIfAccountAlreadyBelongsToEcoWallet(addressBase58ToAdd)) + return false; + } + // TODO --- CHECK IF IT IS OK WITHOUT OWNERS FOR MULTI SIGNATURES + addExtraAccountAndUpdateWallet(accountToAdd, labelToAdd, true); + return true; +} +//=============================================================== + +function checkIfAccountAlreadyBelongsToEcoWallet(baseValue) { + var registeredIndex = searchWalletID(ECO_WALLET, baseValue); + + if (registeredIndex != -1) { + var sTitle = "Public addressBase58 already registered for ECO_WALLET."; + var sText = "Please, delete index " + registeredIndex + " first."; + swal2Simple(sTitle, sText, 5500, "error"); + return true; + } + return false; +} + +function addExtraAccountAndUpdateWallet(accToAdd, labelToAdd, print) { + var newAcc = { + account: accToAdd, + label: labelToAdd, + print: print + }; + ECO_EXTRA_ACCOUNTS.push(newAcc); + ECO_WALLET.push(newAcc); + btnWalletSave(); +} \ No newline at end of file diff --git a/assets/eco-scripts/wallet_center.js b/assets/eco-scripts/wallet_center.js index 0f4d8f30..0e0367b1 100644 --- a/assets/eco-scripts/wallet_center.js +++ b/assets/eco-scripts/wallet_center.js @@ -87,104 +87,12 @@ function drawWalletsStatus() { } //Finishe DrawWallets function //=============================================================== -//=============================================================== -//================ ADD NEW ADDRESS ============================== -function addWalletFromForm() { - var type = $("#type_to_register")[0].value; - var keyToAdd = $("#accountToAddInfo")[0].value; - var accountToAdd; - switch (type) { - case 'publickey': - case 'address': - case 'scripthash': - case 'privatekey': - case 'wif': - case 'encryptedkey': - accountToAdd = new Neon.wallet.Account(keyToAdd); - break; - case 'multisig': - accountToAdd = getAccountFromMultiSigVerification(keyToAdd); - break; - default: - console.error("Account to with not found type.") - } - - var labelToAdd = keyToAdd = $("#accountLabelToAddInfo")[0].value; - if (labelToAdd === "") - labelToAdd = "ImportedWallet_From_" + type; - if (addToWallet(accountToAdd, labelToAdd)) - drawPopulateAllWalletAccountsInfo(); -} - -function addContractToWallet(scriptHashToAdd) { - var accountToAdd; - if (scriptHashToAdd != '') { - accountToAdd = new Neon.wallet.Account(scriptHashToAdd); - labelToAdd = scriptHashToAdd.slice(0, 3) + "..." + scriptHashToAdd.slice(-3) - if (addToWallet(accountToAdd, labelToAdd)) - drawPopulateAllWalletAccountsInfo(); - - $('.nav a[href="#nav-wallet"]').tab('show'); - } else - console.log("Nothing to add. Scripthash looks to be empty!"); -} - -//TODO Add support for adding multisig and specialSC -function addToWallet(accountToAdd, labelToAdd, verificationScriptToAdd = "") { - - if (!accountToAdd.isMultiSig) { - if (!checkIfAccountAlreadyBelongsToEcoWallet(accountToAdd.address)) - return false; - } else { - // Checks for multisig - var vsToAdd = accountToAdd.contract.script; - if (vsToAdd == '') { - alert("Verification script is empty for this multisig!"); - return false; - } - - if (accountToAdd.address != toBase58(getScriptHashFromAVM(vsToAdd))) { - alert("Error on converting verification script to base58"); - return false; - } - - if (!checkIfAccountAlreadyBelongsToEcoWallet(addressBase58ToAdd)) - return false; - } - // TODO --- CHECK IF IT IS OK WITHOUT OWNERS FOR MULTI SIGNATURES - addExtraAccountAndUpdateWallet(accountToAdd, labelToAdd, true); - return true; -} -//=============================================================== - -function checkIfAccountAlreadyBelongsToEcoWallet(baseValue) { - var registeredIndex = searchWalletID(baseValue); - - if (registeredIndex != -1) { - var sTitle = "Public addressBase58 already registered for ECO_WALLET."; - var sText = "Please, delete index " + registeredIndex + " first."; - swal2Simple(sTitle, sText, 5500, "error"); - return false; - } - return true; -} - -function addExtraAccountAndUpdateWallet(accToAdd, labelToAdd, print) { - var newAcc = { - account: accToAdd, - label: labelToAdd, - print: print - }; - ECO_EXTRA_ACCOUNTS.push(newAcc); - ECO_WALLET.push(newAcc); - btnWalletSave(); -} /* //=============================================================== //============= FUNCTION CALLED WHEN SELECTION BOX CHANGES ====== function changeWalletInfo() { var wToChangeIndex = $("#wallet_info")[0].selectedOptions[0].index; - + if (isEncryptedOnly(wToChangeIndex)) { $("#dialog").show(); document.getElementById("walletInfoEncrypted").value = ECO_WALLET[wToChangeIndex].account.encrypted; @@ -197,33 +105,33 @@ function changeWalletInfo() { document.getElementById("addressVerificationScript").value = "-"; document.getElementById("addressOwners").value = "-"; } else { - + document.getElementById("walletInfoAddressBase58").value = ECO_WALLET[wToChangeIndex].account.address; - + if (ECO_WALLET[wToChangeIndex].account._encrypted != null) { document.getElementById("walletInfoEncrypted").value = ECO_WALLET[wToChangeIndex].account.encrypted; } else { $("#dialog").hide(); document.getElementById("walletInfoEncrypted").value = "-"; } - + document.getElementById("walletInfoScripthash").value = ECO_WALLET[wToChangeIndex].account.scriptHash; - + if (!ECO_WALLET[wToChangeIndex].account.isMultiSig && ECO_WALLET[wToChangeIndex].account._publicKey != null) document.getElementById("walletInfoPubKey").value = ECO_WALLET[wToChangeIndex].account.publicKey; else document.getElementById("walletInfoPubKey").value = "-"; - + if (!ECO_WALLET[wToChangeIndex].account.isMultiSig && ECO_WALLET[wToChangeIndex].account._WIF != null) document.getElementById("walletInfoWIF").value = ECO_WALLET[wToChangeIndex].account.WIF; else document.getElementById("walletInfoWIF").value = "-"; - + if (!ECO_WALLET[wToChangeIndex].account.isMultiSig && ECO_WALLET[wToChangeIndex].account._privateKey != null) document.getElementById("walletInfoPrivateKey").value = ECO_WALLET[wToChangeIndex].account.privateKey; else document.getElementById("walletInfoPrivateKey").value = "-"; - + document.getElementById("addressPrintInfo").value = ECO_WALLET[wToChangeIndex].print; document.getElementById("addressVerificationScript").value = ECO_WALLET[wToChangeIndex].account.contract.script; document.getElementById("addressOwners").value = JSON.stringify(ECO_WALLET[wToChangeIndex].owners); @@ -275,7 +183,13 @@ function populateAllWalletData() { //=============================================================== function deleteAccount(idToRemove) { + if (idToRemove >= DEFAULT_WALLET.length) { + var defaultLength = DEFAULT_WALLET.length; + ECO_EXTRA_ACCOUNTS.splice(idToRemove - defaultLength, 1); + btnWalletSave(); + } ECO_WALLET.splice(idToRemove, 1); + drawPopulateAllWalletAccountsInfo(); } diff --git a/assets/eco-scripts/wallet_localStorage.js b/assets/eco-scripts/wallet_localStorage.js index 7c34885e..c490b98e 100644 --- a/assets/eco-scripts/wallet_localStorage.js +++ b/assets/eco-scripts/wallet_localStorage.js @@ -19,30 +19,41 @@ function getIDFromExtraAccountStillEncrypted(baseEncrypted, encryptedToSearch) { } -function getExtraWalletAccountFromLocalStorage() { +async function getExtraWalletAccountFromLocalStorage() { var mySafeExtraAccountsWallet = getLocalStorage("mySafeEncryptedExtraAccounts"); + console.log("hello") + console.log(mySafeExtraAccountsWallet) if (mySafeExtraAccountsWallet) { mySafeExtraAccountsWallet = JSON.parse(mySafeExtraAccountsWallet); var myRecreatedExtraAccounts = []; - for (ea = 0; ea < mySafeExtraAccountsWallet.length; ++ea) { - var storedKey = mySafeExtraAccountsWallet[ea].key; + + await Promise.all(mySafeExtraAccountsWallet.map(async (accountData) => { + var storedKey = accountData.key; + var label = accountData.label; + var print = accountData.print; + console.log(storedKey) var myRestoredAccount = new Neon.wallet.Account(storedKey); - myRecreatedExtraAccounts.push({ - account: myRestoredAccount, - label: mySafeExtraAccountsWallet[ea].label, - print: mySafeExtraAccountsWallet[ea].print - }); - - if (!Neon.wallet.isAddress(storedKey)) { - myRestoredAccount.decrypt("teste").then(decryptedAccount => { - drawPopulateAllWalletAccountsInfo(); - }).catch(err => { - console.error(err); - swal2Simple("Decryption error", "Error when decrypting extra accounts!", 5500, "error"); + console.log(myRestoredAccount) + try { + if (!Neon.wallet.isAddress(storedKey) && !Neon.wallet.isPublicKey(storedKey)) { + console.log("trying to decrypt from local storage") + await myRestoredAccount.decrypt(MASTER_KEY_WALLET); + } + + console.log(myRestoredAccount) + myRecreatedExtraAccounts.push({ + account: myRestoredAccount, + label: label, + print: print }); + } catch (err) { + console.error(err); + swal2Simple("Decryption error", "Error when decrypting extra accounts!", 5500, "error"); } + })); - } + console.log("finally") + console.log(myRecreatedExtraAccounts) return myRecreatedExtraAccounts; } @@ -51,38 +62,58 @@ function getExtraWalletAccountFromLocalStorage() { function restoreWalletExtraAccountsLocalStorage() { - var tempWallet = getExtraWalletAccountFromLocalStorage(); - if (tempWallet != [] && tempWallet.length > 0) { + var mySafeExtraAccountsWallet = getLocalStorage("mySafeEncryptedExtraAccounts"); + if (mySafeExtraAccountsWallet && MASTER_KEY_WALLET == "") { + setMasterKey(() => { + updateExtraAndEcoWallet(); + }, "Restoring accounts"); + } else { + updateExtraAndEcoWallet(); + } +} + +async function updateExtraAndEcoWallet() { + var tempWallet = await getExtraWalletAccountFromLocalStorage(); + if (tempWallet.length > 0) { ECO_EXTRA_ACCOUNTS = tempWallet; - ECO_WALLET = DEFAULT_WALLET; - ECO_WALLET = ECO_WALLET.concat(ECO_EXTRA_ACCOUNTS); + ECO_WALLET = DEFAULT_WALLET.concat(ECO_EXTRA_ACCOUNTS); + drawPopulateAllWalletAccountsInfo(); } } function btnWalletSave() { - var SAFE_ACCOUNTS = []; - for (ea = 0; ea < ECO_EXTRA_ACCOUNTS.length; ++ea) { - if (ECO_EXTRA_ACCOUNTS[ea].account._privateKey != undefined) { - ECO_EXTRA_ACCOUNTS[ea].account.encrypt("teste").then(encryptedAccount => { - var restoredID = getIDFromExtraAccount(encryptedAccount.address); + if (ECO_EXTRA_ACCOUNTS.length > 0) { + if (MASTER_KEY_WALLET == "") { + swal2Simple("MASTER KEY IS EMPTY", "A password is required for saving new addresses", 5500, "error"); + return false; + } + + var SAFE_ACCOUNTS = []; + for (ea = 0; ea < ECO_EXTRA_ACCOUNTS.length; ++ea) { + if (ECO_EXTRA_ACCOUNTS[ea].account._privateKey != undefined) { + ECO_EXTRA_ACCOUNTS[ea].account.encrypt(MASTER_KEY_WALLET).then(encryptedAccount => { + var restoredID = getIDFromExtraAccount(encryptedAccount.address); + SAFE_ACCOUNTS.push({ + key: encryptedAccount.encrypted, + label: ECO_EXTRA_ACCOUNTS[restoredID].label, + print: ECO_EXTRA_ACCOUNTS[restoredID].print + }); + setLocalStorage("mySafeEncryptedExtraAccounts", JSON.stringify(SAFE_ACCOUNTS)); + }).catch(err => { + console.error(err); + swal2Simple("Encryption error", "Error when encripting extra accounts!", 5500, "error"); + }); + } else { SAFE_ACCOUNTS.push({ - key: encryptedAccount.encrypted, - label: ECO_EXTRA_ACCOUNTS[restoredID].label, - print: ECO_EXTRA_ACCOUNTS[restoredID].print + key: ECO_EXTRA_ACCOUNTS[ea].account.address, + label: ECO_EXTRA_ACCOUNTS[ea].label, + print: ECO_EXTRA_ACCOUNTS[ea].print }); setLocalStorage("mySafeEncryptedExtraAccounts", JSON.stringify(SAFE_ACCOUNTS)); - }).catch(err => { - console.error(err); - swal2Simple("Encryption error", "Error when encripting extra accounts!", 5500, "error"); - }); - } else { - SAFE_ACCOUNTS.push({ - key: ECO_EXTRA_ACCOUNTS[ea].account.address, - label: ECO_EXTRA_ACCOUNTS[ea].label, - print: ECO_EXTRA_ACCOUNTS[ea].print - }); - setLocalStorage("mySafeEncryptedExtraAccounts", JSON.stringify(SAFE_ACCOUNTS)); + } } + } else { + localStorage.removeItem("mySafeEncryptedExtraAccounts"); } } @@ -91,7 +122,7 @@ function btnWalletClean() { //ECO_WALLET = ECO_WALLET.filter( ( el ) => !ECO_EXTRA_ACCOUNTS.includes( el ) ); ECO_WALLET = DEFAULT_WALLET; ECO_EXTRA_ACCOUNTS = []; - setLocalStorage("mySafeEncryptedExtraAccounts", JSON.stringify(ECO_EXTRA_ACCOUNTS)); + localStorage.removeItem("mySafeEncryptedExtraAccounts"); drawPopulateAllWalletAccountsInfo(); } diff --git a/index.html b/index.html index 0816fbf9..6ef4ed5d 100644 --- a/index.html +++ b/index.html @@ -1490,6 +1490,8 @@

+ +