最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create private key and public key with web3? - Stack Overflow

programmeradmin0浏览0评论

I want to create a private key and public key, basically a new account in Ethereum using web3. But I want to generate a private key that is 32 bytes in length. Does anyone know how to generate the same?

I used the following code, but not able to generate a 32 bytes long private key.

web3.eth.accounts.create();

I want to create a private key and public key, basically a new account in Ethereum using web3. But I want to generate a private key that is 32 bytes in length. Does anyone know how to generate the same?

I used the following code, but not able to generate a 32 bytes long private key.

web3.eth.accounts.create();
Share Improve this question edited Oct 13, 2022 at 21:14 TylerH 21.1k79 gold badges79 silver badges114 bronze badges asked Nov 1, 2021 at 15:21 Adharsh ChottuAdharsh Chottu 1452 gold badges5 silver badges20 bronze badges 1
  • 1 UnhandledPromiseRejectionWarning: Error: Private key must be 32 bytes in length. – Adharsh Chottu Commented Nov 2, 2021 at 4:56
Add a ment  | 

2 Answers 2

Reset to default 2

Ethereum uses 32 bytes (which is 64 hex characters) long private keys.

You can access the private key generated using the accounts.create() method (docs) in the privateKey property.

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]

the private key is here '0x498262a4afde2bdd9e1bef3aafc6442ec36ccf167e527457a211118de93346ba' this is of a 64 hex format.

so the 32 bytes length private key is 498262a4afde2bdd9e1bef3aafc6442ec36ccf167e527457a211118de93346ba

(remove the 0x)

发布评论

评论列表(0)

  1. 暂无评论