Skip to content

Commit 0fedc82

Browse files
refactor(starknet_api): serialize chain_id as felt (#6140)
1 parent 5640675 commit 0fedc82

2 files changed

Lines changed: 29 additions & 3 deletions

File tree

crates/starknet_api/src/core.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::sync::LazyLock;
77

88
use num_traits::ToPrimitive;
99
use primitive_types::H160;
10+
use serde::de::Error;
1011
use serde::{Deserialize, Deserializer, Serialize, Serializer};
1112
use size_of::SizeOf;
1213
use starknet_types_core::felt::{Felt, NonZeroFelt};
@@ -85,6 +86,22 @@ impl ChainId {
8586
}
8687
}
8788

89+
pub fn deserialize_chain_id_from_hex<'de, D>(deserializer: D) -> Result<ChainId, D::Error>
90+
where
91+
D: Deserializer<'de>,
92+
{
93+
let hex_str = String::deserialize(deserializer)?;
94+
let chain_id_str =
95+
std::str::from_utf8(&hex::decode(hex_str.trim_start_matches("0x")).map_err(|e| {
96+
D::Error::custom(format!("Failed to decode the hex string {hex_str}. Error: {:?}", e))
97+
})?)
98+
.map_err(|e| {
99+
D::Error::custom(format!("Failed to convert to UTF-8 string. Error: {:?}", e))
100+
})?
101+
.to_string();
102+
Ok(ChainId::from(chain_id_str))
103+
}
104+
88105
/// The address of a contract, used for example in [StateDiff](`crate::state::StateDiff`),
89106
/// [DeclareTransaction](`crate::transaction::DeclareTransaction`), and
90107
/// [BlockHeader](`crate::block::BlockHeader`).

crates/starknet_os/src/io/os_input.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
use std::collections::HashMap;
22

33
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
4-
use serde::{Deserialize, Serialize};
4+
use serde::Serialize;
55
use shared_execution_objects::central_objects::CentralTransactionExecutionInfo;
66
use starknet_api::block::{BlockHash, BlockInfo, BlockNumber};
7-
use starknet_api::core::{ChainId, ClassHash, CompiledClassHash, ContractAddress, Nonce};
7+
use starknet_api::core::{
8+
deserialize_chain_id_from_hex,
9+
ChainId,
10+
ClassHash,
11+
CompiledClassHash,
12+
ContractAddress,
13+
Nonce,
14+
};
815
use starknet_api::deprecated_contract_class::ContractClass;
916
use starknet_api::executable_transaction::Transaction;
1017
use starknet_api::state::StorageKey;
@@ -69,8 +76,10 @@ pub struct StarknetOsInput {
6976
}
7077

7178
// TODO(Meshi): Remove Once the blockifier ChainInfo do not support deprecated fee token.
72-
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
79+
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
80+
#[derive(Clone, Debug, PartialEq, Serialize)]
7381
pub struct OsChainInfo {
82+
#[cfg_attr(feature = "deserialize", serde(deserialize_with = "deserialize_chain_id_from_hex"))]
7483
pub(crate) chain_id: ChainId,
7584
pub(crate) strk_fee_token_address: ContractAddress,
7685
}

0 commit comments

Comments
 (0)