moonproof-contracts 0.2.1

Node-side host orchestration for the moonproof contract pipeline — providers, deploy/call/maintenance lifecycle, fee balancing, submission, indexer-driven sync
Documentation
//! Concrete [`WalletProvider`] bridging the runtime's submit pipeline
//! to [`moonproof_wallet`]'s facade layer.
//!
//! # Scope note — fee balancing
//!
//! The TS reference's `WalletProvider.balanceTx` adds dust-fee coverage
//! to the proven transaction before sealing. The moonproof pipeline
//! splits the two steps apart: [`ProofProvider::prove_tx`] proves, and
//! [`WalletProvider::balance_tx`] seals. At the moment this seal is
//! **pure** — no dust-fee intent is injected. A faithful end-to-end
//! port needs fee balancing, which touches both
//! [`crate::unproven_deploy_tx`] / [`crate::unproven_call_tx`] (to
//! include the balancing intent at the preimage stage) and this
//! provider (to expose the caller's coin / encryption public keys).
//!
//! # Transaction stages
//!
//! - **Input**: tagged-serialized
//!   `Transaction<Signature, ProofMarker, PedersenRandomness,
//!   InMemoryDB>` — the output of [`ProofProvider::prove_tx`].
//! - **Output**: tagged-serialized
//!   `Transaction<Signature, ProofMarker, PureGeneratorPedersen,
//!   InMemoryDB>` — the wire form the node's
//!   `pallet_midnight.send_mn_transaction` extrinsic accepts.

use moonproof_macros::async_trait;
use rand::rngs::OsRng;

use crate::error::Result;
use crate::providers::wallet::WalletProvider;
use crate::types::{CoinPublicKey, EncPublicKey};

/// Concrete [`WalletProvider`] that seals a proven contract
/// transaction and exposes the caller's Zswap keys.
#[derive(Debug, Clone)]
pub struct FacadeWalletProvider {
    coin_public_key: CoinPublicKey,
    encryption_public_key: EncPublicKey,
}

impl FacadeWalletProvider {
    /// Construct from the caller's Zswap coin + encryption public
    /// keys. 32-byte arrays in each newtype — see [`CoinPublicKey`]
    /// and [`EncPublicKey`].
    #[must_use]
    pub fn new(coin_public_key: CoinPublicKey, encryption_public_key: EncPublicKey) -> Self {
        Self {
            coin_public_key,
            encryption_public_key,
        }
    }

    /// Construct a `FacadeWalletProvider` with all-zero keys — used by
    /// tests that drive a stateless contract (no coin outputs, no
    /// encryption addressing).
    #[must_use]
    pub fn zero_keys() -> Self {
        Self {
            coin_public_key: CoinPublicKey([0u8; 32]),
            encryption_public_key: EncPublicKey([0u8; 32]),
        }
    }
}

#[async_trait]
impl WalletProvider for FacadeWalletProvider {
    async fn balance_tx(
        &self,
        tx: crate::tx_phase::ProvenTx,
        _ttl_ms: Option<i64>,
    ) -> Result<crate::tx_phase::FinalizedTx> {
        let sealed = tx.into_inner().seal(OsRng);
        Ok(crate::tx_phase::FinalizedTx::new(sealed))
    }

    fn coin_public_key(&self) -> CoinPublicKey {
        self.coin_public_key
    }

    fn encryption_public_key(&self) -> EncPublicKey {
        self.encryption_public_key
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn zero_keys_constructor_returns_zeros() {
        let p = FacadeWalletProvider::zero_keys();
        assert_eq!(p.coin_public_key().0, [0u8; 32]);
        assert_eq!(p.encryption_public_key().0, [0u8; 32]);
    }

    #[test]
    fn with_supplied_keys_constructor_propagates_values() {
        let cpk = CoinPublicKey([0xabu8; 32]);
        let epk = EncPublicKey([0xcdu8; 32]);
        let p = FacadeWalletProvider::new(cpk, epk);
        assert_eq!(p.coin_public_key().0, [0xabu8; 32]);
        assert_eq!(p.encryption_public_key().0, [0xcdu8; 32]);
    }

    #[test]
    fn provider_is_send_sync() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<FacadeWalletProvider>();
    }

    // The pre-Phase-3 test that asserted balance_tx rejects garbage
    // bytes is structurally obsolete — `balance_tx` now takes a typed
    // [`ProvenTx`], so malformed-input rejection is enforced at the
    // type system level, not at runtime.
}