Encoding and Decoding
When working with data from an algorand REST server or preparing transactions there is often a need to encode or decode fields.
Encoding Types
JSON
The encoding most often returned when querying the state of the chain is JSON.
It is easy to visually inspect but may be relatively slow to parse.
All byte arrays are base 64 encoded strings
MessagePack
The encoding used when transmitting transactions to a node is MessagePack.
To inspect a given msgpack file contents a convenience commandline tool is provided:
1msgpacktool -d < file.msgp
Base64
The encoding for byte arrays is Base64.
This is to make it safe for the byte array to be transmitted as part of a json object.
Base32
The encoding used for Addresses and Transaction Ids is Base32.
Individual Field Encodings
Address
In Algorand a public key is a 32 byte array.
The Address developers or users are typically shown is a 58 character long string corresponding to a base32 encoding of the byte array of the public key + a checksum.
Given an address 4H5UNRBJ2Q6JENAXQ6HNTGKLKINP4J4VTQBEPK5F3I6RDICMZBPGNH6KD4
, encoding to and from the public key format can be done as follows:
1address = "4H5UNRBJ2Q6JENAXQ6HNTGKLKINP4J4VTQBEPK5F3I6RDICMZBPGNH6KD4"2pk = encoding.decode_address(address)3addr = encoding.encode_address(pk)4
5assert addr == address
1const address = '4H5UNRBJ2Q6JENAXQ6HNTGKLKINP4J4VTQBEPK5F3I6RDICMZBPGNH6KD4';2const pk = algosdk.decodeAddress(address);3const addr = algosdk.encodeAddress(pk.publicKey);4console.log(address, addr);
1address := "4H5UNRBJ2Q6JENAXQ6HNTGKLKINP4J4VTQBEPK5F3I6RDICMZBPGNH6KD4"2pk, _ := types.DecodeAddress(address)3addr := pk.String()
1String addrAsStr = "4H5UNRBJ2Q6JENAXQ6HNTGKLKINP4J4VTQBEPK5F3I6RDICMZBPGNH6KD4";2// Instantiate a new Address object with string3Address addr = new Address(addrAsStr);4// Or with the bytes5Address addrAgain = new Address(addr.getBytes());6assert addrAgain.equals(addr);
Byte arrays
When transmitting an array of bytes over the network, byte arrays are base64 encoded. The SDK will handle encoding from a byte array to base64 but may not decode some fields and you’ll have to handle it yourself. For example compiled program results or the keys and values in a state delta in an application call will be returned as base64 encoded strings.
Example:
Given a base64 encoded byte array SGksIEknbSBkZWNvZGVkIGZyb20gYmFzZTY0
it may be decoded as follows:
1base64.b64decode(encoded_str).decode("utf-8") print(decoded_str) ``` [Snippet2Source](https://github.com/algorand/js-algorand-sdk/blob/examples/examples/codec.ts#L23-L26)3</TabItem>4<TabItem label='JavaScript' icon='seti:javascript'>5```javascript const b64Encoded = 'SGksIEknbSBkZWNvZGVkIGZyb20gYmFzZTY0'; const b64Decoded =6Buffer.from(b64Encoded, 'base64').toString(); console.log(b64Encoded, b64Decoded); ``` [Snippet7Source](https://github.com/algorand/py-algorand-sdk/blob/examples/examples/codec.py#L24-L27)8</TabItem>9<TabItem label='Go' icon='seti:go'>10```go encoded := "SGksIEknbSBkZWNvZGVkIGZyb20gYmFzZTY0" decoded, _ :=11base64.StdEncoding.DecodeString(encoded) reencoded := base64.StdEncoding.EncodeToString(decoded)12``` [Snippet13Source](https://github.com/algorand/go-algorand-sdk/blob/examples/examples/codec/main.go#L84-L87)14</TabItem>15<TabItem label='Java' icon='seti:java'>16```java String encodedStr = "SGksIEknbSBkZWNvZGVkIGZyb20gYmFzZTY0"; byte[] decodedBytes =17Encoder.decodeFromBase64(encodedStr); String reEncodedStr =18Encoder.encodeToBase64(decodedBytes); assert encodedStr.equals(reEncodedStr); ``` [Snippet19Source](https://github.com/algorand/java-algorand-sdk/blob/examples/examples/src/main/java/com/algorand/examples/CodecExamples.java#L35-L39)20</TabItem>21</Tabs>22
23### Integers24
25Integers in algorand are almost always uint64, sometimes it's required to encode them as bytes. For example when passing them as application arguments in an ApplicationCallTransaction. When encoding an integer to pass as an application argument, the integer should be encoded as the big endian 8 byte representation of the integer value.26
27_Example:_28
29Given an integer `1337`, you may encode it as:30
31<Tabs syncKey="lang">32<TabItem label="Python" icon="seti:python">33 ```python34 val = 133735 encoded_uint = val.to_bytes(8, "big")36 decoded_uint = int.from_bytes(encoded_uint, byteorder="big")37 assert decoded_uint == val
1const int = 1337;2const encoded = algosdk.encodeUint64(int);3const safeDecoded = algosdk.decodeUint64(encoded, 'safe');4const mixedDecoded = algosdk.decodeUint64(encoded, 'bigint');5console.log(int, encoded, safeDecoded, mixedDecoded);
1val := 13372encodedInt := make([]byte, 8)3binary.BigEndian.PutUint64(encodedInt, uint64(val))4
5decodedInt := binary.BigEndian.Uint64(encodedInt)6// decodedInt == val
1BigInteger val = BigInteger.valueOf(1337);2byte[] encodedVal = Encoder.encodeUint64(val);3BigInteger decodedVal = Encoder.decodeUint64(encodedVal);4assert val.equals(decodedVal);
Working with Encoded Structures
transactions
Sometimes an application needs to transmit a transaction or transaction group between the front end and back end. This can be done by msgpack encoding the transaction object on one side and msgpack decoding it on the other side. Often the msgpack’d bytes will be base64 encoded so that they can be safely transmitted in some json payload so we use that encoding here.
Essentially the encoding is:
tx_byte_str = base64encode(msgpack_encode(tx_obj))
and decoding is:
tx_obj = msgpack_decode(base64decode(tx_byte_str))
Example:
Create a payment transaction from one account to another using suggested parameters and amount 10000, we write the msgpack encoded bytes