ex3_node_types/
sign_schema.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use crate::impl_from_uint_for;
use num_bigint::BigUint;
use num_traits::ToPrimitive;
use serde::{Deserialize, Serialize};
use std::fmt::Display;

/// Signature schema
///
/// Note: u8 is enough for the signature schema
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, Ord, PartialOrd, Copy)]
pub enum SignatureSchema {
    BtcEcdsa = 1000,
    EvmEcdsa = 1001,
    TronEcdsa = 1002,

    TonEddsaOpenMask = 2000,
    SolanaEddsa = 2010,
    IcpEddsa = 2020,

    BtcSchnorr = 3000,
    KaspaSchnorr = 3010,
}

impl SignatureSchema {
    /// Get the value of the signature schema
    pub fn value(&self) -> u16 {
        match self {
            SignatureSchema::BtcEcdsa => 1000,
            SignatureSchema::EvmEcdsa => 1001,
            SignatureSchema::TronEcdsa => 1002,
            SignatureSchema::TonEddsaOpenMask => 2000,
            SignatureSchema::SolanaEddsa => 2010,
            SignatureSchema::IcpEddsa => 2020,
            SignatureSchema::BtcSchnorr => 3000,
            SignatureSchema::KaspaSchnorr => 3010,
        }
    }
}

impl Display for SignatureSchema {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SignatureSchema::BtcEcdsa => write!(f, "BtcEcdsa"),
            SignatureSchema::EvmEcdsa => write!(f, "EvmEcdsa"),
            SignatureSchema::TronEcdsa => write!(f, "TronEcdsa"),
            SignatureSchema::TonEddsaOpenMask => write!(f, "TonEddsaOpenMask"),
            SignatureSchema::SolanaEddsa => write!(f, "SolanaEddsa"),
            SignatureSchema::IcpEddsa => write!(f, "IcpEddsa"),
            SignatureSchema::BtcSchnorr => write!(f, "BtcSchnorr"),
            SignatureSchema::KaspaSchnorr => write!(f, "KaspaSchnorr"),
        }
    }
}

impl From<BigUint> for SignatureSchema {
    fn from(value: BigUint) -> Self {
        match value.to_u16().unwrap() {
            1000 => SignatureSchema::BtcEcdsa,
            1001 => SignatureSchema::EvmEcdsa,
            1002 => SignatureSchema::TronEcdsa,
            2000 => SignatureSchema::TonEddsaOpenMask,
            2010 => SignatureSchema::SolanaEddsa,
            2020 => SignatureSchema::IcpEddsa,
            3000 => SignatureSchema::BtcSchnorr,
            3010 => SignatureSchema::KaspaSchnorr,
            _ => panic!("Invalid value for SignatureSchema"),
        }
    }
}

impl_from_uint_for!(SignatureSchema, u16, u32, u64, u128);

#[cfg(test)]
mod tests {
    use super::*;
    use ex3_serde::{bincode, cbor};
    #[test]
    fn test_signature_schema_serde() {
        let schema = SignatureSchema::BtcEcdsa;

        let bytes = bincode::serialize(&schema).unwrap();
        let schema2: SignatureSchema = bincode::deserialize(&bytes).unwrap();

        assert_eq!(schema, schema2);

        let schema = SignatureSchema::BtcEcdsa;
        let bytes = cbor::serialize(&schema).unwrap();
        let schema2: SignatureSchema = cbor::deserialize(&bytes).unwrap();

        assert_eq!(schema, schema2);
    }

    #[test]
    fn test_signature_schema_hex() {
        assert_eq!(format!("{:x}", SignatureSchema::BtcEcdsa.value()), "3e8");
        assert_eq!(format!("{:x}", SignatureSchema::EvmEcdsa.value()), "3e9");
    }
}