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
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 BigInt(cml_chain::utils::BigInt);

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

impl_wasm_cbor_json_api!(BigInt);

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

    #[allow(clippy::should_implement_trait)]
    pub fn from_str(s: &str) -> Result<BigInt, JsError> {
        use std::str::FromStr;
        cml_chain::utils::BigInt::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
    }
}