cml_chain_wasm/transaction/
utils.rs

1use crate::{
2    address::Address,
3    transaction::{DatumOption, ScriptRef, TransactionOutput},
4    utils::LanguageList,
5    Ed25519KeyHashList, NativeScript, Value,
6};
7use cml_core::Slot;
8use cml_crypto_wasm::{DatumHash, ScriptHash};
9use wasm_bindgen::prelude::wasm_bindgen;
10
11use super::TransactionWitnessSet;
12
13#[wasm_bindgen]
14impl TransactionOutput {
15    pub fn new(
16        address: &Address,
17        amount: &Value,
18        datum_option: Option<DatumOption>,
19        script_reference: Option<ScriptRef>,
20    ) -> Self {
21        cml_chain::transaction::TransactionOutput::new(
22            address.clone().into(),
23            amount.clone().into(),
24            datum_option.map(Into::into),
25            script_reference.map(Into::into),
26        )
27        .into()
28    }
29
30    pub fn address(&self) -> Address {
31        self.0.address().clone().into()
32    }
33
34    pub fn set_address(&mut self, addr: &Address) {
35        self.0.set_address(addr.clone().into())
36    }
37
38    pub fn amount(&self) -> Value {
39        self.0.amount().clone().into()
40    }
41
42    pub fn set_amount(&mut self, amount: &Value) {
43        self.0.set_amount(amount.clone().into())
44    }
45
46    pub fn datum(&self) -> Option<DatumOption> {
47        self.0.datum().map(Into::into)
48    }
49
50    /// Get the datum hash from a tx output if present as a hash.
51    /// Returns None if there is no datum, or the datum is inlined.
52    /// Use TransactionOutput::datum() for inlined datums.
53    pub fn datum_hash(&self) -> Option<DatumHash> {
54        self.0.datum_hash().cloned().map(Into::into)
55    }
56
57    pub fn script_ref(&self) -> Option<ScriptRef> {
58        self.0.script_ref().cloned().map(Into::into)
59    }
60}
61
62// TODO: anything here? pub type RequiredSignersSet = BTreeSet<Ed25519KeyHash>;
63
64#[wasm_bindgen]
65impl NativeScript {
66    /// Returns an array of unique Ed25519KeyHashes
67    /// contained within this script recursively on any depth level.
68    /// The order of the keys in the result is not determined in any way.
69    pub fn get_required_signers(&self) -> Ed25519KeyHashList {
70        self.as_ref().get_required_signers().into()
71    }
72
73    pub fn hash(&self) -> ScriptHash {
74        self.0.hash().into()
75    }
76
77    pub fn verify(
78        &self,
79        lower_bound: Option<Slot>,
80        upper_bound: Option<Slot>,
81        key_hashes: &Ed25519KeyHashList,
82    ) -> bool {
83        self.0.verify(lower_bound, upper_bound, key_hashes.as_ref())
84    }
85}
86
87#[wasm_bindgen]
88impl TransactionWitnessSet {
89    pub fn add_all_witnesses(&mut self, other: &TransactionWitnessSet) {
90        self.0.add_all_witnesses(other.clone().into());
91    }
92
93    pub fn languages(&self) -> LanguageList {
94        self.0.languages().into()
95    }
96}