Skip to content

Commit

Permalink
feat: add TryFrom<StarkFelt> for u128 (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
asmaastarkware authored May 19, 2024
1 parent 19f6682 commit df48c34
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 @@ -324,6 +324,20 @@ impl TryFrom<StarkFelt> for u64 {
}
}

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

let bytes: [u8; 16] = u128_bytes.try_into().unwrap();
Ok(u128::from_be_bytes(bytes))
}
}

impl Debug for StarkFelt {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.str_format(f)
Expand Down

0 comments on commit df48c34

Please sign in to comment.