iota_types/stardust/output/
unlock_conditions.rs

1// Copyright (c) 2024 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use iota_stardust_sdk::types::block::address::Address;
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7use serde_with::serde_as;
8
9use crate::{base_types::IotaAddress, stardust::stardust_to_iota_address};
10
11/// Rust version of the stardust expiration unlock condition.
12#[serde_as]
13#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, JsonSchema)]
14pub struct ExpirationUnlockCondition {
15    /// The address who owns the output before the timestamp has passed.
16    pub owner: IotaAddress,
17    /// The address that is allowed to spend the locked funds after the
18    /// timestamp has passed.
19    pub return_address: IotaAddress,
20    /// Before this unix time, Address Unlock Condition is allowed to unlock the
21    /// output, after that only the address defined in Return Address.
22    pub unix_time: u32,
23}
24
25impl ExpirationUnlockCondition {
26    pub(crate) fn new(
27        owner_address: &Address,
28        expiration_unlock_condition: &iota_stardust_sdk::types::block::output::unlock_condition::ExpirationUnlockCondition,
29    ) -> anyhow::Result<Self> {
30        let owner = stardust_to_iota_address(owner_address)?;
31        let return_address =
32            stardust_to_iota_address(expiration_unlock_condition.return_address())?;
33        let unix_time = expiration_unlock_condition.timestamp();
34
35        Ok(Self {
36            owner,
37            return_address,
38            unix_time,
39        })
40    }
41}
42
43/// Rust version of the stardust storage deposit return unlock condition.
44#[serde_as]
45#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, JsonSchema)]
46pub struct StorageDepositReturnUnlockCondition {
47    /// The address to which the consuming transaction should deposit the amount
48    /// defined in Return Amount.
49    pub return_address: IotaAddress,
50    /// The amount of IOTA coins the consuming transaction should deposit to the
51    /// address defined in Return Address.
52    pub return_amount: u64,
53}
54
55impl TryFrom<&iota_stardust_sdk::types::block::output::unlock_condition::StorageDepositReturnUnlockCondition>
56    for StorageDepositReturnUnlockCondition
57{
58    type Error = anyhow::Error;
59
60    fn try_from(
61        unlock: &iota_stardust_sdk::types::block::output::unlock_condition::StorageDepositReturnUnlockCondition,
62    ) -> Result<Self, Self::Error> {
63        let return_address = unlock.return_address().to_string().parse()?;
64        let return_amount = unlock.amount();
65        Ok(Self {
66            return_address,
67            return_amount,
68        })
69    }
70}
71
72/// Rust version of the stardust timelock unlock condition.
73#[serde_as]
74#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq, JsonSchema)]
75pub struct TimelockUnlockCondition {
76    /// The unix time (seconds since Unix epoch) starting from which the output
77    /// can be consumed.
78    pub unix_time: u32,
79}
80
81impl From<&iota_stardust_sdk::types::block::output::unlock_condition::TimelockUnlockCondition>
82    for TimelockUnlockCondition
83{
84    fn from(
85        unlock: &iota_stardust_sdk::types::block::output::unlock_condition::TimelockUnlockCondition,
86    ) -> Self {
87        Self {
88            unix_time: unlock.timestamp(),
89        }
90    }
91}