snarkvm_synthesizer_process/stack/authorize.rs
1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use super::*;
17
18impl<N: Network> Stack<N> {
19 /// Authorizes a call to the program function for the given inputs.
20 #[inline]
21 pub fn authorize<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
22 &self,
23 private_key: &PrivateKey<N>,
24 function_name: impl TryInto<Identifier<N>>,
25 inputs: impl ExactSizeIterator<Item = impl TryInto<Value<N>>>,
26 rng: &mut R,
27 ) -> Result<Authorization<N>> {
28 let timer = timer!("Stack::authorize");
29
30 // Get the program ID.
31 let program_id = *self.program.id();
32 // Prepare the function name.
33 let function_name = function_name.try_into().map_err(|_| anyhow!("Invalid function name"))?;
34 // Retrieve the input types.
35 let input_types = self.get_function(&function_name)?.input_types();
36 lap!(timer, "Retrieve the input types");
37 // Set is_root to true.
38 let is_root = true;
39
40 // This is the root request and does not have a caller.
41 let caller = None;
42 // This is the root request and we do not have a root_tvk to pass on.
43 let root_tvk = None;
44 // Compute the request.
45 let request =
46 Request::sign(private_key, program_id, function_name, inputs, &input_types, root_tvk, is_root, rng)?;
47 lap!(timer, "Compute the request");
48 // Initialize the authorization.
49 let authorization = Authorization::new(request.clone());
50 // Construct the call stack.
51 let call_stack = CallStack::Authorize(vec![request], *private_key, authorization.clone());
52 // Construct the authorization from the function.
53 let _response = self.execute_function::<A, R>(call_stack, caller, root_tvk, rng)?;
54 finish!(timer, "Construct the authorization from the function");
55
56 // Return the authorization.
57 Ok(authorization)
58 }
59}