midnight_base_crypto/envelope.rs
1// This file is part of midnight-ledger.
2// Copyright (C) Midnight Foundation
3// SPDX-License-Identifier: Apache-2.0
4// Licensed under the Apache License, Version 2.0 (the "License");
5// You may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7// http://www.apache.org/licenses/LICENSE-2.0
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14//! This module deals with cryptographic envelopes, specifically through the `[Envelope]` trait.
15
16use serialize::{Serializable, Tagged, tagged_serialize};
17use std::{
18 io::{self, Write},
19 marker::PhantomData,
20};
21
22pub use derive::Envelope;
23
24/// The envelope trait defines a conversion between a 'base' type into a cryptographic envelope
25/// type. This is typically a 1:1 copy of the base, but often with translation or erasure of certain
26/// fields, specifically to avoid self-reference in cryptographic envelopes (e.g. signing the
27/// signature itself).
28///
29/// Note that `T` is the target (envelope) type, and `Self` is the source type (the data *including*
30/// cryptographic values).
31///
32/// The primary benefit of the `Envelope` trait is it's support for `#[derive(Envelope)]`, which
33/// ensures that the cryptographic envelope does not become detached from the source type by making
34/// any misalignment a compilation error.
35pub trait Envelope<T> {
36 /// Converts this value into its envelope.
37 #[allow(clippy::wrong_self_convention)]
38 fn into_envelope(&self) -> T;
39
40 /// Writes the serialization of the envelope into a bytestring.
41 fn envelope_data(&self) -> Vec<u8>
42 where
43 T: Serializable + Tagged,
44 {
45 let mut output = Vec::new();
46 self.envelope_write(&mut output)
47 .expect("in-memory serialization should succeed");
48 output
49 }
50 /// Writes the serialization of the envelope into a [Write]r.
51 fn envelope_write(&self, writer: impl Write) -> io::Result<()>
52 where
53 T: Serializable + Tagged,
54 {
55 tagged_serialize(&self.into_envelope(), writer)
56 }
57}
58
59impl<T: Clone> Envelope<T> for T {
60 fn into_envelope(&self) -> T {
61 self.clone()
62 }
63}
64
65impl<T> Envelope<PhantomData<T>> for T {
66 fn into_envelope(&self) -> PhantomData<T> {
67 PhantomData
68 }
69}
70
71#[cfg(test)]
72mod tests {
73 use serialize::{Deserializable, Serializable, Tagged};
74 use std::marker::PhantomData;
75
76 use super::Envelope;
77
78 #[derive(Serializable, Clone, PartialEq, Eq, Debug)]
79 #[tag = "bar"]
80 struct Bar(u64);
81 #[derive(Serializable, Clone, PartialEq, Eq, Debug)]
82 #[tag = "baz"]
83 struct Baz(u64);
84
85 #[derive(Envelope)]
86 #[envelope(BazEnvelope)]
87 #[envelope(BarEnvelope)]
88 struct Foo {
89 bar: Bar,
90 baz: Baz,
91 }
92
93 #[derive(Serializable, Clone, PartialEq, Eq, Debug)]
94 #[tag = "bar-envelope"]
95 struct BarEnvelope {
96 bar: Bar,
97 baz: PhantomData<Baz>,
98 }
99
100 #[derive(Serializable, Clone, PartialEq, Eq, Debug)]
101 #[tag = "bar-envelope"]
102 struct BazEnvelope {
103 bar: PhantomData<Bar>,
104 baz: Baz,
105 }
106
107 #[test]
108 fn test_envelope() {
109 let foo = Foo {
110 bar: Bar(42),
111 baz: Baz(64),
112 };
113 assert_eq!(
114 BarEnvelope {
115 bar: Bar(42),
116 baz: PhantomData
117 },
118 foo.into_envelope()
119 );
120 assert_eq!(
121 BazEnvelope {
122 bar: PhantomData,
123 baz: Baz(64)
124 },
125 foo.into_envelope()
126 );
127 }
128}