首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EthereumJS:如何从私钥中获取公钥

EthereumJS:如何从私钥中获取公钥
EN

Ethereum用户
提问于 2017-01-09 17:28:26
回答 2查看 19K关注 0票数 18

我试图使用EthereumJS库创建一个私钥及其相应的公钥。到目前为止,我已经能够使用keythereum.create()方法创建私钥。尽管如此,我还是发现自己在处理公钥时被困住了。

我听说使用椭圆曲线数字签名算法(ECDSA),可以从它的私钥中获得Ethereum公钥(64字节长)。问题是如何使用EthereumJS库对此过程进行编码。也许有一些简单的方法来导出公钥,但我找不到它。任何帮助都将不胜感激。

我在以前的StackExchange问题上发现了一些有限的信息,但我更希望有一个侧重于EthereumJS的答案:

以太地址是如何产生的?

从私钥创建密钥文件和地址

谢谢!

EN

回答 2

Ethereum用户

发布于 2017-01-10 08:08:51

电子钱包可用于从私钥获取公钥:

代码语言:javascript
复制
> const hdkey = require('ethereumjs-wallet/hdkey')
> const privateKey = hdkey.fromMasterSeed('random')._hdkey._privateKey
> const Wallet = require('ethereumjs-wallet').default
> const wallet = Wallet.fromPrivateKey(privateKey)
> wallet.getPublicKeyString()
'0x11f2b30c9479ccaa639962e943ca7cfd3498705258ddb49dfe25bba00a555e48cb35a79f3d084ce26dbac0e6bb887463774817cb80e89b20c0990bc47f9075d5'
> wallet.getPublicKey()
<Buffer 11 f2 b3 0c 94 79 cc aa 63 99 62 e9 43 ca 7c fd 34 98 70 52 58 dd b4 9d fe 25 bb a0 0a 55 5e 48 cb 35 a7 9f 3d 08 4c e2 6d ba c0 e6 bb 88 74 63 77 48 ... >

另一种选择是使用空灵 (内部由ethereumjs使用):

代码语言:javascript
复制
> const util = require('ethereumjs-util')
> util.privateToPublic(privateKey)
<Buffer 11 f2 b3 0c 94 79 cc aa 63 99 62 e9 43 ca 7c fd 34 98 70 52 58 dd b4 9d fe 25 bb a0 0a 55 5e 48 cb 35 a7 9f 3d 08 4c e2 6d ba c0 e6 bb 88 74 63 77 48 ... >

还有一个选择是secp256k1

代码语言:javascript
复制
> const secp256k1 = require('secp256k1')
> secp256k1.publicKeyCreate(privateKey, false).slice(1)
<Buffer 11 f2 b3 0c 94 79 cc aa 63 99 62 e9 43 ca 7c fd 34 98 70 52 58 dd b4 9d fe 25 bb a0 0a 55 5e 48 cb 35 a7 9f 3d 08 4c e2 6d ba c0 e6 bb 88 74 63 77 48 ... >

slice(1)是删除类型字节,该字节被硬编码为04 ethereum。

票数 17
EN

Ethereum用户

发布于 2021-12-27 08:50:06

这是正确的答案。你必须使用const wallet = Wallet['default'].fromPrivateKey(privateKeyBuffer);

石竹(用现有的私钥生成Ethereum钱包)

票数 2
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/11253

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档