use super::*;
impl<N: Network> Process<N> {
#[inline]
pub fn authorize<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
&self,
private_key: &PrivateKey<N>,
program_id: impl TryInto<ProgramID<N>>,
function_name: impl TryInto<Identifier<N>>,
inputs: impl ExactSizeIterator<Item = impl TryInto<Value<N>>>,
rng: &mut R,
) -> Result<Authorization<N>> {
self.get_stack(program_id)?.authorize::<A, R>(private_key, function_name, inputs, rng)
}
#[inline]
pub fn authorize_fee_private<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
&self,
private_key: &PrivateKey<N>,
credits: Record<N, Plaintext<N>>,
base_fee_in_microcredits: u64,
priority_fee_in_microcredits: u64,
deployment_or_execution_id: Field<N>,
rng: &mut R,
) -> Result<Authorization<N>> {
let timer = timer!("Process::authorize_fee_private");
let program_id = ProgramID::from_str("credits.aleo")?;
let function_name = Identifier::from_str("fee_private")?;
ensure_record_microcredits_is_sufficient(
&credits,
base_fee_in_microcredits.saturating_add(priority_fee_in_microcredits),
)?;
let inputs = [
Value::Record(credits),
Value::from(Literal::U64(U64::<N>::new(base_fee_in_microcredits))),
Value::from(Literal::U64(U64::<N>::new(priority_fee_in_microcredits))),
Value::from(Literal::Field(deployment_or_execution_id)),
]
.into_iter();
lap!(timer, "Construct the inputs");
let authorization = self.get_stack(program_id)?.authorize::<A, R>(private_key, function_name, inputs, rng)?;
finish!(timer, "Compute the authorization");
Ok(authorization)
}
#[inline]
pub fn authorize_fee_public<A: circuit::Aleo<Network = N>, R: Rng + CryptoRng>(
&self,
private_key: &PrivateKey<N>,
base_fee_in_microcredits: u64,
priority_fee_in_microcredits: u64,
deployment_or_execution_id: Field<N>,
rng: &mut R,
) -> Result<Authorization<N>> {
let timer = timer!("Process::authorize_fee_public");
let program_id = ProgramID::from_str("credits.aleo")?;
let function_name = Identifier::from_str("fee_public")?;
let inputs = [
Value::from(Literal::U64(U64::<N>::new(base_fee_in_microcredits))),
Value::from(Literal::U64(U64::<N>::new(priority_fee_in_microcredits))),
Value::from(Literal::Field(deployment_or_execution_id)),
]
.into_iter();
lap!(timer, "Construct the inputs");
let authorization = self.get_stack(program_id)?.authorize::<A, R>(private_key, function_name, inputs, rng)?;
finish!(timer, "Compute the authorization");
Ok(authorization)
}
}
fn ensure_record_microcredits_is_sufficient<N: Network>(
record: &Record<N, Plaintext<N>>,
fee_in_microcredits: u64,
) -> Result<()> {
let balance = match record.find(&[Identifier::from_str("microcredits")?]) {
Ok(console::program::Entry::Private(Plaintext::Literal(Literal::U64(amount), _))) => *amount,
_ => bail!("The fee record does not contain a 'microcredits' entry"),
};
ensure!(balance >= fee_in_microcredits, "Credits record balance is insufficient to pay the fee");
Ok(())
}