我正在运行一个私有的以太网络。我用的是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();
};
app.js
var newAccount = await web3Service.createAccount();
console.log('newAccount ', newAccount);
var accounts = await web3Service.getAccounts();
console.log('accounts ', accounts);
我根本没有遇到任何错误。但在web3Service.getAccounts();
的响应中,它始终是空的[]
数组。
我已经验证了Etherium设置。所有节点都工作正常。
你可以在这里找到完整的代码库:blockchain-node Sample entire codebase
发布于 2018-05-02 00:05:50
web3.eth.accounts.create
将为您提供以太地址和私钥。为了使新帐户可用于节点,您必须将新帐户信息存储在节点的密钥库中。
当你调用create
时,你会得到一个像这样的对象(from the docs):
web3.eth.accounts.create();
> {
address: "0xb8CE9ab6943e0eCED004cDe8e3bBed6568B2Fa01",
privateKey: "0x348ce564d427a3311b6536bbcff9390d69395b06ed6c486954e971d960fe8709",
signTransaction: function(tx){...},
sign: function(data){...},
encrypt: function(password){...}
}
使用encrypt
函数生成encrypted keystore。这是需要与节点一起存储的内容,以便可以通过web3.eth.getAccounts
检索。该位置将根据节点客户机、操作系统以及在启动节点时是否覆盖密钥库位置而有所不同(例如,Linux上的默认Geth位置是~/.ethereum/keystore
)。
发布于 2018-05-02 14:45:29
在苦苦挣扎之后找到了解决方案:
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);
它没有显式地使用keystore做任何事情。
使用这个--rpcapi db,eth,net,web3,personal标志启动Geth。这是必要的。否则,你将面临错误。
https://stackoverflow.com/questions/50116650
复制相似问题