Skip to content

Commit

Permalink
feat: add TryFrom<StarkFelt> for u32 (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
asmaastarkware authored May 23, 2024
1 parent 36bc27a commit 463e1c0
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,20 @@ impl TryFrom<StarkFelt> for usize {
}
}

impl TryFrom<StarkFelt> for u32 {
type Error = StarknetApiError;
fn try_from(felt: StarkFelt) -> Result<Self, Self::Error> {
const COMPLIMENT_OF_U32: usize = 28; // 32 - 4
let (rest, u32_bytes) = felt.bytes().split_at(COMPLIMENT_OF_U32);
if rest != [0u8; COMPLIMENT_OF_U32] {
return Err(StarknetApiError::OutOfRange { string: felt.to_string() });
}

let bytes: [u8; 4] = u32_bytes.try_into().unwrap();
Ok(u32::from_be_bytes(bytes))
}
}

// TODO(Arni, 1/1/2024): This is a Hack. Remove this and implement arethmetics for StarkFelt.
impl TryFrom<StarkFelt> for u64 {
type Error = StarknetApiError;
Expand Down

0 comments on commit 463e1c0

Please sign in to comment.