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
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 {
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);
Address::Alias(*alias_address).pack(&mut packer).unwrap();
serial_number.pack(&mut packer).unwrap();
token_scheme_kind.pack(&mut packer).unwrap();
FoundryId::new(bytes)
}
pub fn alias_address(&self) -> AliasAddress {
AliasAddress::from(AliasId::new(self.0[1..AliasId::LENGTH + 1].try_into().unwrap()))
}
pub fn serial_number(&self) -> u32 {
u32::from_le_bytes(
self.0[AliasId::LENGTH + 1..AliasId::LENGTH + 1 + core::mem::size_of::<u32>()]
.try_into()
.unwrap(),
)
}
pub fn token_scheme_kind(&self) -> u8 {
*self.0.last().unwrap()
}
}