2025-01-03 10:44:06 +02:00
|
|
|
use std::sync::{
|
|
|
|
|
atomic::{AtomicU64, Ordering},
|
|
|
|
|
Arc,
|
2024-12-05 13:05:58 +02:00
|
|
|
};
|
|
|
|
|
|
2025-02-05 12:24:09 +02:00
|
|
|
use common::ExecutionFailureKind;
|
2024-12-25 09:50:54 +02:00
|
|
|
|
2025-01-31 16:50:00 -05:00
|
|
|
use ::storage::transaction::{Transaction, TransactionPayload, TxKind};
|
2024-12-22 16:14:52 +02:00
|
|
|
use accounts::account_core::{Account, AccountAddress};
|
2025-03-16 11:37:23 -04:00
|
|
|
use anyhow::Result;
|
2024-12-03 09:32:35 +02:00
|
|
|
use config::NodeConfig;
|
2025-03-16 11:37:23 -04:00
|
|
|
use executions::private_exec::{generate_commitments, generate_nullifiers};
|
2024-12-25 09:50:54 +02:00
|
|
|
use log::info;
|
2024-12-22 16:14:52 +02:00
|
|
|
use sequencer_client::{json::SendTxResponse, SequencerClient};
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
2024-12-03 09:32:35 +02:00
|
|
|
use storage::NodeChainStore;
|
2024-12-25 09:50:54 +02:00
|
|
|
use tokio::{sync::RwLock, task::JoinHandle};
|
2025-01-31 16:50:00 -05:00
|
|
|
use utxo::utxo_core::UTXO;
|
2024-12-22 16:14:52 +02:00
|
|
|
use zkvm::{
|
2025-02-28 12:32:54 +02:00
|
|
|
gas_calculator::GasCalculator, prove_mint_utxo, prove_mint_utxo_multiple_assets,
|
|
|
|
|
prove_send_utxo, prove_send_utxo_deshielded, prove_send_utxo_multiple_assets_one_receiver,
|
|
|
|
|
prove_send_utxo_shielded,
|
2024-12-22 16:14:52 +02:00
|
|
|
};
|
2024-12-03 09:32:35 +02:00
|
|
|
|
2024-12-25 09:50:54 +02:00
|
|
|
pub const BLOCK_GEN_DELAY_SECS: u64 = 20;
|
|
|
|
|
|
2024-12-03 09:32:35 +02:00
|
|
|
pub mod config;
|
2024-12-02 00:55:52 +01:00
|
|
|
pub mod executions;
|
2024-12-03 09:32:35 +02:00
|
|
|
pub mod sequencer_client;
|
|
|
|
|
pub mod storage;
|
|
|
|
|
|
2024-12-22 16:14:52 +02:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
|
pub struct MintMoneyPublicTx {
|
|
|
|
|
pub acc: AccountAddress,
|
|
|
|
|
pub amount: u128,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
|
pub struct SendMoneyShieldedTx {
|
|
|
|
|
pub acc_sender: AccountAddress,
|
|
|
|
|
pub amount: u128,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
|
pub struct SendMoneyDeshieldedTx {
|
|
|
|
|
pub receiver_data: Vec<(u128, AccountAddress)>,
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
|
pub struct UTXOPublication {
|
|
|
|
|
pub utxos: Vec<UTXO>,
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-22 16:14:52 +02:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
|
pub enum ActionData {
|
|
|
|
|
MintMoneyPublicTx(MintMoneyPublicTx),
|
|
|
|
|
SendMoneyShieldedTx(SendMoneyShieldedTx),
|
|
|
|
|
SendMoneyDeshieldedTx(SendMoneyDeshieldedTx),
|
2025-01-03 10:44:06 +02:00
|
|
|
UTXOPublication(UTXOPublication),
|
2024-12-22 16:14:52 +02:00
|
|
|
}
|
|
|
|
|
|
2024-12-03 09:32:35 +02:00
|
|
|
pub struct NodeCore {
|
2024-12-25 09:50:54 +02:00
|
|
|
pub storage: Arc<RwLock<NodeChainStore>>,
|
2024-12-05 13:05:58 +02:00
|
|
|
pub curr_height: Arc<AtomicU64>,
|
2024-12-03 09:32:35 +02:00
|
|
|
pub node_config: NodeConfig,
|
2024-12-05 13:05:58 +02:00
|
|
|
pub db_updater_handle: JoinHandle<Result<()>>,
|
2024-12-20 11:02:12 +02:00
|
|
|
pub sequencer_client: Arc<SequencerClient>,
|
2025-02-28 12:32:54 +02:00
|
|
|
pub gas_calculator: GasCalculator,
|
2024-12-05 13:05:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl NodeCore {
|
|
|
|
|
pub async fn start_from_config_update_chain(config: NodeConfig) -> Result<Self> {
|
2024-12-20 11:02:12 +02:00
|
|
|
let client = Arc::new(SequencerClient::new(config.clone())?);
|
2024-12-05 13:05:58 +02:00
|
|
|
|
|
|
|
|
let genesis_id = client.get_genesis_id().await?;
|
2024-12-29 14:11:47 +02:00
|
|
|
info!("Gesesis id is {genesis_id:?}");
|
|
|
|
|
|
2024-12-05 13:05:58 +02:00
|
|
|
let genesis_block = client.get_block(genesis_id.genesis_id).await?.block;
|
|
|
|
|
|
|
|
|
|
let mut storage = NodeChainStore::new_with_genesis(&config.home, genesis_block);
|
|
|
|
|
|
|
|
|
|
let mut chain_height = genesis_id.genesis_id;
|
|
|
|
|
|
|
|
|
|
//Chain update loop
|
|
|
|
|
loop {
|
|
|
|
|
let next_block = chain_height + 1;
|
|
|
|
|
|
|
|
|
|
if let Ok(block) = client.get_block(next_block).await {
|
|
|
|
|
storage.dissect_insert_block(block.block)?;
|
2024-12-29 14:11:47 +02:00
|
|
|
info!("Preprocessed block with id {next_block:?}");
|
2024-12-05 13:05:58 +02:00
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chain_height += 1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-25 09:50:54 +02:00
|
|
|
let wrapped_storage = Arc::new(RwLock::new(storage));
|
2024-12-05 13:05:58 +02:00
|
|
|
let chain_height_wrapped = Arc::new(AtomicU64::new(chain_height));
|
|
|
|
|
|
|
|
|
|
let wrapped_storage_thread = wrapped_storage.clone();
|
|
|
|
|
let wrapped_chain_height_thread = chain_height_wrapped.clone();
|
|
|
|
|
let client_thread = client.clone();
|
|
|
|
|
|
|
|
|
|
let updater_handle = tokio::spawn(async move {
|
|
|
|
|
loop {
|
|
|
|
|
let next_block = wrapped_chain_height_thread.load(Ordering::Relaxed) + 1;
|
|
|
|
|
|
|
|
|
|
if let Ok(block) = client_thread.get_block(next_block).await {
|
|
|
|
|
{
|
2024-12-25 09:50:54 +02:00
|
|
|
let mut storage_guard = wrapped_storage_thread.write().await;
|
2024-12-05 13:05:58 +02:00
|
|
|
|
2024-12-30 09:10:04 +02:00
|
|
|
let block_insertion_result =
|
|
|
|
|
storage_guard.dissect_insert_block(block.block);
|
2024-12-30 07:00:59 +02:00
|
|
|
|
|
|
|
|
if block_insertion_result.is_err() {
|
|
|
|
|
info!("Block insertion failed due to {block_insertion_result:?}");
|
|
|
|
|
|
|
|
|
|
block_insertion_result?;
|
|
|
|
|
}
|
2024-12-29 14:11:47 +02:00
|
|
|
info!("Processed block with id {next_block:?}");
|
2024-12-05 13:05:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wrapped_chain_height_thread.store(next_block, Ordering::Relaxed);
|
|
|
|
|
} else {
|
2024-12-05 13:07:15 +02:00
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(
|
|
|
|
|
config.seq_poll_timeout_secs,
|
|
|
|
|
))
|
|
|
|
|
.await;
|
2024-12-05 13:05:58 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
|
storage: wrapped_storage,
|
|
|
|
|
curr_height: chain_height_wrapped,
|
|
|
|
|
node_config: config.clone(),
|
|
|
|
|
db_updater_handle: updater_handle,
|
2024-12-20 11:02:12 +02:00
|
|
|
sequencer_client: client.clone(),
|
2025-02-28 12:32:54 +02:00
|
|
|
gas_calculator: GasCalculator::from(config.gas_config),
|
2024-12-05 13:05:58 +02:00
|
|
|
})
|
|
|
|
|
}
|
2024-12-22 16:14:52 +02:00
|
|
|
|
2025-01-24 09:10:42 +02:00
|
|
|
pub async fn get_roots(&self) -> [[u8; 32]; 3] {
|
|
|
|
|
let storage = self.storage.read().await;
|
|
|
|
|
[
|
2025-02-05 12:24:09 +02:00
|
|
|
storage.nullifier_store.curr_root.unwrap_or([0; 32]),
|
|
|
|
|
storage.utxo_commitments_store.get_root().unwrap_or([0; 32]),
|
|
|
|
|
storage.pub_tx_store.get_root().unwrap_or([0; 32]),
|
2025-01-24 09:10:42 +02:00
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-25 09:50:54 +02:00
|
|
|
pub async fn create_new_account(&mut self) -> AccountAddress {
|
|
|
|
|
let account = Account::new();
|
2024-12-30 07:35:05 +01:00
|
|
|
account.log();
|
2024-12-25 09:50:54 +02:00
|
|
|
|
|
|
|
|
let addr = account.address;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let mut write_guard = self.storage.write().await;
|
|
|
|
|
|
|
|
|
|
write_guard.acc_map.insert(account.address, account);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addr
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-26 11:38:00 +02:00
|
|
|
pub async fn mint_utxo_private(
|
|
|
|
|
&self,
|
|
|
|
|
acc: AccountAddress,
|
|
|
|
|
amount: u128,
|
2025-01-10 03:00:32 +02:00
|
|
|
) -> Result<(Transaction, [u8; 32]), ExecutionFailureKind> {
|
|
|
|
|
let (utxo, receipt) = prove_mint_utxo(amount, acc)?;
|
2024-12-26 11:38:00 +02:00
|
|
|
let result_hash = utxo.hash;
|
2024-12-22 16:14:52 +02:00
|
|
|
|
2024-12-25 09:50:54 +02:00
|
|
|
let acc_map_read_guard = self.storage.read().await;
|
|
|
|
|
|
2025-04-09 01:34:03 -04:00
|
|
|
let account = acc_map_read_guard.acc_map.get(&acc).unwrap();
|
2024-12-25 09:50:54 +02:00
|
|
|
|
2025-04-09 01:34:03 -04:00
|
|
|
let ephm_key_holder = &account.produce_ephemeral_key_holder();
|
2025-01-03 08:13:59 +02:00
|
|
|
ephm_key_holder.log();
|
2024-12-25 09:50:54 +02:00
|
|
|
|
2025-04-04 14:14:04 -04:00
|
|
|
let eph_pub_key =
|
|
|
|
|
serde_json::to_vec(&ephm_key_holder.generate_ephemeral_public_key()).unwrap();
|
2024-12-22 16:14:52 +02:00
|
|
|
|
|
|
|
|
let encoded_data = Account::encrypt_data(
|
2024-12-25 09:50:54 +02:00
|
|
|
&ephm_key_holder,
|
2025-04-09 01:34:03 -04:00
|
|
|
account.key_holder.viewing_public_key,
|
2024-12-22 16:14:52 +02:00
|
|
|
&serde_json::to_vec(&utxo).unwrap(),
|
|
|
|
|
);
|
|
|
|
|
|
2025-04-09 01:34:03 -04:00
|
|
|
let tag = account.make_tag();
|
2025-03-28 17:12:28 +02:00
|
|
|
|
2024-12-22 16:14:52 +02:00
|
|
|
let comm = generate_commitments(&vec![utxo]);
|
|
|
|
|
|
2025-04-09 01:34:03 -04:00
|
|
|
// TODO: fix address when correspoding method will be added
|
|
|
|
|
let sc_addr = "";
|
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
let secret_r: [u8; 32] = rng.gen();
|
|
|
|
|
|
|
|
|
|
let sc_state = acc_map_read_guard.block_store.get_sc_sc_state(sc_addr).map_err(ExecutionFailureKind::db_error)?;
|
|
|
|
|
|
|
|
|
|
let mut vec_values_u64: Vec<Vec<u64>> = sc_state.into_iter().map(|slice| Self::vec_u8_to_vec_u64(slice.to_vec())).collect();
|
|
|
|
|
|
|
|
|
|
let context = acc_map_read_guard.produce_context(account.address);
|
|
|
|
|
|
|
|
|
|
let serialized_context = serde_json::to_vec(&context).unwrap();
|
|
|
|
|
|
|
|
|
|
let serialized_context_u64 = Self::vec_u8_to_vec_u64(serialized_context);
|
|
|
|
|
|
|
|
|
|
vec_values_u64.push(serialized_context_u64);
|
|
|
|
|
|
|
|
|
|
let vec_public_info: Vec<u64> = vec_values_u64.into_iter().flatten().collect();
|
|
|
|
|
|
|
|
|
|
let (tweak, secret_r, commitment) = new_commitment_vec(vec_public_info, &secret_r);
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
Ok((
|
2024-12-26 11:38:00 +02:00
|
|
|
TransactionPayload {
|
|
|
|
|
tx_kind: TxKind::Private,
|
|
|
|
|
execution_input: vec![],
|
|
|
|
|
execution_output: vec![],
|
|
|
|
|
utxo_commitments_spent_hashes: vec![],
|
|
|
|
|
utxo_commitments_created_hashes: comm
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|hash_data| hash_data.try_into().unwrap())
|
|
|
|
|
.collect(),
|
|
|
|
|
nullifier_created_hashes: vec![],
|
2025-03-16 11:05:55 -04:00
|
|
|
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(
|
|
|
|
|
receipt,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
2025-03-28 17:12:28 +02:00
|
|
|
encoded_data: vec![(encoded_data.0, encoded_data.1.to_vec(), tag)],
|
2024-12-26 11:38:00 +02:00
|
|
|
ephemeral_pub_key: eph_pub_key.to_vec(),
|
2025-04-09 01:34:03 -04:00
|
|
|
commitment,
|
|
|
|
|
tweak,
|
|
|
|
|
secret_r: secret_r.try_into().unwrap(),
|
2024-12-26 11:38:00 +02:00
|
|
|
}
|
|
|
|
|
.into(),
|
|
|
|
|
result_hash,
|
2025-01-10 03:00:32 +02:00
|
|
|
))
|
2024-12-22 16:14:52 +02:00
|
|
|
}
|
|
|
|
|
|
2025-04-09 01:34:21 -04:00
|
|
|
fn vec_u8_to_vec_u64(bytes: Vec<u8>) -> Vec<u64> {
|
|
|
|
|
// Pad with zeros to make sure it's a multiple of 8
|
|
|
|
|
let mut padded = bytes.clone();
|
|
|
|
|
while padded.len() % 8 != 0 {
|
|
|
|
|
padded.push(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
padded
|
|
|
|
|
.chunks(8)
|
|
|
|
|
.map(|chunk| {
|
|
|
|
|
let mut array = [0u8; 8];
|
|
|
|
|
array.copy_from_slice(chunk);
|
|
|
|
|
u64::from_le_bytes(array)
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-01-03 12:43:05 +02:00
|
|
|
pub async fn mint_utxo_multiple_assets_private(
|
|
|
|
|
&self,
|
|
|
|
|
acc: AccountAddress,
|
|
|
|
|
amount: u128,
|
|
|
|
|
number_of_assets: usize,
|
2025-01-10 03:00:32 +02:00
|
|
|
) -> Result<(Transaction, Vec<[u8; 32]>), ExecutionFailureKind> {
|
|
|
|
|
let (utxos, receipt) = prove_mint_utxo_multiple_assets(amount, number_of_assets, acc)?;
|
2025-01-03 12:43:05 +02:00
|
|
|
let result_hashes = utxos.iter().map(|utxo| utxo.hash).collect();
|
|
|
|
|
|
|
|
|
|
let acc_map_read_guard = self.storage.read().await;
|
|
|
|
|
|
2025-04-09 01:34:57 -04:00
|
|
|
let account = acc_map_read_guard.acc_map.get(&acc).unwrap();
|
2025-01-03 12:43:05 +02:00
|
|
|
|
2025-04-09 01:34:57 -04:00
|
|
|
let ephm_key_holder = &account.produce_ephemeral_key_holder();
|
2025-01-03 12:43:05 +02:00
|
|
|
ephm_key_holder.log();
|
|
|
|
|
|
2025-04-04 14:14:04 -04:00
|
|
|
let eph_pub_key =
|
|
|
|
|
serde_json::to_vec(&ephm_key_holder.generate_ephemeral_public_key()).unwrap();
|
2025-01-03 12:43:05 +02:00
|
|
|
|
|
|
|
|
let encoded_data = utxos
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|utxo| {
|
2025-03-28 17:12:28 +02:00
|
|
|
(
|
|
|
|
|
Account::encrypt_data(
|
|
|
|
|
&ephm_key_holder,
|
2025-04-09 01:34:57 -04:00
|
|
|
account.key_holder.viewing_public_key,
|
2025-03-28 17:12:28 +02:00
|
|
|
&serde_json::to_vec(&utxo).unwrap(),
|
|
|
|
|
),
|
2025-04-09 01:34:57 -04:00
|
|
|
account.make_tag(),
|
2025-01-03 12:43:05 +02:00
|
|
|
)
|
|
|
|
|
})
|
2025-03-28 17:12:28 +02:00
|
|
|
.map(|((ciphertext, nonce), tag)| (ciphertext, nonce.to_vec(), tag))
|
2025-01-03 12:43:05 +02:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let comm = generate_commitments(&utxos);
|
|
|
|
|
|
2025-04-09 01:34:57 -04:00
|
|
|
// TODO: fix address when correspoding method will be added
|
|
|
|
|
let sc_addr = "";
|
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
let secret_r: [u8; 32] = rng.gen();
|
|
|
|
|
|
|
|
|
|
let sc_state = acc_map_read_guard.block_store.get_sc_sc_state(sc_addr).map_err(ExecutionFailureKind::db_error)?;
|
|
|
|
|
|
|
|
|
|
let mut vec_values_u64: Vec<Vec<u64>> = sc_state.into_iter().map(|slice| Self::vec_u8_to_vec_u64(slice.to_vec())).collect();
|
|
|
|
|
|
|
|
|
|
let context = acc_map_read_guard.produce_context(account.address);
|
|
|
|
|
|
|
|
|
|
let serialized_context = serde_json::to_vec(&context).unwrap();
|
|
|
|
|
|
|
|
|
|
let serialized_context_u64 = Self::vec_u8_to_vec_u64(serialized_context);
|
|
|
|
|
|
|
|
|
|
vec_values_u64.push(serialized_context_u64);
|
|
|
|
|
|
|
|
|
|
let vec_public_info: Vec<u64> = vec_values_u64.into_iter().flatten().collect();
|
|
|
|
|
|
|
|
|
|
let (tweak, secret_r, commitment) = new_commitment_vec(vec_public_info, &secret_r);
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
Ok((
|
2025-01-03 12:43:05 +02:00
|
|
|
TransactionPayload {
|
|
|
|
|
tx_kind: TxKind::Private,
|
|
|
|
|
execution_input: vec![],
|
|
|
|
|
execution_output: vec![],
|
|
|
|
|
utxo_commitments_spent_hashes: vec![],
|
|
|
|
|
utxo_commitments_created_hashes: comm
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|hash_data| hash_data.try_into().unwrap())
|
|
|
|
|
.collect(),
|
|
|
|
|
nullifier_created_hashes: vec![],
|
2025-03-16 11:05:55 -04:00
|
|
|
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(
|
|
|
|
|
receipt,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
2025-01-03 12:43:05 +02:00
|
|
|
encoded_data,
|
|
|
|
|
ephemeral_pub_key: eph_pub_key.to_vec(),
|
2025-04-09 01:34:57 -04:00
|
|
|
commitment,
|
|
|
|
|
tweak,
|
|
|
|
|
secret_r: secret_r.try_into().unwrap(),
|
2025-01-03 12:43:05 +02:00
|
|
|
}
|
|
|
|
|
.into(),
|
|
|
|
|
result_hashes,
|
2025-01-10 03:00:32 +02:00
|
|
|
))
|
2025-01-03 12:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
2024-12-22 16:14:52 +02:00
|
|
|
pub async fn transfer_utxo_private(
|
|
|
|
|
&self,
|
|
|
|
|
utxo: UTXO,
|
2024-12-30 08:44:26 +02:00
|
|
|
commitment_in: [u8; 32],
|
2024-12-22 16:14:52 +02:00
|
|
|
receivers: Vec<(u128, AccountAddress)>,
|
2025-01-10 03:00:32 +02:00
|
|
|
) -> Result<(Transaction, Vec<(AccountAddress, [u8; 32])>), ExecutionFailureKind> {
|
2024-12-25 09:50:54 +02:00
|
|
|
let acc_map_read_guard = self.storage.read().await;
|
|
|
|
|
|
2025-04-09 01:35:19 -04:00
|
|
|
let account = acc_map_read_guard.acc_map.get(&utxo.owner).unwrap();
|
2024-12-22 16:14:52 +02:00
|
|
|
|
|
|
|
|
let nullifier = generate_nullifiers(
|
|
|
|
|
&utxo,
|
2025-04-09 01:35:19 -04:00
|
|
|
&account
|
2024-12-22 16:14:52 +02:00
|
|
|
.key_holder
|
|
|
|
|
.utxo_secret_key_holder
|
|
|
|
|
.nullifier_secret_key
|
|
|
|
|
.to_bytes()
|
|
|
|
|
.to_vec(),
|
|
|
|
|
);
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
let (resulting_utxos, receipt) = prove_send_utxo(utxo, receivers)?;
|
2024-12-26 11:38:00 +02:00
|
|
|
let utxo_hashes = resulting_utxos
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(utxo, addr)| (addr.clone(), utxo.hash))
|
|
|
|
|
.collect();
|
2024-12-22 16:14:52 +02:00
|
|
|
|
|
|
|
|
let utxos: Vec<UTXO> = resulting_utxos
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(utxo, _)| utxo.clone())
|
|
|
|
|
.collect();
|
|
|
|
|
|
2025-04-09 01:35:19 -04:00
|
|
|
let ephm_key_holder = &account.produce_ephemeral_key_holder();
|
2025-01-03 08:13:59 +02:00
|
|
|
ephm_key_holder.log();
|
2024-12-25 09:50:54 +02:00
|
|
|
|
2025-04-04 14:14:04 -04:00
|
|
|
let eph_pub_key =
|
|
|
|
|
serde_json::to_vec(&ephm_key_holder.generate_ephemeral_public_key()).unwrap();
|
2024-12-25 09:50:54 +02:00
|
|
|
|
2025-03-28 17:12:28 +02:00
|
|
|
let encoded_data: Vec<(Vec<u8>, Vec<u8>, u8)> = utxos
|
2024-12-22 16:14:52 +02:00
|
|
|
.iter()
|
|
|
|
|
.map(|utxo_enc| {
|
2024-12-25 09:50:54 +02:00
|
|
|
let accout_enc = acc_map_read_guard.acc_map.get(&utxo_enc.owner).unwrap();
|
2024-12-22 16:14:52 +02:00
|
|
|
|
|
|
|
|
let (ciphertext, nonce) = Account::encrypt_data(
|
2024-12-25 09:50:54 +02:00
|
|
|
&ephm_key_holder,
|
2024-12-22 16:14:52 +02:00
|
|
|
accout_enc.key_holder.viewing_public_key,
|
|
|
|
|
&serde_json::to_vec(&utxo_enc).unwrap(),
|
|
|
|
|
);
|
|
|
|
|
|
2025-03-28 17:12:28 +02:00
|
|
|
let tag = accout_enc.make_tag();
|
|
|
|
|
|
|
|
|
|
(ciphertext, nonce.to_vec(), tag)
|
2024-12-22 16:14:52 +02:00
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let commitments = generate_commitments(&utxos);
|
2025-04-09 01:35:19 -04:00
|
|
|
|
|
|
|
|
// TODO: fix address when correspoding method will be added
|
|
|
|
|
let sc_addr = "";
|
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
let secret_r: [u8; 32] = rng.gen();
|
|
|
|
|
|
|
|
|
|
let sc_state = acc_map_read_guard.block_store.get_sc_sc_state(sc_addr).map_err(ExecutionFailureKind::db_error)?;
|
|
|
|
|
|
|
|
|
|
let mut vec_values_u64: Vec<Vec<u64>> = sc_state.into_iter().map(|slice| Self::vec_u8_to_vec_u64(slice.to_vec())).collect();
|
|
|
|
|
|
|
|
|
|
let context = acc_map_read_guard.produce_context(account.address);
|
|
|
|
|
|
|
|
|
|
let serialized_context = serde_json::to_vec(&context).unwrap();
|
|
|
|
|
|
|
|
|
|
let serialized_context_u64 = Self::vec_u8_to_vec_u64(serialized_context);
|
|
|
|
|
|
|
|
|
|
vec_values_u64.push(serialized_context_u64);
|
|
|
|
|
|
|
|
|
|
let vec_public_info: Vec<u64> = vec_values_u64.into_iter().flatten().collect();
|
|
|
|
|
|
|
|
|
|
let (tweak, secret_r, commitment) = new_commitment_vec(vec_public_info, &secret_r);
|
2024-12-22 16:14:52 +02:00
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
Ok((
|
2024-12-26 11:38:00 +02:00
|
|
|
TransactionPayload {
|
|
|
|
|
tx_kind: TxKind::Private,
|
|
|
|
|
execution_input: vec![],
|
|
|
|
|
execution_output: vec![],
|
|
|
|
|
utxo_commitments_spent_hashes: vec![commitment_in],
|
|
|
|
|
utxo_commitments_created_hashes: commitments
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|hash_data| hash_data.try_into().unwrap())
|
|
|
|
|
.collect(),
|
|
|
|
|
nullifier_created_hashes: vec![nullifier.try_into().unwrap()],
|
2025-03-16 11:05:55 -04:00
|
|
|
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(
|
|
|
|
|
receipt,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
2024-12-26 11:38:00 +02:00
|
|
|
encoded_data,
|
|
|
|
|
ephemeral_pub_key: eph_pub_key.to_vec(),
|
2025-04-09 01:35:19 -04:00
|
|
|
commitment,
|
|
|
|
|
tweak,
|
|
|
|
|
secret_r: secret_r.try_into().unwrap(),
|
2024-12-26 11:38:00 +02:00
|
|
|
}
|
|
|
|
|
.into(),
|
|
|
|
|
utxo_hashes,
|
2025-01-10 03:00:32 +02:00
|
|
|
))
|
2024-12-22 16:14:52 +02:00
|
|
|
}
|
|
|
|
|
|
2025-01-03 12:43:05 +02:00
|
|
|
pub async fn transfer_utxo_multiple_assets_private(
|
|
|
|
|
&self,
|
|
|
|
|
utxos: Vec<UTXO>,
|
|
|
|
|
commitments_in: Vec<[u8; 32]>,
|
|
|
|
|
number_to_send: usize,
|
|
|
|
|
receiver: AccountAddress,
|
2025-01-10 03:00:32 +02:00
|
|
|
) -> Result<(Transaction, Vec<[u8; 32]>, Vec<[u8; 32]>), ExecutionFailureKind> {
|
2025-01-03 12:43:05 +02:00
|
|
|
let acc_map_read_guard = self.storage.read().await;
|
|
|
|
|
|
2025-04-09 01:35:44 -04:00
|
|
|
let account = acc_map_read_guard.acc_map.get(&utxos[0].owner).unwrap();
|
2025-01-03 12:43:05 +02:00
|
|
|
|
2025-04-09 01:35:44 -04:00
|
|
|
let nsk = account
|
2025-01-03 12:43:05 +02:00
|
|
|
.key_holder
|
|
|
|
|
.utxo_secret_key_holder
|
|
|
|
|
.nullifier_secret_key
|
|
|
|
|
.to_bytes()
|
|
|
|
|
.to_vec();
|
|
|
|
|
|
|
|
|
|
let nullifiers = utxos
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|utxo| generate_nullifiers(utxo, &nsk))
|
|
|
|
|
.map(|vecc| vecc.try_into().unwrap())
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let (resulting_utxos_receiver, resulting_utxos_not_spent, receipt) =
|
2025-01-10 03:00:32 +02:00
|
|
|
prove_send_utxo_multiple_assets_one_receiver(utxos, number_to_send, receiver)?;
|
2025-01-03 12:43:05 +02:00
|
|
|
|
|
|
|
|
let utxo_hashes_receiver = resulting_utxos_receiver
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|utxo| utxo.hash)
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let utxo_hashes_not_spent = resulting_utxos_not_spent
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|utxo| utxo.hash)
|
|
|
|
|
.collect();
|
|
|
|
|
|
2025-04-09 01:35:44 -04:00
|
|
|
let ephm_key_holder = &account.produce_ephemeral_key_holder();
|
2025-01-03 12:43:05 +02:00
|
|
|
ephm_key_holder.log();
|
|
|
|
|
|
2025-04-04 14:14:04 -04:00
|
|
|
let eph_pub_key =
|
|
|
|
|
serde_json::to_vec(&ephm_key_holder.generate_ephemeral_public_key()).unwrap();
|
2025-01-03 12:43:05 +02:00
|
|
|
|
2025-03-28 17:12:28 +02:00
|
|
|
let mut encoded_data: Vec<(Vec<u8>, Vec<u8>, u8)> = resulting_utxos_receiver
|
2025-01-03 12:43:05 +02:00
|
|
|
.iter()
|
|
|
|
|
.map(|utxo_enc| {
|
|
|
|
|
let accout_enc = acc_map_read_guard.acc_map.get(&utxo_enc.owner).unwrap();
|
|
|
|
|
|
|
|
|
|
let (ciphertext, nonce) = Account::encrypt_data(
|
|
|
|
|
&ephm_key_holder,
|
|
|
|
|
accout_enc.key_holder.viewing_public_key,
|
|
|
|
|
&serde_json::to_vec(&utxo_enc).unwrap(),
|
|
|
|
|
);
|
|
|
|
|
|
2025-03-28 17:12:28 +02:00
|
|
|
let tag = accout_enc.make_tag();
|
|
|
|
|
|
|
|
|
|
(ciphertext, nonce.to_vec(), tag)
|
2025-01-03 12:43:05 +02:00
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
2025-03-28 17:12:28 +02:00
|
|
|
let encoded_data_1: Vec<(Vec<u8>, Vec<u8>, u8)> = resulting_utxos_not_spent
|
2025-01-03 12:43:05 +02:00
|
|
|
.iter()
|
|
|
|
|
.map(|utxo_enc| {
|
|
|
|
|
let accout_enc = acc_map_read_guard.acc_map.get(&utxo_enc.owner).unwrap();
|
|
|
|
|
|
|
|
|
|
let (ciphertext, nonce) = Account::encrypt_data(
|
|
|
|
|
&ephm_key_holder,
|
|
|
|
|
accout_enc.key_holder.viewing_public_key,
|
|
|
|
|
&serde_json::to_vec(&utxo_enc).unwrap(),
|
|
|
|
|
);
|
|
|
|
|
|
2025-03-28 17:12:28 +02:00
|
|
|
let tag = accout_enc.make_tag();
|
|
|
|
|
|
|
|
|
|
(ciphertext, nonce.to_vec(), tag)
|
2025-01-03 12:43:05 +02:00
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
encoded_data.extend(encoded_data_1);
|
|
|
|
|
|
|
|
|
|
let mut commitments = generate_commitments(&resulting_utxos_receiver);
|
|
|
|
|
let commitments_1 = generate_commitments(&resulting_utxos_not_spent);
|
|
|
|
|
|
|
|
|
|
commitments.extend(commitments_1);
|
|
|
|
|
|
2025-04-09 01:35:44 -04:00
|
|
|
// TODO: fix address when correspoding method will be added
|
|
|
|
|
let sc_addr = "";
|
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
let secret_r: [u8; 32] = rng.gen();
|
|
|
|
|
|
|
|
|
|
let sc_state = acc_map_read_guard.block_store.get_sc_sc_state(sc_addr).map_err(ExecutionFailureKind::db_error)?;
|
|
|
|
|
|
|
|
|
|
let mut vec_values_u64: Vec<Vec<u64>> = sc_state.into_iter().map(|slice| Self::vec_u8_to_vec_u64(slice.to_vec())).collect();
|
|
|
|
|
|
|
|
|
|
let context = acc_map_read_guard.produce_context(account.address);
|
|
|
|
|
|
|
|
|
|
let serialized_context = serde_json::to_vec(&context).unwrap();
|
|
|
|
|
|
|
|
|
|
let serialized_context_u64 = Self::vec_u8_to_vec_u64(serialized_context);
|
|
|
|
|
|
|
|
|
|
vec_values_u64.push(serialized_context_u64);
|
|
|
|
|
|
|
|
|
|
let vec_public_info: Vec<u64> = vec_values_u64.into_iter().flatten().collect();
|
|
|
|
|
|
|
|
|
|
let (tweak, secret_r, commitment) = new_commitment_vec(vec_public_info, &secret_r);
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
Ok((
|
2025-01-03 12:43:05 +02:00
|
|
|
TransactionPayload {
|
|
|
|
|
tx_kind: TxKind::Private,
|
|
|
|
|
execution_input: vec![],
|
|
|
|
|
execution_output: vec![],
|
|
|
|
|
utxo_commitments_spent_hashes: commitments_in,
|
|
|
|
|
utxo_commitments_created_hashes: commitments
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|hash_data| hash_data.try_into().unwrap())
|
|
|
|
|
.collect(),
|
|
|
|
|
nullifier_created_hashes: nullifiers,
|
2025-03-16 11:05:55 -04:00
|
|
|
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(
|
|
|
|
|
receipt,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
2025-01-03 12:43:05 +02:00
|
|
|
encoded_data,
|
|
|
|
|
ephemeral_pub_key: eph_pub_key.to_vec(),
|
2025-04-09 01:35:44 -04:00
|
|
|
commitment,
|
|
|
|
|
tweak,
|
|
|
|
|
secret_r: secret_r.try_into().unwrap(),
|
2025-01-03 12:43:05 +02:00
|
|
|
}
|
|
|
|
|
.into(),
|
|
|
|
|
utxo_hashes_receiver,
|
|
|
|
|
utxo_hashes_not_spent,
|
2025-01-10 03:00:32 +02:00
|
|
|
))
|
2025-01-03 12:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
2024-12-22 16:14:52 +02:00
|
|
|
pub async fn transfer_balance_shielded(
|
|
|
|
|
&self,
|
|
|
|
|
acc: AccountAddress,
|
|
|
|
|
balance: u64,
|
|
|
|
|
receivers: Vec<(u128, AccountAddress)>,
|
2025-01-10 03:00:32 +02:00
|
|
|
) -> Result<(Transaction, Vec<(AccountAddress, [u8; 32])>), ExecutionFailureKind> {
|
2024-12-25 09:50:54 +02:00
|
|
|
let acc_map_read_guard = self.storage.read().await;
|
|
|
|
|
|
2025-03-16 11:05:44 -04:00
|
|
|
let account = acc_map_read_guard.acc_map.get(&acc).unwrap();
|
2024-12-22 16:14:52 +02:00
|
|
|
|
2025-04-09 01:36:07 -04:00
|
|
|
// TODO: add to transaction structure and do the check. Research has to update the scheme as well.
|
2025-03-16 11:05:55 -04:00
|
|
|
let commitment = sc_core::transaction_payloads_tools::generate_secret_random_commitment(
|
|
|
|
|
balance, account,
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
2024-12-22 16:14:52 +02:00
|
|
|
|
|
|
|
|
let nullifier = executions::se::generate_nullifiers(
|
|
|
|
|
&commitment,
|
2025-03-16 11:05:44 -04:00
|
|
|
&account
|
2024-12-22 16:14:52 +02:00
|
|
|
.key_holder
|
|
|
|
|
.utxo_secret_key_holder
|
|
|
|
|
.nullifier_secret_key
|
|
|
|
|
.to_bytes()
|
|
|
|
|
.to_vec(),
|
|
|
|
|
);
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
let (resulting_utxos, receipt) = prove_send_utxo_shielded(acc, balance as u128, receivers)?;
|
2024-12-26 11:38:00 +02:00
|
|
|
let utxo_hashes = resulting_utxos
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(utxo, addr)| (addr.clone(), utxo.hash))
|
|
|
|
|
.collect();
|
2024-12-22 16:14:52 +02:00
|
|
|
|
|
|
|
|
let utxos: Vec<UTXO> = resulting_utxos
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(utxo, _)| utxo.clone())
|
|
|
|
|
.collect();
|
|
|
|
|
|
2025-03-16 11:05:44 -04:00
|
|
|
let ephm_key_holder = &account.produce_ephemeral_key_holder();
|
2025-01-03 08:13:59 +02:00
|
|
|
ephm_key_holder.log();
|
2024-12-25 09:50:54 +02:00
|
|
|
|
2025-04-04 14:14:04 -04:00
|
|
|
let eph_pub_key =
|
|
|
|
|
serde_json::to_vec(&ephm_key_holder.generate_ephemeral_public_key()).unwrap();
|
2024-12-25 09:50:54 +02:00
|
|
|
|
2025-03-28 17:12:28 +02:00
|
|
|
let encoded_data: Vec<(Vec<u8>, Vec<u8>, u8)> = utxos
|
2024-12-22 16:14:52 +02:00
|
|
|
.iter()
|
|
|
|
|
.map(|utxo_enc| {
|
2024-12-25 09:50:54 +02:00
|
|
|
let accout_enc = acc_map_read_guard.acc_map.get(&utxo_enc.owner).unwrap();
|
2024-12-22 16:14:52 +02:00
|
|
|
|
|
|
|
|
let (ciphertext, nonce) = Account::encrypt_data(
|
2024-12-25 09:50:54 +02:00
|
|
|
&ephm_key_holder,
|
2024-12-22 16:14:52 +02:00
|
|
|
accout_enc.key_holder.viewing_public_key,
|
|
|
|
|
&serde_json::to_vec(&utxo_enc).unwrap(),
|
|
|
|
|
);
|
|
|
|
|
|
2025-03-28 17:12:28 +02:00
|
|
|
let tag = accout_enc.make_tag();
|
|
|
|
|
|
|
|
|
|
(ciphertext, nonce.to_vec(), tag)
|
2024-12-22 16:14:52 +02:00
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let commitments = generate_commitments(&utxos);
|
|
|
|
|
|
2025-04-09 01:36:07 -04:00
|
|
|
// TODO: fix address when correspoding method will be added
|
|
|
|
|
let sc_addr = "";
|
|
|
|
|
|
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
let secret_r: [u8; 32] = rng.gen();
|
|
|
|
|
|
|
|
|
|
let sc_state = acc_map_read_guard.block_store.get_sc_sc_state(sc_addr).map_err(ExecutionFailureKind::db_error)?;
|
|
|
|
|
|
|
|
|
|
let mut vec_values_u64: Vec<Vec<u64>> = sc_state.into_iter().map(|slice| Self::vec_u8_to_vec_u64(slice.to_vec())).collect();
|
|
|
|
|
|
|
|
|
|
let serialized_context_u64 = Self::vec_u8_to_vec_u64(serde_json::to_vec(&acc_map_read_guard.produce_context(account.address)).unwrap());
|
|
|
|
|
|
|
|
|
|
vec_values_u64.push(serialized_context_u64);
|
|
|
|
|
|
|
|
|
|
let vec_public_info: Vec<u64> = vec_values_u64.into_iter().flatten().collect();
|
|
|
|
|
|
|
|
|
|
let (tweak, secret_r, commitment) = new_commitment_vec(vec_public_info, &secret_r);
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
Ok((
|
2024-12-26 11:38:00 +02:00
|
|
|
TransactionPayload {
|
2024-12-30 07:43:27 +02:00
|
|
|
tx_kind: TxKind::Shielded,
|
2024-12-26 11:38:00 +02:00
|
|
|
execution_input: serde_json::to_vec(&ActionData::SendMoneyShieldedTx(
|
|
|
|
|
SendMoneyShieldedTx {
|
|
|
|
|
acc_sender: acc,
|
|
|
|
|
amount: balance as u128,
|
|
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
.unwrap(),
|
|
|
|
|
execution_output: vec![],
|
|
|
|
|
utxo_commitments_spent_hashes: vec![],
|
|
|
|
|
utxo_commitments_created_hashes: commitments
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|hash_data| hash_data.try_into().unwrap())
|
|
|
|
|
.collect(),
|
|
|
|
|
nullifier_created_hashes: vec![nullifier.try_into().unwrap()],
|
2025-03-16 11:05:55 -04:00
|
|
|
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(
|
|
|
|
|
receipt,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
2024-12-26 11:38:00 +02:00
|
|
|
encoded_data,
|
|
|
|
|
ephemeral_pub_key: eph_pub_key.to_vec(),
|
2025-04-09 01:36:07 -04:00
|
|
|
commitment,
|
|
|
|
|
tweak,
|
|
|
|
|
secret_r: secret_r.try_into().unwrap(),
|
2024-12-26 11:38:00 +02:00
|
|
|
}
|
|
|
|
|
.into(),
|
|
|
|
|
utxo_hashes,
|
2025-01-10 03:00:32 +02:00
|
|
|
))
|
2024-12-22 16:14:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn transfer_utxo_deshielded(
|
|
|
|
|
&self,
|
|
|
|
|
utxo: UTXO,
|
2024-12-30 07:43:27 +02:00
|
|
|
comm_gen_hash: [u8; 32],
|
2024-12-22 16:14:52 +02:00
|
|
|
receivers: Vec<(u128, AccountAddress)>,
|
2025-01-10 03:00:32 +02:00
|
|
|
) -> Result<Transaction, ExecutionFailureKind> {
|
2024-12-30 07:00:59 +02:00
|
|
|
let acc_map_read_guard = self.storage.read().await;
|
|
|
|
|
|
2024-12-30 09:10:04 +02:00
|
|
|
let commitment_in = acc_map_read_guard
|
|
|
|
|
.utxo_commitments_store
|
|
|
|
|
.get_tx(comm_gen_hash)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.hash;
|
2024-12-30 07:00:59 +02:00
|
|
|
|
2025-04-09 01:36:23 -04:00
|
|
|
let account = acc_map_read_guard.acc_map.get(&utxo.owner).unwrap();
|
2024-12-22 16:14:52 +02:00
|
|
|
|
2024-12-30 08:00:24 +02:00
|
|
|
let nullifier = generate_nullifiers(
|
2024-12-30 09:10:04 +02:00
|
|
|
&utxo,
|
2025-04-09 01:36:23 -04:00
|
|
|
&account
|
2024-12-30 09:10:04 +02:00
|
|
|
.key_holder
|
|
|
|
|
.utxo_secret_key_holder
|
|
|
|
|
.nullifier_secret_key
|
|
|
|
|
.to_bytes()
|
|
|
|
|
.to_vec(),
|
|
|
|
|
);
|
2024-12-22 16:14:52 +02:00
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
let (resulting_balances, receipt) = prove_send_utxo_deshielded(utxo, receivers)?;
|
2024-12-22 16:14:52 +02:00
|
|
|
|
2025-04-09 01:36:23 -04:00
|
|
|
// TODO: fix address when correspoding method will be added
|
|
|
|
|
let sc_addr = "";
|
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
let secret_r: [u8; 32] = rng.gen();
|
|
|
|
|
|
|
|
|
|
let sc_state = acc_map_read_guard.block_store.get_sc_sc_state(sc_addr).map_err(ExecutionFailureKind::db_error)?;
|
|
|
|
|
|
|
|
|
|
let mut vec_values_u64: Vec<Vec<u64>> = sc_state.into_iter().map(|slice| Self::vec_u8_to_vec_u64(slice.to_vec())).collect();
|
|
|
|
|
|
|
|
|
|
let context = acc_map_read_guard.produce_context(account.address);
|
|
|
|
|
|
|
|
|
|
let serialized_context = serde_json::to_vec(&context).unwrap();
|
|
|
|
|
|
|
|
|
|
let serialized_context_u64 = Self::vec_u8_to_vec_u64(serialized_context);
|
|
|
|
|
|
|
|
|
|
vec_values_u64.push(serialized_context_u64);
|
|
|
|
|
|
|
|
|
|
let vec_public_info: Vec<u64> = vec_values_u64.into_iter().flatten().collect();
|
|
|
|
|
|
|
|
|
|
let (tweak, secret_r, commitment) = new_commitment_vec(vec_public_info, &secret_r);
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
Ok(TransactionPayload {
|
2024-12-30 07:43:27 +02:00
|
|
|
tx_kind: TxKind::Deshielded,
|
2024-12-30 08:00:24 +02:00
|
|
|
execution_input: serde_json::to_vec(&ActionData::SendMoneyDeshieldedTx(
|
2024-12-22 16:14:52 +02:00
|
|
|
SendMoneyDeshieldedTx {
|
2024-12-26 11:38:00 +02:00
|
|
|
receiver_data: resulting_balances,
|
2024-12-22 16:14:52 +02:00
|
|
|
},
|
|
|
|
|
))
|
|
|
|
|
.unwrap(),
|
2024-12-30 08:00:24 +02:00
|
|
|
execution_output: vec![],
|
2024-12-22 16:14:52 +02:00
|
|
|
utxo_commitments_spent_hashes: vec![commitment_in],
|
|
|
|
|
utxo_commitments_created_hashes: vec![],
|
|
|
|
|
nullifier_created_hashes: vec![nullifier.try_into().unwrap()],
|
2025-03-16 11:05:55 -04:00
|
|
|
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(receipt)
|
|
|
|
|
.unwrap(),
|
2024-12-22 16:14:52 +02:00
|
|
|
encoded_data: vec![],
|
2024-12-25 09:50:54 +02:00
|
|
|
ephemeral_pub_key: vec![],
|
2025-04-09 01:36:23 -04:00
|
|
|
commitment,
|
|
|
|
|
tweak,
|
|
|
|
|
secret_r: secret_r.try_into().unwrap(),
|
2024-12-22 16:14:52 +02:00
|
|
|
}
|
2025-01-10 03:00:32 +02:00
|
|
|
.into())
|
2024-12-22 16:14:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn send_private_mint_tx(
|
|
|
|
|
&self,
|
|
|
|
|
acc: AccountAddress,
|
|
|
|
|
amount: u128,
|
2025-02-05 12:24:09 +02:00
|
|
|
) -> Result<(SendTxResponse, [u8; 32], [u8; 32]), ExecutionFailureKind> {
|
2025-01-24 09:10:42 +02:00
|
|
|
//Considering proof time, needs to be done before proof
|
|
|
|
|
let tx_roots = self.get_roots().await;
|
|
|
|
|
|
2024-12-26 11:38:00 +02:00
|
|
|
let point_before_prove = std::time::Instant::now();
|
2025-01-10 03:00:32 +02:00
|
|
|
let (tx, utxo_hash) = self.mint_utxo_private(acc, amount).await?;
|
2024-12-30 07:35:05 +01:00
|
|
|
tx.log();
|
2024-12-26 11:38:00 +02:00
|
|
|
let point_after_prove = std::time::Instant::now();
|
|
|
|
|
|
2024-12-30 07:43:27 +02:00
|
|
|
let commitment_generated_hash = tx.utxo_commitments_created_hashes[0];
|
|
|
|
|
|
2024-12-26 11:38:00 +02:00
|
|
|
let timedelta = (point_after_prove - point_before_prove).as_millis();
|
|
|
|
|
info!("Mint utxo proof spent {timedelta:?} milliseconds");
|
|
|
|
|
|
2024-12-30 09:10:04 +02:00
|
|
|
Ok((
|
2025-01-24 09:10:42 +02:00
|
|
|
self.sequencer_client.send_tx(tx, tx_roots).await?,
|
2024-12-30 09:10:04 +02:00
|
|
|
utxo_hash,
|
|
|
|
|
commitment_generated_hash,
|
|
|
|
|
))
|
2024-12-22 16:14:52 +02:00
|
|
|
}
|
|
|
|
|
|
2025-01-03 12:43:05 +02:00
|
|
|
pub async fn send_private_mint_multiple_assets_tx(
|
|
|
|
|
&self,
|
|
|
|
|
acc: AccountAddress,
|
|
|
|
|
amount: u128,
|
|
|
|
|
number_of_assets: usize,
|
2025-02-05 12:24:09 +02:00
|
|
|
) -> Result<(SendTxResponse, Vec<[u8; 32]>, Vec<[u8; 32]>), ExecutionFailureKind> {
|
2025-01-24 09:10:42 +02:00
|
|
|
//Considering proof time, needs to be done before proof
|
|
|
|
|
let tx_roots = self.get_roots().await;
|
|
|
|
|
|
2025-01-03 12:43:05 +02:00
|
|
|
let point_before_prove = std::time::Instant::now();
|
|
|
|
|
let (tx, utxo_hashes) = self
|
|
|
|
|
.mint_utxo_multiple_assets_private(acc, amount, number_of_assets)
|
2025-01-10 03:00:32 +02:00
|
|
|
.await?;
|
2025-01-03 12:43:05 +02:00
|
|
|
tx.log();
|
|
|
|
|
let point_after_prove = std::time::Instant::now();
|
|
|
|
|
|
|
|
|
|
let commitment_generated_hashes = tx.utxo_commitments_created_hashes.clone();
|
|
|
|
|
|
|
|
|
|
let timedelta = (point_after_prove - point_before_prove).as_millis();
|
|
|
|
|
info!("Mint utxo proof spent {timedelta:?} milliseconds");
|
|
|
|
|
|
|
|
|
|
Ok((
|
2025-01-24 09:10:42 +02:00
|
|
|
self.sequencer_client.send_tx(tx, tx_roots).await?,
|
2025-01-03 12:43:05 +02:00
|
|
|
utxo_hashes,
|
|
|
|
|
commitment_generated_hashes,
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-22 16:14:52 +02:00
|
|
|
pub async fn send_public_deposit(
|
|
|
|
|
&self,
|
|
|
|
|
acc: AccountAddress,
|
|
|
|
|
amount: u128,
|
2025-02-05 12:24:09 +02:00
|
|
|
) -> Result<SendTxResponse, ExecutionFailureKind> {
|
2025-01-24 09:10:42 +02:00
|
|
|
//Considering proof time, needs to be done before proof
|
|
|
|
|
let tx_roots = self.get_roots().await;
|
|
|
|
|
|
2025-03-16 11:05:55 -04:00
|
|
|
let tx: Transaction =
|
|
|
|
|
sc_core::transaction_payloads_tools::create_public_transaction_payload(
|
|
|
|
|
serde_json::to_vec(&ActionData::MintMoneyPublicTx(MintMoneyPublicTx {
|
|
|
|
|
acc,
|
|
|
|
|
amount,
|
|
|
|
|
}))
|
|
|
|
|
.unwrap(),
|
|
|
|
|
)
|
|
|
|
|
.into();
|
2025-01-03 08:13:59 +02:00
|
|
|
tx.log();
|
|
|
|
|
|
2025-01-24 09:10:42 +02:00
|
|
|
Ok(self.sequencer_client.send_tx(tx, tx_roots).await?)
|
2024-12-22 16:14:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn send_private_send_tx(
|
|
|
|
|
&self,
|
|
|
|
|
utxo: UTXO,
|
2024-12-30 08:44:26 +02:00
|
|
|
comm_hash: [u8; 32],
|
2024-12-22 16:14:52 +02:00
|
|
|
receivers: Vec<(u128, AccountAddress)>,
|
2025-02-05 12:24:09 +02:00
|
|
|
) -> Result<(SendTxResponse, Vec<([u8; 32], [u8; 32])>), ExecutionFailureKind> {
|
2025-01-24 09:10:42 +02:00
|
|
|
//Considering proof time, needs to be done before proof
|
|
|
|
|
let tx_roots = self.get_roots().await;
|
|
|
|
|
|
2024-12-26 11:38:00 +02:00
|
|
|
let point_before_prove = std::time::Instant::now();
|
2025-01-10 03:00:32 +02:00
|
|
|
let (tx, utxo_hashes) = self
|
|
|
|
|
.transfer_utxo_private(utxo, comm_hash, receivers)
|
|
|
|
|
.await?;
|
2024-12-30 07:35:05 +01:00
|
|
|
tx.log();
|
2024-12-26 11:38:00 +02:00
|
|
|
let point_after_prove = std::time::Instant::now();
|
|
|
|
|
|
|
|
|
|
let timedelta = (point_after_prove - point_before_prove).as_millis();
|
|
|
|
|
info!("Send private utxo proof spent {timedelta:?} milliseconds");
|
|
|
|
|
|
2025-01-24 09:10:42 +02:00
|
|
|
Ok((
|
|
|
|
|
self.sequencer_client.send_tx(tx, tx_roots).await?,
|
|
|
|
|
utxo_hashes,
|
|
|
|
|
))
|
2024-12-22 16:14:52 +02:00
|
|
|
}
|
|
|
|
|
|
2025-01-03 12:43:05 +02:00
|
|
|
pub async fn send_private_multiple_assets_send_tx(
|
|
|
|
|
&self,
|
|
|
|
|
utxos: Vec<UTXO>,
|
|
|
|
|
comm_hashes: Vec<[u8; 32]>,
|
|
|
|
|
number_to_send: usize,
|
|
|
|
|
receiver: AccountAddress,
|
2025-02-05 12:24:09 +02:00
|
|
|
) -> Result<(SendTxResponse, Vec<[u8; 32]>, Vec<[u8; 32]>), ExecutionFailureKind> {
|
2025-01-24 09:10:42 +02:00
|
|
|
//Considering proof time, needs to be done before proof
|
|
|
|
|
let tx_roots = self.get_roots().await;
|
|
|
|
|
|
2025-01-03 12:43:05 +02:00
|
|
|
let point_before_prove = std::time::Instant::now();
|
|
|
|
|
let (tx, utxo_hashes_received, utxo_hashes_not_spent) = self
|
|
|
|
|
.transfer_utxo_multiple_assets_private(utxos, comm_hashes, number_to_send, receiver)
|
2025-01-10 03:00:32 +02:00
|
|
|
.await?;
|
2025-01-03 12:43:05 +02:00
|
|
|
tx.log();
|
|
|
|
|
let point_after_prove = std::time::Instant::now();
|
|
|
|
|
|
|
|
|
|
let timedelta = (point_after_prove - point_before_prove).as_millis();
|
|
|
|
|
info!("Send private utxo proof spent {timedelta:?} milliseconds");
|
|
|
|
|
|
|
|
|
|
Ok((
|
2025-01-24 09:10:42 +02:00
|
|
|
self.sequencer_client.send_tx(tx, tx_roots).await?,
|
2025-01-03 12:43:05 +02:00
|
|
|
utxo_hashes_received,
|
|
|
|
|
utxo_hashes_not_spent,
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-22 16:14:52 +02:00
|
|
|
pub async fn send_shielded_send_tx(
|
|
|
|
|
&self,
|
|
|
|
|
acc: AccountAddress,
|
|
|
|
|
amount: u64,
|
|
|
|
|
receivers: Vec<(u128, AccountAddress)>,
|
2025-02-05 12:24:09 +02:00
|
|
|
) -> Result<(SendTxResponse, Vec<([u8; 32], [u8; 32])>), ExecutionFailureKind> {
|
2025-01-24 09:10:42 +02:00
|
|
|
//Considering proof time, needs to be done before proof
|
|
|
|
|
let tx_roots = self.get_roots().await;
|
|
|
|
|
|
2024-12-26 11:38:00 +02:00
|
|
|
let point_before_prove = std::time::Instant::now();
|
2025-01-10 03:00:32 +02:00
|
|
|
let (tx, utxo_hashes) = self
|
|
|
|
|
.transfer_balance_shielded(acc, amount, receivers)
|
|
|
|
|
.await?;
|
2024-12-30 07:35:05 +01:00
|
|
|
tx.log();
|
2024-12-26 11:38:00 +02:00
|
|
|
let point_after_prove = std::time::Instant::now();
|
|
|
|
|
|
|
|
|
|
let timedelta = (point_after_prove - point_before_prove).as_millis();
|
|
|
|
|
info!("Send balance shielded proof spent {timedelta:?} milliseconds");
|
|
|
|
|
|
2025-01-24 09:10:42 +02:00
|
|
|
Ok((
|
|
|
|
|
self.sequencer_client.send_tx(tx, tx_roots).await?,
|
|
|
|
|
utxo_hashes,
|
|
|
|
|
))
|
2024-12-22 16:14:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn send_deshielded_send_tx(
|
|
|
|
|
&self,
|
|
|
|
|
utxo: UTXO,
|
2024-12-30 07:43:27 +02:00
|
|
|
comm_gen_hash: [u8; 32],
|
2024-12-22 16:14:52 +02:00
|
|
|
receivers: Vec<(u128, AccountAddress)>,
|
2025-02-05 12:24:09 +02:00
|
|
|
) -> Result<SendTxResponse, ExecutionFailureKind> {
|
2025-01-24 09:10:42 +02:00
|
|
|
//Considering proof time, needs to be done before proof
|
|
|
|
|
let tx_roots = self.get_roots().await;
|
|
|
|
|
|
2024-12-26 11:38:00 +02:00
|
|
|
let point_before_prove = std::time::Instant::now();
|
2024-12-30 09:10:04 +02:00
|
|
|
let tx = self
|
|
|
|
|
.transfer_utxo_deshielded(utxo, comm_gen_hash, receivers)
|
2025-01-10 03:00:32 +02:00
|
|
|
.await?;
|
2024-12-30 07:35:05 +01:00
|
|
|
tx.log();
|
2024-12-26 11:38:00 +02:00
|
|
|
let point_after_prove = std::time::Instant::now();
|
|
|
|
|
|
|
|
|
|
let timedelta = (point_after_prove - point_before_prove).as_millis();
|
|
|
|
|
info!("Send deshielded utxo proof spent {timedelta:?} milliseconds");
|
|
|
|
|
|
2025-01-24 09:10:42 +02:00
|
|
|
Ok(self.sequencer_client.send_tx(tx, tx_roots).await?)
|
2024-12-22 16:14:52 +02:00
|
|
|
}
|
2024-12-25 09:50:54 +02:00
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
pub async fn operate_account_mint_private(
|
2025-01-03 10:44:06 +02:00
|
|
|
&mut self,
|
|
|
|
|
acc_addr: AccountAddress,
|
|
|
|
|
amount: u128,
|
2025-02-05 12:24:09 +02:00
|
|
|
) -> Result<(UTXO, [u8; 32]), ExecutionFailureKind> {
|
2024-12-30 09:10:04 +02:00
|
|
|
let (resp, new_utxo_hash, comm_gen_hash) =
|
2025-01-10 03:00:32 +02:00
|
|
|
self.send_private_mint_tx(acc_addr, amount).await?;
|
2024-12-25 09:50:54 +02:00
|
|
|
info!("Response for mint private is {resp:?}");
|
|
|
|
|
|
|
|
|
|
info!("Awaiting new blocks");
|
|
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(BLOCK_GEN_DELAY_SECS)).await;
|
|
|
|
|
|
|
|
|
|
let new_utxo = {
|
|
|
|
|
let mut write_guard = self.storage.write().await;
|
|
|
|
|
|
|
|
|
|
let acc = write_guard.acc_map.get_mut(&acc_addr).unwrap();
|
|
|
|
|
|
2025-02-07 15:17:55 -05:00
|
|
|
acc.utxo_tree.get_item(new_utxo_hash)?.unwrap().clone()
|
2024-12-25 09:50:54 +02:00
|
|
|
};
|
2024-12-30 07:35:05 +01:00
|
|
|
|
|
|
|
|
new_utxo.log();
|
2025-01-03 10:44:06 +02:00
|
|
|
info!(
|
|
|
|
|
"Account address is {:?} ,new utxo owner address is {:?}",
|
|
|
|
|
hex::encode(acc_addr),
|
|
|
|
|
hex::encode(new_utxo.owner)
|
|
|
|
|
);
|
|
|
|
|
info!(
|
|
|
|
|
"Account {:?} got new utxo with amount {amount:?}",
|
|
|
|
|
hex::encode(acc_addr)
|
|
|
|
|
);
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
Ok((new_utxo, comm_gen_hash))
|
2025-01-03 10:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
pub async fn operate_account_mint_multiple_assets_private(
|
2025-01-03 12:43:05 +02:00
|
|
|
&mut self,
|
|
|
|
|
acc_addr: AccountAddress,
|
|
|
|
|
amount: u128,
|
|
|
|
|
number_of_assets: usize,
|
2025-02-05 12:24:09 +02:00
|
|
|
) -> Result<(Vec<UTXO>, Vec<[u8; 32]>), ExecutionFailureKind> {
|
2025-01-03 12:43:05 +02:00
|
|
|
let (resp, new_utxo_hashes, comm_gen_hashes) = self
|
|
|
|
|
.send_private_mint_multiple_assets_tx(acc_addr, amount, number_of_assets)
|
2025-01-10 03:00:32 +02:00
|
|
|
.await?;
|
2025-01-03 12:43:05 +02:00
|
|
|
info!("Response for mint multiple assets private is {resp:?}");
|
|
|
|
|
|
|
|
|
|
info!("Awaiting new blocks");
|
|
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(BLOCK_GEN_DELAY_SECS)).await;
|
|
|
|
|
|
|
|
|
|
let new_utxos = {
|
|
|
|
|
let mut write_guard = self.storage.write().await;
|
|
|
|
|
|
|
|
|
|
new_utxo_hashes
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|new_utxo_hash| {
|
|
|
|
|
let acc = write_guard.acc_map.get_mut(&acc_addr).unwrap();
|
|
|
|
|
|
|
|
|
|
let new_utxo = acc
|
|
|
|
|
.utxo_tree
|
|
|
|
|
.get_item(new_utxo_hash)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.clone();
|
|
|
|
|
|
|
|
|
|
new_utxo.log();
|
|
|
|
|
info!(
|
|
|
|
|
"Account address is {:?} ,new utxo owner address is {:?}",
|
|
|
|
|
hex::encode(acc_addr),
|
|
|
|
|
hex::encode(new_utxo.owner)
|
|
|
|
|
);
|
|
|
|
|
info!(
|
|
|
|
|
"Account {:?} got new utxo with amount {amount:?} and asset {:?}",
|
|
|
|
|
hex::encode(acc_addr),
|
|
|
|
|
new_utxo.asset
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
new_utxo
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
Ok((new_utxos, comm_gen_hashes))
|
2025-01-03 12:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
pub async fn operate_account_send_deshielded_one_receiver(
|
2025-01-03 10:44:06 +02:00
|
|
|
&mut self,
|
|
|
|
|
acc_addr_sender: AccountAddress,
|
|
|
|
|
acc_addr_rec: AccountAddress,
|
|
|
|
|
utxo: UTXO,
|
|
|
|
|
comm_gen_hash: [u8; 32],
|
2025-02-05 12:24:09 +02:00
|
|
|
) -> Result<(), ExecutionFailureKind> {
|
2025-01-03 10:44:06 +02:00
|
|
|
let amount = utxo.amount;
|
|
|
|
|
|
|
|
|
|
let old_balance = {
|
|
|
|
|
let acc_map_read_guard = self.storage.read().await;
|
|
|
|
|
|
|
|
|
|
let acc = acc_map_read_guard.acc_map.get(&acc_addr_rec).unwrap();
|
|
|
|
|
|
|
|
|
|
acc.balance
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
info!(
|
|
|
|
|
"Balance of receiver {:?} now is {old_balance:?}",
|
|
|
|
|
hex::encode(acc_addr_rec)
|
|
|
|
|
);
|
2024-12-30 09:10:04 +02:00
|
|
|
|
2024-12-25 09:50:54 +02:00
|
|
|
let resp = self
|
2025-01-03 10:44:06 +02:00
|
|
|
.send_deshielded_send_tx(utxo, comm_gen_hash, vec![(amount, acc_addr_rec)])
|
2025-01-10 03:00:32 +02:00
|
|
|
.await?;
|
2024-12-25 09:50:54 +02:00
|
|
|
info!("Response for send deshielded is {resp:?}");
|
|
|
|
|
|
|
|
|
|
info!("Awaiting new blocks");
|
|
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(BLOCK_GEN_DELAY_SECS)).await;
|
|
|
|
|
|
2024-12-30 07:00:59 +02:00
|
|
|
let new_balance = {
|
|
|
|
|
let acc_map_read_guard = self.storage.read().await;
|
2024-12-30 09:10:04 +02:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
let acc = acc_map_read_guard.acc_map.get(&acc_addr_sender).unwrap();
|
2024-12-30 07:00:59 +02:00
|
|
|
|
|
|
|
|
acc.balance
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
info!(
|
|
|
|
|
"Balance of receiver {:?} now is {:?}, delta is {:?}",
|
|
|
|
|
hex::encode(acc_addr_rec),
|
|
|
|
|
new_balance,
|
|
|
|
|
new_balance - old_balance
|
|
|
|
|
);
|
2025-01-10 03:00:32 +02:00
|
|
|
|
|
|
|
|
Ok(())
|
2024-12-25 09:50:54 +02:00
|
|
|
}
|
2024-12-25 11:21:03 +02:00
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
pub async fn operate_account_deposit_public(
|
|
|
|
|
&mut self,
|
|
|
|
|
acc_addr: AccountAddress,
|
|
|
|
|
amount: u128,
|
2025-02-05 12:24:09 +02:00
|
|
|
) -> Result<(), ExecutionFailureKind> {
|
2025-01-03 10:44:06 +02:00
|
|
|
let old_balance = {
|
|
|
|
|
let acc_map_read_guard = self.storage.read().await;
|
|
|
|
|
|
|
|
|
|
let acc = acc_map_read_guard.acc_map.get(&acc_addr).unwrap();
|
2024-12-25 11:21:03 +02:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
acc.balance
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
info!(
|
|
|
|
|
"Balance of {:?} now is {old_balance:?}",
|
|
|
|
|
hex::encode(acc_addr)
|
|
|
|
|
);
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
let resp = self.send_public_deposit(acc_addr, amount).await?;
|
2024-12-25 11:21:03 +02:00
|
|
|
info!("Response for public deposit is {resp:?}");
|
|
|
|
|
|
|
|
|
|
info!("Awaiting new blocks");
|
|
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(BLOCK_GEN_DELAY_SECS)).await;
|
|
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
let new_balance = {
|
2024-12-30 06:41:08 +01:00
|
|
|
let acc_map_read_guard = self.storage.read().await;
|
|
|
|
|
|
2024-12-30 09:10:04 +02:00
|
|
|
let acc = acc_map_read_guard.acc_map.get(&acc_addr).unwrap();
|
2024-12-25 11:21:03 +02:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
acc.balance
|
2024-12-30 06:41:08 +01:00
|
|
|
};
|
2024-12-25 11:21:03 +02:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
info!(
|
|
|
|
|
"Balance of {:?} now is {new_balance:?}, delta is {:?}",
|
|
|
|
|
hex::encode(acc_addr),
|
|
|
|
|
new_balance - old_balance
|
|
|
|
|
);
|
2025-01-10 03:00:32 +02:00
|
|
|
|
|
|
|
|
Ok(())
|
2025-01-03 10:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
pub async fn operate_account_send_shielded_one_receiver(
|
2025-01-03 10:44:06 +02:00
|
|
|
&mut self,
|
|
|
|
|
acc_addr_sender: AccountAddress,
|
|
|
|
|
acc_addr_rec: AccountAddress,
|
|
|
|
|
amount: u128,
|
2025-02-05 12:24:09 +02:00
|
|
|
) -> Result<UTXO, ExecutionFailureKind> {
|
2024-12-26 11:38:00 +02:00
|
|
|
let (resp, new_utxo_hashes) = self
|
2025-01-03 10:44:06 +02:00
|
|
|
.send_shielded_send_tx(acc_addr_sender, amount as u64, vec![(amount, acc_addr_rec)])
|
2025-01-10 03:00:32 +02:00
|
|
|
.await?;
|
2024-12-25 11:21:03 +02:00
|
|
|
info!("Response for send shielded is {resp:?}");
|
|
|
|
|
|
2024-12-26 11:38:00 +02:00
|
|
|
let new_utxo_hash = new_utxo_hashes[0].1;
|
2024-12-25 11:21:03 +02:00
|
|
|
|
|
|
|
|
info!("Awaiting new blocks");
|
|
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(BLOCK_GEN_DELAY_SECS)).await;
|
|
|
|
|
|
|
|
|
|
let new_utxo = {
|
|
|
|
|
let mut write_guard = self.storage.write().await;
|
|
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
let acc = write_guard.acc_map.get_mut(&acc_addr_rec).unwrap();
|
2024-12-30 07:35:05 +01:00
|
|
|
acc.log();
|
2024-12-25 11:21:03 +02:00
|
|
|
|
2025-02-07 15:17:55 -05:00
|
|
|
acc.utxo_tree.get_item(new_utxo_hash)?.unwrap().clone()
|
2024-12-25 11:21:03 +02:00
|
|
|
};
|
2024-12-30 07:35:05 +01:00
|
|
|
new_utxo.log();
|
2025-01-03 10:44:06 +02:00
|
|
|
info!(
|
|
|
|
|
"Account address is {:?} ,new utxo owner address is {:?}",
|
|
|
|
|
hex::encode(acc_addr_rec),
|
|
|
|
|
hex::encode(new_utxo.owner)
|
|
|
|
|
);
|
|
|
|
|
info!(
|
|
|
|
|
"Account {:?} got new utxo with amount {amount:?}",
|
|
|
|
|
hex::encode(acc_addr_rec)
|
|
|
|
|
);
|
2025-01-10 03:00:32 +02:00
|
|
|
|
|
|
|
|
Ok(new_utxo)
|
2024-12-25 11:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
pub async fn operate_account_send_private_one_receiver(
|
2025-01-03 10:44:06 +02:00
|
|
|
&mut self,
|
|
|
|
|
acc_addr_rec: AccountAddress,
|
|
|
|
|
utxo: UTXO,
|
|
|
|
|
comm_gen_hash: [u8; 32],
|
2025-02-05 12:24:09 +02:00
|
|
|
) -> Result<UTXO, ExecutionFailureKind> {
|
2025-01-03 10:44:06 +02:00
|
|
|
let amount = utxo.amount;
|
2024-12-25 11:21:03 +02:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
let (resp, new_utxo_hashes) = self
|
|
|
|
|
.send_private_send_tx(utxo, comm_gen_hash, vec![(amount, acc_addr_rec)])
|
2025-01-10 03:00:32 +02:00
|
|
|
.await?;
|
2025-01-03 10:44:06 +02:00
|
|
|
info!("Response for send private is {resp:?}");
|
|
|
|
|
|
|
|
|
|
let new_utxo_hash = new_utxo_hashes[0].1;
|
2024-12-25 11:21:03 +02:00
|
|
|
|
|
|
|
|
info!("Awaiting new blocks");
|
|
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(BLOCK_GEN_DELAY_SECS)).await;
|
|
|
|
|
|
|
|
|
|
let new_utxo = {
|
|
|
|
|
let mut write_guard = self.storage.write().await;
|
|
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
let acc = write_guard.acc_map.get_mut(&acc_addr_rec).unwrap();
|
2024-12-30 07:35:05 +01:00
|
|
|
acc.log();
|
2024-12-25 11:21:03 +02:00
|
|
|
|
2025-02-07 15:17:55 -05:00
|
|
|
acc.utxo_tree.get_item(new_utxo_hash)?.unwrap().clone()
|
2024-12-25 11:21:03 +02:00
|
|
|
};
|
2024-12-30 07:35:05 +01:00
|
|
|
new_utxo.log();
|
2025-01-03 10:44:06 +02:00
|
|
|
info!(
|
|
|
|
|
"Account address is {:?} ,new utxo owner address is {:?}",
|
|
|
|
|
hex::encode(acc_addr_rec),
|
|
|
|
|
hex::encode(new_utxo.owner)
|
|
|
|
|
);
|
|
|
|
|
info!(
|
|
|
|
|
"Account {:?} got new utxo with amount {:?}",
|
|
|
|
|
hex::encode(acc_addr_rec),
|
|
|
|
|
new_utxo.amount
|
|
|
|
|
);
|
2025-01-10 03:00:32 +02:00
|
|
|
|
|
|
|
|
Ok(new_utxo)
|
2025-01-03 10:44:06 +02:00
|
|
|
}
|
2024-12-25 11:21:03 +02:00
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
pub async fn operate_account_send_private_multiple_assets_one_receiver(
|
2025-01-03 12:43:05 +02:00
|
|
|
&mut self,
|
|
|
|
|
acc_addr: AccountAddress,
|
|
|
|
|
acc_addr_rec: AccountAddress,
|
|
|
|
|
utxos: Vec<UTXO>,
|
|
|
|
|
comm_gen_hashes: Vec<[u8; 32]>,
|
|
|
|
|
number_to_send: usize,
|
2025-02-05 12:24:09 +02:00
|
|
|
) -> Result<(), ExecutionFailureKind> {
|
2025-01-03 12:43:05 +02:00
|
|
|
let (resp, new_utxo_hashes_rec, new_utxo_hashes_not_sp) = self
|
|
|
|
|
.send_private_multiple_assets_send_tx(
|
|
|
|
|
utxos,
|
|
|
|
|
comm_gen_hashes,
|
|
|
|
|
number_to_send,
|
|
|
|
|
acc_addr_rec,
|
|
|
|
|
)
|
2025-01-10 03:00:32 +02:00
|
|
|
.await?;
|
2025-01-03 12:43:05 +02:00
|
|
|
info!("Response for send private multiple assets is {resp:?}");
|
|
|
|
|
|
|
|
|
|
info!("Awaiting new blocks");
|
|
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(BLOCK_GEN_DELAY_SECS)).await;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
let mut write_guard = self.storage.write().await;
|
|
|
|
|
|
|
|
|
|
for new_utxo_hash in new_utxo_hashes_rec {
|
|
|
|
|
let acc = write_guard.acc_map.get_mut(&acc_addr_rec).unwrap();
|
|
|
|
|
acc.log();
|
|
|
|
|
|
2025-02-07 15:17:55 -05:00
|
|
|
let new_utxo = acc.utxo_tree.get_item(new_utxo_hash)?.unwrap().clone();
|
2025-01-03 12:43:05 +02:00
|
|
|
|
|
|
|
|
new_utxo.log();
|
|
|
|
|
info!(
|
|
|
|
|
"Account address is {:?} ,new utxo owner address is {:?}",
|
|
|
|
|
hex::encode(acc_addr_rec),
|
|
|
|
|
hex::encode(new_utxo.owner)
|
|
|
|
|
);
|
|
|
|
|
info!(
|
|
|
|
|
"Account {:?} got new utxo with amount {:?} and asset {:?}",
|
|
|
|
|
hex::encode(acc_addr_rec),
|
|
|
|
|
new_utxo.amount,
|
|
|
|
|
new_utxo.asset,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for new_utxo_hash in new_utxo_hashes_not_sp {
|
|
|
|
|
let acc = write_guard.acc_map.get_mut(&acc_addr).unwrap();
|
|
|
|
|
acc.log();
|
|
|
|
|
|
2025-02-07 15:17:55 -05:00
|
|
|
let new_utxo = acc.utxo_tree.get_item(new_utxo_hash)?.unwrap().clone();
|
2025-01-03 12:43:05 +02:00
|
|
|
|
|
|
|
|
new_utxo.log();
|
|
|
|
|
info!(
|
|
|
|
|
"Account address is {:?} ,new utxo owner address is {:?}",
|
|
|
|
|
hex::encode(acc_addr),
|
|
|
|
|
hex::encode(new_utxo.owner)
|
|
|
|
|
);
|
|
|
|
|
info!(
|
|
|
|
|
"Account {:?} got new utxo with amount {:?} and asset {:?}",
|
|
|
|
|
hex::encode(acc_addr),
|
|
|
|
|
new_utxo.amount,
|
|
|
|
|
new_utxo.asset,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-10 03:00:32 +02:00
|
|
|
|
|
|
|
|
Ok(())
|
2025-01-03 12:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
pub async fn split_utxo(
|
|
|
|
|
&self,
|
|
|
|
|
utxo: UTXO,
|
|
|
|
|
commitment_in: [u8; 32],
|
|
|
|
|
receivers: Vec<(u128, AccountAddress)>,
|
|
|
|
|
visibility_list: [bool; 3],
|
2025-01-10 03:00:32 +02:00
|
|
|
) -> Result<(Transaction, Vec<(AccountAddress, [u8; 32])>), ExecutionFailureKind> {
|
2025-01-03 10:44:06 +02:00
|
|
|
let acc_map_read_guard = self.storage.read().await;
|
2024-12-30 08:44:26 +02:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
let accout = acc_map_read_guard.acc_map.get(&utxo.owner).unwrap();
|
|
|
|
|
|
|
|
|
|
let nullifier = generate_nullifiers(
|
|
|
|
|
&utxo,
|
|
|
|
|
&accout
|
|
|
|
|
.key_holder
|
|
|
|
|
.utxo_secret_key_holder
|
|
|
|
|
.nullifier_secret_key
|
|
|
|
|
.to_bytes()
|
|
|
|
|
.to_vec(),
|
|
|
|
|
);
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
let (resulting_utxos, receipt) = prove_send_utxo(utxo, receivers)?;
|
2025-01-03 10:44:06 +02:00
|
|
|
let utxo_hashes = resulting_utxos
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(utxo, addr)| (addr.clone(), utxo.hash))
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let utxos: Vec<UTXO> = resulting_utxos
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(utxo, _)| utxo.clone())
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let ephm_key_holder = &accout.produce_ephemeral_key_holder();
|
|
|
|
|
ephm_key_holder.log();
|
|
|
|
|
|
2025-04-04 14:14:04 -04:00
|
|
|
let eph_pub_key =
|
|
|
|
|
serde_json::to_vec(&ephm_key_holder.generate_ephemeral_public_key()).unwrap();
|
2025-01-03 10:44:06 +02:00
|
|
|
|
2025-03-28 17:12:28 +02:00
|
|
|
let encoded_data: Vec<(Vec<u8>, Vec<u8>, u8)> = utxos
|
2025-01-03 10:44:06 +02:00
|
|
|
.iter()
|
|
|
|
|
.map(|utxo_enc| {
|
|
|
|
|
let accout_enc = acc_map_read_guard.acc_map.get(&utxo_enc.owner).unwrap();
|
|
|
|
|
|
|
|
|
|
let (ciphertext, nonce) = Account::encrypt_data(
|
|
|
|
|
&ephm_key_holder,
|
|
|
|
|
accout_enc.key_holder.viewing_public_key,
|
|
|
|
|
&serde_json::to_vec(&utxo_enc).unwrap(),
|
|
|
|
|
);
|
|
|
|
|
|
2025-03-28 17:12:28 +02:00
|
|
|
let tag = accout_enc.make_tag();
|
|
|
|
|
|
|
|
|
|
(ciphertext, nonce.to_vec(), tag)
|
2025-01-03 10:44:06 +02:00
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
let commitments = generate_commitments(&utxos);
|
|
|
|
|
|
|
|
|
|
let publication = ActionData::UTXOPublication(UTXOPublication {
|
|
|
|
|
utxos: utxos
|
|
|
|
|
.iter()
|
|
|
|
|
.enumerate()
|
|
|
|
|
.filter_map(|(id, item)| {
|
|
|
|
|
if visibility_list[id] {
|
|
|
|
|
Some(item.clone())
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect(),
|
|
|
|
|
});
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
Ok((
|
2025-01-03 10:44:06 +02:00
|
|
|
TransactionPayload {
|
|
|
|
|
tx_kind: TxKind::Shielded,
|
|
|
|
|
execution_input: vec![],
|
|
|
|
|
execution_output: serde_json::to_vec(&publication).unwrap(),
|
|
|
|
|
utxo_commitments_spent_hashes: vec![commitment_in],
|
|
|
|
|
utxo_commitments_created_hashes: commitments
|
|
|
|
|
.clone()
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|hash_data| hash_data.try_into().unwrap())
|
|
|
|
|
.collect(),
|
|
|
|
|
nullifier_created_hashes: vec![nullifier.try_into().unwrap()],
|
2025-03-16 11:05:55 -04:00
|
|
|
execution_proof_private: sc_core::transaction_payloads_tools::encode_receipt(
|
|
|
|
|
receipt,
|
|
|
|
|
)
|
|
|
|
|
.unwrap(),
|
2025-01-03 10:44:06 +02:00
|
|
|
encoded_data,
|
|
|
|
|
ephemeral_pub_key: eph_pub_key.to_vec(),
|
|
|
|
|
}
|
|
|
|
|
.into(),
|
|
|
|
|
utxo_hashes,
|
2025-01-10 03:00:32 +02:00
|
|
|
))
|
2025-01-03 10:44:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn send_split_tx(
|
|
|
|
|
&self,
|
|
|
|
|
utxo: UTXO,
|
|
|
|
|
comm_hash: [u8; 32],
|
|
|
|
|
receivers: Vec<(u128, AccountAddress)>,
|
|
|
|
|
visibility_list: [bool; 3],
|
2025-02-05 12:24:09 +02:00
|
|
|
) -> Result<(SendTxResponse, Vec<([u8; 32], [u8; 32])>, Vec<[u8; 32]>), ExecutionFailureKind>
|
|
|
|
|
{
|
2025-01-24 09:10:42 +02:00
|
|
|
//Considering proof time, needs to be done before proof
|
|
|
|
|
let tx_roots = self.get_roots().await;
|
|
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
let point_before_prove = std::time::Instant::now();
|
|
|
|
|
let (tx, utxo_hashes) = self
|
|
|
|
|
.split_utxo(utxo, comm_hash, receivers, visibility_list)
|
2025-01-10 03:00:32 +02:00
|
|
|
.await?;
|
2025-01-03 10:44:06 +02:00
|
|
|
tx.log();
|
|
|
|
|
let point_after_prove = std::time::Instant::now();
|
|
|
|
|
|
|
|
|
|
let timedelta = (point_after_prove - point_before_prove).as_millis();
|
|
|
|
|
info!("Send private utxo proof spent {timedelta:?} milliseconds");
|
|
|
|
|
|
|
|
|
|
let commitments = tx.utxo_commitments_created_hashes.clone();
|
|
|
|
|
|
|
|
|
|
Ok((
|
2025-01-24 09:10:42 +02:00
|
|
|
self.sequencer_client.send_tx(tx, tx_roots).await?,
|
2025-01-03 10:44:06 +02:00
|
|
|
utxo_hashes,
|
|
|
|
|
commitments,
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
pub async fn operate_account_send_split_utxo(
|
2025-01-03 10:44:06 +02:00
|
|
|
&mut self,
|
|
|
|
|
addrs_receivers: [AccountAddress; 3],
|
|
|
|
|
utxo: UTXO,
|
|
|
|
|
comm_gen_hash: [u8; 32],
|
|
|
|
|
visibility_list: [bool; 3],
|
2025-02-05 12:24:09 +02:00
|
|
|
) -> Result<(Vec<UTXO>, Vec<[u8; 32]>), ExecutionFailureKind> {
|
2025-01-03 10:44:06 +02:00
|
|
|
let (resp, new_utxo_hashes, commitments_hashes) = self
|
|
|
|
|
.send_split_tx(
|
|
|
|
|
utxo.clone(),
|
|
|
|
|
comm_gen_hash,
|
|
|
|
|
addrs_receivers
|
|
|
|
|
.clone()
|
|
|
|
|
.map(|addr| (utxo.amount / 3, addr))
|
|
|
|
|
.to_vec(),
|
|
|
|
|
visibility_list,
|
|
|
|
|
)
|
2025-01-10 03:00:32 +02:00
|
|
|
.await?;
|
2025-01-03 10:44:06 +02:00
|
|
|
info!("Response for send shielded is {resp:?}");
|
2024-12-25 11:21:03 +02:00
|
|
|
|
|
|
|
|
info!("Awaiting new blocks");
|
|
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(BLOCK_GEN_DELAY_SECS)).await;
|
|
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
let new_utxos: Vec<UTXO> = {
|
2024-12-25 11:21:03 +02:00
|
|
|
let mut write_guard = self.storage.write().await;
|
|
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
new_utxo_hashes
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|(acc_addr_rec, new_utxo_hash)| {
|
|
|
|
|
let acc = write_guard.acc_map.get_mut(&acc_addr_rec).unwrap();
|
|
|
|
|
|
|
|
|
|
let new_utxo = acc
|
|
|
|
|
.utxo_tree
|
|
|
|
|
.get_item(new_utxo_hash)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.clone();
|
|
|
|
|
new_utxo.log();
|
|
|
|
|
|
|
|
|
|
info!(
|
|
|
|
|
"Account address is {:?} ,new utxo owner address is {:?}",
|
|
|
|
|
hex::encode(acc_addr_rec),
|
|
|
|
|
hex::encode(new_utxo.owner)
|
|
|
|
|
);
|
|
|
|
|
info!(
|
|
|
|
|
"Account {:?} got new utxo with amount {:?}",
|
|
|
|
|
hex::encode(acc_addr_rec),
|
|
|
|
|
new_utxo.amount
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
new_utxo
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
2024-12-25 11:21:03 +02:00
|
|
|
};
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
Ok((new_utxos, commitments_hashes))
|
2024-12-25 11:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
///Mint utxo, make it public
|
2025-02-05 12:24:09 +02:00
|
|
|
pub async fn subscenario_1(&mut self) -> Result<(), ExecutionFailureKind> {
|
2024-12-25 11:21:03 +02:00
|
|
|
let acc_addr = self.create_new_account().await;
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
let (new_utxo, comm_gen_hash) = self.operate_account_mint_private(acc_addr, 100).await?;
|
2024-12-25 11:21:03 +02:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
self.operate_account_send_deshielded_one_receiver(
|
|
|
|
|
acc_addr,
|
|
|
|
|
acc_addr,
|
|
|
|
|
new_utxo,
|
|
|
|
|
comm_gen_hash,
|
|
|
|
|
)
|
2025-01-10 03:00:32 +02:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2025-01-03 10:44:06 +02:00
|
|
|
}
|
2024-12-25 11:21:03 +02:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
///Deposit balance, make it private
|
2025-02-05 12:24:09 +02:00
|
|
|
pub async fn subscenario_2(&mut self) -> Result<(), ExecutionFailureKind> {
|
2025-01-03 10:44:06 +02:00
|
|
|
let acc_addr = self.create_new_account().await;
|
2024-12-30 07:35:05 +01:00
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
self.operate_account_deposit_public(acc_addr, 100).await?;
|
2024-12-25 11:21:03 +02:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
self.operate_account_send_shielded_one_receiver(acc_addr, acc_addr, 100)
|
2025-01-10 03:00:32 +02:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2025-01-03 10:44:06 +02:00
|
|
|
}
|
2024-12-25 11:21:03 +02:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
///Mint utxo, privately send it to another user
|
2025-02-05 12:24:09 +02:00
|
|
|
pub async fn subscenario_3(&mut self) -> Result<(), ExecutionFailureKind> {
|
2025-01-03 10:44:06 +02:00
|
|
|
let acc_addr = self.create_new_account().await;
|
|
|
|
|
let acc_addr_rec = self.create_new_account().await;
|
2024-12-25 11:21:03 +02:00
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
let (new_utxo, comm_gen_hash) = self.operate_account_mint_private(acc_addr, 100).await?;
|
2024-12-25 11:21:03 +02:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
self.operate_account_send_private_one_receiver(acc_addr_rec, new_utxo, comm_gen_hash)
|
2025-01-10 03:00:32 +02:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2025-01-03 10:44:06 +02:00
|
|
|
}
|
2024-12-25 11:21:03 +02:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
///Deposit balance, shielded send it to another user
|
2025-02-05 12:24:09 +02:00
|
|
|
pub async fn subscenario_4(&mut self) -> Result<(), ExecutionFailureKind> {
|
2025-01-03 10:44:06 +02:00
|
|
|
let acc_addr = self.create_new_account().await;
|
|
|
|
|
let acc_addr_rec = self.create_new_account().await;
|
2024-12-25 11:21:03 +02:00
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
self.operate_account_deposit_public(acc_addr, 100).await?;
|
2024-12-25 11:21:03 +02:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
self.operate_account_send_shielded_one_receiver(acc_addr, acc_addr_rec, 100)
|
2025-01-10 03:00:32 +02:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2024-12-25 11:21:03 +02:00
|
|
|
}
|
|
|
|
|
|
2024-12-26 11:38:00 +02:00
|
|
|
///Mint utxo, deshielded send it to another user
|
2025-02-05 12:24:09 +02:00
|
|
|
pub async fn subscenario_5(&mut self) -> Result<(), ExecutionFailureKind> {
|
2024-12-26 11:38:00 +02:00
|
|
|
let acc_addr = self.create_new_account().await;
|
|
|
|
|
let acc_addr_rec = self.create_new_account().await;
|
|
|
|
|
|
2025-01-10 03:00:32 +02:00
|
|
|
let (new_utxo, comm_gen_hash) = self.operate_account_mint_private(acc_addr, 100).await?;
|
2024-12-26 11:38:00 +02:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
self.operate_account_send_deshielded_one_receiver(
|
|
|
|
|
acc_addr,
|
|
|
|
|
acc_addr_rec,
|
|
|
|
|
new_utxo,
|
|
|
|
|
comm_gen_hash,
|
|
|
|
|
)
|
2025-01-10 03:00:32 +02:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2025-01-03 10:44:06 +02:00
|
|
|
}
|
2024-12-26 11:38:00 +02:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
///First complex scenario.
|
|
|
|
|
/// Creating accounts A, B, C, D.
|
|
|
|
|
/// Minting UTXO for A, splitting it between B, C, D.
|
|
|
|
|
/// Variable `visibility_list` decides, which of actions will be visible on blockchain.
|
|
|
|
|
/// Variable `publication index` decides, who of B, C or D moves its UTXO into public state.
|
2025-01-10 03:00:32 +02:00
|
|
|
pub async fn scenario_1(
|
|
|
|
|
&mut self,
|
|
|
|
|
visibility_list: [bool; 3],
|
|
|
|
|
publication_index: usize,
|
2025-02-05 12:24:09 +02:00
|
|
|
) -> Result<(), ExecutionFailureKind> {
|
2025-01-03 10:44:06 +02:00
|
|
|
let acc_addr_sender = self.create_new_account().await;
|
2024-12-26 11:38:00 +02:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
let acc_addr_rec_1 = self.create_new_account().await;
|
|
|
|
|
let acc_addr_rec_2 = self.create_new_account().await;
|
|
|
|
|
let acc_addr_rec_3 = self.create_new_account().await;
|
2024-12-26 11:38:00 +02:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
let addrs_receivers = [acc_addr_rec_1, acc_addr_rec_2, acc_addr_rec_3];
|
2024-12-26 11:38:00 +02:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
let (new_utxo, comm_gen_hash) = self
|
2025-01-10 03:00:32 +02:00
|
|
|
.operate_account_mint_private(acc_addr_sender, 99)
|
|
|
|
|
.await?;
|
2024-12-26 11:38:00 +02:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
let (new_utxos, comm_gen_hashes) = self
|
|
|
|
|
.operate_account_send_split_utxo(
|
|
|
|
|
addrs_receivers,
|
|
|
|
|
new_utxo,
|
|
|
|
|
comm_gen_hash,
|
|
|
|
|
visibility_list,
|
|
|
|
|
)
|
2025-01-10 03:00:32 +02:00
|
|
|
.await?;
|
2024-12-30 07:35:05 +01:00
|
|
|
|
2025-01-03 10:44:06 +02:00
|
|
|
self.operate_account_send_deshielded_one_receiver(
|
|
|
|
|
addrs_receivers[publication_index],
|
|
|
|
|
addrs_receivers[publication_index],
|
|
|
|
|
new_utxos[publication_index].clone(),
|
|
|
|
|
comm_gen_hashes[publication_index],
|
|
|
|
|
)
|
2025-01-10 03:00:32 +02:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2024-12-26 11:38:00 +02:00
|
|
|
}
|
2025-01-03 12:43:05 +02:00
|
|
|
|
|
|
|
|
///Mint number of different assets with same amount for account
|
2025-01-10 03:00:32 +02:00
|
|
|
pub async fn scenario_2(
|
|
|
|
|
&mut self,
|
|
|
|
|
number_of_assets: usize,
|
|
|
|
|
number_to_send: usize,
|
2025-02-05 12:24:09 +02:00
|
|
|
) -> Result<(), ExecutionFailureKind> {
|
2025-01-03 12:43:05 +02:00
|
|
|
let acc_addr_sender = self.create_new_account().await;
|
|
|
|
|
let acc_addr_receiver = self.create_new_account().await;
|
|
|
|
|
|
|
|
|
|
let (utxos, comm_gen_hashes) = self
|
|
|
|
|
.operate_account_mint_multiple_assets_private(acc_addr_sender, 100, number_of_assets)
|
2025-01-10 03:00:32 +02:00
|
|
|
.await?;
|
2025-01-03 12:43:05 +02:00
|
|
|
|
|
|
|
|
self.operate_account_send_private_multiple_assets_one_receiver(
|
|
|
|
|
acc_addr_sender,
|
|
|
|
|
acc_addr_receiver,
|
|
|
|
|
utxos,
|
|
|
|
|
comm_gen_hashes,
|
|
|
|
|
number_to_send,
|
|
|
|
|
)
|
2025-01-10 03:00:32 +02:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2025-01-03 12:43:05 +02:00
|
|
|
}
|
2024-12-03 09:32:35 +02:00
|
|
|
}
|
2025-01-10 03:00:32 +02:00
|
|
|
|
|
|
|
|
pub fn generate_commitments_helper(input_utxos: &[UTXO]) -> Vec<[u8; 32]> {
|
|
|
|
|
generate_commitments(input_utxos)
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|comm_raw| comm_raw.try_into().unwrap())
|
|
|
|
|
.collect()
|
|
|
|
|
}
|