From 8f9960f377e318221f479744b0405b0c0f415c4e Mon Sep 17 00:00:00 2001 From: aniket-prajapati Date: Thu, 13 Jun 2024 14:54:18 +0530 Subject: [PATCH 1/2] added verifier contract for l3 interface --- Cargo.lock | 2 +- Cargo.toml | 2 +- Makefile | 2 +- .../Cargo.toml | 2 +- .../src/clients/mod.rs | 12 +++- .../src/interfaces/core_contract.rs | 0 .../src/interfaces/messaging.rs | 0 .../src/interfaces/mod.rs | 1 + .../src/interfaces/operator.rs | 0 .../src/interfaces/verifier_contract.rs | 61 +++++++++++++++++++ .../src/lib.rs | 0 11 files changed, 76 insertions(+), 6 deletions(-) rename crates/l3/{appchain-core-contract-client => appchain-client}/Cargo.toml (95%) rename crates/l3/{appchain-core-contract-client => appchain-client}/src/clients/mod.rs (76%) rename crates/l3/{appchain-core-contract-client => appchain-client}/src/interfaces/core_contract.rs (100%) rename crates/l3/{appchain-core-contract-client => appchain-client}/src/interfaces/messaging.rs (100%) rename crates/l3/{appchain-core-contract-client => appchain-client}/src/interfaces/mod.rs (68%) rename crates/l3/{appchain-core-contract-client => appchain-client}/src/interfaces/operator.rs (100%) create mode 100644 crates/l3/appchain-client/src/interfaces/verifier_contract.rs rename crates/l3/{appchain-core-contract-client => appchain-client}/src/lib.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 0114213..14c0ce8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -63,7 +63,7 @@ dependencies = [ ] [[package]] -name = "appchain-core-contract-client" +name = "appchain-client" version = "0.1.0" dependencies = [ "appchain-utils", diff --git a/Cargo.toml b/Cargo.toml index 49eb0f5..daee00e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ resolver = "2" members = [ "crates/l2/starknet-core-contract-client", - "crates/l2/ethereum-instance", "crates/l2/starknet-eth-bridge-client", "crates/l2/starknet-token-bridge-client", "crates/l2/starkgate-manager-client", "crates/l2/utils", "crates/l2/starknet-erc20-client", "crates/l2/starkgate-registry-client", "crates/l2/starknet-proxy-client", "crates/l3/appchain-core-contract-client", "crates/l3/starknet-instance", "crates/l3/appchain-utils" + "crates/l2/ethereum-instance", "crates/l2/starknet-eth-bridge-client", "crates/l2/starknet-token-bridge-client", "crates/l2/starkgate-manager-client", "crates/l2/utils", "crates/l2/starknet-erc20-client", "crates/l2/starkgate-registry-client", "crates/l2/starknet-proxy-client", "crates/l3/appchain-client", "crates/l3/starknet-instance", "crates/l3/appchain-utils" ] [workspace.package] diff --git a/Makefile b/Makefile index dc96074..8c1fbc8 100644 --- a/Makefile +++ b/Makefile @@ -11,4 +11,4 @@ artifacts: forge build cp out/UnsafeProxy.sol/UnsafeProxy.json crates/starknet-proxy-client/src/artifacts/ cp out/StarknetSovereign.sol/Starknet.json crates/starknet-core-contract-client/src/artifacts/ - (cd lib/piltover && scarb build && cp target/dev/* ../../crates/l3/appchain-core-contract-client/artifacts) \ No newline at end of file + (cd lib/piltover && scarb build && cp target/dev/* ../../crates/l3/appchain-client/artifacts) \ No newline at end of file diff --git a/crates/l3/appchain-core-contract-client/Cargo.toml b/crates/l3/appchain-client/Cargo.toml similarity index 95% rename from crates/l3/appchain-core-contract-client/Cargo.toml rename to crates/l3/appchain-client/Cargo.toml index c5cade9..086b373 100644 --- a/crates/l3/appchain-core-contract-client/Cargo.toml +++ b/crates/l3/appchain-client/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "appchain-core-contract-client" +name = "appchain-client" edition.workspace = true version.workspace = true authors.workspace = true diff --git a/crates/l3/appchain-core-contract-client/src/clients/mod.rs b/crates/l3/appchain-client/src/clients/mod.rs similarity index 76% rename from crates/l3/appchain-core-contract-client/src/clients/mod.rs rename to crates/l3/appchain-client/src/clients/mod.rs index b69cfb6..0903409 100644 --- a/crates/l3/appchain-core-contract-client/src/clients/mod.rs +++ b/crates/l3/appchain-client/src/clients/mod.rs @@ -1,14 +1,15 @@ use crate::interfaces::core_contract::CoreContract; use crate::interfaces::messaging::Messaging; use crate::interfaces::operator::Operator; -use starknet_ff::FieldElement; - +use crate::interfaces::verifier_contract::VerifierContract; use appchain_utils::LocalWalletSignerMiddleware; +use starknet_ff::FieldElement; pub struct StarknetCoreContractClient<'a> { operator: Operator<'a>, messaging: Messaging<'a>, core_contract: CoreContract<'a>, + verifier_contract: VerifierContract<'a>, } impl<'a> StarknetCoreContractClient<'a> { @@ -17,6 +18,7 @@ impl<'a> StarknetCoreContractClient<'a> { operator: Operator::new(address, signer), messaging: Messaging::new(address, signer), core_contract: CoreContract::new(address, signer), + verifier_contract: VerifierContract::new(address, signer), } } } @@ -38,3 +40,9 @@ impl<'a> AsRef> for StarknetCoreContractClient<'a> { &self.core_contract } } + +impl<'a> AsRef> for StarknetCoreContractClient<'a> { + fn as_ref(&self) -> &VerifierContract<'a> { + &self.verifier_contract + } +} diff --git a/crates/l3/appchain-core-contract-client/src/interfaces/core_contract.rs b/crates/l3/appchain-client/src/interfaces/core_contract.rs similarity index 100% rename from crates/l3/appchain-core-contract-client/src/interfaces/core_contract.rs rename to crates/l3/appchain-client/src/interfaces/core_contract.rs diff --git a/crates/l3/appchain-core-contract-client/src/interfaces/messaging.rs b/crates/l3/appchain-client/src/interfaces/messaging.rs similarity index 100% rename from crates/l3/appchain-core-contract-client/src/interfaces/messaging.rs rename to crates/l3/appchain-client/src/interfaces/messaging.rs diff --git a/crates/l3/appchain-core-contract-client/src/interfaces/mod.rs b/crates/l3/appchain-client/src/interfaces/mod.rs similarity index 68% rename from crates/l3/appchain-core-contract-client/src/interfaces/mod.rs rename to crates/l3/appchain-client/src/interfaces/mod.rs index 828004c..c8bb437 100644 --- a/crates/l3/appchain-core-contract-client/src/interfaces/mod.rs +++ b/crates/l3/appchain-client/src/interfaces/mod.rs @@ -1,3 +1,4 @@ pub mod core_contract; pub mod messaging; pub mod operator; +pub mod verifier_contract; diff --git a/crates/l3/appchain-core-contract-client/src/interfaces/operator.rs b/crates/l3/appchain-client/src/interfaces/operator.rs similarity index 100% rename from crates/l3/appchain-core-contract-client/src/interfaces/operator.rs rename to crates/l3/appchain-client/src/interfaces/operator.rs diff --git a/crates/l3/appchain-client/src/interfaces/verifier_contract.rs b/crates/l3/appchain-client/src/interfaces/verifier_contract.rs new file mode 100644 index 0000000..70c8ac4 --- /dev/null +++ b/crates/l3/appchain-client/src/interfaces/verifier_contract.rs @@ -0,0 +1,61 @@ +use appchain_utils::LocalWalletSignerMiddleware; +use appchain_utils::{call_contract, invoke_contract}; +use color_eyre::eyre::eyre; +use color_eyre::Result; +use starknet_accounts::ConnectedAccount; +use starknet_core::types::{FieldElement, InvokeTransactionResult}; +use starknet_providers::jsonrpc::HttpTransport; +use starknet_providers::JsonRpcClient; + +pub struct VerifierContract<'a> { + signer: &'a LocalWalletSignerMiddleware, + address: FieldElement, +} + +impl<'a> VerifierContract<'a> { + pub fn new(address: FieldElement, signer: &'a LocalWalletSignerMiddleware) -> Self { + Self { signer, address } + } + + fn provider(&self) -> &JsonRpcClient { + self.signer.provider() + } + pub async fn verify_and_register_fact( + &self, + serialized_proof: Vec, + ) -> Result { + invoke_contract( + self.signer, + self.address, + "verify_and_register_fact", + serialized_proof, + ) + .await + } + + pub async fn verify_and_register_fact_from_contract( + &self, + contract_address: FieldElement, + ) -> Result { + let mut calldata = Vec::new(); + calldata.push(contract_address); + + invoke_contract( + self.signer, + self.address, + "verify_and_register_fact_from_contract", + vec![contract_address], + ) + .await + } + + pub async fn is_valid(&self, fact: FieldElement) -> Result { + let provider = self.provider(); + let values = call_contract(provider, self.address, "is_valid", vec![fact]).await?; + + values + .first() + .map(|value| *value != FieldElement::ZERO) + .ok_or_else(|| eyre!("Contract error: expected at least one return value")) + } +} diff --git a/crates/l3/appchain-core-contract-client/src/lib.rs b/crates/l3/appchain-client/src/lib.rs similarity index 100% rename from crates/l3/appchain-core-contract-client/src/lib.rs rename to crates/l3/appchain-client/src/lib.rs From 4ecf9fb3fe11c162a607013d9b1d3e75ab89154c Mon Sep 17 00:00:00 2001 From: aniket-prajapati Date: Thu, 13 Jun 2024 16:45:25 +0530 Subject: [PATCH 2/2] updated structure for verifier contract --- Cargo.lock | 74 ++++++++++++------- Cargo.toml | 4 +- Makefile | 2 +- .../l3/appchain-client/src/interfaces/mod.rs | 4 - .../Cargo.toml | 2 +- .../src/clients/mod.rs | 9 --- .../src/interfaces/core_contract.rs | 0 .../src/interfaces/messaging.rs | 0 .../src/interfaces/mod.rs | 3 + .../src/interfaces/operator.rs | 0 .../src/lib.rs | 0 crates/l3/verifier-contract-client/Cargo.toml | 24 ++++++ .../src/clients/mod.rs | 21 ++++++ .../src/interfaces/mod.rs | 1 + .../src/interfaces/verifier_contract.rs | 0 crates/l3/verifier-contract-client/src/lib.rs | 20 +++++ 16 files changed, 121 insertions(+), 43 deletions(-) delete mode 100644 crates/l3/appchain-client/src/interfaces/mod.rs rename crates/l3/{appchain-client => core-contract-client}/Cargo.toml (96%) rename crates/l3/{appchain-client => core-contract-client}/src/clients/mod.rs (76%) rename crates/l3/{appchain-client => core-contract-client}/src/interfaces/core_contract.rs (100%) rename crates/l3/{appchain-client => core-contract-client}/src/interfaces/messaging.rs (100%) create mode 100644 crates/l3/core-contract-client/src/interfaces/mod.rs rename crates/l3/{appchain-client => core-contract-client}/src/interfaces/operator.rs (100%) rename crates/l3/{appchain-client => core-contract-client}/src/lib.rs (100%) create mode 100644 crates/l3/verifier-contract-client/Cargo.toml create mode 100644 crates/l3/verifier-contract-client/src/clients/mod.rs create mode 100644 crates/l3/verifier-contract-client/src/interfaces/mod.rs rename crates/l3/{appchain-client => verifier-contract-client}/src/interfaces/verifier_contract.rs (100%) create mode 100644 crates/l3/verifier-contract-client/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 14c0ce8..37270d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -62,32 +62,6 @@ dependencies = [ "libc", ] -[[package]] -name = "appchain-client" -version = "0.1.0" -dependencies = [ - "appchain-utils", - "async-trait", - "color-eyre", - "dirs", - "hex", - "log", - "num-traits", - "serde_json", - "starknet", - "starknet-accounts 0.6.1", - "starknet-contract 0.6.0", - "starknet-core 0.7.2", - "starknet-crypto 0.6.1", - "starknet-ff 0.3.5", - "starknet-instance", - "starknet-providers 0.7.0", - "starknet-signers 0.5.0", - "thiserror", - "tokio", - "url", -] - [[package]] name = "appchain-utils" version = "0.1.0" @@ -566,6 +540,32 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" +[[package]] +name = "core-contract-client" +version = "0.1.0" +dependencies = [ + "appchain-utils", + "async-trait", + "color-eyre", + "dirs", + "hex", + "log", + "num-traits", + "serde_json", + "starknet", + "starknet-accounts 0.6.1", + "starknet-contract 0.6.0", + "starknet-core 0.7.2", + "starknet-crypto 0.6.1", + "starknet-ff 0.3.5", + "starknet-instance", + "starknet-providers 0.7.0", + "starknet-signers 0.5.0", + "thiserror", + "tokio", + "url", +] + [[package]] name = "core-foundation" version = "0.9.4" @@ -4050,6 +4050,28 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "verifier-contract-client" +version = "0.1.0" +dependencies = [ + "appchain-utils", + "async-trait", + "color-eyre", + "dirs", + "hex", + "log", + "num-traits", + "serde_json", + "starknet", + "starknet-accounts 0.6.1", + "starknet-core 0.7.2", + "starknet-ff 0.3.5", + "starknet-providers 0.7.0", + "thiserror", + "tokio", + "url", +] + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index daee00e..0f559dc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,8 +2,8 @@ resolver = "2" members = [ "crates/l2/starknet-core-contract-client", - "crates/l2/ethereum-instance", "crates/l2/starknet-eth-bridge-client", "crates/l2/starknet-token-bridge-client", "crates/l2/starkgate-manager-client", "crates/l2/utils", "crates/l2/starknet-erc20-client", "crates/l2/starkgate-registry-client", "crates/l2/starknet-proxy-client", "crates/l3/appchain-client", "crates/l3/starknet-instance", "crates/l3/appchain-utils" -] + "crates/l2/ethereum-instance", "crates/l2/starknet-eth-bridge-client", "crates/l2/starknet-token-bridge-client", "crates/l2/starkgate-manager-client", "crates/l2/utils", "crates/l2/starknet-erc20-client", "crates/l2/starkgate-registry-client", "crates/l2/starknet-proxy-client", "crates/l3/core-contract-client", "crates/l3/starknet-instance", "crates/l3/appchain-utils" +, "crates/l3/verifier-contract-client"] [workspace.package] authors = ["Starkware Exploration Team", "OnlyDust Contributors"] diff --git a/Makefile b/Makefile index 8c1fbc8..18bcd89 100644 --- a/Makefile +++ b/Makefile @@ -11,4 +11,4 @@ artifacts: forge build cp out/UnsafeProxy.sol/UnsafeProxy.json crates/starknet-proxy-client/src/artifacts/ cp out/StarknetSovereign.sol/Starknet.json crates/starknet-core-contract-client/src/artifacts/ - (cd lib/piltover && scarb build && cp target/dev/* ../../crates/l3/appchain-client/artifacts) \ No newline at end of file + (cd lib/piltover && scarb build && cp target/dev/* ../../crates/l3/core-contract-client/artifacts) \ No newline at end of file diff --git a/crates/l3/appchain-client/src/interfaces/mod.rs b/crates/l3/appchain-client/src/interfaces/mod.rs deleted file mode 100644 index c8bb437..0000000 --- a/crates/l3/appchain-client/src/interfaces/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -pub mod core_contract; -pub mod messaging; -pub mod operator; -pub mod verifier_contract; diff --git a/crates/l3/appchain-client/Cargo.toml b/crates/l3/core-contract-client/Cargo.toml similarity index 96% rename from crates/l3/appchain-client/Cargo.toml rename to crates/l3/core-contract-client/Cargo.toml index 086b373..0630ad5 100644 --- a/crates/l3/appchain-client/Cargo.toml +++ b/crates/l3/core-contract-client/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "appchain-client" +name = "core-contract-client" edition.workspace = true version.workspace = true authors.workspace = true diff --git a/crates/l3/appchain-client/src/clients/mod.rs b/crates/l3/core-contract-client/src/clients/mod.rs similarity index 76% rename from crates/l3/appchain-client/src/clients/mod.rs rename to crates/l3/core-contract-client/src/clients/mod.rs index 0903409..fb8d51a 100644 --- a/crates/l3/appchain-client/src/clients/mod.rs +++ b/crates/l3/core-contract-client/src/clients/mod.rs @@ -1,7 +1,6 @@ use crate::interfaces::core_contract::CoreContract; use crate::interfaces::messaging::Messaging; use crate::interfaces::operator::Operator; -use crate::interfaces::verifier_contract::VerifierContract; use appchain_utils::LocalWalletSignerMiddleware; use starknet_ff::FieldElement; @@ -9,7 +8,6 @@ pub struct StarknetCoreContractClient<'a> { operator: Operator<'a>, messaging: Messaging<'a>, core_contract: CoreContract<'a>, - verifier_contract: VerifierContract<'a>, } impl<'a> StarknetCoreContractClient<'a> { @@ -18,7 +16,6 @@ impl<'a> StarknetCoreContractClient<'a> { operator: Operator::new(address, signer), messaging: Messaging::new(address, signer), core_contract: CoreContract::new(address, signer), - verifier_contract: VerifierContract::new(address, signer), } } } @@ -40,9 +37,3 @@ impl<'a> AsRef> for StarknetCoreContractClient<'a> { &self.core_contract } } - -impl<'a> AsRef> for StarknetCoreContractClient<'a> { - fn as_ref(&self) -> &VerifierContract<'a> { - &self.verifier_contract - } -} diff --git a/crates/l3/appchain-client/src/interfaces/core_contract.rs b/crates/l3/core-contract-client/src/interfaces/core_contract.rs similarity index 100% rename from crates/l3/appchain-client/src/interfaces/core_contract.rs rename to crates/l3/core-contract-client/src/interfaces/core_contract.rs diff --git a/crates/l3/appchain-client/src/interfaces/messaging.rs b/crates/l3/core-contract-client/src/interfaces/messaging.rs similarity index 100% rename from crates/l3/appchain-client/src/interfaces/messaging.rs rename to crates/l3/core-contract-client/src/interfaces/messaging.rs diff --git a/crates/l3/core-contract-client/src/interfaces/mod.rs b/crates/l3/core-contract-client/src/interfaces/mod.rs new file mode 100644 index 0000000..3bae6a7 --- /dev/null +++ b/crates/l3/core-contract-client/src/interfaces/mod.rs @@ -0,0 +1,3 @@ +pub mod core_contract; +pub mod messaging; +pub mod operator; \ No newline at end of file diff --git a/crates/l3/appchain-client/src/interfaces/operator.rs b/crates/l3/core-contract-client/src/interfaces/operator.rs similarity index 100% rename from crates/l3/appchain-client/src/interfaces/operator.rs rename to crates/l3/core-contract-client/src/interfaces/operator.rs diff --git a/crates/l3/appchain-client/src/lib.rs b/crates/l3/core-contract-client/src/lib.rs similarity index 100% rename from crates/l3/appchain-client/src/lib.rs rename to crates/l3/core-contract-client/src/lib.rs diff --git a/crates/l3/verifier-contract-client/Cargo.toml b/crates/l3/verifier-contract-client/Cargo.toml new file mode 100644 index 0000000..a38e97e --- /dev/null +++ b/crates/l3/verifier-contract-client/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "verifier-contract-client" +authors.workspace = true +edition.workspace = true +repository.workspace = true +version.workspace = true + +[dependencies] +starknet = { workspace = true } +dirs = { workspace = true } +serde_json = { workspace = true } +thiserror = { workspace = true } +hex = { workspace = true } +appchain-utils = { path = "../appchain-utils" } +tokio = { workspace = true } +log = { workspace = true } +num-traits = { workspace = true } +async-trait = { workspace = true } +starknet-accounts = { workspace = true } +starknet-core = { workspace = true } +starknet-ff = { workspace = true } +starknet-providers = { workspace = true } +url = { workspace = true } +color-eyre = { workspace = true } \ No newline at end of file diff --git a/crates/l3/verifier-contract-client/src/clients/mod.rs b/crates/l3/verifier-contract-client/src/clients/mod.rs new file mode 100644 index 0000000..66183d2 --- /dev/null +++ b/crates/l3/verifier-contract-client/src/clients/mod.rs @@ -0,0 +1,21 @@ +use crate::interfaces::verifier_contract::VerifierContract; +use appchain_utils::LocalWalletSignerMiddleware; +use starknet_ff::FieldElement; + +pub struct StarknetVerifierContractClient<'a> { + verifier_contract: VerifierContract<'a>, +} + +impl<'a> StarknetVerifierContractClient<'a> { + pub fn new(address: FieldElement, signer: &'a LocalWalletSignerMiddleware) -> Self { + Self { + verifier_contract: VerifierContract::new(address, signer), + } + } +} + +impl<'a> AsRef> for StarknetVerifierContractClient<'a> { + fn as_ref(&self) -> &VerifierContract<'a> { + &self.verifier_contract + } +} diff --git a/crates/l3/verifier-contract-client/src/interfaces/mod.rs b/crates/l3/verifier-contract-client/src/interfaces/mod.rs new file mode 100644 index 0000000..750b46e --- /dev/null +++ b/crates/l3/verifier-contract-client/src/interfaces/mod.rs @@ -0,0 +1 @@ +pub mod verifier_contract; diff --git a/crates/l3/appchain-client/src/interfaces/verifier_contract.rs b/crates/l3/verifier-contract-client/src/interfaces/verifier_contract.rs similarity index 100% rename from crates/l3/appchain-client/src/interfaces/verifier_contract.rs rename to crates/l3/verifier-contract-client/src/interfaces/verifier_contract.rs diff --git a/crates/l3/verifier-contract-client/src/lib.rs b/crates/l3/verifier-contract-client/src/lib.rs new file mode 100644 index 0000000..d9945cf --- /dev/null +++ b/crates/l3/verifier-contract-client/src/lib.rs @@ -0,0 +1,20 @@ +pub mod clients; +pub mod interfaces; + +use appchain_utils::deploy_contract; +use appchain_utils::{LocalWalletSignerMiddleware, NO_CONSTRUCTOR_ARG}; +use clients::StarknetVerifierContractClient; +use color_eyre::Result; +use std::path::Path; + + +pub async fn deploy_starknet_verifier_contract<'a>( + signer: &'a LocalWalletSignerMiddleware, + path_to_sierra: &Path, + path_to_casm: &Path, +) -> Result> { + let contract_address = + deploy_contract(signer, path_to_sierra, path_to_casm, NO_CONSTRUCTOR_ARG).await?; + + Ok(StarknetVerifierContractClient::new(contract_address, signer)) +}