diff --git a/crates/context/config/src/client/env/proxy/types/starknet.rs b/crates/context/config/src/client/env/proxy/types/starknet.rs index fff5306e0..b472f7cb5 100644 --- a/crates/context/config/src/client/env/proxy/types/starknet.rs +++ b/crates/context/config/src/client/env/proxy/types/starknet.rs @@ -337,8 +337,8 @@ impl From for ProposalAction { amount, calldata, ) => ProposalAction::ExternalFunctionCall { - receiver_id: format!("0x{}", hex::encode(contract.to_bytes_be())), - method_name: format!("0x{}", hex::encode(selector.to_bytes_be())), + receiver_id: contract.to_string(), + method_name: selector.to_string(), args: calldata .iter() .map(|felt| format!("0x{}", hex::encode(felt.to_bytes_be()))) @@ -522,43 +522,44 @@ impl From<(Vec, Vec)> for ContextStorageEntry { struct ValueCodec<'a>(&'a serde_json::Value); -impl<'a> Encode for ValueCodec<'a> { - fn encode(&self, writer: &mut W) -> Result<(), Error> { - match self.0 { - serde_json::Value::Bool(b) => { - writer.write(Felt::from(*b as u64)); - Ok(()) +impl<'a> ValueCodec<'a> { + fn encode_string(s: &str, writer: &mut W) -> Result<(), Error> { + if s.starts_with("0x") { + // Attempt to handle hex string directly as a single Felt + if let Ok(felt) = Felt::from_hex(s) { + writer.write(felt); + return Ok(()); } - serde_json::Value::String(s) => { - if s.starts_with("0x") { - // Handle hex string directly as a single Felt - writer.write( - Felt::from_hex(&s) - .map_err(|e| Error::custom(&format!("Invalid hex string: {}", e)))?, - ); - } else { - // Regular string - split into chunks - let chunk_size = 31; - let chunks: Vec<_> = s.as_bytes().chunks(chunk_size).collect(); + } + // Regular string - split into chunks + let chunk_size = 31; + let bytes = s.as_bytes(); - // Write number of chunks first - writer.write(Felt::from(chunks.len())); + // Write number of chunks first + writer.write(Felt::from(bytes.len() / chunk_size)); - // Write each chunk as a Felt - for chunk in chunks { - writer.write(Felt::from_bytes_be_slice(chunk)); - } + // Write each chunk as a Felt + for chunk in bytes.chunks(chunk_size) { + writer.write(Felt::from_bytes_be_slice(chunk)); + } + Ok(()) + } +} + +impl<'a> Encode for ValueCodec<'a> { + fn encode(&self, writer: &mut W) -> Result<(), Error> { + match self.0 { + serde_json::Value::String(s) => Self::encode_string(s, writer), + serde_json::Value::Object(obj) => { + writer.write(Felt::from(obj.len())); + for (key, value) in obj { + Self::encode_string(key, writer)?; + ValueCodec(value).encode(writer)?; } Ok(()) } - serde_json::Value::Array(arr) => { - // Write array length first - writer.write(Felt::from(arr.len())); - - // Encode each array element - for item in arr { - ValueCodec(item).encode(writer)?; - } + serde_json::Value::Bool(b) => { + writer.write(Felt::from(*b as u64)); Ok(()) } serde_json::Value::Number(n) => { @@ -571,11 +572,13 @@ impl<'a> Encode for ValueCodec<'a> { } Ok(()) } - serde_json::Value::Object(obj) => { - writer.write(Felt::from(obj.len())); - for (key, value) in obj { - ValueCodec(&serde_json::Value::String(key.clone())).encode(writer)?; - ValueCodec(value).encode(writer)?; + serde_json::Value::Array(arr) => { + // Write array length first + writer.write(Felt::from(arr.len())); + + // Encode each array element + for item in arr { + ValueCodec(item).encode(writer)?; } Ok(()) }