bee_block/output/unlock_condition/
immutable_alias_address.rs

1// Copyright 2022 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use derive_more::From;
5
6use crate::{
7    address::{Address, AliasAddress},
8    Error,
9};
10
11/// Defines the permanent [`AliasAddress`] that owns this output.
12#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, From, packable::Packable)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14pub struct ImmutableAliasAddressUnlockCondition(#[packable(verify_with = verify_alias_address)] Address);
15
16impl ImmutableAliasAddressUnlockCondition {
17    /// The [`UnlockCondition`](crate::output::UnlockCondition) kind of an [`ImmutableAliasAddressUnlockCondition`].
18    pub const KIND: u8 = 6;
19
20    /// Creates a new [`ImmutableAliasAddressUnlockCondition`].
21    #[inline(always)]
22    pub fn new(address: AliasAddress) -> Self {
23        Self(Address::Alias(address))
24    }
25
26    /// Returns the address of an [`ImmutableAliasAddressUnlockCondition`].
27    #[inline(always)]
28    pub fn address(&self) -> &Address {
29        // An ImmutableAliasAddressUnlockCondition must have an AliasAddress.
30        // It has already been validated at construction that the address is an `AliasAddress`.
31        debug_assert!(&self.0.is_alias());
32        &self.0
33    }
34
35    /// Returns the alias address of an [`ImmutableAliasAddressUnlockCondition`].
36    pub fn alias_address(&self) -> &AliasAddress {
37        // An ImmutableAliasAddressUnlockCondition must have an AliasAddress.
38        if let Address::Alias(alias_address) = &self.0 {
39            alias_address
40        } else {
41            // It has already been validated at construction that the address is an `AliasAddress`.
42            unreachable!();
43        }
44    }
45}
46
47fn verify_alias_address<const VERIFY: bool>(address: &Address, _: &()) -> Result<(), Error> {
48    if VERIFY && !address.is_alias() {
49        Err(Error::InvalidAddressKind(address.kind()))
50    } else {
51        Ok(())
52    }
53}
54
55#[cfg(feature = "dto")]
56#[allow(missing_docs)]
57pub mod dto {
58    use serde::{Deserialize, Serialize};
59
60    use crate::address::dto::AddressDto;
61
62    #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
63    pub struct ImmutableAliasAddressUnlockConditionDto {
64        #[serde(rename = "type")]
65        pub kind: u8,
66        pub address: AddressDto,
67    }
68}