cardano-serialization-lib 15.0.3

(De)serialization functions for the Cardano blockchain along with related utility functions
Documentation
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
use crate::*;

#[test]
fn subtract_values() {
    let policy1 = PolicyID::from([0; ScriptHash::BYTE_COUNT]);
    let policy2 = PolicyID::from([1; ScriptHash::BYTE_COUNT]);

    let asset1 = AssetName(vec![1]);
    let asset2 = AssetName(vec![2]);
    let asset3 = AssetName(vec![3]);
    let asset4 = AssetName(vec![4]);

    let mut token_bundle1 = MultiAsset::new();
    {
        let mut asset_list1 = Assets::new();
        asset_list1.insert(&asset1, &BigNum(1));
        asset_list1.insert(&asset2, &BigNum(1));
        asset_list1.insert(&asset3, &BigNum(1));
        asset_list1.insert(&asset4, &BigNum(2));
        token_bundle1.insert(&policy1, &asset_list1);

        let mut asset_list2 = Assets::new();
        asset_list2.insert(&asset1, &BigNum(1));
        token_bundle1.insert(&policy2, &asset_list2);
    }
    let assets1 = Value {
        coin: BigNum(1555554),
        multiasset: Some(token_bundle1),
    };

    let mut token_bundle2 = MultiAsset::new();
    {
        let mut asset_list2 = Assets::new();
        // more than asset1 bundle
        asset_list2.insert(&asset1, &BigNum(2));
        // exactly equal to asset1 bundle
        asset_list2.insert(&asset2, &BigNum(1));
        // skip asset 3
        // less than in asset1 bundle
        asset_list2.insert(&asset4, &BigNum(1));
        token_bundle2.insert(&policy1, &asset_list2);

        // this policy should be removed entirely
        let mut asset_list2 = Assets::new();
        asset_list2.insert(&asset1, &BigNum(1));
        token_bundle2.insert(&policy2, &asset_list2);
    }

    let assets2 = Value {
        coin: BigNum(2555554),
        multiasset: Some(token_bundle2),
    };

    let result = assets1.clamped_sub(&assets2);
    assert_eq!(result.coin().to_str(), "0");
    assert_eq!(
        result.multiasset().unwrap().len(),
        1 // policy 2 was deleted successfully
    );
    let policy1_content = result.multiasset().unwrap().get(&policy1).unwrap();
    assert_eq!(policy1_content.len(), 2);
    assert_eq!(policy1_content.get(&asset3).unwrap().to_str(), "1");
    assert_eq!(policy1_content.get(&asset4).unwrap().to_str(), "1");
}

#[test]
fn compare_values() {
    let policy1 = PolicyID::from([0; ScriptHash::BYTE_COUNT]);

    let asset1 = AssetName(vec![1]);
    let asset2 = AssetName(vec![2]);

    // testing cases with no assets
    {
        let a = Value::new(&BigNum(1));
        let b = Value::new(&BigNum(1));
        assert_eq!(a.partial_cmp(&b).unwrap(), std::cmp::Ordering::Equal);
    }
    {
        let a = Value::new(&BigNum(2));
        let b = Value::new(&BigNum(1));
        assert_eq!(a.partial_cmp(&b).unwrap(), std::cmp::Ordering::Greater);
    }
    {
        let a = Value::new(&BigNum(1));
        let b = Value::new(&BigNum(2));
        assert_eq!(a.partial_cmp(&b).unwrap(), std::cmp::Ordering::Less);
    }
    // testing case where one side has assets
    {
        let mut token_bundle1 = MultiAsset::new();
        let mut asset_list1 = Assets::new();
        asset_list1.insert(&asset1, &BigNum(1));
        token_bundle1.insert(&policy1, &asset_list1);
        let a = Value {
            coin: BigNum(1),
            multiasset: Some(token_bundle1),
        };
        let b = Value::new(&BigNum(1));
        assert_eq!(a.partial_cmp(&b).unwrap(), std::cmp::Ordering::Greater);
    }
    {
        let mut token_bundle1 = MultiAsset::new();
        let mut asset_list1 = Assets::new();
        asset_list1.insert(&asset1, &BigNum(1));
        token_bundle1.insert(&policy1, &asset_list1);
        let a = Value::new(&BigNum(1));
        let b = Value {
            coin: BigNum(1),
            multiasset: Some(token_bundle1),
        };
        assert_eq!(a.partial_cmp(&b).unwrap(), std::cmp::Ordering::Less);
    }
    // testing case where both sides has assets
    {
        let mut token_bundle1 = MultiAsset::new();
        let mut asset_list1 = Assets::new();
        asset_list1.insert(&asset1, &BigNum(1));
        token_bundle1.insert(&policy1, &asset_list1);
        let a = Value {
            coin: BigNum(1),
            multiasset: Some(token_bundle1),
        };

        let mut token_bundle2 = MultiAsset::new();
        let mut asset_list2 = Assets::new();
        asset_list2.insert(&asset1, &BigNum(1));
        token_bundle2.insert(&policy1, &asset_list2);
        let b = Value {
            coin: BigNum(1),
            multiasset: Some(token_bundle2),
        };
        assert_eq!(a.partial_cmp(&b).unwrap(), std::cmp::Ordering::Equal);
    }
    {
        let mut token_bundle1 = MultiAsset::new();
        let mut asset_list1 = Assets::new();
        asset_list1.insert(&asset1, &BigNum(1));
        token_bundle1.insert(&policy1, &asset_list1);
        let a = Value {
            coin: BigNum(2),
            multiasset: Some(token_bundle1),
        };

        let mut token_bundle2 = MultiAsset::new();
        let mut asset_list2 = Assets::new();
        asset_list2.insert(&asset1, &BigNum(1));
        token_bundle2.insert(&policy1, &asset_list2);
        let b = Value {
            coin: BigNum(1),
            multiasset: Some(token_bundle2),
        };
        assert_eq!(a.partial_cmp(&b).unwrap(), std::cmp::Ordering::Greater);
    }
    {
        let mut token_bundle1 = MultiAsset::new();
        let mut asset_list1 = Assets::new();
        asset_list1.insert(&asset1, &BigNum(1));
        token_bundle1.insert(&policy1, &asset_list1);
        let a = Value {
            coin: BigNum(1),
            multiasset: Some(token_bundle1),
        };

        let mut token_bundle2 = MultiAsset::new();
        let mut asset_list2 = Assets::new();
        asset_list2.insert(&asset1, &BigNum(1));
        token_bundle2.insert(&policy1, &asset_list2);
        let b = Value {
            coin: BigNum(2),
            multiasset: Some(token_bundle2),
        };
        assert_eq!(a.partial_cmp(&b).unwrap(), std::cmp::Ordering::Less);
    }
    {
        let mut token_bundle1 = MultiAsset::new();
        let mut asset_list1 = Assets::new();
        asset_list1.insert(&asset1, &BigNum(2));
        token_bundle1.insert(&policy1, &asset_list1);
        let a = Value {
            coin: BigNum(1),
            multiasset: Some(token_bundle1),
        };

        let mut token_bundle2 = MultiAsset::new();
        let mut asset_list2 = Assets::new();
        asset_list2.insert(&asset1, &BigNum(1));
        token_bundle2.insert(&policy1, &asset_list2);
        let b = Value {
            coin: BigNum(1),
            multiasset: Some(token_bundle2),
        };
        assert_eq!(a.partial_cmp(&b).unwrap(), std::cmp::Ordering::Greater);
    }
    {
        let mut token_bundle1 = MultiAsset::new();
        let mut asset_list1 = Assets::new();
        asset_list1.insert(&asset1, &BigNum(2));
        token_bundle1.insert(&policy1, &asset_list1);
        let a = Value {
            coin: BigNum(2),
            multiasset: Some(token_bundle1),
        };

        let mut token_bundle2 = MultiAsset::new();
        let mut asset_list2 = Assets::new();
        asset_list2.insert(&asset1, &BigNum(1));
        token_bundle2.insert(&policy1, &asset_list2);
        let b = Value {
            coin: BigNum(1),
            multiasset: Some(token_bundle2),
        };
        assert_eq!(a.partial_cmp(&b).unwrap(), std::cmp::Ordering::Greater);
    }
    {
        let mut token_bundle1 = MultiAsset::new();
        let mut asset_list1 = Assets::new();
        asset_list1.insert(&asset1, &BigNum(2));
        token_bundle1.insert(&policy1, &asset_list1);
        let a = Value {
            coin: BigNum(1),
            multiasset: Some(token_bundle1),
        };

        let mut token_bundle2 = MultiAsset::new();
        let mut asset_list2 = Assets::new();
        asset_list2.insert(&asset1, &BigNum(1));
        token_bundle2.insert(&policy1, &asset_list2);
        let b = Value {
            coin: BigNum(2),
            multiasset: Some(token_bundle2),
        };
        assert_eq!(a.partial_cmp(&b), None);
    }
    {
        let mut token_bundle1 = MultiAsset::new();
        let mut asset_list1 = Assets::new();
        asset_list1.insert(&asset1, &BigNum(1));
        token_bundle1.insert(&policy1, &asset_list1);
        let a = Value {
            coin: BigNum(1),
            multiasset: Some(token_bundle1),
        };

        let mut token_bundle2 = MultiAsset::new();
        let mut asset_list2 = Assets::new();
        asset_list2.insert(&asset1, &BigNum(2));
        token_bundle2.insert(&policy1, &asset_list2);
        let b = Value {
            coin: BigNum(1),
            multiasset: Some(token_bundle2),
        };
        assert_eq!(a.partial_cmp(&b).unwrap(), std::cmp::Ordering::Less);
    }
    {
        let mut token_bundle1 = MultiAsset::new();
        let mut asset_list1 = Assets::new();
        asset_list1.insert(&asset1, &BigNum(1));
        token_bundle1.insert(&policy1, &asset_list1);
        let a = Value {
            coin: BigNum(1),
            multiasset: Some(token_bundle1),
        };

        let mut token_bundle2 = MultiAsset::new();
        let mut asset_list2 = Assets::new();
        asset_list2.insert(&asset1, &BigNum(2));
        token_bundle2.insert(&policy1, &asset_list2);
        let b = Value {
            coin: BigNum(2),
            multiasset: Some(token_bundle2),
        };
        assert_eq!(a.partial_cmp(&b).unwrap(), std::cmp::Ordering::Less);
    }
    {
        let mut token_bundle1 = MultiAsset::new();
        let mut asset_list1 = Assets::new();
        asset_list1.insert(&asset1, &BigNum(1));
        token_bundle1.insert(&policy1, &asset_list1);
        let a = Value {
            coin: BigNum(2),
            multiasset: Some(token_bundle1),
        };

        let mut token_bundle2 = MultiAsset::new();
        let mut asset_list2 = Assets::new();
        asset_list2.insert(&asset1, &BigNum(2));
        token_bundle2.insert(&policy1, &asset_list2);
        let b = Value {
            coin: BigNum(1),
            multiasset: Some(token_bundle2),
        };
        assert_eq!(a.partial_cmp(&b), None);
    }
    {
        let mut token_bundle1 = MultiAsset::new();
        let mut asset_list1 = Assets::new();
        asset_list1.insert(&asset1, &BigNum(1));
        token_bundle1.insert(&policy1, &asset_list1);
        let a = Value {
            coin: BigNum(1),
            multiasset: Some(token_bundle1),
        };

        let mut token_bundle2 = MultiAsset::new();
        let mut asset_list2 = Assets::new();
        asset_list2.insert(&asset2, &BigNum(1));
        token_bundle2.insert(&policy1, &asset_list2);
        let b = Value {
            coin: BigNum(1),
            multiasset: Some(token_bundle2),
        };
        assert_eq!(a.partial_cmp(&b), None);
    }
}

#[test]
fn bigint_serialization() {
    let zero = BigInt::from_str("0").unwrap();
    let zero_rt = BigInt::from_bytes(zero.to_bytes()).unwrap();
    assert_eq!(zero.to_str(), zero_rt.to_str());
    assert_eq!(zero.to_bytes(), vec![0x00]);

    let pos_small = BigInt::from_str("100").unwrap();
    let pos_small_rt = BigInt::from_bytes(pos_small.to_bytes()).unwrap();
    assert_eq!(pos_small.to_str(), pos_small_rt.to_str());

    let pos_big = BigInt::from_str("123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890").unwrap();
    let pos_big_rt = BigInt::from_bytes(pos_big.to_bytes()).unwrap();
    assert_eq!(pos_big.to_str(), pos_big_rt.to_str());

    let neg_small = BigInt::from_str("-100").unwrap();
    let neg_small_rt = BigInt::from_bytes(neg_small.to_bytes()).unwrap();
    assert_eq!(neg_small.to_str(), neg_small_rt.to_str());

    let neg_big = BigInt::from_str("-123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890").unwrap();
    let neg_big_rt = BigInt::from_bytes(neg_big.to_bytes()).unwrap();
    assert_eq!(neg_big.to_str(), neg_big_rt.to_str());

    // taken from CBOR RFC examples
    // negative big int
    assert_eq!(
        hex::decode("c349010000000000000000").unwrap(),
        BigInt::from_str("-18446744073709551617")
            .unwrap()
            .to_bytes()
    );
    // positive big int
    assert_eq!(
        hex::decode("c249010000000000000000").unwrap(),
        BigInt::from_str("18446744073709551616").unwrap().to_bytes()
    );
    // uint
    assert_eq!(
        hex::decode("1b000000e8d4a51000").unwrap(),
        BigInt::from_str("1000000000000").unwrap().to_bytes()
    );
    // nint (lowest possible - used to be unsupported but works now)
    assert_eq!(
        hex::decode("3bffffffffffffffff").unwrap(),
        BigInt::from_str("-18446744073709551616")
            .unwrap()
            .to_bytes()
    );
    // this one fits in an i64 though
    assert_eq!(
        hex::decode("3903e7").unwrap(),
        BigInt::from_str("-1000").unwrap().to_bytes()
    );

    let x = BigInt::from_str("-18446744073709551617").unwrap();
    let x_rt = BigInt::from_bytes(x.to_bytes()).unwrap();
    assert_eq!(x.to_str(), x_rt.to_str());
}

#[test]
fn bounded_bytes_read_chunked() {
    use std::io::Cursor;
    let chunks = vec![
        vec![
            0x52, 0x73, 0x6F, 0x6D, 0x65, 0x20, 0x72, 0x61, 0x6E, 0x64, 0x6F, 0x6D, 0x20, 0x73,
            0x74, 0x72, 0x69, 0x6E, 0x67,
        ],
        vec![0x44, 0x01, 0x02, 0x03, 0x04],
    ];
    let mut expected = Vec::new();
    for chunk in chunks.iter() {
        expected.extend_from_slice(&chunk[1..]);
    }
    let mut vec = vec![0x5f];
    for mut chunk in chunks {
        vec.append(&mut chunk);
    }
    vec.push(0xff);
    let mut raw = Deserializer::from(Cursor::new(vec.clone()));
    let found = read_bounded_bytes(&mut raw).unwrap();
    assert_eq!(found, expected);
}

#[test]
fn bounded_bytes_write_chunked() {
    let mut chunk_64 = vec![0x58, crate::utils::BOUNDED_BYTES_CHUNK_SIZE as u8];
    chunk_64.extend(std::iter::repeat(37).take(crate::utils::BOUNDED_BYTES_CHUNK_SIZE));
    let chunks = vec![chunk_64, vec![0x44, 0x01, 0x02, 0x03, 0x04]];
    let mut input = Vec::new();
    input.extend_from_slice(&chunks[0][2..]);
    input.extend_from_slice(&chunks[1][1..]);
    let mut serializer = cbor_event::se::Serializer::new_vec();
    write_bounded_bytes(&mut serializer, &input).unwrap();
    let written = serializer.finalize();
    let mut expected = vec![0x5f];
    for mut chunk in chunks {
        expected.append(&mut chunk);
    }
    expected.push(0xff);
    assert_eq!(expected, written);
}

#[test]
fn correct_script_data_hash() {
    let mut datums = PlutusList::new();
    datums.add(&PlutusData::new_integer(&BigInt::from_str("1000").unwrap()));
    let mut redeemers = Redeemers::new();
    redeemers.add(&Redeemer::new(
        &RedeemerTag::new_spend(),
        &BigNum::from_str("1").unwrap(),
        &PlutusData::new_integer(&BigInt::from_str("2000").unwrap()),
        &ExUnits::new(
            &BigNum::from_str("0").unwrap(),
            &BigNum::from_str("0").unwrap(),
        ),
    ));
    let plutus_cost_model = CostModel::from_bytes(vec![
        159, 26, 0, 3, 2, 89, 0, 1, 1, 26, 0, 6, 11, 199, 25, 2, 109, 0, 1, 26, 0, 2, 73, 240, 25,
        3, 232, 0, 1, 26, 0, 2, 73, 240, 24, 32, 26, 0, 37, 206, 168, 25, 113, 247, 4, 25, 116, 77,
        24, 100, 25, 116, 77, 24, 100, 25, 116, 77, 24, 100, 25, 116, 77, 24, 100, 25, 116, 77, 24,
        100, 25, 116, 77, 24, 100, 24, 100, 24, 100, 25, 116, 77, 24, 100, 26, 0, 2, 73, 240, 24,
        32, 26, 0, 2, 73, 240, 24, 32, 26, 0, 2, 73, 240, 24, 32, 26, 0, 2, 73, 240, 25, 3, 232, 0,
        1, 26, 0, 2, 73, 240, 24, 32, 26, 0, 2, 73, 240, 25, 3, 232, 0, 8, 26, 0, 2, 66, 32, 26, 0,
        6, 126, 35, 24, 118, 0, 1, 1, 26, 0, 2, 73, 240, 25, 3, 232, 0, 8, 26, 0, 2, 73, 240, 26,
        0, 1, 183, 152, 24, 247, 1, 26, 0, 2, 73, 240, 25, 39, 16, 1, 26, 0, 2, 21, 94, 25, 5, 46,
        1, 25, 3, 232, 26, 0, 2, 73, 240, 25, 3, 232, 1, 26, 0, 2, 73, 240, 24, 32, 26, 0, 2, 73,
        240, 24, 32, 26, 0, 2, 73, 240, 24, 32, 1, 1, 26, 0, 2, 73, 240, 1, 26, 0, 2, 73, 240, 4,
        26, 0, 1, 148, 175, 24, 248, 1, 26, 0, 1, 148, 175, 24, 248, 1, 26, 0, 2, 55, 124, 25, 5,
        86, 1, 26, 0, 2, 189, 234, 25, 1, 241, 1, 26, 0, 2, 73, 240, 24, 32, 26, 0, 2, 73, 240, 24,
        32, 26, 0, 2, 73, 240, 24, 32, 26, 0, 2, 73, 240, 24, 32, 26, 0, 2, 73, 240, 24, 32, 26, 0,
        2, 73, 240, 24, 32, 26, 0, 2, 66, 32, 26, 0, 6, 126, 35, 24, 118, 0, 1, 1, 25, 240, 76, 25,
        43, 210, 0, 1, 26, 0, 2, 73, 240, 24, 32, 26, 0, 2, 66, 32, 26, 0, 6, 126, 35, 24, 118, 0,
        1, 1, 26, 0, 2, 66, 32, 26, 0, 6, 126, 35, 24, 118, 0, 1, 1, 26, 0, 37, 206, 168, 25, 113,
        247, 4, 0, 26, 0, 1, 65, 187, 4, 26, 0, 2, 73, 240, 25, 19, 136, 0, 1, 26, 0, 2, 73, 240,
        24, 32, 26, 0, 3, 2, 89, 0, 1, 1, 26, 0, 2, 73, 240, 24, 32, 26, 0, 2, 73, 240, 24, 32, 26,
        0, 2, 73, 240, 24, 32, 26, 0, 2, 73, 240, 24, 32, 26, 0, 2, 73, 240, 24, 32, 26, 0, 2, 73,
        240, 24, 32, 26, 0, 2, 73, 240, 24, 32, 26, 0, 51, 13, 167, 1, 1, 255,
    ])
    .unwrap();
    let mut cost_models = Costmdls::new();
    cost_models.insert(&Language::new_plutus_v1(), &plutus_cost_model);
    let script_data_hash = hash_script_data(&redeemers, &cost_models, Some(datums));

    assert_eq!(
        hex::encode(script_data_hash.to_bytes()),
        "8452337aed2f75d45838155503407b4241a75f021c3818ec90383c8e0faca5a4"
    );
}

#[test]
fn native_scripts_from_wallet_json() {
    let cosigner0_hex = "1423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db11423856bc91c49e928f6f30f4e8d665d53eb4ab6028bd0ac971809d514c92db1";
    let cosigner1_hex = "a48d97f57ce49433f347d44ee07e54a100229b4f8e125d25f7bca9ad66d9707a25cd1331f46f7d6e279451637ca20802a25c441ba9436abf644fe5410d1080e3";
    let self_key_hex = "6ce83a12e9d4c783f54c0bb511303b37160a6e4f3f96b8e878a7c1f7751e18c4ccde3fb916d330d07f7bd51fb6bd99aa831d925008d3f7795033f48abd6df7f6";
    let native_script = encode_json_str_to_native_script(
        &format!(
            r#"
        {{
            "cosigners": {{
                "cosigner#0": "{}",
                "cosigner#1": "{}",
                "cosigner#2": "self"
            }},
            "template": {{
                "some": {{
                    "at_least": 2,
                    "from": [
                        {{
                            "all": [
                                "cosigner#0",
                                {{ "active_from": 120 }}
                            ]
                        }},
                        {{
                            "any": [
                                "cosigner#1",
                                {{ "active_until": 1000 }}
                            ]
                        }},
                        "cosigner#2"
                    ]
                }}
            }}
        }}"#,
            cosigner0_hex, cosigner1_hex
        ),
        self_key_hex,
        ScriptSchema::Wallet,
    );

    let n_of_k = native_script.unwrap().as_script_n_of_k().unwrap();
    let from = n_of_k.native_scripts();
    assert_eq!(n_of_k.n(), 2);
    assert_eq!(from.len(), 3);
    let all = from.get(0).as_script_all().unwrap().native_scripts();
    assert_eq!(all.len(), 2);
    let all_0 = all.get(0).as_script_pubkey().unwrap();
    assert_eq!(
        all_0.addr_keyhash(),
        Bip32PublicKey::from_bytes(&hex::decode(cosigner0_hex).unwrap())
            .unwrap()
            .to_raw_key()
            .hash()
    );
    let all_1 = all.get(1).as_timelock_start().unwrap();
    assert_eq!(all_1.slot().unwrap(), 120);
    let any = from.get(1).as_script_any().unwrap().native_scripts();
    assert_eq!(all.len(), 2);
    let any_0 = any.get(0).as_script_pubkey().unwrap();
    assert_eq!(
        any_0.addr_keyhash(),
        Bip32PublicKey::from_bytes(&hex::decode(cosigner1_hex).unwrap())
            .unwrap()
            .to_raw_key()
            .hash()
    );
    let any_1 = any.get(1).as_timelock_expiry().unwrap();
    assert_eq!(any_1.slot().unwrap(), 1000);
    let self_key = from.get(2).as_script_pubkey().unwrap();
    assert_eq!(
        self_key.addr_keyhash(),
        Bip32PublicKey::from_bytes(&hex::decode(self_key_hex).unwrap())
            .unwrap()
            .to_raw_key()
            .hash()
    );
}

#[test]
fn int_to_str() {
    assert_eq!(
        Int::new(&BigNum(u64::max_value())).to_str(),
        u64::max_value().to_string()
    );
    assert_eq!(
        Int::new(&BigNum(u64::min_value())).to_str(),
        u64::min_value().to_string()
    );
    assert_eq!(
        Int::new_negative(&BigNum(u64::max_value())).to_str(),
        (-(u64::max_value() as i128)).to_string()
    );
    assert_eq!(
        Int::new_negative(&BigNum(u64::min_value())).to_str(),
        (-(u64::min_value() as i128)).to_string()
    );
    assert_eq!(Int::new_i32(142).to_str(), "142");
    assert_eq!(Int::new_i32(-142).to_str(), "-142");
}

#[test]
fn int_as_i32_or_nothing() {
    let over_pos_i32 = (i32::max_value() as i64) + 1;
    assert!(Int::new(&BigNum(over_pos_i32 as u64))
        .as_i32_or_nothing()
        .is_none());

    let valid_pos_i32 = i32::max_value() as i64;
    assert_eq!(
        Int::new(&BigNum(valid_pos_i32 as u64))
            .as_i32_or_nothing()
            .unwrap(),
        i32::max_value()
    );

    let over_neg_i32 = (i32::min_value() as i64) - 1;
    assert!(Int::new_negative(&BigNum((-over_neg_i32) as u64))
        .as_i32_or_nothing()
        .is_none());

    let valid_neg_i32 = i32::min_value() as i64;
    assert_eq!(
        Int::new_negative(&BigNum((-valid_neg_i32) as u64))
            .as_i32_or_nothing()
            .unwrap(),
        i32::min_value()
    );

    assert!(Int::new(&BigNum(u64::max_value()))
        .as_i32_or_nothing()
        .is_none());
    assert_eq!(
        Int::new(&BigNum(i32::max_value() as u64))
            .as_i32_or_nothing()
            .unwrap(),
        i32::max_value()
    );
    assert_eq!(
        Int::new_negative(&BigNum(i32::max_value() as u64))
            .as_i32_or_nothing()
            .unwrap(),
        -i32::max_value()
    );

    assert_eq!(Int::new_i32(42).as_i32_or_nothing().unwrap(), 42);
    assert_eq!(Int::new_i32(-42).as_i32_or_nothing().unwrap(), -42);
}

#[test]
fn int_as_i32_or_fail() {
    let over_pos_i32 = (i32::max_value() as i64) + 1;
    assert!(Int::new(&BigNum(over_pos_i32 as u64))
        .as_i32_or_fail()
        .is_err());

    let valid_pos_i32 = i32::max_value() as i64;
    assert_eq!(
        Int::new(&BigNum(valid_pos_i32 as u64))
            .as_i32_or_fail()
            .unwrap(),
        i32::max_value()
    );

    let over_neg_i32 = (i32::min_value() as i64) - 1;
    assert!(Int::new_negative(&BigNum((-over_neg_i32) as u64))
        .as_i32_or_fail()
        .is_err());

    let valid_neg_i32 = i32::min_value() as i64;
    assert_eq!(
        Int::new_negative(&BigNum((-valid_neg_i32) as u64))
            .as_i32_or_fail()
            .unwrap(),
        i32::min_value()
    );

    assert!(Int::new(&BigNum(u64::max_value()))
        .as_i32_or_fail()
        .is_err());
    assert_eq!(
        Int::new(&BigNum(i32::max_value() as u64))
            .as_i32_or_fail()
            .unwrap(),
        i32::max_value()
    );
    assert_eq!(
        Int::new_negative(&BigNum(i32::max_value() as u64))
            .as_i32_or_fail()
            .unwrap(),
        -i32::max_value()
    );

    assert_eq!(Int::new_i32(42).as_i32_or_fail().unwrap(), 42);
    assert_eq!(Int::new_i32(-42).as_i32_or_fail().unwrap(), -42);
}

#[test]
fn int_full_range() {
    // cbor_event's nint API worked via i64 but we now have a workaround for it
    // so these tests are here to make sure that workaround works.

    // first nint below of i64::MIN
    let bytes_x = vec![0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
    let x = Int::from_bytes(bytes_x.clone()).unwrap();
    assert_eq!(x.to_str(), "-9223372036854775809");
    assert_eq!(bytes_x, x.to_bytes());

    // smallest possible nint which is -u64::MAX - 1
    let bytes_y = vec![0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff];
    let y = Int::from_bytes(bytes_y.clone()).unwrap();
    assert_eq!(y.to_str(), "-18446744073709551616");
    assert_eq!(bytes_y, y.to_bytes());
}

#[test]
fn test_bigint_add() {
    assert_eq!(to_bigint(10).add(&to_bigint(20)), to_bigint(30),);
    assert_eq!(to_bigint(500).add(&to_bigint(800)), to_bigint(1300),);
}

#[test]
fn test_bigint_mul() {
    assert_eq!(to_bigint(10).mul(&to_bigint(20)), to_bigint(200),);
    assert_eq!(to_bigint(500).mul(&to_bigint(800)), to_bigint(400000),);
    assert_eq!(to_bigint(12).mul(&to_bigint(22)), to_bigint(264),);
}

#[test]
fn test_bigint_div_ceil() {
    assert_eq!(to_bigint(20).div_ceil(&to_bigint(10)), to_bigint(2),);
    assert_eq!(to_bigint(20).div_ceil(&to_bigint(2)), to_bigint(10),);
    assert_eq!(to_bigint(21).div_ceil(&to_bigint(2)), to_bigint(11),);
    assert_eq!(to_bigint(6).div_ceil(&to_bigint(3)), to_bigint(2),);
    assert_eq!(to_bigint(5).div_ceil(&to_bigint(3)), to_bigint(2),);
    assert_eq!(to_bigint(7).div_ceil(&to_bigint(3)), to_bigint(3),);
}

#[test]
fn test_bignum_div() {
    assert_eq!(BigNum(10).div_floor(&BigNum(1)), BigNum(10),);
    assert_eq!(BigNum(10).div_floor(&BigNum(3)), BigNum(3),);
    assert_eq!(BigNum(10).div_floor(&BigNum(4)), BigNum(2),);
    assert_eq!(BigNum(10).div_floor(&BigNum(5)), BigNum(2),);
    assert_eq!(BigNum(10).div_floor(&BigNum(6)), BigNum(1),);
    assert_eq!(BigNum(10).div_floor(&BigNum(12)), BigNum::zero(),);
}

#[test]
fn test_vasil_v1_costmodel_hashing() {
    let v1 = Language::new_plutus_v1();
    let v1_cost_model = TxBuilderConstants::plutus_vasil_cost_models()
        .get(&v1)
        .unwrap();
    let mut costmodels = Costmdls::new();
    costmodels.insert(&v1, &v1_cost_model);
    let hash = hash_script_data(
        &Redeemers::from(vec![Redeemer::new(
            &RedeemerTag::new_spend(),
            &BigNum::zero(),
            &PlutusData::new_integer(&BigInt::from_str("42").unwrap()),
            &ExUnits::new(&BigNum(1700), &BigNum(368100)),
        )]),
        &costmodels,
        Some(PlutusList::from(vec![PlutusData::new_integer(
            &BigInt::from_str("42").unwrap(),
        )])),
    );
    assert_eq!(
        hex::encode(hash.to_bytes()),
        "f173f8e25f385c61c33ab84c1e4a1af36fcd47dc7ab83d89f926828f618630f5"
    );
}

#[test]
fn bigint_as_int() {
    let zero = BigInt::from_str("0").unwrap();
    let zero_int = zero.as_int().unwrap();
    assert_eq!(zero_int.0, 0i128);

    let pos = BigInt::from_str("1024").unwrap();
    let pos_int = pos.as_int().unwrap();
    assert_eq!(pos_int.0, 1024i128);

    let neg = BigInt::from_str("-1024").unwrap();
    let neg_int = neg.as_int().unwrap();
    assert_eq!(neg_int.0, -1024i128);
}

#[test]
fn has_transaction_set_tag_tx_with_only_tag() {
    let hex = "84a400d90102818258203b40265111d8bb3c3c608d95b3a0bf83461ace32d79336579a1939b3aad1c0b700018182581d611c616f1acb460668a9b2f123c80372c2adad3583b9c6cd2b1deeed1c01021a00016f32030aa100d9010281825820f9aa3fccb7fe539e471188ccc9ee65514c5961c070b06ca185962484a4813bee58406d68d8b7b2ee54f1f46b64e3f61a14f840be2ec125c858ec917f634a1eb898a51660654839226016a2588d39920e6dfe1b66d917027f198b5eb887d20f4ac805f5f6";
    let tx_sets = has_transaction_set_tag(hex::decode(hex).unwrap()).unwrap();
    assert_eq!(tx_sets, TransactionSetsState::AllSetsHaveTag);
}

#[test]
fn has_transaction_set_tag_tx_without_tag() {
    let hex = "84a400818258203b40265111d8bb3c3c608d95b3a0bf83461ace32d79336579a1939b3aad1c0b700018182581d611c616f1acb460668a9b2f123c80372c2adad3583b9c6cd2b1deeed1c01021a00016f32030aa10081825820f9aa3fccb7fe539e471188ccc9ee65514c5961c070b06ca185962484a4813bee5840fae5de40c94d759ce13bf9886262159c4f26a289fd192e165995b785259e503f6887bf39dfa23a47cf163784c6eee23f61440e749bc1df3c73975f5231aeda0ff5f6";
    let tx_sets = has_transaction_set_tag(hex::decode(hex).unwrap()).unwrap();
    assert_eq!(tx_sets, TransactionSetsState::AllSetsHaveNoTag);
}

#[test]
fn has_transaction_set_tag_mixed() {
    let hex = "84a400818258203b40265111d8bb3c3c608d95b3a0bf83461ace32d79336579a1939b3aad1c0b700018182581d611c616f1acb460668a9b2f123c80372c2adad3583b9c6cd2b1deeed1c01021a00016f32030aa100d9010281825820f9aa3fccb7fe539e471188ccc9ee65514c5961c070b06ca185962484a4813bee58406d68d8b7b2ee54f1f46b64e3f61a14f840be2ec125c858ec917f634a1eb898a51660654839226016a2588d39920e6dfe1b66d917027f198b5eb887d20f4ac805f5f6";
    let tx_sets = has_transaction_set_tag(hex::decode(hex).unwrap()).unwrap();
    assert_eq!(tx_sets, TransactionSetsState::MixedSets);
}


#[test]
fn value_empty_asset_equal() {
    let a = Value {
        coin: BigNum(0),
        multiasset: None,
    };
    let b = Value {
        coin: BigNum(0),
        multiasset: Some(MultiAsset::new()),
    };
    let c = Value {
        coin: BigNum(0),
        multiasset: None,
    };

    assert_eq!(a, b);
    assert_eq!(a, c);
}

#[test]
fn value_empty_asset_not_equal() {
    let a = Value {
        coin: BigNum(1),
        multiasset: None,
    };
    let b = Value {
        coin: BigNum(2),
        multiasset: Some(MultiAsset::new()),
    };
    let c = Value {
        coin: BigNum(3),
        multiasset: None,
    };

    assert_ne!(a, b);
    assert_ne!(a, c);
}