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
//! `types` is a module that provides all base types used at `eddsa` module
use rst_common::standard::bytes::Bytes;
use rst_common::with_cryptography::ed25519_dalek::{
    PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH, SIGNATURE_LENGTH,
};
use rst_common::with_cryptography::hex;

pub mod errors {
    use rst_common::with_errors::thiserror::{self, Error};

    pub use crate::errors::CommonError;
    use crate::keysecure::types::errors::KeySecureError;
    use crate::passphrase::types::errors::PassphraseError;

    /// `EddsaError` used specifically when manage public and private keys through
    /// `Eddsa` algorithm
    #[derive(Debug, Error, PartialEq)]
    pub enum EddsaError {
        #[error("eddsa: unable to parse signature: `{0}`")]
        ParseSignatureError(String),

        #[error("eddsa: unable to encode pem: `{0}`")]
        EncodePemError(String),

        #[error("eddsa: unable to decode pem: `{0}`")]
        DecodePemError(String),

        #[error("eddsa: invalid given signature: `{0}`")]
        InvalidSignatureError(String),

        #[error("eddsa: invalid given public key: `{0}`")]
        InvalidPubKeyError(String),

        #[error("eddsa: unable to parse public key: `{0}`")]
        ParsePublicKeyError(String),

        #[error("eddsa: common error")]
        Common(#[from] CommonError),

        #[error("eddsa: keysecure error")]
        KeySecure(#[from] KeySecureError),

        #[error("eddsa: passphrase error")]
        Passphrase(#[from] PassphraseError),
    }
}

use crate::types::{ByteHex, BytesValue, Hexer, Value};
use errors::{CommonError, EddsaError};

#[derive(PartialEq, Debug, Clone)]
pub struct PublicKeyBytes([u8; PUBLIC_KEY_LENGTH]);

impl Value<[u8; PUBLIC_KEY_LENGTH]> for PublicKeyBytes {
    fn get(&self) -> Result<[u8; PUBLIC_KEY_LENGTH], CommonError> {
        let byte_slice = self.bytes().slice(0..PUBLIC_KEY_LENGTH);
        let byte_output = &byte_slice[..];

        let output: Result<[u8; PUBLIC_KEY_LENGTH], CommonError> =
            <&[u8; PUBLIC_KEY_LENGTH]>::try_from(byte_output)
                .map(|val| val.to_owned())
                .map_err(|_| CommonError::ParseValueError("unable to parse bytes".to_string()));

        output
    }
}

impl TryFrom<ByteHex> for PublicKeyBytes {
    type Error = EddsaError;

    fn try_from(value: ByteHex) -> Result<Self, Self::Error> {
        let result = hex::decode(value.hex())
            .map_err(|err| EddsaError::Common(CommonError::ParseHexError(err.to_string())))?;

        let peer_pub_bytes: [u8; PUBLIC_KEY_LENGTH] = match result.try_into() {
            Ok(value) => value,
            Err(_) => {
                return Err(EddsaError::ParsePublicKeyError(
                    "unable to parse given public key".to_string(),
                ))
            }
        };

        Ok(PublicKeyBytes(peer_pub_bytes))
    }
}

impl BytesValue for PublicKeyBytes {
    fn bytes(&self) -> Bytes {
        Bytes::from(self.0.to_vec())
    }
}

impl From<[u8; PUBLIC_KEY_LENGTH]> for PublicKeyBytes {
    fn from(value: [u8; PUBLIC_KEY_LENGTH]) -> Self {
        PublicKeyBytes(value)
    }
}

#[derive(PartialEq, Debug, Clone)]
pub struct PrivateKeyBytes([u8; SECRET_KEY_LENGTH]);

impl Value<[u8; PUBLIC_KEY_LENGTH]> for PrivateKeyBytes {
    fn get(&self) -> Result<[u8; SECRET_KEY_LENGTH], CommonError> {
        let byte_slice = self.bytes().slice(0..SECRET_KEY_LENGTH);
        let byte_output = &byte_slice[..];

        let output: Result<[u8; SECRET_KEY_LENGTH], CommonError> =
            <&[u8; SECRET_KEY_LENGTH]>::try_from(byte_output)
                .map(|val| val.to_owned())
                .map_err(|_| CommonError::ParseValueError("unable to parse bytes".to_string()));

        output
    }
}

impl BytesValue for PrivateKeyBytes {
    fn bytes(&self) -> Bytes {
        Bytes::from(self.0.to_vec())
    }
}

impl From<[u8; SECRET_KEY_LENGTH]> for PrivateKeyBytes {
    fn from(value: [u8; SECRET_KEY_LENGTH]) -> Self {
        PrivateKeyBytes(value)
    }
}

#[derive(PartialEq, Debug, Clone)]
pub struct SignatureBytes([u8; SIGNATURE_LENGTH]);

impl Value<[u8; SIGNATURE_LENGTH]> for SignatureBytes {
    fn get(&self) -> Result<[u8; SIGNATURE_LENGTH], CommonError> {
        let byte_slice = self.bytes().slice(0..SIGNATURE_LENGTH);
        let byte_output = &byte_slice[..];

        let output: Result<[u8; SIGNATURE_LENGTH], CommonError> =
            <&[u8; SIGNATURE_LENGTH]>::try_from(byte_output)
                .map(|val| val.to_owned())
                .map_err(|_| CommonError::ParseValueError("unable to parse bytes".to_string()));

        output
    }
}

impl TryFrom<ByteHex> for SignatureBytes {
    type Error = EddsaError;

    fn try_from(value: ByteHex) -> Result<Self, Self::Error> {
        let result = hex::decode(value.hex())
            .map_err(|err| EddsaError::Common(CommonError::ParseHexError(err.to_string())))?;

        let peer_sig_bytes: [u8; SIGNATURE_LENGTH] = match result.try_into() {
            Ok(value) => value,
            Err(_) => {
                return Err(EddsaError::ParsePublicKeyError(
                    "unable to parse given public key".to_string(),
                ))
            }
        };

        Ok(SignatureBytes(peer_sig_bytes))
    }
}

impl BytesValue for SignatureBytes {
    fn bytes(&self) -> Bytes {
        Bytes::from(self.0.to_vec())
    }
}

impl From<[u8; SIGNATURE_LENGTH]> for SignatureBytes {
    fn from(value: [u8; SIGNATURE_LENGTH]) -> Self {
        SignatureBytes(value)
    }
}