miden_objects/address/
type.rs

1use crate::AddressError;
2use crate::errors::Bech32Error;
3
4/// The type of an [`Address`](super::Address) in Miden.
5///
6/// The byte values of this address type should be chosen as a multiple of 8. That way, the first
7/// character of the bech32 string after the `1` separator will be different for every address type.
8/// This makes the type of the address conveniently human-readable.
9///
10/// For instance, [`AddressType::AccountId`] is chosen as 232 which is 0b1110_1000 in binary. Base32
11/// encodes one character for every 5 bits and 0b11101 (= 29) translates to `a`. So, every account
12/// ID address will start with `mm1a`.
13///
14/// See the table in the [bech32 spec](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#bech32)
15/// for a convenient overview.
16#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
17#[repr(u8)]
18#[non_exhaustive]
19pub enum AddressType {
20    AccountId = Self::ACCOUNT_ID,
21}
22
23impl AddressType {
24    // Constants for internal use only.
25    const ACCOUNT_ID: u8 = 232;
26}
27
28impl TryFrom<u8> for AddressType {
29    type Error = AddressError;
30
31    /// Decodes an [`AddressType`] from its byte representation.
32    fn try_from(byte: u8) -> Result<Self, Self::Error> {
33        match byte {
34            Self::ACCOUNT_ID => Ok(Self::AccountId),
35            other => Err(AddressError::Bech32DecodeError(Bech32Error::UnknownAddressType(other))),
36        }
37    }
38}