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
155
156
157
158
159
160
161
162
163
164
165
166
167
// This file was code-generated using an experimental CDDL to rust tool:
// https://github.com/dcSpark/cddl-codegen

pub use cml_crypto_wasm::{
    AnchorDocHash, AuxiliaryDataHash, BlockBodyHash, BlockHeaderHash, DatumHash, Ed25519KeyHash,
    Ed25519Signature, GenesisDelegateHash, GenesisHash, KESVkey, NonceHash, PoolMetadataHash,
    ScriptDataHash, ScriptHash, TransactionHash, VRFKeyHash, VRFVkey,
};

pub mod hash;
pub mod utils;

use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};

use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions};

use crate::byron::AddrAttributes;

pub type Vkey = cml_crypto_wasm::PublicKey;

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

impl_wasm_cbor_json_api!(BootstrapWitness);

impl_wasm_conversions!(cml_chain::crypto::BootstrapWitness, BootstrapWitness);

#[wasm_bindgen]
impl BootstrapWitness {
    pub fn public_key(&self) -> Vkey {
        self.0.public_key.clone().into()
    }

    pub fn signature(&self) -> Ed25519Signature {
        self.0.signature.clone().into()
    }

    pub fn chain_code(&self) -> Vec<u8> {
        self.0.chain_code.clone()
    }

    pub fn attributes(&self) -> AddrAttributes {
        self.0.attributes.clone().into()
    }

    pub fn new(
        public_key: &Vkey,
        signature: &Ed25519Signature,
        chain_code: Vec<u8>,
        attributes: &AddrAttributes,
    ) -> Result<BootstrapWitness, JsError> {
        cml_chain::crypto::BootstrapWitness::new(
            public_key.clone().into(),
            signature.clone().into(),
            chain_code,
            attributes.clone().into(),
        )
        .map(Into::into)
        .map_err(Into::into)
    }
}

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

impl_wasm_cbor_json_api!(KESSignature);

impl_wasm_conversions!(cml_chain::crypto::KESSignature, KESSignature);

#[wasm_bindgen]
impl KESSignature {
    pub fn get(&self) -> Vec<u8> {
        self.0.get().clone()
    }
}

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

impl_wasm_cbor_json_api!(Nonce);

impl_wasm_conversions!(cml_chain::crypto::Nonce, Nonce);

#[wasm_bindgen]
impl Nonce {
    pub fn new_identity() -> Self {
        Self(cml_chain::crypto::Nonce::new_identity())
    }

    pub fn new_hash(hash: &NonceHash) -> Self {
        Self(cml_chain::crypto::Nonce::new_hash(hash.clone().into()))
    }

    pub fn kind(&self) -> NonceKind {
        match &self.0 {
            cml_chain::crypto::Nonce::Identity { .. } => NonceKind::Identity,
            cml_chain::crypto::Nonce::Hash { .. } => NonceKind::Hash,
        }
    }

    pub fn as_hash(&self) -> Option<NonceHash> {
        match &self.0 {
            cml_chain::crypto::Nonce::Hash { hash, .. } => Some((*hash).into()),
            _ => None,
        }
    }
}

#[wasm_bindgen]
pub enum NonceKind {
    Identity,
    Hash,
}

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

impl_wasm_cbor_json_api!(VRFCert);

impl_wasm_conversions!(cml_chain::crypto::VRFCert, VRFCert);

#[wasm_bindgen]
impl VRFCert {
    pub fn output(&self) -> Vec<u8> {
        self.0.output.clone()
    }

    pub fn proof(&self) -> Vec<u8> {
        self.0.proof.clone()
    }

    pub fn new(output: Vec<u8>, proof: Vec<u8>) -> Result<VRFCert, JsError> {
        cml_chain::crypto::VRFCert::new(output, proof)
            .map(Into::into)
            .map_err(Into::into)
    }
}

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

impl_wasm_cbor_json_api!(Vkeywitness);

impl_wasm_conversions!(cml_chain::crypto::Vkeywitness, Vkeywitness);

#[wasm_bindgen]
impl Vkeywitness {
    pub fn vkey(&self) -> Vkey {
        self.0.vkey.clone().into()
    }

    pub fn ed25519_signature(&self) -> Ed25519Signature {
        self.0.ed25519_signature.clone().into()
    }

    pub fn new(vkey: &Vkey, ed25519_signature: &Ed25519Signature) -> Self {
        Self(cml_chain::crypto::Vkeywitness::new(
            vkey.clone().into(),
            ed25519_signature.clone().into(),
        ))
    }
}