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
use crate::{
    address::Address,
    transaction::{DatumOption, ScriptRef, TransactionOutput},
    utils::LanguageList,
    Ed25519KeyHashList, NativeScript, Value,
};
use cml_crypto_wasm::{DatumHash, ScriptHash};
use wasm_bindgen::prelude::wasm_bindgen;

use super::TransactionWitnessSet;

#[wasm_bindgen]
impl TransactionOutput {
    pub fn new(
        address: &Address,
        amount: &Value,
        datum_option: Option<DatumOption>,
        script_reference: Option<ScriptRef>,
    ) -> Self {
        cml_chain::transaction::TransactionOutput::new(
            address.clone().into(),
            amount.clone().into(),
            datum_option.map(Into::into),
            script_reference.map(Into::into),
        )
        .into()
    }

    pub fn address(&self) -> Address {
        self.0.address().clone().into()
    }

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

    pub fn set_amount(&mut self, amount: &Value) {
        self.0.set_amount(amount.clone().into())
    }

    pub fn datum(&self) -> Option<DatumOption> {
        self.0.datum().map(Into::into)
    }

    /// Get the datum hash from a tx output if present as a hash.
    /// Returns None if there is no datum, or the datum is inlined.
    /// Use TransactionOutput::datum() for inlined datums.
    pub fn datum_hash(&self) -> Option<DatumHash> {
        self.0.datum_hash().cloned().map(Into::into)
    }

    pub fn script_ref(&self) -> Option<ScriptRef> {
        self.0.script_ref().cloned().map(Into::into)
    }
}

// TODO: anything here? pub type RequiredSignersSet = BTreeSet<Ed25519KeyHash>;

#[wasm_bindgen]
impl NativeScript {
    /// Returns an array of unique Ed25519KeyHashes
    /// contained within this script recursively on any depth level.
    /// The order of the keys in the result is not determined in any way.
    pub fn get_required_signers(&self) -> Ed25519KeyHashList {
        self.as_ref().get_required_signers().into()
    }

    pub fn hash(&self) -> ScriptHash {
        self.0.hash().into()
    }
}

#[wasm_bindgen]
impl TransactionWitnessSet {
    pub fn add_all_witnesses(&mut self, other: &TransactionWitnessSet) {
        self.0.add_all_witnesses(other.clone().into());
    }

    pub fn languages(&self) -> LanguageList {
        self.0.languages().into()
    }
}