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 defuse_serde_utils::base64::Base64;
13use impl_tools::autoimpl;
14use near_sdk::{AccountId, near};
15use serde_with::serde_as;
16
17use crate::{Deadline, Nonce};
18
19// TODO: add version
20#[near(serializers = [json])]
21#[autoimpl(Deref using self.message)]
22#[autoimpl(DerefMut using self.message)]
23#[derive(Debug, Clone)]
24pub struct DefusePayload<T> {
25    pub signer_id: AccountId,
26    pub verifying_contract: AccountId,
27    pub deadline: Deadline,
28    #[serde_as(as = "Base64")]
29    #[cfg_attr(
30        all(feature = "abi", not(target_arch = "wasm32")),
31        schemars(example = "self::examples::nonce")
32    )]
33    pub nonce: Nonce,
34
35    #[serde(flatten)]
36    pub message: T,
37}
38
39pub trait ExtractDefusePayload<T> {
40    type Error;
41
42    fn extract_defuse_payload(self) -> Result<DefusePayload<T>, Self::Error>;
43}
44
45impl<T> ExtractDefusePayload<T> for DefusePayload<T> {
46    type Error = Infallible;
47
48    #[inline]
49    fn extract_defuse_payload(self) -> Result<Self, Self::Error> {
50        Ok(self)
51    }
52}
53
54#[cfg(all(feature = "abi", not(target_arch = "wasm32")))]
55mod examples {
56    use super::*;
57
58    use near_sdk::base64::{self, Engine};
59
60    pub fn nonce() -> String {
61        base64::engine::general_purpose::STANDARD.encode(Nonce::default())
62    }
63}