cml_chain_wasm/
utils.rs

1use crate::SubCoin;
2
3use super::{Int, Script, ScriptHash};
4use cml_chain::plutus::Language;
5use wasm_bindgen::prelude::{wasm_bindgen, JsError};
6
7use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions, impl_wasm_list};
8
9impl_wasm_list!(Language, Language, LanguageList, true, true);
10
11#[wasm_bindgen]
12#[derive(Clone, Debug)]
13pub struct BigInteger(cml_chain::utils::BigInteger);
14
15impl_wasm_conversions!(cml_chain::utils::BigInteger, BigInteger);
16
17impl_wasm_cbor_json_api!(BigInteger);
18
19#[wasm_bindgen]
20impl BigInteger {
21    pub fn from_int(x: &Int) -> Self {
22        Self(cml_chain::utils::BigInteger::from_int(x.as_ref()))
23    }
24
25    #[allow(clippy::should_implement_trait)]
26    pub fn from_str(s: &str) -> Result<BigInteger, JsError> {
27        use std::str::FromStr;
28        cml_chain::utils::BigInteger::from_str(s)
29            .map(Self)
30            .map_err(Into::into)
31    }
32
33    pub fn to_str(&self) -> String {
34        self.0.to_string()
35    }
36
37    /// Converts to a u64
38    /// Returns None if the number was negative or too big for a u64
39    pub fn as_u64(&self) -> Option<u64> {
40        self.0.as_u64()
41    }
42
43    /// Converts to an Int
44    /// Returns None when the number is too big for an Int (outside +/- 64-bit unsigned)
45    /// Retains encoding info if the original was encoded as an Int
46    pub fn as_int(&self) -> Option<Int> {
47        self.0.as_int().map(Into::into)
48    }
49}
50
51#[wasm_bindgen]
52impl Script {
53    pub fn hash(&self) -> ScriptHash {
54        self.0.hash().into()
55    }
56
57    // Returns which language the script is if it's a Plutus script
58    // Returns None otherwise (i.e. NativeScript)
59    pub fn language(&self) -> Option<Language> {
60        self.0.language()
61    }
62}
63
64#[derive(Clone, Debug)]
65#[wasm_bindgen]
66pub struct NetworkId(cml_chain::NetworkId);
67
68impl_wasm_cbor_json_api!(NetworkId);
69
70impl_wasm_conversions!(cml_chain::NetworkId, NetworkId);
71
72#[wasm_bindgen]
73impl NetworkId {
74    pub fn new(network: u64) -> Self {
75        cml_chain::NetworkId::new(network).into()
76    }
77
78    pub fn mainnet() -> Self {
79        cml_chain::NetworkId::mainnet().into()
80    }
81
82    pub fn testnet() -> Self {
83        cml_chain::NetworkId::testnet().into()
84    }
85
86    pub fn network(&self) -> u64 {
87        self.0.network
88    }
89}
90
91#[wasm_bindgen]
92impl SubCoin {
93    /// Converts base 10 floats to SubCoin.
94    /// This is the format used by blockfrost for ex units
95    /// Warning: If the passed in float was not meant to be base 10
96    /// this might result in a slightly inaccurate fraction.
97    pub fn from_base10_f32(f: f32) -> Self {
98        cml_chain::SubCoin::from_base10_f32(f).into()
99    }
100}
101
102// we provide direct From/Into conversions between NonemptySet<T> and TList
103// to allow the auto-generated code to work directly without changes
104macro_rules! impl_wasm_conversions_into {
105    ($rust:ty, $wasm:ty) => {
106        impl From<$rust> for $wasm {
107            fn from(native: $rust) -> Self {
108                Self(native.into())
109            }
110        }
111
112        #[allow(clippy::from_over_into)]
113        impl Into<$rust> for $wasm {
114            fn into(self) -> $rust {
115                self.0.into()
116            }
117        }
118    };
119}
120
121impl_wasm_conversions_into!(cml_chain::NonemptySetCertificate, crate::CertificateList);
122
123impl_wasm_conversions_into!(
124    cml_chain::NonemptySetPlutusV1Script,
125    crate::PlutusV1ScriptList
126);
127
128impl_wasm_conversions_into!(
129    cml_chain::NonemptySetPlutusV2Script,
130    crate::PlutusV2ScriptList
131);
132
133impl_wasm_conversions_into!(
134    cml_chain::NonemptySetPlutusV3Script,
135    crate::PlutusV3ScriptList
136);
137
138impl_wasm_conversions_into!(cml_chain::NonemptySetPlutusData, crate::PlutusDataList);
139
140impl_wasm_conversions_into!(
141    cml_chain::NonemptySetBootstrapWitness,
142    crate::BootstrapWitnessList
143);
144
145impl_wasm_conversions_into!(cml_chain::NonemptySetVkeywitness, crate::VkeywitnessList);
146
147impl_wasm_conversions_into!(
148    cml_chain::NonemptySetProposalProcedure,
149    crate::ProposalProcedureList
150);
151
152impl_wasm_conversions_into!(
153    cml_chain::NonemptySetTransactionInput,
154    crate::TransactionInputList
155);
156
157impl_wasm_conversions_into!(cml_chain::NonemptySetNativeScript, crate::NativeScriptList);
158
159impl_wasm_conversions_into!(
160    cml_chain::utils::NonemptySetRawBytes<cml_crypto::Ed25519KeyHash>,
161    crate::Ed25519KeyHashList
162);
163
164impl_wasm_conversions_into!(
165    cml_chain::SetCommitteeColdCredential,
166    crate::CommitteeColdCredentialList
167);