From 3775a71005d2f99a8e9dbe9d158ea16c052abab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Thu, 29 Feb 2024 11:47:02 +0100 Subject: [PATCH] Add tests for parsing Casper public keys. Public keys were taken directly from: - https://cspr.live/account/01c377281132044bd3278b039925eeb3efdb9d99dd5f46d9ec6a764add34581af7 - https://cspr.live/account/0202e99759649fa63a72c685b72e696b30c90f1deabb02d0d9b1de45eb371a73e5bb --- kairos-cli/src/crypto/public_key.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/kairos-cli/src/crypto/public_key.rs b/kairos-cli/src/crypto/public_key.rs index f3a10045..07b356d0 100644 --- a/kairos-cli/src/crypto/public_key.rs +++ b/kairos-cli/src/crypto/public_key.rs @@ -22,3 +22,32 @@ impl CasperPublicKey { }) } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_casper_ed25519_public_key() { + // This public key has a 01 prefix indicating Ed25519. + let bytes = hex::decode("01c377281132044bd3278b039925eeb3efdb9d99dd5f46d9ec6a764add34581af7").unwrap(); + let result = CasperPublicKey::from_bytes(&bytes); + assert!(result.is_ok(), "Ed25519 public key should be parsed correctly"); + } + + #[test] + fn test_casper_secp256k1_public_key() { + // This public key has a 02 prefix indicating Secp256k1. + let bytes = hex::decode("0202e99759649fa63a72c685b72e696b30c90f1deabb02d0d9b1de45eb371a73e5bb").unwrap(); + let result = CasperPublicKey::from_bytes(&bytes); + assert!(result.is_ok(), "Secp256k1 public key should be parsed correctly"); + } + + #[test] + fn test_casper_unrecognized_prefix() { + // Using a 99 prefix which is not recognized. + let bytes = hex::decode("99c377281132044bd3278b039925eeb3efdb9d99dd5f46d9ec6a764add34581af7").unwrap(); + let result = CasperPublicKey::from_bytes(&bytes); + assert!(result.is_err(), "Unrecognized prefix should result in an error"); + } +}