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
use ed25519_dalek::{PublicKey, Verifier};
use hex::{encode, ToHex};
use crate::error::CryptoError;
#[derive(Debug)]
pub struct Signature(pub ed25519_dalek::Signature);
impl Signature {
#[tracing::instrument(level = "debug", ret, skip(self))]
pub fn encode_signature(self) -> String {
self.0.encode_hex::<String>()
}
#[tracing::instrument(level = "debug", ret, skip(self))]
pub fn verify(&self, msg: &[u8], public_key: &[u8; 32]) -> Result<(), CryptoError> {
PublicKey::from_bytes(public_key)
.map_err(|e| CryptoError::MessageVerificationError(e.to_string()))?
.verify(msg, &self.0)
.map_err(|_| CryptoError::MessageVerificationError(encode(msg)))
}
}
#[cfg(test)]
mod tests {
use crate::common::test_utilities::{
generate_message, get_expected_message, SIGNING_PUBLIC_KEY, SIGNING_SECRET_KEY,
};
use crate::identity::SigningKeypair;
#[test]
fn should_sign_custom_struct() {
let keypair = SigningKeypair::try_from_str(SIGNING_PUBLIC_KEY, SIGNING_SECRET_KEY).unwrap();
let message_to_sign = generate_message();
let expected_message = get_expected_message();
let signature = keypair.sign(&message_to_sign);
signature
.verify(&expected_message, &keypair.public())
.expect("OK");
}
}