user7729135
user7729135

Reputation: 409

Hex string to base58

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

Upvotes: 2

Views: 6627

Answers (1)

Hazzu
Hazzu

Reputation: 2155

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

Upvotes: 5

Related Questions