Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create private key and public key with web3?

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();
like image 445
Adharsh Chottu Avatar asked Sep 15 '25 08:09

Adharsh Chottu


1 Answers

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);
like image 121
Petr Hejda Avatar answered Sep 16 '25 22:09

Petr Hejda