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");
}
  • what is the problem you see in the solution given in the link you have provided that you say ' was posted long time ago',? – Achala Dissanayake Sep 21 '17 at 21:36
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!

  • thanks for the answer! When I run the 1st suggestion an error shows up: Error: invalid address at web3.js:3879:15, etc. I'm wondering if I need to include anything in rpcapi ? (As I'm using a private chain) – Ay. Sep 22 '17 at 10:10
  • you unlocked account and tried? – Achala Dissanayake Sep 22 '17 at 10:36
  • can you try this and post what returns web3.eth.personal.sign(dataToSign, address, password [, callback])? – Achala Dissanayake Sep 22 '17 at 10:57
  • Shall I try exactly command you stated or I shall replace address, password and dataToSign with my values? – Ay. Sep 22 '17 at 11:01
  • Of course you have to replace – Achala Dissanayake Sep 22 '17 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.