【问题标题】:How to create private key and public key with web3?如何使用 web3 创建私钥和公钥?
【发布时间】:2021-12-16 07:24:26
【问题描述】:

我想创建一个私钥和公钥,基本上是使用 web3 在以太坊中的一个新帐户。但我想生成一个长度为 32 字节的私钥。有谁知道如何生成相同的? 使用以下代码,但无法生成 32 字节长的私钥。

web3.eth.accounts.create();

【问题讨论】:

  • UnhandledPromiseRejectionWarning:错误:私钥长度必须为 32 个字节。

标签: javascript ethereum web3 web3js


【解决方案1】:

以太坊使用 32 字节(即 64 个十六进制字符)长的私钥。

您可以在privateKey 属性中访问使用accounts.create() 方法(docs) 生成的私钥。

const account = web3.eth.accounts.create();
console.log(account.privateKey);

【讨论】:

    【解决方案2】:

    web3.eth.accounts.create()
    
    address: '0xB9b1415Dbd1B3C9a5890433DF8B2b389445CC6Fa',
      privateKey: '0x498262a4afde2bdd9e1bef3aafc6442ec36ccf167e527457a211118de93346ba',
      signTransaction: [Function: signTransaction],
      sign: [Function: sign],
      encrypt: [Function: encrypt]
      
      
      
      

    私钥在这里'0x498262a4afde2bdd9e1bef3aafc6442ec36ccf167e527457a211118de93346ba' 这是 64 十六进制格式。

    所以 32 字节长度的私钥498262a4afde2bdd9e1bef3aafc6442ec36ccf167e527457a211118de93346ba

    (去掉0x

    【讨论】:

    • 请勿使用此私钥和公钥传输代币,因为私钥在此公共域中可用,您可能会遭受资产损失
    猜你喜欢
    • 2019-02-28
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    相关资源
    最近更新 更多