defuse_core/payload/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
pub mod erc191;
pub mod multi;
pub mod nep413;
pub mod raw;
pub mod ton_connect;
pub mod webauthn;

use core::convert::Infallible;

use defuse_serde_utils::base64::Base64;
use impl_tools::autoimpl;
use near_sdk::{AccountId, near};
use serde_with::serde_as;

use crate::{Deadline, Nonce};

// TODO: add version
#[cfg_attr(
    all(feature = "abi", not(target_arch = "wasm32")),
    serde_as(schemars = true)
)]
#[cfg_attr(
    not(all(feature = "abi", not(target_arch = "wasm32"))),
    serde_as(schemars = false)
)]
#[near(serializers = [json])]
#[autoimpl(Deref using self.message)]
#[autoimpl(DerefMut using self.message)]
#[derive(Debug, Clone)]
pub struct DefusePayload<T> {
    pub signer_id: AccountId,
    pub verifying_contract: AccountId,
    pub deadline: Deadline,
    #[serde_as(as = "Base64")]
    #[cfg_attr(
        all(feature = "abi", not(target_arch = "wasm32")),
        schemars(example = "self::examples::nonce")
    )]
    pub nonce: Nonce,

    #[serde(flatten)]
    pub message: T,
}

pub trait ExtractDefusePayload<T> {
    type Error;

    fn extract_defuse_payload(self) -> Result<DefusePayload<T>, Self::Error>;
}

impl<T> ExtractDefusePayload<T> for DefusePayload<T> {
    type Error = Infallible;

    #[inline]
    fn extract_defuse_payload(self) -> Result<Self, Self::Error> {
        Ok(self)
    }
}

#[cfg(all(feature = "abi", not(target_arch = "wasm32")))]
mod examples {
    use super::*;

    use near_sdk::base64::{self, Engine};

    pub fn nonce() -> String {
        base64::engine::general_purpose::STANDARD.encode(Nonce::default())
    }
}