When I run my contract deploy code in a browser (even if MetaMask is installed), I get this error:

authentication needed: password or unlock

The code follows:

var owner = $('#account').val();
var contract = web3.eth.contract(abi);
var contractInstance = contract.new(
    tokenName, tokenSymbol, { data: bytecode, from: owner, gas: 6721975 }

Please, please, explain how to do authentication for the user to be able to deploy my contract.

web3.js 0.20.6.

I connect to network with the following code:

    var provider = new Web3.providers.HttpProvider('http://localhost:8545');
    var web3 = new Web3(provider);
  • If you're using MetaMask, I believe you shouldn't see that message. Can you share the code where you instantiate web3? My guess is you're not actually using MetaMask's provider and are still connecting to some other kind of node (maybe a local geth/parity instance) that requires a password to unlock the account.
    – user19510
    Apr 5 '18 at 0:17
  • @smarx Your guess is right: I connect MetaMask to my local Geth node and connect my Web app (which I am debugging) to the same Geth node. Should I connect MetaMask to Get and my Web app to MetaMask? (How to connect to MetaMask?)
    – porton
    Apr 5 '18 at 0:56
  • @smarx I've edited the question as you requested
    – porton
    Apr 5 '18 at 0:57

To use MetaMask, you need code that looks like this:

var web3 = new Web3(web3.currentProvider);

(MetaMask injects a web3 global variable that has the right provider attached.)

To have MetaMask connect to your local geth node, just click the drop-down in the MetaMask UI that lets you choose your network. One of the options should be "localhost 8545." If you're not on that port, you can choose "custom RPC" and specify a URL.

  • OK, now it seems to work with MetaMask. But how to do it for users without MetaMask?
    – porton
    Apr 5 '18 at 11:48
  • For users not running MetaMask (or Mist or some other web3 provider), I suppose they would need to run a local node (e.g. geth or parity) and keep an account there unlocked. But that's an extraordinary amount of trust for a user to place in your app, so hopefully users wouldn't do that.
    – user19510
    Apr 5 '18 at 12:04
  • Can't users without MetaMask just enter private key explicitly (without running a local node)?
    – porton
    Apr 5 '18 at 12:18
  • They could, although giving your app their private key is just as dangerous as giving you access to a node with that account unlocked. To use a private key, you would need code to sign transactions locally and then broadcast them to a public node via sendRawTransaction/sendSignedTransaction (varies between web3.js versions). But again, users shouldn't trust you with their private keys.
    – user19510
    Apr 5 '18 at 12:21

You can also set it up like so:

const assert = require('assert');
const ganache = require('ganache-cli');
const Web3 = require('web3');

const provider = ganache.provider();
const web3 = new Web3(provider);

const { interface, bytecode } = require('../compile');

let accounts;
let inbox;

beforeEach(async () => {
  // Get a list of accounts
  accounts = await web3.eth.getAccounts();

  // Use one of those accounts to deploy contract
  inbox = await new web3.eth.Contract(JSON.parse(interface))
      data: bytecode,
      arguments: ['hi there']
    .send({ from: accounts[0], gas: '1000000' });


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.