From 0b1df5b3d58a668cc2a5be8758e4b3665c8be263 Mon Sep 17 00:00:00 2001 From: Oleksandr Pravdyvyi Date: Fri, 4 Apr 2025 12:11:54 +0300 Subject: [PATCH] fix: public context structure added --- Cargo.lock | 14 ++++++++++++++ Cargo.toml | 2 +- accounts/src/account_core/mod.rs | 20 ++++++++++++++++++++ node_core/src/storage/mod.rs | 24 +++++++++++++++++++++++- node_core/src/storage/public_context.rs | 14 ++++++++++++++ 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 node_core/src/storage/public_context.rs diff --git a/Cargo.lock b/Cargo.lock index e5e44db..babf1b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1565,6 +1565,7 @@ dependencies = [ "digest 0.10.7", "elliptic-curve", "rfc6979", + "serdect", "signature", "spki", ] @@ -1608,6 +1609,7 @@ dependencies = [ "pkcs8", "rand_core 0.6.4", "sec1", + "serdect", "subtle 2.6.1", "zeroize", ] @@ -2636,6 +2638,7 @@ dependencies = [ "ecdsa", "elliptic-curve", "once_cell", + "serdect", "sha2 0.10.8", "signature", ] @@ -4590,6 +4593,7 @@ dependencies = [ "der", "generic-array 0.14.7", "pkcs8", + "serdect", "subtle 2.6.1", "zeroize", ] @@ -4784,6 +4788,16 @@ dependencies = [ "serde", ] +[[package]] +name = "serdect" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a84f14a19e9a014bb9f4512488d9829a68e04ecabffb0f9904cd1ace94598177" +dependencies = [ + "base16ct", + "serde", +] + [[package]] name = "sha1" version = "0.10.6" diff --git a/Cargo.toml b/Cargo.toml index 54e3059..f20532c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,7 +58,7 @@ features = ["std", "std_rng", "getrandom"] version = "0.8.5" [workspace.dependencies.k256] -features = ["ecdsa-core", "arithmetic", "expose-field"] +features = ["ecdsa-core", "arithmetic", "expose-field", "serde"] version = "0.13.4" [workspace.dependencies.elliptic-curve] diff --git a/accounts/src/account_core/mod.rs b/accounts/src/account_core/mod.rs index 3421a63..647ca24 100644 --- a/accounts/src/account_core/mod.rs +++ b/accounts/src/account_core/mod.rs @@ -26,6 +26,16 @@ pub struct Account { pub utxo_tree: UTXOSparseMerkleTree, } +///A strucure, which represents all the visible(public) information +/// +/// known to each node about account `address` +pub struct AccountPublicMask { + pub nullifier_public_key: AffinePoint, + pub viewing_public_key: AffinePoint, + pub address: AccountAddress, + pub balance: u64, +} + impl Account { pub fn new() -> Self { let key_holder = AddressKeyHolder::new_os_random(); @@ -126,6 +136,16 @@ impl Account { pub fn make_tag(&self) -> Tag { self.address[0] } + + ///Produce account public mask + pub fn make_account_public_mask(&self) -> AccountPublicMask { + AccountPublicMask { + nullifier_public_key: self.key_holder.nullifer_public_key, + viewing_public_key: self.key_holder.viewing_public_key, + address: self.address, + balance: self.balance, + } + } } impl Default for Account { diff --git a/node_core/src/storage/mod.rs b/node_core/src/storage/mod.rs index 4afcb62..ca2f8d8 100644 --- a/node_core/src/storage/mod.rs +++ b/node_core/src/storage/mod.rs @@ -1,10 +1,14 @@ -use std::{collections::HashMap, path::Path}; +use std::{ + collections::{BTreeMap, HashMap}, + path::Path, +}; use accounts::account_core::{Account, AccountAddress}; use anyhow::Result; use block_store::NodeBlockStore; use elliptic_curve::group::GroupEncoding; use k256::AffinePoint; +use public_context::PublicSCContext; use storage::{ block::Block, merkle_tree_public::merkle_tree::{PublicTransactionMerkleTree, UTXOCommitmentsMerkleTree}, @@ -18,6 +22,7 @@ use crate::ActionData; pub mod accounts_store; pub mod block_store; +pub mod public_context; pub struct NodeChainStore { pub acc_map: HashMap, @@ -149,4 +154,21 @@ impl NodeChainStore { Ok(()) } + + pub fn produce_context(&self, caller: AccountAddress) -> PublicSCContext { + let mut account_masks = BTreeMap::new(); + + for (acc_addr, acc) in &self.acc_map { + account_masks.insert(*acc_addr, acc.make_account_public_mask()); + } + + PublicSCContext { + caller_address: caller, + caller_balance: self.acc_map.get(&caller).unwrap().balance, + account_masks, + nullifier_store_root: self.nullifier_store.curr_root.unwrap_or([0; 32]), + comitment_store_root: self.utxo_commitments_store.get_root().unwrap_or([0; 32]), + pub_tx_store_root: self.pub_tx_store.get_root().unwrap_or([0; 32]), + } + } } diff --git a/node_core/src/storage/public_context.rs b/node_core/src/storage/public_context.rs new file mode 100644 index 0000000..68c2573 --- /dev/null +++ b/node_core/src/storage/public_context.rs @@ -0,0 +1,14 @@ +use std::collections::BTreeMap; + +use accounts::account_core::{AccountAddress, AccountPublicMask}; +use storage::{merkle_tree_public::TreeHashType, AccountId}; + +///Strucutre, representing context, given to a smart contract on a call +pub struct PublicSCContext { + pub caller_address: AccountAddress, + pub caller_balance: u64, + pub account_masks: BTreeMap, + pub nullifier_store_root: TreeHashType, + pub comitment_store_root: TreeHashType, + pub pub_tx_store_root: TreeHashType, +}