From 5ccbb5ac21db586174ae0769221f0bd00343676d Mon Sep 17 00:00:00 2001 From: Rostyslav Tyshko Date: Fri, 6 Jun 2025 16:53:20 -0400 Subject: [PATCH] add `encode_utxo_for_owners` implementation --- sc_core/src/public_context.rs | 29 +++++++++++++++++++ .../test_methods/guest/src/bin/template_sc.rs | 5 ++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/sc_core/src/public_context.rs b/sc_core/src/public_context.rs index 438fa50..747a39b 100644 --- a/sc_core/src/public_context.rs +++ b/sc_core/src/public_context.rs @@ -3,6 +3,7 @@ use std::collections::BTreeMap; use accounts::account_core::{AccountAddress, AccountPublicMask}; use common::merkle_tree_public::TreeHashType; use serde::{ser::SerializeStruct, Serialize}; +use crate::traits::IPrivateOutput; pub const PUBLIC_SC_CONTEXT: &str = "PublicSCContext"; pub const CALLER_ADDRESS: &str = "caller_address"; @@ -87,6 +88,34 @@ impl PublicSCContext { Ok(u64_list) } + + pub fn encode_utxo_for_owners(&self, private_outputs: IPO) -> Vec<(Vec, Vec, u8)> { + let utxos = private_outputs.make_utxo_list(); + + // ToDo: when errorhandling is implemented this `unwrap()` call has to be removed + let caller_account_mask = self.account_masks.get(&self.caller_address).unwrap(); + + let ephm_key_holder = caller_account_mask.produce_ephemeral_key_holder(); + + let encoded_data = utxos + .iter() + .map(|utxo| { + // ToDo: when errorhandling is implemented this `unwrap()` call has to be removed + let account_mask = self.account_masks.get(&utxo.owner).unwrap(); + ( + AccountPublicMask::encrypt_data( + &ephm_key_holder, + account_mask.viewing_public_key, + &serde_json::to_vec(&utxo).unwrap(), + ), + account_mask.make_tag(), + ) + }) + .map(|((ciphertext, nonce), tag)| (ciphertext, nonce.to_vec(), tag)) + .collect(); + + encoded_data + } } #[cfg(test)] diff --git a/template/zk_test_template/test_methods/guest/src/bin/template_sc.rs b/template/zk_test_template/test_methods/guest/src/bin/template_sc.rs index 168277a..eb914eb 100644 --- a/template/zk_test_template/test_methods/guest/src/bin/template_sc.rs +++ b/template/zk_test_template/test_methods/guest/src/bin/template_sc.rs @@ -44,17 +44,16 @@ fn main() { env::commit(&state_changes); //Next, push one of possible variants depending on execution type - //ToDo: Make UTXO encoding for their owners available from PublicSCContext { public => { env::commit(&public_outputs); }, private => { - env::commit(&(public_context.encode_utxo_for_owners(private_outputs.make_utxo_list()))); + env::commit(&(public_context.encode_utxo_for_owners(private_outputs))); }, shielded | deshielded => { env::commit(&public_outputs); - env::commit(&(public_context.encode_utxo_for_owners(private_outputs.make_utxo_list()))); + env::commit(&(public_context.encode_utxo_for_owners(private_outputs))); }, } }