On web3 0.20.1, I am using Infura to deploy a contract to Rinkeby. This probably requires me to add a wallet to web3.accounts to provide the gas needed.

When I ran the following nodejs code,

Web3 = require('web3')
var web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/nyxynyx-api-key"))
var privateKey = 'nyxynyx-private-key'
web3.eth.accounts.wallet.add("0x" + privateKey);

var contractCode = '60606-contract-code-here';
var abi = [{"constant":false,"inputs":[{"name":"givenNumber","type":"uint8"}],"name":"setNumber","outputs":[],"type":"function"},{"constant":true,"inputs":[{"name":"givenNumber","type":"uint8"}],"name":"guessNumber","outputs":[{"name":"","type":"bool"}],"type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"from","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[],"name":"SetNumber","type":"event"}];
GuessNumber = web3.eth.contract(abi)
var contractInstance = GuessNumber.new({from: web3.eth.accounts[0], gas: 200000, data: '0x' + contractCode});

the following error is encountered:

web3.eth.accounts.wallet.add("0x" + privateKey);

TypeError: Cannot read property 'add' of undefined

Is this because web3.eth.accounts.wallet.add is a web3 v1.0 function? If it is, what is the equivalent code needed in v0.20.1?

4 Answers 4


First create a wallet object. first parameter is number of accounts which you want and the second parameter is 32 byte random array as an entropy

var wallet = w3.eth.accounts.wallet.create(0, utils.entropy())

then add new account to your created wallet with

var newAccount = w3.eth.accounts.privateKeyToAccount(textPrivateKey.text)

Another solution to your problem

We can also use this method to add wallet in our eth.wallets

Points to be look at :-

  1. We are taking values of interface and bytecode from another file which is having our contract code compiled in it.
  2. hdwalletprovider input your 12 word mnemonic and rinkeby address

`const hdwalletprovider=require('truffle-hdwallet-provider'); const Web3=require('web3'); const {interface,bytecode}=require('./compile');

const provider=new hdwalletprovider(
    'hello blue cat eng cook name version brave entire sail trumpet scrub', // 12 word mnemonic
    'https://rinkeby.infura.io/v3/5d5eaf76b6a84578a245e058b870a8aa' );

const web3=new Web3(provider);

const deploy=async() => {
    const accounts= await web3.eth.getAccounts();

   const result=await new web3.eth.Contract(JSON.parse(interface))
   .deploy({data : bytecode})
   .send({gas: '10000000', from: accounts[0] });



Web3 v1.0 is far superior in my experience, so I recommend switching over when convenient.


If you are using v1.0, then try using web3.eth.accounts.create(\*Passcode here*\); to create new wallets

Your Answer

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

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