Skip to main content

miden_protocol/account/account_id/v1/
mod.rs

1mod prefix;
2use alloc::string::{String, ToString};
3use alloc::vec::Vec;
4use core::fmt;
5use core::hash::Hash;
6
7use bech32::Bech32m;
8use bech32::primitives::decode::{ByteIter, CheckedHrpstring};
9use miden_crypto::utils::hex_to_bytes;
10pub use prefix::AccountIdPrefixV1;
11
12use crate::account::account_id::NetworkId;
13use crate::account::{AccountIdVersion, AccountType};
14use crate::address::AddressType;
15use crate::errors::{AccountError, AccountIdError, Bech32Error};
16use crate::utils::serde::{
17    ByteReader,
18    ByteWriter,
19    Deserializable,
20    DeserializationError,
21    Serializable,
22};
23use crate::{EMPTY_WORD, Felt, Hasher, Word};
24
25// ACCOUNT ID VERSION 1
26// ================================================================================================
27
28/// Version 1 of the [`Account`](crate::account::Account) identifier.
29///
30/// See the [`AccountId`](super::AccountId) type's documentation for details.
31#[derive(Debug, Copy, Clone, Eq, PartialEq)]
32pub struct AccountIdV1 {
33    suffix: Felt,
34    prefix: Felt,
35}
36
37impl Hash for AccountIdV1 {
38    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
39        self.prefix.as_canonical_u64().hash(state);
40        self.suffix.as_canonical_u64().hash(state);
41    }
42}
43
44impl AccountIdV1 {
45    // CONSTANTS
46    // --------------------------------------------------------------------------------------------
47
48    /// The serialized size of an [`AccountIdV1`] in bytes.
49    const SERIALIZED_SIZE: usize = 15;
50
51    /// The least significant nibble determines the account version.
52    const VERSION_MASK: u64 = 0b1111;
53
54    /// The second most significant bit of the prefix's least significant byte encodes the account
55    /// type.
56    pub(crate) const ACCOUNT_TYPE_MASK: u8 = 0b1 << Self::ACCOUNT_TYPE_SHIFT;
57    pub(crate) const ACCOUNT_TYPE_SHIFT: u64 = 4;
58
59    /// The element index in the seed digest that becomes the account ID suffix (after
60    /// [`shape_suffix`]).
61    pub(crate) const SEED_DIGEST_SUFFIX_ELEMENT_IDX: usize = 0;
62    /// The element index in the seed digest that becomes the account ID prefix.
63    pub(crate) const SEED_DIGEST_PREFIX_ELEMENT_IDX: usize = 1;
64
65    // CONSTRUCTORS
66    // --------------------------------------------------------------------------------------------
67
68    /// See [`AccountId::new`](super::AccountId::new) for details.
69    pub fn new(
70        seed: Word,
71        code_commitment: Word,
72        storage_commitment: Word,
73    ) -> Result<Self, AccountIdError> {
74        let seed_digest = compute_digest(seed, code_commitment, storage_commitment);
75
76        // Use the first half-word of the seed digest as the account ID, where the prefix is the
77        // most significant element.
78        let mut suffix = seed_digest[Self::SEED_DIGEST_SUFFIX_ELEMENT_IDX];
79        let prefix = seed_digest[Self::SEED_DIGEST_PREFIX_ELEMENT_IDX];
80
81        suffix = shape_suffix(suffix);
82
83        Self::try_from_elements(suffix, prefix)
84    }
85
86    /// See [`AccountId::new_unchecked`](super::AccountId::new_unchecked) for details.
87    pub fn new_unchecked(elements: [Felt; 2]) -> Self {
88        let prefix = elements[0];
89        let suffix = elements[1];
90
91        // Panic on invalid felts in debug mode.
92        if cfg!(debug_assertions) {
93            validate_prefix(prefix).expect("AccountId::new_unchecked called with invalid prefix");
94            validate_suffix(suffix).expect("AccountId::new_unchecked called with invalid suffix");
95        }
96
97        Self { prefix, suffix }
98    }
99
100    /// See [`AccountId::try_from_elements`](super::AccountId::try_from_elements) for details.
101    pub fn try_from_elements(suffix: Felt, prefix: Felt) -> Result<Self, AccountIdError> {
102        validate_suffix(suffix)?;
103        validate_prefix(prefix)?;
104
105        Ok(AccountIdV1 { suffix, prefix })
106    }
107
108    /// See [`AccountId::dummy`](super::AccountId::dummy) for details.
109    #[cfg(any(feature = "testing", test))]
110    pub fn dummy(mut bytes: [u8; 15], account_type: AccountType) -> AccountIdV1 {
111        let version = AccountIdVersion::Version1 as u8;
112        let low_nibble = ((account_type as u8) << Self::ACCOUNT_TYPE_SHIFT) | version;
113
114        // Set least significant byte.
115        bytes[7] = low_nibble;
116
117        // Clear the 32nd most significant bit.
118        bytes[3] &= 0b1111_1110;
119
120        let prefix_bytes =
121            bytes[0..8].try_into().expect("we should have sliced off exactly 8 bytes");
122        let prefix = Felt::try_from(u64::from_be_bytes(prefix_bytes))
123            .expect("should be a valid felt due to the most significant bit being zero");
124
125        let mut suffix_bytes = [0; 8];
126        // Overwrite first 7 bytes, leaving the 8th byte 0 (which will be cleared by
127        // shape_suffix anyway).
128        suffix_bytes[..7].copy_from_slice(&bytes[8..]);
129
130        // Clear the most significant bit of the suffix to make sure we get a valid felt
131        let suffix = u64::from_be_bytes(suffix_bytes) & 0x7fff_ffff_ffff_ffff;
132        let mut suffix =
133            Felt::try_from(suffix).expect("no bits were set so felt should still be valid");
134
135        suffix = shape_suffix(suffix);
136
137        let account_id = Self::try_from_elements(suffix, prefix)
138            .expect("we should have shaped the felts to produce a valid id");
139
140        debug_assert_eq!(account_id.account_type(), account_type);
141
142        account_id
143    }
144
145    /// See [`AccountId::compute_account_seed`](super::AccountId::compute_account_seed) for details.
146    pub fn compute_account_seed(
147        init_seed: [u8; 32],
148        account_type: AccountType,
149        version: AccountIdVersion,
150        code_commitment: Word,
151        storage_commitment: Word,
152    ) -> Result<Word, AccountError> {
153        crate::account::account_id::seed::compute_account_seed(
154            init_seed,
155            account_type,
156            version,
157            code_commitment,
158            storage_commitment,
159        )
160    }
161
162    // PUBLIC ACCESSORS
163    // --------------------------------------------------------------------------------------------
164
165    /// See [`AccountId::account_type`](super::AccountId::account_type) for details.
166    pub fn account_type(&self) -> AccountType {
167        extract_account_type(self.prefix().as_u64())
168    }
169
170    /// See [`AccountId::is_public`](super::AccountId::is_public) for details.
171    pub fn is_public(&self) -> bool {
172        self.account_type() == AccountType::Public
173    }
174
175    /// See [`AccountId::version`](super::AccountId::version) for details.
176    pub fn version(&self) -> AccountIdVersion {
177        extract_version(self.prefix().as_u64())
178            .expect("account ID should have been constructed with a valid version")
179    }
180
181    /// See [`AccountId::from_hex`](super::AccountId::from_hex) for details.
182    pub fn from_hex(hex_str: &str) -> Result<AccountIdV1, AccountIdError> {
183        hex_to_bytes(hex_str)
184            .map_err(AccountIdError::AccountIdHexParseError)
185            .and_then(AccountIdV1::try_from)
186    }
187
188    /// See [`AccountId::to_hex`](super::AccountId::to_hex) for details.
189    pub fn to_hex(self) -> String {
190        // We need to pad the suffix with 16 zeroes so it produces a correctly padded 8 byte
191        // big-endian hex string. Only then can we cut off the last zero byte by truncating. We
192        // cannot use `:014x` padding.
193        let mut hex_string =
194            format!("0x{:016x}{:016x}", self.prefix().as_u64(), self.suffix().as_canonical_u64());
195        hex_string.truncate(32);
196        hex_string
197    }
198
199    /// See [`AccountId::to_bech32`](super::AccountId::to_bech32) for details.
200    pub fn to_bech32(&self, network_id: NetworkId) -> String {
201        let id_bytes: [u8; Self::SERIALIZED_SIZE] = (*self).into();
202
203        let mut data = [0; Self::SERIALIZED_SIZE + 1];
204        data[0] = AddressType::AccountId as u8;
205        data[1..16].copy_from_slice(&id_bytes);
206
207        // SAFETY: Encoding only panics if the total length of the hrp, data (in GF(32)), separator
208        // and checksum exceeds Bech32m::CODE_LENGTH, which is 1023. Since the data is 26 bytes in
209        // that field and the hrp is at most 83 in size we are way below the limit.
210        //
211        // The only allowed checksum algorithm is [`Bech32m`](bech32::Bech32m) due to being the
212        // best available checksum algorithm with no known weaknesses (unlike
213        // [`Bech32`](bech32::Bech32)). No checksum is also not allowed since the intended
214        // use of bech32 is to have error detection capabilities.
215        bech32::encode::<Bech32m>(network_id.into_hrp(), &data)
216            .expect("code length of bech32 should not be exceeded")
217    }
218
219    /// See [`AccountId::from_bech32`](super::AccountId::from_bech32) for details.
220    pub fn from_bech32(bech32_string: &str) -> Result<(NetworkId, Self), AccountIdError> {
221        // We use CheckedHrpString instead of bech32::decode with an explicit checksum algorithm so
222        // we don't allow the `Bech32` or `NoChecksum` algorithms.
223        let checked_string = CheckedHrpstring::new::<Bech32m>(bech32_string).map_err(|source| {
224            // The CheckedHrpStringError does not implement core::error::Error, only
225            // std::error::Error, so for now we convert it to a String. Even if it will
226            // implement the trait in the future, we should include it as an opaque
227            // error since the crate does not have a stable release yet.
228            AccountIdError::Bech32DecodeError(Bech32Error::DecodeError(source.to_string().into()))
229        })?;
230
231        let hrp = checked_string.hrp();
232        let network_id = NetworkId::from_hrp(hrp);
233
234        let mut byte_iter = checked_string.byte_iter();
235
236        // The length must be the serialized size of the account ID plus the address byte.
237        if byte_iter.len() != Self::SERIALIZED_SIZE + 1 {
238            return Err(AccountIdError::Bech32DecodeError(Bech32Error::InvalidDataLength {
239                expected: Self::SERIALIZED_SIZE + 1,
240                actual: byte_iter.len(),
241            }));
242        }
243
244        let address_byte = byte_iter.next().expect("there should be at least one byte");
245        if address_byte != AddressType::AccountId as u8 {
246            return Err(AccountIdError::Bech32DecodeError(Bech32Error::UnknownAddressType(
247                address_byte,
248            )));
249        }
250
251        Self::from_bech32_byte_iter(byte_iter).map(|account_id| (network_id, account_id))
252    }
253
254    /// Decodes the data from the bech32 byte iterator into an [`AccountId`].
255    pub(crate) fn from_bech32_byte_iter(byte_iter: ByteIter<'_>) -> Result<Self, AccountIdError> {
256        // The _remaining_ length of the iterator must be the serialized size of the account ID.
257        if byte_iter.len() != Self::SERIALIZED_SIZE {
258            return Err(AccountIdError::Bech32DecodeError(Bech32Error::InvalidDataLength {
259                expected: Self::SERIALIZED_SIZE,
260                actual: byte_iter.len(),
261            }));
262        }
263
264        // Every byte is guaranteed to be overwritten since we've checked the length of the
265        // iterator.
266        let mut id_bytes = [0_u8; Self::SERIALIZED_SIZE];
267        for (i, byte) in byte_iter.enumerate() {
268            id_bytes[i] = byte;
269        }
270
271        let account_id = Self::try_from(id_bytes)?;
272
273        Ok(account_id)
274    }
275
276    /// Returns the [`AccountIdPrefixV1`] of this account ID.
277    ///
278    /// See also [`AccountId::prefix`](super::AccountId::prefix) for details.
279    pub fn prefix(&self) -> AccountIdPrefixV1 {
280        // SAFETY: We only construct account IDs with valid prefixes, so we don't have to validate
281        // it again.
282        AccountIdPrefixV1::new_unchecked(self.prefix)
283    }
284
285    /// See [`AccountId::suffix`](super::AccountId::suffix) for details.
286    pub const fn suffix(&self) -> Felt {
287        self.suffix
288    }
289}
290
291// CONVERSIONS FROM ACCOUNT ID
292// ================================================================================================
293
294impl From<AccountIdV1> for [Felt; 2] {
295    fn from(id: AccountIdV1) -> Self {
296        [id.prefix, id.suffix]
297    }
298}
299
300impl From<AccountIdV1> for [u8; 15] {
301    fn from(id: AccountIdV1) -> Self {
302        let mut result = [0_u8; 15];
303        result[..8].copy_from_slice(&id.prefix().as_u64().to_be_bytes());
304        // The last byte of the suffix is always zero so we skip it here.
305        result[8..].copy_from_slice(&id.suffix().as_canonical_u64().to_be_bytes()[..7]);
306        result
307    }
308}
309
310impl From<AccountIdV1> for u128 {
311    fn from(id: AccountIdV1) -> Self {
312        let mut le_bytes = [0_u8; 16];
313        le_bytes[..8].copy_from_slice(&id.suffix().as_canonical_u64().to_le_bytes());
314        le_bytes[8..].copy_from_slice(&id.prefix().as_u64().to_le_bytes());
315        u128::from_le_bytes(le_bytes)
316    }
317}
318
319// CONVERSIONS TO ACCOUNT ID
320// ================================================================================================
321
322impl TryFrom<[u8; 15]> for AccountIdV1 {
323    type Error = AccountIdError;
324
325    /// See [`TryFrom<[u8; 15]> for
326    /// AccountId`](super::AccountId#impl-TryFrom<%5Bu8;+15%5D>-for-AccountId) for details.
327    fn try_from(mut bytes: [u8; 15]) -> Result<Self, Self::Error> {
328        // Felt::try_from expects little-endian order, so reverse the individual felt slices.
329        // This prefix slice has 8 bytes.
330        bytes[..8].reverse();
331        // The suffix slice has 7 bytes, since the 8th byte will always be zero.
332        bytes[8..15].reverse();
333
334        let prefix_slice = &bytes[..8];
335        let suffix_slice = &bytes[8..15];
336
337        // The byte order is little-endian here, so we prepend a 0 to set the least significant
338        // byte.
339        let mut suffix_bytes = [0; 8];
340        suffix_bytes[1..8].copy_from_slice(suffix_slice);
341
342        let prefix = Felt::try_from(u64::from_le_bytes(
343            prefix_slice.try_into().expect("prefix slice should be 8 bytes"),
344        ))
345        .map_err(|err| {
346            AccountIdError::AccountIdInvalidPrefixFieldElement(DeserializationError::InvalidValue(
347                err.to_string(),
348            ))
349        })?;
350
351        let suffix = Felt::try_from(u64::from_le_bytes(suffix_bytes)).map_err(|err| {
352            AccountIdError::AccountIdInvalidSuffixFieldElement(DeserializationError::InvalidValue(
353                err.to_string(),
354            ))
355        })?;
356
357        Self::try_from_elements(suffix, prefix)
358    }
359}
360
361impl TryFrom<u128> for AccountIdV1 {
362    type Error = AccountIdError;
363
364    /// See [`TryFrom<u128> for AccountId`](super::AccountId#impl-TryFrom<u128>-for-AccountId) for
365    /// details.
366    fn try_from(int: u128) -> Result<Self, Self::Error> {
367        let mut bytes: [u8; 15] = [0; 15];
368        bytes.copy_from_slice(&int.to_be_bytes()[0..15]);
369
370        Self::try_from(bytes)
371    }
372}
373
374// SERIALIZATION
375// ================================================================================================
376
377impl Serializable for AccountIdV1 {
378    fn write_into<W: ByteWriter>(&self, target: &mut W) {
379        let bytes: [u8; 15] = (*self).into();
380        bytes.write_into(target);
381    }
382
383    fn get_size_hint(&self) -> usize {
384        Self::SERIALIZED_SIZE
385    }
386}
387
388impl Deserializable for AccountIdV1 {
389    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
390        <[u8; 15]>::read_from(source)?
391            .try_into()
392            .map_err(|err: AccountIdError| DeserializationError::InvalidValue(err.to_string()))
393    }
394}
395
396// HELPER FUNCTIONS
397// ================================================================================================
398
399/// Checks that the prefix has a known value for the version.
400pub(crate) fn validate_prefix(
401    prefix: Felt,
402) -> Result<(AccountType, AccountIdVersion), AccountIdError> {
403    let prefix = prefix.as_canonical_u64();
404
405    let account_type = extract_account_type(prefix);
406
407    // Validate version bits.
408    let version = extract_version(prefix)?;
409
410    Ok((account_type, version))
411}
412
413/// Checks that the suffix:
414/// - has its most significant bit set to zero.
415/// - has its lower 8 bits set to zero.
416fn validate_suffix(suffix: Felt) -> Result<(), AccountIdError> {
417    let suffix = suffix.as_canonical_u64();
418
419    // Validate most significant bit is zero.
420    if suffix >> 63 != 0 {
421        return Err(AccountIdError::AccountIdSuffixMostSignificantBitMustBeZero);
422    }
423
424    // Validate lower 8 bits of second felt are zero.
425    if suffix & 0xff != 0 {
426        return Err(AccountIdError::AccountIdSuffixLeastSignificantByteMustBeZero);
427    }
428
429    Ok(())
430}
431
432pub(crate) fn extract_account_type(prefix: u64) -> AccountType {
433    let bits = (prefix & AccountIdV1::ACCOUNT_TYPE_MASK as u64) >> AccountIdV1::ACCOUNT_TYPE_SHIFT;
434    // SAFETY: `ACCOUNT_TYPE_MASK` is u8 so casting bits is lossless
435    match bits as u8 {
436        AccountType::PRIVATE => AccountType::Private,
437        AccountType::PUBLIC => AccountType::Public,
438        _ => unreachable!("account type mask is 1 bit so every value is covered above"),
439    }
440}
441
442pub(crate) fn extract_version(prefix: u64) -> Result<AccountIdVersion, AccountIdError> {
443    // SAFETY: The mask guarantees that we only mask out the least significant nibble, so casting to
444    // u8 is safe.
445    let version = (prefix & AccountIdV1::VERSION_MASK) as u8;
446    AccountIdVersion::try_from(version)
447}
448
449/// Shapes the suffix so it meets the requirements of the account ID, by setting the lower 8 bits to
450/// zero.
451fn shape_suffix(suffix: Felt) -> Felt {
452    let mut suffix = suffix.as_canonical_u64();
453
454    // Clear the lower 8 bits.
455    suffix &= 0xffff_ffff_ffff_ff00;
456
457    // SAFETY: Felt was previously valid and we only cleared bits, so it must still be valid.
458    Felt::try_from(suffix).expect("no bits were set so felt should still be valid")
459}
460
461// COMMON TRAIT IMPLS
462// ================================================================================================
463
464impl PartialOrd for AccountIdV1 {
465    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
466        Some(self.cmp(other))
467    }
468}
469
470impl Ord for AccountIdV1 {
471    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
472        u128::from(*self).cmp(&u128::from(*other))
473    }
474}
475
476impl fmt::Display for AccountIdV1 {
477    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
478        write!(f, "{}", self.to_hex())
479    }
480}
481
482/// Returns the digest of two hashing permutations over the seed, code commitment, storage
483/// commitment and padding.
484pub(crate) fn compute_digest(seed: Word, code_commitment: Word, storage_commitment: Word) -> Word {
485    let mut elements = Vec::with_capacity(16);
486    elements.extend(seed);
487    elements.extend(*code_commitment);
488    elements.extend(*storage_commitment);
489    elements.extend(EMPTY_WORD);
490    Hasher::hash_elements(&elements)
491}
492
493// TESTS
494// ================================================================================================
495
496#[cfg(test)]
497mod tests {
498
499    use rstest::rstest;
500
501    use super::*;
502    use crate::account::AccountIdPrefix;
503    use crate::testing::account_id::{
504        ACCOUNT_ID_PRIVATE_NON_FUNGIBLE_FAUCET,
505        ACCOUNT_ID_PRIVATE_SENDER,
506        ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET,
507        ACCOUNT_ID_REGULAR_PRIVATE_ACCOUNT_UPDATABLE_CODE,
508        ACCOUNT_ID_REGULAR_PUBLIC_ACCOUNT_IMMUTABLE_CODE,
509    };
510
511    #[test]
512    fn account_id_from_felts_with_max_pop_count() {
513        let valid_suffix = Felt::try_from(0x7fff_ffff_ffff_ff00u64).unwrap();
514        let valid_prefix = Felt::try_from(0x7fff_ffff_ffff_fff1u64).unwrap();
515
516        let id1 = AccountIdV1::new_unchecked([valid_prefix, valid_suffix]);
517        assert_eq!(id1.account_type(), AccountType::Public);
518        assert_eq!(id1.version(), AccountIdVersion::Version1);
519    }
520
521    #[rstest]
522    fn account_id_serde_roundtrip(
523        // Use the highest possible input to check if the constructed id is a valid Felt in that
524        // scenario.
525        // Use the lowest possible input to check whether the constructor produces valid IDs with
526        // all-zeroes input.
527        #[values([0xff; 15], [0; 15])] input: [u8; 15],
528        #[values(AccountType::Private, AccountType::Public)] account_type: AccountType,
529    ) {
530        let id = AccountIdV1::dummy(input, account_type);
531        assert_eq!(id.account_type(), account_type);
532        assert_eq!(id.version(), AccountIdVersion::Version1);
533
534        // Do a serialization roundtrip to ensure validity.
535        let serialized_id = id.to_bytes();
536        AccountIdV1::read_from_bytes(&serialized_id).unwrap();
537        assert_eq!(serialized_id.len(), AccountIdV1::SERIALIZED_SIZE);
538
539        let serialized_prefix = id.prefix().to_bytes();
540        AccountIdPrefix::read_from_bytes(&serialized_prefix).unwrap();
541        assert_eq!(serialized_prefix.len(), AccountIdPrefix::SERIALIZED_SIZE);
542    }
543
544    #[test]
545    fn account_id_prefix_serialization_compatibility() {
546        // Ensure that an AccountIdPrefix can be read from the serialized bytes of an AccountId.
547        let account_id = AccountIdV1::try_from(ACCOUNT_ID_PRIVATE_SENDER).unwrap();
548        let id_bytes = account_id.to_bytes();
549        assert_eq!(account_id.prefix().to_bytes(), id_bytes[..8]);
550
551        let deserialized_prefix = AccountIdPrefix::read_from_bytes(&id_bytes).unwrap();
552        assert_eq!(AccountIdPrefix::V1(account_id.prefix()), deserialized_prefix);
553
554        // Ensure AccountId and AccountIdPrefix's hex representation are compatible.
555        assert!(account_id.to_hex().starts_with(&account_id.prefix().to_hex()));
556    }
557
558    // CONVERSION TESTS
559    // ================================================================================================
560
561    #[test]
562    fn test_account_id_conversion_roundtrip() {
563        for (idx, account_id) in [
564            ACCOUNT_ID_REGULAR_PUBLIC_ACCOUNT_IMMUTABLE_CODE,
565            ACCOUNT_ID_REGULAR_PRIVATE_ACCOUNT_UPDATABLE_CODE,
566            ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET,
567            ACCOUNT_ID_PRIVATE_NON_FUNGIBLE_FAUCET,
568            ACCOUNT_ID_PRIVATE_SENDER,
569        ]
570        .into_iter()
571        .enumerate()
572        {
573            let id = AccountIdV1::try_from(account_id).expect("account ID should be valid");
574            assert_eq!(id, AccountIdV1::from_hex(&id.to_hex()).unwrap(), "failed in {idx}");
575            assert_eq!(id, AccountIdV1::try_from(<[u8; 15]>::from(id)).unwrap(), "failed in {idx}");
576            assert_eq!(id, AccountIdV1::try_from(u128::from(id)).unwrap(), "failed in {idx}");
577            // The u128 big-endian representation without the least significant byte and the
578            // [u8; 15] representations should be equivalent.
579            assert_eq!(u128::from(id).to_be_bytes()[0..15], <[u8; 15]>::from(id));
580            assert_eq!(account_id, u128::from(id), "failed in {idx}");
581        }
582    }
583
584    #[test]
585    fn test_account_id_accessors() {
586        let account_id = AccountIdV1::try_from(ACCOUNT_ID_REGULAR_PUBLIC_ACCOUNT_IMMUTABLE_CODE)
587            .expect("valid account ID");
588        assert!(account_id.is_public());
589
590        let account_id = AccountIdV1::try_from(ACCOUNT_ID_REGULAR_PRIVATE_ACCOUNT_UPDATABLE_CODE)
591            .expect("valid account ID");
592        assert!(!account_id.is_public());
593
594        let account_id =
595            AccountIdV1::try_from(ACCOUNT_ID_PUBLIC_FUNGIBLE_FAUCET).expect("valid account ID");
596        assert!(account_id.is_public());
597
598        let account_id = AccountIdV1::try_from(ACCOUNT_ID_PRIVATE_NON_FUNGIBLE_FAUCET)
599            .expect("valid account ID");
600        assert!(!account_id.is_public());
601    }
602}