Skip to main content

moonproof_runtime/
types.rs

1//! Pure data types — wasm-friendly subset.
2//!
3//! Lifecycle types (`TxStatus`, `SegmentStatus`, `Fees`, `UnshieldedUtxo*`,
4//! `FinalizedTxData`, `ContractStateStreamStart`, `ContractStateQueryAt`,
5//! `CallResult*`, `PartitionedTranscript*`) live in [`moonproof_contracts::types`]
6//! to keep the host-only `moonproof_wallet::ProtocolVersion` dep out of
7//! the wasm-friendly runtime crate.
8
9use serde::{Deserialize, Serialize};
10
11// Re-exported so generated codegen output can `use ::moonproof_runtime::types::ContractAddress`
12// without pulling in `moonproof_ledger::coin_structure` as a direct dep.
13pub use moonproof_ledger::coin_structure::contract::ContractAddress;
14
15// =====================================================================
16// Primitive / newtype wrappers
17// =====================================================================
18
19/// Opaque identifier for a stored private state blob. Port of TS
20/// `PrivateStateId = string`.
21#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
22pub struct PrivateStateId(pub String);
23
24impl PrivateStateId {
25    #[must_use]
26    pub fn new(s: impl Into<String>) -> Self {
27        Self(s.into())
28    }
29
30    #[inline]
31    #[must_use]
32    pub fn as_str(&self) -> &str {
33        &self.0
34    }
35}
36
37impl std::fmt::Display for PrivateStateId {
38    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39        self.0.fmt(f)
40    }
41}
42
43impl From<&str> for PrivateStateId {
44    fn from(s: &str) -> Self {
45        PrivateStateId(s.to_owned())
46    }
47}
48
49impl From<String> for PrivateStateId {
50    fn from(s: String) -> Self {
51        PrivateStateId(s)
52    }
53}
54
55/// A block hash — indexer-hex convention. Port of TS `BlockHash = string`.
56#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
57pub struct BlockHash(pub String);
58
59impl BlockHash {
60    #[must_use]
61    pub fn new(s: impl Into<String>) -> Self {
62        Self(s.into())
63    }
64    #[inline]
65    #[must_use]
66    pub fn as_str(&self) -> &str {
67        &self.0
68    }
69}
70
71/// Transaction identifier as the indexer reports it — hex string.
72#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
73pub struct TransactionId(pub String);
74
75impl TransactionId {
76    #[must_use]
77    pub fn new(s: impl Into<String>) -> Self {
78        Self(s.into())
79    }
80    #[inline]
81    #[must_use]
82    pub fn as_str(&self) -> &str {
83        &self.0
84    }
85}
86
87impl std::fmt::Display for TransactionId {
88    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89        self.0.fmt(f)
90    }
91}
92
93/// Transaction hash as the indexer reports it — hex string.
94#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
95pub struct TransactionHash(pub String);
96
97impl TransactionHash {
98    #[must_use]
99    pub fn new(s: impl Into<String>) -> Self {
100        Self(s.into())
101    }
102    #[inline]
103    #[must_use]
104    pub fn as_str(&self) -> &str {
105        &self.0
106    }
107}
108
109/// A provable circuit identifier — the exported name from the Compact
110/// source. Matches TS `AnyProvableCircuitId = string`.
111#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
112pub struct CircuitId(pub String);
113
114impl CircuitId {
115    #[must_use]
116    pub fn new(s: impl Into<String>) -> Self {
117        Self(s.into())
118    }
119    #[inline]
120    #[must_use]
121    pub fn as_str(&self) -> &str {
122        &self.0
123    }
124}
125
126impl std::fmt::Display for CircuitId {
127    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
128        self.0.fmt(f)
129    }
130}
131
132impl From<&str> for CircuitId {
133    fn from(s: &str) -> Self {
134        Self(s.to_owned())
135    }
136}
137
138impl From<&CircuitId> for CircuitId {
139    fn from(c: &CircuitId) -> Self {
140        c.clone()
141    }
142}
143
144impl From<String> for CircuitId {
145    fn from(s: String) -> Self {
146        Self(s)
147    }
148}
149
150/// The Zswap coin public key used as the sender/owner identity. 32 bytes.
151#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
152pub struct CoinPublicKey(pub [u8; 32]);
153
154impl CoinPublicKey {
155    #[must_use]
156    pub fn to_hex(&self) -> String {
157        hex::encode(self.0)
158    }
159}
160
161/// The Zswap encryption public key used to seal coins addressed to the
162/// current user. 32 bytes.
163#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
164pub struct EncPublicKey(pub [u8; 32]);
165
166impl EncPublicKey {
167    #[must_use]
168    pub fn to_hex(&self) -> String {
169        hex::encode(self.0)
170    }
171}
172
173// =====================================================================
174// ZK artifacts — ZKIR / ProverKey / VerifierKey as branded byte types
175// =====================================================================
176
177/// Raw ZKIR bytes produced by `compactc`'s `zkir/<circuit>.bzkir` output.
178#[derive(Debug, Clone, PartialEq, Eq)]
179pub struct Zkir(pub Vec<u8>);
180
181impl Zkir {
182    #[must_use]
183    pub fn new(bytes: impl Into<Vec<u8>>) -> Self {
184        Self(bytes.into())
185    }
186    #[inline]
187    #[must_use]
188    pub fn as_bytes(&self) -> &[u8] {
189        &self.0
190    }
191}
192
193/// Raw prover-key bytes produced by `compactc`'s `keys/<circuit>.prover` output.
194#[derive(Debug, Clone, PartialEq, Eq)]
195pub struct ProverKey(pub Vec<u8>);
196
197impl ProverKey {
198    #[must_use]
199    pub fn new(bytes: impl Into<Vec<u8>>) -> Self {
200        Self(bytes.into())
201    }
202    #[inline]
203    #[must_use]
204    pub fn as_bytes(&self) -> &[u8] {
205        &self.0
206    }
207}
208
209/// Raw verifier-key bytes produced by `compactc`'s `keys/<circuit>.verifier` output.
210#[derive(Debug, Clone, PartialEq, Eq)]
211pub struct VerifierKey(pub Vec<u8>);
212
213impl VerifierKey {
214    #[must_use]
215    pub fn new(bytes: impl Into<Vec<u8>>) -> Self {
216        Self(bytes.into())
217    }
218    #[inline]
219    #[must_use]
220    pub fn as_bytes(&self) -> &[u8] {
221        &self.0
222    }
223}
224
225/// Bundle of all ZK artifacts for a single circuit. Port of TS `ZKConfig<K>`.
226#[derive(Debug, Clone, PartialEq, Eq)]
227pub struct ZkConfig {
228    pub circuit_id: CircuitId,
229    pub prover_key: ProverKey,
230    pub verifier_key: VerifierKey,
231    pub zkir: Zkir,
232}
233
234#[cfg(test)]
235mod tests {
236    use super::*;
237
238    #[test]
239    fn private_state_id_as_str_roundtrips() {
240        let id = PrivateStateId::new("hello");
241        assert_eq!(id.as_str(), "hello");
242        assert_eq!(format!("{id}"), "hello");
243    }
244
245    #[test]
246    fn circuit_id_from_str_and_display() {
247        let id: CircuitId = "increment".into();
248        assert_eq!(id.as_str(), "increment");
249        assert_eq!(format!("{id}"), "increment");
250    }
251
252    #[test]
253    fn zk_artifact_byte_accessors() {
254        let z = Zkir::new(vec![1u8, 2, 3]);
255        assert_eq!(z.as_bytes(), &[1, 2, 3]);
256        let pk = ProverKey::new(vec![9u8, 9, 9]);
257        assert_eq!(pk.as_bytes(), &[9, 9, 9]);
258        let vk = VerifierKey::new(vec![0xff, 0xee]);
259        assert_eq!(vk.as_bytes(), &[0xff, 0xee]);
260    }
261
262    #[test]
263    fn coin_pubkey_hex_is_lowercase_64_chars() {
264        let pk = CoinPublicKey([0xabu8; 32]);
265        let hex = pk.to_hex();
266        assert_eq!(hex.len(), 64);
267        assert_eq!(hex, "ab".repeat(32));
268    }
269}