Understanding Private/Public Keys and Addresses in Ethereum Using Python
This article explains the relationship between private keys, public keys, and addresses in Ethereum, describes the signing and verification processes, and provides Python code examples using the eth_account library to generate keys, sign messages, verify signatures, and derive addresses.
The author, a master's graduate in artificial intelligence, introduces blockchain cryptography concepts and demonstrates how to work with Ethereum keys using Python.
1. Relationship between private key, public key, and address – Private and public keys are generated by elliptic curve cryptography; the public key cannot be reverse‑engineered to obtain the private key. The public key is used to verify signatures, while the address is a shortened, hashed representation of the public key obtained via SHA‑256, RIPEMD‑160, and Base58 encoding.
2. Encryption flow – The private key signs a message (producing a signature). The counterpart uses the sender’s public key to verify the signature, ensuring the message has not been tampered with.
3. Python implementation (Ethereum)
Generating keys
<code>from eth_account import Account
from eth_utils.hexadecimal import encode_hex
from eth_account.messages import encode_defunct
import json
def get_key(key):
"""
获取公钥私钥
:return: 返回公钥、私钥
"""
ac1 = Account.create(key)
return encode_hex(ac1.key), ac1.address
</code>The generated output includes a private key, public key, and address (shown as example values).
Signing a message
<code>def message_sign(text, prv_key):
"""
基于私钥获取签名
:param text: 待签名的文本
:param prv_key: 私钥
:return: 签名
"""
try:
message = encode_defunct(text=text)
result = Account.sign_message(message, prv_key)
result = str(result)
result = result[result.find("'signature':"):result.find(')})')]
return result[result.find("(") + 2:].replace("'", '')
except:
return '私钥格式不对'
</code>The function returns a hexadecimal signature string.
Verifying a signature
<code>def verifity(text, signature, address):
"""
验证签名
:param text: 原文本
:param signature: 签名
:param address: 公钥
:return: 验证结果
"""
try:
message = encode_defunct(text=text)
address_new = Account.recover_message(message, signature=signature)
if address == address_new:
return '验证一致'
else:
return '验证失败'
except:
return '格式错误'
</code>The verification function compares the recovered address with the expected address and reports consistency.
Deriving an address from a private key
<code>def recover_address(prv_key):
"""
基于私钥推出地址
:param prv_key: 私钥
:return: 地址
"""
try:
acct = Account.from_key(prv_key)
return acct.address
except:
return '格式错误'
</code>This demonstrates that the private key can be used to compute the corresponding address.
4. Conclusion – The tutorial uses the eth_account Python package, which can be installed via pip install eth_account . The library also supports additional features such as address derivation from transaction hashes and public‑key encryption.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.