1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
use std::fmt::LowerHex;
use std::str::FromStr;

use num_bigint::BigUint;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use starknet_api::serde_utils::{bytes_from_hex_str, hex_str_from_bytes};
use starknet_api::StarknetApiError;

use crate::contract_address::ContractAddress;
use crate::error::{ConversionError, DevnetResult, Error};
use crate::serde_helpers::hex_string::{
    deserialize_prefixed_hex_string_to_felt, serialize_to_prefixed_hex,
};
use crate::traits::{ToDecimalString, ToHexString};

#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
pub struct Felt(pub(crate) [u8; 32]);

impl Serialize for Felt {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serialize_to_prefixed_hex(self, serializer)
    }
}

impl<'de> Deserialize<'de> for Felt {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserialize_prefixed_hex_string_to_felt(deserializer)
    }
}

impl Felt {
    pub fn new(bytes: [u8; 32]) -> DevnetResult<Self> {
        if bytes[0] < 0x10 {
            return Ok(Self(bytes));
        }
        Err(Error::StarknetApiError(StarknetApiError::OutOfRange {
            string: hex_str_from_bytes::<32, true>(bytes),
        }))
    }

    pub fn to_field_element(&self) -> DevnetResult<starknet_rs_ff::FieldElement> {
        starknet_rs_ff::FieldElement::from_bytes_be(&self.0)
            .map_err(|_| Error::ConversionError(crate::error::ConversionError::FromByteArrayError))
    }

    pub fn from_prefixed_hex_str(hex_str: &str) -> DevnetResult<Self> {
        let bytes = bytes_from_hex_str::<32, true>(hex_str).map_err(|err| {
            Error::StarknetApiError(starknet_api::StarknetApiError::InnerDeserialization(err))
        })?;

        Self::new(bytes)
    }

    pub fn bytes(&self) -> [u8; 32] {
        self.0
    }

    pub fn is_zero(&self) -> bool {
        self.0.iter().all(|&x| x == 0)
    }
}

impl ToHexString for Felt {
    fn to_prefixed_hex_str(&self) -> String {
        hex_str_from_bytes::<32, true>(self.0)
    }

    fn to_nonprefixed_hex_str(&self) -> String {
        hex_str_from_bytes::<32, false>(self.0)
    }
}

impl ToDecimalString for Felt {
    fn to_decimal_string(&self) -> String {
        let bigint = BigUint::from_bytes_be(&self.bytes());
        bigint.to_str_radix(10)
    }
}

impl From<Felt> for starknet_rs_ff::FieldElement {
    fn from(value: Felt) -> Self {
        starknet_rs_ff::FieldElement::from_bytes_be(&value.0)
            .expect("Convert Felt to FieldElement, should be the same")
    }
}

impl From<&Felt> for starknet_rs_ff::FieldElement {
    fn from(value: &Felt) -> Self {
        starknet_rs_ff::FieldElement::from_bytes_be(&value.0)
            .expect("Convert Felt to FieldElement, should be the same")
    }
}

impl From<starknet_rs_ff::FieldElement> for Felt {
    fn from(value: starknet_rs_ff::FieldElement) -> Self {
        Self(value.to_bytes_be())
    }
}

impl From<&starknet_rs_ff::FieldElement> for Felt {
    fn from(value: &starknet_rs_ff::FieldElement) -> Self {
        Self(value.to_bytes_be())
    }
}

impl From<u128> for Felt {
    fn from(value: u128) -> Self {
        let le_part: [u8; 16] = value.to_be_bytes();
        let byte_arr: [u8; 32] = [[0u8; 16], le_part].concat().try_into().unwrap();
        Self(byte_arr)
    }
}

impl TryFrom<Felt> for u128 {
    type Error = Error;

    fn try_from(value: Felt) -> Result<Self, Self::Error> {
        let ff = starknet_rs_ff::FieldElement::from(value);
        ff.try_into().map_err(|_| {
            Error::ConversionError(ConversionError::OutOfRangeError(
                "Felt is too large to be converted into u128 value".to_string(),
            ))
        })
    }
}

impl From<ContractAddress> for Felt {
    fn from(value: ContractAddress) -> Self {
        value.0.0
    }
}

impl From<starknet_api::hash::StarkFelt> for Felt {
    fn from(value: starknet_api::hash::StarkFelt) -> Self {
        let arr: [u8; 32] = value.bytes().try_into().expect("slice of incorrect length");
        Self(arr)
    }
}

impl From<Felt> for starknet_api::hash::StarkFelt {
    fn from(value: Felt) -> Self {
        starknet_api::hash::StarkFelt::new(value.0).expect("Invalid bytes")
    }
}

impl From<&Felt> for starknet_api::hash::StarkFelt {
    fn from(value: &Felt) -> Self {
        starknet_api::hash::StarkFelt::new(value.0).expect("Invalid bytes")
    }
}

impl From<starknet_api::core::ClassHash> for Felt {
    fn from(value: starknet_api::core::ClassHash) -> Self {
        Felt::from(value.0)
    }
}

impl From<Felt> for starknet_api::core::ClassHash {
    fn from(value: Felt) -> Self {
        Self(starknet_api::hash::StarkFelt::from(value))
    }
}

impl From<Felt> for starknet_api::core::CompiledClassHash {
    fn from(value: Felt) -> Self {
        Self(starknet_api::hash::StarkFelt::from(value))
    }
}

impl From<starknet_api::core::PatriciaKey> for Felt {
    fn from(value: starknet_api::core::PatriciaKey) -> Self {
        let arr: [u8; 32] = value.key().bytes().try_into().expect("slice of incorrect length");
        Self(arr)
    }
}

impl TryFrom<Felt> for starknet_api::core::PatriciaKey {
    type Error = crate::error::Error;

    fn try_from(value: Felt) -> Result<Self, Self::Error> {
        Ok(starknet_api::core::PatriciaKey::try_from(starknet_api::hash::StarkFelt::from(value))?)
    }
}

impl From<Felt> for starknet_api::block::BlockHash {
    fn from(value: Felt) -> Self {
        Self(value.into())
    }
}

impl From<starknet_api::block::BlockHash> for Felt {
    fn from(value: starknet_api::block::BlockHash) -> Self {
        value.0.into()
    }
}

impl TryFrom<BigUint> for Felt {
    type Error = crate::error::Error;

    fn try_from(value: BigUint) -> DevnetResult<Self> {
        let hex_str = format!("0x{}", value.to_str_radix(16));
        Felt::from_prefixed_hex_str(&hex_str)
    }
}

impl From<Felt> for BigUint {
    fn from(felt: Felt) -> Self {
        BigUint::from_str(&felt.to_decimal_string()).expect("Should never fail: felt is 251 bits")
    }
}

impl From<starknet_api::core::Nonce> for Felt {
    fn from(value: starknet_api::core::Nonce) -> Self {
        value.0.into()
    }
}

impl LowerHex for Felt {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.to_prefixed_hex_str().as_str())
    }
}

/// Returns (high, low)
pub fn split_biguint(biguint: BigUint) -> DevnetResult<(Felt, Felt)> {
    let high = Felt::try_from(biguint.clone() >> 128)?;
    let low_mask = (BigUint::from(1_u8) << 128) - 1_u8;
    let low = Felt::try_from(biguint & low_mask)?;
    Ok((high, low))
}

pub type Nonce = Felt;
pub type TransactionVersion = Felt;
pub type TransactionSignature = Vec<Felt>;
pub type CompiledClassHash = Felt;
pub type EntryPointSelector = Felt;
pub type Calldata = Vec<Felt>;
pub type ContractAddressSalt = Felt;
pub type BlockHash = Felt;
pub type TransactionHash = Felt;
pub type ClassHash = Felt;
pub type Key = Felt;

#[cfg(test)]
mod tests {
    use std::str::FromStr;

    use num_bigint::BigUint;

    use super::Felt;
    use crate::traits::ToDecimalString;
    #[test]
    fn correct_conversion_from_hex_str_to_felt() {
        assert!(
            Felt::from_prefixed_hex_str(
                "0x3FCBF77B28C96F4F2FB5BD2D176AB083A12A5E123ADEB0DE955D7EE228C9854"
            )
            .is_ok()
        )
    }

    #[test]
    fn correct_value_after_hex_str_to_felt() {
        let felt = Felt::from_prefixed_hex_str("0xAA").unwrap();
        assert_eq!(felt.0[31], 170);
    }

    #[test]
    fn correct_conversion_from_bigint_to_felt() {
        let bigint = BigUint::from(123456u128);
        assert_eq!(Felt::try_from(bigint).unwrap(), Felt::from_prefixed_hex_str("0x1e240").unwrap())
    }

    #[test]
    /// 2**250 + 1
    fn correct_conversion_from_decimal_string_to_felt() {
        let s = "1809251394333065553493296640760748560207343510400633813116524750123642650625";
        let bigint = BigUint::from_str(s).unwrap();
        assert_eq!(
            Felt::try_from(bigint).unwrap(),
            Felt::from_prefixed_hex_str(
                "0x400000000000000000000000000000000000000000000000000000000000001"
            )
            .unwrap()
        )
    }

    #[test]
    /// 2**250 + 1
    fn correct_conversion_from_felt_to_decimal_string() {
        assert_eq!(
            Felt::from_prefixed_hex_str(
                "0x400000000000000000000000000000000000000000000000000000000000001"
            )
            .unwrap()
            .to_decimal_string(),
            "1809251394333065553493296640760748560207343510400633813116524750123642650625"
        );
    }
}