mirror of
https://github.com/logos-blockchain/lssa-zkvm-testing.git
synced 2026-01-03 22:03:10 +00:00
make client carry the private key of the user
This commit is contained in:
parent
f93c481be3
commit
486822842f
@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
hash,
|
||||
types::{Address, Commitment, Nonce},
|
||||
types::{Address, Commitment, Key, Nonce},
|
||||
};
|
||||
use risc0_zkvm::{serde::to_vec, sha::Impl};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@ -14,11 +14,15 @@ pub struct Account {
|
||||
|
||||
impl Account {
|
||||
/// Creates a new account with address = hash(private_key) and balance = 0
|
||||
pub fn new_from_private_key(private_key: Address, nonce: Nonce) -> Self {
|
||||
let address = hash(&private_key);
|
||||
pub fn new_from_private_key(private_key: Key, nonce: Nonce) -> Self {
|
||||
let address = Self::address_for_key(&private_key);
|
||||
Self::new(address, nonce)
|
||||
}
|
||||
|
||||
pub fn address_for_key(private_key: &Key) -> Address {
|
||||
hash(private_key)
|
||||
}
|
||||
|
||||
pub fn new(address: Address, nonce: Nonce) -> Self {
|
||||
Self {
|
||||
address,
|
||||
|
||||
@ -9,56 +9,50 @@ use nssa::program::{PinataProgram, TransferProgram};
|
||||
use risc0_zkvm::Receipt;
|
||||
|
||||
use crate::mocked_components::sequencer::MockedSequencer;
|
||||
use crate::mocked_components::{client::MockedClient, ACCOUNTS_PRIVATE_KEYS};
|
||||
use crate::mocked_components::{client::MockedClient, USER_CLIENTS};
|
||||
|
||||
mod mocked_components;
|
||||
|
||||
fn main() {
|
||||
let mut sequencer = MockedSequencer::new();
|
||||
let addresses = sequencer.addresses();
|
||||
let addresses: [Address; 3] = USER_CLIENTS.map(|client| client.user_address());
|
||||
|
||||
println!("addresses: {:?}", addresses);
|
||||
println!("🚀 Initial balances");
|
||||
sequencer.print();
|
||||
|
||||
// A public execution of the Transfer Program
|
||||
MockedClient::transfer_public(&addresses[1], &addresses[2], 10, &mut sequencer).unwrap();
|
||||
USER_CLIENTS[0]
|
||||
.transfer_public(&addresses[1], 10, &mut sequencer)
|
||||
.unwrap();
|
||||
println!("🚀 Balances after transfer");
|
||||
sequencer.print();
|
||||
|
||||
// A shielded execution of the Transfer Program
|
||||
let private_account_2 =
|
||||
MockedClient::transfer_shielded(&addresses[1], &addresses[2], 15, &mut sequencer).unwrap();
|
||||
let private_account_1 = USER_CLIENTS[0]
|
||||
.transfer_shielded(&addresses[1], 15, &mut sequencer)
|
||||
.unwrap();
|
||||
println!("Balances after shielded execution");
|
||||
sequencer.print();
|
||||
|
||||
// A private execution of the Transfer Program
|
||||
let [_, private_account_1] = MockedClient::transfer_private(
|
||||
private_account_2,
|
||||
&ACCOUNTS_PRIVATE_KEYS[1], // <-- this is shifted 🫠
|
||||
&addresses[3],
|
||||
8,
|
||||
&mut sequencer,
|
||||
)
|
||||
.unwrap();
|
||||
let [_, private_account_2] = USER_CLIENTS[1]
|
||||
.transfer_private(private_account_1, &addresses[2], 8, &mut sequencer)
|
||||
.unwrap();
|
||||
println!("🚀 Balances after shielded execution");
|
||||
sequencer.print();
|
||||
|
||||
// A deshielded execution of the Transfer Program
|
||||
MockedClient::transfer_deshielded(
|
||||
private_account_1,
|
||||
&ACCOUNTS_PRIVATE_KEYS[0],
|
||||
&addresses[0],
|
||||
1,
|
||||
&mut sequencer,
|
||||
)
|
||||
.unwrap();
|
||||
USER_CLIENTS[2]
|
||||
.transfer_deshielded(private_account_2, &addresses[0], 1, &mut sequencer)
|
||||
.unwrap();
|
||||
println!("🚀 Balances after deshielded execution");
|
||||
sequencer.print();
|
||||
|
||||
// A public execution of the Pinata program
|
||||
let preimage = bytes_to_words(b"NSSA Selective privacy is great!").to_vec();
|
||||
sequencer
|
||||
.process_public_execution::<PinataProgram>(&[addresses[0], addresses[3]], preimage)
|
||||
.process_public_execution::<PinataProgram>(&[[0xcafe; 8], addresses[2]], preimage)
|
||||
.unwrap();
|
||||
println!("🚀 Balances after public piñata execution");
|
||||
sequencer.print();
|
||||
|
||||
@ -13,9 +13,20 @@ pub mod transfer_private;
|
||||
pub mod transfer_public;
|
||||
pub mod transfer_shielded;
|
||||
|
||||
pub struct MockedClient;
|
||||
pub struct MockedClient {
|
||||
user_private_key: Key,
|
||||
}
|
||||
|
||||
impl MockedClient {
|
||||
pub const fn new(user_private_key: Key) -> Self {
|
||||
Self { user_private_key }
|
||||
}
|
||||
|
||||
pub fn user_address(&self) -> Address {
|
||||
let address = Account::address_for_key(&self.user_private_key);
|
||||
address
|
||||
}
|
||||
|
||||
fn prove_and_send_to_sequencer<P: nssa::Program>(
|
||||
input_accounts: &[Account],
|
||||
instruction_data: P::InstructionData,
|
||||
|
||||
@ -9,13 +9,13 @@ use super::{MockedClient, MockedSequencer};
|
||||
impl MockedClient {
|
||||
/// A shielded execution of the Transfer program
|
||||
pub fn transfer_shielded(
|
||||
from_address: &Address,
|
||||
&self,
|
||||
to_address: &Address,
|
||||
balance_to_move: u128,
|
||||
sequencer: &mut MockedSequencer,
|
||||
) -> Result<Account, ()> {
|
||||
// All of this is executed locally by the sender
|
||||
let sender_account = sequencer.get_account(&from_address).unwrap();
|
||||
let sender_account = sequencer.get_account(&self.user_address()).ok_or(())?;
|
||||
let commitment_tree_root = sequencer.get_commitment_tree_root();
|
||||
let receiver_addr = to_address;
|
||||
let mut receiver_account = Self::fresh_account_for_mint(*receiver_addr);
|
||||
|
||||
@ -9,8 +9,8 @@ use super::{MockedClient, MockedSequencer};
|
||||
impl MockedClient {
|
||||
/// A private execution of the Transfer program
|
||||
pub fn transfer_private(
|
||||
&self,
|
||||
from_account: Account,
|
||||
from_account_pk: &Key,
|
||||
to_address: &Address,
|
||||
balance_to_move: u128,
|
||||
sequencer: &mut MockedSequencer,
|
||||
@ -18,11 +18,12 @@ impl MockedClient {
|
||||
// All of this is executed locally by the sender
|
||||
let commitment_tree_root = sequencer.get_commitment_tree_root();
|
||||
let receiver_addr = to_address;
|
||||
// let from_account = sequencer.get_account(&self.user_address()).ok_or(())?;
|
||||
let sender_commitment_auth_path =
|
||||
sequencer.get_authentication_path_for(&from_account.commitment());
|
||||
let mut receiver_account = Self::fresh_account_for_mint(*receiver_addr);
|
||||
let visibilities = vec![
|
||||
InputVisibiility::Private(Some((from_account_pk.clone(), sender_commitment_auth_path))),
|
||||
InputVisibiility::Private(Some((self.user_private_key, sender_commitment_auth_path))),
|
||||
InputVisibiility::Private(None),
|
||||
];
|
||||
|
||||
|
||||
@ -6,13 +6,13 @@ use crate::mocked_components::{client::MockedClient, sequencer::MockedSequencer}
|
||||
|
||||
impl MockedClient {
|
||||
pub fn transfer_public(
|
||||
sender_address: &Address,
|
||||
&self,
|
||||
receiver_address: &Address,
|
||||
amount_to_transfer: u128,
|
||||
sequencer: &mut MockedSequencer,
|
||||
) -> Result<(), ()> {
|
||||
sequencer.process_public_execution::<TransferProgram>(
|
||||
&[*sender_address, *receiver_address],
|
||||
&[self.user_address(), *receiver_address],
|
||||
amount_to_transfer,
|
||||
)
|
||||
}
|
||||
|
||||
@ -8,8 +8,8 @@ use super::{MockedClient, MockedSequencer};
|
||||
|
||||
impl MockedClient {
|
||||
pub fn transfer_deshielded(
|
||||
&self,
|
||||
from_account: Account,
|
||||
from_account_pk: &Key,
|
||||
to_address: &Address,
|
||||
balance_to_move: u128,
|
||||
sequencer: &mut MockedSequencer,
|
||||
@ -17,11 +17,12 @@ impl MockedClient {
|
||||
// All of this is executed locally by the sender
|
||||
let commitment_tree_root = sequencer.get_commitment_tree_root();
|
||||
let receiver_addr = to_address;
|
||||
// let from_account = sequencer.get_account(&self.user_address()).ok_or(())?;
|
||||
let sender_commitment_auth_path =
|
||||
sequencer.get_authentication_path_for(&from_account.commitment());
|
||||
let to_account = sequencer.get_account(&to_address).unwrap();
|
||||
let visibilities = vec![
|
||||
InputVisibiility::Private(Some((from_account_pk.clone(), sender_commitment_auth_path))),
|
||||
InputVisibiility::Private(Some((self.user_private_key, sender_commitment_auth_path))),
|
||||
InputVisibiility::Public,
|
||||
];
|
||||
let private_outputs = Self::prove_and_send_to_sequencer::<TransferProgram>(
|
||||
|
||||
@ -1,6 +1,12 @@
|
||||
use core::types::Key;
|
||||
|
||||
use crate::mocked_components::client::MockedClient;
|
||||
|
||||
pub mod client;
|
||||
pub mod sequencer;
|
||||
|
||||
pub const ACCOUNTS_PRIVATE_KEYS: [Key; 3] = [[1; 8], [2; 8], [3; 8]];
|
||||
pub const USER_CLIENTS: [MockedClient; 3] = [
|
||||
MockedClient::new([1; 8]),
|
||||
MockedClient::new([2; 8]),
|
||||
MockedClient::new([3; 8]),
|
||||
];
|
||||
|
||||
@ -8,7 +8,7 @@ use std::collections::{BTreeMap, HashSet};
|
||||
use program_methods::{PINATA_ID, TRANSFER_ID, TRANSFER_MULTIPLE_ID};
|
||||
use sparse_merkle_tree::SparseMerkleTree;
|
||||
|
||||
use super::ACCOUNTS_PRIVATE_KEYS;
|
||||
use crate::mocked_components::USER_CLIENTS;
|
||||
|
||||
pub mod process_privacy_execution;
|
||||
pub mod process_public_execution;
|
||||
@ -25,12 +25,12 @@ const DEPLOYED_PROGRAM_IDS: [ProgramId; 3] = [TRANSFER_ID, TRANSFER_MULTIPLE_ID,
|
||||
|
||||
impl MockedSequencer {
|
||||
pub fn new() -> Self {
|
||||
let mut accounts: BTreeMap<Address, Account> = ACCOUNTS_PRIVATE_KEYS
|
||||
let mut accounts: BTreeMap<Address, Account> = USER_CLIENTS
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|client| client.user_address())
|
||||
.zip(ACCOUNTS_INITIAL_BALANCES)
|
||||
.map(|(key, initial_balance)| {
|
||||
let mut this = Account::new_from_private_key(key, [0; 8]);
|
||||
.map(|(address, initial_balance)| {
|
||||
let mut this = Account::new(address, [0; 8]);
|
||||
this.balance = initial_balance;
|
||||
this
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user