-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add ProveResponse to unionlabs (#811)
no more proto generated type merge after #810
- Loading branch information
Showing
7 changed files
with
187 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
pub mod prove_request; | ||
pub mod prove_response; | ||
pub mod validator_set_commit; | ||
pub mod zero_knowledge_proof; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{ | ||
errors::{required, InvalidLength, MissingField}, | ||
ethereum::H256, | ||
union::galois::zero_knowledge_proof::ZeroKnowledgeProof, | ||
Proto, TypeUrl, | ||
}; | ||
|
||
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)] | ||
pub struct ProveResponse { | ||
pub proof: ZeroKnowledgeProof, | ||
pub trusted_validator_set_root: H256, | ||
pub untrusted_validator_set_root: H256, | ||
} | ||
|
||
impl Proto for ProveResponse { | ||
type Proto = protos::union::galois::api::v1::ProveResponse; | ||
} | ||
|
||
impl TypeUrl for protos::union::galois::api::v1::ProveResponse { | ||
const TYPE_URL: &'static str = "/union.galois.api.v1.ProveResponse"; | ||
} | ||
|
||
impl From<ProveResponse> for protos::union::galois::api::v1::ProveResponse { | ||
fn from(value: ProveResponse) -> Self { | ||
Self { | ||
proof: Some(value.proof.into()), | ||
trusted_validator_set_root: value.trusted_validator_set_root.into(), | ||
untrusted_validator_set_root: value.untrusted_validator_set_root.into(), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum TryFromProveResponseError { | ||
MissingField(MissingField), | ||
TrustedValidatorSetRoot(InvalidLength), | ||
UntrustedValidatorSetRoot(InvalidLength), | ||
} | ||
|
||
impl TryFrom<protos::union::galois::api::v1::ProveResponse> for ProveResponse { | ||
type Error = TryFromProveResponseError; | ||
|
||
fn try_from(value: protos::union::galois::api::v1::ProveResponse) -> Result<Self, Self::Error> { | ||
Ok(Self { | ||
proof: required!(value.proof)?.into(), | ||
trusted_validator_set_root: value | ||
.trusted_validator_set_root | ||
.try_into() | ||
.map_err(TryFromProveResponseError::TrustedValidatorSetRoot)?, | ||
untrusted_validator_set_root: value | ||
.untrusted_validator_set_root | ||
.try_into() | ||
.map_err(TryFromProveResponseError::UntrustedValidatorSetRoot)?, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use std::fmt::Debug; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{Proto, TypeUrl}; | ||
|
||
#[derive(Clone, PartialEq, Serialize, Deserialize)] | ||
pub struct ZeroKnowledgeProof { | ||
#[serde(with = "::serde_utils::hex_string")] | ||
pub content: Vec<u8>, | ||
#[serde(with = "::serde_utils::hex_string")] | ||
pub compressed_content: Vec<u8>, | ||
#[serde(with = "::serde_utils::hex_string")] | ||
pub evm_proof: Vec<u8>, | ||
#[serde(with = "::serde_utils::hex_string")] | ||
pub public_inputs: Vec<u8>, | ||
} | ||
|
||
impl Debug for ZeroKnowledgeProof { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
f.debug_struct("ZeroKnowledgeProof") | ||
.field("content", &serde_utils::to_hex(&self.content)) | ||
.field( | ||
"compressed_content", | ||
&serde_utils::to_hex(&self.compressed_content), | ||
) | ||
.field("evm_proof", &serde_utils::to_hex(&self.evm_proof)) | ||
.field("public_inputs", &serde_utils::to_hex(&self.public_inputs)) | ||
.finish() | ||
} | ||
} | ||
|
||
impl Proto for ZeroKnowledgeProof { | ||
type Proto = protos::union::galois::api::v1::ZeroKnowledgeProof; | ||
} | ||
|
||
impl TypeUrl for protos::union::galois::api::v1::ZeroKnowledgeProof { | ||
const TYPE_URL: &'static str = "/union.galois.api.v1.ZeroKnowledgeProof"; | ||
} | ||
|
||
impl From<ZeroKnowledgeProof> for protos::union::galois::api::v1::ZeroKnowledgeProof { | ||
fn from(value: ZeroKnowledgeProof) -> Self { | ||
Self { | ||
content: value.content, | ||
compressed_content: value.compressed_content, | ||
evm_proof: value.evm_proof, | ||
public_inputs: value.public_inputs, | ||
} | ||
} | ||
} | ||
|
||
impl From<protos::union::galois::api::v1::ZeroKnowledgeProof> for ZeroKnowledgeProof { | ||
fn from(value: protos::union::galois::api::v1::ZeroKnowledgeProof) -> Self { | ||
Self { | ||
content: value.content, | ||
compressed_content: value.compressed_content, | ||
evm_proof: value.evm_proof, | ||
public_inputs: value.public_inputs, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn serde() { | ||
let json = serde_json::to_string_pretty(&ZeroKnowledgeProof { | ||
content: [].into(), | ||
compressed_content: [].into(), | ||
evm_proof: [].into(), | ||
public_inputs: [].into(), | ||
}) | ||
.unwrap(); | ||
|
||
println!("{json}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters