starknet_devnet_types/rpc/
felt.rs

1use num_bigint::BigUint;
2use starknet_types_core::felt::Felt;
3
4use crate::error::{ConversionError, DevnetResult, Error};
5
6/// Returns (high, low)
7pub fn split_biguint(biguint: BigUint) -> (Felt, Felt) {
8    let high = Felt::from(&biguint >> 128);
9    let low_mask = (BigUint::from(1_u8) << 128) - 1_u8;
10    let low = Felt::from(biguint & low_mask);
11    (high, low)
12}
13
14/// Join high and low part of a felt as biguint
15pub fn join_felts(high: &Felt, low: &Felt) -> BigUint {
16    let high = high.to_biguint();
17    let low = low.to_biguint();
18    (high << 128) + low
19}
20
21pub fn felt_from_prefixed_hex(s: &str) -> DevnetResult<Felt> {
22    if !s.starts_with("0x") {
23        Err(Error::ConversionError(ConversionError::CustomFromHexError(format!(
24            "Missing prefix 0x in {s}"
25        ))))
26    } else {
27        Felt::from_hex(s)
28            .map_err(|e| Error::ConversionError(ConversionError::CustomFromHexError(e.to_string())))
29    }
30}
31
32pub fn try_felt_to_num<T: TryFrom<BigUint>>(f: Felt) -> Result<T, <T as TryFrom<BigUint>>::Error> {
33    f.to_biguint().try_into()
34}
35
36pub type Nonce = Felt;
37pub type TransactionVersion = Felt;
38pub type TransactionSignature = Vec<Felt>;
39pub type CompiledClassHash = Felt;
40pub type EntryPointSelector = Felt;
41pub type Calldata = Vec<Felt>;
42pub type ContractAddressSalt = Felt;
43pub type BlockHash = Felt;
44pub type TransactionHash = Felt;
45pub type ClassHash = Felt;
46pub type Key = Felt;