Skip to content

Commit

Permalink
feat(saya): load starknet account from CLI (#2054)
Browse files Browse the repository at this point in the history
* init

* parsing args earlier

* chain id from string

* improve error handling for input parsing

* saya args cleanup

* fix: clippy

---------

Co-authored-by: Mateusz Chudkowski <[email protected]>
Co-authored-by: Mateusz Zając <[email protected]>
Co-authored-by: glihm <[email protected]>
  • Loading branch information
4 people authored Jun 28, 2024
1 parent 2ea37a1 commit 1fcaa7a
Show file tree
Hide file tree
Showing 13 changed files with 195 additions and 77 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions bin/saya/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ katana-primitives.workspace = true
katana-rpc-api.workspace = true
saya-core.workspace = true
serde_json.workspace = true
starknet.workspace = true
tokio.workspace = true
tracing-subscriber.workspace = true
tracing.workspace = true
Expand Down
11 changes: 8 additions & 3 deletions bin/saya/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ cargo run -r --bin sozo -- \
--fee-estimate-multiplier 20 \
--name <WORLD_NAME>
```

Once the migration is done, please take note of the address of the world as it will be re-used in the commands below.

1. Set world configs
Expand Down Expand Up @@ -133,6 +134,7 @@ cargo run -r --bin sozo -- model get Position <ACCOUNT_ADDRESS> \
--rpc-url <SEPOLIA_ENDPOINT> \
--world <WORLD_ADDRESS>
```

```json
// Expected on Sepolia as we've executed the transaction on the Katana shard.
{
Expand Down Expand Up @@ -163,8 +165,7 @@ If not (this includes Apple Silicon), some emulation will take place to run the

It's important that the `--start-block` of Saya is the first block produced by Katana as for now Katana is not fetching events from the forked network.

**IMPORTANT NOTE:**
For now, please add your account address and account private key in `saya/core/src/dojo_os/mod.rs` as those parameters are still not exposed currently. As you are using `cargo run`, it will rebuild with your account configuration before running `saya`.
Starknet sepolia network chain id is `0x00000000000000000000000000000000000000000000534e5f5345504f4c4941`.

```bash
cargo run -r --bin saya -- \
Expand All @@ -173,7 +174,11 @@ cargo run -r --bin saya -- \
--world <WORLD_ADDRESS> \
--url <PROVER_URL> \
--private-key <PROVER_KEY> \
--start-block <FORKED_BLOCK_PLUS_1>
--start-block <FORKED_BLOCK_PLUS_1> \
--starknet-url <STARKNET_URL> \
--chain-id <STARKNET_CHAIN_ID> \
--signer-address <STARKNET_ACCOUNT_SIGNER> \
--signer-key <STARKNET_ACCOUNT_KEY> \
```

After this command, Saya will pick up the blocks with transactions, generate the proof for the state transition, and send it to the base layer world contract.
Expand Down
72 changes: 49 additions & 23 deletions bin/saya/src/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use std::path::PathBuf;
use clap::Parser;
use saya_core::data_availability::celestia::CelestiaConfig;
use saya_core::data_availability::DataAvailabilityConfig;
use saya_core::{ProverAccessKey, SayaConfig};
use saya_core::{ProverAccessKey, SayaConfig, StarknetAccountData};
use starknet::core::utils::cairo_short_string_to_felt;
use starknet_account::StarknetAccountOptions;
use tracing::Subscriber;
use tracing_subscriber::{fmt, EnvFilter};
use url::Url;
Expand All @@ -16,6 +18,7 @@ use crate::args::proof::ProofOptions;

mod data_availability;
mod proof;
mod starknet_account;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
Expand All @@ -28,18 +31,6 @@ pub struct SayaArgs {
#[arg(default_value = "http://localhost:5050")]
pub rpc_url: Url,

/// Specify the Prover URL.
#[arg(long)]
#[arg(value_name = "PROVER URL")]
#[arg(help = "The Prover URL for remote proving.")]
pub url: Url,

/// Specify the Prover Key.
#[arg(long)]
#[arg(value_name = "PROVER KEY")]
#[arg(help = "An authorized prover key for remote proving.")]
pub private_key: String,

#[arg(long)]
#[arg(value_name = "STORE PROOFS")]
#[arg(help = "When enabled all proofs are saved as a file.")]
Expand Down Expand Up @@ -73,6 +64,10 @@ pub struct SayaArgs {
#[command(flatten)]
#[command(next_help_heading = "Choose the proof pipeline configuration")]
pub proof: ProofOptions,

#[command(flatten)]
#[command(next_help_heading = "Starknet account configuration for settlement")]
pub starknet_account: StarknetAccountOptions,
}

impl SayaArgs {
Expand Down Expand Up @@ -135,28 +130,39 @@ impl TryFrom<SayaArgs> for SayaConfig {
None => None,
};

let prover_key = ProverAccessKey::from_hex_string(&args.private_key).map_err(|e| {
Box::new(std::io::Error::new(std::io::ErrorKind::InvalidInput, e.to_string()))
})?;
let starknet_account = StarknetAccountData {
starknet_url: args.starknet_account.starknet_url,
chain_id: cairo_short_string_to_felt(&args.starknet_account.chain_id)?,
signer_address: args.starknet_account.signer_address,
signer_key: args.starknet_account.signer_key,
};

let prover_key =
ProverAccessKey::from_hex_string(&args.proof.private_key).map_err(|e| {
Box::new(std::io::Error::new(std::io::ErrorKind::InvalidInput, e.to_string()))
})?;

Ok(SayaConfig {
katana_rpc: args.rpc_url,
url: args.url,
private_key: prover_key,
prover_url: args.proof.prover_url,
prover_key,
store_proofs: args.store_proofs,
start_block: args.start_block,
batch_size: args.batch_size,
data_availability: da_config,
world_address: args.proof.world_address,
fact_registry_address: args.proof.fact_registry_address,
skip_publishing_proof,
starknet_account,
})
}
}
}

#[cfg(test)]
mod tests {
use katana_primitives::FieldElement;

use super::*;
use crate::args::data_availability::CelestiaOptions;

Expand All @@ -171,9 +177,6 @@ mod tests {
let args = SayaArgs {
config_file: Some(config_file_path.clone()),
rpc_url: Url::parse("http://localhost:5050").unwrap(),
url: Url::parse("http://localhost:5050").unwrap(),
private_key: "0xd0fa91f4949e9a777ebec071ca3ca6acc1f5cd6c6827f123b798f94e73425027"
.into(),
store_proofs: true,
json_log: false,
start_block: 0,
Expand All @@ -190,16 +193,24 @@ mod tests {
proof: ProofOptions {
world_address: Default::default(),
fact_registry_address: Default::default(),
prover_url: Url::parse("http://localhost:5050").unwrap(),
private_key: Default::default(),
},
starknet_account: StarknetAccountOptions {
starknet_url: Url::parse("http://localhost:5030").unwrap(),
chain_id: "SN_SEPOLIA".to_string(),
signer_address: Default::default(),
signer_key: Default::default(),
},
};

let config: SayaConfig = args.try_into().unwrap();

assert_eq!(config.katana_rpc.as_str(), "http://localhost:5050/");
assert_eq!(config.url.as_str(), "http://localhost:1234/");
assert_eq!(config.prover_url.as_str(), "http://localhost:1234/");
assert_eq!(config.batch_size, 4);
assert_eq!(
config.private_key.signing_key_as_hex_string(),
config.prover_key.signing_key_as_hex_string(),
"0xd0fa91f4949e9a777ebec071ca3ca6acc1f5cd6c6827f123b798f94e73425027"
);
assert!(!config.store_proofs);
Expand All @@ -212,5 +223,20 @@ mod tests {
} else {
panic!("Expected Celestia config");
}

let expected = StarknetAccountData {
starknet_url: Url::parse("http://localhost:5030").unwrap(),
chain_id: FieldElement::from_hex_be("0x534e5f5345504f4c4941").unwrap(),
signer_address: FieldElement::from_hex_be(
"0x3aa0a12c62a46a200b1a1211e8cd09b520164104e76d79648ca459cf05db94",
)
.unwrap(),
signer_key: FieldElement::from_hex_be(
"0x6b41bfa82e791a8b4e6b3ee058cb25b89714e4a23bd9a1ad6e6ba0bbc0b145b",
)
.unwrap(),
};

assert_eq!(config.starknet_account, expected);
}
}
11 changes: 11 additions & 0 deletions bin/saya/src/args/proof.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::Args;
use katana_primitives::FieldElement;
use url::Url;

#[derive(Debug, Args, Clone)]
pub struct ProofOptions {
Expand All @@ -10,4 +11,14 @@ pub struct ProofOptions {
#[arg(help = "The address of the Fact Registry contract.")]
#[arg(long = "registry")]
pub fact_registry_address: FieldElement,

#[arg(long)]
#[arg(value_name = "PROVER URL")]
#[arg(help = "The Prover URL for remote proving.")]
pub prover_url: Url,

#[arg(long)]
#[arg(value_name = "PROVER KEY")]
#[arg(help = "An authorized prover key for remote proving.")]
pub private_key: String,
}
28 changes: 28 additions & 0 deletions bin/saya/src/args/starknet_account.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! Data availability options.
use clap::Args;
use katana_primitives::FieldElement;
use url::Url;

#[derive(Debug, Args, Clone)]
pub struct StarknetAccountOptions {
#[arg(long)]
#[arg(env)]
#[arg(help = "The url of the starknet node.")]
pub starknet_url: Url,

#[arg(long)]
#[arg(env)]
#[arg(help = "The chain id of the starknet node.")]
pub chain_id: String,

#[arg(long)]
#[arg(env)]
#[arg(help = "The address of the starknet account.")]
pub signer_address: FieldElement,

#[arg(long)]
#[arg(env)]
#[arg(help = "The private key of the starknet account.")]
pub signer_key: FieldElement,
}
12 changes: 9 additions & 3 deletions bin/saya/src/args/test_saya_config_file.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"katana_rpc": "http://localhost:5050",
"url": "http://localhost:1234",
"private_key": "0xd0fa91f4949e9a777ebec071ca3ca6acc1f5cd6c6827f123b798f94e73425027",
"prover_url": "http://localhost:1234",
"prover_key": "0xd0fa91f4949e9a777ebec071ca3ca6acc1f5cd6c6827f123b798f94e73425027",
"store_proofs": false,
"batch_size": 4,
"world_address": "0x332b8ff41b1b026991fa9b7f0ec352909f8bc33416b65a80527edc988a9b082",
Expand All @@ -16,5 +16,11 @@
}
},
"prover": "Stone",
"verifier": "StoneLocal"
"verifier": "StoneLocal",
"starknet_account": {
"starknet_url": "http://localhost:5030",
"chain_id": "SN_SEPOLIA",
"signer_address": "0x3aa0a12c62a46a200b1a1211e8cd09b520164104e76d79648ca459cf05db94",
"signer_key": "0x6b41bfa82e791a8b4e6b3ee058cb25b89714e4a23bd9a1ad6e6ba0bbc0b145b"
}
}
2 changes: 1 addition & 1 deletion crates/saya/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ thiserror.workspace = true
tokio.workspace = true
tracing.workspace = true
url.workspace = true

once_cell.workspace = true
# TODO: use features for each possible DA.
celestia-rpc = "0.2.0"
celestia-types = "0.2.0"
Expand Down
Loading

0 comments on commit 1fcaa7a

Please sign in to comment.