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
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
// This file was code-generated using an experimental CDDL to rust tool:
// https://github.com/dcSpark/cddl-codegen

use crate::address::Address;
use crate::assets::{Coin, Mint, PositiveCoin, Value};
use crate::auxdata::AuxiliaryData;
use crate::governance::VotingProcedures;
use crate::plutus::PlutusData;
use crate::Script;
use crate::{
    BootstrapWitnessList, CertificateList, NativeScriptList, NetworkId, PlutusDataList,
    PlutusV1ScriptList, PlutusV2ScriptList, PlutusV3ScriptList, ProposalProcedureList,
    RedeemerList, Slot, TransactionInputList, TransactionOutputList, VkeywitnessList, Withdrawals,
};
use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions};
use cml_crypto_wasm::{
    AuxiliaryDataHash, DatumHash, Ed25519KeyHash, ScriptDataHash, TransactionHash,
};
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};

pub mod utils;

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct AlonzoFormatTxOut(cml_chain::transaction::AlonzoFormatTxOut);

impl_wasm_cbor_json_api!(AlonzoFormatTxOut);

impl_wasm_conversions!(cml_chain::transaction::AlonzoFormatTxOut, AlonzoFormatTxOut);

#[wasm_bindgen]
impl AlonzoFormatTxOut {
    pub fn address(&self) -> Address {
        self.0.address.clone().into()
    }

    pub fn amount(&self) -> Value {
        self.0.amount.clone().into()
    }

    pub fn set_datum_hash(&mut self, datum_hash: &DatumHash) {
        self.0.datum_hash = Some(datum_hash.clone().into())
    }

    pub fn datum_hash(&self) -> Option<DatumHash> {
        self.0.datum_hash.map(std::convert::Into::into)
    }

    pub fn new(address: &Address, amount: &Value) -> Self {
        Self(cml_chain::transaction::AlonzoFormatTxOut::new(
            address.clone().into(),
            amount.clone().into(),
        ))
    }
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct ConwayFormatTxOut(cml_chain::transaction::ConwayFormatTxOut);

impl_wasm_cbor_json_api!(ConwayFormatTxOut);

impl_wasm_conversions!(cml_chain::transaction::ConwayFormatTxOut, ConwayFormatTxOut);

#[wasm_bindgen]
impl ConwayFormatTxOut {
    pub fn address(&self) -> Address {
        self.0.address.clone().into()
    }

    pub fn amount(&self) -> Value {
        self.0.amount.clone().into()
    }

    pub fn set_datum_option(&mut self, datum_option: &DatumOption) {
        self.0.datum_option = Some(datum_option.clone().into())
    }

    pub fn datum_option(&self) -> Option<DatumOption> {
        self.0.datum_option.clone().map(std::convert::Into::into)
    }

    pub fn set_script_reference(&mut self, script_reference: &ScriptRef) {
        self.0.script_reference = Some(script_reference.clone().into())
    }

    pub fn script_reference(&self) -> Option<ScriptRef> {
        self.0
            .script_reference
            .clone()
            .map(std::convert::Into::into)
    }

    pub fn new(address: &Address, amount: &Value) -> Self {
        Self(cml_chain::transaction::ConwayFormatTxOut::new(
            address.clone().into(),
            amount.clone().into(),
        ))
    }
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct DatumOption(cml_chain::transaction::DatumOption);

impl_wasm_cbor_json_api!(DatumOption);

impl_wasm_conversions!(cml_chain::transaction::DatumOption, DatumOption);

#[wasm_bindgen]
impl DatumOption {
    pub fn new_hash(datum_hash: &DatumHash) -> Self {
        Self(cml_chain::transaction::DatumOption::new_hash(
            datum_hash.clone().into(),
        ))
    }

    pub fn new_datum(datum: &PlutusData) -> Self {
        Self(cml_chain::transaction::DatumOption::new_datum(
            datum.clone().into(),
        ))
    }

    pub fn kind(&self) -> DatumOptionKind {
        match &self.0 {
            cml_chain::transaction::DatumOption::Hash { .. } => DatumOptionKind::Hash,
            cml_chain::transaction::DatumOption::Datum { .. } => DatumOptionKind::Datum,
        }
    }

    pub fn as_hash(&self) -> Option<DatumHash> {
        match &self.0 {
            cml_chain::transaction::DatumOption::Hash { datum_hash, .. } => {
                Some((*datum_hash).into())
            }
            _ => None,
        }
    }

    pub fn as_datum(&self) -> Option<PlutusData> {
        match &self.0 {
            cml_chain::transaction::DatumOption::Datum { datum, .. } => Some(datum.clone().into()),
            _ => None,
        }
    }
}

#[wasm_bindgen]
pub enum DatumOptionKind {
    Hash,
    Datum,
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct NativeScript(cml_chain::transaction::NativeScript);

impl_wasm_cbor_json_api!(NativeScript);

impl_wasm_conversions!(cml_chain::transaction::NativeScript, NativeScript);

#[wasm_bindgen]
impl NativeScript {
    pub fn new_script_pubkey(ed25519_key_hash: &Ed25519KeyHash) -> Self {
        Self(cml_chain::transaction::NativeScript::new_script_pubkey(
            ed25519_key_hash.clone().into(),
        ))
    }

    pub fn new_script_all(native_scripts: &NativeScriptList) -> Self {
        Self(cml_chain::transaction::NativeScript::new_script_all(
            native_scripts.clone().into(),
        ))
    }

    pub fn new_script_any(native_scripts: &NativeScriptList) -> Self {
        Self(cml_chain::transaction::NativeScript::new_script_any(
            native_scripts.clone().into(),
        ))
    }

    pub fn new_script_n_of_k(n: u64, native_scripts: &NativeScriptList) -> Self {
        Self(cml_chain::transaction::NativeScript::new_script_n_of_k(
            n,
            native_scripts.clone().into(),
        ))
    }

    pub fn new_script_invalid_before(before: Slot) -> Self {
        Self(cml_chain::transaction::NativeScript::new_script_invalid_before(before))
    }

    pub fn new_script_invalid_hereafter(after: Slot) -> Self {
        Self(cml_chain::transaction::NativeScript::new_script_invalid_hereafter(after))
    }

    pub fn kind(&self) -> NativeScriptKind {
        match &self.0 {
            cml_chain::transaction::NativeScript::ScriptPubkey(_) => NativeScriptKind::ScriptPubkey,
            cml_chain::transaction::NativeScript::ScriptAll(_) => NativeScriptKind::ScriptAll,
            cml_chain::transaction::NativeScript::ScriptAny(_) => NativeScriptKind::ScriptAny,
            cml_chain::transaction::NativeScript::ScriptNOfK(_) => NativeScriptKind::ScriptNOfK,
            cml_chain::transaction::NativeScript::ScriptInvalidBefore(_) => {
                NativeScriptKind::ScriptInvalidBefore
            }
            cml_chain::transaction::NativeScript::ScriptInvalidHereafter(_) => {
                NativeScriptKind::ScriptInvalidHereafter
            }
        }
    }

    pub fn as_script_pubkey(&self) -> Option<ScriptPubkey> {
        match &self.0 {
            cml_chain::transaction::NativeScript::ScriptPubkey(script_pubkey) => {
                Some(script_pubkey.clone().into())
            }
            _ => None,
        }
    }

    pub fn as_script_all(&self) -> Option<ScriptAll> {
        match &self.0 {
            cml_chain::transaction::NativeScript::ScriptAll(script_all) => {
                Some(script_all.clone().into())
            }
            _ => None,
        }
    }

    pub fn as_script_any(&self) -> Option<ScriptAny> {
        match &self.0 {
            cml_chain::transaction::NativeScript::ScriptAny(script_any) => {
                Some(script_any.clone().into())
            }
            _ => None,
        }
    }

    pub fn as_script_n_of_k(&self) -> Option<ScriptNOfK> {
        match &self.0 {
            cml_chain::transaction::NativeScript::ScriptNOfK(script_n_of_k) => {
                Some(script_n_of_k.clone().into())
            }
            _ => None,
        }
    }

    pub fn as_script_invalid_before(&self) -> Option<ScriptInvalidBefore> {
        match &self.0 {
            cml_chain::transaction::NativeScript::ScriptInvalidBefore(script_invalid_before) => {
                Some(script_invalid_before.clone().into())
            }
            _ => None,
        }
    }

    pub fn as_script_invalid_hereafter(&self) -> Option<ScriptInvalidHereafter> {
        match &self.0 {
            cml_chain::transaction::NativeScript::ScriptInvalidHereafter(
                script_invalid_hereafter,
            ) => Some(script_invalid_hereafter.clone().into()),
            _ => None,
        }
    }
}

#[wasm_bindgen]
pub enum NativeScriptKind {
    ScriptPubkey,
    ScriptAll,
    ScriptAny,
    ScriptNOfK,
    ScriptInvalidBefore,
    ScriptInvalidHereafter,
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct RequiredSigners(Vec<cml_chain::crypto::Ed25519KeyHash>);

impl_wasm_conversions!(Vec<cml_chain::crypto::Ed25519KeyHash>, RequiredSigners);

#[wasm_bindgen]
impl RequiredSigners {
    pub fn new() -> Self {
        Self(Vec::new())
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn get(&self, index: usize) -> Ed25519KeyHash {
        self.0[index].into()
    }

    pub fn add(&mut self, elem: &Ed25519KeyHash) {
        self.0.push(elem.clone().into());
    }
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct ScriptAll(cml_chain::transaction::ScriptAll);

impl_wasm_cbor_json_api!(ScriptAll);

impl_wasm_conversions!(cml_chain::transaction::ScriptAll, ScriptAll);

#[wasm_bindgen]
impl ScriptAll {
    pub fn native_scripts(&self) -> NativeScriptList {
        self.0.native_scripts.clone().into()
    }

    pub fn new(native_scripts: &NativeScriptList) -> Self {
        Self(cml_chain::transaction::ScriptAll::new(
            native_scripts.clone().into(),
        ))
    }
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct ScriptAny(cml_chain::transaction::ScriptAny);

impl_wasm_cbor_json_api!(ScriptAny);

impl_wasm_conversions!(cml_chain::transaction::ScriptAny, ScriptAny);

#[wasm_bindgen]
impl ScriptAny {
    pub fn native_scripts(&self) -> NativeScriptList {
        self.0.native_scripts.clone().into()
    }

    pub fn new(native_scripts: &NativeScriptList) -> Self {
        Self(cml_chain::transaction::ScriptAny::new(
            native_scripts.clone().into(),
        ))
    }
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct ScriptInvalidBefore(cml_chain::transaction::ScriptInvalidBefore);

impl_wasm_cbor_json_api!(ScriptInvalidBefore);

impl_wasm_conversions!(
    cml_chain::transaction::ScriptInvalidBefore,
    ScriptInvalidBefore
);

#[wasm_bindgen]
impl ScriptInvalidBefore {
    pub fn before(&self) -> Slot {
        self.0.before
    }

    pub fn new(before: Slot) -> Self {
        Self(cml_chain::transaction::ScriptInvalidBefore::new(before))
    }
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct ScriptInvalidHereafter(cml_chain::transaction::ScriptInvalidHereafter);

impl_wasm_cbor_json_api!(ScriptInvalidHereafter);

impl_wasm_conversions!(
    cml_chain::transaction::ScriptInvalidHereafter,
    ScriptInvalidHereafter
);

#[wasm_bindgen]
impl ScriptInvalidHereafter {
    pub fn after(&self) -> Slot {
        self.0.after
    }

    pub fn new(after: Slot) -> Self {
        Self(cml_chain::transaction::ScriptInvalidHereafter::new(after))
    }
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct ScriptNOfK(cml_chain::transaction::ScriptNOfK);

impl_wasm_cbor_json_api!(ScriptNOfK);

impl_wasm_conversions!(cml_chain::transaction::ScriptNOfK, ScriptNOfK);

#[wasm_bindgen]
impl ScriptNOfK {
    pub fn n(&self) -> u64 {
        self.0.n
    }

    pub fn native_scripts(&self) -> NativeScriptList {
        self.0.native_scripts.clone().into()
    }

    pub fn new(n: u64, native_scripts: &NativeScriptList) -> Self {
        Self(cml_chain::transaction::ScriptNOfK::new(
            n,
            native_scripts.clone().into(),
        ))
    }
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct ScriptPubkey(cml_chain::transaction::ScriptPubkey);

impl_wasm_cbor_json_api!(ScriptPubkey);

impl_wasm_conversions!(cml_chain::transaction::ScriptPubkey, ScriptPubkey);

#[wasm_bindgen]
impl ScriptPubkey {
    pub fn ed25519_key_hash(&self) -> Ed25519KeyHash {
        self.0.ed25519_key_hash.into()
    }

    pub fn new(ed25519_key_hash: &Ed25519KeyHash) -> Self {
        Self(cml_chain::transaction::ScriptPubkey::new(
            ed25519_key_hash.clone().into(),
        ))
    }
}

pub type ScriptRef = Script;

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct Transaction(cml_chain::transaction::Transaction);

impl_wasm_cbor_json_api!(Transaction);

impl_wasm_conversions!(cml_chain::transaction::Transaction, Transaction);

#[wasm_bindgen]
impl Transaction {
    pub fn body(&self) -> TransactionBody {
        self.0.body.clone().into()
    }

    pub fn witness_set(&self) -> TransactionWitnessSet {
        self.0.witness_set.clone().into()
    }

    pub fn is_valid(&self) -> bool {
        self.0.is_valid
    }

    pub fn auxiliary_data(&self) -> Option<AuxiliaryData> {
        self.0.auxiliary_data.clone().map(std::convert::Into::into)
    }

    pub fn new(
        body: &TransactionBody,
        witness_set: &TransactionWitnessSet,
        is_valid: bool,
        auxiliary_data: Option<AuxiliaryData>,
    ) -> Self {
        Self(cml_chain::transaction::Transaction::new(
            body.clone().into(),
            witness_set.clone().into(),
            is_valid,
            auxiliary_data.map(Into::into),
        ))
    }
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct TransactionBody(cml_chain::transaction::TransactionBody);

impl_wasm_cbor_json_api!(TransactionBody);

impl_wasm_conversions!(cml_chain::transaction::TransactionBody, TransactionBody);

#[wasm_bindgen]
impl TransactionBody {
    pub fn inputs(&self) -> TransactionInputList {
        self.0.inputs.clone().into()
    }

    pub fn outputs(&self) -> TransactionOutputList {
        self.0.outputs.clone().into()
    }

    pub fn fee(&self) -> Coin {
        self.0.fee
    }

    pub fn set_ttl(&mut self, ttl: u64) {
        self.0.ttl = Some(ttl)
    }

    pub fn ttl(&self) -> Option<u64> {
        self.0.ttl
    }

    pub fn set_certs(&mut self, certs: &CertificateList) {
        self.0.certs = Some(certs.clone().into())
    }

    pub fn certs(&self) -> Option<CertificateList> {
        self.0.certs.clone().map(std::convert::Into::into)
    }

    pub fn set_withdrawals(&mut self, withdrawals: &Withdrawals) {
        self.0.withdrawals = Some(withdrawals.clone().into())
    }

    pub fn withdrawals(&self) -> Option<Withdrawals> {
        self.0.withdrawals.clone().map(std::convert::Into::into)
    }

    pub fn set_auxiliary_data_hash(&mut self, auxiliary_data_hash: &AuxiliaryDataHash) {
        self.0.auxiliary_data_hash = Some(auxiliary_data_hash.clone().into())
    }

    pub fn auxiliary_data_hash(&self) -> Option<AuxiliaryDataHash> {
        self.0.auxiliary_data_hash.map(std::convert::Into::into)
    }

    pub fn set_validity_interval_start(&mut self, validity_interval_start: u64) {
        self.0.validity_interval_start = Some(validity_interval_start)
    }

    pub fn validity_interval_start(&self) -> Option<u64> {
        self.0.validity_interval_start
    }

    pub fn set_mint(&mut self, mint: &Mint) {
        self.0.mint = Some(mint.clone().into())
    }

    pub fn mint(&self) -> Option<Mint> {
        self.0.mint.clone().map(std::convert::Into::into)
    }

    pub fn set_script_data_hash(&mut self, script_data_hash: &ScriptDataHash) {
        self.0.script_data_hash = Some(script_data_hash.clone().into())
    }

    pub fn script_data_hash(&self) -> Option<ScriptDataHash> {
        self.0.script_data_hash.map(std::convert::Into::into)
    }

    pub fn set_collateral_inputs(&mut self, collateral_inputs: &TransactionInputList) {
        self.0.collateral_inputs = Some(collateral_inputs.clone().into())
    }

    pub fn collateral_inputs(&self) -> Option<TransactionInputList> {
        self.0
            .collateral_inputs
            .clone()
            .map(std::convert::Into::into)
    }

    pub fn set_required_signers(&mut self, required_signers: &RequiredSigners) {
        self.0.required_signers = Some(required_signers.clone().into())
    }

    pub fn required_signers(&self) -> Option<RequiredSigners> {
        self.0
            .required_signers
            .clone()
            .map(std::convert::Into::into)
    }

    pub fn set_network_id(&mut self, network_id: &NetworkId) {
        self.0.network_id = Some(network_id.clone().into())
    }

    pub fn network_id(&self) -> Option<NetworkId> {
        self.0.network_id.map(std::convert::Into::into)
    }

    pub fn set_collateral_return(&mut self, collateral_return: &TransactionOutput) {
        self.0.collateral_return = Some(collateral_return.clone().into())
    }

    pub fn collateral_return(&self) -> Option<TransactionOutput> {
        self.0
            .collateral_return
            .clone()
            .map(std::convert::Into::into)
    }

    pub fn set_total_collateral(&mut self, total_collateral: Coin) {
        self.0.total_collateral = Some(total_collateral)
    }

    pub fn total_collateral(&self) -> Option<Coin> {
        self.0.total_collateral
    }

    pub fn set_reference_inputs(&mut self, reference_inputs: &TransactionInputList) {
        self.0.reference_inputs = Some(reference_inputs.clone().into())
    }

    pub fn reference_inputs(&self) -> Option<TransactionInputList> {
        self.0
            .reference_inputs
            .clone()
            .map(std::convert::Into::into)
    }

    pub fn set_voting_procedures(&mut self, voting_procedures: &VotingProcedures) {
        self.0.voting_procedures = Some(voting_procedures.clone().into())
    }

    pub fn voting_procedures(&self) -> Option<VotingProcedures> {
        self.0
            .voting_procedures
            .clone()
            .map(std::convert::Into::into)
    }

    pub fn set_proposal_procedures(&mut self, proposal_procedures: &ProposalProcedureList) {
        self.0.proposal_procedures = Some(proposal_procedures.clone().into())
    }

    pub fn proposal_procedures(&self) -> Option<ProposalProcedureList> {
        self.0
            .proposal_procedures
            .clone()
            .map(std::convert::Into::into)
    }

    pub fn set_current_treasury_value(&mut self, current_treasury_value: Coin) {
        self.0.current_treasury_value = Some(current_treasury_value)
    }

    pub fn current_treasury_value(&self) -> Option<Coin> {
        self.0.current_treasury_value
    }

    pub fn set_donation(&mut self, donation: PositiveCoin) {
        self.0.donation = Some(donation)
    }

    pub fn donation(&self) -> Option<PositiveCoin> {
        self.0.donation
    }

    pub fn new(inputs: &TransactionInputList, outputs: &TransactionOutputList, fee: Coin) -> Self {
        Self(cml_chain::transaction::TransactionBody::new(
            inputs.clone().into(),
            outputs.clone().into(),
            fee,
        ))
    }
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct TransactionInput(cml_chain::transaction::TransactionInput);

impl_wasm_cbor_json_api!(TransactionInput);

impl_wasm_conversions!(cml_chain::transaction::TransactionInput, TransactionInput);

#[wasm_bindgen]
impl TransactionInput {
    pub fn transaction_id(&self) -> TransactionHash {
        self.0.transaction_id.into()
    }

    pub fn index(&self) -> u64 {
        self.0.index
    }

    pub fn new(transaction_id: &TransactionHash, index: u64) -> Self {
        Self(cml_chain::transaction::TransactionInput::new(
            transaction_id.clone().into(),
            index,
        ))
    }
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct TransactionOutput(cml_chain::transaction::TransactionOutput);

impl_wasm_cbor_json_api!(TransactionOutput);

impl_wasm_conversions!(cml_chain::transaction::TransactionOutput, TransactionOutput);

#[wasm_bindgen]
impl TransactionOutput {
    pub fn new_alonzo_format_tx_out(alonzo_format_tx_out: &AlonzoFormatTxOut) -> Self {
        Self(
            cml_chain::transaction::TransactionOutput::new_alonzo_format_tx_out(
                alonzo_format_tx_out.clone().into(),
            ),
        )
    }

    pub fn new_conway_format_tx_out(conway_format_tx_out: &ConwayFormatTxOut) -> Self {
        Self(
            cml_chain::transaction::TransactionOutput::new_conway_format_tx_out(
                conway_format_tx_out.clone().into(),
            ),
        )
    }

    pub fn kind(&self) -> TransactionOutputKind {
        match &self.0 {
            cml_chain::transaction::TransactionOutput::AlonzoFormatTxOut(_) => {
                TransactionOutputKind::AlonzoFormatTxOut
            }
            cml_chain::transaction::TransactionOutput::ConwayFormatTxOut(_) => {
                TransactionOutputKind::ConwayFormatTxOut
            }
        }
    }

    pub fn as_alonzo_format_tx_out(&self) -> Option<AlonzoFormatTxOut> {
        match &self.0 {
            cml_chain::transaction::TransactionOutput::AlonzoFormatTxOut(alonzo_format_tx_out) => {
                Some(alonzo_format_tx_out.clone().into())
            }
            _ => None,
        }
    }

    pub fn as_conway_format_tx_out(&self) -> Option<ConwayFormatTxOut> {
        match &self.0 {
            cml_chain::transaction::TransactionOutput::ConwayFormatTxOut(conway_format_tx_out) => {
                Some(conway_format_tx_out.clone().into())
            }
            _ => None,
        }
    }
}

#[wasm_bindgen]
pub enum TransactionOutputKind {
    AlonzoFormatTxOut,
    ConwayFormatTxOut,
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct TransactionWitnessSet(cml_chain::transaction::TransactionWitnessSet);

impl_wasm_cbor_json_api!(TransactionWitnessSet);

impl_wasm_conversions!(
    cml_chain::transaction::TransactionWitnessSet,
    TransactionWitnessSet
);

#[wasm_bindgen]
impl TransactionWitnessSet {
    pub fn set_vkeywitnesses(&mut self, vkeywitnesses: &VkeywitnessList) {
        self.0.vkeywitnesses = Some(vkeywitnesses.clone().into())
    }

    pub fn vkeywitnesses(&self) -> Option<VkeywitnessList> {
        self.0.vkeywitnesses.clone().map(std::convert::Into::into)
    }

    pub fn set_native_scripts(&mut self, native_scripts: &NativeScriptList) {
        self.0.native_scripts = Some(native_scripts.clone().into())
    }

    pub fn native_scripts(&self) -> Option<NativeScriptList> {
        self.0.native_scripts.clone().map(std::convert::Into::into)
    }

    pub fn set_bootstrap_witnesses(&mut self, bootstrap_witnesses: &BootstrapWitnessList) {
        self.0.bootstrap_witnesses = Some(bootstrap_witnesses.clone().into())
    }

    pub fn bootstrap_witnesses(&self) -> Option<BootstrapWitnessList> {
        self.0
            .bootstrap_witnesses
            .clone()
            .map(std::convert::Into::into)
    }

    pub fn set_plutus_v1_scripts(&mut self, plutus_v1_scripts: &PlutusV1ScriptList) {
        self.0.plutus_v1_scripts = Some(plutus_v1_scripts.clone().into())
    }

    pub fn plutus_v1_scripts(&self) -> Option<PlutusV1ScriptList> {
        self.0
            .plutus_v1_scripts
            .clone()
            .map(std::convert::Into::into)
    }

    pub fn set_plutus_datums(&mut self, plutus_datums: &PlutusDataList) {
        self.0.plutus_datums = Some(plutus_datums.clone().into())
    }

    pub fn plutus_datums(&self) -> Option<PlutusDataList> {
        self.0.plutus_datums.clone().map(std::convert::Into::into)
    }

    pub fn set_redeemers(&mut self, redeemers: &RedeemerList) {
        self.0.redeemers = Some(redeemers.clone().into())
    }

    pub fn redeemers(&self) -> Option<RedeemerList> {
        self.0.redeemers.clone().map(std::convert::Into::into)
    }

    pub fn set_plutus_v2_scripts(&mut self, plutus_v2_scripts: &PlutusV2ScriptList) {
        self.0.plutus_v2_scripts = Some(plutus_v2_scripts.clone().into())
    }

    pub fn plutus_v2_scripts(&self) -> Option<PlutusV2ScriptList> {
        self.0
            .plutus_v2_scripts
            .clone()
            .map(std::convert::Into::into)
    }

    pub fn set_plutus_v3_scripts(&mut self, plutus_v3_scripts: &PlutusV3ScriptList) {
        self.0.plutus_v3_scripts = Some(plutus_v3_scripts.clone().into())
    }

    pub fn plutus_v3_scripts(&self) -> Option<PlutusV3ScriptList> {
        self.0
            .plutus_v3_scripts
            .clone()
            .map(std::convert::Into::into)
    }

    pub fn new() -> Self {
        Self(cml_chain::transaction::TransactionWitnessSet::new())
    }
}