Skip to content

Commit 9178ff9

Browse files
committed
docs: add TOFU certificate validation example
Add examples/tofu.rs, demonstrating Trust On First Use (TOFU) certificate validation for SSL connections. The example shows how to implement a custom TofuStore and how to configure a client to use TOFU for secure certificate verification. It uses a simple in-memory TofuStore implementation for demonstration purposes only.
1 parent 63beb8a commit 9178ff9

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

examples/tofu.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
extern crate electrum_client;
2+
3+
use electrum_client::{Client, ConfigBuilder, ElectrumApi, TofuStore};
4+
use std::collections::HashMap;
5+
use std::sync::{Arc, Mutex};
6+
7+
/// A simple in-memory implementation of TofuStore for demonstration purposes.
8+
#[derive(Debug, Default)]
9+
struct MyTofuStore {
10+
certs: Mutex<HashMap<String, Vec<u8>>>,
11+
}
12+
13+
impl TofuStore for MyTofuStore {
14+
fn get_certificate(
15+
&self,
16+
host: &str,
17+
) -> Result<Option<Vec<u8>>, Box<dyn std::error::Error + Send + Sync>> {
18+
let certs = self.certs.lock().unwrap();
19+
Ok(certs.get(host).cloned())
20+
}
21+
22+
fn set_certificate(
23+
&self,
24+
host: &str,
25+
cert: Vec<u8>,
26+
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
27+
let mut certs = self.certs.lock().unwrap();
28+
certs.insert(host.to_string(), cert);
29+
Ok(())
30+
}
31+
}
32+
33+
fn main() {
34+
let store = Arc::new(MyTofuStore::default());
35+
let config = ConfigBuilder::new().tofu_store(store).build();
36+
37+
let client =
38+
Client::from_config("ssl://electrum.blockstream.info:50002", config).unwrap();
39+
let res = client.server_features();
40+
println!("{:#?}", res);
41+
}
42+
43+

0 commit comments

Comments
 (0)