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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
// Copyright 2021-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

mod address;
mod expiration;
mod governor_address;
mod immutable_alias_address;
mod state_controller_address;
mod storage_deposit_return;
mod timelock;

use alloc::{boxed::Box, collections::BTreeSet, vec::Vec};

use bitflags::bitflags;
use derive_more::{Deref, From};
use iterator_sorted::is_unique_sorted;
use packable::{
    bounded::BoundedU8,
    error::{UnpackError, UnpackErrorExt},
    packer::Packer,
    prefix::BoxedSlicePrefix,
    unpacker::Unpacker,
    Packable,
};

pub use self::{
    address::AddressUnlockCondition, expiration::ExpirationUnlockCondition,
    governor_address::GovernorAddressUnlockCondition, immutable_alias_address::ImmutableAliasAddressUnlockCondition,
    state_controller_address::StateControllerAddressUnlockCondition,
    storage_deposit_return::StorageDepositReturnUnlockCondition, timelock::TimelockUnlockCondition,
};
use crate::types::block::{address::Address, create_bitflags, protocol::ProtocolParameters, Error};

///
#[derive(Clone, Eq, PartialEq, Hash, From)]
pub enum UnlockCondition {
    /// An address unlock condition.
    Address(AddressUnlockCondition),
    /// A storage deposit return unlock condition.
    StorageDepositReturn(StorageDepositReturnUnlockCondition),
    /// A timelock unlock condition.
    Timelock(TimelockUnlockCondition),
    /// An expiration unlock condition.
    Expiration(ExpirationUnlockCondition),
    /// A state controller address unlock condition.
    StateControllerAddress(StateControllerAddressUnlockCondition),
    /// A governor address unlock condition.
    GovernorAddress(GovernorAddressUnlockCondition),
    /// An immutable alias address unlock condition.
    ImmutableAliasAddress(ImmutableAliasAddressUnlockCondition),
}

impl PartialOrd for UnlockCondition {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        Some(self.cmp(other))
    }
}
impl Ord for UnlockCondition {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.kind().cmp(&other.kind())
    }
}

impl core::fmt::Debug for UnlockCondition {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Address(unlock_condition) => unlock_condition.fmt(f),
            Self::StorageDepositReturn(unlock_condition) => unlock_condition.fmt(f),
            Self::Timelock(unlock_condition) => unlock_condition.fmt(f),
            Self::Expiration(unlock_condition) => unlock_condition.fmt(f),
            Self::StateControllerAddress(unlock_condition) => unlock_condition.fmt(f),
            Self::GovernorAddress(unlock_condition) => unlock_condition.fmt(f),
            Self::ImmutableAliasAddress(unlock_condition) => unlock_condition.fmt(f),
        }
    }
}

impl UnlockCondition {
    /// Return the output kind of an `Output`.
    pub fn kind(&self) -> u8 {
        match self {
            Self::Address(_) => AddressUnlockCondition::KIND,
            Self::StorageDepositReturn(_) => StorageDepositReturnUnlockCondition::KIND,
            Self::Timelock(_) => TimelockUnlockCondition::KIND,
            Self::Expiration(_) => ExpirationUnlockCondition::KIND,
            Self::StateControllerAddress(_) => StateControllerAddressUnlockCondition::KIND,
            Self::GovernorAddress(_) => GovernorAddressUnlockCondition::KIND,
            Self::ImmutableAliasAddress(_) => ImmutableAliasAddressUnlockCondition::KIND,
        }
    }

    /// Returns the [`UnlockConditionFlags`] for the given [`UnlockCondition`].
    pub(crate) fn flag(&self) -> UnlockConditionFlags {
        match self {
            Self::Address(_) => UnlockConditionFlags::ADDRESS,
            Self::StorageDepositReturn(_) => UnlockConditionFlags::STORAGE_DEPOSIT_RETURN,
            Self::Timelock(_) => UnlockConditionFlags::TIMELOCK,
            Self::Expiration(_) => UnlockConditionFlags::EXPIRATION,
            Self::StateControllerAddress(_) => UnlockConditionFlags::STATE_CONTROLLER_ADDRESS,
            Self::GovernorAddress(_) => UnlockConditionFlags::GOVERNOR_ADDRESS,
            Self::ImmutableAliasAddress(_) => UnlockConditionFlags::IMMUTABLE_ALIAS_ADDRESS,
        }
    }

    /// Checks whether the unlock condition is an [`AddressUnlockCondition`].
    pub fn is_address(&self) -> bool {
        matches!(self, Self::Address(_))
    }

    /// Gets the unlock condition as an actual [`AddressUnlockCondition`].
    /// NOTE: Will panic if the unlock condition is not an [`AddressUnlockCondition`].
    pub fn as_address(&self) -> &AddressUnlockCondition {
        if let Self::Address(unlock_condition) = self {
            unlock_condition
        } else {
            panic!("invalid downcast of non-AddressUnlockCondition");
        }
    }

    /// Checks whether the unlock condition is a [`StorageDepositReturnUnlockCondition`].
    pub fn is_storage_deposit_return(&self) -> bool {
        matches!(self, Self::StorageDepositReturn(_))
    }

    /// Gets the unlock condition as an actual [`StorageDepositReturnUnlockCondition`].
    /// NOTE: Will panic if the unlock condition is not a [`StorageDepositReturnUnlockCondition`].
    pub fn as_storage_deposit_return(&self) -> &StorageDepositReturnUnlockCondition {
        if let Self::StorageDepositReturn(unlock_condition) = self {
            unlock_condition
        } else {
            panic!("invalid downcast of non-StorageDepositReturnUnlockCondition");
        }
    }

    /// Checks whether the unlock condition is a [`TimelockUnlockCondition`].
    pub fn is_timelock(&self) -> bool {
        matches!(self, Self::Timelock(_))
    }

    /// Gets the unlock condition as an actual [`TimelockUnlockCondition`].
    /// NOTE: Will panic if the unlock condition is not a [`TimelockUnlockCondition`].
    pub fn as_timelock(&self) -> &TimelockUnlockCondition {
        if let Self::Timelock(unlock_condition) = self {
            unlock_condition
        } else {
            panic!("invalid downcast of non-TimelockUnlockCondition");
        }
    }

    /// Checks whether the unlock condition is an [`ExpirationUnlockCondition`].
    pub fn is_expiration(&self) -> bool {
        matches!(self, Self::Expiration(_))
    }

    /// Gets the unlock condition as an actual [`ExpirationUnlockCondition`].
    /// NOTE: Will panic if the unlock condition is not an [`ExpirationUnlockCondition`].
    pub fn as_expiration(&self) -> &ExpirationUnlockCondition {
        if let Self::Expiration(unlock_condition) = self {
            unlock_condition
        } else {
            panic!("invalid downcast of non-ExpirationUnlockCondition");
        }
    }

    /// Checks whether the unlock condition is a [`StateControllerAddressUnlockCondition`].
    pub fn is_state_controller_address(&self) -> bool {
        matches!(self, Self::StateControllerAddress(_))
    }

    /// Gets the unlock condition as an actual [`StateControllerAddressUnlockCondition`].
    /// NOTE: Will panic if the unlock condition is not a [`StateControllerAddressUnlockCondition`].
    pub fn as_state_controller_address(&self) -> &StateControllerAddressUnlockCondition {
        if let Self::StateControllerAddress(unlock_condition) = self {
            unlock_condition
        } else {
            panic!("invalid downcast of non-StateControllerAddressUnlockCondition");
        }
    }

    /// Checks whether the unlock condition is a [`GovernorAddressUnlockCondition`].
    pub fn is_governor_address(&self) -> bool {
        matches!(self, Self::GovernorAddress(_))
    }

    /// Gets the unlock condition as an actual [`GovernorAddressUnlockCondition`].
    /// NOTE: Will panic if the unlock condition is not a [`GovernorAddressUnlockCondition`].
    pub fn as_governor_address(&self) -> &GovernorAddressUnlockCondition {
        if let Self::GovernorAddress(unlock_condition) = self {
            unlock_condition
        } else {
            panic!("invalid downcast of non-GovernorAddressUnlockCondition");
        }
    }

    /// Checks whether the unlock condition is an [`ImmutableAliasAddressUnlockCondition`].
    pub fn is_immutable_alias_address(&self) -> bool {
        matches!(self, Self::ImmutableAliasAddress(_))
    }

    /// Gets the unlock condition as an actual [`ImmutableAliasAddressUnlockCondition`].
    /// NOTE: Will panic if the unlock condition is not an [`ImmutableAliasAddressUnlockCondition`].
    pub fn as_immutable_alias_address(&self) -> &ImmutableAliasAddressUnlockCondition {
        if let Self::ImmutableAliasAddress(unlock_condition) = self {
            unlock_condition
        } else {
            panic!("invalid downcast of non-ImmutableAliasAddressUnlockCondition");
        }
    }
}

create_bitflags!(
    /// A bitflags-based representation of the set of active [`UnlockCondition`]s.
    pub UnlockConditionFlags,
    u16,
    [
        (ADDRESS, AddressUnlockCondition),
        (STORAGE_DEPOSIT_RETURN, StorageDepositReturnUnlockCondition),
        (TIMELOCK, TimelockUnlockCondition),
        (EXPIRATION, ExpirationUnlockCondition),
        (STATE_CONTROLLER_ADDRESS, StateControllerAddressUnlockCondition),
        (GOVERNOR_ADDRESS, GovernorAddressUnlockCondition),
        (IMMUTABLE_ALIAS_ADDRESS, ImmutableAliasAddressUnlockCondition),
    ]
);

impl Packable for UnlockCondition {
    type UnpackError = Error;
    type UnpackVisitor = ProtocolParameters;

    fn pack<P: Packer>(&self, packer: &mut P) -> Result<(), P::Error> {
        match self {
            Self::Address(unlock_condition) => {
                AddressUnlockCondition::KIND.pack(packer)?;
                unlock_condition.pack(packer)
            }
            Self::StorageDepositReturn(unlock_condition) => {
                StorageDepositReturnUnlockCondition::KIND.pack(packer)?;
                unlock_condition.pack(packer)
            }
            Self::Timelock(unlock_condition) => {
                TimelockUnlockCondition::KIND.pack(packer)?;
                unlock_condition.pack(packer)
            }
            Self::Expiration(unlock_condition) => {
                ExpirationUnlockCondition::KIND.pack(packer)?;
                unlock_condition.pack(packer)
            }
            Self::StateControllerAddress(unlock_condition) => {
                StateControllerAddressUnlockCondition::KIND.pack(packer)?;
                unlock_condition.pack(packer)
            }
            Self::GovernorAddress(unlock_condition) => {
                GovernorAddressUnlockCondition::KIND.pack(packer)?;
                unlock_condition.pack(packer)
            }
            Self::ImmutableAliasAddress(unlock_condition) => {
                ImmutableAliasAddressUnlockCondition::KIND.pack(packer)?;
                unlock_condition.pack(packer)
            }
        }?;

        Ok(())
    }

    fn unpack<U: Unpacker, const VERIFY: bool>(
        unpacker: &mut U,
        visitor: &Self::UnpackVisitor,
    ) -> Result<Self, UnpackError<Self::UnpackError, U::Error>> {
        Ok(match u8::unpack::<_, VERIFY>(unpacker, &()).coerce()? {
            AddressUnlockCondition::KIND => {
                Self::from(AddressUnlockCondition::unpack::<_, VERIFY>(unpacker, &()).coerce()?)
            }
            StorageDepositReturnUnlockCondition::KIND => {
                Self::from(StorageDepositReturnUnlockCondition::unpack::<_, VERIFY>(unpacker, visitor).coerce()?)
            }
            TimelockUnlockCondition::KIND => {
                Self::from(TimelockUnlockCondition::unpack::<_, VERIFY>(unpacker, &()).coerce()?)
            }
            ExpirationUnlockCondition::KIND => {
                Self::from(ExpirationUnlockCondition::unpack::<_, VERIFY>(unpacker, &()).coerce()?)
            }
            StateControllerAddressUnlockCondition::KIND => {
                Self::from(StateControllerAddressUnlockCondition::unpack::<_, VERIFY>(unpacker, &()).coerce()?)
            }
            GovernorAddressUnlockCondition::KIND => {
                Self::from(GovernorAddressUnlockCondition::unpack::<_, VERIFY>(unpacker, &()).coerce()?)
            }
            ImmutableAliasAddressUnlockCondition::KIND => {
                Self::from(ImmutableAliasAddressUnlockCondition::unpack::<_, VERIFY>(unpacker, &()).coerce()?)
            }
            k => return Err(UnpackError::Packable(Error::InvalidOutputKind(k))),
        })
    }
}

pub(crate) type UnlockConditionCount = BoundedU8<0, { UnlockConditions::COUNT_MAX }>;

///
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Deref, Packable)]
#[packable(unpack_error = Error, with = |e| e.unwrap_item_err_or_else(|p| Error::InvalidUnlockConditionCount(p.into())))]
#[packable(unpack_visitor = ProtocolParameters)]
pub struct UnlockConditions(
    #[packable(verify_with = verify_unique_sorted_packable)] BoxedSlicePrefix<UnlockCondition, UnlockConditionCount>,
);

impl TryFrom<Vec<UnlockCondition>> for UnlockConditions {
    type Error = Error;

    #[inline(always)]
    fn try_from(unlock_conditions: Vec<UnlockCondition>) -> Result<Self, Self::Error> {
        Self::from_vec(unlock_conditions)
    }
}

impl TryFrom<BTreeSet<UnlockCondition>> for UnlockConditions {
    type Error = Error;

    #[inline(always)]
    fn try_from(unlock_conditions: BTreeSet<UnlockCondition>) -> Result<Self, Self::Error> {
        Self::from_set(unlock_conditions)
    }
}

impl IntoIterator for UnlockConditions {
    type Item = UnlockCondition;
    type IntoIter = alloc::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        Vec::from(Into::<Box<[UnlockCondition]>>::into(self.0)).into_iter()
    }
}

impl UnlockConditions {
    ///
    pub const COUNT_MAX: u8 = 7;

    /// Creates a new [`UnlockConditions`] from a vec.
    pub fn from_vec(unlock_conditions: Vec<UnlockCondition>) -> Result<Self, Error> {
        let mut unlock_conditions =
            BoxedSlicePrefix::<UnlockCondition, UnlockConditionCount>::try_from(unlock_conditions.into_boxed_slice())
                .map_err(Error::InvalidUnlockConditionCount)?;

        unlock_conditions.sort_by_key(UnlockCondition::kind);
        // Sort is obviously fine now but uniqueness still needs to be checked.
        verify_unique_sorted::<true>(&unlock_conditions)?;

        Ok(Self(unlock_conditions))
    }

    /// Creates a new [`UnlockConditions`] from an ordered set.
    pub fn from_set(unlock_conditions: BTreeSet<UnlockCondition>) -> Result<Self, Error> {
        Ok(Self(
            unlock_conditions
                .into_iter()
                .collect::<Box<[_]>>()
                .try_into()
                .map_err(Error::InvalidUnlockConditionCount)?,
        ))
    }

    /// Gets a reference to an [`UnlockCondition`] from an unlock condition kind, if any.
    #[inline(always)]
    pub fn get(&self, key: u8) -> Option<&UnlockCondition> {
        self.0
            .binary_search_by_key(&key, UnlockCondition::kind)
            // PANIC: indexation is fine since the index has been found.
            .map(|index| &self.0[index])
            .ok()
    }

    /// Gets a reference to an [`AddressUnlockCondition`], if any.
    #[inline(always)]
    pub fn address(&self) -> Option<&AddressUnlockCondition> {
        self.get(AddressUnlockCondition::KIND).map(UnlockCondition::as_address)
    }

    /// Gets a reference to a [`StorageDepositReturnUnlockCondition`], if any.
    #[inline(always)]
    pub fn storage_deposit_return(&self) -> Option<&StorageDepositReturnUnlockCondition> {
        self.get(StorageDepositReturnUnlockCondition::KIND)
            .map(UnlockCondition::as_storage_deposit_return)
    }

    /// Gets a reference to a [`TimelockUnlockCondition`], if any.
    #[inline(always)]
    pub fn timelock(&self) -> Option<&TimelockUnlockCondition> {
        self.get(TimelockUnlockCondition::KIND)
            .map(UnlockCondition::as_timelock)
    }

    /// Gets a reference to an [`ExpirationUnlockCondition`], if any.
    #[inline(always)]
    pub fn expiration(&self) -> Option<&ExpirationUnlockCondition> {
        self.get(ExpirationUnlockCondition::KIND)
            .map(UnlockCondition::as_expiration)
    }

    /// Gets a reference to a [`StateControllerAddressUnlockCondition`], if any.
    #[inline(always)]
    pub fn state_controller_address(&self) -> Option<&StateControllerAddressUnlockCondition> {
        self.get(StateControllerAddressUnlockCondition::KIND)
            .map(UnlockCondition::as_state_controller_address)
    }

    /// Gets a reference to a [`GovernorAddressUnlockCondition`], if any.
    #[inline(always)]
    pub fn governor_address(&self) -> Option<&GovernorAddressUnlockCondition> {
        self.get(GovernorAddressUnlockCondition::KIND)
            .map(UnlockCondition::as_governor_address)
    }

    /// Gets a reference to an [`ImmutableAliasAddressUnlockCondition`], if any.
    #[inline(always)]
    pub fn immutable_alias_address(&self) -> Option<&ImmutableAliasAddressUnlockCondition> {
        self.get(ImmutableAliasAddressUnlockCondition::KIND)
            .map(UnlockCondition::as_immutable_alias_address)
    }

    /// Returns the address to be unlocked.
    #[inline(always)]
    pub fn locked_address<'a>(&'a self, address: &'a Address, milestone_timestamp: u32) -> &'a Address {
        self.expiration()
            .and_then(|e| e.return_address_expired(milestone_timestamp))
            .unwrap_or(address)
    }

    /// Returns whether a time lock exists and is still relevant.
    #[inline(always)]
    pub fn is_time_locked(&self, milestone_timestamp: u32) -> bool {
        self.timelock()
            .map_or(false, |timelock| milestone_timestamp < timelock.timestamp())
    }

    /// Returns whether an expiration exists and is expired.
    #[inline(always)]
    pub fn is_expired(&self, milestone_timestamp: u32) -> bool {
        self.expiration()
            .map_or(false, |expiration| milestone_timestamp >= expiration.timestamp())
    }
}

#[inline]
fn verify_unique_sorted<const VERIFY: bool>(unlock_conditions: &[UnlockCondition]) -> Result<(), Error> {
    if VERIFY && !is_unique_sorted(unlock_conditions.iter().map(UnlockCondition::kind)) {
        Err(Error::UnlockConditionsNotUniqueSorted)
    } else {
        Ok(())
    }
}

#[inline]
fn verify_unique_sorted_packable<const VERIFY: bool>(
    unlock_conditions: &[UnlockCondition],
    _: &ProtocolParameters,
) -> Result<(), Error> {
    verify_unique_sorted::<VERIFY>(unlock_conditions)
}

pub(crate) fn verify_allowed_unlock_conditions(
    unlock_conditions: &UnlockConditions,
    allowed_unlock_conditions: UnlockConditionFlags,
) -> Result<(), Error> {
    for (index, unlock_condition) in unlock_conditions.iter().enumerate() {
        if !allowed_unlock_conditions.contains(unlock_condition.flag()) {
            return Err(Error::UnallowedUnlockCondition {
                index,
                kind: unlock_condition.kind(),
            });
        }
    }

    Ok(())
}

#[cfg(test)]
mod test {
    use pretty_assertions::assert_eq;

    use super::*;

    #[test]
    fn all_flags_present() {
        assert_eq!(
            UnlockConditionFlags::ALL_FLAGS,
            &[
                UnlockConditionFlags::ADDRESS,
                UnlockConditionFlags::STORAGE_DEPOSIT_RETURN,
                UnlockConditionFlags::TIMELOCK,
                UnlockConditionFlags::EXPIRATION,
                UnlockConditionFlags::STATE_CONTROLLER_ADDRESS,
                UnlockConditionFlags::GOVERNOR_ADDRESS,
                UnlockConditionFlags::IMMUTABLE_ALIAS_ADDRESS
            ]
        );
    }
}

#[cfg(feature = "serde")]
pub mod dto {
    use alloc::format;

    use serde::{Deserialize, Serialize, Serializer};
    use serde_json::Value;

    pub use self::{
        address::dto::AddressUnlockConditionDto, expiration::dto::ExpirationUnlockConditionDto,
        governor_address::dto::GovernorAddressUnlockConditionDto,
        immutable_alias_address::dto::ImmutableAliasAddressUnlockConditionDto,
        state_controller_address::dto::StateControllerAddressUnlockConditionDto,
        storage_deposit_return::dto::StorageDepositReturnUnlockConditionDto, timelock::dto::TimelockUnlockConditionDto,
    };
    use super::*;
    use crate::types::{block::Error, TryFromDto, ValidationParams};

    #[derive(Clone, Debug, Eq, PartialEq, From)]
    pub enum UnlockConditionDto {
        /// An address unlock condition.
        Address(AddressUnlockConditionDto),
        /// A storage deposit return unlock condition.
        StorageDepositReturn(StorageDepositReturnUnlockConditionDto),
        /// A timelock unlock condition.
        Timelock(TimelockUnlockConditionDto),
        /// An expiration unlock condition.
        Expiration(ExpirationUnlockConditionDto),
        /// A state controller address unlock condition.
        StateControllerAddress(StateControllerAddressUnlockConditionDto),
        /// A governor address unlock condition.
        GovernorAddress(GovernorAddressUnlockConditionDto),
        /// An immutable alias address unlock condition.
        ImmutableAliasAddress(ImmutableAliasAddressUnlockConditionDto),
    }

    impl<'de> Deserialize<'de> for UnlockConditionDto {
        fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
            let value = Value::deserialize(d)?;
            Ok(
                match value
                    .get("type")
                    .and_then(Value::as_u64)
                    .ok_or_else(|| serde::de::Error::custom("invalid unlock condition type"))?
                    as u8
                {
                    AddressUnlockCondition::KIND => {
                        Self::Address(AddressUnlockConditionDto::deserialize(value).map_err(|e| {
                            serde::de::Error::custom(format!("cannot deserialize address unlock condition: {e}"))
                        })?)
                    }
                    StorageDepositReturnUnlockCondition::KIND => Self::StorageDepositReturn(
                        StorageDepositReturnUnlockConditionDto::deserialize(value).map_err(|e| {
                            serde::de::Error::custom(format!(
                                "cannot deserialize storage deposit unlock condition: {e}"
                            ))
                        })?,
                    ),
                    TimelockUnlockCondition::KIND => {
                        Self::Timelock(TimelockUnlockConditionDto::deserialize(value).map_err(|e| {
                            serde::de::Error::custom(format!("cannot deserialize timelock unlock condition: {e}"))
                        })?)
                    }
                    ExpirationUnlockCondition::KIND => {
                        Self::Expiration(ExpirationUnlockConditionDto::deserialize(value).map_err(|e| {
                            serde::de::Error::custom(format!("cannot deserialize expiration unlock condition: {e}"))
                        })?)
                    }
                    StateControllerAddressUnlockCondition::KIND => Self::StateControllerAddress(
                        StateControllerAddressUnlockConditionDto::deserialize(value).map_err(|e| {
                            serde::de::Error::custom(format!(
                                "cannot deserialize state controller unlock condition: {e}"
                            ))
                        })?,
                    ),
                    GovernorAddressUnlockCondition::KIND => {
                        Self::GovernorAddress(GovernorAddressUnlockConditionDto::deserialize(value).map_err(|e| {
                            serde::de::Error::custom(format!("cannot deserialize governor unlock condition: {e}"))
                        })?)
                    }
                    ImmutableAliasAddressUnlockCondition::KIND => Self::ImmutableAliasAddress(
                        ImmutableAliasAddressUnlockConditionDto::deserialize(value).map_err(|e| {
                            serde::de::Error::custom(format!(
                                "cannot deserialize immutable alias address unlock condition: {e}"
                            ))
                        })?,
                    ),
                    _ => return Err(serde::de::Error::custom("invalid unlock condition type")),
                },
            )
        }
    }

    impl Serialize for UnlockConditionDto {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
        where
            S: Serializer,
        {
            #[derive(Serialize)]
            #[serde(untagged)]
            enum UnlockConditionDto_<'a> {
                T1(&'a AddressUnlockConditionDto),
                T2(&'a StorageDepositReturnUnlockConditionDto),
                T3(&'a TimelockUnlockConditionDto),
                T4(&'a ExpirationUnlockConditionDto),
                T5(&'a StateControllerAddressUnlockConditionDto),
                T6(&'a GovernorAddressUnlockConditionDto),
                T7(&'a ImmutableAliasAddressUnlockConditionDto),
            }
            #[derive(Serialize)]
            struct TypedUnlockCondition<'a> {
                #[serde(flatten)]
                unlock_condition: UnlockConditionDto_<'a>,
            }
            let unlock_condition = match self {
                Self::Address(o) => TypedUnlockCondition {
                    unlock_condition: UnlockConditionDto_::T1(o),
                },
                Self::StorageDepositReturn(o) => TypedUnlockCondition {
                    unlock_condition: UnlockConditionDto_::T2(o),
                },
                Self::Timelock(o) => TypedUnlockCondition {
                    unlock_condition: UnlockConditionDto_::T3(o),
                },
                Self::Expiration(o) => TypedUnlockCondition {
                    unlock_condition: UnlockConditionDto_::T4(o),
                },
                Self::StateControllerAddress(o) => TypedUnlockCondition {
                    unlock_condition: UnlockConditionDto_::T5(o),
                },
                Self::GovernorAddress(o) => TypedUnlockCondition {
                    unlock_condition: UnlockConditionDto_::T6(o),
                },
                Self::ImmutableAliasAddress(o) => TypedUnlockCondition {
                    unlock_condition: UnlockConditionDto_::T7(o),
                },
            };
            unlock_condition.serialize(serializer)
        }
    }

    impl From<&UnlockCondition> for UnlockConditionDto {
        fn from(value: &UnlockCondition) -> Self {
            match value {
                UnlockCondition::Address(v) => Self::Address(AddressUnlockConditionDto::from(v)),
                UnlockCondition::StorageDepositReturn(v) => {
                    Self::StorageDepositReturn(StorageDepositReturnUnlockConditionDto::from(v))
                }
                UnlockCondition::Timelock(v) => Self::Timelock(TimelockUnlockConditionDto::from(v)),
                UnlockCondition::Expiration(v) => Self::Expiration(ExpirationUnlockConditionDto::from(v)),
                UnlockCondition::StateControllerAddress(v) => {
                    Self::StateControllerAddress(StateControllerAddressUnlockConditionDto::from(v))
                }
                UnlockCondition::GovernorAddress(v) => {
                    Self::GovernorAddress(GovernorAddressUnlockConditionDto::from(v))
                }
                UnlockCondition::ImmutableAliasAddress(v) => {
                    Self::ImmutableAliasAddress(ImmutableAliasAddressUnlockConditionDto::from(v))
                }
            }
        }
    }

    impl TryFromDto for UnlockCondition {
        type Dto = UnlockConditionDto;
        type Error = Error;

        fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result<Self, Self::Error> {
            Ok(match dto {
                UnlockConditionDto::Address(v) => Self::Address(AddressUnlockCondition::try_from(v)?),
                UnlockConditionDto::StorageDepositReturn(v) => Self::StorageDepositReturn(
                    StorageDepositReturnUnlockCondition::try_from_dto_with_params_inner(v, params)?,
                ),
                UnlockConditionDto::Timelock(v) => Self::Timelock(TimelockUnlockCondition::try_from(v)?),
                UnlockConditionDto::Expiration(v) => Self::Expiration(ExpirationUnlockCondition::try_from(v)?),
                UnlockConditionDto::StateControllerAddress(v) => {
                    Self::StateControllerAddress(StateControllerAddressUnlockCondition::try_from(v)?)
                }
                UnlockConditionDto::GovernorAddress(v) => {
                    Self::GovernorAddress(GovernorAddressUnlockCondition::try_from(v)?)
                }
                UnlockConditionDto::ImmutableAliasAddress(v) => {
                    Self::ImmutableAliasAddress(ImmutableAliasAddressUnlockCondition::try_from(v)?)
                }
            })
        }
    }

    impl UnlockConditionDto {
        /// Return the unlock condition kind of a `UnlockConditionDto`.
        pub fn kind(&self) -> u8 {
            match self {
                Self::Address(_) => AddressUnlockCondition::KIND,
                Self::StorageDepositReturn(_) => StorageDepositReturnUnlockCondition::KIND,
                Self::Timelock(_) => TimelockUnlockCondition::KIND,
                Self::Expiration(_) => ExpirationUnlockCondition::KIND,
                Self::StateControllerAddress(_) => StateControllerAddressUnlockCondition::KIND,
                Self::GovernorAddress(_) => GovernorAddressUnlockCondition::KIND,
                Self::ImmutableAliasAddress(_) => ImmutableAliasAddressUnlockCondition::KIND,
            }
        }
    }
}