Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Blockchain 5 min read

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

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

The generated output includes a private key, public key, and address (shown as example values).

Signing a message

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 '私钥格式不对'

The function returns a hexadecimal signature string.

Verifying a signature

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 '格式错误'

The verification function compares the recovered address with the expected address and reports consistency.

Deriving an address from a private key

def recover_address(prv_key):
    """
    基于私钥推出地址
    :param prv_key: 私钥
    :return: 地址
    """
    try:
        acct = Account.from_key(prv_key)
        return acct.address
    except:
        return '格式错误'

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.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

BlockchaincryptographyEthereumPrivateKeyPublicKey

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion