Skip to content

Commit df81e5c

Browse files
authored
refactor(core): remove dependency on thiserror (#426)
1 parent a2a1e83 commit df81e5c

File tree

6 files changed

+165
-68
lines changed

6 files changed

+165
-68
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

starknet-core/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ serde_json = { version = "1.0.96", default-features = false, features = ["alloc"
2929
serde_json_pythonic = { version = "0.1.2", default-features = false, features = ["alloc", "raw_value"] }
3030
serde_with = "2.3.2"
3131
sha3 = "0.10.7"
32-
thiserror = "1.0.40"
3332

3433
[dev-dependencies]
3534
criterion = { version = "0.4.0", default-features = false }

starknet-core/src/crypto.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,48 @@ use crate::types::FieldElement;
22

33
pub use starknet_crypto::{pedersen_hash, ExtendedSignature, Signature};
44
use starknet_crypto::{rfc6979_generate_k, sign, verify, SignError, VerifyError};
5-
use thiserror::Error;
65

7-
#[derive(Debug, Error)]
8-
pub enum EcdsaSignError {
9-
#[error("message hash out of range")]
10-
MessageHashOutOfRange,
11-
}
6+
mod errors {
7+
use core::fmt::{Display, Formatter, Result};
8+
use std::error::Error;
9+
10+
#[derive(Debug)]
11+
pub enum EcdsaSignError {
12+
MessageHashOutOfRange,
13+
}
14+
15+
#[derive(Debug)]
16+
pub enum EcdsaVerifyError {
17+
MessageHashOutOfRange,
18+
InvalidPublicKey,
19+
SignatureROutOfRange,
20+
SignatureSOutOfRange,
21+
}
22+
23+
impl Error for EcdsaSignError {}
1224

13-
#[derive(Debug, Error)]
14-
pub enum EcdsaVerifyError {
15-
#[error("message hash out of range")]
16-
MessageHashOutOfRange,
17-
#[error("invalid public key")]
18-
InvalidPublicKey,
19-
#[error("signature r value out of range")]
20-
SignatureROutOfRange,
21-
#[error("signature s value out of range")]
22-
SignatureSOutOfRange,
25+
impl Display for EcdsaSignError {
26+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
27+
match self {
28+
Self::MessageHashOutOfRange => write!(f, "message hash out of range"),
29+
}
30+
}
31+
}
32+
33+
impl Error for EcdsaVerifyError {}
34+
35+
impl Display for EcdsaVerifyError {
36+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
37+
match self {
38+
Self::MessageHashOutOfRange => write!(f, "message hash out of range"),
39+
Self::InvalidPublicKey => write!(f, "invalid public key"),
40+
Self::SignatureROutOfRange => write!(f, "signature r value out of range"),
41+
Self::SignatureSOutOfRange => write!(f, "signature s value out of range"),
42+
}
43+
}
44+
}
2345
}
46+
pub use errors::{EcdsaSignError, EcdsaVerifyError};
2447

2548
pub fn compute_hash_on_elements(data: &[FieldElement]) -> FieldElement {
2649
let mut current_hash = FieldElement::ZERO;

starknet-core/src/types/codegen.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// https://github.com/xJonathanLEI/starknet-jsonrpc-codegen
44

55
// Code generated with version:
6-
// https://github.com/xJonathanLEI/starknet-jsonrpc-codegen#14c217ef55ee23dcba2a956ddecb6e4bb9d02bb9
6+
// https://github.com/xJonathanLEI/starknet-jsonrpc-codegen#73397c9d87962949675bca8fdc08faa88aa8ffb4
77

88
// Code generation requested but not implemented for these types:
99
// - `BLOCK_ID`
@@ -986,49 +986,60 @@ pub struct SierraEntryPoint {
986986
}
987987

988988
/// JSON-RPC error codes
989-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, thiserror::Error)]
989+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
990990
pub enum StarknetError {
991991
/// Failed to write transaction
992-
#[error("Failed to write transaction")]
993992
FailedToReceiveTransaction,
994993
/// Contract not found
995-
#[error("Contract not found")]
996994
ContractNotFound,
997995
/// Block not found
998-
#[error("Block not found")]
999996
BlockNotFound,
1000997
/// Transaction hash not found
1001-
#[error("Transaction hash not found")]
1002998
TransactionHashNotFound,
1003999
/// Invalid transaction index in a block
1004-
#[error("Invalid transaction index in a block")]
10051000
InvalidTransactionIndex,
10061001
/// Class hash not found
1007-
#[error("Class hash not found")]
10081002
ClassHashNotFound,
10091003
/// Requested page size is too big
1010-
#[error("Requested page size is too big")]
10111004
PageSizeTooBig,
10121005
/// There are no blocks
1013-
#[error("There are no blocks")]
10141006
NoBlocks,
10151007
/// The supplied continuation token is invalid or unknown
1016-
#[error("The supplied continuation token is invalid or unknown")]
10171008
InvalidContinuationToken,
10181009
/// Too many keys provided in a filter
1019-
#[error("Too many keys provided in a filter")]
10201010
TooManyKeysInFilter,
10211011
/// Contract error
1022-
#[error("Contract error")]
10231012
ContractError,
10241013
/// Invalid contract class
1025-
#[error("Invalid contract class")]
10261014
InvalidContractClass,
10271015
/// Class already declared
1028-
#[error("Class already declared")]
10291016
ClassAlreadyDeclared,
10301017
}
10311018

1019+
impl std::error::Error for StarknetError {}
1020+
1021+
impl std::fmt::Display for StarknetError {
1022+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1023+
match self {
1024+
Self::FailedToReceiveTransaction => write!(f, "Failed to write transaction"),
1025+
Self::ContractNotFound => write!(f, "Contract not found"),
1026+
Self::BlockNotFound => write!(f, "Block not found"),
1027+
Self::TransactionHashNotFound => write!(f, "Transaction hash not found"),
1028+
Self::InvalidTransactionIndex => write!(f, "Invalid transaction index in a block"),
1029+
Self::ClassHashNotFound => write!(f, "Class hash not found"),
1030+
Self::PageSizeTooBig => write!(f, "Requested page size is too big"),
1031+
Self::NoBlocks => write!(f, "There are no blocks"),
1032+
Self::InvalidContinuationToken => {
1033+
write!(f, "The supplied continuation token is invalid or unknown")
1034+
}
1035+
Self::TooManyKeysInFilter => write!(f, "Too many keys provided in a filter"),
1036+
Self::ContractError => write!(f, "Contract error"),
1037+
Self::InvalidContractClass => write!(f, "Invalid contract class"),
1038+
Self::ClassAlreadyDeclared => write!(f, "Class already declared"),
1039+
}
1040+
}
1041+
}
1042+
10321043
#[serde_as]
10331044
#[derive(Debug, Clone, Serialize, Deserialize)]
10341045
#[cfg_attr(feature = "no_unknown_fields", serde(deny_unknown_fields))]

starknet-core/src/types/contract/mod.rs

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -238,27 +238,58 @@ pub enum EventFieldKind {
238238
Nested,
239239
}
240240

241-
#[derive(Debug, thiserror::Error)]
242-
pub enum ComputeClassHashError {
243-
#[error("invalid builtin name")]
244-
InvalidBuiltinName,
245-
#[error("json serialization error: {0}")]
246-
Json(JsonError),
247-
}
241+
mod errors {
242+
use core::fmt::{Display, Formatter, Result};
243+
use std::error::Error;
244+
245+
#[derive(Debug)]
246+
pub enum ComputeClassHashError {
247+
InvalidBuiltinName,
248+
Json(JsonError),
249+
}
248250

249-
#[derive(Debug, thiserror::Error)]
250-
pub enum CompressProgramError {
251-
#[error("json serialization error: {0}")]
252-
Json(JsonError),
253-
#[error("compression io error: {0}")]
254-
Io(std::io::Error),
255-
}
251+
#[derive(Debug)]
252+
pub enum CompressProgramError {
253+
Json(JsonError),
254+
Io(std::io::Error),
255+
}
256+
257+
#[derive(Debug)]
258+
pub struct JsonError {
259+
pub(crate) message: String,
260+
}
261+
262+
impl Error for ComputeClassHashError {}
256263

257-
#[derive(Debug, thiserror::Error)]
258-
#[error("{message}")]
259-
pub struct JsonError {
260-
pub(crate) message: String,
264+
impl Display for ComputeClassHashError {
265+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
266+
match self {
267+
Self::InvalidBuiltinName => write!(f, "invalid builtin name"),
268+
Self::Json(inner) => write!(f, "json serialization error: {}", inner),
269+
}
270+
}
271+
}
272+
273+
impl Error for CompressProgramError {}
274+
275+
impl Display for CompressProgramError {
276+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
277+
match self {
278+
Self::Json(inner) => write!(f, "json serialization error: {}", inner),
279+
Self::Io(inner) => write!(f, "compression io error: {}", inner),
280+
}
281+
}
282+
}
283+
284+
impl Error for JsonError {}
285+
286+
impl Display for JsonError {
287+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
288+
write!(f, "{}", self.message)
289+
}
290+
}
261291
}
292+
pub use errors::{CompressProgramError, ComputeClassHashError, JsonError};
262293

263294
impl SierraClass {
264295
pub fn class_hash(&self) -> Result<FieldElement, ComputeClassHashError> {

starknet-core/src/utils.rs

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use crate::{crypto::compute_hash_on_elements, types::FieldElement};
22

33
use sha3::{Digest, Keccak256};
44
use starknet_crypto::pedersen_hash;
5-
use thiserror::Error;
65

76
const DEFAULT_ENTRY_POINT_NAME: &str = "__default__";
87
const DEFAULT_L1_ENTRY_POINT_NAME: &str = "__l1_default__";
@@ -36,25 +35,60 @@ pub struct UdcUniqueSettings {
3635
pub udc_contract_address: FieldElement,
3736
}
3837

39-
#[derive(Debug, Error)]
40-
#[error("the provided name contains non-ASCII characters")]
41-
pub struct NonAsciiNameError;
38+
mod errors {
39+
use core::fmt::{Display, Formatter, Result};
40+
use std::error::Error;
4241

43-
#[derive(Debug, Error)]
44-
pub enum CairoShortStringToFeltError {
45-
#[error("Cairo string can only contain ASCII characters")]
46-
NonAsciiCharacter,
47-
#[error("short string exceeds maximum length of 31 characters")]
48-
StringTooLong,
49-
}
42+
#[derive(Debug)]
43+
pub struct NonAsciiNameError;
44+
45+
#[derive(Debug)]
46+
pub enum CairoShortStringToFeltError {
47+
NonAsciiCharacter,
48+
StringTooLong,
49+
}
50+
51+
#[derive(Debug)]
52+
pub enum ParseCairoShortStringError {
53+
ValueOutOfRange,
54+
UnexpectedNullTerminator,
55+
}
56+
57+
impl Error for NonAsciiNameError {}
58+
59+
impl Display for NonAsciiNameError {
60+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
61+
write!(f, "the provided name contains non-ASCII characters")
62+
}
63+
}
5064

51-
#[derive(Debug, Error)]
52-
pub enum ParseCairoShortStringError {
53-
#[error("field element value out of range")]
54-
ValueOutOfRange,
55-
#[error("unexpected null terminator")]
56-
UnexpectedNullTerminator,
65+
impl Error for CairoShortStringToFeltError {}
66+
67+
impl Display for CairoShortStringToFeltError {
68+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
69+
match self {
70+
Self::NonAsciiCharacter => {
71+
write!(f, "Cairo string can only contain ASCII characters")
72+
}
73+
Self::StringTooLong => {
74+
write!(f, "short string exceeds maximum length of 31 characters")
75+
}
76+
}
77+
}
78+
}
79+
80+
impl Error for ParseCairoShortStringError {}
81+
82+
impl Display for ParseCairoShortStringError {
83+
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
84+
match self {
85+
Self::ValueOutOfRange => write!(f, "field element value out of range"),
86+
Self::UnexpectedNullTerminator => write!(f, "unexpected null terminator"),
87+
}
88+
}
89+
}
5790
}
91+
pub use errors::{CairoShortStringToFeltError, NonAsciiNameError, ParseCairoShortStringError};
5892

5993
/// A variant of eth-keccak that computes a value that fits in a Starknet field element.
6094
pub fn starknet_keccak(data: &[u8]) -> FieldElement {

0 commit comments

Comments
 (0)