2025-10-16 15:58:35 +03:00
|
|
|
use std::{path::PathBuf, sync::Arc};
|
2024-12-05 13:05:58 +02:00
|
|
|
|
2025-11-26 00:27:20 +03:00
|
|
|
use anyhow::Result;
|
2025-09-05 22:50:10 -03:00
|
|
|
use base64::{Engine, engine::general_purpose::STANDARD as BASE64};
|
2025-11-26 00:27:20 +03:00
|
|
|
use chain_storage::WalletChainStore;
|
2025-07-22 15:22:20 +03:00
|
|
|
use common::{
|
2025-10-13 13:29:32 +03:00
|
|
|
sequencer_client::SequencerClient,
|
2025-09-08 10:11:04 +03:00
|
|
|
transaction::{EncodedTransaction, NSSATransaction},
|
2025-07-22 15:22:20 +03:00
|
|
|
};
|
2025-08-11 08:55:08 +03:00
|
|
|
use config::WalletConfig;
|
2025-08-06 14:56:58 +03:00
|
|
|
use log::info;
|
2025-11-27 22:07:53 +03:00
|
|
|
use nssa::{Account, AccountId};
|
2025-10-15 15:25:36 +03:00
|
|
|
use nssa_core::{Commitment, MembershipProof};
|
2025-10-16 15:58:35 +03:00
|
|
|
use tokio::io::AsyncWriteExt;
|
2025-08-06 14:56:58 +03:00
|
|
|
|
2025-08-21 15:58:31 +03:00
|
|
|
use crate::{
|
2025-10-28 16:02:30 +02:00
|
|
|
config::PersistentStorage,
|
2025-11-27 22:07:53 +03:00
|
|
|
helperfunctions::{fetch_persistent_storage, get_home, produce_data_for_storage},
|
2025-08-21 15:58:31 +03:00
|
|
|
poller::TxPoller,
|
2025-08-08 15:22:04 +03:00
|
|
|
};
|
2025-08-07 14:07:34 +03:00
|
|
|
|
|
|
|
|
pub const HOME_DIR_ENV_VAR: &str = "NSSA_WALLET_HOME_DIR";
|
2024-12-25 09:50:54 +02:00
|
|
|
|
2025-04-24 15:51:34 +03:00
|
|
|
pub mod chain_storage;
|
2025-10-13 17:25:36 +03:00
|
|
|
pub mod cli;
|
2024-12-03 09:32:35 +02:00
|
|
|
pub mod config;
|
2025-08-07 14:07:34 +03:00
|
|
|
pub mod helperfunctions;
|
2025-08-21 15:58:31 +03:00
|
|
|
pub mod poller;
|
2025-11-27 22:07:53 +03:00
|
|
|
pub mod program_interactions;
|
2025-09-16 14:53:00 +03:00
|
|
|
pub mod token_transfers;
|
2025-10-21 10:32:42 +03:00
|
|
|
pub mod transaction_utils;
|
2024-12-22 16:14:52 +02:00
|
|
|
|
2025-08-11 08:55:08 +03:00
|
|
|
pub struct WalletCore {
|
2025-08-11 09:05:18 +03:00
|
|
|
pub storage: WalletChainStore,
|
2025-08-21 15:58:31 +03:00
|
|
|
pub poller: TxPoller,
|
2024-12-20 11:02:12 +02:00
|
|
|
pub sequencer_client: Arc<SequencerClient>,
|
2025-10-28 16:02:30 +02:00
|
|
|
pub last_synced_block: u64,
|
2024-12-05 13:05:58 +02:00
|
|
|
}
|
|
|
|
|
|
2025-08-11 08:55:08 +03:00
|
|
|
impl WalletCore {
|
2025-10-16 15:58:35 +03:00
|
|
|
pub async fn start_from_config_update_chain(config: WalletConfig) -> Result<Self> {
|
2025-08-07 14:07:34 +03:00
|
|
|
let client = Arc::new(SequencerClient::new(config.sequencer_addr.clone())?);
|
2025-09-04 13:33:17 +03:00
|
|
|
let tx_poller = TxPoller::new(config.clone(), client.clone());
|
2024-12-05 13:05:58 +02:00
|
|
|
|
2025-09-02 09:01:33 +03:00
|
|
|
let mut storage = WalletChainStore::new(config)?;
|
|
|
|
|
|
2025-10-28 16:02:30 +02:00
|
|
|
let PersistentStorage {
|
|
|
|
|
accounts: persistent_accounts,
|
|
|
|
|
last_synced_block,
|
|
|
|
|
} = fetch_persistent_storage().await?;
|
2025-09-02 09:01:33 +03:00
|
|
|
for pers_acc_data in persistent_accounts {
|
|
|
|
|
storage.insert_account_data(pers_acc_data);
|
|
|
|
|
}
|
2024-12-05 13:05:58 +02:00
|
|
|
|
|
|
|
|
Ok(Self {
|
2025-08-11 09:05:18 +03:00
|
|
|
storage,
|
2025-08-21 15:58:31 +03:00
|
|
|
poller: tx_poller,
|
2024-12-20 11:02:12 +02:00
|
|
|
sequencer_client: client.clone(),
|
2025-10-28 16:02:30 +02:00
|
|
|
last_synced_block,
|
2024-12-05 13:05:58 +02:00
|
|
|
})
|
|
|
|
|
}
|
2024-12-22 16:14:52 +02:00
|
|
|
|
2025-11-26 00:27:20 +03:00
|
|
|
/// Store persistent data at home
|
2025-10-28 16:02:30 +02:00
|
|
|
pub async fn store_persistent_data(&self) -> Result<PathBuf> {
|
2025-08-20 17:16:51 +03:00
|
|
|
let home = get_home()?;
|
2025-10-28 16:02:30 +02:00
|
|
|
let storage_path = home.join("storage.json");
|
2025-08-22 15:58:43 +03:00
|
|
|
|
2025-10-28 16:02:30 +02:00
|
|
|
let data = produce_data_for_storage(&self.storage.user_data, self.last_synced_block);
|
|
|
|
|
let storage = serde_json::to_vec_pretty(&data)?;
|
2025-08-20 17:16:51 +03:00
|
|
|
|
2025-10-28 16:02:30 +02:00
|
|
|
let mut storage_file = tokio::fs::File::create(storage_path.as_path()).await?;
|
|
|
|
|
storage_file.write_all(&storage).await?;
|
2025-08-20 17:16:51 +03:00
|
|
|
|
2025-10-28 16:02:30 +02:00
|
|
|
info!("Stored data at {storage_path:#?}");
|
2025-08-20 17:16:51 +03:00
|
|
|
|
2025-10-28 16:02:30 +02:00
|
|
|
Ok(storage_path)
|
2025-08-20 17:16:51 +03:00
|
|
|
}
|
|
|
|
|
|
2025-11-26 00:27:20 +03:00
|
|
|
/// Store persistent data at home
|
2025-11-03 15:45:50 +02:00
|
|
|
pub async fn store_config_changes(&self) -> Result<PathBuf> {
|
|
|
|
|
let home = get_home()?;
|
|
|
|
|
let config_path = home.join("wallet_config.json");
|
|
|
|
|
let config = serde_json::to_vec_pretty(&self.storage.wallet_config)?;
|
|
|
|
|
|
|
|
|
|
let mut config_file = tokio::fs::File::create(config_path.as_path()).await?;
|
|
|
|
|
config_file.write_all(&config).await?;
|
|
|
|
|
|
|
|
|
|
info!("Stored data at {config_path:#?}");
|
|
|
|
|
|
|
|
|
|
Ok(config_path)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-24 17:09:30 +03:00
|
|
|
pub fn create_new_account_public(&mut self) -> AccountId {
|
2025-09-08 14:48:58 +03:00
|
|
|
self.storage
|
|
|
|
|
.user_data
|
|
|
|
|
.generate_new_public_transaction_private_key()
|
2024-12-25 09:50:54 +02:00
|
|
|
}
|
|
|
|
|
|
2025-11-24 17:09:30 +03:00
|
|
|
pub fn create_new_account_private(&mut self) -> AccountId {
|
2025-09-11 18:32:46 +03:00
|
|
|
self.storage
|
|
|
|
|
.user_data
|
|
|
|
|
.generate_new_privacy_preserving_transaction_key_chain()
|
2025-09-02 07:32:39 +03:00
|
|
|
}
|
|
|
|
|
|
2025-11-26 00:27:20 +03:00
|
|
|
/// Get account balance
|
2025-11-24 17:09:30 +03:00
|
|
|
pub async fn get_account_balance(&self, acc: AccountId) -> Result<u128> {
|
2025-09-02 09:01:33 +03:00
|
|
|
Ok(self
|
|
|
|
|
.sequencer_client
|
|
|
|
|
.get_account_balance(acc.to_string())
|
|
|
|
|
.await?
|
|
|
|
|
.balance)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-26 00:27:20 +03:00
|
|
|
/// Get accounts nonces
|
2025-11-24 17:09:30 +03:00
|
|
|
pub async fn get_accounts_nonces(&self, accs: Vec<AccountId>) -> Result<Vec<u128>> {
|
2025-09-02 09:01:33 +03:00
|
|
|
Ok(self
|
|
|
|
|
.sequencer_client
|
|
|
|
|
.get_accounts_nonces(accs.into_iter().map(|acc| acc.to_string()).collect())
|
|
|
|
|
.await?
|
|
|
|
|
.nonces)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-26 00:27:20 +03:00
|
|
|
/// Get account
|
2025-11-24 17:09:30 +03:00
|
|
|
pub async fn get_account_public(&self, account_id: AccountId) -> Result<Account> {
|
|
|
|
|
let response = self
|
|
|
|
|
.sequencer_client
|
|
|
|
|
.get_account(account_id.to_string())
|
|
|
|
|
.await?;
|
2025-09-05 22:50:10 -03:00
|
|
|
Ok(response.account)
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-24 17:09:30 +03:00
|
|
|
pub fn get_account_private(&self, account_id: &AccountId) -> Option<Account> {
|
2025-10-03 15:59:27 -03:00
|
|
|
self.storage
|
|
|
|
|
.user_data
|
|
|
|
|
.user_private_accounts
|
2025-11-24 17:09:30 +03:00
|
|
|
.get(account_id)
|
2025-10-03 15:59:27 -03:00
|
|
|
.map(|value| value.1.clone())
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-24 17:09:30 +03:00
|
|
|
pub fn get_private_account_commitment(&self, account_id: &AccountId) -> Option<Commitment> {
|
|
|
|
|
let (keys, account) = self
|
|
|
|
|
.storage
|
|
|
|
|
.user_data
|
|
|
|
|
.user_private_accounts
|
|
|
|
|
.get(account_id)?;
|
2025-10-03 15:59:27 -03:00
|
|
|
Some(Commitment::new(&keys.nullifer_public_key, account))
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-26 00:27:20 +03:00
|
|
|
/// Poll transactions
|
2025-09-12 16:00:57 +03:00
|
|
|
pub async fn poll_native_token_transfer(&self, hash: String) -> Result<NSSATransaction> {
|
2025-08-21 15:58:31 +03:00
|
|
|
let transaction_encoded = self.poller.poll_tx(hash).await?;
|
2025-09-05 22:50:10 -03:00
|
|
|
let tx_base64_decode = BASE64.decode(transaction_encoded)?;
|
2025-09-25 11:53:42 +03:00
|
|
|
let pub_tx = borsh::from_slice::<EncodedTransaction>(&tx_base64_decode).unwrap();
|
2025-08-21 15:58:31 +03:00
|
|
|
|
2025-09-08 10:11:04 +03:00
|
|
|
Ok(NSSATransaction::try_from(&pub_tx)?)
|
2025-08-21 15:58:31 +03:00
|
|
|
}
|
2025-10-14 08:33:20 +03:00
|
|
|
|
2025-10-15 15:25:36 +03:00
|
|
|
pub async fn check_private_account_initialized(
|
|
|
|
|
&self,
|
2025-11-24 17:09:30 +03:00
|
|
|
account_id: &AccountId,
|
2025-10-15 15:25:36 +03:00
|
|
|
) -> Result<Option<MembershipProof>> {
|
2025-11-24 17:09:30 +03:00
|
|
|
if let Some(acc_comm) = self.get_private_account_commitment(account_id) {
|
2025-10-15 15:25:36 +03:00
|
|
|
self.sequencer_client
|
|
|
|
|
.get_proof_for_commitment(acc_comm)
|
|
|
|
|
.await
|
|
|
|
|
.map_err(anyhow::Error::from)
|
2025-10-14 08:33:20 +03:00
|
|
|
} else {
|
2025-10-15 15:25:36 +03:00
|
|
|
Ok(None)
|
2025-10-14 08:33:20 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn decode_insert_privacy_preserving_transaction_results(
|
|
|
|
|
&mut self,
|
|
|
|
|
tx: nssa::privacy_preserving_transaction::PrivacyPreservingTransaction,
|
2025-11-24 17:09:30 +03:00
|
|
|
acc_decode_data: &[(nssa_core::SharedSecretKey, AccountId)],
|
2025-10-14 08:33:20 +03:00
|
|
|
) -> Result<()> {
|
2025-11-24 17:09:30 +03:00
|
|
|
for (output_index, (secret, acc_account_id)) in acc_decode_data.iter().enumerate() {
|
2025-10-14 08:33:20 +03:00
|
|
|
let acc_ead = tx.message.encrypted_private_post_states[output_index].clone();
|
|
|
|
|
let acc_comm = tx.message.new_commitments[output_index].clone();
|
|
|
|
|
|
|
|
|
|
let res_acc = nssa_core::EncryptionScheme::decrypt(
|
|
|
|
|
&acc_ead.ciphertext,
|
2025-10-14 09:36:21 +03:00
|
|
|
secret,
|
2025-10-14 08:33:20 +03:00
|
|
|
&acc_comm,
|
|
|
|
|
output_index as u32,
|
|
|
|
|
)
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
println!("Received new acc {res_acc:#?}");
|
|
|
|
|
|
|
|
|
|
self.storage
|
2025-11-24 17:09:30 +03:00
|
|
|
.insert_private_account_data(*acc_account_id, res_acc);
|
2025-10-14 08:33:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
println!("Transaction data is {:?}", tx.message);
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
2024-12-03 09:32:35 +02:00
|
|
|
}
|