From b76b8314ad81ddf1e6f86c5aef51ed5a08510313 Mon Sep 17 00:00:00 2001 From: "sinu.eth" <65924192+sinui0@users.noreply.github.com> Date: Tue, 1 Oct 2024 07:17:48 -0700 Subject: [PATCH] feat(wasm): clone getters (#600) * feat(wasm): clone getters * fix(wasm): get rid of move semantics and implement Clone * disable test feature --- Cargo.toml | 2 +- crates/core/src/attestation/proof.rs | 4 +-- crates/core/src/connection/proof.rs | 2 +- crates/core/src/merkle.rs | 4 +-- crates/core/src/presentation.rs | 2 +- crates/core/src/secrets.rs | 2 +- crates/core/src/transcript/encoding/proof.rs | 4 +-- crates/core/src/transcript/encoding/tree.rs | 2 +- crates/core/src/transcript/proof.rs | 2 +- crates/wasm/src/tests.rs | 26 ++++++++++++++++++-- crates/wasm/src/types.rs | 11 +++++---- 11 files changed, 42 insertions(+), 19 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cadea13b3..f1750ca26 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -117,7 +117,7 @@ rand_chacha = { version = "0.3" } rand_core = { version = "0.6" } regex = { version = "1.10" } ring = { version = "0.17" } -rs_merkle = { git = "https://github.com/tlsnotary/rs-merkle.git", rev = "7fb354c" } +rs_merkle = { git = "https://github.com/tlsnotary/rs-merkle.git", rev = "85f3e82" } rstest = { version = "0.17" } rustls = { version = "0.21" } rustls-pemfile = { version = "1.0" } diff --git a/crates/core/src/attestation/proof.rs b/crates/core/src/attestation/proof.rs index faa57f71e..e193b1e4a 100644 --- a/crates/core/src/attestation/proof.rs +++ b/crates/core/src/attestation/proof.rs @@ -12,7 +12,7 @@ use crate::{ }; /// Proof of an attestation. -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct AttestationProof { signature: Signature, header: Header, @@ -71,7 +71,7 @@ impl AttestationProof { } /// Proof of an attestation body. -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub(crate) struct BodyProof { body: Body, proof: MerkleProof, diff --git a/crates/core/src/connection/proof.rs b/crates/core/src/connection/proof.rs index 4c93934ba..9de13ffcd 100644 --- a/crates/core/src/connection/proof.rs +++ b/crates/core/src/connection/proof.rs @@ -12,7 +12,7 @@ use crate::{ }; /// TLS server identity proof. -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct ServerIdentityProof { name: ServerName, opening: ServerCertOpening, diff --git a/crates/core/src/merkle.rs b/crates/core/src/merkle.rs index a9b13a699..6d521c958 100644 --- a/crates/core/src/merkle.rs +++ b/crates/core/src/merkle.rs @@ -16,7 +16,7 @@ impl MerkleError { } } -#[derive(Serialize, Deserialize)] +#[derive(Clone, Serialize, Deserialize)] pub(crate) struct MerkleProof { alg: HashAlgId, tree_len: usize, @@ -74,7 +74,7 @@ impl rs_merkle::Hasher for RsMerkleHasher<'_> { } } -#[derive(Serialize, Deserialize)] +#[derive(Clone, Serialize, Deserialize)] pub(crate) struct MerkleTree { alg: HashAlgId, tree: rs_merkle::MerkleTree, diff --git a/crates/core/src/presentation.rs b/crates/core/src/presentation.rs index 982402926..7503acf9b 100644 --- a/crates/core/src/presentation.rs +++ b/crates/core/src/presentation.rs @@ -12,7 +12,7 @@ use crate::{ }; /// A verifiable presentation. -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Presentation { attestation: AttestationProof, identity: Option, diff --git a/crates/core/src/secrets.rs b/crates/core/src/secrets.rs index a875f3e29..14c1e7cb2 100644 --- a/crates/core/src/secrets.rs +++ b/crates/core/src/secrets.rs @@ -9,7 +9,7 @@ use crate::{ }; /// Secret data of an [`Attestation`](crate::attestation::Attestation). -#[derive(Serialize, Deserialize)] +#[derive(Clone, Serialize, Deserialize)] pub struct Secrets { pub(crate) server_name: ServerName, pub(crate) server_cert_opening: ServerCertOpening, diff --git a/crates/core/src/transcript/encoding/proof.rs b/crates/core/src/transcript/encoding/proof.rs index 0f5d3a786..ead2a25b5 100644 --- a/crates/core/src/transcript/encoding/proof.rs +++ b/crates/core/src/transcript/encoding/proof.rs @@ -16,7 +16,7 @@ use crate::{ }; /// An opening of a leaf in the encoding tree. -#[derive(Serialize, Deserialize)] +#[derive(Clone, Serialize, Deserialize)] pub(super) struct Opening { pub(super) direction: Direction, pub(super) seq: Subsequence, @@ -26,7 +26,7 @@ pub(super) struct Opening { opaque_debug::implement!(Opening); /// An encoding proof. -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct EncodingProof { pub(super) inclusion_proof: MerkleProof, pub(super) openings: HashMap, diff --git a/crates/core/src/transcript/encoding/tree.rs b/crates/core/src/transcript/encoding/tree.rs index 6f2fa8df3..22df01e58 100644 --- a/crates/core/src/transcript/encoding/tree.rs +++ b/crates/core/src/transcript/encoding/tree.rs @@ -52,7 +52,7 @@ impl EncodingLeaf { } /// A merkle tree of transcript encodings. -#[derive(Serialize, Deserialize)] +#[derive(Clone, Serialize, Deserialize)] pub struct EncodingTree { /// Merkle tree of the commitments. tree: MerkleTree, diff --git a/crates/core/src/transcript/proof.rs b/crates/core/src/transcript/proof.rs index 65ef148e5..fd8dd4f32 100644 --- a/crates/core/src/transcript/proof.rs +++ b/crates/core/src/transcript/proof.rs @@ -19,7 +19,7 @@ use crate::{ }; /// Proof of the contents of a transcript. -#[derive(Serialize, Deserialize)] +#[derive(Clone, Serialize, Deserialize)] pub struct TranscriptProof { encoding_proof: Option, hash_proofs: Vec, diff --git a/crates/wasm/src/tests.rs b/crates/wasm/src/tests.rs index c5f582e74..7a206983b 100644 --- a/crates/wasm/src/tests.rs +++ b/crates/wasm/src/tests.rs @@ -11,8 +11,11 @@ use tlsn_verifier::{Verifier, VerifierConfig}; use wasm_bindgen::prelude::*; use crate::{ + build_presentation, prover::JsProver, - types::{Commit, HttpRequest, Method, Reveal}, + types::{ + Attestation, Commit, HttpRequest, Method, NotarizationOutput, Presentation, Reveal, Secrets, + }, verifier::JsVerifier, }; @@ -122,13 +125,32 @@ pub async fn test_notarize() -> Result<(), JsValue> { let _ = prover.transcript()?; - prover + let NotarizationOutput { + attestation, + secrets, + } = prover .notarize(Commit { sent: vec![0..10], recv: vec![0..10], }) .await?; + let attestation = Attestation::deserialize(attestation.serialize())?; + let secrets = Secrets::deserialize(secrets.serialize())?; + + let presentation = build_presentation( + &attestation, + &secrets, + Reveal { + sent: vec![(0..10)], + recv: vec![(0..10)], + }, + )?; + + let presentation = Presentation::deserialize(presentation.serialize())?; + + let _ = presentation.verify()?; + Ok(()) } diff --git a/crates/wasm/src/types.rs b/crates/wasm/src/types.rs index d9041ddaf..73ff1df78 100644 --- a/crates/wasm/src/types.rs +++ b/crates/wasm/src/types.rs @@ -179,7 +179,7 @@ pub enum KeyType { P256, } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] #[wasm_bindgen] #[serde(transparent)] pub struct Attestation(pub(crate) tlsn_core::attestation::Attestation); @@ -207,7 +207,7 @@ impl From for Attestation { } } -#[derive(Debug, Serialize, Deserialize)] +#[derive(Debug, Clone, Serialize, Deserialize)] #[wasm_bindgen] #[serde(transparent)] pub struct Secrets(pub(crate) tlsn_core::Secrets); @@ -244,10 +244,11 @@ pub struct Presentation(tlsn_core::presentation::Presentation); #[wasm_bindgen] impl Presentation { /// Verifies the presentation. - pub fn verify(self) -> Result { + pub fn verify(&self) -> Result { let provider = CryptoProvider::default(); self.0 + .clone() .verify(&provider) .map(PresentationOutput::from) .map_err(JsError::from) @@ -288,8 +289,8 @@ impl From for PresentationOutput { } } -#[derive(Debug, Tsify, Serialize)] -#[tsify(into_wasm_abi)] +#[derive(Debug, Serialize)] +#[wasm_bindgen(getter_with_clone)] pub struct NotarizationOutput { pub attestation: Attestation, pub secrets: Secrets,