Skip to content

Commit

Permalink
Add TryFrom<Vec<(Vec<u8>, Vec<u8>)>> for MetadataFeature, uncomment test
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoralf-M committed Jan 11, 2024
1 parent 2ae1695 commit 03b52bb
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 62 deletions.
25 changes: 25 additions & 0 deletions sdk/src/types/block/output/feature/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,31 @@ impl StorageScore for MetadataFeature {}

impl WorkScore for MetadataFeature {}

impl TryFrom<Vec<(Vec<u8>, Vec<u8>)>> for MetadataFeature {
type Error = Error;

fn try_from(data: Vec<(Vec<u8>, Vec<u8>)>) -> Result<Self, Error> {
let mut res = BTreeMap::<
BoxedSlicePrefix<u8, MetadataFeatureKeyLength>,
BoxedSlicePrefix<u8, MetadataFeatureValueLength>,
>::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<BTreeMap<Vec<u8>, Vec<u8>>> for MetadataFeature {
type Error = Error;

Expand Down
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
119 changes: 59 additions & 60 deletions sdk/tests/client/input_selection/nft_outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
));
}

0 comments on commit 03b52bb

Please sign in to comment.