1use crate::byron::AddrAttributes;
2pub use cml_crypto::{
3 AnchorDocHash, AuxiliaryDataHash, BlockBodyHash, BlockHeaderHash, DatumHash, Ed25519KeyHash,
4 Ed25519Signature, GenesisDelegateHash, GenesisHash, KESVkey, NonceHash, PoolMetadataHash,
5 ScriptDataHash, ScriptHash, TransactionHash, VRFKeyHash, VRFVkey,
6};
7
8pub type Vkey = cml_crypto::PublicKey;
9
10pub mod hash;
11pub mod utils;
12pub mod cbor_encodings;
16pub mod serialization;
17
18use cbor_encodings::{
19 BootstrapWitnessEncoding, KESSignatureEncoding, VRFCertEncoding, VkeywitnessEncoding,
20};
21use cml_core::error::*;
22use cml_core::serialization::{LenEncoding, StringEncoding};
23use std::convert::TryFrom;
24
25#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
26pub struct BootstrapWitness {
27 pub public_key: Vkey,
28 pub signature: Ed25519Signature,
29 pub chain_code: Vec<u8>,
30 pub attributes: AddrAttributes,
31 #[serde(skip)]
32 pub encodings: Option<BootstrapWitnessEncoding>,
33}
34
35impl BootstrapWitness {
36 pub fn new(
37 public_key: Vkey,
38 signature: Ed25519Signature,
39 chain_code: Vec<u8>,
40 attributes: AddrAttributes,
41 ) -> Result<Self, DeserializeError> {
42 if chain_code.len() < 32 || chain_code.len() > 32 {
43 return Err(DeserializeFailure::RangeCheck {
44 found: chain_code.len() as isize,
45 min: Some(32),
46 max: Some(32),
47 }
48 .into());
49 }
50 Ok(Self {
51 public_key,
52 signature,
53 chain_code,
54 attributes,
55 encodings: None,
56 })
57 }
58}
59
60#[derive(Clone, Debug)]
61pub struct KESSignature {
62 pub inner: Vec<u8>,
63 pub encodings: Option<KESSignatureEncoding>,
64}
65
66impl KESSignature {
67 pub fn get(&self) -> &Vec<u8> {
68 &self.inner
69 }
70
71 pub fn new(inner: Vec<u8>) -> Result<Self, DeserializeError> {
72 if inner.len() != 448 {
73 return Err(DeserializeError::new(
74 "KESSignature",
75 DeserializeFailure::RangeCheck {
76 found: inner.len() as isize,
77 min: Some(448),
78 max: Some(448),
79 },
80 ));
81 }
82 Ok(Self {
83 inner,
84 encodings: None,
85 })
86 }
87}
88
89impl TryFrom<Vec<u8>> for KESSignature {
90 type Error = DeserializeError;
91
92 fn try_from(inner: Vec<u8>) -> Result<Self, Self::Error> {
93 KESSignature::new(inner)
94 }
95}
96
97impl From<KESSignature> for Vec<u8> {
98 fn from(wrapper: KESSignature) -> Self {
99 wrapper.inner
100 }
101}
102
103impl serde::Serialize for KESSignature {
104 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
105 where
106 S: serde::Serializer,
107 {
108 serializer.serialize_str(&hex::encode(self.inner.clone()))
109 }
110}
111
112impl<'de> serde::de::Deserialize<'de> for KESSignature {
113 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
114 where
115 D: serde::de::Deserializer<'de>,
116 {
117 let s = <String as serde::de::Deserialize>::deserialize(deserializer)?;
118 hex::decode(&s)
119 .ok()
120 .and_then(|bytes| KESSignature::new(bytes).ok())
121 .ok_or_else(|| {
122 serde::de::Error::invalid_value(
123 serde::de::Unexpected::Str(&s),
124 &"invalid hex bytes",
125 )
126 })
127 }
128}
129
130impl schemars::JsonSchema for KESSignature {
131 fn schema_name() -> String {
132 String::from("KESSignature")
133 }
134
135 fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
136 String::json_schema(gen)
137 }
138
139 fn is_referenceable() -> bool {
140 String::is_referenceable()
141 }
142}
143
144#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
145pub enum Nonce {
146 Identity {
147 #[serde(skip)]
148 identity_encoding: Option<cbor_event::Sz>,
149 #[serde(skip)]
150 len_encoding: LenEncoding,
151 },
152 Hash {
153 hash: NonceHash,
154 #[serde(skip)]
155 len_encoding: LenEncoding,
156 #[serde(skip)]
157 tag_encoding: Option<cbor_event::Sz>,
158 #[serde(skip)]
159 hash_encoding: StringEncoding,
160 },
161}
162
163impl Nonce {
164 pub fn new_identity() -> Self {
165 Self::Identity {
166 identity_encoding: None,
167 len_encoding: LenEncoding::default(),
168 }
169 }
170
171 pub fn new_hash(hash: NonceHash) -> Self {
172 Self::Hash {
173 hash,
174 len_encoding: LenEncoding::default(),
175 tag_encoding: None,
176 hash_encoding: StringEncoding::default(),
177 }
178 }
179}
180
181#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
182pub struct VRFCert {
183 pub output: Vec<u8>,
184 pub proof: Vec<u8>,
185 #[serde(skip)]
186 pub encodings: Option<VRFCertEncoding>,
187}
188
189impl VRFCert {
190 pub fn new(output: Vec<u8>, proof: Vec<u8>) -> Result<Self, DeserializeError> {
191 if proof.len() < 80 || proof.len() > 80 {
192 return Err(DeserializeFailure::RangeCheck {
193 found: proof.len() as isize,
194 min: Some(80),
195 max: Some(80),
196 }
197 .into());
198 }
199 Ok(Self {
200 output,
201 proof,
202 encodings: None,
203 })
204 }
205}
206
207#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
208pub struct Vkeywitness {
209 pub vkey: Vkey,
210 pub ed25519_signature: Ed25519Signature,
211 #[serde(skip)]
212 pub encodings: Option<VkeywitnessEncoding>,
213}
214
215impl Vkeywitness {
216 pub fn new(vkey: Vkey, ed25519_signature: Ed25519Signature) -> Self {
217 Self {
218 vkey,
219 ed25519_signature,
220 encodings: None,
221 }
222 }
223}