代码之家  ›  专栏  ›  技术社区  ›  raimtoon

web3.eth.accounts.create方法实际上没有创建新帐户

  •  0
  • raimtoon  · 技术社区  · 6 年前

    我试图在专用网络中通过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创建 ?

    我感谢你的帮助。

    1 回复  |  直到 6 年前
        1
  •  2
  •   leberknecht    6 年前

    如果我做对了, web.eth.accounts.create 是一种创建帐户而不将其存储在本地节点上的方法,因此它基本上是一种在不将任何内容存储在密钥库中的情况下即时获取有效密钥对的方法)

    web3.eth.personal.newAccount() 如果在geth节点上激活了个人API(这是ganache的默认行为,使用 geth 你需要通过激活它 geth --dev/testnet --rpc --rpcapi eth,web3,personal (注意:当然,您应该非常小心地允许在mainnet上使用个人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