How do i decode public address that I get from Algorand daemon?

300 Views Asked by At

Hey guys so I was fiddling around with an dapp Idea.

When I get a specific address from the smart contract,

it gives me a specific encoded type

for example 9HUG4x4wVzHmXRedwA6b9ygZbN/KGeDfmRDLgGZtLmI= .

How can I decode that to the the public address I want?

2

There are 2 best solutions below

0
Web Zero On

you can simple pass that encoded string to pyteal.Base64Decode()

enter image description here

this class would return an string that would be decoded public address.

0
Fabrice On

The answer https://stackoverflow.com/a/73395946/2945326 is a way to decode a base64 address into a 32-byte raw address/public key inside a smart contract. In most cases, you should avoid decoding base64 within a smart contract. Instead, you should ensure that smart contracts always deal directly with raw bytes.

If you want to decode an address outside of a smart contract (for example to display it on a website), then you need to first decode the base64. This gives you a 32-byte raw address/public key. To be displayed, this needs to be converted to an address following https://developer.algorand.org/docs/get-details/encoding/#address.

Concretely, in Python:

import base64
import algosdk

raw_address_bytes = base64.b64decode("ZXUc/psAtm6K6AOMpvytibbSa8H6WFc6O8XTp/rHuEE=")
address = algosdk.encoding.encode_address(raw_address_bytes)

print(address)

which displays

MV2RZ7U3AC3G5CXIAOGKN7FNRG3NE26B7JMFOOR3YXJ2P6WHXBA4IGKRJ4

See https://forum.algorand.org/t/how-to-convert-public-key-back-to-address-in-js-sdk/3643/3?u=fabrice