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