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

白昼怎懂夜的黑 提交于 2020-01-02 09:58:16

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/50116650/private-network-web3-eth-getaccounts-always-send-empty-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!