Convert Byte to String in Pyteal - Algorand

Convert Byte to String in Pyteal

You have selected 0 posts.

1.3k views 7 likes 2 links

post by Dalvor on Dec 8, 2021

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?

post by Ben on Dec 8, 2021

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

post by Dalvor on Dec 8, 2021

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?

post by Ben on Dec 9, 2021

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

post by Dalvor on Dec 12, 2021

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


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

Thanks!