spark_rust/rpc/mod.rs
1//! # RPC client implementation for connecting to Spark Operators
2//!
3//! This module provides the RPC client implementation used to establish secure connections
4//! to Spark Operators. It serves as the communication layer between the Spark Wallet SDK
5//! and Spark Operators.
6//!
7//! ## Features
8//!
9//! The RPC module handles:
10//! - Connection management and pooling
11//! - Service client creation for different Spark services
12//! - TLS configuration with proper certificate verification
13//! - Secure channel establishment to Spark operators
14//!
15//! ## Architecture
16//!
17//! The module is organized into:
18//! - [`SparkRpcClient`] - The main client wrapper that manages connections
19//! - `connections` - Implementation details for establishing secure connections
20//! - `traits` - Interface definitions for RPC connection behavior
21//!
22//! ## Usage
23//!
24//! The RPC clients are used internally by the SDK to communicate with Spark Operators.
25//! End users typically don't need to interact with these types directly.
26
27use connections::connection::SparkConnection;
28use spark_protos::{
29 authn::spark_authn_service_client::SparkAuthnServiceClient,
30 spark::spark_service_client::SparkServiceClient,
31};
32use tonic::transport::Channel;
33
34use crate::error::SparkSdkError;
35
36/// Enum representing different types of Spark RPC clients.
37///
38/// This enum wraps the different connection implementations that can be used
39/// to communicate with Spark Operators. Currently, there is one implementation:
40/// - `DefaultConnection`: TLS connection with Amazon root CA certificate verification
41#[derive(Clone)]
42pub(crate) enum SparkRpcClient {
43 /// Default connection type using TLS with Amazon root CA certificate verification
44 DefaultConnection(SparkConnection),
45}
46
47impl SparkRpcClient {
48 /// Creates a new Spark service client using the underlying connection.
49 ///
50 /// This method returns a client that can be used to make RPC calls to the
51 /// Spark service, which provides core wallet functionality like transfers,
52 /// deposits, and Lightning operations.
53 ///
54 /// # Returns
55 /// - `Ok(SparkServiceClient)` if client creation succeeds
56 /// - `Err(SparkSdkError)` if there was an error creating the client
57 pub(crate) fn get_new_spark_service_connection(
58 &self,
59 ) -> Result<SparkServiceClient<Channel>, SparkSdkError> {
60 match self {
61 SparkRpcClient::DefaultConnection(client) => {
62 Ok(SparkServiceClient::new(client.channel.clone()))
63 }
64 }
65 }
66
67 /// Creates a new Spark authentication service client using the underlying connection.
68 ///
69 /// This method returns a client that can be used to make RPC calls to the
70 /// Spark authentication service, which handles user identity verification
71 /// and authorization with Spark operators.
72 ///
73 /// # Returns
74 /// - `Ok(SparkAuthnServiceClient)` if client creation succeeds
75 /// - `Err(SparkSdkError)` if there was an error creating the client
76 pub(crate) fn get_new_spark_authn_service_connection(
77 &self,
78 ) -> Result<SparkAuthnServiceClient<Channel>, SparkSdkError> {
79 match self {
80 SparkRpcClient::DefaultConnection(client) => {
81 Ok(SparkAuthnServiceClient::new(client.channel.clone()))
82 }
83 }
84 }
85}
86
87pub(crate) mod connections;
88pub(crate) mod traits;