newton_cli/commands/
policy_client.rs

1use alloy::primitives::{Address, Bytes};
2use clap::{Parser, Subcommand};
3use eyre::Context;
4use newton_prover_chainio::policy_client::PolicyClientController;
5use newton_prover_core::config::NewtonAvsConfig;
6use std::path::PathBuf;
7use tracing;
8
9use crate::config::NewtonCliConfig;
10
11/// Policy client commands
12#[derive(Debug, Parser)]
13#[command(name = "policy-client")]
14pub struct PolicyClientCommand {
15    #[command(subcommand)]
16    pub subcommand: PolicyClientSubcommand,
17}
18
19#[derive(Debug, Subcommand)]
20pub enum PolicyClientSubcommand {
21    /// Set policy parameters for a policy client
22    #[command(name = "set-policy-params")]
23    SetPolicyParams(SetPolicyParamsCommand),
24}
25
26/// Set policy parameters command
27#[derive(Debug, Parser)]
28pub struct SetPolicyParamsCommand {
29    /// Policy client address
30    #[arg(long)]
31    policy_client: Address,
32
33    /// Path to JSON file containing policy parameters
34    #[arg(long)]
35    policy_params: PathBuf,
36
37    /// Expire after (in blocks)
38    #[arg(long)]
39    expire_after: u32,
40
41    #[arg(long, env = "PRIVATE_KEY")]
42    private_key: Option<String>,
43
44    #[arg(long, env = "RPC_URL")]
45    rpc_url: Option<String>,
46}
47
48impl PolicyClientCommand {
49    /// Execute the policy-client command
50    pub async fn execute(self: Box<Self>, _config: NewtonAvsConfig<NewtonCliConfig>) -> eyre::Result<()> {
51        match self.subcommand {
52            PolicyClientSubcommand::SetPolicyParams(cmd) => {
53                let private_key = cmd
54                    .private_key
55                    .ok_or_else(|| eyre::eyre!("PRIVATE_KEY is required (set via --private-key or env var)"))?;
56                let rpc_url = cmd
57                    .rpc_url
58                    .ok_or_else(|| eyre::eyre!("RPC_URL is required (set via --rpc-url or env var)"))?;
59
60                tracing::info!("Setting policy params for policy client: {}", cmd.policy_client);
61
62                // Read policy params JSON file
63                let policy_params_str = std::fs::read_to_string(&cmd.policy_params)
64                    .with_context(|| format!("Failed to read policy params file: {:?}", cmd.policy_params))?;
65
66                // Validate it's valid JSON
67                let _policy_params_json: serde_json::Value = serde_json::from_str(&policy_params_str)
68                    .with_context(|| format!("Failed to parse policy params as JSON: {:?}", cmd.policy_params))?;
69
70                // Convert to bytes
71                let policy_params_bytes = Bytes::copy_from_slice(policy_params_str.as_bytes());
72
73                // Create controller and set policy
74                let controller = PolicyClientController::new(private_key, rpc_url, cmd.policy_client);
75                let (receipt, policy_id) = controller
76                    .set_policy(policy_params_bytes, cmd.expire_after)
77                    .await
78                    .map_err(|e| eyre::eyre!("Failed to set policy: {}", e))?;
79
80                tracing::info!("Policy set successfully!");
81                tracing::info!("Transaction hash: {:?}", receipt.transaction_hash);
82                tracing::info!("Policy ID: {:?}", policy_id);
83
84                Ok(())
85            }
86        }
87    }
88}