Skip to content

Instantly share code, notes, and snippets.

@pcaversaccio
Last active February 22, 2025 12:37
Show Gist options
  • Save pcaversaccio/c739bdb99f66e076129b472c39ac4583 to your computer and use it in GitHub Desktop.
Save pcaversaccio/c739bdb99f66e076129b472c39ac4583 to your computer and use it in GitHub Desktop.
Generate a Tron address from `bytes` input.
import hashlib
def base58_encode(data: bytes) -> str:
alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
value = int.from_bytes(data, "big")
result = []
while value:
value, remainder = divmod(value, 58)
result.append(alphabet[remainder])
result.reverse()
num_leading_zeros = len(data) - len(data.lstrip(b"\x00"))
return (alphabet[0] * num_leading_zeros) + "".join(result)
# Generate the Tron address.
# Example: https://tronscan.org/#/transaction/1b9dac9676d32ea498089da820a599cbb04d8b725e991d418ce99d3fc699e829.
# The bytes data is here: 0ecb93c00000000000000000000000413026a67b1c7194681bb28627432ee5784a732c26.
# `0ecb93c0` is the function signature of `addBlackList(address)`.
# `0000000000000000000000413026a67b1c7194681bb28627432ee5784a732c26` is the ABI-encoded 32-bytes address.
address_bytes = bytes.fromhex("413026a67b1c7194681bb28627432ee5784a732c26")
checksum = hashlib.sha256(hashlib.sha256(address_bytes).digest()).digest()[:4]
tron_address = base58_encode(address_bytes + checksum)
print(tron_address)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment