diff --git a/Cargo.toml b/Cargo.toml index fba1d8d..44fcae3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,8 +25,8 @@ license = "Apache-2.0" [workspace.dependencies] akd = { version = "0.11", default-features = false } -anyhow = "1.0" bincode = "2.0.0-rc.3" +anyhow = "1.0" clap = { version = "4.5", features = ["derive"] } clap-verbosity-flag = "2.2.0" colored = "2.1" diff --git a/plexi_core/Cargo.toml b/plexi_core/Cargo.toml index e41de4b..2461f36 100644 --- a/plexi_core/Cargo.toml +++ b/plexi_core/Cargo.toml @@ -13,15 +13,16 @@ categories.workspace = true build = "src/build.rs" [features] -default = ["openapi"] +default = ["openapi", "bincode"] auditor = ["akd", "akd/parallel_vrf", "akd/parallel_insert", "akd/experimental"] -client = ["auditor", "reqwest"] +client = ["auditor", "reqwest", "bincode"] openapi = ["utoipa"] +bincode = ["dep:bincode"] [dependencies] akd = { workspace = true, features = ["whatsapp_v1", "public_auditing"], optional = true } anyhow = { workspace = true } -bincode = { workspace = true } +bincode = { workspace = true, optional = true } ed25519-dalek = { workspace = true } hex = { workspace = true, features = ["serde"] } prost = { workspace = true } @@ -39,4 +40,4 @@ ed25519-dalek = { workspace = true, features = ["rand_core"] } getrandom = { workspace = true, features = ["js"] } [build-dependencies] -prost-build = { version = "0.13" } \ No newline at end of file +prost-build = { version = "0.13" } diff --git a/plexi_core/src/lib.rs b/plexi_core/src/lib.rs index cdab5ae..df2ab7d 100644 --- a/plexi_core/src/lib.rs +++ b/plexi_core/src/lib.rs @@ -7,6 +7,7 @@ use std::{ }; use anyhow::anyhow; +#[cfg(feature = "bincode")] use bincode::{BorrowDecode, Decode, Encode}; use ed25519_dalek::SIGNATURE_LENGTH; use prost::Message; @@ -24,11 +25,6 @@ pub mod crypto; pub mod namespaces; pub mod proto; -const SIGNATURE_VERSIONS: [SignatureVersion; 2] = [ - SignatureVersion::ProtobufEd25519, - SignatureVersion::BincodeEd25519, -]; - #[derive(Error, Debug)] #[cfg_attr(feature = "openapi", derive(ToSchema))] pub enum PlexiError { @@ -49,6 +45,7 @@ pub enum PlexiError { #[repr(u32)] pub enum SignatureVersion { ProtobufEd25519 = 0x0001, + #[cfg(feature = "bincode")] BincodeEd25519 = 0x0002, Unknown(u32), } @@ -57,6 +54,7 @@ impl From for u32 { fn from(val: SignatureVersion) -> Self { match val { SignatureVersion::ProtobufEd25519 => 0x0001, + #[cfg(feature = "bincode")] SignatureVersion::BincodeEd25519 => 0x0002, SignatureVersion::Unknown(u) => u, } @@ -67,6 +65,7 @@ impl From for SignatureVersion { fn from(u: u32) -> Self { match u { 0x0001 => Self::ProtobufEd25519, + #[cfg(feature = "bincode")] 0x0002 => Self::BincodeEd25519, _ => Self::Unknown(u), } @@ -86,6 +85,7 @@ impl fmt::Display for SignatureVersion { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let s = match self { Self::ProtobufEd25519 => "0x0001", + #[cfg(feature = "bincode")] Self::BincodeEd25519 => "0x0002", Self::Unknown(_u) => "unknown", }; @@ -93,6 +93,7 @@ impl fmt::Display for SignatureVersion { } } +#[cfg(feature = "bincode")] impl Encode for SignatureVersion { fn encode( &self, @@ -103,6 +104,7 @@ impl Encode for SignatureVersion { } } +#[cfg(feature = "bincode")] impl Decode for SignatureVersion { fn decode( decoder: &mut D, @@ -112,6 +114,7 @@ impl Decode for SignatureVersion { } } +#[cfg(feature = "bincode")] impl<'de> BorrowDecode<'de> for SignatureVersion { fn borrow_decode>( buffer: &mut B, @@ -121,7 +124,8 @@ impl<'de> BorrowDecode<'de> for SignatureVersion { } } -#[derive(Clone, Copy, Debug, Serialize, Deserialize, Encode, Decode)] +#[derive(Clone, Copy, Debug, Serialize, Deserialize)] +#[cfg_attr(feature = "bincode", derive(Encode, Decode))] #[cfg_attr(feature = "openapi", derive(ToSchema))] pub struct Epoch(u64); @@ -223,8 +227,9 @@ impl Sub for Epoch { } } -#[derive(Clone, Debug, Serialize, Deserialize, Encode, Decode)] +#[derive(Clone, Debug, Serialize, Deserialize)] #[cfg_attr(feature = "openapi", derive(ToSchema))] +#[cfg_attr(feature = "bincode", derive(Encode, Decode))] pub struct SignatureMessage { version: SignatureVersion, namespace: String, @@ -243,16 +248,16 @@ impl SignatureMessage { epoch: &Epoch, digest: Vec, ) -> Result { - if !SIGNATURE_VERSIONS.contains(version) { - return Err(PlexiError::BadParameter("version".to_string())); + match version { + SignatureVersion::Unknown(_) => Err(PlexiError::BadParameter("version".to_string())), + _ => Ok(Self { + version: *version, + namespace, + timestamp, + epoch: *epoch, + digest, + }), } - Ok(Self { - version: *version, - namespace, - timestamp, - epoch: *epoch, - digest, - }) } pub fn version(&self) -> &SignatureVersion { @@ -275,6 +280,7 @@ impl SignatureMessage { self.digest.clone() } + #[cfg(feature = "bincode")] fn to_vec_bincode(&self) -> Result, PlexiError> { bincode::encode_to_vec(self, bincode::config::legacy()) .map_err(|_e| PlexiError::Serialization) @@ -297,6 +303,7 @@ impl SignatureMessage { pub fn to_vec(&self) -> Result, PlexiError> { match self.version { SignatureVersion::ProtobufEd25519 => self.to_vec_proto(), + #[cfg(feature = "bincode")] SignatureVersion::BincodeEd25519 => self.to_vec_bincode(), _ => Err(PlexiError::Serialization), } @@ -463,6 +470,7 @@ impl SignatureResponse { pub fn verify(&self, verifying_key: &[u8]) -> anyhow::Result<()> { // at the time of writting, all version use ed25519 keys. this simplify parsing of the verifying key match self.version { + #[cfg(feature = "bincode")] SignatureVersion::BincodeEd25519 => (), SignatureVersion::ProtobufEd25519 => (), SignatureVersion::Unknown(_) => { @@ -648,6 +656,7 @@ mod tests { use super::*; #[test] + #[cfg(feature = "bincode")] fn test_vector() { const TEST_VECTORS: &str = std::include_str!("../tests/test-vectors.json");