Ethereum 专用网络:web3.eth.getAccounts()始终发送空数组

Ethereum 专用网络:web3.eth.getAccounts()始终发送空数组,ethereum,web3js,web3,Ethereum,Web3js,Web3,我正在运行一个专用以太坊网络。我确实使用https://aws.amazon.com/blockchain/templates/ 整个设置已经完成。AWS上的设置看起来很正常。现在,我正在尝试创建帐户并检索所有这些帐户。为此,我使用以下方法 Web3Service.js var Web3 = require('web3'); var web3 = new Web3(new Web3.providers.HttpProvider(process.env.NETWORK_URL)); export

我正在运行一个专用以太坊网络。我确实使用
https://aws.amazon.com/blockchain/templates/

整个设置已经完成。AWS上的设置看起来很正常。现在,我正在尝试创建帐户并检索所有这些帐户。为此,我使用以下方法

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();
};
/**
 *
 * 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 newAccount = await  web3Service.createAccount();
console.log('newAccount ', newAccount);

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

var accounts = await  web3Service.getAccounts();
console.log('accounts ', accounts);
我根本没有面临任何错误。但是在
web3Service.getAccounts()的响应中它总是空的
[]
数组

我已经验证了Etherium的设置。所有节点都工作正常


您可以在这里找到整个代码库:

web3.eth.accounts.create
将为您提供以太坊地址和私钥。为了使新帐户可用于节点,必须将新帐户信息存储在节点的密钥库中

当您调用
create
时,您将得到一个如下()的对象:


使用
加密
功能生成密码。这是需要与节点一起存储的内容,以便通过
web3.eth.getAccounts
检索。根据节点客户端、操作系统以及启动节点时是否覆盖密钥库位置(例如,Linux上的默认Geth位置为
~/.ethereum/keystore
),位置会有所不同。

找到解决方案后:

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();
};
/**
 *
 * 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 newAccount = await  web3Service.createAccount();
console.log('newAccount ', newAccount);

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

var accounts = await  web3Service.getAccounts();
console.log('accounts ', accounts);
更新资料来源:

他们没有明确地对密钥库做任何事情


使用此--rpcapi db、eth、net、web3、personal标志启动Geth。这是必要的。否则,您将面临错误。

hmm。您的意思是在创建帐户后,我必须编写一些函数来生成密钥库,并将密钥库复制到`Geth location on Linux is~/.ethereum/keystore`?他们必须以某种方式自动实现这一点。有实用程序API可用于存储特定节点的密钥(例如,对于块菌…请注意,这不是我的回购协议,我不担保。我只是发布了我找到的第一个。),但我不确定是否有一个通用的帐户可用于所有节点客户端。当您使用
web3.eth.personal.newAccount
时,您将无法再访问该帐户的私钥,这将导致以后出现其他问题(请参阅)。此外,向Geth节点公开个人API会带来安全风险。虽然您的答案可能在创建帐户和通过
getAccounts
检索帐户时起作用,但您也引入了一些其他潜在问题。是的,这是正确的。但我仍然可以在需要时使用新帐户签署交易。我的场景需要节点上的一个帐户才能与节点上的另一个帐户进行交易。这就是为什么我找到了一个简单的方法来获得个人帐户。