Skip to content

Commit dfc1629

Browse files
heeckhausinui0
andauthored
fix(examples): fix examples for alpha.7 release (#603)
* doc: Fix examples for alpha7 release + Use secp256k1 key for notary server fixture + fix tower issue + Fixed doctes issues (Avoid doc test failures when ignored tests are run) + Run wasm tests in incognitto mode to avoid chromiumoxide ws errors * Added comment * minor improvements * formatting * polish attestation example * use shorthand fs write * clean * simplify discord example --------- Co-authored-by: sinu <[email protected]>
1 parent 6e20930 commit dfc1629

File tree

22 files changed

+408
-570
lines changed

22 files changed

+408
-570
lines changed

crates/core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
//! ```no_run
4646
//! # use tlsn_core::transcript::{TranscriptCommitConfigBuilder, Transcript, Direction};
4747
//! # use tlsn_core::hash::HashAlgId;
48-
//! # fn main() -> Result<(), Box<dyn std::error::Error> {
48+
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
4949
//! # let transcript: Transcript = unimplemented!();
5050
//! let (sent_len, recv_len) = transcript.len();
5151
//!

crates/core/src/transcript/proof.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,32 @@ impl<'a> TranscriptProofBuilder<'a> {
268268
self.reveal_with_kind(ranges, direction, self.default_kind)
269269
}
270270

271+
/// Reveals the given ranges in the sent transcript using the default kind
272+
/// of commitment.
273+
///
274+
/// # Arguments
275+
///
276+
/// * `ranges` - The ranges to reveal.
277+
pub fn reveal_sent(
278+
&mut self,
279+
ranges: &dyn ToRangeSet<usize>,
280+
) -> Result<&mut Self, TranscriptProofBuilderError> {
281+
self.reveal(ranges, Direction::Sent)
282+
}
283+
284+
/// Reveals the given ranges in the received transcript using the default
285+
/// kind of commitment.
286+
///
287+
/// # Arguments
288+
///
289+
/// * `ranges` - The ranges to reveal.
290+
pub fn reveal_recv(
291+
&mut self,
292+
ranges: &dyn ToRangeSet<usize>,
293+
) -> Result<&mut Self, TranscriptProofBuilderError> {
294+
self.reveal(ranges, Direction::Received)
295+
}
296+
271297
/// Builds the transcript proof.
272298
pub fn build(self) -> Result<TranscriptProof, TranscriptProofBuilderError> {
273299
let encoding_proof = if !self.encoding_proof_idxs.is_empty() {

crates/data-fixtures/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ macro_rules! define_fixture {
44
($name:ident, $doc:tt, $path:tt) => {
55
#[doc = $doc]
66
///
7-
/// ```ignore
7+
/// ```text
88
#[doc = include_str!($path)]
99
/// ```
1010
pub const $name: &[u8] = include_bytes!($path);

crates/examples/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Ignore files from examples.
2+
*.tlsn

crates/examples/Cargo.toml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@ notary-client = { workspace = true }
99
tlsn-common = { workspace = true }
1010
tlsn-core = { workspace = true }
1111
tlsn-prover = { workspace = true }
12+
tlsn-utils = { workspace = true }
1213
tlsn-verifier = { workspace = true }
14+
tlsn-formats = { workspace = true }
1315

16+
bincode = { workspace = true }
1417
chrono = { workspace = true }
1518
dotenv = { version = "0.15.0" }
1619
elliptic-curve = { workspace = true, features = ["pkcs8"] }
1720
futures = { workspace = true }
1821
http-body-util = { workspace = true }
22+
hex = { workspace = true }
1923
hyper = { workspace = true, features = ["client", "http1"] }
2024
hyper-util = { workspace = true, features = ["full"] }
2125
k256 = { workspace = true, features = ["ecdsa"] }
@@ -35,12 +39,16 @@ tracing = { workspace = true }
3539
tracing-subscriber = { workspace = true }
3640

3741
[[example]]
38-
name = "attestation_prover"
39-
path = "attestation/prover.rs"
42+
name = "attestation_prove"
43+
path = "attestation/prove.rs"
4044

4145
[[example]]
42-
name = "attestation_verifier"
43-
path = "attestation/verifier.rs"
46+
name = "attestation_present"
47+
path = "attestation/present.rs"
48+
49+
[[example]]
50+
name = "attestation_verify"
51+
path = "attestation/verify.rs"
4452

4553
[[example]]
4654
name = "interactive"
@@ -49,7 +57,3 @@ path = "interactive/interactive.rs"
4957
[[example]]
5058
name = "discord_dm"
5159
path = "discord/discord_dm.rs"
52-
53-
[[example]]
54-
name = "discord_dm_verifier"
55-
path = "discord/discord_dm_verifier.rs"

crates/examples/attestation/README.md

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
## Simple Example: Notarize Public Data from example.com (Rust) <a name="rust-simple"></a>
1+
## Simple Attestation Example: Notarize Public Data from example.com (Rust) <a name="rust-simple"></a>
22

33
This example demonstrates the simplest possible use case for TLSNotary:
4-
1. Notarize: Fetch <https://example.com/> and create a proof of its content.
5-
2. Verify the proof.
6-
7-
Next, we will redact the content and verify it again:
8-
1. Redact the `USER_AGENT` and titles.
9-
2. Verify the redacted proof.
4+
1. Fetch <https://example.com/> and acquire an attestation of its content.
5+
2. Create a verifiable presentation using the attestation, while redacting the value of a header.
6+
3. Verify the presentation.
107

118
### 1. Notarize <https://example.com/>
129

13-
Run a simple prover:
10+
Run the `prove` binary.
1411

1512
```shell
16-
cargo run --release --example simple_prover
13+
cargo run --release --example attestation_prove
1714
```
1815

1916
If the notarization was successful, you should see this output in the console:
@@ -22,66 +19,54 @@ If the notarization was successful, you should see this output in the console:
2219
Starting an MPC TLS connection with the server
2320
Got a response from the server
2421
Notarization completed successfully!
25-
The proof has been written to `simple_proof.json`
22+
The attestation has been written to `example.attestation.tlsn` and the corresponding secrets to `example.secrets.tlsn`.
2623
```
2724

28-
⚠️ In this simple example the `Notary` server is automatically started in the background. Note that this is for demonstration purposes only. In a real work example, the notary should be run by a neutral party or the verifier of the proofs. Consult the [Notary Server Docs](https://docs.tlsnotary.org/developers/notary_server.html) for more details on how to run a notary server.
25+
⚠️ In this simple example the `Notary` server is automatically started in the background. Note that this is for demonstration purposes only. In a real world example, the notary should be run by a trusted party. Consult the [Notary Server Docs](https://docs.tlsnotary.org/developers/notary_server.html) for more details on how to run a notary server.
26+
27+
### 2. Build a verifiable presentation
2928

30-
### 2. Verify the Proof
29+
This will build a verifiable presentation with the `User-Agent` header redacted from the request. This presentation can be shared with any verifier you wish to present the data to.
3130

32-
When you open `simple_proof.json` in an editor, you will see a JSON file with lots of non-human-readable byte arrays. You can decode this file by running:
31+
Run the `present` binary.
3332

3433
```shell
35-
cargo run --release --example simple_verifier
34+
cargo run --release --example attestation_present
3635
```
3736

38-
This will output the TLS-transaction in clear text:
37+
If successful, you should see this output in the console:
3938

4039
```log
41-
Successfully verified that the bytes below came from a session with Dns("example.com") at 2023-11-03 08:48:20 UTC.
42-
Note that the bytes which the Prover chose not to disclose are shown as X.
43-
44-
Bytes sent:
45-
...
46-
```
47-
48-
### 3. Redact Information
49-
50-
Open `simple_prover.rs` and locate the line with:
51-
52-
```rust
53-
let redact = false;
40+
Presentation built successfully!
41+
The presentation has been written to `example.presentation.tlsn`.
5442
```
5543

56-
and change it to:
44+
### 3. Verify the presentation
5745

58-
```rust
59-
let redact = true;
60-
```
46+
This will read the presentation from the previous step, verify it, and print the disclosed data to console.
6147

62-
Next, if you run the `simple_prover` and `simple_verifier` again, you'll notice redacted `X`'s in the output:
48+
Run the `verify` binary.
6349

6450
```shell
65-
cargo run --release --example simple_prover
66-
cargo run --release --example simple_verifier
51+
cargo run --release --example attestation_verify
6752
```
6853

54+
If successful, you should see this output in the console:
55+
6956
```log
70-
<!doctype html>
71-
<html>
72-
<head>
73-
<title>XXXXXXXXXXXXXX</title>
74-
...
75-
```
57+
Verifying presentation with {key algorithm} key: { hex encoded key }
7658
77-
You can also use <https://explorer.tlsnotary.org/> to inspect your proofs. Simply drag and drop `simple_proof.json` from your file explorer into the drop zone. Redacted bytes are marked with X characters. [Notary public key](../../notary/server/fixture/notary/notary.pub)
59+
**Ask yourself, do you trust this key?**
7860
79-
### (Optional) Extra Experiments
61+
-------------------------------------------------------------------
62+
Successfully verified that the data below came from a session with example.com at 2024-10-03 03:01:40 UTC.
63+
Note that the data which the Prover chose not to disclose are shown as X.
8064
81-
Feel free to try these extra challenges:
65+
Data sent:
66+
...
67+
```
8268

83-
- [ ] Modify the `server_name` (or any other data) in `simple_proof.json` and verify that the proof is no longer valid.
84-
- [ ] Modify the `build_proof_with_redactions` function in `simple_prover.rs` to redact more or different data.
69+
⚠️ Notice that the presentation comes with a "verifying key". This is the key the Notary used when issuing the attestation that the presentation was built from. If you trust the Notary, or more specifically the verifying key, then you can trust that the presented data is authentic.
8570

8671
### Next steps
8772

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// This example demonstrates how to build a verifiable presentation from an
2+
// attestation and the corresponding connection secrets. See the `prove.rs`
3+
// example to learn how to acquire an attestation from a Notary.
4+
5+
use tlsn_core::{attestation::Attestation, presentation::Presentation, CryptoProvider, Secrets};
6+
use tlsn_formats::http::HttpTranscript;
7+
8+
fn main() -> Result<(), Box<dyn std::error::Error>> {
9+
// Read attestation from disk.
10+
let attestation: Attestation =
11+
bincode::deserialize(&std::fs::read("example.attestation.tlsn")?)?;
12+
13+
// Read secrets from disk.
14+
let secrets: Secrets = bincode::deserialize(&std::fs::read("example.secrets.tlsn")?)?;
15+
16+
// Parse the HTTP transcript.
17+
let transcript = HttpTranscript::parse(secrets.transcript())?;
18+
19+
// Build a transcript proof.
20+
let mut builder = secrets.transcript_proof_builder();
21+
22+
let request = &transcript.requests[0];
23+
// Reveal the structure of the request without the headers or body.
24+
builder.reveal_sent(&request.without_data())?;
25+
// Reveal the request target.
26+
builder.reveal_sent(&request.request.target)?;
27+
// Reveal all headers except the value of the User-Agent header.
28+
for header in &request.headers {
29+
if !header.name.as_str().eq_ignore_ascii_case("User-Agent") {
30+
builder.reveal_sent(header)?;
31+
} else {
32+
builder.reveal_sent(&header.without_value())?;
33+
}
34+
}
35+
// Reveal the entire response.
36+
builder.reveal_recv(&transcript.responses[0])?;
37+
38+
let transcript_proof = builder.build()?;
39+
40+
// Use default crypto provider to build the presentation.
41+
let provider = CryptoProvider::default();
42+
43+
let mut builder = attestation.presentation_builder(&provider);
44+
45+
builder
46+
.identity_proof(secrets.identity_proof())
47+
.transcript_proof(transcript_proof);
48+
49+
let presentation: Presentation = builder.build()?;
50+
51+
// Write the presentation to disk.
52+
std::fs::write(
53+
"example.presentation.tlsn",
54+
bincode::serialize(&presentation)?,
55+
)?;
56+
57+
println!("Presentation built successfully!");
58+
println!("The presentation has been written to `example.presentation.tlsn`.");
59+
60+
Ok(())
61+
}

0 commit comments

Comments
 (0)