lssa/nssa/program_methods/guest/src/bin/authenticated_transfer.rs

32 lines
1.1 KiB
Rust
Raw Normal View History

2025-08-06 20:05:04 -03:00
use nssa_core::account::{Account, AccountWithMetadata};
use risc0_zkvm::guest::env;
/// A transfer of balance program.
/// To be used both in public and private contexts.
fn main() {
// Read input accounts.
// It is expected to receive only two accounts: [sender_account, receiver_account]
let input_accounts: Vec<AccountWithMetadata> = env::read();
let balance_to_move: u128 = env::read();
// Unpack sender and receiver
assert_eq!(input_accounts.len(), 2);
let [sender, receiver] = input_accounts
.try_into()
.unwrap_or_else(|_| panic!("Bad input"));
// Check sender has authorized this operation
assert!(sender.is_authorized);
// Check sender has enough balance
assert!(sender.account.balance >= balance_to_move);
// Create accounts post states, with updated balances
let mut sender_post = sender.account.clone();
let mut receiver_post = receiver.account.clone();
sender_post.balance -= balance_to_move;
receiver_post.balance += balance_to_move;
env::commit(&vec![sender_post, receiver_post]);
}