Convert Byte to String in Pyteal

I am currently saving some wallet addresses in the global state for my smart contract:

option_one_wallet = Txn.application_args[8]
option_two_wallet = Txn.application_args[9]

However when I try to use them later, I get this error:

logic eval error: not an address

When I look at the smart contract state, it looks like Txn.sender() is a string while optionOneWallet and optionTwoWallet are byte strings:
https://testnet.algoexplorer.io/application/49823982

Looking through the pyteal documentation, I don’t see a way to convert byte strings to strings. What would be the best method for me to convert the byte string to a string?

Each SDK has a method to convert the 58 byte address (the one we see normally) to the 32 byte address that logic needs. The implementation is just base32 encode/decode with some padding adding/removal logic

In Python for example:
https://py-algorand-sdk.readthedocs.io/en/latest/algosdk/encoding.html#algosdk.encoding.decode_address

Ben

1 Like

Hey Ben, thanks for the reply!

Would this mean if I want to send a Algorand wallet address to be used in the smart contract I have to encode the wallet address with the encode address function? If I wanted to save these wallet addresses as a global variable to be used later, is there any way in pyteal that I can decode the addresses again or would it be done automatically?

If you’re passing it as a foreign account the SDKs will handle it for you, if its an argument it would need to be encoded since those are just byte strings.

In teal you don’t need to think about conversion since it should be the 32 byte version always when it makes it to the contract logic

Ben

1 Like

I see. I was previously decoding the address incorrectly. Now it works after

wallet := types.DecodeAddress("address")
[]byte(wallet[:])

Thanks!

2 Likes