From e7286b86bc611faa961d02b0f9047d6eefb9b3a1 Mon Sep 17 00:00:00 2001 From: Monadic Cat Date: Mon, 24 Jul 2023 22:45:01 -0500 Subject: [PATCH] add extract_interaction() --- twilight-util/Cargo.toml | 3 ++- twilight-util/src/signature_validation.rs | 26 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/twilight-util/Cargo.toml b/twilight-util/Cargo.toml index 8fa39da1417..24a92dc58cb 100644 --- a/twilight-util/Cargo.toml +++ b/twilight-util/Cargo.toml @@ -15,6 +15,7 @@ version = "0.15.2" [dependencies] twilight-model = { default-features = false, optional = true, path = "../twilight-model", version = "0.15.2" } twilight-validate = { default-features = false, optional = true, path = "../twilight-validate", version = "0.15.1" } +serde_json = { default-features = false, optional = true, version = "1.0.96" } # Signature validation ed25519-dalek = { version = "2.0.0-rc.2", optional = true, default-features = false} @@ -30,7 +31,7 @@ builder = ["dep:twilight-model", "dep:twilight-validate"] link = ["dep:twilight-model"] permission-calculator = ["dep:twilight-model"] snowflake = ["dep:twilight-model"] -signature-validation = ["dep:ed25519-dalek", "dep:hex"] +signature-validation = ["dep:ed25519-dalek", "dep:hex", "dep:serde_json"] full = ["builder", "link", "permission-calculator", "snowflake", "signature-validation"] [package.metadata.docs.rs] diff --git a/twilight-util/src/signature_validation.rs b/twilight-util/src/signature_validation.rs index 335125f876e..97c71a0697c 100644 --- a/twilight-util/src/signature_validation.rs +++ b/twilight-util/src/signature_validation.rs @@ -2,6 +2,7 @@ //! an HTTPS endpoint to send Interactions to. use ed25519_dalek::{Signature, SignatureError, VerifyingKey}; +use twilight_model::application::interaction::Interaction; #[derive(Debug)] pub struct FromHexError(hex::FromHexError); @@ -66,3 +67,28 @@ pub fn check_signature( Err(e) => Err(SignatureValidationFailure::InvalidSignature(SigError(e))), } } + +pub enum ExtractFailure { + Signature(SignatureValidationFailure), + Deserialize(serde_json::Error), +} +impl From for ExtractFailure { + fn from(value: SignatureValidationFailure) -> Self { + Self::Signature(value) + } +} +impl From for ExtractFailure { + fn from(value: serde_json::Error) -> Self { + Self::Deserialize(value) + } +} + +pub fn extract_interaction( + sig: &[u8], + timestamp: &[u8], + body: &[u8], + key: &Key, +) -> Result { + check_signature(sig, timestamp, body, key)?; + Ok(serde_json::from_slice(body)?) +}