3

I deploying contract using Truffle and got an error Error: authentication needed: password or unlock. I already checked related questons and found only ugly hacks like "unlock desired account directly on node using web3.personal.unlockAccount". But I do not want to connect to my node each time (I have 3 test networks in my truffle.js) when I want to deploy new version.

My question is: is there any way to specify password in truffle infrastructure to unlocks account specified in field from under the network item? password or pass does not works.

  • 1
    With truffle you can use a providerengine like this truffleframework.com/tutorials/using-infura-custom-provider, but with your own node. – Ismael Aug 29 '17 at 17:04
  • 1
    @Ismael thank you for reply, but Infura provides very poor set of RPC calls. For instance, with Infura you cannot use even subscription because each "subscribe" call preceded with "createFilter" one that does not supported with Infura. I already tried it. – Alex G.P. Aug 30 '17 at 3:19
  • It should work the same if you use your own url to the geth instance instead of the infura one. – Ismael Aug 30 '17 at 4:52
  • It checked this article twice. It looks like absolutely useless for my case. Could you please point me where exactly in your article specified account unlocking process? I see only wallet derivation, connection to remote node and... that's all. – Alex G.P. Aug 30 '17 at 8:51
  • To use a UTC file instead of the HD wallet you should do something like const EthereumWallet = require('ethereumjs-wallet'); const jsonWallet = fs.readFileSync('<<path to UTC file>>'); const parsedWallet = JSON.parse(jsonWallet); const wallet = EthereumWallet.fromV3(parsedWallet, '<<YOUR_PASSWORD_GOES_HERE>>'); – Ismael Aug 30 '17 at 19:27
6

Ok, finally I added some code to unlock account on the fly. I do not want to store password in truffle.js (but it is possible), so doing next things:

Changed 1_initial_migration.js this way:

const Web3 = require('web3');

const TruffleConfig = require('../truffle');

var Migrations = artifacts.require("./Migrations.sol");


module.exports = function(deployer, network, addresses) {
  const config = TruffleConfig.networks[network];

  if (process.env.ACCOUNT_PASSWORD) {
    const web3 = new Web3(new Web3.providers.HttpProvider('http://' + config.host + ':' + config.port));

    console.log('>> Unlocking account ' + config.from);
    web3.personal.unlockAccount(config.from, process.env.ACCOUNT_PASSWORD, 36000);
  }

  console.log('>> Deploying migration');
  deployer.deploy(Migrations);
};

And instead of calling truffle deploy I am running it like:

ACCOUNT_PASSWORD=MySup4P@ssw0rd truffle deploy

As you can see node url and port are taken from config, so it is possible to add password fields there, then check in migration for "callability" i.e.:

...
password: function() { return process.env.ACCOUNT_PASSWORD }
...

-- same thing but password logic will be moved to truffle.js

  • You do not want to store the password in truffle.js, but are fine with having it in your environment and console command history? – WBT Jul 31 at 15:46

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.