1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use packable::{packer::SlicePacker, Packable};

use crate::block::{
    address::{Address, AliasAddress},
    output::{AliasId, TokenId},
};

impl_id!(pub FoundryId, 38, "Defines the unique identifier of a foundry.");

#[cfg(feature = "serde")]
string_serde_impl!(FoundryId);

impl From<TokenId> for FoundryId {
    fn from(token_id: TokenId) -> Self {
        FoundryId::new(*token_id)
    }
}

impl FoundryId {
    /// Builds a new [`FoundryId`] from its components.
    pub fn build(alias_address: &AliasAddress, serial_number: u32, token_scheme_kind: u8) -> Self {
        let mut bytes = [0u8; FoundryId::LENGTH];
        let mut packer = SlicePacker::new(&mut bytes);

        // PANIC: packing to an array of the correct length can't fail.
        Address::Alias(*alias_address).pack(&mut packer).unwrap();
        serial_number.pack(&mut packer).unwrap();
        token_scheme_kind.pack(&mut packer).unwrap();

        FoundryId::new(bytes)
    }

    /// Returns the [`AliasAddress`] of the [`FoundryId`].
    pub fn alias_address(&self) -> AliasAddress {
        // PANIC: the lengths are known.
        AliasAddress::from(AliasId::new(self.0[1..AliasId::LENGTH + 1].try_into().unwrap()))
    }

    /// Returns the serial number of the [`FoundryId`].
    pub fn serial_number(&self) -> u32 {
        // PANIC: the lengths are known.
        u32::from_le_bytes(
            self.0[AliasId::LENGTH + 1..AliasId::LENGTH + 1 + core::mem::size_of::<u32>()]
                .try_into()
                .unwrap(),
        )
    }

    /// Returns the [`TokenScheme`](crate::block::output::TokenScheme) kind of the [`FoundryId`].
    pub fn token_scheme_kind(&self) -> u8 {
        // PANIC: the lengths are known.
        *self.0.last().unwrap()
    }
}