146 lines
4.6 KiB
Rust
146 lines
4.6 KiB
Rust
//! End-to-end authentication example
|
|
//!
|
|
//! This example demonstrates the complete authentication flow with the simplified approach:
|
|
//! 1. Create a WebSocket client with authentication configuration
|
|
//! 2. Authenticate using private key
|
|
//! 3. Connect to WebSocket with authentication
|
|
//! 4. Send authenticated requests
|
|
//!
|
|
//! To run this example:
|
|
//! ```bash
|
|
//! cargo run --example client_auth_example --features "crypto"
|
|
//! ```
|
|
|
|
use circle_client_ws::CircleWsClientBuilder;
|
|
use log::{error, info};
|
|
use std::time::Duration;
|
|
use tokio::time::sleep;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
// Initialize logging
|
|
env_logger::init();
|
|
|
|
info!("Starting simplified authentication example");
|
|
|
|
// Configuration
|
|
let ws_url = "ws://localhost:8080/ws".to_string();
|
|
|
|
// Example 1: Authenticate with private key
|
|
info!("=== Example 1: Private Key Authentication ===");
|
|
let private_key = "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef";
|
|
|
|
let mut client = CircleWsClientBuilder::new(ws_url.clone())
|
|
.with_keypair(private_key.to_string())
|
|
.build();
|
|
|
|
match client.connect().await {
|
|
Ok(_) => {
|
|
info!("Successfully connected to WebSocket");
|
|
}
|
|
Err(e) => {
|
|
error!("WebSocket connection failed: {}", e);
|
|
return Err(e.into());
|
|
}
|
|
}
|
|
|
|
match client.authenticate().await {
|
|
Ok(true) => {
|
|
info!("Successfully authenticated with private key");
|
|
}
|
|
Ok(false) => {
|
|
error!("Authentication failed");
|
|
}
|
|
Err(e) => {
|
|
error!("Private key authentication failed: {}", e);
|
|
}
|
|
}
|
|
|
|
// Example 2: Send authenticated request
|
|
info!("=== Example 2: Send Authenticated Request ===");
|
|
let script = "print('Hello from authenticated client!');".to_string();
|
|
match client.play(script).await {
|
|
Ok(result) => {
|
|
info!("Play request successful: {}", result.output);
|
|
}
|
|
Err(e) => {
|
|
error!("Play request failed: {}", e);
|
|
}
|
|
}
|
|
|
|
// Keep connection alive for a moment
|
|
sleep(Duration::from_secs(2)).await;
|
|
|
|
// Disconnect
|
|
client.disconnect().await;
|
|
info!("Disconnected from WebSocket");
|
|
|
|
// Example 3: Different private key authentication
|
|
info!("=== Example 3: Different Private Key Authentication ===");
|
|
let private_key2 = "0xfedcba0987654321fedcba0987654321fedcba0987654321fedcba0987654321";
|
|
|
|
let mut client2 = CircleWsClientBuilder::new(ws_url.clone())
|
|
.with_keypair(private_key2.to_string())
|
|
.build();
|
|
|
|
match client2.connect().await {
|
|
Ok(_) => {
|
|
info!("Connected with second private key authentication");
|
|
|
|
match client2.authenticate().await {
|
|
Ok(true) => {
|
|
info!("Successfully authenticated with second private key");
|
|
let script = "print('Hello from second auth!');".to_string();
|
|
match client2.play(script).await {
|
|
Ok(result) => {
|
|
info!("Second auth request successful: {}", result.output);
|
|
}
|
|
Err(e) => {
|
|
error!("Second auth request failed: {}", e);
|
|
}
|
|
}
|
|
}
|
|
Ok(false) => {
|
|
error!("Second private key authentication failed");
|
|
}
|
|
Err(e) => {
|
|
error!("Second private key authentication failed: {}", e);
|
|
}
|
|
}
|
|
|
|
client2.disconnect().await;
|
|
}
|
|
Err(e) => {
|
|
error!("Second auth connection failed: {}", e);
|
|
}
|
|
}
|
|
|
|
// Example 4: Non-authenticated connection (fallback)
|
|
info!("=== Example 4: Non-Authenticated Connection ===");
|
|
let mut client3 = CircleWsClientBuilder::new(ws_url).build();
|
|
|
|
match client3.connect().await {
|
|
Ok(()) => {
|
|
info!("Connected without authentication (fallback mode)");
|
|
|
|
let script = "print('Hello from non-auth client!');".to_string();
|
|
match client3.play(script).await {
|
|
Ok(result) => {
|
|
info!("Non-auth request successful: {}", result.output);
|
|
}
|
|
Err(e) => {
|
|
error!("Non-auth request failed: {}", e);
|
|
}
|
|
}
|
|
|
|
client3.disconnect().await;
|
|
}
|
|
Err(e) => {
|
|
error!("Non-auth connection failed: {}", e);
|
|
}
|
|
}
|
|
|
|
info!("Simplified authentication example completed");
|
|
Ok(())
|
|
}
|