如何预生成以太坊合约地址?Web3.py实现问题排查
问题解决:合约预期地址与实际部署地址不一致
- 核心问题:你使用的sender地址格式错误,RLP编码要求传入20字节的原始地址(bytes类型),而非字符串格式的地址,这是导致地址不匹配的根本原因,和
normalize_transaction无关。 - 修正步骤:
- 将字符串格式的sender地址转换为bytes(去除0x前缀后解码)
- 确保RLP编码的元素类型正确:[部署者地址bytes, nonce整数]
- 修正后的代码:
from eth_utils import to_bytes from rlp import encode sender = w3.eth.default_account # 把字符串地址转换为20字节的bytes类型 sender_bytes = to_bytes(hexstr=sender) expected_address = w3.keccak(encode([sender_bytes, 0]))[12:] print("Expected address:") print(expected_address.hex())
- 额外验证:可以通过
w3.eth.get_transaction_count(sender)确认部署前的nonce值,确保确实为0。
内容的提问来源于stack exchange,提问作者Goober117
相关产品推荐
相关产品推荐

