cml_cip25_wasm/
utils.rs

1use cml_chain_wasm::{
2    assets::AssetName,
3    auxdata::{Metadata, TransactionMetadatum},
4    PolicyId,
5};
6
7use crate::*;
8
9use wasm_bindgen::prelude::JsError;
10
11use cml_core_wasm::impl_wasm_json_api;
12
13#[wasm_bindgen]
14impl CIP25Metadata {
15    /// Create a Metadata containing only the CIP25 schema
16    pub fn to_metadata(&self) -> Result<Metadata, JsError> {
17        self.0.to_metadata().map(Metadata::from).map_err(Into::into)
18    }
19
20    /// Read the CIP25 schema from a Metadata. Ignores all other data besides CIP25
21    /// Can fail if the Metadata does not conform to CIP25
22    pub fn from_metadata(metadata: &Metadata) -> Result<CIP25Metadata, JsError> {
23        cml_cip25::CIP25Metadata::from_metadata(metadata.as_ref())
24            .map(Self)
25            .map_err(Into::into)
26    }
27
28    /// Add to an existing metadata (could be empty) the full CIP25 metadata
29    pub fn add_to_metadata(&self, metadata: &mut Metadata) -> Result<(), JsError> {
30        self.0
31            .add_to_metadata(metadata.as_mut())
32            .map_err(Into::into)
33    }
34}
35
36#[wasm_bindgen]
37impl CIP25String64 {
38    pub fn new(s: &str) -> Result<CIP25String64, JsError> {
39        cml_cip25::CIP25String64::new_str(s)
40            .map(Self)
41            .map_err(Into::into)
42    }
43
44    pub fn to_str(&self) -> String {
45        self.0.to_str().to_owned()
46    }
47
48    pub fn get_str(&self) -> String {
49        self.0.get().clone()
50    }
51}
52
53#[wasm_bindgen]
54
55impl CIP25ChunkableString {
56    pub fn from_string(str: &str) -> Self {
57        Self(cml_cip25::CIP25ChunkableString::from(str))
58    }
59
60    #[allow(clippy::inherent_to_string)]
61    pub fn to_string(&self) -> String {
62        String::from(&self.0)
63    }
64}
65
66#[derive(Clone, Debug)]
67#[wasm_bindgen]
68pub struct CIP25MiniMetadataDetails(cml_cip25::utils::CIP25MiniMetadataDetails);
69
70impl_wasm_conversions!(
71    cml_cip25::utils::CIP25MiniMetadataDetails,
72    CIP25MiniMetadataDetails
73);
74
75impl_wasm_json_api!(CIP25MiniMetadataDetails);
76
77#[wasm_bindgen]
78impl CIP25MiniMetadataDetails {
79    pub fn new() -> Self {
80        CIP25MiniMetadataDetails(cml_cip25::utils::CIP25MiniMetadataDetails {
81            name: None,
82            image: None,
83        })
84    }
85
86    pub fn set_name(&mut self, name: &CIP25String64) {
87        self.0.name = Some(name.clone().into())
88    }
89
90    pub fn name(&self) -> Option<CIP25String64> {
91        self.0.name.clone().map(CIP25String64)
92    }
93
94    pub fn set_image(&mut self, image: &CIP25ChunkableString) {
95        self.0.image = Some(image.clone().into())
96    }
97
98    pub fn image(&self) -> Option<CIP25ChunkableString> {
99        self.0.image.clone().map(CIP25ChunkableString)
100    }
101
102    /// loose parsing of CIP25 metadata to allow for common exceptions to the format
103    /// `metadatum` should represent the data where the `CIP25MetadataDetails` is in the cip25 structure
104    pub fn loose_parse(
105        metadatum: &TransactionMetadatum,
106    ) -> Result<CIP25MiniMetadataDetails, JsValue> {
107        let parsed_data =
108            cml_cip25::utils::CIP25MiniMetadataDetails::loose_parse(&metadatum.clone().into())
109                .map_err(|e| JsValue::from_str(&format!("loose_parse: {e}")))?;
110        Ok(CIP25MiniMetadataDetails(parsed_data))
111    }
112}
113
114#[derive(Clone, Debug)]
115#[wasm_bindgen]
116pub struct CIP25LabelMetadata(cml_cip25::CIP25LabelMetadata);
117
118impl_wasm_conversions!(cml_cip25::CIP25LabelMetadata, CIP25LabelMetadata);
119
120impl_wasm_cbor_json_api_cbor_event_serialize!(CIP25LabelMetadata);
121
122#[wasm_bindgen]
123impl CIP25LabelMetadata {
124    /// Note that Version 1 can only support utf8 string asset names.
125    /// Version 2 can support any asset name.
126    pub fn new(version: CIP25Version) -> Self {
127        Self(cml_cip25::CIP25LabelMetadata::new(version))
128    }
129
130    /// If this is version 1 and the asset name is not a utf8 asset name
131    /// then this will return an error.
132    /// This function will never return an error for version 2.
133    /// On success, returns the previous details that were overwritten, or None otherwise.
134    pub fn set(
135        &mut self,
136        policy_id: &PolicyId,
137        asset_name: &AssetName,
138        details: &CIP25MetadataDetails,
139    ) -> Result<Option<CIP25MetadataDetails>, JsError> {
140        self.0
141            .set(
142                policy_id.clone().into(),
143                asset_name.clone().into(),
144                details.clone().into(),
145            )
146            .map(|old| old.map(Into::into))
147            .map_err(Into::into)
148    }
149
150    pub fn get(
151        &self,
152        policy_id: &PolicyId,
153        asset_name: &AssetName,
154    ) -> Option<CIP25MetadataDetails> {
155        self.0
156            .get(policy_id.as_ref(), asset_name.as_ref())
157            .map(|details| details.clone().into())
158    }
159
160    pub fn version(&self) -> CIP25Version {
161        self.0.version()
162    }
163}