Private Network : web3.eth.getAccounts() always send empty array
Asked Answered
P

2

2

I am running a private Ethereum network. I do use https://aws.amazon.com/blockchain/templates/

The entire setup has been done. Things look setup properly on AWS. Now, I am trying to create the account and retrieve all those accounts. For that, I am using methods as below.

Web3Service.js

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider(process.env.NETWORK_URL));

exports.getAccounts = function () {
    return web3.eth.getAccounts();
};

exports.createAccount = function () {
    return web3.eth.accounts.create();
};

app.js

var newAccount = await  web3Service.createAccount();
console.log('newAccount ', newAccount);

var accounts = await  web3Service.getAccounts();
console.log('accounts ', accounts);

I am not facing any errors at all. But in the response of the web3Service.getAccounts(); it's always empty [] array.

I have verified the Etherium setup. All nodes working perfectly.

You can find the entire codebase here : blockchain-node Sample entire codebase

Pacificia answered 1/5, 2018 at 12:40 Comment(0)
L
0

web3.eth.accounts.create will provide you with the Ethereum address and the private key. In order to make new accounts available to a node, you have to store the new account information in the node's keystore.

When you call create, you will get an object like this (from the docs):

web3.eth.accounts.create();
> {
    address: "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01",
    privateKey: "0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709",
    signTransaction: function(tx){...},
    sign: function(data){...},
    encrypt: function(password){...}
}

Use the encrypt function to generate the encrypted keystore. This is what needs to be stored with the node in order to be retrievable through web3.eth.getAccounts. The location is going to vary depending on node client, OS, and if you override the keystore location when starting the node (for example, the default Geth location on Linux is ~/.ethereum/keystore).

Lavonnelaw answered 1/5, 2018 at 16:5 Comment(2)
hmm. Do you mean after creating an account, I have to write some functions which do generate keystore and copy that keystore to ` Geth location on Linux is ~/.ethereum/keystore`? Their must be some way to do that automatically.Pacificia
There are utility APIs available for storing the key for specific nodes (ie, github.com/yondonfu/truffle-keystore-provider for Truffle...Note this is not my repo and am not vouching for it. I'm just posting the first one I found.), but I'm not sure if there's a universal one that will work with all node clientsLavonnelaw
P
0

After struggling found the solution :

Web3Service.js

/**
 *
 * Accounts Functions
 */

exports.createAccount = function () {
    /* *
     * Create Account Local Machine Only.
     * It will not return in web3.eth.getAccounts(); call
     */
    return web3.eth.accounts.create();
};

exports.createPersonalAccount = function (password) {
    /* *
     * Create Account in Node.
     * web3.eth.getAccounts();
     */
    return web3.eth.personal.newAccount(password);
};

app.js

var personalAccount = await web3Service.createPersonalAccount('123456789');
console.log('personalAccount ', personalAccount);

var accounts = await  web3Service.getAccounts();
console.log('accounts ', accounts);

Updated source : Working Source Code

There is no explicitly do anything with keystore.

Start your Geth using this --rpcapi db,eth,net,web3,personal flag. It is necessary. Otherwise, you will face the error.

Pacificia answered 2/5, 2018 at 6:45 Comment(2)
When you use web3.eth.personal.newAccount, you no longer have access to the private key for the account which will cause you other issues down the road (see https://mcmap.net/q/1329640/-what-is-the-difference-between-web3-eth-accounts-create-and-web3-eth-personal-newaccount/8474917). Also, exposing the personal API to your Geth node introduces security risks. While your answer may be functional in creating an account and retrieving it through getAccoounts, you are also introducing several other potential issues.Lavonnelaw
Yes, that is correct. But I can still sign the transaction using the new account when needed. My Scenario required an account on to Node to do the transaction with another account on Node. That is why I was finding an easy way to get the Personal account.Pacificia

© 2022 - 2024 — McMap. All rights reserved.