-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add MultiUnlock * Add EmptyUnlock * Make WeightedAddress public * Fix packable compilation issue * Nits * Nit * return errors * Remove variable * Derive deref * Typo * More deref * UnlocksCount = WeightedAddressCount * nit * Fix verify_unlocks * no_std * Update sdk/src/types/block/unlock/multi.rs Co-authored-by: Thoralf-M <[email protected]> * Update sdk/src/types/block/unlock/empty.rs Co-authored-by: Thoralf-M <[email protected]> * Fmt * Comment and rename * Move import to dto * Order * review --------- Co-authored-by: Thoralf-M <[email protected]> Co-authored-by: DaughterOfMars <[email protected]>
- Loading branch information
1 parent
b0660db
commit b1762a3
Showing
6 changed files
with
218 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2023 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
/// Used to maintain correct index relationship between addresses and signatures when unlocking a | ||
/// [`MultiAddress`](crate::types::block::address::MultiAddress) where not all addresses are unlocked. | ||
#[derive(Clone, Debug, Eq, PartialEq, Hash, packable::Packable)] | ||
pub struct EmptyUnlock; | ||
|
||
impl EmptyUnlock { | ||
/// The [`Unlock`](crate::types::block::unlock::Unlock) kind of an [`EmptyUnlock`]. | ||
pub const KIND: u8 = 6; | ||
} | ||
|
||
mod dto { | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::*; | ||
use crate::types::block::Error; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct EmptyUnlockDto { | ||
#[serde(rename = "type")] | ||
kind: u8, | ||
} | ||
|
||
impl From<&EmptyUnlock> for EmptyUnlockDto { | ||
fn from(_: &EmptyUnlock) -> Self { | ||
Self { | ||
kind: EmptyUnlock::KIND, | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<EmptyUnlockDto> for EmptyUnlock { | ||
type Error = Error; | ||
|
||
fn try_from(_: EmptyUnlockDto) -> Result<Self, Self::Error> { | ||
Ok(Self) | ||
} | ||
} | ||
|
||
crate::impl_serde_typed_dto!(EmptyUnlock, EmptyUnlockDto, "empty unlock"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2023 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use alloc::{boxed::Box, vec::Vec}; | ||
|
||
use derive_more::Deref; | ||
use packable::{prefix::BoxedSlicePrefix, Packable}; | ||
|
||
use crate::types::block::{address::WeightedAddressCount, unlock::Unlock, Error}; | ||
|
||
pub(crate) type UnlocksCount = WeightedAddressCount; | ||
|
||
/// Unlocks a [`MultiAddress`](crate::types::block::address::MultiAddress) with a list of other unlocks. | ||
#[derive(Clone, Debug, Deref, Eq, PartialEq, Hash, Packable)] | ||
#[packable(unpack_error = Error, with = |e| e.unwrap_item_err_or_else(|p| Error::InvalidMultiUnlockCount(p.into())))] | ||
pub struct MultiUnlock(#[packable(verify_with = verify_unlocks)] BoxedSlicePrefix<Unlock, UnlocksCount>); | ||
|
||
impl MultiUnlock { | ||
/// The [`Unlock`](crate::types::block::unlock::Unlock) kind of an [`MultiUnlock`]. | ||
pub const KIND: u8 = 5; | ||
|
||
/// Creates a new [`MultiUnlock`]. | ||
#[inline(always)] | ||
pub fn new(unlocks: impl IntoIterator<Item = Unlock>) -> Result<Self, Error> { | ||
let unlocks = unlocks.into_iter().collect::<Box<[_]>>(); | ||
|
||
verify_unlocks::<true>(&unlocks, &())?; | ||
|
||
Ok(Self( | ||
BoxedSlicePrefix::<Unlock, UnlocksCount>::try_from(unlocks).map_err(Error::InvalidMultiUnlockCount)?, | ||
)) | ||
} | ||
|
||
/// Return the inner unlocks of an [`MultiUnlock`]. | ||
#[inline(always)] | ||
pub fn unlocks(&self) -> &[Unlock] { | ||
&self.0 | ||
} | ||
} | ||
|
||
fn verify_unlocks<const VERIFY: bool>(unlocks: &[Unlock], _visitor: &()) -> Result<(), Error> { | ||
if VERIFY && unlocks.iter().any(Unlock::is_multi) { | ||
return Err(Error::MultiUnlockRecursion); | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
|
||
mod dto { | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use super::*; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
struct MultiUnlockDto { | ||
#[serde(rename = "type")] | ||
kind: u8, | ||
unlocks: Vec<Unlock>, | ||
} | ||
|
||
impl From<&MultiUnlock> for MultiUnlockDto { | ||
fn from(value: &MultiUnlock) -> Self { | ||
Self { | ||
kind: MultiUnlock::KIND, | ||
unlocks: value.0.to_vec(), | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<MultiUnlockDto> for MultiUnlock { | ||
type Error = Error; | ||
|
||
fn try_from(value: MultiUnlockDto) -> Result<Self, Self::Error> { | ||
Self::new(value.unlocks) | ||
} | ||
} | ||
|
||
crate::impl_serde_typed_dto!(MultiUnlock, MultiUnlockDto, "multi unlock"); | ||
} |