1

does anyone know any package that support the following conversion of base58 to hex string or the other way round from hex string to base58 encoding. below is an example of a python implementation.

https://www.reddit.com/r/Tronix/comments/ja8khn/convert_my_address/

this hex string <- "4116cecf977b1ecc53eed37ee48c0ee58bcddbea5e" should result in this : "TC3ockcvHNmt7uJ8f5k3be1QrZtMzE8MxK"

here is a link to be used for verification: https://tronscan.org/#/tools/tron-convert-tool

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user7729135
  • 399
  • 1
  • 3
  • 11

1 Answers1

4

I was looking for it and I was able to design functions that produce the desired result.

import base58


def hex_to_base58(hex_string):
    if hex_string[:2] in ["0x", "0X"]:
        hex_string = "41" + hex_string[2:]
    bytes_str = bytes.fromhex(hex_string)
    base58_str = base58.b58encode_check(bytes_str)
    return base58_str.decode("UTF-8")


def base58_to_hex(base58_string):
    asc_string = base58.b58decode_check(base58_string)
    return asc_string.hex().upper()

They are useful if you want to convert the public key (hex) of the transactions to the addresses of the wallets (base58).

public_key_hex = "0x4ab99740bdf786204e57c00677cf5bf8ee766476"
address = hex_to_base58(public_key_hex)
print(address)
# TGnKLLBQyCo6QF911j65ipBz5araDSYQAD
Hazzu
  • 1,051
  • 1
  • 4
  • 8
  • just a small note, that is not the public key, that is the address in hex (the address is the last 20 bytes of the hash of the public key these 20 bytes(40 hex characters + "0x41" so 21 bytes are the address now) are then base58check encoded) – whitebat 199 Aug 04 '22 at 11:06