snarkos_aot/auth/
auth_program.rs

1use anyhow::{bail, Result};
2use clap::Args;
3use snarkvm::{console::program::Locator, synthesizer::Process};
4
5use super::{auth_fee::estimate_cost, query};
6use crate::{Authorization, Key, Network, Value};
7
8#[derive(Debug, Args)]
9pub struct AuthProgramOptions<N: Network> {
10    /// Query to load the program with.
11    #[clap(short, long)]
12    pub query: Option<String>,
13    /// Program ID and function name (eg. credits.aleo/transfer_public)
14    locator: Locator<N>,
15    /// Program inputs (eg. 1u64 5field)
16    #[clap(num_args = 1, value_delimiter = ' ')]
17    inputs: Vec<Value<N>>,
18}
19
20#[derive(Debug, Args)]
21pub struct AuthorizeProgram<N: Network> {
22    #[clap(flatten)]
23    pub key: Key<N>,
24    #[clap(flatten)]
25    pub options: AuthProgramOptions<N>,
26    /// The seed to use for the authorization generation
27    #[clap(long)]
28    pub seed: Option<u64>,
29}
30
31impl<N: Network> AuthorizeProgram<N> {
32    /// Initializes a new authorization.
33    pub fn parse(self) -> Result<(Authorization<N>, u64)> {
34        let private_key = self.key.try_get()?;
35
36        let mut process = Process::load()?;
37        match (self.options.query, self.options.locator.program_id()) {
38            (_, id) if *id == N::credits() => {}
39            (None, id) => {
40                bail!("Query required to authorize non-credits program {}", id);
41            }
42            (Some(query), id) => query::load_program(&mut process, *id, &query)?,
43        };
44
45        let auth = process
46            .get_stack(self.options.locator.program_id())?
47            .authorize::<N::Circuit, _>(
48                &private_key,
49                self.options.locator.resource(),
50                self.options.inputs.iter(),
51                &mut super::rng_from_seed(self.seed),
52            )?;
53
54        let cost = estimate_cost(&process, &auth)?;
55
56        Ok((auth, cost))
57    }
58}