Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: starknet-types-macros adding felt!() #90

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"crates/starknet-types-core",
"crates/starknet-types-rpc",
"crates/starknet-types-macros",
]

exclude = ["ensure_no_std", "fuzz"]
Expand Down
21 changes: 21 additions & 0 deletions crates/starknet-types-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "starknet-types-macros"
version = "0.1.5"
edition = "2021"
license = "MIT"
homepage = "https://github.com/starknet-io/types-rs"
repository = "https://github.com/starknet-io/types-rs"
categories = ["mathematics", "cryptography"]
keywords = ["stark", "zkp", "cairo"]
description = "Macros types representation for Starknet"
readme = "README.md"

[lib]
proc-macro = true

[dependencies]
starknet-types-core = { path = "../starknet-types-core", default-features = false, features = [
"serde",
] }
syn = { version = "0.15", features = ["full", "extra-traits"] }
quote = "0.6"
24 changes: 24 additions & 0 deletions crates/starknet-types-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use proc_macro::TokenStream;
use quote::quote;
use starknet_types_core::felt::Felt;
use syn::{parse_macro_input, LitStr};

#[proc_macro]
pub fn felt(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as LitStr);

let str_value = input.value();

let felt_value = if str_value.starts_with("0x") {
Felt::from_hex(&str_value).expect("invalid Felt value")
} else {
Felt::from_dec_str(&str_value).expect("invalid Felt value")
};

let felt_bytes = felt_value.to_bytes_be();

quote! {
Felt::from_bytes_be(&[#(#felt_bytes),*])
}
.into()
}
5 changes: 4 additions & 1 deletion crates/starknet-types-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ default = ["std"]
std = ["serde/std", "starknet-types-core/std"]

[dependencies]
starknet-types-core = { path = "../starknet-types-core", default-features = false, features = ["serde"] }
starknet-types-core = { path = "../starknet-types-core", default-features = false, features = [
"serde",
] }
starknet-types-macros = { path = "../starknet-types-macros" }
serde = { version = "1", default-features = false, features = ["derive"] }

[dev-dependencies]
Expand Down
6 changes: 4 additions & 2 deletions crates/starknet-types-rpc/src/custom/block_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ impl<'de, F: Deserialize<'de>> serde::Deserialize<'de> for BlockId<F> {
#[test]
fn block_id_from_hash() {
pub use starknet_types_core::felt::Felt;
use starknet_types_macros::felt;

let s = "{\"block_hash\":\"0x123\"}";
let block_id: BlockId<Felt> = serde_json::from_str(s).unwrap();
assert_eq!(block_id, BlockId::Hash(Felt::from_hex("0x123").unwrap()));
assert_eq!(block_id, BlockId::Hash(felt!("0x123")));
}

#[test]
Expand Down Expand Up @@ -106,8 +107,9 @@ fn block_id_from_pending() {
#[test]
fn block_id_to_hash() {
pub use starknet_types_core::felt::Felt;
use starknet_types_macros::felt;

let block_id = BlockId::Hash(Felt::from_hex("0x123").unwrap());
let block_id = BlockId::Hash(felt!("0x123"));
let s = serde_json::to_string(&block_id).unwrap();
assert_eq!(s, "{\"block_hash\":\"0x123\"}");
}
Expand Down