1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use crate::auxdata::metadata::Metadata;

use crate::{
    plutus::{PlutusV1Script, PlutusV2Script},
    transaction::NativeScript,
};

use super::{AuxiliaryData, ConwayFormatAuxData, ShelleyMaFormatAuxData};

impl AuxiliaryData {
    pub fn new() -> Self {
        Self::new_shelley(Metadata::new())
    }

    pub fn metadata(&self) -> Option<&Metadata> {
        match self {
            Self::Shelley(shelley) => Some(shelley),
            Self::ShelleyMA(shelley_ma) => Some(&shelley_ma.transaction_metadata),
            Self::Conway(conway) => conway.metadata.as_ref(),
        }
    }

    /// Mut ref to the general tx metadata.
    /// Will be created if it didn't exist (i.e. Conway format)
    pub fn metadata_mut(&mut self) -> &mut Metadata {
        match self {
            Self::Shelley(shelley) => shelley,
            Self::ShelleyMA(shelley_ma) => &mut shelley_ma.transaction_metadata,
            Self::Conway(conway) => {
                if conway.metadata.is_none() {
                    conway.metadata = Some(Metadata::new());
                }
                conway.metadata.as_mut().unwrap()
            }
        }
    }

    pub fn native_scripts(&self) -> Option<&Vec<NativeScript>> {
        match self {
            Self::Shelley { .. } => None,
            Self::ShelleyMA(shelley_ma) => Some(&shelley_ma.auxiliary_scripts),
            Self::Conway(conway) => conway.native_scripts.as_ref(),
        }
        .filter(|scripts| !scripts.is_empty())
    }

    pub fn plutus_v1_scripts(&self) -> Option<&Vec<PlutusV1Script>> {
        match self {
            Self::Shelley { .. } => None,
            Self::ShelleyMA(_shelley_ma) => None,
            Self::Conway(conway) => conway.plutus_v1_scripts.as_ref(),
        }
        .filter(|scripts| !scripts.is_empty())
    }

    pub fn plutus_v2_scripts(&self) -> Option<&Vec<PlutusV2Script>> {
        match self {
            Self::Shelley { .. } => None,
            Self::ShelleyMA(_shelley_ma) => None,
            Self::Conway(conway) => conway.plutus_v2_scripts.as_ref(),
        }
        .filter(|scripts| !scripts.is_empty())
    }

    /// Warning: overwrites any conflicting metadatum labels present
    pub fn add_metadata(&mut self, other: Metadata) {
        let metadata = match self {
            Self::Shelley(shelley) => shelley,
            Self::ShelleyMA(shelley_ma) => &mut shelley_ma.transaction_metadata,
            Self::Conway(conway) => {
                if conway.metadata.is_none() {
                    conway.metadata = Some(Metadata::new());
                }
                conway.metadata.as_mut().unwrap()
            }
        };
        metadata.entries.extend(other.entries);
    }

    /// Warning: does not check for duplicates and may migrate eras
    pub fn add_native_scripts(&mut self, scripts: Vec<NativeScript>) {
        match self {
            Self::Shelley(shelley) => {
                *self = Self::ShelleyMA(ShelleyMaFormatAuxData::new(shelley.clone(), scripts));
            }
            Self::ShelleyMA(shelley_ma) => {
                shelley_ma.auxiliary_scripts.extend(scripts);
            }
            Self::Conway(conway) => {
                if let Some(old_scripts) = &mut conway.native_scripts {
                    old_scripts.extend(scripts);
                } else {
                    conway.native_scripts = Some(scripts);
                }
            }
        }
    }

    /// Warning: does not check for duplicates and may migrate eras
    pub fn add_plutus_v1_scripts(&mut self, scripts: Vec<PlutusV1Script>) {
        match self {
            Self::Shelley(shelley) => {
                let mut conway = ConwayFormatAuxData::new();
                if !shelley.entries.is_empty() {
                    conway.metadata = Some(shelley.clone());
                }
                conway.plutus_v1_scripts = Some(scripts);
                *self = Self::Conway(conway);
            }
            Self::ShelleyMA(shelley_ma) => {
                let mut conway = ConwayFormatAuxData::new();
                if !shelley_ma.transaction_metadata.entries.is_empty() {
                    conway.metadata = Some(shelley_ma.transaction_metadata.clone());
                }
                if !shelley_ma.auxiliary_scripts.is_empty() {
                    conway.native_scripts = Some(shelley_ma.auxiliary_scripts.clone());
                }
                conway.plutus_v1_scripts = Some(scripts);
                *self = Self::Conway(conway);
            }
            Self::Conway(conway) => {
                if let Some(old_scripts) = &mut conway.plutus_v1_scripts {
                    old_scripts.extend(scripts);
                } else {
                    conway.plutus_v1_scripts = Some(scripts);
                }
            }
        }
    }

    /// Warning: does not check for duplicates and may migrate eras
    pub fn add_plutus_v2_scripts(&mut self, scripts: Vec<PlutusV2Script>) {
        match self {
            Self::Shelley(shelley) => {
                let mut conway = ConwayFormatAuxData::new();
                if !shelley.entries.is_empty() {
                    conway.metadata = Some(shelley.clone());
                }
                conway.plutus_v2_scripts = Some(scripts);
                *self = Self::Conway(conway);
            }
            Self::ShelleyMA(shelley_ma) => {
                let mut conway = ConwayFormatAuxData::new();
                if !shelley_ma.transaction_metadata.entries.is_empty() {
                    conway.metadata = Some(shelley_ma.transaction_metadata.clone());
                }
                if !shelley_ma.auxiliary_scripts.is_empty() {
                    conway.native_scripts = Some(shelley_ma.auxiliary_scripts.clone());
                }
                conway.plutus_v2_scripts = Some(scripts);
                *self = Self::Conway(conway);
            }
            Self::Conway(conway) => {
                if let Some(old_scripts) = &mut conway.plutus_v2_scripts {
                    old_scripts.extend(scripts);
                } else {
                    conway.plutus_v2_scripts = Some(scripts);
                }
            }
        }
    }

    /// Adds everything present in other to self
    /// May change the era the aux data is in if necessary
    /// Warning: overwrites any metadatum labels present
    /// also does not check for duplicates in scripts
    pub fn add(&mut self, other: AuxiliaryData) {
        // to avoid redundant migrating of formats, we set the content with
        // plutus scripts first, then native scripts, then metadata in
        // reverse chronological (era format wise) order.
        match other {
            Self::Shelley(shelley) => {
                self.add_metadata(shelley);
            }
            Self::ShelleyMA(shelley_ma) => {
                self.add_native_scripts(shelley_ma.auxiliary_scripts);
                self.add_metadata(shelley_ma.transaction_metadata);
            }
            Self::Conway(conway) => {
                if let Some(scripts) = conway.plutus_v2_scripts {
                    self.add_plutus_v2_scripts(scripts);
                }
                if let Some(scripts) = conway.plutus_v1_scripts {
                    self.add_plutus_v1_scripts(scripts);
                }
                if let Some(scripts) = conway.native_scripts {
                    self.add_native_scripts(scripts);
                }
                if let Some(metadata) = conway.metadata {
                    self.add_metadata(metadata);
                }
            }
        }
    }
}

impl Default for AuxiliaryData {
    fn default() -> Self {
        Self::new()
    }
}