secret_cosmwasm_crypto/
errors.rs1#[cfg(feature = "backtraces")]
2use std::backtrace::Backtrace;
3use std::fmt::Debug;
4use thiserror::Error;
5
6pub type CryptoResult<T> = core::result::Result<T, CryptoError>;
7
8#[derive(Error, Debug)]
9pub enum CryptoError {
10 #[error("Batch verify error: {msg}")]
11 BatchErr {
12 msg: String,
13 #[cfg(feature = "backtraces")]
14 backtrace: Backtrace,
15 },
16 #[error("Crypto error: {msg}")]
17 GenericErr {
18 msg: String,
19 #[cfg(feature = "backtraces")]
20 backtrace: Backtrace,
21 },
22 #[error("Invalid hash format")]
23 InvalidHashFormat {
24 #[cfg(feature = "backtraces")]
25 backtrace: Backtrace,
26 },
27 #[error("Invalid public key format")]
28 InvalidPubkeyFormat {
29 #[cfg(feature = "backtraces")]
30 backtrace: Backtrace,
31 },
32 #[error("Invalid private key format")]
33 InvalidPrivateKeyFormat {
34 #[cfg(feature = "backtraces")]
35 backtrace: Backtrace,
36 },
37 #[error("Invalid signature format")]
38 InvalidSignatureFormat {
39 #[cfg(feature = "backtraces")]
40 backtrace: Backtrace,
41 },
42 #[error("Invalid recovery parameter. Supported values: 0 and 1.")]
43 InvalidRecoveryParam {
44 #[cfg(feature = "backtraces")]
45 backtrace: Backtrace,
46 },
47}
48
49impl CryptoError {
50 pub fn batch_err(msg: impl Into<String>) -> Self {
51 CryptoError::BatchErr {
52 msg: msg.into(),
53 #[cfg(feature = "backtraces")]
54 backtrace: Backtrace::capture(),
55 }
56 }
57
58 pub fn generic_err(msg: impl Into<String>) -> Self {
59 CryptoError::GenericErr {
60 msg: msg.into(),
61 #[cfg(feature = "backtraces")]
62 backtrace: Backtrace::capture(),
63 }
64 }
65
66 pub fn invalid_hash_format() -> Self {
67 CryptoError::InvalidHashFormat {
68 #[cfg(feature = "backtraces")]
69 backtrace: Backtrace::capture(),
70 }
71 }
72
73 pub fn invalid_pubkey_format() -> Self {
74 CryptoError::InvalidPubkeyFormat {
75 #[cfg(feature = "backtraces")]
76 backtrace: Backtrace::capture(),
77 }
78 }
79
80 pub fn invalid_privkey_format() -> Self {
81 CryptoError::InvalidPrivateKeyFormat {
82 #[cfg(feature = "backtraces")]
83 backtrace: Backtrace::capture(),
84 }
85 }
86
87 pub fn invalid_signature_format() -> Self {
88 CryptoError::InvalidSignatureFormat {
89 #[cfg(feature = "backtraces")]
90 backtrace: Backtrace::capture(),
91 }
92 }
93
94 pub fn invalid_recovery_param() -> Self {
95 CryptoError::InvalidRecoveryParam {
96 #[cfg(feature = "backtraces")]
97 backtrace: Backtrace::capture(),
98 }
99 }
100
101 pub fn code(&self) -> u32 {
104 match self {
105 CryptoError::InvalidHashFormat { .. } => 3,
106 CryptoError::InvalidSignatureFormat { .. } => 4,
107 CryptoError::InvalidPubkeyFormat { .. } => 5,
108 CryptoError::InvalidRecoveryParam { .. } => 6,
109 CryptoError::BatchErr { .. } => 7,
110 CryptoError::GenericErr { .. } => 10,
111 CryptoError::InvalidPrivateKeyFormat { .. } => 1000, }
113 }
114}
115
116#[cfg(test)]
117mod tests {
118 use super::*;
119
120 #[test]
122 fn batch_err_works() {
123 let error = CryptoError::batch_err("something went wrong in a batch way");
124 match error {
125 CryptoError::BatchErr { msg, .. } => {
126 assert_eq!(msg, "something went wrong in a batch way")
127 }
128 _ => panic!("wrong error type!"),
129 }
130 }
131
132 #[test]
133 fn generic_err_works() {
134 let error = CryptoError::generic_err("something went wrong in a general way");
135 match error {
136 CryptoError::GenericErr { msg, .. } => {
137 assert_eq!(msg, "something went wrong in a general way")
138 }
139 _ => panic!("wrong error type!"),
140 }
141 }
142
143 #[test]
144 fn invalid_hash_format_works() {
145 let error = CryptoError::invalid_hash_format();
146 match error {
147 CryptoError::InvalidHashFormat { .. } => {}
148 _ => panic!("wrong error type!"),
149 }
150 }
151
152 #[test]
153 fn invalid_signature_format_works() {
154 let error = CryptoError::invalid_signature_format();
155 match error {
156 CryptoError::InvalidSignatureFormat { .. } => {}
157 _ => panic!("wrong error type!"),
158 }
159 }
160
161 #[test]
162 fn invalid_pubkey_format_works() {
163 let error = CryptoError::invalid_pubkey_format();
164 match error {
165 CryptoError::InvalidPubkeyFormat { .. } => {}
166 _ => panic!("wrong error type!"),
167 }
168 }
169}