Skip to main content

defuse_core/payload/
mod.rs

1pub mod erc191;
2pub mod multi;
3pub mod nep413;
4pub mod raw;
5pub mod sep53;
6pub mod tip191;
7pub mod ton_connect;
8pub mod webauthn;
9
10use core::convert::Infallible;
11
12use impl_tools::autoimpl;
13use serde::{Deserialize, Serialize};
14use serde_with::{base64::Base64, serde_as};
15
16use crate::{AccountId, Nonce, Timestamp};
17
18// TODO: add version
19#[serde_as]
20#[autoimpl(Deref using self.message)]
21#[autoimpl(DerefMut using self.message)]
22#[cfg_attr(feature = "schemars-v0_8", derive(::schemars::JsonSchema))]
23#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct DefusePayload<T> {
25    pub signer_id: AccountId,
26    pub verifying_contract: AccountId,
27    pub deadline: Timestamp,
28    #[serde_as(as = "Base64")]
29    #[cfg_attr(feature = "schemars-v0_8", schemars(example = "self::examples::nonce"))]
30    pub nonce: Nonce,
31
32    #[serde(flatten)]
33    pub message: T,
34}
35
36pub trait ExtractDefusePayload<T> {
37    type Error;
38
39    fn extract_defuse_payload(self) -> Result<DefusePayload<T>, Self::Error>;
40}
41
42impl<T> ExtractDefusePayload<T> for DefusePayload<T> {
43    type Error = Infallible;
44
45    #[inline]
46    fn extract_defuse_payload(self) -> Result<Self, Self::Error> {
47        Ok(self)
48    }
49}
50
51/// Data that can be deterministically hashed for signing or verification.
52///
53/// Implementations of this trait typically represent a message formatted
54/// according to an external signing standard. The [`.hash()`](Self::hash)
55/// method returns the digest that should be signed or used for verification.
56pub trait Payload {
57    fn hash(&self) -> [u8; 32];
58}
59
60/// Extension of [`Payload`] for types that include a signature.
61///
62/// Implementers verify the signature and, when successful, return the
63/// signer's public key. This trait is mainly intended for internal use and
64/// does not constitute a stable public API.
65pub trait SignedPayload: Payload {
66    type PublicKey;
67
68    fn verify(&self) -> Option<Self::PublicKey>;
69}
70
71#[cfg(feature = "schemars-v0_8")]
72mod examples {
73    use super::Nonce;
74
75    use base64::Engine;
76
77    pub fn nonce() -> String {
78        base64::engine::general_purpose::STANDARD.encode(Nonce::default())
79    }
80}