Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ImmutableAccountAddressUnlockCondition inner type #1838

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
use derive_more::From;

use crate::types::block::{
address::AccountAddress,
address::{AccountAddress, Address},
error::Error,
output::{StorageScore, StorageScoreParameters},
};

/// Defines the permanent [`AccountAddress`] that owns this output.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, From, packable::Packable)]
pub struct ImmutableAccountAddressUnlockCondition(AccountAddress);
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, From, packable::Packable)]
pub struct ImmutableAccountAddressUnlockCondition(#[packable(verify_with = verify_address)] Address);

impl ImmutableAccountAddressUnlockCondition {
/// The [`UnlockCondition`](crate::types::block::output::UnlockCondition) kind of an
Expand All @@ -20,12 +21,12 @@ impl ImmutableAccountAddressUnlockCondition {
/// Creates a new [`ImmutableAccountAddressUnlockCondition`].
#[inline(always)]
pub fn new(address: impl Into<AccountAddress>) -> Self {
Self(address.into())
Self(Address::from(address.into()))
}

/// Returns the account address of an [`ImmutableAccountAddressUnlockCondition`].
pub fn address(&self) -> &AccountAddress {
&self.0
self.0.as_account()
}
}

Expand All @@ -35,6 +36,15 @@ impl StorageScore for ImmutableAccountAddressUnlockCondition {
}
}

#[inline]
fn verify_address<const VERIFY: bool>(address: &Address) -> Result<(), Error> {
if VERIFY && !address.is_account() {
Err(Error::InvalidAddressKind(address.kind()))
} else {
Ok(())
}
}

#[cfg(feature = "serde")]
mod dto {
use serde::{Deserialize, Serialize};
Expand All @@ -45,14 +55,14 @@ mod dto {
struct ImmutableAccountAddressUnlockConditionDto {
#[serde(rename = "type")]
kind: u8,
address: AccountAddress,
address: Address,
}

impl From<&ImmutableAccountAddressUnlockCondition> for ImmutableAccountAddressUnlockConditionDto {
fn from(value: &ImmutableAccountAddressUnlockCondition) -> Self {
Self {
kind: ImmutableAccountAddressUnlockCondition::KIND,
address: value.0,
address: value.0.clone(),
}
}
}
Expand Down
Loading