defuse_core/payload/
raw.rs

1use defuse_crypto::{Curve, Ed25519, Payload, SignedPayload, serde::AsCurve};
2use near_sdk::{env, near, serde::de::DeserializeOwned, serde_json};
3use serde_with::serde_as;
4
5use super::ExtractDefusePayload;
6
7#[cfg_attr(
8    all(feature = "abi", not(target_arch = "wasm32")),
9    serde_as(schemars = true)
10)]
11#[cfg_attr(
12    not(all(feature = "abi", not(target_arch = "wasm32"))),
13    serde_as(schemars = false)
14)]
15#[near(serializers = [borsh, json])]
16#[derive(Debug, Clone)]
17pub struct SignedRawEd25519Payload {
18    pub payload: String,
19
20    #[serde_as(as = "AsCurve<Ed25519>")]
21    pub public_key: <Ed25519 as Curve>::PublicKey,
22    #[serde_as(as = "AsCurve<Ed25519>")]
23    pub signature: <Ed25519 as Curve>::Signature,
24}
25
26impl Payload for SignedRawEd25519Payload {
27    #[inline]
28    fn hash(&self) -> [u8; 32] {
29        env::sha256_array(self.payload.as_bytes())
30    }
31}
32
33impl SignedPayload for SignedRawEd25519Payload {
34    type PublicKey = <Ed25519 as Curve>::PublicKey;
35
36    #[inline]
37    fn verify(&self) -> Option<Self::PublicKey> {
38        Ed25519::verify(&self.signature, self.payload.as_bytes(), &self.public_key)
39    }
40}
41
42impl<T> ExtractDefusePayload<T> for SignedRawEd25519Payload
43where
44    T: DeserializeOwned,
45{
46    type Error = serde_json::Error;
47
48    fn extract_defuse_payload(self) -> Result<super::DefusePayload<T>, Self::Error> {
49        serde_json::from_str(&self.payload)
50    }
51}