moonproof-runtime 0.2.2

Wasm-friendly Compact contract runtime core — IR walker, IR-driven circuit executor, contract state, runtime values
Documentation
//! Pure data types — wasm-friendly subset.
//!
//! Lifecycle types (`TxStatus`, `SegmentStatus`, `Fees`, `UnshieldedUtxo*`,
//! `FinalizedTxData`, `ContractStateStreamStart`, `ContractStateQueryAt`,
//! `CallResult*`, `PartitionedTranscript*`) live in [`moonproof_contracts::types`]
//! to keep the host-only `moonproof_wallet::ProtocolVersion` dep out of
//! the wasm-friendly runtime crate.

use serde::{Deserialize, Serialize};

// Re-exported so generated codegen output can `use ::moonproof_runtime::types::ContractAddress`
// without pulling in `moonproof_ledger::coin_structure` as a direct dep.
pub use moonproof_ledger::coin_structure::contract::ContractAddress;

// =====================================================================
// Primitive / newtype wrappers
// =====================================================================

/// Opaque identifier for a stored private state blob. Port of TS
/// `PrivateStateId = string`.
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct PrivateStateId(pub String);

impl PrivateStateId {
    #[must_use]
    pub fn new(s: impl Into<String>) -> Self {
        Self(s.into())
    }

    #[inline]
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Display for PrivateStateId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl From<&str> for PrivateStateId {
    fn from(s: &str) -> Self {
        PrivateStateId(s.to_owned())
    }
}

impl From<String> for PrivateStateId {
    fn from(s: String) -> Self {
        PrivateStateId(s)
    }
}

/// A block hash — indexer-hex convention. Port of TS `BlockHash = string`.
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct BlockHash(pub String);

impl BlockHash {
    #[must_use]
    pub fn new(s: impl Into<String>) -> Self {
        Self(s.into())
    }
    #[inline]
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

/// Transaction identifier as the indexer reports it — hex string.
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct TransactionId(pub String);

impl TransactionId {
    #[must_use]
    pub fn new(s: impl Into<String>) -> Self {
        Self(s.into())
    }
    #[inline]
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Display for TransactionId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

/// Transaction hash as the indexer reports it — hex string.
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct TransactionHash(pub String);

impl TransactionHash {
    #[must_use]
    pub fn new(s: impl Into<String>) -> Self {
        Self(s.into())
    }
    #[inline]
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

/// A provable circuit identifier — the exported name from the Compact
/// source. Matches TS `AnyProvableCircuitId = string`.
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct CircuitId(pub String);

impl CircuitId {
    #[must_use]
    pub fn new(s: impl Into<String>) -> Self {
        Self(s.into())
    }
    #[inline]
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Display for CircuitId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl From<&str> for CircuitId {
    fn from(s: &str) -> Self {
        Self(s.to_owned())
    }
}

impl From<&CircuitId> for CircuitId {
    fn from(c: &CircuitId) -> Self {
        c.clone()
    }
}

impl From<String> for CircuitId {
    fn from(s: String) -> Self {
        Self(s)
    }
}

/// The Zswap coin public key used as the sender/owner identity. 32 bytes.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct CoinPublicKey(pub [u8; 32]);

impl CoinPublicKey {
    #[must_use]
    pub fn to_hex(&self) -> String {
        hex::encode(self.0)
    }
}

/// The Zswap encryption public key used to seal coins addressed to the
/// current user. 32 bytes.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct EncPublicKey(pub [u8; 32]);

impl EncPublicKey {
    #[must_use]
    pub fn to_hex(&self) -> String {
        hex::encode(self.0)
    }
}

// =====================================================================
// ZK artifacts — ZKIR / ProverKey / VerifierKey as branded byte types
// =====================================================================

/// Raw ZKIR bytes produced by `compactc`'s `zkir/<circuit>.bzkir` output.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Zkir(pub Vec<u8>);

impl Zkir {
    #[must_use]
    pub fn new(bytes: impl Into<Vec<u8>>) -> Self {
        Self(bytes.into())
    }
    #[inline]
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }
}

/// Raw prover-key bytes produced by `compactc`'s `keys/<circuit>.prover` output.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProverKey(pub Vec<u8>);

impl ProverKey {
    #[must_use]
    pub fn new(bytes: impl Into<Vec<u8>>) -> Self {
        Self(bytes.into())
    }
    #[inline]
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }
}

/// Raw verifier-key bytes produced by `compactc`'s `keys/<circuit>.verifier` output.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VerifierKey(pub Vec<u8>);

impl VerifierKey {
    #[must_use]
    pub fn new(bytes: impl Into<Vec<u8>>) -> Self {
        Self(bytes.into())
    }
    #[inline]
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }
}

/// Bundle of all ZK artifacts for a single circuit. Port of TS `ZKConfig<K>`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ZkConfig {
    pub circuit_id: CircuitId,
    pub prover_key: ProverKey,
    pub verifier_key: VerifierKey,
    pub zkir: Zkir,
}

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

    #[test]
    fn private_state_id_as_str_roundtrips() {
        let id = PrivateStateId::new("hello");
        assert_eq!(id.as_str(), "hello");
        assert_eq!(format!("{id}"), "hello");
    }

    #[test]
    fn circuit_id_from_str_and_display() {
        let id: CircuitId = "increment".into();
        assert_eq!(id.as_str(), "increment");
        assert_eq!(format!("{id}"), "increment");
    }

    #[test]
    fn zk_artifact_byte_accessors() {
        let z = Zkir::new(vec![1u8, 2, 3]);
        assert_eq!(z.as_bytes(), &[1, 2, 3]);
        let pk = ProverKey::new(vec![9u8, 9, 9]);
        assert_eq!(pk.as_bytes(), &[9, 9, 9]);
        let vk = VerifierKey::new(vec![0xff, 0xee]);
        assert_eq!(vk.as_bytes(), &[0xff, 0xee]);
    }

    #[test]
    fn coin_pubkey_hex_is_lowercase_64_chars() {
        let pk = CoinPublicKey([0xabu8; 32]);
        let hex = pk.to_hex();
        assert_eq!(hex.len(), 64);
        assert_eq!(hex, "ab".repeat(32));
    }
}