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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
use cbor_event::{de::Deserializer, se::Serializer};
use cml_core::{
    error::{DeserializeError, DeserializeFailure},
    serialization::{fit_sz, sz_max, Deserialize, Serialize},
    Int,
};
use cml_crypto::ScriptHash;
use derivative::Derivative;
use std::io::{BufRead, Seek, Write};

use crate::{
    crypto::hash::{hash_script, ScriptHashNamespace},
    plutus::{Language, PlutusScript, PlutusV1Script, PlutusV2Script},
    NativeScript, Script,
};

impl Script {
    pub fn hash(&self) -> ScriptHash {
        match self {
            Self::Native { script, .. } => script.hash(),
            Self::PlutusV1 { script, .. } => script.hash(),
            Self::PlutusV2 { script, .. } => script.hash(),
            Self::PlutusV3 { script, .. } => script.hash(),
        }
    }

    // Returns which language the script is if it's a Plutus script
    // Returns None otherwise (i.e. NativeScript)
    pub fn language(&self) -> Option<Language> {
        match self {
            Self::Native { .. } => None,
            Self::PlutusV1 { .. } => Some(Language::PlutusV1),
            Self::PlutusV2 { .. } => Some(Language::PlutusV2),
            Self::PlutusV3 { .. } => Some(Language::PlutusV3),
        }
    }
}

impl NativeScript {
    pub fn hash(&self) -> ScriptHash {
        hash_script(ScriptHashNamespace::NativeScript, &self.to_cbor_bytes())
    }
}

impl From<NativeScript> for Script {
    fn from(script: NativeScript) -> Self {
        Self::new_native(script)
    }
}

impl From<PlutusV1Script> for Script {
    fn from(script: PlutusV1Script) -> Self {
        Self::new_plutus_v1(script)
    }
}

impl From<PlutusV2Script> for Script {
    fn from(script: PlutusV2Script) -> Self {
        Self::new_plutus_v2(script)
    }
}

impl From<PlutusScript> for Script {
    fn from(script: PlutusScript) -> Self {
        match script {
            PlutusScript::PlutusV1(v1) => Self::new_plutus_v1(v1),
            PlutusScript::PlutusV2(v2) => Self::new_plutus_v2(v2),
        }
    }
}

const BOUNDED_BYTES_CHUNK_SIZE: usize = 64;

// to get around not having access from outside the library we just write the raw CBOR indefinite byte string code here
fn write_cbor_indefinite_byte_tag<W: Write>(
    serializer: &mut Serializer<W>,
) -> cbor_event::Result<&mut Serializer<W>> {
    serializer.write_raw_bytes(&[0x5f])
}

use cml_core::serialization::StringEncoding;

fn valid_indefinite_string_encoding(chunks: &[(u64, cbor_event::Sz)], total_len: usize) -> bool {
    let mut len_counter = 0;
    let valid_sz = chunks.iter().all(|(len, sz)| {
        len_counter += len;
        *len <= sz_max(*sz)
    });
    valid_sz && len_counter == total_len as u64
}

/// Write bounded bytes according to Cardano's special format:
/// bounded_bytes = bytes .size (0..64)
///  ; the real bounded_bytes does not have this limit. it instead has a different
///   ; limit which cannot be expressed in CDDL.
///   ; The limit is as follows:
///   ;  - bytes with a definite-length encoding are limited to size 0..64
///   ;  - for bytes with an indefinite-length CBOR encoding, each chunk is
///   ;    limited to size 0..64
///   ;  ( reminder: in CBOR, the indefinite-length encoding of bytestrings
///   ;    consists of a token #2.31 followed by a sequence of definite-length
///   ;    encoded bytestrings and a stop code )
pub fn write_bounded_bytes<'se, W: Write>(
    serializer: &'se mut Serializer<W>,
    bytes: &[u8],
    enc: &StringEncoding,
    force_canonical: bool,
) -> cbor_event::Result<&'se mut Serializer<W>> {
    match enc {
        StringEncoding::Definite(sz) if !force_canonical => {
            if bytes.len() <= BOUNDED_BYTES_CHUNK_SIZE {
                let fit_sz = fit_sz(bytes.len() as u64, Some(*sz), force_canonical);
                return serializer.write_bytes_sz(bytes, cbor_event::StringLenSz::Len(fit_sz));
            }
        }
        StringEncoding::Indefinite(chunks) if !force_canonical => {
            if valid_indefinite_string_encoding(chunks, bytes.len()) {
                write_cbor_indefinite_byte_tag(serializer)?;
                let mut start = 0;
                for (len, sz) in chunks {
                    let end = start + *len as usize;
                    serializer
                        .write_bytes_sz(&bytes[start..end], cbor_event::StringLenSz::Len(*sz))?;
                    start = end;
                }
                return serializer.write_special(cbor_event::Special::Break);
            }
        }
        _ =>
            /* handled below */
            {}
    };
    // This is a fallback for when either it's canonical or the passed in encoding isn't
    // compatible with the passed in bytes (e.g. someone deserialized then modified the bytes)
    // If we truly need to encode canonical CBOR there's really no way to abide by both canonical
    // CBOR as well as following the Cardano format. So this is the best attempt at it while keeping
    // chunks when len > 64
    if bytes.len() <= BOUNDED_BYTES_CHUNK_SIZE {
        serializer.write_bytes(bytes)
    } else {
        write_cbor_indefinite_byte_tag(serializer)?;
        for chunk in bytes.chunks(BOUNDED_BYTES_CHUNK_SIZE) {
            serializer.write_bytes(chunk)?;
        }
        serializer.write_special(cbor_event::Special::Break)
    }
}

/// Read bounded bytes according to Cardano's special format:
/// bounded_bytes = bytes .size (0..64)
///  ; the real bounded_bytes does not have this limit. it instead has a different
///  ; limit which cannot be expressed in CDDL.
///  ; The limit is as follows:
///  ;  - bytes with a definite-length encoding are limited to size 0..64
///  ;  - for bytes with an indefinite-length CBOR encoding, each chunk is
///  ;    limited to size 0..64
///  ;  ( reminder: in CBOR, the indefinite-length encoding of bytestrings
///  ;    consists of a token #2.31 followed by a sequence of definite-length
///  ;    encoded bytestrings and a stop code )
pub fn read_bounded_bytes<R: BufRead + Seek>(
    raw: &mut Deserializer<R>,
) -> Result<(Vec<u8>, StringEncoding), DeserializeError> {
    let (bytes, bytes_enc) = raw.bytes_sz()?;
    match &bytes_enc {
        cbor_event::StringLenSz::Len(_sz) => {
            if bytes.len() > BOUNDED_BYTES_CHUNK_SIZE {
                return Err(DeserializeFailure::OutOfRange {
                    min: 0,
                    max: BOUNDED_BYTES_CHUNK_SIZE,
                    found: bytes.len(),
                }
                .into());
            }
        }
        cbor_event::StringLenSz::Indefinite(chunks) => {
            for (chunk_len, _chunk_len_sz) in chunks.iter() {
                if *chunk_len as usize > BOUNDED_BYTES_CHUNK_SIZE {
                    return Err(DeserializeFailure::OutOfRange {
                        min: 0,
                        max: BOUNDED_BYTES_CHUNK_SIZE,
                        found: *chunk_len as usize,
                    }
                    .into());
                }
            }
        }
    }
    Ok((bytes, bytes_enc.into()))
}

#[derive(Clone, Debug)]
enum BigIntEncoding {
    Int(cbor_event::Sz),
    Bytes(StringEncoding),
}

#[derive(Clone, Debug, Derivative)]
#[derivative(Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct BigInt {
    num: num_bigint::BigInt,
    #[derivative(
        PartialEq = "ignore",
        Ord = "ignore",
        PartialOrd = "ignore",
        Hash = "ignore"
    )]
    encoding: Option<BigIntEncoding>,
}

impl serde::Serialize for BigInt {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}

impl<'de> serde::de::Deserialize<'de> for BigInt {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        use std::str::FromStr;
        let s = <String as serde::de::Deserialize>::deserialize(deserializer)?;
        BigInt::from_str(&s).map_err(|_e| {
            serde::de::Error::invalid_value(
                serde::de::Unexpected::Str(&s),
                &"string rep of a big int",
            )
        })
    }
}

impl schemars::JsonSchema for BigInt {
    fn schema_name() -> String {
        String::from("BigInt")
    }
    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        String::json_schema(gen)
    }
    fn is_referenceable() -> bool {
        String::is_referenceable()
    }
}

impl std::fmt::Display for BigInt {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.num.fmt(f)
    }
}

impl std::str::FromStr for BigInt {
    type Err = num_bigint::ParseBigIntError;
    fn from_str(string: &str) -> Result<Self, Self::Err> {
        num_bigint::BigInt::from_str(string).map(|num| Self {
            num,
            encoding: None,
        })
    }
}

impl BigInt {
    // can't be a trait due to being in other crate
    pub fn from_int(x: &Int) -> Self {
        Self {
            num: Into::<i128>::into(x).into(),
            encoding: x.encoding().map(BigIntEncoding::Int),
        }
    }

    /// Converts to a u64
    /// Returns None if the number was negative or too big for a u64
    pub fn as_u64(&self) -> Option<u64> {
        let (sign, u64_digits) = self.num.to_u64_digits();
        if sign == num_bigint::Sign::Minus {
            return None;
        }
        match u64_digits.len() {
            0 => Some(0),
            1 => Some(*u64_digits.first().unwrap()),
            _ => None,
        }
    }

    /// Converts to an Int
    /// Returns None when the number is too big for an Int (outside +/- 64-bit unsigned)
    /// Retains encoding info if the original was encoded as an Int
    pub fn as_int(&self) -> Option<Int> {
        let (sign, u64_digits) = self.num.to_u64_digits();
        // unsigned raw value that can fit in the up to 8 bytes of a CBOR uint or nint
        // negative values evaluate to -u64_value - 1
        let u64_value = match u64_digits.len() {
            0 => 0u64,
            1 => {
                if sign == num_bigint::Sign::Minus {
                    (*u64_digits.first().unwrap())
                        .checked_sub(1)
                        .expect("negative (non-zero) so can't underflow")
                } else {
                    *u64_digits.first().unwrap()
                }
            }
            // this could actually be -u64::MAX which in CBOR can be a single u64 as the sign
            // is encoded separately so values here start from -1 instead of 0.
            2 if sign == num_bigint::Sign::Minus && u64_digits[0] == 0 && u64_digits[1] == 1 => {
                u64::MAX
            }
            _ => return None,
        };
        let encoding = match &self.encoding {
            Some(BigIntEncoding::Int(sz)) => Some(*sz),
            _ => None,
        };
        match sign {
            num_bigint::Sign::NoSign | num_bigint::Sign::Plus => Some(Int::Uint {
                value: u64_value,
                encoding,
            }),
            num_bigint::Sign::Minus => Some(Int::Nint {
                value: u64_value,
                encoding,
            }),
        }
    }
}

impl Serialize for BigInt {
    fn serialize<'se, W: Write>(
        &self,
        serializer: &'se mut Serializer<W>,
        force_canonical: bool,
    ) -> cbor_event::Result<&'se mut Serializer<W>> {
        let write_self_as_bytes = |serializer: &'se mut Serializer<W>,
                                   enc: &StringEncoding|
         -> cbor_event::Result<&'se mut Serializer<W>> {
            let (sign, bytes) = self.num.to_bytes_be();
            match sign {
                // positive bigint
                num_bigint::Sign::Plus | num_bigint::Sign::NoSign => {
                    serializer.write_tag(2u64)?;
                    write_bounded_bytes(serializer, &bytes, enc, force_canonical)
                }
                // negative bigint
                num_bigint::Sign::Minus => {
                    serializer.write_tag(3u64)?;
                    use std::ops::Neg;
                    // CBOR RFC defines this as the bytes of -n -1
                    let adjusted = self
                        .num
                        .clone()
                        .neg()
                        .checked_sub(&num_bigint::BigInt::from(1u32))
                        .unwrap()
                        .to_biguint()
                        .unwrap();
                    write_bounded_bytes(serializer, &adjusted.to_bytes_be(), enc, force_canonical)
                }
            }
        };
        // use encoding if possible
        match &self.encoding {
            Some(BigIntEncoding::Int(_sz)) if !force_canonical => {
                // as_int() retains encoding info so we can direclty use Int::serialize()
                if let Some(int) = self.as_int() {
                    return int.serialize(serializer, force_canonical);
                }
            }
            Some(BigIntEncoding::Bytes(str_enc)) if !force_canonical => {
                let (_sign, bytes) = self.num.to_bytes_be();
                let valid_non_canonical = match str_enc {
                    StringEncoding::Canonical => false,
                    StringEncoding::Definite(sz) => bytes.len() <= sz_max(*sz) as usize,
                    StringEncoding::Indefinite(chunks) => {
                        valid_indefinite_string_encoding(chunks, bytes.len())
                    }
                };
                if valid_non_canonical {
                    return write_self_as_bytes(serializer, str_enc);
                }
            }
            _ =>
                /* always fallback to default */
                {}
        }
        // fallback for:
        // 1) canonical bytes needed
        // 2) no encoding specified (never deseiralized)
        // 3) deserialized but data changed and no longer compatible
        let (sign, u64_digits) = self.num.to_u64_digits();
        match u64_digits.len() {
            0 => serializer.write_unsigned_integer(0),
            // we use the uint/nint encodings to use a minimum of space
            1 => match sign {
                // uint
                num_bigint::Sign::Plus | num_bigint::Sign::NoSign => {
                    serializer.write_unsigned_integer(*u64_digits.first().unwrap())
                }
                // nint
                num_bigint::Sign::Minus => serializer
                    .write_negative_integer(-(*u64_digits.first().unwrap() as i128) as i64),
            },
            _ => {
                // Small edge case: nint's minimum is -18446744073709551616 but in this bigint lib
                // that takes 2 u64 bytes so we put that as a special case here:
                if sign == num_bigint::Sign::Minus && u64_digits == vec![0, 1] {
                    serializer.write_negative_integer(-18446744073709551616i128 as i64)
                } else {
                    write_self_as_bytes(serializer, &StringEncoding::Canonical)
                }
            }
        }
    }
}

impl Deserialize for BigInt {
    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
        (|| -> Result<_, DeserializeError> {
            match raw.cbor_type()? {
                // bigint
                cbor_event::Type::Tag => {
                    let tag = raw.tag()?;
                    let (bytes, bytes_enc) = read_bounded_bytes(raw)?;
                    match tag {
                        // positive bigint
                        2 => Ok(Self {
                            num: num_bigint::BigInt::from_bytes_be(num_bigint::Sign::Plus, &bytes),
                            encoding: Some(BigIntEncoding::Bytes(bytes_enc)),
                        }),
                        // negative bigint
                        3 => {
                            // CBOR RFC defines this as the bytes of -n -1
                            let initial =
                                num_bigint::BigInt::from_bytes_be(num_bigint::Sign::Plus, &bytes);
                            use std::ops::Neg;
                            let adjusted = initial
                                .checked_add(&num_bigint::BigInt::from(1u32))
                                .unwrap()
                                .neg();
                            Ok(Self {
                                num: adjusted,
                                encoding: Some(BigIntEncoding::Bytes(bytes_enc)),
                            })
                        }
                        _ => Err(DeserializeFailure::TagMismatch {
                            found: tag,
                            expected: 2,
                        }
                        .into()),
                    }
                }
                // uint
                cbor_event::Type::UnsignedInteger => {
                    let (num, num_enc) = raw.unsigned_integer_sz()?;
                    Ok(Self {
                        num: num_bigint::BigInt::from(num),
                        encoding: Some(BigIntEncoding::Int(num_enc)),
                    })
                }
                // nint
                cbor_event::Type::NegativeInteger => {
                    let (num, num_enc) = raw.negative_integer_sz()?;
                    Ok(Self {
                        num: num_bigint::BigInt::from(num),
                        encoding: Some(BigIntEncoding::Int(num_enc)),
                    })
                }
                _ => Err(DeserializeFailure::NoVariantMatched.into()),
            }
        })()
        .map_err(|e| e.annotate("BigInt"))
    }
}

impl<T> std::convert::From<T> for BigInt
where
    T: std::convert::Into<num_bigint::BigInt>,
{
    fn from(x: T) -> Self {
        Self {
            num: x.into(),
            encoding: None,
        }
    }
}

#[derive(Clone, Copy, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub struct NetworkId {
    pub network: u64,
    #[serde(skip)]
    pub encoding: Option<cbor_event::Sz>,
}

impl NetworkId {
    pub fn new(network: u64) -> Self {
        Self {
            network,
            encoding: None,
        }
    }

    pub fn mainnet() -> Self {
        Self {
            network: 1,
            encoding: None,
        }
    }

    pub fn testnet() -> Self {
        Self {
            network: 0,
            encoding: None,
        }
    }
}

impl From<u64> for NetworkId {
    fn from(network: u64) -> Self {
        NetworkId::new(network)
    }
}

impl From<NetworkId> for u64 {
    fn from(id: NetworkId) -> u64 {
        id.network
    }
}

impl Serialize for NetworkId {
    fn serialize<'se, W: Write>(
        &self,
        serializer: &'se mut Serializer<W>,
        force_canonical: bool,
    ) -> cbor_event::Result<&'se mut Serializer<W>> {
        serializer.write_unsigned_integer_sz(
            self.network,
            fit_sz(self.network, self.encoding, force_canonical),
        )
    }
}

impl Deserialize for NetworkId {
    fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
        let (network, encoding) = raw.unsigned_integer_sz().map(|(x, enc)| (x, Some(enc)))?;
        Ok(Self { network, encoding })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn bigint_uint_min() {
        let bytes = [0x00];
        let x = BigInt::from_cbor_bytes(&bytes).unwrap();
        assert_eq!(bytes, x.to_cbor_bytes().as_slice());
        assert_eq!(x.as_u64(), Some(u64::MIN));
        assert_eq!(x.as_int().unwrap().to_string(), x.to_string());
        assert_eq!(x.to_string(), "0");
    }

    #[test]
    fn bigint_uint_max() {
        let bytes = [0x1B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF];
        let x = BigInt::from_cbor_bytes(&bytes).unwrap();
        assert_eq!(bytes, x.to_cbor_bytes().as_slice());
        assert_eq!(x.as_u64(), Some(u64::MAX));
        assert_eq!(x.as_int().unwrap().to_string(), x.to_string());
        assert_eq!(x.to_string(), "18446744073709551615");
    }

    #[test]
    fn bigint_above_uint_min() {
        let bytes = [
            0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        ];
        let x = BigInt::from_cbor_bytes(&bytes).unwrap();
        assert_eq!(bytes, x.to_cbor_bytes().as_slice());
        assert_eq!(x.as_int(), None);
        assert_eq!(x.to_string(), "18446744073709551616");
    }

    #[test]
    fn bigint_nint_min() {
        let bytes = [0x3B, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF];
        let x = BigInt::from_cbor_bytes(&bytes).unwrap();
        assert_eq!(bytes, x.to_cbor_bytes().as_slice());
        assert_eq!(
            Into::<i128>::into(&x.as_int().unwrap()),
            -((u64::MAX as i128) + 1)
        );
        assert_eq!(x.as_int().unwrap().to_string(), x.to_string());
        assert_eq!(x.to_string(), "-18446744073709551616");
    }

    #[test]
    fn bigint_nint_max() {
        let bytes = [0x20];
        let x = BigInt::from_cbor_bytes(&bytes).unwrap();
        assert_eq!(bytes, x.to_cbor_bytes().as_slice());
        assert_eq!(x.as_u64(), None);
        assert_eq!(x.as_int().unwrap().to_string(), x.to_string());
        assert_eq!(x.to_string(), "-1");
    }

    #[test]
    fn bigint_below_nint_min() {
        let bytes = [
            0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        ];
        let x = BigInt::from_cbor_bytes(&bytes).unwrap();
        assert_eq!(bytes, x.to_cbor_bytes().as_slice());
        assert_eq!(x.as_int(), None);
        assert_eq!(x.to_string(), "-18446744073709551617");
    }
}