use defuse_crypto::{Curve, Ed25519, Payload, SignedPayload, serde::AsCurve}; use near_sdk::{env, near, serde::de::DeserializeOwned, serde_json}; use serde_with::serde_as; use super::ExtractDefusePayload; #[near(serializers = [borsh, json])] #[derive(Debug, Clone)] pub struct SignedRawEd25519Payload { pub payload: String, #[serde_as(as = "AsCurve")] pub public_key: ::PublicKey, #[serde_as(as = "AsCurve")] pub signature: ::Signature, } impl Payload for SignedRawEd25519Payload { #[inline] fn hash(&self) -> [u8; 32] { env::sha256_array(self.payload.as_bytes()) } } impl SignedPayload for SignedRawEd25519Payload { type PublicKey = ::PublicKey; #[inline] fn verify(&self) -> Option { Ed25519::verify(&self.signature, self.payload.as_bytes(), &self.public_key) } } impl ExtractDefusePayload for SignedRawEd25519Payload where T: DeserializeOwned, { type Error = serde_json::Error; fn extract_defuse_payload(self) -> Result, Self::Error> { serde_json::from_str(&self.payload) } }