Member-only story
Validating Spacemesh Addresses
In order to check that I’m doing it right in my last article, and to validate new addresses entered by users, I decided to implement some simple address validation. Understanding and validating addresses is crucial for developers working with the Spacemesh ecosystem, and it can save you a lot of headaches down the road. This article will guide you through the process of validating Spacemesh addresses, complete with a straightforward Python implementation.
Requirements
Before we begin, ensure you have Python 3.7+ installed, along with the bech32 library.
pip install bech32
I covered the anatomy of a Spacemesh address in my last article, but I’ll go over it again briefly here.
A Spacemesh wallet address…
…starts with the prefix “sm1” for mainnet addresses (or “stest1” for testnet).
…has a total length of 48 characters. (51 for testnet, to accomodate the extended hrp)
…uses Bech32 encoding, which includes error-checking capabilities.
…when decoded, contains 24 bytes of data, with the first 4 bytes being zero.
Breakdown and Example
Knowing these things, in order to validate that an incoming string is also a valid Spacemesh address, we must…
- Check the prefix and length
- Decode the Bech32 address