2

I'm using web3.jp 1.0

How to connect (log in) my existing account (for example, created through myetherwallet) to make a transaction or call contract?

As I understand it, the list of accounts is provided by the RPC node, because when using testerpc in the Web3.eth.accounts, test accounts were available, but new ones were not added.

const Web3 = require('web3');
console.log(web3.eth.getAccounts()) // []

thank you!

3 Answers 3

3

To create new accounts on Ethereum you can use following instructions:

var Accounts = require('web3-eth-accounts');
var accounts = new Accounts('http://localhost:8545');
//Generates an account with public key and private key
web3.eth.accounts.create();
> {
    address: "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01",
    privateKey: "0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709",
    signTransaction: function(tx){...},
    sign: function(data){...},
    encrypt: function(password){...}
  }
1
  • 4
    User asked for adding existing account
    – realtebo
    Jan 22, 2020 at 9:11
3

Here's how you add an existing account, if you have the account's private key and password:

const account = web3.eth.accounts.privateKeyToAccount("0x" + YOUR_PRIVATE_KEY);
await web3.eth.personal.unlockAccount(account.address, YOUR_PASSWORD);
2

Creating accounts on ethereum -

  1. Make sure you have installed the web3 node module.
  2. If not then you can install it using npm install web3

Example-

var Web3 = require('web3')
var web3 = new Web3(Web3.givenProvider || 'http://localhost:7545')

//Now to create an account

web3.eth.accounts.create()

> {
    address: "0xF2CD2AA0c7926743B1D4310b2BC984a0a453c3d4",
    privateKey:"0xd7325de5c2c1cf0009fac77d3d04a9c004b038883446b065871bc3e831dcd098",
    signTransaction: function(tx){...},
    sign: function(data){...},
    encrypt: function(password){...}
}

For more information, refer to the web3.js documentation - https://web3js.readthedocs.io/en/1.0/web3.html

1
  • 3
    User asked for adding existing account
    – realtebo
    Jan 22, 2020 at 9:11

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.