6

I have a test that exercises a contract function across different accounts. How do I unlock these accounts in the context of the test?

contract('MyContract', function (accounts) {

    it("should perform differently for different accounts", function (done) {

        var thisContract = MyContract.deployed();

        thisContract.addAccounts([accounts[1], accounts[2]], {from: accounts[0]} ).then(
            function (tx_id) {
              /* this works because I manual unlock acc 0 on geth */
                return thisContract.doSomething({from: accounts[0]});
            }).then(
            function (tx_id) {
              /* is there something here I can do to unlock acc 1? */
                return thisContract.doSomething({from: accounts[1]});
            }).then(
            function (tx_id) {
              /* then here... */
                return thisContract.doSomething({from: accounts[2]};
                done();
            }
        ).catch(done);

    });

});
2
  • Did you ever figure this out? I'm using testrpc and need to unlock my contract's account to test a function that sends to an address Jun 7, 2017 at 14:11
  • Hi @ElliottMcNary . If you are using testrpc you do not have to unlock accounts. On a geth node console, you do that with personal.newAccount("passphrase”) and personal.unlockAccount(address, "password").
    – Interition
    Jun 8, 2017 at 6:53

1 Answer 1

4

You cannot unlock accounts from your code. It would be an issue if a code can unlock your accounts.

It seems like you are writing unit tests. Instead of running geth, you might want to try using testrpc. Accounts in testrpc do not need to be unlocked before use.

6
  • I have been using TestRPC but ran into issues because it does not truly replicate the behavior of a node itself :-) Chicken and egg.
    – Interition
    Apr 16, 2016 at 22:23
  • You can also try to unlock multiple accounts on geth before you run your unit tests.
    – uzyn
    Apr 17, 2016 at 3:41
  • Hi uzyn, As you cannot have someone manually unlocking your account before communicating with the smartcontract, how does current dapps platform make it work if you cannot unlock accounts from your code? Thanks !
    – fabdarice
    Jun 11, 2017 at 21:13
  • @fabdarice There are 2 ways to do that: 1. unlock accounts at the node (geth/parity); or 2. sign your tx at your code and only broadcast the signed tx at node.
    – uzyn
    Jun 13, 2017 at 1:46
  • could you maybe expand a little bit of both solutions? Sorry I'm kinda new to this. I'm personally interested in the case where any user can use your platform to make a transaction. That would require their private key and password to unlock their account, but I don't want them to feel unsafe about doing so.
    – fabdarice
    Jun 13, 2017 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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