diff --git a/core/src/identity/ed25519.rs b/core/src/identity/ed25519.rs index 89ed0b26..1c6662c2 100644 --- a/core/src/identity/ed25519.rs +++ b/core/src/identity/ed25519.rs @@ -100,7 +100,7 @@ pub struct PublicKey(ed25519::PublicKey); impl PublicKey { /// Verify the Ed25519 signature on a message using the public key. pub fn verify(&self, msg: &[u8], sig: &[u8]) -> bool { - ed25519::Signature::from_bytes(sig).map(|s| self.0.verify(msg, &s)).is_ok() + ed25519::Signature::from_bytes(sig).and_then(|s| self.0.verify(msg, &s)).is_ok() } /// Encode the public key into a byte array in compressed form, i.e. @@ -189,5 +189,21 @@ mod tests { } QuickCheck::new().tests(10).quickcheck(prop as fn() -> _); } -} + #[test] + fn ed25519_signature() { + let kp = Keypair::generate(); + let pk = kp.public(); + + let msg = "hello world".as_bytes(); + let sig = kp.sign(msg); + assert!(pk.verify(msg, &sig)); + + let mut invalid_sig = sig.clone(); + invalid_sig[3..6].copy_from_slice(&[10, 23, 42]); + assert!(!pk.verify(msg, &invalid_sig)); + + let invalid_msg = "h3ll0 w0rld".as_bytes(); + assert!(!pk.verify(invalid_msg, &sig)); + } +}