cardano_sdk/chain/
metadata.rs

1use super::common::*;
2use super::scripts::*;
3use super::MetadataHash;
4use cbored::{CborRepr, Encode};
5use cryptoxide::hashing::blake2b_256;
6
7#[derive(Debug, Clone, PartialEq, Eq, CborRepr)]
8#[cborrepr(enumtype = "enumtype")]
9pub enum Metadata {
10    #[cborrepr(cbortype = "map")]
11    V1(MetadataV1),
12    #[cborrepr(cbortype = "array")]
13    V2(MetadataV2),
14    #[cborrepr(cbortype = "tag")]
15    V3(MetadataV3),
16}
17
18impl Metadata {
19    pub fn hash(&self) -> MetadataHash {
20        let mut writer = cbored::Writer::new();
21        self.encode(&mut writer);
22        let data = writer.finalize();
23        MetadataHash(blake2b_256(&data))
24    }
25}
26
27#[derive(Debug, Clone, PartialEq, Eq, CborRepr)]
28#[cborrepr(structure = "flat")]
29pub struct MetadataV1(pub MetadataElementsMap);
30
31#[derive(Debug, Clone, PartialEq, Eq, CborRepr)]
32#[cborrepr(structure = "array_lastopt")]
33pub struct MetadataV2 {
34    pub transaction_metadata: MetadataV1,
35    pub auxiliary_scripts: Option<NativeScripts>,
36}
37
38#[derive(Debug, Clone, PartialEq, Eq, CborRepr)]
39#[cborrepr(structure = "mapint", tag = 259)]
40pub struct MetadataV3 {
41    pub metadata: Option<MetadataV1>,
42    pub native_scripts: Option<NativeScripts>,
43    pub plutus_scripts: Option<PlutusScripts>,
44}
45
46crate::map_structure!(
47    MetadataElementsMap,
48    MetadataElement,
49    MetadataElement,
50    [PartialOrd Ord]
51);
52crate::vec_structure!(MetadataElements, MetadataElement, [PartialOrd Ord]);
53
54#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, CborRepr)]
55#[cborrepr(enumtype = "enumtype")]
56pub enum MetadataElement {
57    #[cborrepr(cbortype = "positive")]
58    Positive(u64),
59    #[cborrepr(cbortype = "negative")]
60    Negative(n64),
61    #[cborrepr(cbortype = "bytes")]
62    Bytes(Bytes),
63    #[cborrepr(cbortype = "text")]
64    Text(String),
65    #[cborrepr(cbortype = "array")]
66    Array(MetadataElements),
67    #[cborrepr(cbortype = "map")]
68    Map(MetadataElementsMap),
69}