use super::*;
use snarkvm_synthesizer_error::*;
impl<N: Network> Stack<N> {
#[inline]
pub fn authorize<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
&self,
private_key: &PrivateKey<N>,
function_name: impl TryInto<Identifier<N>>,
inputs: impl ExactSizeIterator<Item = impl TryInto<Value<N>>>,
rng: &mut R,
) -> Result<Authorization<N>, StackAuthError> {
let timer = timer!("Stack::authorize");
let program_id = *self.program.id();
let function_name = function_name.try_into().map_err(|_| anyhow!("Invalid function name"))?;
let input_types = self.get_function(&function_name)?.input_types();
lap!(timer, "Retrieve the input types");
let is_root = true;
let program_checksum = match self.program().contains_constructor() {
true => Some(self.program_checksum_as_field()?),
false => None,
};
let caller = None;
let root_tvk = None;
let request = Request::sign(
private_key,
program_id,
function_name,
inputs,
&input_types,
root_tvk,
is_root,
program_checksum,
false,
rng,
)?;
lap!(timer, "Compute the request");
let authorization = Authorization::new(request.clone());
let call_stack = CallStack::Authorize(vec![request], Some(*private_key), authorization.clone());
let _response = self.execute_function::<A, R>(call_stack, caller, root_tvk, rng)?;
finish!(timer, "Construct the authorization from the function");
Ok(authorization)
}
#[inline]
pub fn authorize_unchecked<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
&self,
private_key: &PrivateKey<N>,
function_name: impl TryInto<Identifier<N>>,
inputs: impl ExactSizeIterator<Item = impl TryInto<Value<N>>>,
rng: &mut R,
) -> Result<Authorization<N>, StackAuthError> {
let timer = timer!("Stack::authorize_unchecked");
let program_id = *self.program.id();
let function_name = function_name.try_into().map_err(|_| anyhow!("Invalid function name"))?;
let input_types = self.get_function(&function_name)?.input_types();
lap!(timer, "Retrieve the input types");
let is_root = true;
let caller = None;
let root_tvk = None;
let program_checksum = match self.program().contains_constructor() {
true => Some(self.program_checksum_as_field()?),
false => None,
};
let request = Request::sign(
private_key,
program_id,
function_name,
inputs,
&input_types,
root_tvk,
is_root,
program_checksum,
false,
rng,
)?;
lap!(timer, "Compute the request");
let authorization = Authorization::new(request.clone());
let call_stack = CallStack::Authorize(vec![request], Some(*private_key), authorization.clone());
let _response = self.evaluate_function::<A, R>(call_stack, caller, root_tvk, rng)?;
finish!(timer, "Construct the authorization from the function");
Ok(authorization)
}
#[inline]
pub fn authorize_request<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
&self,
request: Request<N>,
rng: &mut R,
) -> Result<Authorization<N>, StackAuthError> {
let timer = timer!("Stack::authorize_request");
let authorization = Authorization::new(request.clone());
let call_stack = CallStack::Authorize(vec![request], None, authorization.clone());
let caller = None;
let root_tvk = None;
let _response = self.evaluate_function::<A, R>(call_stack, caller, root_tvk, rng)?;
finish!(timer, "Construct the authorization from the function");
Ok(authorization)
}
}