如何使用 web3 创建私钥和 public 密钥?

How to create private key and public key with web3?

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

web3.eth.accounts.create();

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

您可以在privateKey属性.

中访问使用accounts.create()方法(docs)生成的私钥
const account = web3.eth.accounts.create();
console.log(account.privateKey);

web3.eth.accounts.create()

address: '0xB9b1415Dbd1B3C9a5890433DF8B2b389445CC6Fa',
  privateKey: '0x498262a4afde2bdd9e1bef3aafc6442ec36ccf167e527457a211118de93346ba',
  signTransaction: [Function: signTransaction],
  sign: [Function: sign],
  encrypt: [Function: encrypt]
  
  
  
  

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

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

(去掉0x)