# How do I manage unlocking accounts in a test in Truffle

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);

});

});

• 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 – Elliott McNary Jun 7 '17 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 '17 at 6:53

## 1 Answer

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.

• 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 '16 at 22:23
• You can also try to unlock multiple accounts on geth before you run your unit tests. – uzyn Apr 17 '16 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 '17 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 '17 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 '17 at 21:23