snarkvm_synthesizer_process/
authorize.rs1use super::*;
17
18use snarkvm_synthesizer_error::*;
19
20impl<N: Network> Process<N> {
21 #[inline]
25 pub fn authorize<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
26 &self,
27 private_key: &PrivateKey<N>,
28 program_id: impl TryInto<ProgramID<N>>,
29 function_name: impl TryInto<Identifier<N>>,
30 inputs: impl ExactSizeIterator<Item = impl TryInto<Value<N>>>,
31 rng: &mut R,
32 ) -> Result<Authorization<N>, ProcessAuthError> {
33 self.get_stack(program_id)?
35 .authorize::<A, R>(private_key, function_name, inputs, rng)
36 .map_err(ProcessAuthError::from)
37 }
38
39 #[inline]
42 pub fn authorize_unchecked<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
43 &self,
44 private_key: &PrivateKey<N>,
45 program_id: impl TryInto<ProgramID<N>>,
46 function_name: impl TryInto<Identifier<N>>,
47 inputs: impl ExactSizeIterator<Item = impl TryInto<Value<N>>>,
48 rng: &mut R,
49 ) -> Result<Authorization<N>, ProcessAuthError> {
50 self.get_stack(program_id)?
52 .authorize_unchecked::<A, R>(private_key, function_name, inputs, rng)
53 .map_err(ProcessAuthError::from)
54 }
55
56 #[inline]
59 pub fn authorize_request<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
60 &self,
61 request: Request<N>,
62 rng: &mut R,
63 ) -> Result<Authorization<N>, ProcessAuthError> {
64 let program_id = request.program_id();
66 self.get_stack(program_id)?.authorize_request::<A, R>(request, rng).map_err(ProcessAuthError::from)
68 }
69
70 #[inline]
73 pub fn authorize_fee_private<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
74 &self,
75 private_key: &PrivateKey<N>,
76 credits: Record<N, Plaintext<N>>,
77 base_fee_in_microcredits: u64,
78 priority_fee_in_microcredits: u64,
79 deployment_or_execution_id: Field<N>,
80 rng: &mut R,
81 ) -> Result<Authorization<N>, ProcessAuthError> {
82 let timer = timer!("Process::authorize_fee_private");
83
84 let program_id = ProgramID::from_str("credits.aleo")?;
86 let function_name = Identifier::from_str("fee_private")?;
88
89 ensure_record_microcredits_is_sufficient(
91 &credits,
92 base_fee_in_microcredits.saturating_add(priority_fee_in_microcredits),
93 )?;
94
95 let inputs = [
97 Value::Record(credits),
98 Value::from(Literal::U64(U64::<N>::new(base_fee_in_microcredits))),
99 Value::from(Literal::U64(U64::<N>::new(priority_fee_in_microcredits))),
100 Value::from(Literal::Field(deployment_or_execution_id)),
101 ]
102 .into_iter();
103 lap!(timer, "Construct the inputs");
104
105 let authorization = self.get_stack(program_id)?.authorize::<A, R>(private_key, function_name, inputs, rng)?;
107 finish!(timer, "Compute the authorization");
108
109 Ok(authorization)
111 }
112
113 #[inline]
115 pub fn authorize_fee_public<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
116 &self,
117 private_key: &PrivateKey<N>,
118 base_fee_in_microcredits: u64,
119 priority_fee_in_microcredits: u64,
120 deployment_or_execution_id: Field<N>,
121 rng: &mut R,
122 ) -> Result<Authorization<N>, ProcessAuthError> {
123 let timer = timer!("Process::authorize_fee_public");
124
125 let program_id = ProgramID::from_str("credits.aleo")?;
127 let function_name = Identifier::from_str("fee_public")?;
129
130 let inputs = [
132 Value::from(Literal::U64(U64::<N>::new(base_fee_in_microcredits))),
133 Value::from(Literal::U64(U64::<N>::new(priority_fee_in_microcredits))),
134 Value::from(Literal::Field(deployment_or_execution_id)),
135 ]
136 .into_iter();
137 lap!(timer, "Construct the inputs");
138
139 let authorization = self.get_stack(program_id)?.authorize::<A, R>(private_key, function_name, inputs, rng)?;
141 finish!(timer, "Compute the authorization");
142
143 Ok(authorization)
145 }
146}
147
148fn ensure_record_microcredits_is_sufficient<N: Network>(
150 record: &Record<N, Plaintext<N>>,
151 fee_in_microcredits: u64,
152) -> Result<()> {
153 let balance = match record.find(&[Identifier::from_str("microcredits")?]) {
155 Ok(console::program::Entry::Private(Plaintext::Literal(Literal::U64(amount), _))) => *amount,
156 _ => bail!("The fee record does not contain a 'microcredits' entry"),
157 };
158 ensure!(balance >= fee_in_microcredits, "Credits record balance is insufficient to pay the fee");
160 Ok(())
161}