Skip to content

Common Utilities

← Back to Examples Overview

Utility functions and helpers.

ExampleDescription
Address Basics

This example demonstrates basic address operations using the Address class:

  • Parsing addresses from base32 strings with Address.fromString()
  • Creating the zero address with Address.zeroAddress()
  • Encoding addresses back to base32 strings with Address.toString()
  • Comparing addresses for equality with Address.equals()
  • Computing the 4-byte checksum with Address.checksum()
  • Validating addresses with isValidAddress()
  • Accessing the 32-byte public key property
  • Using address constants: ALGORAND_ADDRESS_LENGTH, ALGORAND_ZERO_ADDRESS_STRING
Address Encoding

This example demonstrates how to encode and decode addresses between different formats and compute application addresses:

  • encodeAddress() to convert 32-byte public key to base32 string
  • decodeAddress() to convert base32 string to Address object
  • getApplicationAddress() to compute an app’s escrow address from app ID
  • getAddress() helper that accepts string, Address, or Addressable
  • Round-trip verification: encode(decode(address)) equals original
  • Understanding the relationship between public key bytes and checksum
Array Utilities

This example demonstrates the array utility functions for comparing and concatenating byte arrays:

  • arrayEqual() for comparing two arrays element-by-element
  • concatArrays() for joining multiple Uint8Arrays into a new array
Constants Reference

This example displays all the protocol constants available in the common package. These constants define limits, sizes, and separators used throughout Algorand.

Crypto Hash (SHA-512/256)

This example demonstrates the hash() function for computing Algorand-compatible SHA-512/256 hashes. This hash algorithm is used throughout Algorand for:

  • Transaction IDs
  • Address checksums
  • Application escrow addresses
  • State proof verification
Logger Type

This example demonstrates the Logger interface for consistent logging across the AlgoKit Utils SDK.

Topics covered:

  • Logger type definition with all log levels
  • Console-based Logger implementation
  • No-op (silent) logger for production
  • Custom formatting logger
  • Compatibility with common logging patterns
JSON BigInt

This example demonstrates parsing and stringifying JSON with BigInt support for handling large numbers safely without precision loss.

Topics covered:

  • parseJson() for parsing JSON strings with large numbers
  • Numbers > Number.MAX_SAFE_INTEGER are parsed as BigInt
  • Numbers <= Number.MAX_SAFE_INTEGER are parsed as regular numbers
  • stringifyJson() for serializing objects containing BigInt values
  • Round-trip preservation: stringifyJson(parseJson(str))
  • Comparison with native JSON.parse() showing precision loss
  • Algorand-relevant examples with microAlgo amounts
MessagePack

This example demonstrates encoding and decoding MessagePack data, which is used for Algorand transaction encoding.

Topics covered:

  • encodeMsgpack() to serialize data to MessagePack format
  • decodeMsgpack() to deserialize MessagePack bytes
  • Encoding simple objects with various types (strings, numbers, arrays)
  • Key sorting: encodeMsgpack() sorts keys alphabetically (canonical encoding)
  • Map returns: decodeMsgpack() returns a Map by default for object data
  • BigInt value encoding/decoding
  • Uint8Array (bytes) encoding
  • Size comparison: MessagePack vs JSON
Primitive Codecs

This example demonstrates how to use primitive codec types for encoding/decoding basic values in wire format (JSON or MessagePack).

Topics covered:

  • Codec interface: encode(), decode(), defaultValue() methods
  • numberCodec for encoding/decoding numbers
  • bigIntCodec for encoding/decoding BigInt values
  • booleanCodec for encoding/decoding boolean values
  • stringCodec for encoding/decoding UTF-8 strings
  • bytesCodec for encoding/decoding raw bytes
  • bytesBase64Codec for base64 encoding
  • addressCodec for encoding/decoding Algorand addresses
  • Round-trip verification: decode(encode(value)) equals original
Composite Codecs

This example demonstrates how to use composite codec types for encoding/decoding arrays, maps, and records in wire format (JSON or MessagePack).

Topics covered:

  • ArrayCodec for encoding/decoding typed arrays
  • Pre-built array codecs: numberArrayCodec, stringArrayCodec, bigIntArrayCodec,

booleanArrayCodec, bytesArrayCodec, addressArrayCodec

  • MapCodec for encoding/decoding Map<K, V> types
  • RecordCodec for encoding/decoding Record<string, V> types
  • Creating custom ArrayCodec with a specific element codec
  • Encoding nested structures (array of arrays, map of arrays)
  • Round-trip verification for each composite codec
Model Codecs

This example demonstrates how to use model codecs for encoding/decoding complex object structures with field metadata.

Topics covered:

  • ObjectModelCodec for encoding/decoding typed objects
  • Defining field metadata with FieldMetadata type
  • Encoding format options: ‘json’ vs ‘msgpack’
  • PrimitiveModelCodec for simple value types
  • ArrayModelCodec for array model types
  • Handling optional fields
  • Field renaming with wireKey vs property name
  • Round-trip encoding with ObjectModelCodec
Sourcemap

This example demonstrates how to use ProgramSourceMap for mapping TEAL program counters (PC) to source locations for debugging purposes.

Topics covered:

  • ProgramSourceMap class construction from sourcemap data
  • Sourcemap format: version, sources, names, mappings
  • getPcs() to get all program counter values
  • getLocationForPc() to get source location for a specific PC
  • SourceLocation properties: line, column, sourceIndex, nameIndex
  • getPcsOnSourceLine() to find PCs for a source line
  • How sourcemaps enable TEAL debugging by mapping PC to source

Run any example from the repository’s examples directory:

Terminal window
cd examples
npm run example common/01-*.ts