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
use crate::byron::AddrAttributes;
pub use cml_crypto::{
    AnchorDocHash, AuxiliaryDataHash, BlockBodyHash, BlockHeaderHash, DatumHash, Ed25519KeyHash,
    Ed25519Signature, GenesisDelegateHash, GenesisHash, KESVkey, NonceHash, PoolMetadataHash,
    ScriptDataHash, ScriptHash, TransactionHash, VRFKeyHash, VRFVkey,
};

pub type Vkey = cml_crypto::PublicKey;

pub mod hash;
pub mod utils;
// This file was code-generated using an experimental CDDL to rust tool:
// https://github.com/dcSpark/cddl-codegen

pub mod cbor_encodings;
pub mod serialization;

use cbor_encodings::{
    BootstrapWitnessEncoding, KESSignatureEncoding, VRFCertEncoding, VkeywitnessEncoding,
};
use cml_core::error::*;
use cml_core::serialization::{LenEncoding, StringEncoding};
use std::convert::TryFrom;

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub struct BootstrapWitness {
    pub public_key: Vkey,
    pub signature: Ed25519Signature,
    pub chain_code: Vec<u8>,
    pub attributes: AddrAttributes,
    #[serde(skip)]
    pub encodings: Option<BootstrapWitnessEncoding>,
}

impl BootstrapWitness {
    pub fn new(
        public_key: Vkey,
        signature: Ed25519Signature,
        chain_code: Vec<u8>,
        attributes: AddrAttributes,
    ) -> Result<Self, DeserializeError> {
        if chain_code.len() < 32 || chain_code.len() > 32 {
            return Err(DeserializeFailure::RangeCheck {
                found: chain_code.len() as isize,
                min: Some(32),
                max: Some(32),
            }
            .into());
        }
        Ok(Self {
            public_key,
            signature,
            chain_code,
            attributes,
            encodings: None,
        })
    }
}

#[derive(Clone, Debug)]
pub struct KESSignature {
    pub inner: Vec<u8>,
    pub encodings: Option<KESSignatureEncoding>,
}

impl KESSignature {
    pub fn get(&self) -> &Vec<u8> {
        &self.inner
    }

    pub fn new(inner: Vec<u8>) -> Result<Self, DeserializeError> {
        if inner.len() != 448 {
            return Err(DeserializeError::new(
                "KESSignature",
                DeserializeFailure::RangeCheck {
                    found: inner.len() as isize,
                    min: Some(448),
                    max: Some(448),
                },
            ));
        }
        Ok(Self {
            inner,
            encodings: None,
        })
    }
}

impl TryFrom<Vec<u8>> for KESSignature {
    type Error = DeserializeError;

    fn try_from(inner: Vec<u8>) -> Result<Self, Self::Error> {
        KESSignature::new(inner)
    }
}

impl From<KESSignature> for Vec<u8> {
    fn from(wrapper: KESSignature) -> Self {
        wrapper.inner
    }
}

impl serde::Serialize for KESSignature {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&hex::encode(self.inner.clone()))
    }
}

impl<'de> serde::de::Deserialize<'de> for KESSignature {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::de::Deserializer<'de>,
    {
        let s = <String as serde::de::Deserialize>::deserialize(deserializer)?;
        hex::decode(&s)
            .ok()
            .and_then(|bytes| KESSignature::new(bytes).ok())
            .ok_or_else(|| {
                serde::de::Error::invalid_value(
                    serde::de::Unexpected::Str(&s),
                    &"invalid hex bytes",
                )
            })
    }
}

impl schemars::JsonSchema for KESSignature {
    fn schema_name() -> String {
        String::from("KESSignature")
    }

    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
        String::json_schema(gen)
    }

    fn is_referenceable() -> bool {
        String::is_referenceable()
    }
}

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub enum Nonce {
    Identity {
        #[serde(skip)]
        identity_encoding: Option<cbor_event::Sz>,
        #[serde(skip)]
        len_encoding: LenEncoding,
    },
    Hash {
        hash: NonceHash,
        #[serde(skip)]
        len_encoding: LenEncoding,
        #[serde(skip)]
        tag_encoding: Option<cbor_event::Sz>,
        #[serde(skip)]
        hash_encoding: StringEncoding,
    },
}

impl Nonce {
    pub fn new_identity() -> Self {
        Self::Identity {
            identity_encoding: None,
            len_encoding: LenEncoding::default(),
        }
    }

    pub fn new_hash(hash: NonceHash) -> Self {
        Self::Hash {
            hash,
            len_encoding: LenEncoding::default(),
            tag_encoding: None,
            hash_encoding: StringEncoding::default(),
        }
    }
}

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub struct VRFCert {
    pub output: Vec<u8>,
    pub proof: Vec<u8>,
    #[serde(skip)]
    pub encodings: Option<VRFCertEncoding>,
}

impl VRFCert {
    pub fn new(output: Vec<u8>, proof: Vec<u8>) -> Result<Self, DeserializeError> {
        if proof.len() < 80 || proof.len() > 80 {
            return Err(DeserializeFailure::RangeCheck {
                found: proof.len() as isize,
                min: Some(80),
                max: Some(80),
            }
            .into());
        }
        Ok(Self {
            output,
            proof,
            encodings: None,
        })
    }
}

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub struct Vkeywitness {
    pub vkey: Vkey,
    pub ed25519_signature: Ed25519Signature,
    #[serde(skip)]
    pub encodings: Option<VkeywitnessEncoding>,
}

impl Vkeywitness {
    pub fn new(vkey: Vkey, ed25519_signature: Ed25519Signature) -> Self {
        Self {
            vkey,
            ed25519_signature,
            encodings: None,
        }
    }
}