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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// This file was code-generated using an experimental CDDL to rust tool:
// https://github.com/dcSpark/cddl-codegen

use super::{
    MapTransactionIndexToAuxiliaryData, TransactionBodyList, TransactionIndex,
    TransactionWitnessSetList,
};
use crate::crypto::{KESSignature, VRFCert, Vkey};
use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions};
use cml_crypto_wasm::{BlockBodyHash, BlockHeaderHash, Ed25519Signature, KESVkey, VRFVkey};
use wasm_bindgen::prelude::{wasm_bindgen, JsError, JsValue};

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

impl_wasm_cbor_json_api!(Block);

impl_wasm_conversions!(cml_chain::block::Block, Block);

#[wasm_bindgen]
impl Block {
    pub fn header(&self) -> Header {
        self.0.header.clone().into()
    }

    pub fn transaction_bodies(&self) -> TransactionBodyList {
        self.0.transaction_bodies.clone().into()
    }

    pub fn transaction_witness_sets(&self) -> TransactionWitnessSetList {
        self.0.transaction_witness_sets.clone().into()
    }

    pub fn auxiliary_data_set(&self) -> MapTransactionIndexToAuxiliaryData {
        self.0.auxiliary_data_set.clone().into()
    }

    pub fn invalid_transactions(&self) -> Vec<TransactionIndex> {
        self.0.invalid_transactions.clone()
    }

    pub fn new(
        header: &Header,
        transaction_bodies: &TransactionBodyList,
        transaction_witness_sets: &TransactionWitnessSetList,
        auxiliary_data_set: &MapTransactionIndexToAuxiliaryData,
        invalid_transactions: Vec<TransactionIndex>,
    ) -> Self {
        Self(cml_chain::block::Block::new(
            header.clone().into(),
            transaction_bodies.clone().into(),
            transaction_witness_sets.clone().into(),
            auxiliary_data_set.clone().into(),
            invalid_transactions,
        ))
    }
}

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

impl_wasm_cbor_json_api!(Header);

impl_wasm_conversions!(cml_chain::block::Header, Header);

#[wasm_bindgen]
impl Header {
    pub fn header_body(&self) -> HeaderBody {
        self.0.header_body.clone().into()
    }

    pub fn body_signature(&self) -> KESSignature {
        self.0.body_signature.clone().into()
    }

    pub fn new(header_body: &HeaderBody, body_signature: &KESSignature) -> Self {
        Self(cml_chain::block::Header::new(
            header_body.clone().into(),
            body_signature.clone().into(),
        ))
    }
}

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

impl_wasm_cbor_json_api!(HeaderBody);

impl_wasm_conversions!(cml_chain::block::HeaderBody, HeaderBody);

#[wasm_bindgen]
impl HeaderBody {
    pub fn block_number(&self) -> u64 {
        self.0.block_number
    }

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

    pub fn prev_hash(&self) -> Option<BlockHeaderHash> {
        self.0.prev_hash.map(std::convert::Into::into)
    }

    pub fn issuer_vkey(&self) -> Vkey {
        self.0.issuer_vkey.clone().into()
    }

    pub fn vrf_vkey(&self) -> VRFVkey {
        self.0.vrf_vkey.into()
    }

    pub fn vrf_result(&self) -> VRFCert {
        self.0.vrf_result.clone().into()
    }

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

    pub fn block_body_hash(&self) -> BlockBodyHash {
        self.0.block_body_hash.into()
    }

    pub fn operational_cert(&self) -> OperationalCert {
        self.0.operational_cert.clone().into()
    }

    pub fn protocol_version(&self) -> ProtocolVersion {
        self.0.protocol_version.clone().into()
    }

    pub fn new(
        block_number: u64,
        slot: u64,
        prev_hash: Option<BlockHeaderHash>,
        issuer_vkey: &Vkey,
        vrf_vkey: &VRFVkey,
        vrf_result: &VRFCert,
        block_body_size: u64,
        block_body_hash: &BlockBodyHash,
        operational_cert: &OperationalCert,
        protocol_version: &ProtocolVersion,
    ) -> Self {
        Self(cml_chain::block::HeaderBody::new(
            block_number,
            slot,
            prev_hash.map(Into::into),
            issuer_vkey.clone().into(),
            vrf_vkey.clone().into(),
            vrf_result.clone().into(),
            block_body_size,
            block_body_hash.clone().into(),
            operational_cert.clone().into(),
            protocol_version.clone().into(),
        ))
    }
}

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

impl_wasm_cbor_json_api!(OperationalCert);

impl_wasm_conversions!(cml_chain::block::OperationalCert, OperationalCert);

#[wasm_bindgen]
impl OperationalCert {
    pub fn hot_vkey(&self) -> KESVkey {
        self.0.hot_vkey.into()
    }

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

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

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

    pub fn new(
        hot_vkey: &KESVkey,
        sequence_number: u64,
        kes_period: u64,
        sigma: &Ed25519Signature,
    ) -> Self {
        Self(cml_chain::block::OperationalCert::new(
            hot_vkey.clone().into(),
            sequence_number,
            kes_period,
            sigma.clone().into(),
        ))
    }
}

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

impl_wasm_cbor_json_api!(ProtocolVersion);

impl_wasm_conversions!(cml_chain::block::ProtocolVersion, ProtocolVersion);

#[wasm_bindgen]
impl ProtocolVersion {
    pub fn major(&self) -> u64 {
        self.0.major
    }

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

    pub fn new(major: u64, minor: u64) -> Self {
        Self(cml_chain::block::ProtocolVersion::new(major, minor))
    }
}