From 2ea51387bd5be3ac295fd07c4a3cb4694761f8b6 Mon Sep 17 00:00:00 2001 From: Philipp Gackstatter Date: Mon, 14 Oct 2024 09:28:40 +0200 Subject: [PATCH] feat(objects): Fix Deserializable for Non Fungible Asset --- objects/src/assets/fungible.rs | 39 ++++++++++++++++++++++++++ objects/src/assets/nonfungible.rs | 46 +++++++++++++++++++++++++++++-- 2 files changed, 83 insertions(+), 2 deletions(-) diff --git a/objects/src/assets/fungible.rs b/objects/src/assets/fungible.rs index 9b6e8267d..d9a9bd78b 100644 --- a/objects/src/assets/fungible.rs +++ b/objects/src/assets/fungible.rs @@ -209,3 +209,42 @@ impl FungibleAsset { .map_err(|err| DeserializationError::InvalidValue(err.to_string())) } } + +// TESTS +// ================================================================================================ + +#[cfg(test)] +mod tests { + use super::*; + use crate::accounts::account_id::testing::{ + ACCOUNT_ID_FUNGIBLE_FAUCET_OFF_CHAIN, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN, + ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_1, ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_2, + ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_3, ACCOUNT_ID_NON_FUNGIBLE_FAUCET_OFF_CHAIN, + }; + + #[test] + fn test_fungible_asset_serde() { + for fungible_account_id in [ + ACCOUNT_ID_FUNGIBLE_FAUCET_OFF_CHAIN, + ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN, + ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_1, + ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_2, + ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_3, + ] { + let account_id = AccountId::try_from(fungible_account_id).unwrap(); + let fungible_asset = FungibleAsset::new(account_id, 10).unwrap(); + assert_eq!( + fungible_asset, + FungibleAsset::read_from_bytes(&fungible_asset.to_bytes()).unwrap() + ); + } + + let account_id = AccountId::try_from(ACCOUNT_ID_FUNGIBLE_FAUCET_ON_CHAIN_3).unwrap(); + let asset = FungibleAsset::new(account_id, 50).unwrap(); + let mut asset_bytes = asset.to_bytes(); + // Set invalid Faucet ID. + asset_bytes[0..8].copy_from_slice(&ACCOUNT_ID_NON_FUNGIBLE_FAUCET_OFF_CHAIN.to_le_bytes()); + let err = FungibleAsset::read_from_bytes(&asset_bytes).unwrap_err(); + assert!(matches!(err, DeserializationError::InvalidValue(_))); + } +} diff --git a/objects/src/assets/nonfungible.rs b/objects/src/assets/nonfungible.rs index fa9c2a3a2..9f18692d0 100644 --- a/objects/src/assets/nonfungible.rs +++ b/objects/src/assets/nonfungible.rs @@ -188,8 +188,10 @@ impl Serializable for NonFungibleAsset { impl Deserializable for NonFungibleAsset { fn read_from(source: &mut R) -> Result { - let value: Word = source.read()?; - Self::try_from(value).map_err(|err| DeserializationError::InvalidValue(err.to_string())) + let faucet_id: AccountId = source.read()?; + + Self::deserialize_with_account_id(faucet_id, source) + .map_err(|err| DeserializationError::InvalidValue(err.to_string())) } } @@ -204,6 +206,8 @@ impl NonFungibleAsset { let hash_2: Felt = source.read()?; let hash_3: Felt = source.read()?; + // The second felt in the data_hash will be replaced by the faucet id, so we can set it to + // zero here. NonFungibleAsset::from_parts(faucet_id, [hash_0, Felt::ZERO, hash_2, hash_3]) .map_err(|err| DeserializationError::InvalidValue(err.to_string())) } @@ -244,3 +248,41 @@ impl NonFungibleAssetDetails { &self.asset_data } } + +// TESTS +// ================================================================================================ + +#[cfg(test)] +mod tests { + use super::*; + use crate::accounts::account_id::testing::{ + ACCOUNT_ID_FUNGIBLE_FAUCET_OFF_CHAIN, ACCOUNT_ID_NON_FUNGIBLE_FAUCET_OFF_CHAIN, + ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN, ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN_1, + }; + + #[test] + fn test_non_fungible_asset_serde() { + for non_fungible_account_id in [ + ACCOUNT_ID_NON_FUNGIBLE_FAUCET_OFF_CHAIN, + ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN, + ACCOUNT_ID_NON_FUNGIBLE_FAUCET_ON_CHAIN_1, + ] { + let account_id = AccountId::try_from(non_fungible_account_id).unwrap(); + let details = NonFungibleAssetDetails::new(account_id, vec![1, 2, 3]).unwrap(); + let non_fungible_asset = NonFungibleAsset::new(&details).unwrap(); + assert_eq!( + non_fungible_asset, + NonFungibleAsset::read_from_bytes(&non_fungible_asset.to_bytes()).unwrap() + ); + } + + let account = AccountId::try_from(ACCOUNT_ID_NON_FUNGIBLE_FAUCET_OFF_CHAIN).unwrap(); + let details = NonFungibleAssetDetails::new(account, vec![4, 5, 6, 7]).unwrap(); + let asset = NonFungibleAsset::new(&details).unwrap(); + let mut asset_bytes = asset.to_bytes(); + // Set invalid Faucet ID. + asset_bytes[0..8].copy_from_slice(&ACCOUNT_ID_FUNGIBLE_FAUCET_OFF_CHAIN.to_le_bytes()); + let err = NonFungibleAsset::read_from_bytes(&asset_bytes).unwrap_err(); + assert!(matches!(err, DeserializationError::InvalidValue(_))); + } +}