Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add has_capabilty to restricted address
Browse files Browse the repository at this point in the history
Alex Coats committed Oct 18, 2023
1 parent caa44a6 commit 9377537
Showing 2 changed files with 13 additions and 26 deletions.
5 changes: 5 additions & 0 deletions sdk/src/types/block/address/restricted.rs
Original file line number Diff line number Diff line change
@@ -47,6 +47,11 @@ impl RestrictedAddress {
self.allowed_capabilities = allowed_capabilities.into();
self
}

/// Returns whether a given [`AddressCapabilityFlag`] is enabled.
pub fn has_capability(&self, flag: AddressCapabilityFlag) -> bool {
self.allowed_capabilities.has_capability(flag)
}
}

impl TryFrom<Address> for RestrictedAddress {
34 changes: 8 additions & 26 deletions sdk/src/types/block/semantic.rs
Original file line number Diff line number Diff line change
@@ -341,60 +341,42 @@ pub fn semantic_validation(
.filter_map(Address::as_restricted_opt);
for address in addresses {
if created_output.native_tokens().map(|t| t.len()).unwrap_or_default() > 0
&& !address
.allowed_capabilities()
.has_capability(AddressCapabilityFlag::OutputsWithNativeTokens)
&& !address.has_capability(AddressCapabilityFlag::OutputsWithNativeTokens)
{
// TODO: add a variant https://github.com/iotaledger/iota-sdk/issues/1430
return Ok(Some(TransactionFailureReason::SemanticValidationFailed));
}

if created_output.mana() > 0
&& !address
.allowed_capabilities()
.has_capability(AddressCapabilityFlag::OutputsWithMana)
{
if created_output.mana() > 0 && !address.has_capability(AddressCapabilityFlag::OutputsWithMana) {
// TODO: add a variant https://github.com/iotaledger/iota-sdk/issues/1430
return Ok(Some(TransactionFailureReason::SemanticValidationFailed));
}

if unlock_conditions.timelock().is_some()
&& !address
.allowed_capabilities()
.has_capability(AddressCapabilityFlag::OutputsWithTimelock)
&& !address.has_capability(AddressCapabilityFlag::OutputsWithTimelock)
{
// TODO: add a variant https://github.com/iotaledger/iota-sdk/issues/1430
return Ok(Some(TransactionFailureReason::SemanticValidationFailed));
}

if unlock_conditions.expiration().is_some()
&& !address
.allowed_capabilities()
.has_capability(AddressCapabilityFlag::OutputsWithExpiration)
&& !address.has_capability(AddressCapabilityFlag::OutputsWithExpiration)
{
// TODO: add a variant https://github.com/iotaledger/iota-sdk/issues/1430
return Ok(Some(TransactionFailureReason::SemanticValidationFailed));
}

if unlock_conditions.storage_deposit_return().is_some()
&& !address
.allowed_capabilities()
.has_capability(AddressCapabilityFlag::OutputsWithStorageDepositReturn)
&& !address.has_capability(AddressCapabilityFlag::OutputsWithStorageDepositReturn)
{
// TODO: add a variant https://github.com/iotaledger/iota-sdk/issues/1430
return Ok(Some(TransactionFailureReason::SemanticValidationFailed));
}

if match &created_output {
Output::Account(_) => !address
.allowed_capabilities()
.has_capability(AddressCapabilityFlag::AccountOutputs),
Output::Nft(_) => !address
.allowed_capabilities()
.has_capability(AddressCapabilityFlag::NftOutputs),
Output::Delegation(_) => !address
.allowed_capabilities()
.has_capability(AddressCapabilityFlag::DelegationOutputs),
Output::Account(_) => !address.has_capability(AddressCapabilityFlag::AccountOutputs),
Output::Nft(_) => !address.has_capability(AddressCapabilityFlag::NftOutputs),
Output::Delegation(_) => !address.has_capability(AddressCapabilityFlag::DelegationOutputs),
_ => false,
} {
// TODO: add a variant https://github.com/iotaledger/iota-sdk/issues/1430

0 comments on commit 9377537

Please sign in to comment.