1use bytes::Bytes;
2use derive_more::Display;
3use num_derive::FromPrimitive;
4use std::panic;
5
6use crate::Verifier;
7
8#[cfg(feature = "arweave")]
9use crate::ArweaveSigner;
10
11#[cfg(any(feature = "solana", feature = "algorand"))]
12use crate::Ed25519Signer;
13
14#[cfg(any(feature = "ethereum", feature = "erc20"))]
15use crate::Secp256k1Signer;
16
17#[cfg(feature = "cosmos")]
18use crate::CosmosSigner;
19
20#[cfg(feature = "aptos")]
21use crate::AptosSigner;
22
23#[cfg(feature = "aptos")]
24use crate::MultiAptosSigner;
25
26use crate::error::BundlrError;
27use crate::signers::typed_ethereum::TypedEthereumSigner;
28
29#[derive(FromPrimitive, Display, PartialEq, Eq, Debug, Clone)]
30pub enum SignerMap {
31 None = -1,
32 Arweave = 1,
33 ED25519 = 2,
34 Ethereum = 3,
35 Solana = 4,
36 InjectedAptos = 5,
37 MultiAptos = 6,
38 TypedEthereum = 7,
39 Cosmos, }
41
42pub struct Config {
43 pub sig_length: usize,
44 pub pub_length: usize,
45 pub sig_name: String,
46}
47
48#[allow(unused)]
49impl Config {
50 pub fn total_length(&self) -> u32 {
51 self.sig_length as u32 + self.pub_length as u32
52 }
53}
54
55impl From<u16> for SignerMap {
56 fn from(t: u16) -> Self {
57 match t {
58 1 => SignerMap::Arweave,
59 2 => SignerMap::ED25519,
60 3 => SignerMap::Ethereum,
61 4 => SignerMap::Solana,
62 5 => SignerMap::InjectedAptos,
63 6 => SignerMap::MultiAptos,
64 7 => SignerMap::TypedEthereum,
65 _ => SignerMap::None,
66 }
67 }
68}
69
70impl SignerMap {
71 pub fn as_u16(&self) -> u16 {
72 match self {
73 SignerMap::Arweave => 1,
74 SignerMap::ED25519 => 2,
75 SignerMap::Ethereum => 3,
76 SignerMap::Solana => 4,
77 SignerMap::InjectedAptos => 5,
78 SignerMap::MultiAptos => 6,
79 SignerMap::TypedEthereum => 7,
80 _ => u16::MAX,
81 }
82 }
83
84 pub fn get_config(&self) -> Config {
85 match *self {
86 #[cfg(feature = "arweave")]
87 SignerMap::Arweave => Config {
88 sig_length: 512,
89 pub_length: 512,
90 sig_name: "arweave".to_owned(),
91 },
92 #[cfg(feature = "algorand")]
93 SignerMap::ED25519 => Config {
94 sig_length: ed25519_dalek::SIGNATURE_LENGTH,
95 pub_length: ed25519_dalek::PUBLIC_KEY_LENGTH,
96 sig_name: "ed25519".to_owned(),
97 },
98 #[cfg(any(feature = "ethereum", feature = "erc20"))]
99 SignerMap::Ethereum => Config {
100 sig_length: secp256k1::constants::COMPACT_SIGNATURE_SIZE + 1,
101 pub_length: secp256k1::constants::UNCOMPRESSED_PUBLIC_KEY_SIZE,
102 sig_name: "ethereum".to_owned(),
103 },
104 #[cfg(feature = "solana")]
105 SignerMap::Solana => Config {
106 sig_length: ed25519_dalek::SIGNATURE_LENGTH,
107 pub_length: ed25519_dalek::PUBLIC_KEY_LENGTH,
108 sig_name: "solana".to_owned(),
109 },
110 #[cfg(feature = "aptos")]
111 SignerMap::InjectedAptos => Config {
112 sig_length: ed25519_dalek::SIGNATURE_LENGTH,
113 pub_length: ed25519_dalek::PUBLIC_KEY_LENGTH,
114 sig_name: "injectedAptos".to_owned(),
115 },
116 #[cfg(feature = "aptos")]
117 SignerMap::MultiAptos => Config {
118 sig_length: ed25519_dalek::SIGNATURE_LENGTH * 32 + 4, pub_length: ed25519_dalek::PUBLIC_KEY_LENGTH * 32 + 1, sig_name: "multiAptos".to_owned(),
121 },
122 #[cfg(feature = "cosmos")]
123 SignerMap::Cosmos => Config {
124 sig_length: secp256k1::constants::COMPACT_SIGNATURE_SIZE,
125 pub_length: secp256k1::constants::PUBLIC_KEY_SIZE,
126 sig_name: "cosmos".to_owned(),
127 },
128 #[cfg(any(feature = "ethereum", feature = "erc20"))]
129 SignerMap::TypedEthereum => Config {
130 sig_length: secp256k1::constants::COMPACT_SIGNATURE_SIZE + 1,
131 pub_length: 42,
132 sig_name: "typedEthereum".to_owned(),
133 },
134 #[allow(unreachable_patterns)]
135 _ => panic!("{:?} get_config has no", self),
136 }
137 }
138
139 pub fn verify(&self, pk: &[u8], message: &[u8], signature: &[u8]) -> Result<(), BundlrError> {
140 match *self {
141 #[cfg(feature = "arweave")]
142 SignerMap::Arweave => ArweaveSigner::verify(
143 Bytes::copy_from_slice(pk),
144 Bytes::copy_from_slice(message),
145 Bytes::copy_from_slice(signature),
146 ),
147 #[cfg(feature = "algorand")]
148 SignerMap::ED25519 => Ed25519Signer::verify(
149 Bytes::copy_from_slice(pk),
150 Bytes::copy_from_slice(message),
151 Bytes::copy_from_slice(signature),
152 ),
153 #[cfg(any(feature = "ethereum", feature = "erc20"))]
154 SignerMap::Ethereum => Secp256k1Signer::verify(
155 Bytes::copy_from_slice(pk),
156 Bytes::copy_from_slice(message),
157 Bytes::copy_from_slice(signature),
158 ),
159 #[cfg(feature = "solana")]
160 SignerMap::Solana => Ed25519Signer::verify(
161 Bytes::copy_from_slice(pk),
162 Bytes::copy_from_slice(message),
163 Bytes::copy_from_slice(signature),
164 ),
165 #[cfg(feature = "aptos")]
166 SignerMap::InjectedAptos => AptosSigner::verify(
167 Bytes::copy_from_slice(pk),
168 Bytes::copy_from_slice(message),
169 Bytes::copy_from_slice(signature),
170 ),
171 #[cfg(feature = "aptos")]
172 SignerMap::MultiAptos => MultiAptosSigner::verify(
173 Bytes::copy_from_slice(pk),
174 Bytes::copy_from_slice(message),
175 Bytes::copy_from_slice(signature),
176 ),
177 #[cfg(feature = "cosmos")]
178 SignerMap::Cosmos => CosmosSigner::verify(
179 Bytes::copy_from_slice(pk),
180 Bytes::copy_from_slice(message),
181 Bytes::copy_from_slice(signature),
182 ),
183 #[cfg(any(feature = "ethereum", feature = "erc20"))]
184 SignerMap::TypedEthereum => TypedEthereumSigner::verify(
185 Bytes::copy_from_slice(pk),
186 Bytes::copy_from_slice(message),
187 Bytes::copy_from_slice(signature),
188 ),
189 #[allow(unreachable_patterns)]
190 _ => panic!("{:?} verify not implemented in SignerMap yet", self),
191 }
192 }
193}