简体   繁体   中英

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

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

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 ).

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

Thier 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM