Skip to main content

moonproof_runtime/
zswap_local.rs

1//! Compact-runtime-side zswap local state — the per-circuit-execution
2//! view of "outputs the circuit just produced + inputs it spent + the
3//! caller's coin public key".
4//!
5//! Port of the data shape of
6//! `@midnight-ntwrk/midnight-js-protocol/compact-runtime`'s
7//! `ZswapLocalState`. This is **distinct from** the wallet-side
8//! `ZswapWalletState` (`moonproof_ledger::zswap::local::State`): the wallet
9//! state tracks *received* coins after on-chain confirmation; this
10//! state tracks *unsubmitted* coin moves emitted during circuit
11//! execution.
12//!
13//! Lives in the runtime crate because the IR executor consumes it; the
14//! lifecycle layer (`moonproof_contracts`) owns the translator that turns
15//! it into a ledger-typed `UnprovenOffer`.
16
17use moonproof_ledger::coin_structure::coin::{
18    Info as CoinInfo, QualifiedInfo as QualifiedCoinInfo,
19};
20use moonproof_ledger::coin_structure::transfer::Recipient;
21
22use crate::types::CoinPublicKey;
23
24/// One entry in [`CompactZswapLocalState::outputs`]. Pairs a
25/// shielded-coin info value with its recipient — mirrors the TS
26/// `{ coinInfo, recipient }` shape.
27#[derive(Debug, Clone, PartialEq)]
28pub struct CompactZswapOutput {
29    pub coin_info: CoinInfo,
30    pub recipient: Recipient,
31}
32
33/// Compact-runtime-side zswap local state.
34#[derive(Debug, Clone)]
35pub struct CompactZswapLocalState {
36    pub coin_public_key: CoinPublicKey,
37    /// Insertion index for the next pending output — TS
38    /// `currentIndex`. Not consulted by the offer translator for the
39    /// no-coin case but preserved verbatim for round-trip fidelity
40    /// with TS.
41    pub current_index: u64,
42    /// Inputs the circuit consumed — qualified shielded coins that
43    /// were already on-chain.
44    pub inputs: Vec<QualifiedCoinInfo>,
45    /// Outputs the circuit produced — coin info + recipient pairs.
46    pub outputs: Vec<CompactZswapOutput>,
47}
48
49impl CompactZswapLocalState {
50    /// Construct an empty state pinned to `coin_public_key`. Matches
51    /// TS `emptyZswapLocalState(coinPublicKey)`.
52    #[must_use]
53    pub fn empty(coin_public_key: CoinPublicKey) -> Self {
54        Self {
55            coin_public_key,
56            current_index: 0,
57            inputs: Vec::new(),
58            outputs: Vec::new(),
59        }
60    }
61}