snarkos_cli/commands/developer/
transfer_private.rs

1// Copyright 2024-2025 Aleo Network Foundation
2// This file is part of the snarkOS library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use super::Developer;
17use snarkvm::{
18    console::network::{CanaryV0, MainnetV0, Network, TestnetV0},
19    prelude::{
20        Address,
21        Locator,
22        PrivateKey,
23        VM,
24        Value,
25        query::Query,
26        store::{ConsensusStore, helpers::memory::ConsensusMemory},
27    },
28};
29
30use aleo_std::StorageMode;
31use anyhow::{Result, bail};
32use clap::Parser;
33use std::{path::PathBuf, str::FromStr};
34use zeroize::Zeroize;
35
36/// Executes the `transfer_private` function in the `credits.aleo` program.
37#[derive(Debug, Parser)]
38pub struct TransferPrivate {
39    /// Specify the network to create a `transfer_private` for.
40    #[clap(default_value = "0", long = "network")]
41    pub network: u16,
42    /// The input record used to craft the transfer.
43    #[clap(long)]
44    input_record: String,
45    /// The recipient address.
46    #[clap(long)]
47    recipient: String,
48    /// The number of microcredits to transfer.
49    #[clap(long)]
50    amount: u64,
51    /// The private key used to generate the execution.
52    #[clap(short, long)]
53    private_key: String,
54    /// The endpoint to query node state from.
55    #[clap(short, long)]
56    query: String,
57    /// The priority fee in microcredits.
58    #[clap(long)]
59    priority_fee: u64,
60    /// The record to spend the fee from.
61    #[clap(long)]
62    fee_record: String,
63    /// The endpoint used to broadcast the generated transaction.
64    #[clap(short, long, conflicts_with = "dry_run")]
65    broadcast: Option<String>,
66    /// Performs a dry-run of transaction generation.
67    #[clap(short, long, conflicts_with = "broadcast")]
68    dry_run: bool,
69    /// Store generated deployment transaction to a local file.
70    #[clap(long)]
71    store: Option<String>,
72    /// Specify the path to a directory containing the ledger
73    #[clap(long = "storage_path")]
74    pub storage_path: Option<PathBuf>,
75}
76
77impl Drop for TransferPrivate {
78    /// Zeroize the private key when the `TransferPrivate` struct goes out of scope.
79    fn drop(&mut self) {
80        self.private_key.zeroize();
81    }
82}
83
84impl TransferPrivate {
85    /// Creates an Aleo transfer with the provided inputs.
86    #[allow(clippy::format_in_format_args)]
87    pub fn parse(self) -> Result<String> {
88        // Ensure that the user has specified an action.
89        if !self.dry_run && self.broadcast.is_none() && self.store.is_none() {
90            bail!("❌ Please specify one of the following actions: --broadcast, --dry-run, --store");
91        }
92
93        // Construct the transfer for the specified network.
94        match self.network {
95            MainnetV0::ID => self.construct_transfer_private::<MainnetV0>(),
96            TestnetV0::ID => self.construct_transfer_private::<TestnetV0>(),
97            CanaryV0::ID => self.construct_transfer_private::<CanaryV0>(),
98            unknown_id => bail!("Unknown network ID ({unknown_id})"),
99        }
100    }
101
102    /// Construct and process the `transfer_private` transaction.
103    fn construct_transfer_private<N: Network>(&self) -> Result<String> {
104        // Specify the query
105        let query = Query::from(&self.query);
106
107        // Retrieve the recipient.
108        let recipient = Address::<N>::from_str(&self.recipient)?;
109
110        // Retrieve the private key.
111        let private_key = PrivateKey::from_str(&self.private_key)?;
112
113        println!("📦 Creating private transfer of {} microcredits to {}...\n", self.amount, recipient);
114
115        // Generate the transfer_private transaction.
116        let transaction = {
117            // Initialize an RNG.
118            let rng = &mut rand::thread_rng();
119
120            // Initialize the storage.
121            let storage_mode = match &self.storage_path {
122                Some(path) => StorageMode::Custom(path.clone()),
123                None => StorageMode::Production,
124            };
125            let store = ConsensusStore::<N, ConsensusMemory<N>>::open(storage_mode)?;
126
127            // Initialize the VM.
128            let vm = VM::from(store)?;
129
130            // Prepare the fee.
131            let fee_record = Developer::parse_record(&private_key, &self.fee_record)?;
132            let priority_fee = self.priority_fee;
133
134            // Prepare the inputs for a transfer.
135            let input_record = Developer::parse_record(&private_key, &self.input_record)?;
136            let inputs = [
137                Value::Record(input_record),
138                Value::from_str(&format!("{}", recipient))?,
139                Value::from_str(&format!("{}u64", self.amount))?,
140            ];
141
142            // Create a new transaction.
143            vm.execute(
144                &private_key,
145                ("credits.aleo", "transfer_private"),
146                inputs.iter(),
147                Some(fee_record),
148                priority_fee,
149                Some(query),
150                rng,
151            )?
152        };
153        let locator = Locator::<N>::from_str("credits.aleo/transfer_private")?;
154        println!("✅ Created private transfer of {} microcredits to {}\n", &self.amount, recipient);
155
156        // Determine if the transaction should be broadcast, stored, or displayed to the user.
157        Developer::handle_transaction(&self.broadcast, self.dry_run, &self.store, transaction, locator.to_string())
158    }
159}