【问题标题】:How do I generate an Ethereum public key from a known private key using Python如何使用 Python 从已知私钥生成以太坊公钥
【发布时间】:2023-04-07 02:23:01
【问题描述】:

我有兴趣使用 Python 从私钥生成以太坊公钥。我试过谷歌搜索并找到了一些资源,但这些都是 JS 没有使用 Python 本身。

【问题讨论】:

标签: python ethereum


【解决方案1】:

公钥与地址

帐户的地址是公钥的 keccak256 的最后 20 个字节。以太坊中的大多数任务都需要地址而不是公钥。

获取公钥

使用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'

获取地址

以防万一问题是想询问地址...

generate the address from scratch 有更简单的方法,但由于我们已经完成了 eth-keys 设置,所以这是一个单行:

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

【讨论】:

  • 很好的信息。我以前认为eth地址也是公钥
  • 非常感谢这回答了我的问题:)
【解决方案2】:

评论太长了。 我不确定以太坊是如何工作的,以及你面临的具体问题是什么。

但是一个快速的谷歌给了我这些链接

A reddit answer to something similar you are asking

被引用的链接似乎坏了,所以我做了一些回溯,这里是probable link to the referred code

python library for ethereum

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 2019-08-15
    • 2022-01-13
    • 2019-10-12
    • 1970-01-01
    • 2011-07-11
    相关资源
    最近更新 更多