1use std::fmt::Debug;
2
3use fawkes_crypto::{
4 backend::bellman_groth16::{
5 engines::Engine,
6 group::{G1Point, G2Point},
7 prover::Proof,
8 },
9 ff_uint::Num,
10};
11use serde::{Deserialize, Serialize};
12
13pub mod evm;
14pub mod near;
15pub mod substrate;
16mod utils;
17pub mod waves;
18
19#[derive(Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq)]
22#[repr(u16)]
23pub enum TxType {
24 #[serde(rename = "0000")]
25 Deposit = 0,
26 #[serde(rename = "0001")]
27 Transfer = 1,
28 #[serde(rename = "0002")]
29 Withdraw = 2,
30}
31
32impl TryFrom<u16> for TxType {
33 type Error = std::io::Error;
34
35 fn try_from(value: u16) -> Result<Self, Self::Error> {
36 match value {
37 0 => Ok(TxType::Deposit),
38 1 => Ok(TxType::Transfer),
39 2 => Ok(TxType::Withdraw),
40 _ => Err(std::io::Error::new(
41 std::io::ErrorKind::InvalidData,
42 "invalid tx type",
43 )),
44 }
45 }
46}
47
48#[derive(Serialize, Deserialize)]
49#[serde(bound = "")]
50pub struct TxData<E: Engine> {
51 pub tx_type: TxType,
52 pub proof: Proof<E>,
53 pub tree_proof: Proof<E>,
54 pub root_after: Num<E::Fr>,
55 pub delta: Num<E::Fr>,
56 pub out_commit: Num<E::Fr>,
57 pub nullifier: Num<E::Fr>,
58 pub memo: Vec<u8>,
59 pub extra_data: Vec<u8>,
60 pub token_id: String,
61}
62
63struct DebugProof<'a, E: Engine>(&'a Proof<E>);
64
65impl<'a, E: Engine> Debug for DebugProof<'a, E> {
66 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67 f.debug_struct("Proof")
68 .field("a", &(self.0.a.0, self.0.a.1))
69 .field(
70 "b",
71 &(self.0.b.0 .0, self.0.b.0 .1, self.0.b.1 .0, self.0.b.1 .1),
72 )
73 .field("c", &(self.0.c.0, self.0.c.1))
74 .finish()
75 }
76}
77
78impl<E: Engine> Debug for TxData<E> {
79 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80 f.debug_struct("TxData")
81 .field("tx_type", &self.tx_type)
82 .field("proof", &DebugProof(&self.proof))
83 .field("tree_proof", &DebugProof(&self.tree_proof))
84 .field("root_after", &self.root_after)
85 .field("delta", &self.delta)
86 .field("out_commit", &self.out_commit)
87 .field("nullifier", &self.nullifier)
88 .field("memo", &self.memo)
89 .field("extra_data", &self.extra_data)
90 .field("token_id", &self.token_id)
91 .finish()
92 }
93}
94
95impl<E: Engine> Clone for TxData<E> {
96 fn clone(&self) -> Self {
97 Self {
98 tx_type: self.tx_type,
99 proof: Proof {
100 a: G1Point(self.proof.a.0, self.proof.a.1),
101 b: G2Point(self.proof.b.0, self.proof.b.1),
102 c: G1Point(self.proof.c.0, self.proof.c.1),
103 },
104 tree_proof: Proof {
105 a: G1Point(self.tree_proof.a.0, self.tree_proof.a.1),
106 b: G2Point(self.tree_proof.b.0, self.tree_proof.b.1),
107 c: G1Point(self.tree_proof.c.0, self.tree_proof.c.1),
108 },
109 root_after: self.root_after.clone(),
110 delta: self.delta.clone(),
111 out_commit: self.out_commit.clone(),
112 nullifier: self.nullifier.clone(),
113 memo: self.memo.clone(),
114 extra_data: self.extra_data.clone(),
115 token_id: self.token_id.clone(),
116 }
117 }
118}
119
120impl<E: Engine> PartialEq for TxData<E> {
121 fn eq(&self, other: &Self) -> bool {
122 self.tx_type == other.tx_type
123 && self.proof.a.0 == other.proof.a.0
124 && self.proof.a.1 == other.proof.a.1
125 && self.proof.b.0 .0 == other.proof.b.0 .0
126 && self.proof.b.0 .1 == other.proof.b.0 .1
127 && self.proof.b.1 .0 == other.proof.b.1 .0
128 && self.proof.b.1 .1 == other.proof.b.1 .1
129 && self.proof.c.0 == other.proof.c.0
130 && self.proof.c.1 == other.proof.c.1
131 && self.tree_proof.a.0 == other.tree_proof.a.0
132 && self.tree_proof.a.1 == other.tree_proof.a.1
133 && self.tree_proof.b.0 .0 == other.tree_proof.b.0 .0
134 && self.tree_proof.b.0 .1 == other.tree_proof.b.0 .1
135 && self.tree_proof.b.1 .0 == other.tree_proof.b.1 .0
136 && self.tree_proof.b.1 .1 == other.tree_proof.b.1 .1
137 && self.tree_proof.c.0 == other.tree_proof.c.0
138 && self.tree_proof.c.1 == other.tree_proof.c.1
139 && self.root_after == other.root_after
140 && self.delta == other.delta
141 && self.out_commit == other.out_commit
142 && self.nullifier == other.nullifier
143 && self.memo == other.memo
144 && self.extra_data == other.extra_data
145 }
146}
147
148impl<E: Engine> Eq for TxData<E> {}