【问题标题】:web3.eth.accounts.create method doesn't actually create new accountweb3.eth.accounts.create 方法实际上并没有创建新帐户
【发布时间】:2018-07-25 07:11:39
【问题描述】:

我尝试在私有网络中通过 RPC 创建一个 eth 帐户。

到目前为止我所做的是:

  1. 启动 geth 节点并创建专用网络。
  2. 使用 web3 1.0.0、typescript 创建简单的 javascript 程序
  3. 运行并获得如下结果,但未创建帐户

代码:

const result = await web3.eth.personal.unlockAccount(senderId, senderPassword, duration)
if (result === true) {
    // const newAccountResult = await web3.eth.personal.newAccount('password')
    const newAccountResult = await web3.eth.accounts.create('user01')
    console.log(newAccountResult)
}

结果:

web3.eth.accounts.create 返回以下结果

{ address: '0xf10105f862C1cB10550F4EeB38697308c7A290Fc',
  privateKey: '0x5cba6b397fc8a96d006988388553ec17a000f7da9783d906979a2e1c482e7fcb',
  signTransaction: [Function: signTransaction],
  sign: [Function: sign],
  encrypt: [Function: encrypt] }

web3.eth.getAccounts 方法只返回 1 个帐户。

[ '0xaf0034c41928Db81E570061c58c249f61CFF57f2' ]

似乎web3.eth.accounts.create 方法已成功,因为结果包括帐户地址和私钥。 但我不明白为什么web3.eth.getAccounts 方法不包括创建的帐户。

我也通过控制台检查了geth,结果是一样的。

> eth.accounts
["0xaf0034c41928db81e570061c58c249f61cff57f2"]

eth.personal.newAccount 不起作用。 web3.eth.accounts.create之后我需要做什么吗?

感谢您的帮助。

【问题讨论】:

    标签: ethereum web3js


    【解决方案1】:

    如果我没记错的话,web.eth.accounts.create 是一种无需在本地节点上存储帐户即可创建帐户的方法,因此它基本上是一种无需在密钥库中存储任何内容即可即时获取有效密钥对的方法)

    web3.eth.personal.newAccount() 如果您在 geth 节点上激活了个人 API,则应该可用(这是 ganache 的默认行为,使用 geth 您需要通过 geth --dev/testnet --rpc --rpcapi eth,web3,personal 激活它(注意:您当然应该是非常小心地在主网上允许个人 API,确保 RPC 访问受到限制,因此只有您/特权用户可以访问它)

    (async () => {
        let newAccount = await web3.eth.personal.newAccount();
        console.log(newAccount);
        let accounts = await web3.eth.getAccounts();
        console.log(accounts);
    })();
    

    应该给出类似的东西

    0xb71DCf0191E2B90efCD2638781DE40797895De66
    [
        ...
    '0xb71DCf0191E2B90efCD2638781DE40797895De66' ]
    

    参考https://medium.com/@andthentherewere0/should-i-use-web3-eth-accounts-or-web3-eth-personal-for-account-creation-15eded74d0eb

    【讨论】:

    • 我之前尝试过newAccount 方法,但以某种方式失败,但再次尝试然后成功!谢谢!
    猜你喜欢
    • 2018-10-09
    • 2018-10-30
    • 2019-06-23
    • 2018-09-20
    • 1970-01-01
    • 2021-12-28
    • 2014-11-29
    • 2022-08-20
    相关资源
    最近更新 更多