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
//! Models for data stored in the database

pub mod account;
pub mod secrets;

use std::fmt::Display;

use serde::{Deserialize, Serialize};

use crate::LockKeeperError;

/// Convenience type for serializing byte arrays as hex strings.
/// Add the `#[serde(try_from = "HexBytes", into = "HexBytes")]` attribute macro
/// above any type you'd like to serialize this way. This type should only be
/// used to serialize byte collections. It should not be used directly.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub(crate) struct HexBytes(String);

impl Display for HexBytes {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl<T: AsRef<[u8]>> From<T> for HexBytes {
    fn from(bytes: T) -> Self {
        Self(hex::encode(bytes))
    }
}

impl TryFrom<HexBytes> for Vec<u8> {
    type Error = LockKeeperError;

    fn try_from(bytes: HexBytes) -> Result<Self, Self::Error> {
        Ok(hex::decode(bytes.0)?)
    }
}

impl<const N: usize> TryFrom<HexBytes> for [u8; N] {
    type Error = LockKeeperError;

    fn try_from(bytes: HexBytes) -> Result<Self, Self::Error> {
        let byte_array = hex::decode(bytes.0)?
            .try_into()
            // We know that we have a sequence of bytes so the only possible error is that it's the
            // wrong length
            .map_err(|_| LockKeeperError::InvalidKeyIdLength)?;
        Ok(byte_array)
    }
}