简体   繁体   English

如何使用 web3 创建私钥和公钥?

[英]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.我想创建一个私钥和公钥,基本上是使用 web3 在以太坊中创建一个新帐户。 But I want to generate a private key that is 32 bytes in length.但我想生成一个长度为 32 字节的私钥。 Does anyone know how to generate the same?有谁知道如何生成相同的? Used the following code, but not able to generate a 32 bytes long private key.使用了以下代码,但无法生成 32 字节长的私钥。

 web3.eth.accounts.create();

Ethereum uses 32 bytes (which is 64 hex characters) long private keys.以太坊使用 32 字节(即 64 个十六进制字符)长的私钥。

You can access the private key generated using the accounts.create() method ( docs ) in the privateKey property.您可以访问使用privateKey属性中的accounts.create()方法 ( docs ) 生成的privateKey

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.私钥在这里是'0x498262a4afde2bdd9e1bef3aafc6442ec36ccf167e527457a211118de93346ba',这是 64 位十六进制格式。

so the 32 bytes length private key is 498262a4afde2bdd9e1bef3aafc6442ec36ccf167e527457a211118de93346ba所以32 字节长度的私钥498262a4afde2bdd9e1bef3aafc6442ec36ccf167e527457a211118de93346ba

(remove the 0x ) (删除0x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM