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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// This file was code-generated using an experimental CDDL to rust tool:
// https://github.com/dcSpark/cddl-codegen

pub mod cbor_encodings;
pub mod serialization;
pub mod utils;

use self::cbor_encodings::PlutusV3ScriptEncoding;

use super::{Rational, SubCoin};
use crate::utils::BigInteger;
use cbor_encodings::{
    CostModelsEncoding, ExUnitPricesEncoding, ExUnitsEncoding, PlutusV1ScriptEncoding,
    PlutusV2ScriptEncoding, RedeemerEncoding,
};

use cml_core::serialization::{LenEncoding, Serialize, StringEncoding};
use cml_core::Int;
use cml_crypto::{blake2b256, DatumHash};

pub use utils::{ConstrPlutusData, PlutusMap, PlutusScript};

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub struct CostModels {
    pub plutus_v1: Option<Vec<Int>>,
    pub plutus_v2: Option<Vec<Int>>,
    pub plutus_v3: Option<Vec<Int>>,
    #[serde(skip)]
    pub encodings: Option<CostModelsEncoding>,
}

impl CostModels {
    pub fn new() -> Self {
        Self {
            plutus_v1: None,
            plutus_v2: None,
            plutus_v3: None,
            encodings: None,
        }
    }
}

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

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub struct ExUnitPrices {
    pub mem_price: SubCoin,
    pub step_price: SubCoin,
    #[serde(skip)]
    pub encodings: Option<ExUnitPricesEncoding>,
}

impl ExUnitPrices {
    pub fn new(mem_price: SubCoin, step_price: SubCoin) -> Self {
        Self {
            mem_price,
            step_price,
            encodings: None,
        }
    }
}

#[derive(
    Clone, Debug, derivative::Derivative, serde::Deserialize, serde::Serialize, schemars::JsonSchema,
)]
#[derivative(PartialEq, Hash, Eq)]
pub struct ExUnits {
    pub mem: u64,
    pub steps: u64,
    #[serde(skip)]
    #[derivative(PartialEq = "ignore", Hash = "ignore")]
    pub encodings: Option<ExUnitsEncoding>,
}

impl ExUnits {
    pub fn new(mem: u64, steps: u64) -> Self {
        Self {
            mem,
            steps,
            encodings: None,
        }
    }
}

#[derive(
    Copy,
    Eq,
    PartialEq,
    Ord,
    PartialOrd,
    Clone,
    Debug,
    serde::Deserialize,
    serde::Serialize,
    schemars::JsonSchema,
)]
#[wasm_bindgen::prelude::wasm_bindgen]
pub enum Language {
    PlutusV1,
    PlutusV2,
    PlutusV3,
}

#[derive(
    Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema, derivative::Derivative,
)]
#[derivative(
    Eq,
    PartialEq,
    Ord = "feature_allow_slow_enum",
    PartialOrd = "feature_allow_slow_enum",
    Hash
)]
pub enum PlutusData {
    ConstrPlutusData(ConstrPlutusData),
    Map(PlutusMap),
    List {
        list: Vec<PlutusData>,
        #[derivative(
            PartialEq = "ignore",
            Ord = "ignore",
            PartialOrd = "ignore",
            Hash = "ignore"
        )]
        #[serde(skip)]
        list_encoding: LenEncoding,
    },
    Integer(BigInteger),
    Bytes {
        bytes: Vec<u8>,
        #[derivative(
            PartialEq = "ignore",
            Ord = "ignore",
            PartialOrd = "ignore",
            Hash = "ignore"
        )]
        #[serde(skip)]
        bytes_encoding: StringEncoding,
    },
}

impl PlutusData {
    pub fn new_constr_plutus_data(constr_plutus_data: ConstrPlutusData) -> Self {
        Self::ConstrPlutusData(constr_plutus_data)
    }

    pub fn new_map(map: PlutusMap) -> Self {
        Self::Map(map)
    }

    pub fn new_list(list: Vec<PlutusData>) -> Self {
        Self::List {
            list,
            list_encoding: LenEncoding::default(),
        }
    }

    pub fn new_integer(integer: BigInteger) -> Self {
        Self::Integer(integer)
    }

    pub fn new_bytes(bytes: Vec<u8>) -> Self {
        Self::Bytes {
            bytes,
            bytes_encoding: StringEncoding::default(),
        }
    }

    pub fn hash(&self) -> DatumHash {
        DatumHash::from(blake2b256(&self.to_cbor_bytes()))
    }
}

#[derive(Clone, Debug, derivative::Derivative)]
#[derivative(Hash, PartialEq, Eq)]
pub struct PlutusV1Script {
    pub inner: Vec<u8>,
    #[derivative(PartialEq = "ignore", Hash = "ignore")]
    pub encodings: Option<PlutusV1ScriptEncoding>,
}

impl PlutusV1Script {
    pub fn get(&self) -> &Vec<u8> {
        &self.inner
    }

    pub fn new(inner: Vec<u8>) -> Self {
        Self {
            inner,
            encodings: None,
        }
    }
}

impl From<Vec<u8>> for PlutusV1Script {
    fn from(inner: Vec<u8>) -> Self {
        PlutusV1Script::new(inner)
    }
}

impl From<PlutusV1Script> for Vec<u8> {
    fn from(wrapper: PlutusV1Script) -> Self {
        wrapper.inner
    }
}

impl serde::Serialize for PlutusV1Script {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&hex::encode(self.inner.clone()))
    }
}

impl<'de> serde::de::Deserialize<'de> for PlutusV1Script {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        let s = <String as serde::de::Deserialize>::deserialize(deserializer)?;
        hex::decode(&s).map(PlutusV1Script::new).map_err(|_e| {
            serde::de::Error::invalid_value(serde::de::Unexpected::Str(&s), &"invalid hex bytes")
        })
    }
}

impl schemars::JsonSchema for PlutusV1Script {
    fn schema_name() -> String {
        String::from("PlutusV1Script")
    }

    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        String::json_schema(gen)
    }

    fn is_referenceable() -> bool {
        String::is_referenceable()
    }
}

#[derive(Clone, Debug, derivative::Derivative)]
#[derivative(Hash, PartialEq, Eq)]
pub struct PlutusV2Script {
    pub inner: Vec<u8>,
    #[derivative(PartialEq = "ignore", Hash = "ignore")]
    pub encodings: Option<PlutusV2ScriptEncoding>,
}

impl PlutusV2Script {
    pub fn get(&self) -> &Vec<u8> {
        &self.inner
    }

    pub fn new(inner: Vec<u8>) -> Self {
        Self {
            inner,
            encodings: None,
        }
    }
}

impl From<Vec<u8>> for PlutusV2Script {
    fn from(inner: Vec<u8>) -> Self {
        PlutusV2Script::new(inner)
    }
}

impl From<PlutusV2Script> for Vec<u8> {
    fn from(wrapper: PlutusV2Script) -> Self {
        wrapper.inner
    }
}

impl serde::Serialize for PlutusV2Script {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&hex::encode(self.inner.clone()))
    }
}

impl<'de> serde::de::Deserialize<'de> for PlutusV2Script {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        let s = <String as serde::de::Deserialize>::deserialize(deserializer)?;
        hex::decode(&s).map(PlutusV2Script::new).map_err(|_e| {
            serde::de::Error::invalid_value(serde::de::Unexpected::Str(&s), &"invalid hex bytes")
        })
    }
}

impl schemars::JsonSchema for PlutusV2Script {
    fn schema_name() -> String {
        String::from("PlutusV2Script")
    }

    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        String::json_schema(gen)
    }

    fn is_referenceable() -> bool {
        String::is_referenceable()
    }
}

#[derive(Clone, Debug, derivative::Derivative)]
#[derivative(Hash, PartialEq, Eq)]
pub struct PlutusV3Script {
    pub inner: Vec<u8>,
    #[derivative(PartialEq = "ignore", Hash = "ignore")]
    pub encodings: Option<PlutusV3ScriptEncoding>,
}

impl PlutusV3Script {
    pub fn get(&self) -> &Vec<u8> {
        &self.inner
    }

    pub fn new(inner: Vec<u8>) -> Self {
        Self {
            inner,
            encodings: None,
        }
    }
}

impl From<Vec<u8>> for PlutusV3Script {
    fn from(inner: Vec<u8>) -> Self {
        PlutusV3Script::new(inner)
    }
}

impl From<PlutusV3Script> for Vec<u8> {
    fn from(wrapper: PlutusV3Script) -> Self {
        wrapper.inner
    }
}

impl serde::Serialize for PlutusV3Script {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&hex::encode(self.inner.clone()))
    }
}

impl<'de> serde::de::Deserialize<'de> for PlutusV3Script {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        let s = <String as serde::de::Deserialize>::deserialize(deserializer)?;
        hex::decode(&s).map(PlutusV3Script::new).map_err(|_e| {
            serde::de::Error::invalid_value(serde::de::Unexpected::Str(&s), &"invalid hex bytes")
        })
    }
}

impl schemars::JsonSchema for PlutusV3Script {
    fn schema_name() -> String {
        String::from("PlutusV3Script")
    }

    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        String::json_schema(gen)
    }

    fn is_referenceable() -> bool {
        String::is_referenceable()
    }
}

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub struct Redeemer {
    pub tag: RedeemerTag,
    pub index: u64,
    pub data: PlutusData,
    pub ex_units: ExUnits,
    #[serde(skip)]
    pub encodings: Option<RedeemerEncoding>,
}

impl Redeemer {
    pub fn new(tag: RedeemerTag, index: u64, data: PlutusData, ex_units: ExUnits) -> Self {
        Self {
            tag,
            index,
            data,
            ex_units,
            encodings: None,
        }
    }
}

#[derive(
    Copy,
    Eq,
    Hash,
    PartialEq,
    Ord,
    PartialOrd,
    Clone,
    Debug,
    serde::Deserialize,
    serde::Serialize,
    schemars::JsonSchema,
)]
#[wasm_bindgen::prelude::wasm_bindgen]
pub enum RedeemerTag {
    Spend,
    Mint,
    Cert,
    Reward,
}