Skip to content

Commit

Permalink
Merge branch 'master' into contract-error-data-msg
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Nov 6, 2023
2 parents 8db3dfa + 5f1e5cd commit b3858e8
Show file tree
Hide file tree
Showing 24 changed files with 655 additions and 64 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ members = [
all-features = true

[dependencies]
starknet-ff = { version = "0.3.4", path = "./starknet-ff", default-features = false }
starknet-ff = { version = "0.3.5", path = "./starknet-ff", default-features = false }
starknet-crypto = { version = "0.6.1", path = "./starknet-crypto" }
starknet-core = { version = "0.7.0", path = "./starknet-core", default-features = false }
starknet-core = { version = "0.7.2", path = "./starknet-core", default-features = false }
starknet-providers = { version = "0.7.0", path = "./starknet-providers" }
starknet-contract = { version = "0.6.0", path = "./starknet-contract" }
starknet-signers = { version = "0.5.0", path = "./starknet-signers" }
Expand Down
2 changes: 1 addition & 1 deletion examples/starknet-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ crate-type = ["cdylib", "rlib"]
default = ["console_error_panic_hook"]

[dependencies]
starknet-ff = { version = "0.3.4", path = "../../starknet-ff" }
starknet-ff = { version = "0.3.5", path = "../../starknet-ff" }
starknet-crypto = { version = "0.6.1", path = "../../starknet-crypto" }
console_error_panic_hook = { version = "0.1.7", optional = true }
wasm-bindgen = "0.2.84"
2 changes: 1 addition & 1 deletion starknet-accounts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["ethereum", "starknet", "web3"]
exclude = ["test-data/**"]

[dependencies]
starknet-core = { version = "0.7.0", path = "../starknet-core" }
starknet-core = { version = "0.7.2", path = "../starknet-core" }
starknet-providers = { version = "0.7.0", path = "../starknet-providers" }
starknet-signers = { version = "0.5.0", path = "../starknet-signers" }
async-trait = "0.1.68"
Expand Down
2 changes: 1 addition & 1 deletion starknet-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ keywords = ["ethereum", "starknet", "web3"]
exclude = ["test-data/**"]

[dependencies]
starknet-core = { version = "0.7.0", path = "../starknet-core" }
starknet-core = { version = "0.7.2", path = "../starknet-core" }
starknet-providers = { version = "0.7.0", path = "../starknet-providers" }
starknet-accounts = { version = "0.6.0", path = "../starknet-accounts" }
serde = { version = "1.0.160", features = ["derive"] }
Expand Down
4 changes: 2 additions & 2 deletions starknet-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "starknet-core"
version = "0.7.0"
version = "0.7.2"
authors = ["Jonathan LEI <[email protected]>"]
license = "MIT OR Apache-2.0"
edition = "2021"
Expand All @@ -18,7 +18,7 @@ all-features = true

[dependencies]
starknet-crypto = { version = "0.6.1", path = "../starknet-crypto", default-features = false, features = ["alloc"] }
starknet-ff = { version = "0.3.4", path = "../starknet-ff", default-features = false, features = ["serde"] }
starknet-ff = { version = "0.3.5", path = "../starknet-ff", default-features = false, features = ["serde"] }
base64 = { version = "0.21.0", default-features = false, features = ["alloc"] }
flate2 = { version = "1.0.25", optional = true }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
Expand Down
29 changes: 21 additions & 8 deletions starknet-core/src/serde/byte_array.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
pub mod base64 {
use alloc::{format, string::String, vec::Vec};
use alloc::{fmt::Formatter, format, vec::Vec};

use base64::{engine::general_purpose::STANDARD, Engine};
use serde::{Deserialize, Deserializer, Serializer};
use serde::{de::Visitor, Deserializer, Serializer};

struct Base64Visitor;

pub fn serialize<S, T>(value: T, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -16,12 +18,23 @@ pub mod base64 {
where
D: Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
match STANDARD.decode(value) {
Ok(value) => Ok(value),
Err(err) => Err(serde::de::Error::custom(format!(
"invalid base64 string: {err}"
))),
deserializer.deserialize_any(Base64Visitor)
}

impl<'de> Visitor<'de> for Base64Visitor {
type Value = Vec<u8>;

fn expecting(&self, formatter: &mut Formatter) -> alloc::fmt::Result {
write!(formatter, "string")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
STANDARD
.decode(v)
.map_err(|err| serde::de::Error::custom(format!("invalid base64 string: {err}")))
}
}
}
28 changes: 20 additions & 8 deletions starknet-core/src/serde/num_hex.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod u64 {
use alloc::{format, string::String};
use alloc::{fmt::Formatter, format};

use serde::{Deserialize, Deserializer, Serializer};
use serde::{de::Visitor, Deserializer, Serializer};

struct NumHexVisitor;

pub fn serialize<S>(value: &u64, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -14,12 +16,22 @@ pub mod u64 {
where
D: Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
match u64::from_str_radix(value.trim_start_matches("0x"), 16) {
Ok(value) => Ok(value),
Err(err) => Err(serde::de::Error::custom(format!(
"invalid u64 hex string: {err}"
))),
deserializer.deserialize_any(NumHexVisitor)
}

impl<'de> Visitor<'de> for NumHexVisitor {
type Value = u64;

fn expecting(&self, formatter: &mut Formatter) -> alloc::fmt::Result {
write!(formatter, "string")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
u64::from_str_radix(v.trim_start_matches("0x"), 16)
.map_err(|err| serde::de::Error::custom(format!("invalid u64 hex string: {err}")))
}
}
}
74 changes: 61 additions & 13 deletions starknet-core/src/serde/unsigned_field_element.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use alloc::{format, string::String};
use alloc::{fmt::Formatter, format};

use serde::{de::Error as DeError, Deserialize, Deserializer, Serializer};
use serde::{
de::{Error as DeError, Visitor},
Deserializer, Serializer,
};
use serde_with::{DeserializeAs, SerializeAs};

use crate::types::FieldElement;
Expand All @@ -11,6 +14,10 @@ pub struct UfeHexOption;

pub struct UfePendingBlockHash;

struct UfeHexVisitor;
struct UfeHexOptionVisitor;
struct UfePendingBlockHashVisitor;

impl SerializeAs<FieldElement> for UfeHex {
fn serialize_as<S>(value: &FieldElement, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -25,11 +32,23 @@ impl<'de> DeserializeAs<'de, FieldElement> for UfeHex {
where
D: Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
match FieldElement::from_hex_be(&value) {
Ok(value) => Ok(value),
Err(err) => Err(DeError::custom(format!("invalid hex string: {err}"))),
}
deserializer.deserialize_any(UfeHexVisitor)
}
}

impl<'de> Visitor<'de> for UfeHexVisitor {
type Value = FieldElement;

fn expecting(&self, formatter: &mut Formatter) -> alloc::fmt::Result {
write!(formatter, "string")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: DeError,
{
FieldElement::from_hex_be(v)
.map_err(|err| DeError::custom(format!("invalid hex string: {err}")))
}
}

Expand All @@ -50,10 +69,24 @@ impl<'de> DeserializeAs<'de, Option<FieldElement>> for UfeHexOption {
where
D: Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
match value.as_str() {
deserializer.deserialize_any(UfeHexOptionVisitor)
}
}

impl<'de> Visitor<'de> for UfeHexOptionVisitor {
type Value = Option<FieldElement>;

fn expecting(&self, formatter: &mut Formatter) -> alloc::fmt::Result {
write!(formatter, "string")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: DeError,
{
match v {
"" => Ok(None),
_ => match FieldElement::from_hex_be(&value) {
_ => match FieldElement::from_hex_be(v) {
Ok(value) => Ok(Some(value)),
Err(err) => Err(DeError::custom(format!("invalid hex string: {err}"))),
},
Expand All @@ -79,11 +112,25 @@ impl<'de> DeserializeAs<'de, Option<FieldElement>> for UfePendingBlockHash {
where
D: Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
if value.is_empty() || value == "pending" || value == "None" {
deserializer.deserialize_any(UfePendingBlockHashVisitor)
}
}

impl<'de> Visitor<'de> for UfePendingBlockHashVisitor {
type Value = Option<FieldElement>;

fn expecting(&self, formatter: &mut Formatter) -> alloc::fmt::Result {
write!(formatter, "string")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: DeError,
{
if v.is_empty() || v == "pending" || v == "None" {
Ok(None)
} else {
match FieldElement::from_hex_be(&value) {
match FieldElement::from_hex_be(v) {
Ok(value) => Ok(Some(value)),
Err(err) => Err(DeError::custom(format!("invalid hex string: {err}"))),
}
Expand All @@ -95,6 +142,7 @@ impl<'de> DeserializeAs<'de, Option<FieldElement>> for UfePendingBlockHash {
mod tests {
use super::*;

use serde::Deserialize;
use serde_with::serde_as;

#[serde_as]
Expand Down
25 changes: 20 additions & 5 deletions starknet-core/src/types/eth_address.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloc::{format, string::String};
use alloc::{fmt::Formatter, format};
use core::str::FromStr;

use serde::{Deserialize, Serialize};
use serde::{de::Visitor, Deserialize, Serialize};
use starknet_ff::FieldElement;

// 0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF
Expand All @@ -17,6 +17,8 @@ pub struct EthAddress {
inner: [u8; 20],
}

struct EthAddressVisitor;

mod errors {
use core::fmt::{Display, Formatter, Result};

Expand Down Expand Up @@ -84,9 +86,22 @@ impl<'de> Deserialize<'de> for EthAddress {
where
D: serde::Deserializer<'de>,
{
let value = String::deserialize(deserializer)?;
value
.parse()
deserializer.deserialize_any(EthAddressVisitor)
}
}

impl<'de> Visitor<'de> for EthAddressVisitor {
type Value = EthAddress;

fn expecting(&self, formatter: &mut Formatter) -> alloc::fmt::Result {
write!(formatter, "string")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
v.parse()
.map_err(|err| serde::de::Error::custom(format!("{}", err)))
}
}
Expand Down
7 changes: 3 additions & 4 deletions starknet-core/src/types/execution_result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,12 @@ impl<'de> Deserialize<'de> for ExecutionResult {

match (raw.execution_status, raw.revert_reason) {
(TransactionExecutionStatus::Succeeded, None) => Ok(Self::Succeeded),
(TransactionExecutionStatus::Reverted, Some(reason)) => Ok(Self::Reverted { reason }),
(TransactionExecutionStatus::Reverted, reason) => Ok(Self::Reverted {
reason: reason.unwrap_or_default(),
}),
(TransactionExecutionStatus::Succeeded, Some(_)) => Err(serde::de::Error::custom(
"field `revert_reason` must not exist when `execution_status` is `SUCCEEDED`",
)),
(TransactionExecutionStatus::Reverted, None) => Err(serde::de::Error::custom(
"field `revert_reason` missing when `execution_status` is `REVERTED`",
)),
}
}
}
Loading

0 comments on commit b3858e8

Please sign in to comment.