snarkvm_synthesizer_process/stack/
authorize.rs1use super::*;
17use snarkvm_synthesizer_error::*;
18
19impl<N: Network> Stack<N> {
20 #[inline]
22 pub fn authorize<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
23 &self,
24 private_key: &PrivateKey<N>,
25 function_name: impl TryInto<Identifier<N>>,
26 inputs: impl ExactSizeIterator<Item = impl TryInto<Value<N>>>,
27 rng: &mut R,
28 ) -> Result<Authorization<N>, StackAuthError> {
29 let timer = timer!("Stack::authorize");
30
31 let program_id = *self.program.id();
33 let function_name = function_name.try_into().map_err(|_| anyhow!("Invalid function name"))?;
35 let input_types = self.get_function(&function_name)?.input_types();
37 lap!(timer, "Retrieve the input types");
38 let is_root = true;
40 let program_checksum = match self.program().contains_constructor() {
42 true => Some(self.program_checksum_as_field()?),
43 false => None,
44 };
45
46 let caller = None;
48 let root_tvk = None;
50 let request = Request::sign(
52 private_key,
53 program_id,
54 function_name,
55 inputs,
56 &input_types,
57 root_tvk,
58 is_root,
59 program_checksum,
60 false,
61 rng,
62 )?;
63 lap!(timer, "Compute the request");
64 let authorization = Authorization::new(request.clone());
66 let call_stack = CallStack::Authorize(vec![request], Some(*private_key), authorization.clone());
68 let _response = self.execute_function::<A, R>(call_stack, caller, root_tvk, rng)?;
70 finish!(timer, "Construct the authorization from the function");
71
72 Ok(authorization)
74 }
75
76 #[inline]
79 pub fn authorize_unchecked<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
80 &self,
81 private_key: &PrivateKey<N>,
82 function_name: impl TryInto<Identifier<N>>,
83 inputs: impl ExactSizeIterator<Item = impl TryInto<Value<N>>>,
84 rng: &mut R,
85 ) -> Result<Authorization<N>, StackAuthError> {
86 let timer = timer!("Stack::authorize_unchecked");
87
88 let program_id = *self.program.id();
90 let function_name = function_name.try_into().map_err(|_| anyhow!("Invalid function name"))?;
92 let input_types = self.get_function(&function_name)?.input_types();
94 lap!(timer, "Retrieve the input types");
95 let is_root = true;
97
98 let caller = None;
100 let root_tvk = None;
102 let program_checksum = match self.program().contains_constructor() {
104 true => Some(self.program_checksum_as_field()?),
105 false => None,
106 };
107 let request = Request::sign(
109 private_key,
110 program_id,
111 function_name,
112 inputs,
113 &input_types,
114 root_tvk,
115 is_root,
116 program_checksum,
117 false,
118 rng,
119 )?;
120 lap!(timer, "Compute the request");
121 let authorization = Authorization::new(request.clone());
123 let call_stack = CallStack::Authorize(vec![request], Some(*private_key), authorization.clone());
125 let _response = self.evaluate_function::<A, R>(call_stack, caller, root_tvk, rng)?;
127 finish!(timer, "Construct the authorization from the function");
128
129 Ok(authorization)
131 }
132
133 #[inline]
142 pub fn sample_authorization<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
143 &self,
144 address: Address<A::Network>,
145 program_id: ProgramID<A::Network>,
146 function_name: Identifier<A::Network>,
147 inputs: impl ExactSizeIterator<Item = impl TryInto<Value<A::Network>>>,
148 rng: &mut R,
149 ) -> Result<Authorization<N>, StackAuthError> {
150 let timer = timer!("Stack::sample_authorization");
151
152 if program_id != *self.program.id() {
153 return Err(anyhow!("Program ID mismatch").into());
154 }
155
156 let program_id = *self.program.id();
158 let input_types = self.get_function(&function_name)?.input_types();
160 lap!(timer, "Retrieve the input types");
161
162 let caller = None;
164 let root_tvk = None;
166
167 let mocked_request = Request::sample(address, program_id, function_name, inputs, &input_types, false, rng)?;
169
170 lap!(timer, "Compute the mocked request");
171 let authorization = Authorization::new(mocked_request.clone());
173 let call_stack = CallStack::AuthorizeMocked(vec![mocked_request], address, authorization.clone());
175 let _response = self.evaluate_function::<A, R>(call_stack, caller, root_tvk, rng)?;
177 finish!(timer, "Construct the mocked authorization from the function");
178
179 Ok(authorization)
181 }
182
183 #[inline]
186 pub fn authorize_request<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
187 &self,
188 request: Request<N>,
189 rng: &mut R,
190 ) -> Result<Authorization<N>, StackAuthError> {
191 let timer = timer!("Stack::authorize_request");
192
193 let authorization = Authorization::new(request.clone());
195 let call_stack = CallStack::Authorize(vec![request], None, authorization.clone());
197 let caller = None;
199 let root_tvk = None;
201 let _response = self.evaluate_function::<A, R>(call_stack, caller, root_tvk, rng)?;
203 finish!(timer, "Construct the authorization from the function");
204
205 Ok(authorization)
207 }
208}