worker_wasm_interactions_rs/
util.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use ed25519_dalek::{PUBLIC_KEY_LENGTH, PublicKey, Signature, SIGNATURE_LENGTH, Verifier};
use twilight_model::channel::message::MessageFlags;
use twilight_model::http::interaction::{InteractionResponse, InteractionResponseData, InteractionResponseType};
use worker::*;

pub trait ToOwnedString {
    fn to_owned_string(self) -> String;
}

impl ToOwnedString for String {
    fn to_owned_string(self) -> String {
        self
    }
}

impl ToOwnedString for &str {
    fn to_owned_string(self) -> String {
        self.to_string()
    }
}

pub fn map_error<T: ToString>(error: T) -> Error {
    Error::RustError(error.to_string())
}

pub fn validate_headers<S: AsRef<str>>(req: &Request, body: &[u8], public_key: S) -> Result<bool> {
    let sig = req.headers().get("x-signature-ed25519")?;
    let timestamp = req.headers().get("x-signature-timestamp")?;
    if sig.is_none() || timestamp.is_none() {
        return Ok(false);
    }
    let sig = sig.unwrap();
    let timestamp = timestamp.unwrap();

    let public_key_hex = hex::decode(public_key.as_ref())
        .map_err(|err| Error::from(err.to_string()))?;
    let signature_hex = hex::decode(sig)
        .map_err(|err| Error::from(err.to_string()))?;
    let public_key = PublicKey::from_bytes(&public_key_hex.as_slice()[..PUBLIC_KEY_LENGTH]).unwrap();
    let signature = Signature::from_bytes(&signature_hex.as_slice()[..SIGNATURE_LENGTH]).unwrap();

    let mut full_body = timestamp.into_bytes();
    full_body.extend_from_slice(body);

    if let Err(err) = public_key.verify(full_body.as_slice(), &signature) {
        console_log!("Received invalid signature: {}", err);
        Ok(false)
    } else {
        Ok(true)
    }
}

pub(crate) fn error_message(message: String) -> InteractionResponse {
    InteractionResponse {
        kind: InteractionResponseType::ChannelMessageWithSource,
        data: Some(InteractionResponseData {
            allowed_mentions: None,
            attachments: None,
            choices: None,
            components: None,
            content: Some(message),
            custom_id: None,
            embeds: None,
            flags: Some(MessageFlags::EPHEMERAL),
            title: None,
            tts: None,
        }),
    }
}