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
use super::{Int, Script, ScriptHash};
use cml_chain::plutus::Language;
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};

use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions, impl_wasm_list};

impl_wasm_list!(Language, Language, LanguageList, true, true);

#[wasm_bindgen]
#[derive(Clone, Debug)]
pub struct BigInteger(cml_chain::utils::BigInteger);

impl_wasm_conversions!(cml_chain::utils::BigInteger, BigInteger);

impl_wasm_cbor_json_api!(BigInteger);

#[wasm_bindgen]
impl BigInteger {
    pub fn from_int(x: &Int) -> Self {
        Self(cml_chain::utils::BigInteger::from_int(x.as_ref()))
    }

    #[allow(clippy::should_implement_trait)]
    pub fn from_str(s: &str) -> Result<BigInteger, JsError> {
        use std::str::FromStr;
        cml_chain::utils::BigInteger::from_str(s)
            .map(Self)
            .map_err(Into::into)
    }

    pub fn to_str(&self) -> String {
        self.0.to_string()
    }

    /// Converts to a u64
    /// Returns None if the number was negative or too big for a u64
    pub fn as_u64(&self) -> Option<u64> {
        self.0.as_u64()
    }

    /// 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> {
        self.0.as_int().map(Into::into)
    }
}

#[wasm_bindgen]
impl Script {
    pub fn hash(&self) -> ScriptHash {
        self.0.hash().into()
    }

    // Returns which language the script is if it's a Plutus script
    // Returns None otherwise (i.e. NativeScript)
    pub fn language(&self) -> Option<Language> {
        self.0.language()
    }
}

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

impl_wasm_cbor_json_api!(NetworkId);

impl_wasm_conversions!(cml_chain::NetworkId, NetworkId);

#[wasm_bindgen]
impl NetworkId {
    pub fn new(network: u64) -> Self {
        cml_chain::NetworkId::new(network).into()
    }

    pub fn mainnet() -> Self {
        cml_chain::NetworkId::mainnet().into()
    }

    pub fn testnet() -> Self {
        cml_chain::NetworkId::testnet().into()
    }

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

// we provide direct From/Into conversions between NonemptySet<T> and TList
// to allow the auto-generated code to work directly without changes
macro_rules! impl_wasm_conversions_into {
    ($rust:ty, $wasm:ty) => {
        impl From<$rust> for $wasm {
            fn from(native: $rust) -> Self {
                Self(native.into())
            }
        }

        #[allow(clippy::from_over_into)]
        impl Into<$rust> for $wasm {
            fn into(self) -> $rust {
                self.0.into()
            }
        }
    };
}

impl_wasm_conversions_into!(cml_chain::NonemptySetCertificate, crate::CertificateList);

impl_wasm_conversions_into!(
    cml_chain::NonemptySetPlutusV1Script,
    crate::PlutusV1ScriptList
);

impl_wasm_conversions_into!(
    cml_chain::NonemptySetPlutusV2Script,
    crate::PlutusV2ScriptList
);

impl_wasm_conversions_into!(
    cml_chain::NonemptySetPlutusV3Script,
    crate::PlutusV3ScriptList
);

impl_wasm_conversions_into!(cml_chain::NonemptySetPlutusData, crate::PlutusDataList);

impl_wasm_conversions_into!(
    cml_chain::NonemptySetBootstrapWitness,
    crate::BootstrapWitnessList
);

impl_wasm_conversions_into!(cml_chain::NonemptySetVkeywitness, crate::VkeywitnessList);

impl_wasm_conversions_into!(
    cml_chain::NonemptySetProposalProcedure,
    crate::ProposalProcedureList
);

impl_wasm_conversions_into!(
    cml_chain::NonemptySetTransactionInput,
    crate::TransactionInputList
);

impl_wasm_conversions_into!(cml_chain::NonemptySetNativeScript, crate::NativeScriptList);

impl_wasm_conversions_into!(
    cml_chain::utils::NonemptySetRawBytes<cml_crypto::Ed25519KeyHash>,
    crate::Ed25519KeyHashList
);

impl_wasm_conversions_into!(
    cml_chain::SetCommitteeColdCredential,
    crate::CommitteeColdCredentialList
);