zescrow_core/condition/
ed25519.rs1use bincode::{Decode, Encode};
2use ed25519_dalek::{Signature, Verifier, VerifyingKey};
3#[cfg(feature = "json")]
4use hex::serde as hex_serde;
5#[cfg(feature = "json")]
6use serde::{Deserialize, Serialize};
7
8#[cfg_attr(feature = "json", derive(Serialize, Deserialize))]
28#[derive(Debug, Clone, Encode, Decode, PartialEq, Eq)]
29pub struct Ed25519 {
30 #[cfg_attr(feature = "json", serde(with = "hex_serde"))]
32 pub public_key: [u8; 32],
33
34 #[cfg_attr(feature = "json", serde(with = "hex_serde"))]
36 pub signature: Vec<u8>,
37
38 #[cfg_attr(feature = "json", serde(with = "hex_serde"))]
40 pub message: Vec<u8>,
41}
42
43impl Ed25519 {
44 pub fn verify(&self) -> Result<(), Error> {
46 let pk = VerifyingKey::from_bytes(&self.public_key)
47 .map_err(|e| Error::InvalidPublicKey(e.to_string()))?;
48 let sig = Signature::from_slice(&self.signature).map_err(Error::InvalidSignature)?;
49 pk.verify(&self.message, &sig)
50 .map_err(|_| Error::VerificationFailed)
51 }
52}
53
54#[derive(Debug, thiserror::Error)]
56pub enum Error {
57 #[error("public key decoding error: {0}")]
59 InvalidPublicKey(String),
60
61 #[error("signature decoding error: {0}")]
63 InvalidSignature(#[from] ed25519_dalek::SignatureError),
64
65 #[error("signature verification failed")]
67 VerificationFailed,
68}