Skip to main content

trustless_protocol/
base64.rs

1use secrecy::SecretBox;
2
3/// A byte buffer that serializes as base64 on the wire.
4///
5/// Implements [`zeroize::Zeroize`], [`secrecy::SerializableSecret`], and
6/// [`secrecy::CloneableSecret`] so it can be used inside [`SecretBox`] with
7/// full serde and Clone support.
8#[serde_with::serde_as]
9#[derive(Clone, serde::Serialize, serde::Deserialize)]
10#[serde(transparent)]
11pub struct Base64Bytes(
12    #[serde_as(
13        as = "serde_with::base64::Base64<serde_with::base64::Standard, serde_with::formats::Padded>"
14    )]
15    Vec<u8>,
16);
17
18impl zeroize::Zeroize for Base64Bytes {
19    fn zeroize(&mut self) {
20        self.0.zeroize();
21    }
22}
23
24impl secrecy::SerializableSecret for Base64Bytes {}
25impl secrecy::CloneableSecret for Base64Bytes {}
26
27impl std::ops::Deref for Base64Bytes {
28    type Target = Vec<u8>;
29    fn deref(&self) -> &Self::Target {
30        &self.0
31    }
32}
33
34impl From<Vec<u8>> for Base64Bytes {
35    fn from(v: Vec<u8>) -> Self {
36        Self(v)
37    }
38}
39
40impl Base64Bytes {
41    /// Wrap into a `SecretBox`.
42    pub fn into_secret(self) -> SecretBox<Base64Bytes> {
43        Box::new(self).into()
44    }
45}