1

I'm trying to design a user interface for private blockchain using Ethereum to test my contracts. So, users with different password and account address login.

I'm aware of this question, but it was posted a long time ago.


I want a way to alert a user when it put in incorrect password/account_address to unlock its account. But the problem is that,

web3.personal.unlockAccount(account,password)

doesn't return false, when it cannot unlock.

Question: How can I alert a user when it inserts wrong password or address?

In my HTML/.js file I have a login like this:

var password="";
var account="";
// user inserts its password and account_address using "onclick"
// then logIn() is called


function logIn(){

 var pass= document.getElementById("pass").value;
 var addrx =document.getElementById("addrx").value;
 //var myaddress = document.getElementById("myaddress").value;
 password=pass;
 account= addrx;
 if (!web3.personal.unlockAccount(account,password)) // here is where I want to detect incorrect inputs. 
 alert("Password/address is incorrect");
}
Aydin
  • 2,117
  • 6
  • 25
  • 41

1 Answers1

1

I'm not sure how good my suggestion are gonna be. If I had to face your problem I would try these;

1.Use web3.eth.sign(dataToSign, address [, callback]) : As documented here,

Function - (optional) Optional callback, returns an error object as first parameter and the result as second.

You can try to sign a piece of data with the account that supposed to be unlocked and catch the error object that's thrown when the account is locked. This means the mismatch of account and the password provided.

OR

  1. Trying to send a transaction with the account supposed to unlocked
    (This suggestion only valid since you refer to private chain, if it live chain this may cost ETH)

    And as in the previous method catch the error if the account is not unlocked. Which has been clearly pointed in this answer of the question link you have mentioned.
function isAccountLocked(account) {
    try {
        web3.eth.sendTransaction({
            from: account,
            to: account,
            value: 0
        });
        return false;
    } catch (err) {
        return (err.message == "authentication needed: password or unlock");
    }
}

function unlockAccountsIfNeeded(accounts, passwords) { for (var i = 0; i < accounts.length; i++) { if (isAccountLocked(accounts[i])) { console.log("Account " + accounts[i] + " is locked. Unlocking") web3.personal.unlockAccount(accounts[i], passwords[i]); } } }

Hope this helps!

Achala Dissanayake
  • 5,819
  • 15
  • 28
  • 38