From 03b52bbbf04215b86dc738d604bee6b11d307959 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thoralf=20M=C3=BCller?= Date: Thu, 11 Jan 2024 10:09:13 +0100 Subject: [PATCH] Add TryFrom, Vec)>> for MetadataFeature, uncomment test --- .../types/block/output/feature/metadata.rs | 25 ++++ .../high_level/minting/create_native_token.rs | 2 - .../client/input_selection/nft_outputs.rs | 119 +++++++++--------- 3 files changed, 84 insertions(+), 62 deletions(-) diff --git a/sdk/src/types/block/output/feature/metadata.rs b/sdk/src/types/block/output/feature/metadata.rs index a3de25dd5f..719f8bf6db 100644 --- a/sdk/src/types/block/output/feature/metadata.rs +++ b/sdk/src/types/block/output/feature/metadata.rs @@ -89,6 +89,31 @@ impl StorageScore for MetadataFeature {} impl WorkScore for MetadataFeature {} +impl TryFrom, Vec)>> for MetadataFeature { + type Error = Error; + + fn try_from(data: Vec<(Vec, Vec)>) -> Result { + let mut res = BTreeMap::< + BoxedSlicePrefix, + BoxedSlicePrefix, + >::new(); + for (k, v) in data { + if !k.iter().all(|b| b.is_ascii_graphic()) { + return Err(Error::NonAsciiMetadataKey(k.to_vec())); + } + res.insert( + k.into_boxed_slice() + .try_into() + .map_err(Error::InvalidMetadataFeatureKeyLength)?, + v.into_boxed_slice() + .try_into() + .map_err(Error::InvalidMetadataFeatureValueLength)?, + ); + } + Ok(Self(res.try_into().map_err(Error::InvalidMetadataFeatureLength)?)) + } +} + impl TryFrom, Vec>> for MetadataFeature { type Error = Error; diff --git a/sdk/src/wallet/operations/transaction/high_level/minting/create_native_token.rs b/sdk/src/wallet/operations/transaction/high_level/minting/create_native_token.rs index 0206c59bad..1cb8695367 100644 --- a/sdk/src/wallet/operations/transaction/high_level/minting/create_native_token.rs +++ b/sdk/src/wallet/operations/transaction/high_level/minting/create_native_token.rs @@ -1,8 +1,6 @@ // Copyright 2022 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -use alloc::collections::BTreeMap; - use primitive_types::U256; use serde::{Deserialize, Serialize}; diff --git a/sdk/tests/client/input_selection/nft_outputs.rs b/sdk/tests/client/input_selection/nft_outputs.rs index ff23a74eb7..c2318ad35c 100644 --- a/sdk/tests/client/input_selection/nft_outputs.rs +++ b/sdk/tests/client/input_selection/nft_outputs.rs @@ -1210,68 +1210,67 @@ fn transitioned_zero_nft_id_no_longer_is_zero() { }); } -// TODO: enable again when Irc27Metadata is updated -// #[test] -// fn changed_immutable_metadata() { -// let protocol_parameters = protocol_parameters(); -// let nft_id_1 = NftId::from_str(NFT_ID_1).unwrap(); +#[test] +fn changed_immutable_metadata() { + let protocol_parameters = protocol_parameters(); + let nft_id_1 = NftId::from_str(NFT_ID_1).unwrap(); -// #[cfg(feature = "irc_27")] -// let metadata = iota_sdk::types::block::output::feature::Irc27Metadata::new( -// "image/jpeg", -// "https://mywebsite.com/my-nft-files-1.jpeg".parse().unwrap(), -// "file 1", -// ) -// .with_issuer_name("Alice"); -// #[cfg(not(feature = "irc_27"))] -// let metadata = [1, 2, 3]; - -// let nft_output = -// NftOutputBuilder::new_with_minimum_amount(protocol_parameters.storage_score_parameters(), nft_id_1) -// .with_immutable_features(MetadataFeature::try_from(metadata)) -// .add_unlock_condition(AddressUnlockCondition::new( -// Address::try_from_bech32(BECH32_ADDRESS_ED25519_0).unwrap(), -// )) -// .finish_output() -// .unwrap(); - -// let inputs = [InputSigningData { -// output: nft_output.clone(), -// output_metadata: rand_output_metadata(), -// chain: None, -// }]; - -// #[cfg(feature = "irc_27")] -// let metadata = iota_sdk::types::block::output::feature::Irc27Metadata::new( -// "image/jpeg", -// "https://mywebsite.com/my-nft-files-2.jpeg".parse().unwrap(), -// "file 2", -// ) -// .with_issuer_name("Alice"); -// #[cfg(not(feature = "irc_27"))] -// let metadata = [4, 5, 6]; + #[cfg(feature = "irc_27")] + let metadata = iota_sdk::types::block::output::feature::Irc27Metadata::new( + "image/jpeg", + "https://mywebsite.com/my-nft-files-1.jpeg".parse().unwrap(), + "file 1", + ) + .with_issuer_name("Alice"); + #[cfg(not(feature = "irc_27"))] + let metadata = vec![(vec![42], vec![42])]; + + let nft_output = + NftOutputBuilder::new_with_minimum_amount(protocol_parameters.storage_score_parameters(), nft_id_1) + .with_immutable_features(MetadataFeature::try_from(metadata)) + .add_unlock_condition(AddressUnlockCondition::new( + Address::try_from_bech32(BECH32_ADDRESS_ED25519_0).unwrap(), + )) + .finish_output() + .unwrap(); + + let inputs = [InputSigningData { + output: nft_output.clone(), + output_metadata: rand_output_metadata(), + chain: None, + }]; + + #[cfg(feature = "irc_27")] + let metadata = iota_sdk::types::block::output::feature::Irc27Metadata::new( + "image/jpeg", + "https://mywebsite.com/my-nft-files-2.jpeg".parse().unwrap(), + "file 2", + ) + .with_issuer_name("Alice"); + #[cfg(not(feature = "irc_27"))] + let metadata = vec![(vec![43], vec![43])]; -// // New nft output with changed immutable metadata feature -// let updated_nft_output = NftOutputBuilder::from(nft_output.as_nft()) -// .with_minimum_amount(protocol_parameters.storage_score_parameters()) -// .with_immutable_features(MetadataFeature::try_from(metadata)) -// .finish_output() -// .unwrap(); + // New nft output with changed immutable metadata feature + let updated_nft_output = NftOutputBuilder::from(nft_output.as_nft()) + .with_minimum_amount(protocol_parameters.storage_score_parameters()) + .with_immutable_features(MetadataFeature::try_from(metadata)) + .finish_output() + .unwrap(); -// let outputs = [updated_nft_output]; + let outputs = [updated_nft_output]; -// let selected = InputSelection::new( -// inputs, -// outputs, -// [Address::try_from_bech32(BECH32_ADDRESS_ED25519_0).unwrap()], -// protocol_parameters, -// ) -// .select(); + let selected = InputSelection::new( + inputs, + outputs, + [Address::try_from_bech32(BECH32_ADDRESS_ED25519_0).unwrap()], + protocol_parameters, + ) + .select(); -// assert!(matches!( -// selected, -// Err(Error::UnfulfillableRequirement(Requirement::Nft( -// nft_id, -// ))) if nft_id == nft_id_1 -// )); -// } + assert!(matches!( + selected, + Err(Error::UnfulfillableRequirement(Requirement::Nft( + nft_id, + ))) if nft_id == nft_id_1 + )); +}