Skip to content

Commit 83a4f89

Browse files
authored
feat: add bool frontend type (#63)
1 parent 2d4d31d commit 83a4f89

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed

src/backends/mock_main.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ impl Pod for MockMainPod {
505505
pub mod tests {
506506
use super::*;
507507
use crate::backends::mock_signed::MockSigner;
508-
use crate::examples::{great_boy_pod_full_flow, zu_kyc_pod_builder, zu_kyc_sign_pod_builders};
508+
use crate::examples::{great_boy_pod_full_flow, tickets_pod_full_flow, zu_kyc_pod_builder, zu_kyc_sign_pod_builders};
509509
use crate::middleware;
510510

511511
#[test]
@@ -550,4 +550,15 @@ pub mod tests {
550550

551551
assert_eq!(pod.verify(), true);
552552
}
553+
554+
#[test]
555+
fn test_mock_main_tickets() {
556+
let tickets_builder = tickets_pod_full_flow();
557+
let mut prover = MockProver {};
558+
let proof_pod = tickets_builder.prove(&mut prover).unwrap();
559+
let pod = proof_pod.pod.into_any().downcast::<MockMainPod>().unwrap();
560+
561+
println!("{}", pod);
562+
assert_eq!(pod.verify(), true);
563+
}
553564
}

src/examples.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::HashMap;
22

3+
use crate::backends::mock_signed::MockSigner;
34
use crate::frontend::{MainPodBuilder, SignedPod, SignedPodBuilder, Value};
45
use crate::middleware::{containers::Dictionary, Params, PodType, KEY_SIGNER, KEY_TYPE};
56
use crate::op;
@@ -130,8 +131,6 @@ pub fn great_boy_pod_builder(
130131
}
131132

132133
pub fn great_boy_pod_full_flow() -> MainPodBuilder {
133-
use crate::backends::mock_signed::MockSigner;
134-
135134
let params = Params {
136135
max_input_signed_pods: 6,
137136
max_statements: 100,
@@ -194,3 +193,35 @@ pub fn great_boy_pod_full_flow() -> MainPodBuilder {
194193
alice,
195194
)
196195
}
196+
197+
// Tickets
198+
199+
pub fn tickets_sign_pod_builder(params: &Params) -> SignedPodBuilder {
200+
// Create a signed pod with all atomic types (string, int, bool)
201+
let mut builder = SignedPodBuilder::new(params);
202+
builder.insert("eventId", 123);
203+
builder.insert("productId", 456);
204+
builder.insert("attendeeName", "John Doe");
205+
builder.insert("attendeeEmail", "john.doe@example.com");
206+
builder.insert("isConsumed", true);
207+
builder.insert("isRevoked", false);
208+
builder
209+
}
210+
211+
pub fn tickets_pod_builder(params: &Params, signed_pod: &SignedPod, expected_event_id: i64, expect_consumed: bool, blacklisted_emails: &Value) -> MainPodBuilder {
212+
// Create a main pod referencing this signed pod with some statements
213+
let mut builder = MainPodBuilder::new(params);
214+
builder.add_signed_pod(signed_pod);
215+
builder.pub_op(op!(eq, (signed_pod, "eventId"), expected_event_id));
216+
builder.pub_op(op!(eq, (signed_pod, "isConsumed"), expect_consumed));
217+
builder.pub_op(op!(eq, (signed_pod, "isRevoked"), false));
218+
builder.pub_op(op!(not_contains, blacklisted_emails, (signed_pod, "attendeeEmail")));
219+
builder
220+
}
221+
222+
pub fn tickets_pod_full_flow() -> MainPodBuilder {
223+
let params = Params::default();
224+
let builder = tickets_sign_pod_builder(&params);
225+
let signed_pod = builder.sign(&mut MockSigner { pk: "test".into() }).unwrap();
226+
tickets_pod_builder(&params, &signed_pod, 123, true, &Value::Dictionary(Dictionary::new(&HashMap::new())))
227+
}

src/frontend.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub struct Origin(pub PodClass, pub PodId);
3030
pub enum Value {
3131
String(String),
3232
Int(i64),
33+
Bool(bool),
3334
Dictionary(Dictionary),
3435
Set(Set),
3536
Array(Array),
@@ -47,11 +48,18 @@ impl From<i64> for Value {
4748
}
4849
}
4950

51+
impl From<bool> for Value {
52+
fn from(b: bool) -> Self {
53+
Value::Bool(b)
54+
}
55+
}
56+
5057
impl From<&Value> for middleware::Value {
5158
fn from(v: &Value) -> Self {
5259
match v {
5360
Value::String(s) => middleware::Value(hash_str(s).0),
5461
Value::Int(v) => middleware::Value::from(*v),
62+
Value::Bool(b) => middleware::Value::from(*b as i64),
5563
Value::Dictionary(d) => middleware::Value(d.commitment().0),
5664
Value::Set(s) => middleware::Value(s.commitment().0),
5765
Value::Array(a) => middleware::Value(a.commitment().0),
@@ -64,6 +72,7 @@ impl fmt::Display for Value {
6472
match self {
6573
Value::String(s) => write!(f, "\"{}\"", s),
6674
Value::Int(v) => write!(f, "{}", v),
75+
Value::Bool(b) => write!(f, "{}", b),
6776
Value::Dictionary(d) => write!(f, "dict:{}", d.commitment()),
6877
Value::Set(s) => write!(f, "set:{}", s.commitment()),
6978
Value::Array(a) => write!(f, "arr:{}", a.commitment()),
@@ -224,6 +233,12 @@ impl From<i64> for OperationArg {
224233
}
225234
}
226235

236+
impl From<bool> for OperationArg {
237+
fn from(b: bool) -> Self {
238+
Self::Literal(Value::from(b))
239+
}
240+
}
241+
227242
impl From<(Origin, &str)> for OperationArg {
228243
fn from((origin, key): (Origin, &str)) -> Self {
229244
Self::Key(AnchoredKey(origin, key.to_string()))
@@ -578,8 +593,9 @@ pub mod build_utils {
578593
#[cfg(test)]
579594
pub mod tests {
580595
use super::*;
596+
use crate::backends::mock_main::MockProver;
581597
use crate::backends::mock_signed::MockSigner;
582-
use crate::examples::{great_boy_pod_full_flow, zu_kyc_pod_builder, zu_kyc_sign_pod_builders};
598+
use crate::examples::{great_boy_pod_full_flow, tickets_pod_full_flow, zu_kyc_pod_builder, zu_kyc_sign_pod_builders};
583599

584600
#[test]
585601
fn test_front_zu_kyc() -> Result<()> {
@@ -617,4 +633,12 @@ pub mod tests {
617633

618634
Ok(())
619635
}
620-
}
636+
637+
#[test]
638+
fn test_front_tickets() -> Result<()> {
639+
let builder = tickets_pod_full_flow();
640+
println!("{}", builder);
641+
642+
Ok(())
643+
}
644+
}

0 commit comments

Comments
 (0)