73 lines
2.7 KiB
Rust
Raw Normal View History

2025-07-17 10:10:39 -03:00
use core::{
2025-07-16 15:37:49 -03:00
account::Account,
bytes_to_words,
input::InputVisibiility,
2025-07-17 09:20:03 -03:00
types::{Address, AuthenticationPath, Commitment, Nullifier},
};
2025-07-17 11:07:18 -03:00
use nssa::program::TransferMultipleProgram;
2025-07-17 11:34:56 -03:00
use program_methods::OUTER_ID;
2025-07-17 12:46:57 -03:00
use sparse_merkle_tree::SparseMerkleTree;
2025-07-15 09:26:34 -03:00
fn mint_fresh_account(address: Address) -> Account {
2025-07-16 16:45:07 -03:00
let nonce = [0; 8];
Account::new(address, nonce)
}
2025-07-11 19:21:06 -03:00
2025-07-11 19:35:54 -03:00
/// A private execution of the transfer function.
/// This actually "burns" a sender private account and "mints" two new private accounts:
2025-07-11 19:44:43 -03:00
/// one for the recipient with the transferred balance, and another owned by the sender with the remaining balance.
2025-07-16 17:25:03 -03:00
fn main() {
2025-07-11 19:35:54 -03:00
// This is supposed to be an existing private account (UTXO) with balance equal to 150.
// And it is supposed to be a private account of the user running this private execution (hence the access to the private key)
2025-07-15 09:26:34 -03:00
let sender_private_key = [1, 2, 3, 4, 4, 3, 2, 1];
2025-07-11 19:44:43 -03:00
let sender = {
// Creating it now but it's supposed to be already created by other previous transactions.
let mut account = Account::new_from_private_key(sender_private_key, [1; 8]);
account.balance = 150;
account
};
2025-07-15 13:54:59 -03:00
2025-07-15 13:08:13 -03:00
let commitment_tree = SparseMerkleTree::new([sender.commitment()].into_iter().collect());
2025-07-15 13:54:59 -03:00
let root = bytes_to_words(&commitment_tree.root());
let auth_path: Vec<[u32; 8]> = commitment_tree
.get_authentication_path_for_value(sender.commitment())
.iter()
.map(bytes_to_words)
.collect();
let auth_path: AuthenticationPath = auth_path.try_into().unwrap();
2025-07-11 19:21:06 -03:00
let balance_to_move: u128 = 3;
2025-07-16 16:45:07 -03:00
// This is the new private account (UTXO) being minted by this private execution. (The `receiver_address` would be <Npk> in UTXO's terminology)
2025-07-17 11:07:18 -03:00
let receiver_address_1 = [99; 8];
let receiver_1 = mint_fresh_account(receiver_address_1);
let receiver_address_2 = [100; 8];
let receiver_2 = mint_fresh_account(receiver_address_2);
2025-07-11 19:35:54 -03:00
let visibilities = vec![
2025-07-15 13:54:59 -03:00
InputVisibiility::Private(Some((sender_private_key, auth_path))),
InputVisibiility::Private(None),
2025-07-17 11:07:18 -03:00
InputVisibiility::Private(None),
];
2025-07-17 09:43:44 -03:00
2025-07-17 15:34:35 -03:00
let (receipt, _) = nssa::invoke_privacy_execution::<TransferMultipleProgram>(
2025-07-17 11:07:18 -03:00
&[sender, receiver_1, receiver_2],
2025-07-17 16:05:24 -03:00
vec![30, 40],
&visibilities,
root,
)
.unwrap();
2025-07-11 19:21:06 -03:00
2025-07-17 09:20:03 -03:00
let output: (Vec<Account>, Vec<Nullifier>, Vec<Commitment>, [u32; 8]) =
receipt.journal.decode().unwrap();
println!("public_outputs: {:?}", output.0);
println!("nullifiers: {:?}", output.1);
println!("commitments: {:?}", output.2);
2025-07-17 09:20:03 -03:00
println!("commitment_tree_root: {:?}", output.3);
assert!(
nssa::verify_privacy_execution(receipt, &output.0, &output.1, &output.2, &output.3).is_ok()
);
2025-07-11 19:21:06 -03:00
}