简体   繁体   English

如何使用 Python 从已知私钥生成以太坊公钥

[英]How do I generate an Ethereum public key from a known private key using Python

I'm interested in generating an Ethereum public key from a private key using Python.我有兴趣使用 Python 从私钥生成以太坊公钥。 I've tried googling around and found some resources but these are all JS nothing using Python itself.我试过谷歌搜索并找到了一些资源,但这些都是 JS 没有使用 Python 本身。

Public Key vs Address公钥与地址

An account's address is the last 20 bytes of the keccak256 of the public key.一个账户的地址是公钥的 keccak256 的最后 20 个字节。 Most tasks in Ethereum require the address instead of the public key.以太坊中的大多数任务都需要地址而不是公钥。

Getting the Public Key获取公钥

Install eth_keys with pip install eth-keys使用pip install eth-keys安装 eth_keys

from eth_keys import keys
from eth_utils import decode_hex

priv_key_bytes = decode_hex('0x44b9abf2708d9adeb1722dcc1e61bef14e5611dee710d66f106e356a111bef90')
priv_key = keys.PrivateKey(priv_key_bytes)
pub_key = priv_key.public_key

assert pub_key.to_hex() == '0xcabb8a3a73ea4a03d025a6ac2ebbbb19a545e4fb10e791ec9b5c942d77aa20760f64e4604cdfbec665435a382a8c9bfd560c6f0fca8a2708cda302f658368b36'

Getting the Address获取地址

Just in case the question was intending to ask about the address...以防万一问题是想询问地址...

There are simpler ways to generate the address from scratch , but since we've already done the eth-keys setup, this is a one-liner: 从头开始生成地址有更简单的方法,但由于我们已经完成了 eth-keys 设置,这是一个单行:

assert pub_key.to_checksum_address() == '0xa0784ba3fcea41fD65a7A47b4cc1FA4C3DaA326f'

Tested Python 3.7 code using pyethereum : 使用pyethereum测试Python 3.7代码:

from ethereum.utils import privtoaddr
pk = '44b9abf2708d9adeb1722dcc1e61bef14e5611dee710d66f106e356a111bef90'
pubkey = privtoaddr(pk).hex()
print(pubkey)
# a0784ba3fcea41fd65a7a47b4cc1fa4c3daa326f

To install pyethereum (tested in Ubuntu 16.04): 要安装pyethereum (在Ubuntu 16.04中测试):

sudo apt-get install libssl-dev build-essential automake pkg-config libtool libffi-dev libgmp-dev libyaml-cpp-dev
pip install ethereum

Too long for a comment.评论太长了。 I'm not exactly sure how ethereum works and what exact problem you are facing.我不确定以太坊的工作原理以及您面临的确切问题。

But a quick google gave me these links但是一个快速的谷歌给了我这些链接

A reddit answer to something similar you are asking 对您所问的类似问题的 reddit 答案

The link which is being referred seems broken, so I did some backtracking, and here is the probable link to the referred code被引用的链接似乎已损坏,所以我做了一些回溯,这是引用代码可能链接

python library for ethereum以太坊的python库

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

相关问题 如何在 Python 中使用以太坊私钥签署消息? - How can I sign message using an Ethereum private key in Python? 如何使用 python 从存储在 AWS 的 Secrets Manager 中的私钥生成 OpenSSH RSA 密钥? - How can I generate OpenSSH RSA key using python from the private key stored in Secrets manager in AWS? 使用 Python 生成私有/公共 SSH 密钥 - Generate private / public SSH key with Python 如何使用 python 中的公钥验证私钥 - How to validate a private key using a public key in python 有什么方法可以使用Python从私有密钥生成公共SSH密钥吗? - Is there any way to generate public SSH key from private one using Python? 我们如何使用python的密码库从私钥(受密码保护)中检索公钥? - How can we retrieve public key from private key ( protected with passphrase ) using python's Cryptography library? Python:如何从“ .cert”文件中提取公钥? - Python: How do I extract public key from a '.cert' file? 如何将以太坊地址转换为公钥 - How to convert ethereum addres to public key 使用python gnupg,我的私钥被作为公钥读入 - Using python gnupg, my private key is being read in as a public key 使用 python 从私钥获取公钥 OpenSSL - get public key from private key with python OpenSSL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM