Skip to content

Commit

Permalink
Use it with Packable
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez committed Jan 10, 2024
1 parent 964642d commit db4dddf
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
9 changes: 3 additions & 6 deletions sdk/src/types/block/output/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,8 @@ impl Packable for AccountOutput {
let features = Features::unpack::<_, VERIFY>(unpacker, &())?;

if VERIFY {
verify_restricted_addresses(&unlock_conditions, AccountOutput::KIND, features.native_token(), mana)
.map_err(UnpackError::Packable)?;
verify_allowed_features(&features, Self::ALLOWED_FEATURES).map_err(UnpackError::Packable)?;
}

Expand Down Expand Up @@ -620,12 +622,7 @@ mod tests {

use super::*;
use crate::types::block::{
output::account::dto::AccountOutputDto,
protocol::protocol_parameters,
rand::output::{
feature::rand_allowed_features, rand_account_id, rand_account_output,
unlock_condition::rand_address_unlock_condition_different_from_account_id,
},
output::account::dto::AccountOutputDto, protocol::protocol_parameters, rand::output::rand_account_output,
};

#[test]
Expand Down
13 changes: 3 additions & 10 deletions sdk/src/types/block/output/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,8 @@ impl Packable for AnchorOutput {
let features = Features::unpack::<_, VERIFY>(unpacker, &())?;

if VERIFY {
verify_restricted_addresses(&unlock_conditions, AnchorOutput::KIND, features.native_token(), mana)
.map_err(UnpackError::Packable)?;
verify_allowed_features(&features, Self::ALLOWED_FEATURES).map_err(UnpackError::Packable)?;
}

Expand Down Expand Up @@ -684,16 +686,7 @@ mod dto {
mod tests {
use super::*;
use crate::types::block::{
output::anchor::dto::AnchorOutputDto,
protocol::protocol_parameters,
rand::output::{
feature::rand_allowed_features,
rand_anchor_id, rand_anchor_output,
unlock_condition::{
rand_governor_address_unlock_condition_different_from,
rand_state_controller_address_unlock_condition_different_from,
},
},
output::anchor::dto::AnchorOutputDto, protocol::protocol_parameters, rand::output::rand_anchor_output,
};

#[test]
Expand Down
10 changes: 10 additions & 0 deletions sdk/src/types/block/output/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ impl From<&BasicOutput> for BasicOutputBuilder {
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Packable)]
#[packable(unpack_error = Error)]
#[packable(unpack_visitor = ProtocolParameters)]
#[packable(verify_with = verify_basic_output)]
pub struct BasicOutput {
/// Amount of IOTA coins held by the output.
amount: u64,
Expand Down Expand Up @@ -398,6 +399,15 @@ fn verify_features_packable<const VERIFY: bool>(features: &Features, _: &Protoco
verify_features::<VERIFY>(features)
}

fn verify_basic_output<const VERIFY: bool>(output: &BasicOutput, _: &ProtocolParameters) -> Result<(), Error> {
verify_restricted_addresses(
output.unlock_conditions(),
BasicOutput::KIND,
output.features.native_token(),
output.mana,
)
}

#[cfg(feature = "serde")]
mod dto {
use alloc::vec::Vec;
Expand Down
8 changes: 8 additions & 0 deletions sdk/src/types/block/output/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ impl From<&DelegationOutput> for DelegationOutputBuilder {
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Packable)]
#[packable(unpack_error = Error)]
#[packable(unpack_visitor = ProtocolParameters)]
#[packable(verify_with = verify_delegation_output)]
pub struct DelegationOutput {
/// Amount of IOTA coins held by the output.
amount: u64,
Expand Down Expand Up @@ -396,6 +397,13 @@ fn verify_unlock_conditions_packable<const VERIFY: bool>(
verify_unlock_conditions::<VERIFY>(unlock_conditions)
}

fn verify_delegation_output<const VERIFY: bool>(
output: &DelegationOutput,
_: &ProtocolParameters,
) -> Result<(), Error> {
verify_restricted_addresses(output.unlock_conditions(), DelegationOutput::KIND, None, 0)
}

#[cfg(feature = "serde")]
mod dto {
use alloc::vec::Vec;
Expand Down
2 changes: 2 additions & 0 deletions sdk/src/types/block/output/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,8 @@ impl Packable for NftOutput {
let features = Features::unpack::<_, VERIFY>(unpacker, &())?;

if VERIFY {
verify_restricted_addresses(&unlock_conditions, NftOutput::KIND, features.native_token(), mana)
.map_err(UnpackError::Packable)?;
verify_allowed_features(&features, Self::ALLOWED_FEATURES).map_err(UnpackError::Packable)?;
}

Expand Down

0 comments on commit db4dddf

Please sign in to comment.