当前位置: 代码网 > it编程>前端脚本>Python > 使用Python计算TRC20地址

使用Python计算TRC20地址

2025年02月13日 Python 我要评论
trc20地址介绍trc20地址是一种数字钱包地址,用于接收或发送tether加密货币。每个trc20地址由一组26到64个字符组成,通常以0x开头,后面跟着一系列大写字母和数字。如果您想要使用trc

trc20地址介绍

trc20地址是一种数字钱包地址,用于接收或发送tether加密货币。每个trc20地址由一组26到64个字符组成,通常以0x开头,后面跟着一系列大写字母和数字。如果您想要使用trc20地址进行交易,您需要将其提供给数字货币交易所或其他交易平台,并按照指示操作即可。

python计算trc20地址

完整代码

import hashlib
from eth_utils import keccak
import base58
import ecdsa
from tronpy.keys import privatekey


def test1():
    # 示例私钥,可替换为你自己的私钥
    private_key_hex = "your_private_key_hex_string"

    # 将十六进制私钥转换为字节形式
    private_key_bytes = bytes.fromhex(private_key_hex)

    # 使用 secp256k1 曲线生成私钥对象
    sk = ecdsa.signingkey.from_string(private_key_bytes, curve=ecdsa.secp256k1)

    # 从私钥派生公钥
    vk = sk.get_verifying_key()
    public_key_bytes = vk.to_string()
    public_key_point = vk.pubkey.point

    # hash the public key using keccak-256
    public_key_hash = keccak(public_key_bytes)

    # take the last 20 bytes of the hash and prepend 0x41
    # 添加 tron 地址前缀 (0x41)
    tron_prefix = b'\x41'
    prefixed_hash160 = tron_prefix + public_key_hash[-20:]

    # 对添加前缀后的结果进行两次 sha-256 哈希以获取校验和
    first_sha256 = hashlib.sha256(prefixed_hash160).digest()
    second_sha256 = hashlib.sha256(first_sha256).digest()

    # 取前 4 字节作为校验和
    checksum = second_sha256[:4]

    # 将前缀、哈希结果和校验和拼接
    full_payload = prefixed_hash160 + checksum

    # convert to hexadecimal string
    trc_address_hex = prefixed_hash160.hex()

    # 使用 base58 编码生成最终的 trc 地址
    trc_address = base58.b58encode(full_payload).decode()

    # 打印私钥(以十六进制字符串形式)
    print("私钥 (十六进制):", sk.to_string().hex())
    # 打印公钥(以十六进制字符串形式)
    print("公钥 (十六进制):", vk.to_string().hex())
    # 打印公钥对应的椭圆曲线上的点
    print("公钥对应的椭圆曲线上的点:")
    print("  x 坐标 (十六进制):", hex(public_key_point.x()))
    print("  y 坐标 (十六进制):", hex(public_key_point.y()))
    print("keccak-256哈希:", public_key_hash.hex())
    print("校验和:", checksum.hex())
    print("完整地址数据:", full_payload.hex())
    print("trc 地址数据:", trc_address_hex)
    print("trc 地址:", trc_address)
    print('\n')


def test2():
    # 指定私钥(请替换为你实际的私钥)
    private_key_hex = "your_private_key_hex_string"

    # 将十六进制私钥转换为privatekey对象
    private_key = privatekey(bytes.fromhex(private_key_hex))

    # 从私钥派生公钥
    public_key = private_key.public_key

    # 从公钥生成trc地址
    address = public_key.to_base58check_address()

    print("指定的私钥 (十六进制):", private_key_hex)
    print("公钥 (十六进制):", public_key.hex())
    print("trc地址:", address)
    print('\n')


test1()
test2()

到此这篇关于使用python计算trc20地址的文章就介绍到这了,更多相关python计算trc20地址内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com