Skip to content

Commit 794750f

Browse files
committed
feat: add UPPER_BOUND & LOWER_BOUND to all our new types
1 parent c5ce32c commit 794750f

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

crates/starknet-types-core/src/contract_address.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ use crate::{
2525
pub struct ContractAddress(PatriciaKey);
2626

2727
impl ContractAddress {
28+
/// Lower inclusive bound
29+
pub const LOWER_BOUND: Self = Self::from_hex_unchecked("0x0");
30+
/// Upper non-inclusive bound
31+
pub const UPPER_BOUND: Self = Self(PatriciaKey::UPPER_BOUND);
32+
2833
pub const ZERO: Self = Self::from_hex_unchecked("0x0");
2934
pub const ONE: Self = Self::from_hex_unchecked("0x1");
3035
pub const TWO: Self = Self::from_hex_unchecked("0x2");
@@ -91,6 +96,11 @@ impl FromStr for ContractAddress {
9196
}
9297

9398
impl ContractAddress {
99+
/// Create a new [ContractAddress] from an hex encoded string without checking it is a valid value.
100+
///
101+
/// Should NEVER be used on user inputs,
102+
/// as it can cause erroneous execution if dynamically initialized with bad values.
103+
/// Should mostly be used at compilation time on hardcoded static string.
94104
pub const fn from_hex_unchecked(s: &'static str) -> ContractAddress {
95105
let patricia_key = PatriciaKey::from_hex_unchecked(s);
96106

@@ -110,8 +120,6 @@ mod test {
110120

111121
#[test]
112122
fn basic_values() {
113-
assert!(ContractAddress::try_from(Felt::ZERO).is_err());
114-
assert!(ContractAddress::try_from(Felt::ONE).is_err());
115123
assert!(ContractAddress::try_from(PATRICIA_KEY_UPPER_BOUND).is_err());
116124

117125
let felt = Felt::TWO;

crates/starknet-types-core/src/patricia_key.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ pub const PATRICIA_KEY_UPPER_BOUND: Felt =
2525
)]
2626
pub struct PatriciaKey(Felt);
2727

28+
impl PatriciaKey {
29+
/// Lower inclusive bound
30+
pub const LOWER_BOUND: Self = Self(Felt::ZERO);
31+
/// Upper non-inclusive bound
32+
pub const UPPER_BOUND: Self = Self::from_hex_unchecked(
33+
"0x800000000000000000000000000000000000000000000000000000000000001",
34+
);
35+
}
36+
2837
impl core::fmt::Display for PatriciaKey {
2938
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
3039
write!(f, "{}", self.0)
@@ -110,6 +119,11 @@ impl FromStr for PatriciaKey {
110119
}
111120

112121
impl PatriciaKey {
122+
/// Create a new [PatriciaKey] from an hex encoded string without checking it is a valid value.
123+
///
124+
/// Should NEVER be used on user inputs,
125+
/// as it can cause erroneous execution if dynamically initialized with bad values.
126+
/// Should mostly be used at compilation time on hardcoded static string.
113127
pub const fn from_hex_unchecked(s: &'static str) -> PatriciaKey {
114128
let felt = Felt::from_hex_unwrap(s);
115129

crates/starknet-types-core/src/regular_contract_address.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ use crate::{
3131
)]
3232
pub struct RegularContractAddress(ContractAddress);
3333

34+
impl RegularContractAddress {
35+
/// Lower inclusive bound
36+
pub const LOWER_BOUND: Self = Self::from_hex_unchecked("0x4");
37+
/// Upper non-inclusive bound
38+
pub const UPPER_BOUND: Self = Self(ContractAddress::UPPER_BOUND);
39+
}
40+
3441
impl core::fmt::Display for RegularContractAddress {
3542
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
3643
write!(f, "{}", self.0)
@@ -167,7 +174,7 @@ impl Felt {
167174
/// https://docs.starknet.io/learn/protocol/state#special-addresses
168175
/// https://github.com/starkware-libs/sequencer/blob/ecd4779abef7bf345938a69f18ef70b6239d3a50/crates/blockifier/resources/blockifier_versioned_constants_0_15_0.json#L92-L97
169176
pub fn is_regular_contract_address(&self) -> bool {
170-
self > &Felt::THREE && self < &PATRICIA_KEY_UPPER_BOUND
177+
self > &RegularContractAddress::LOWER_BOUND && self < &PATRICIA_KEY_UPPER_BOUND
171178
}
172179
}
173180

@@ -218,6 +225,11 @@ impl FromStr for RegularContractAddress {
218225
}
219226

220227
impl RegularContractAddress {
228+
/// Create a new [RegularContractAddress] from an hex encoded string without checking it is a valid value.
229+
///
230+
/// Should NEVER be used on user inputs,
231+
/// as it can cause erroneous execution if dynamically initialized with bad values.
232+
/// Should mostly be used at compilation time on hardcoded static string.
221233
pub const fn from_hex_unchecked(s: &'static str) -> RegularContractAddress {
222234
let contract_address = ContractAddress::from_hex_unchecked(s);
223235

0 commit comments

Comments
 (0)