Home > Mobile >  Buffer.from(<string>, 'hex') equivalent in Python
Buffer.from(<string>, 'hex') equivalent in Python

Time:11-21

I have a typescript library that I need to translate into Python. I am using the library bs58 in Typescript and its equivalent base58 library in python.

My problem is coming when I try to replicate this:


const decodedTxHash = Buffer.from('34cc2932f90774851410a536e3db2c2e61266a1587fbc15e7e9c79b41631ac74', 'hex')
        
const nearBurnTxHash = bs58.encode(decodedTxHash)

This results in: 4Z6m9qjt9BNxTF1SdDw3bzYGXYzMp2gTmwRy5AJxpNps

What would be the way to get the same result in Python? I can tell you that I tried all I could think of about making it into a bytearray, feeding it as a string as bytes, nothing gave me the same result.

Any ideas?

CodePudding user response:

According to your title you're only asking on how to convert hex into bytes which can simply be archived by bytes.fromhex("<some hex in here>").
A full working example for your code will be:

import base58
raw_bytes = bytes.fromhex("34cc2932f90774851410a536e3db2c2e61266a1587fbc15e7e9c79b41631ac74")
b58_encoded = base58.b58encode(raw_bytes)

print(b58_encoded)
  • Related