1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkVM library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::*;

impl<N: Network> Process<N> {
    /// Authorizes a call to the program function for the given inputs.
    #[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>> {
        // Authorize the call.
        self.get_stack(program_id)?.authorize::<A, R>(private_key, function_name, inputs, rng)
    }

    /// Authorizes the fee given the credits record, the fee amount (in microcredits),
    /// and the deployment or execution ID.
    #[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");

        // Ensure the fee has the correct program ID.
        let program_id = ProgramID::from_str("credits.aleo")?;
        // Ensure the fee has the correct function.
        let function_name = Identifier::from_str("fee_private")?;

        // Ensure the record contains a sufficient balance to pay the fee.
        ensure_record_microcredits_is_sufficient(
            &credits,
            base_fee_in_microcredits.saturating_add(priority_fee_in_microcredits),
        )?;

        // Construct the inputs.
        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");

        // Authorize the call.
        let authorization = self.get_stack(program_id)?.authorize::<A, R>(private_key, function_name, inputs, rng)?;
        finish!(timer, "Compute the authorization");

        // Return the authorization.
        Ok(authorization)
    }

    /// Authorizes the fee given the the fee amount (in microcredits) and the deployment or execution ID.
    #[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");

        // Ensure the fee has the correct program ID.
        let program_id = ProgramID::from_str("credits.aleo")?;
        // Ensure the fee has the correct function.
        let function_name = Identifier::from_str("fee_public")?;

        // Construct the inputs.
        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");

        // Authorize the call.
        let authorization = self.get_stack(program_id)?.authorize::<A, R>(private_key, function_name, inputs, rng)?;
        finish!(timer, "Compute the authorization");

        // Return the authorization.
        Ok(authorization)
    }
}

/// Ensures the record contains a sufficient balance to pay the fee.
fn ensure_record_microcredits_is_sufficient<N: Network>(
    record: &Record<N, Plaintext<N>>,
    fee_in_microcredits: u64,
) -> Result<()> {
    // Retrieve the balance from the record.
    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 the balance is sufficient to pay the fee.
    ensure!(balance >= fee_in_microcredits, "Credits record balance is insufficient to pay the fee");
    Ok(())
}