prople_crypto/eddsa/
pubkey.rs1use rst_common::with_cryptography::ed25519_dalek::{
4 Signature as EdDSASignature, Verifier, VerifyingKey,
5};
6
7use crate::eddsa::types::errors::*;
8use crate::eddsa::types::{PublicKeyBytes, SignatureBytes};
9use crate::types::{ByteHex, Value};
10
11#[derive(Debug, Clone, PartialEq)]
15pub struct PubKey {
16 key: VerifyingKey,
17}
18
19impl PubKey {
20 pub fn new(key: VerifyingKey) -> Self {
21 Self { key }
22 }
23
24 pub fn serialize(&self) -> PublicKeyBytes {
25 PublicKeyBytes::from(self.key.to_bytes())
26 }
27
28 pub fn to_hex(&self) -> ByteHex {
29 ByteHex::from(self.key)
30 }
31
32 pub fn from_hex(val: ByteHex) -> Result<Self, EddsaError> {
33 let pub_bytes = PublicKeyBytes::try_from(val)?;
34 let pub_bytes_val = pub_bytes
35 .get()
36 .map_err(|err| EddsaError::ParsePublicKeyError(err.to_string()))?;
37
38 VerifyingKey::from_bytes(&pub_bytes_val)
39 .map(|val| Self { key: val })
40 .map_err(|err| EddsaError::InvalidPubKeyError(err.to_string()))
41 }
42
43 pub fn verify(&self, message: &[u8], signature_hex: ByteHex) -> Result<bool, EddsaError> {
44 let signature_bytes = SignatureBytes::try_from(signature_hex)?;
45 let signature_bytes_val = signature_bytes
46 .get()
47 .map_err(|err| EddsaError::ParseSignatureError(err.to_string()))?;
48
49 let signature = EdDSASignature::from_bytes(&signature_bytes_val);
50 self.key
51 .verify(message, &signature)
52 .map(|_| true)
53 .map_err(|err| EddsaError::InvalidSignatureError(err.to_string()))
54 }
55}