moonproof-contracts 0.2.2

Node-side host orchestration for the moonproof contract pipeline — providers, deploy/call/maintenance lifecycle, fee balancing, submission, indexer-driven sync
Documentation
//! Wallet provider trait — port of
//! `@midnight-ntwrk/midnight-js-types/src/wallet-provider.ts`.
//!
//! Responsible for (a) balancing a proven transaction (i.e. attaching
//! dust / zswap fee coins) and (b) exposing the caller's Zswap coin +
//! encryption public keys so the deploy / call builders can address
//! change outputs back to the caller.
//!
//! # Balance phase
//!
//! TS `balanceTx` takes an `UnboundTransaction` (proven, not yet
//! sealed) and returns a `FinalizedTransaction` (proven + sealed,
//! ready for submission). Moonproof's facade expresses the same step
//! via the `finalize_recipe` pipeline; concrete
//! [`WalletProvider`] impls call into that.
//!
//! The Rust trait uses byte-level transaction shapes in and out so
//! implementations can choose their own deserialisation strategy
//! (tagged, untagged, etc.) without the trait carrying heavy type
//! parameters. Concrete impls live in a sibling module.

use moonproof_macros::async_trait;

use crate::error::Result;
use crate::tx_phase::{FinalizedTx, ProvenTx};
use crate::types::{CoinPublicKey, EncPublicKey};

/// Rust port of TS `WalletProvider`.
#[async_trait]
pub trait WalletProvider: Send + Sync + std::fmt::Debug {
    /// Balance a proven, not-yet-sealed transaction.
    ///
    /// Input: a [`ProvenTx`] — the "pre-binding / post-prove" shape.
    /// Output: a [`FinalizedTx`] — the sealed wire shape the node
    /// accepts.
    ///
    /// The `ttl_ms` parameter mirrors the TS `ttl?: Date` parameter —
    /// absent defaults to the implementation's chosen default.
    async fn balance_tx(&self, tx: ProvenTx, ttl_ms: Option<i64>) -> Result<FinalizedTx>;

    /// The caller's Zswap coin public key. Used as the owner of
    /// change coins produced during contract construction / circuit
    /// execution.
    fn coin_public_key(&self) -> CoinPublicKey;

    /// The caller's Zswap encryption public key. Used to seal
    /// ciphertexts on outputs addressed back to the caller.
    fn encryption_public_key(&self) -> EncPublicKey;
}

#[async_trait]
impl<T: WalletProvider + ?Sized> WalletProvider for std::sync::Arc<T> {
    async fn balance_tx(&self, tx: ProvenTx, ttl_ms: Option<i64>) -> Result<FinalizedTx> {
        (**self).balance_tx(tx, ttl_ms).await
    }
    fn coin_public_key(&self) -> CoinPublicKey {
        (**self).coin_public_key()
    }
    fn encryption_public_key(&self) -> EncPublicKey {
        (**self).encryption_public_key()
    }
}

#[async_trait]
impl<T: WalletProvider + ?Sized> WalletProvider for Box<T> {
    async fn balance_tx(&self, tx: ProvenTx, ttl_ms: Option<i64>) -> Result<FinalizedTx> {
        (**self).balance_tx(tx, ttl_ms).await
    }
    fn coin_public_key(&self) -> CoinPublicKey {
        (**self).coin_public_key()
    }
    fn encryption_public_key(&self) -> EncPublicKey {
        (**self).encryption_public_key()
    }
}