pub struct Client { /* private fields */ }Expand description
Generalized Electrum client that supports multiple backends. Can re-instantiate client_type if connections drops
Implementations§
Source§impl Client
impl Client
Sourcepub fn new(url: &str) -> Result<Self, Error>
pub fn new(url: &str) -> Result<Self, Error>
Default constructor supporting multiple backends by providing a prefix
Supported prefixes are:
- tcp:// for a TCP plaintext client.
- ssl:// for an SSL-encrypted client. The server certificate will be verified.
- ws:// for a WebSocket client (requires
use-websocketfeature). - wss:// for a secure WebSocket client (requires
use-websocketand a rustls feature).
If no prefix is specified, then tcp:// is assumed.
See Client::from_config for more configuration options
Examples found in repository?
More examples
examples/websocket.rs (line 15)
7fn main() {
8 // Test WSS (secure WebSocket)
9 // You can override the server with environment variables:
10 // WSS_SERVER=wss://your-server:port cargo run --example websocket --features use-websocket
11 let wss_server =
12 std::env::var("WSS_SERVER").unwrap_or_else(|_| "wss://rostrum.riften.net:443".to_string());
13
14 println!("Testing WSS connection to {}...", wss_server);
15 match Client::new(&wss_server) {
16 Ok(client) => {
17 println!("Connected to WSS server!");
18 match client.ping() {
19 Ok(()) => println!("WSS ping successful!"),
20 Err(e) => println!("WSS ping failed: {:?}", e),
21 }
22 }
23 Err(e) => println!("WSS connection failed: {:?}", e),
24 }
25
26 // Test WS (plaintext WebSocket)
27 let ws_server = std::env::var("WS_SERVER")
28 .unwrap_or_else(|_| "ws://rostrum.cauldron.quest:50003".to_string());
29
30 println!("\nTesting WS connection to {}...", ws_server);
31 match Client::new(&ws_server) {
32 Ok(client) => {
33 println!("Connected to WS server!");
34 match client.ping() {
35 Ok(()) => println!("WS ping successful!"),
36 Err(e) => println!("WS ping failed: {:?}", e),
37 }
38 }
39 Err(e) => println!("WS connection failed: {:?}", e),
40 }
41}examples/batch_headers.rs (line 12)
7fn main() {
8 let server = std::env::var("ELECTRUM_SERVER")
9 .unwrap_or_else(|_| "wss://rostrum.riften.net:443".to_string());
10
11 println!("Connecting to {}...", server);
12 let client = match Client::new(&server) {
13 Ok(c) => c,
14 Err(e) => {
15 eprintln!("Connection failed: {:?}", e);
16 return;
17 }
18 };
19
20 println!("Connected! Testing ping...");
21 if let Err(e) = client.ping() {
22 eprintln!("Ping failed: {:?}", e);
23 return;
24 }
25 println!("Ping successful!");
26
27 // Create a batch request for multiple block headers
28 let mut batch = Batch::default();
29
30 // Request headers for blocks 0, 1, 2, 3, 4
31 let heights = [0, 1, 2, 3, 4];
32 for height in &heights {
33 batch.raw(
34 "blockchain.block.header".to_string(),
35 vec![Param::Usize(*height)],
36 );
37 }
38
39 println!("\nBatch requesting {} headers...", heights.len());
40
41 match client.batch_call(&batch) {
42 Ok(results) => {
43 println!("Got {} results:", results.len());
44 for (i, (height, result)) in heights.iter().zip(results.iter()).enumerate() {
45 let header_hex = result.as_str().unwrap_or("<not a string>");
46 // Show first 32 chars of header hex
47 let preview = if header_hex.len() > 64 {
48 format!("{}...", &header_hex[..64])
49 } else {
50 header_hex.to_string()
51 };
52 println!(" [{}] Block {}: {}", i, height, preview);
53 }
54 }
55 Err(e) => {
56 eprintln!("Batch call failed: {:?}", e);
57 }
58 }
59}Sourcepub fn from_config(url: &str, config: Config) -> Result<Self, Error>
pub fn from_config(url: &str, config: Config) -> Result<Self, Error>
Generic constructor that supports multiple backends and allows configuration through the Config
Examples found in repository?
examples/tor.rs (line 9)
3fn main() {
4 // NOTE: This assumes Tor is running localy, with an unauthenticated Socks5 listening at
5 // localhost:9050
6 let proxy = Socks5Config::new("127.0.0.1:9050");
7 let config = ConfigBuilder::new().socks5(Some(proxy)).build();
8
9 let client = Client::from_config("tcp://explorernuoc63nb.onion:110", config.clone()).unwrap();
10 let res = client.ping();
11 println!("{:#?}", res);
12
13 // works both with onion v2/v3 (if your Tor supports them)
14 let client = Client::from_config(
15 "tcp://explorerzydxu5ecjrkwceayqybizmpjjznk5izmitf2modhcusuqlid.onion:110",
16 config,
17 )
18 .unwrap();
19 let res = client.ping();
20 println!("{:#?}", res);
21}Trait Implementations§
Source§impl ElectrumApi for Client
impl ElectrumApi for Client
Source§fn raw_call(
&self,
method_name: &str,
params: impl IntoIterator<Item = Param>,
) -> Result<Value, Error>
fn raw_call( &self, method_name: &str, params: impl IntoIterator<Item = Param>, ) -> Result<Value, Error>
Executes the requested API call returning the raw answer.
Auto Trait Implementations§
impl !Freeze for Client
impl RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl UnsafeUnpin for Client
impl UnwindSafe for Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more