Skip to content

Commit

Permalink
Improving account logic and removing saving button. It should be auto…
Browse files Browse the repository at this point in the history
…matic
  • Loading branch information
vncoelho committed Dec 21, 2023
1 parent a5c5e41 commit f338b62
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 35 deletions.
29 changes: 19 additions & 10 deletions assets/eco-scripts/wallet_addNewAccounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@ function addAccountAndDraw(accountToAdd, labelToAdd) {
function addSafeAccount(accountToAdd, labelToAdd) {
//Asks for password if privatekey or wif or encrypted
if (MASTER_KEY_WALLET != "") {
swal2Simple("You have a master key", "This account will be linked to your password", 5500, "success");
swal2Simple("Your master key isalready defined", "This account will be linked to your password.\n Remember it!", 5500, "success");
addAccountAndDraw(accountToAdd, labelToAdd);
} else {
setMasterKey(() => {
addAccountAndDraw(accountToAdd, labelToAdd);
}, "Adding new account.");
}, "Restoring account from WIF/PrivateKey/EncryptedKey");
}
}

function setMasterKey(callback, labelToAdd) {
function setMasterKey(callback, labelToAdd, loading = false) {
var serializedHTML =
'<div class="input-group">' +
'<input id="input-newaccount-password-1" placeholder="password" class="form-control swal-input" type="password">' +
Expand All @@ -100,7 +100,7 @@ function setMasterKey(callback, labelToAdd) {
});

swalWithBootstrapButtons.fire({
title: "Enter your password for local storage (MasterKey) - " + labelToAdd,
title: "Define your Master Key for local storage.\n" + labelToAdd,
html: serializedHTML,
color: "#00AF92",
background: "#263A40",
Expand All @@ -121,9 +121,18 @@ function setMasterKey(callback, labelToAdd) {
}).then((result) => {
if (result.isConfirmed) {
callback();
}else{
swal2Simple("Be carefull!", "A password is needed for this action.", 0, "error");
callback();
} else {
// if loading user should not be able to save of add until loaded or cleanned
if (loading) {
// Disable Save
// Disable add new accounts
// Only enable load or clean
//$("#btnWalletSaveID")[0].disabled = true;
$("#btnAddNewAccount")[0].disabled = true;
$("#btnLoadSavedAccount")[0].disabled = false;
}
swal2Simple("Nothing will happen!!", "A password is needed for this action.", 0, "error");
//callback();
}
});
}
Expand All @@ -147,7 +156,8 @@ function tryToDecrypt(keyToAdd, labelToAdd) {
});

swalWithBootstrapButtons.fire({
title: "Enter your decription key. After that the MasterKey will be used to re-encrypt.",
title: "Let's try to decrypt your account. Enter your password.",
footer: "After that the MasterKey will be used to re-encrypt.",
html: serializedHTML,
color: "#00AF92",
background: "#263A40",
Expand All @@ -159,11 +169,10 @@ function tryToDecrypt(keyToAdd, labelToAdd) {
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);
//console.error(err);
Swal.update({ footer: "Decryption error. Password should be wrong." });
resolve(false); // Resolve with false if decryption fails
}
Expand Down
76 changes: 54 additions & 22 deletions assets/eco-scripts/wallet_localStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function getIDFromExtraAccount(addressToSearch) {
swal2Simple("Search error", "ERROR WHILE SEARCHING FOR ADDRESS", 5500, "error");
return -1;
}

function getIDFromExtraAccountStillEncrypted(baseEncrypted, encryptedToSearch) {
for (ea = 0; ea < baseEncrypted.length; ++ea)
if (baseEncrypted[ea].encrypted != undefined)
Expand All @@ -18,7 +19,6 @@ function getIDFromExtraAccountStillEncrypted(baseEncrypted, encryptedToSearch) {
return -1;
}


async function getExtraWalletAccountFromLocalStorage() {
var serializedAccountKeys = getLocalStorage("mySafeEncryptedExtraAccounts");

Expand All @@ -44,8 +44,8 @@ async function getExtraWalletAccountFromLocalStorage() {
} catch (err) {
console.error(err);
myRecreatedExtraAccounts = [];
$("#btnWalletSaveID")[0].disabled = true;
$("#btnNewWallet")[0].disabled = true;
//$("#btnWalletSaveID")[0].disabled = true;
$("#btnAddNewAccount")[0].disabled = true;
$("#btnLoadSavedAccount")[0].disabled = false;
MASTER_KEY_WALLET = "";
var footerMsg = "You can not save anything meanwhile. Try to clean or redefine password";
Expand All @@ -55,8 +55,8 @@ async function getExtraWalletAccountFromLocalStorage() {

//console.log(myRecreatedExtraAccounts)
if (myRecreatedExtraAccounts.length != 0) {
$("#btnWalletSaveID")[0].disabled = false;
$("#btnNewWallet")[0].disabled = false;
//$("#btnWalletSaveID")[0].disabled = false;
$("#btnAddNewAccount")[0].disabled = false;
$("#btnLoadSavedAccount")[0].disabled = true;
}

Expand All @@ -68,19 +68,23 @@ async function getExtraWalletAccountFromLocalStorage() {
function restoreWalletExtraAccountsLocalStorage() {
// Serialized Storage
var serializedKeys = getLocalStorage("mySafeEncryptedExtraAccounts");
var serializedKeysJson = JSON.parse(serializedKeys);
if (serializedKeys) {
//Since there are keys, clean button will be enabled
$("#btnWalletCleanID")[0].disabled = false;

var nAccounts = serializedKeysJson.length;
var nEncryptedAccounts = getNumberOfSafeEncryptedAccountsFromVector(serializedKeysJson);
//console.log("You have " + nAccounts + " account and " + nEncryptedAccounts + " encrypted accounts to be decrypted.");
var serializedKeysJson = JSON.parse(serializedKeys);
var nAccounts = serializedKeysJson.length;
var nEncryptedAccounts = getNumberOfSafeEncryptedAccountsFromVector(serializedKeysJson);
//console.log("You have " + nAccounts + " account and " + nEncryptedAccounts + " encrypted accounts to be decrypted.");

if (serializedKeys && MASTER_KEY_WALLET == "") {
setMasterKey(() => {
updateExtraAndEcoWallet();
}, "Restoring: " + nAccounts + " accounts (" + nEncryptedAccounts + " encrypted)");
} else {
console.error("restoreWalletExtraAccountsLocalStorage error. Trying to restore with a key already set. Not expected.")
//updateExtraAndEcoWallet();
if (MASTER_KEY_WALLET == "") {
setMasterKey(() => {
updateExtraAndEcoWallet();
}, "Restoring: " + nAccounts + " accounts (" + nEncryptedAccounts + " encrypted)", true);
} else {
console.error("restoreWalletExtraAccountsLocalStorage error. Trying to restore with a key already set. Not expected.")
//updateExtraAndEcoWallet();
}
}
}

Expand All @@ -103,6 +107,7 @@ function getNumberOfSafeEncryptedAccountsFromVector(mySafeExtraAccountsWallet) {
return nEncryptedAccounts;
}

/*
function btnWalletSave() {
if (MASTER_KEY_WALLET == "") {
setMasterKey(() => {
Expand All @@ -111,15 +116,18 @@ function btnWalletSave() {
} else {
extraWalletSave();
}
}
}*/

function extraWalletSave() {
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");
swal2Simple("MASTER KEY NOT FOUND", "A password is required for saving your accounts", 5500, "error");
return false;
}

// Since something is saved Clean will be enabled
$("#btnWalletCleanID")[0].disabled = false;

var SAFE_ACCOUNTS = [];
for (ea = 0; ea < ECO_EXTRA_ACCOUNTS.length; ++ea) {
if (ECO_EXTRA_ACCOUNTS[ea].account._privateKey != undefined) {
Expand All @@ -146,16 +154,40 @@ function extraWalletSave() {
}
} else {
localStorage.removeItem("mySafeEncryptedExtraAccounts");
$("#btnWalletCleanID")[0].disabled = true;
}
}

function btnWalletClean() {
const swalWithBootstrapButtons = Swal.mixin({
customClass: {
confirmButton: "btn btn-danger",
cancelButton: "btn btn-success"
},
buttonsStyling: false
});
swalWithBootstrapButtons.fire({
title: "You will delete all your extra accounts?",
text: "Be carefull. Your local storage for wallet will be empty.",
showCancelButton: true,
icon: "question",
confirmButtonText: "Yes, delete it!",
color: "#00AF92",
background: "#263A40",
}).then((result) => {
if (result.isConfirmed) {
ECO_WALLET = DEFAULT_WALLET;
ECO_EXTRA_ACCOUNTS = [];
localStorage.removeItem("mySafeEncryptedExtraAccounts");
drawPopulateAllWalletAccountsInfo();
swal2Simple("Deleted!", "All saved addresses were delete", 0, "success");
} else {
swal2Simple("Safe!", "Your local storage for accounts is preserved", 0, "success");
}
});

// This filter also worked
//ECO_WALLET = ECO_WALLET.filter( ( el ) => !ECO_EXTRA_ACCOUNTS.includes( el ) );
ECO_WALLET = DEFAULT_WALLET;
ECO_EXTRA_ACCOUNTS = [];
localStorage.removeItem("mySafeEncryptedExtraAccounts");
drawPopulateAllWalletAccountsInfo();
}


Expand Down
10 changes: 7 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -739,13 +739,17 @@ <h4 class="green-title">Wallet
<button type="submit" onclick="populateAllWalletData()" class="btn btn-info btn-sm"><i
class="fas fa-sync-alt"></i>
</button>
<!--
<button type="submit" onclick="btnWalletSave()" id="btnWalletSaveID" class="btn btn-success btn-sm">
Save <i class="fas fa-save"></i>
</button>
<button type="submit" onclick="btnWalletClean()" class="btn btn-danger btn-sm">
-->
<button type="submit" disabled onclick="btnWalletClean()" id="btnWalletCleanID"
class="btn btn-danger btn-sm">
Clean Extra <i class="fas fa-trash"></i>
</button>
<button type="submit" disabled onclick="restoreWalletExtraAccountsLocalStorage()" id="btnLoadSavedAccount" class="btn btn-danger btn-sm">
<button type="submit" disabled onclick="restoreWalletExtraAccountsLocalStorage()"
id="btnLoadSavedAccount" class="btn btn-danger btn-sm">
Load Saved Accounts <i class="fas fa-trash"></i>
</button>
</h4>
Expand Down Expand Up @@ -808,7 +812,7 @@ <h4 class="green-title">Transfer NEP17 Tokens
<div class="generalContainer">
<div class="standardContainer" id="RegisterAddress" style="margin-bottom: 30px;">
<h4 class="green-title">Import account
<button type="button" onclick="addWalletFromForm();" id="btnNewWallet"
<button type="button" onclick="addWalletFromForm();" id="btnAddNewAccount"
class="btn btn-success btn-sm"><i class="fas fa-user-plus"></i></button>
</h4>
<div class="container">
Expand Down

0 comments on commit f338b62

Please sign in to comment.