Skip to main content

open_payments/client/
config.rs

1use serde::{Deserialize, Serialize};
2use std::path::PathBuf;
3
4///
5/// Configuration for an authenticated Open Payments client.
6///
7/// This struct contains all the necessary configuration for creating an authenticated
8/// client that can sign HTTP requests. It includes paths to cryptographic keys and
9/// identifiers used in the signing process.
10///
11/// ## Example
12///
13/// ```rust
14/// use open_payments::client::ClientConfig;
15/// use std::path::PathBuf;
16///
17/// let config = ClientConfig {
18///     key_id: "my-key-2024".to_string(),
19///     private_key_path: PathBuf::from("keys/private.pem"),
20///     jwks_path: Some(PathBuf::from("keys/jwks.json")),
21///     wallet_address_url: "https://rafiki.money/alice".into(),
22/// };
23/// ```
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct ClientConfig {
26    pub key_id: String,
27
28    /// Path to the private key file used for signing HTTP requests.
29    ///
30    /// The private key should be in PEM format (either in plain text or base64 encoded) and compatible with Ed25519 signing.
31    /// If the file doesn't exist, a new key will be generated automatically.
32    pub private_key_path: PathBuf,
33
34    /// Optional path where the JSON Web Key Set (JWKS) should be saved.
35    ///
36    /// If provided, the client will automatically generate a JWKS containing the
37    /// public key corresponding to the private key and save it to this location.
38    ///
39    /// ## Usage
40    ///
41    /// - Set to `Some(path)` to enable automatic JWKS generation
42    /// - Set to `None` to disable JWKS generation
43    /// - The JWKS file will be created automatically when the client is initialized
44    ///
45    /// Example: `Some(PathBuf::from("keys/jwks.json"))`
46    pub jwks_path: Option<PathBuf>,
47
48    /// URL of the wallet address to use for the client.
49    ///
50    /// This is the URL of the wallet address that will be used to send and receive payments.
51    pub wallet_address_url: String,
52}
53
54impl Default for ClientConfig {
55    /// Creates a default configuration with reasonable defaults.
56    ///
57    /// The default configuration uses:
58    /// - Empty key ID
59    /// - `private.key` as the private key path
60    /// - `jwks.json` as the JWKS path
61    ///
62    /// **Note**: You should typically override the `key_id` with a meaningful value
63    /// and consider using more secure paths for production environments.
64    ///
65    /// ## Example
66    ///
67    /// ```rust
68    /// use open_payments::client::ClientConfig;
69    ///
70    /// let mut config = ClientConfig::default();
71    /// config.key_id = "my-key".to_string();
72    /// ```
73    fn default() -> Self {
74        Self {
75            key_id: "".into(),
76            private_key_path: PathBuf::from("private.key"),
77            jwks_path: None,
78            wallet_address_url: "".into(),
79        }
80    }
81}