Skip to content

Commit 42639ae

Browse files
committed
feat(example): add TOFU certificate validation example
- Provide an usage example for the trait - Demonstrate how to initialize a using - Include a sample in-memory store implementation for demonstration purposes
1 parent f1dd212 commit 42639ae

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

examples/tofu.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
extern crate electrum_client;
2+
3+
use electrum_client::{Client, Config, 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+
36+
let client = Client::from_config_with_tofu(
37+
"ssl://electrum.blockstream.info:50002",
38+
Config::default(),
39+
store,
40+
)
41+
.unwrap();
42+
43+
let res = client.server_features();
44+
println!("{:#?}", res);
45+
}

0 commit comments

Comments
 (0)