4 releases
Uses new Rust 2024
new 0.0.4 | May 7, 2025 |
---|---|
0.0.3 | May 7, 2025 |
0.0.2 | May 7, 2025 |
0.0.1 | May 6, 2025 |
#1 in #regtest
40 downloads per month
17KB
251 lines
spark-config
A lightweight crate to manage Spark configuration in Rust applications.
Overview
The spark-config
crate provides a structured way to load, validate, and access configuration settings for Spark applications. It handles Bitcoin network settings, operator configurations, service provider connections, and integrations with Mempool, Electrs, and LRC20 Node services.
Features
- Load configuration from TOML files
- Support for multiple Bitcoin networks (Mainnet, Testnet, Signet, Regtest)
- Configuration for Spark operators and service providers
- Integration with Mempool, Electrs, and LRC20 Node services
- Automatic validation of configuration values
- Flexible path resolution (absolute paths, HOME-relative paths)
Usage
Add spark-config
to your Cargo.toml
:
[dependencies]
# Option 1: Using local path
spark-config = { path = "crates/generic/spark-config" }
# Option 2: Using version and path
spark-config = { version = "0.0.1", path = "crates/generic/spark-config" }
# Option 3: From repository (when published)
spark-config = "0.0.1"
Loading Configuration
use spark_config::SparkConfig;
// Load config from a TOML file
let config = SparkConfig::<String>::from_toml_file("/path/to/config.toml");
// Or using a relative path (will be resolved relative to HOME)
let config = SparkConfig::<String>::from_toml_file("config/config.toml");
Note: Configuration loading uses
expect()
and will panic if:
- The configuration file cannot be found or opened
- The file cannot be read
- The TOML syntax is invalid
- Required fields are missing or have invalid values
This is by design, as configuration is considered critical for application startup. If you have any use case that requires more graceful error handling, please open an issue.
Accessing Configuration Values
// Get Bitcoin network
let network = config.bitcoin_network();
// Get Mempool configuration
if let Some(mempool_url) = config.mempool_base_url() {
println!("Mempool URL: {}", mempool_url);
}
// Get authentication details
if let Some((username, password)) = config.mempool_auth() {
// Use credentials...
}
// Access service provider endpoint
let ssp_key = "lightspark";
if let Some(endpoint) = config.ssp_endpoint(&ssp_key.to_string()) {
println!("SSP endpoint: {}", endpoint);
}
// Get coordinator operator
if let Some(coordinator) = config.coordinator_operator() {
println!("Coordinator base URL: {}", coordinator.base_url());
}
Configuration File Format
The configuration is stored in TOML format. Here's an example structure:
# Bitcoin network (Mainnet, Testnet, Signet, Regtest)
bitcoin_network = "Regtest"
# Mempool configuration
[mempool]
base_url = "https://regtest-mempool.example.com"
username = "mempool-username"
password = "mempool-password"
# Electrs configuration
[electrs]
base_url = "https://regtest-mempool.example.com/api"
username = "electrs-username"
password = "electrs-password"
# LRC20 Node configuration
[lrc20_node]
base_url = "https://lrc20node.example.com/api"
# Service Provider configurations
[ssp_pool.lightspark]
base_url = "https://api.example.com"
schema_endpoint = "graphql"
identity_public_key = "02000000000000000000000000000000000000000000000000000000000000abcd"
running_authority = "Lightspark"
# Operator configurations
[[operators]]
id = 1
base_url = "https://0.spark.lightspark.com"
identity_public_key = "02000000000000000000000000000000000000000000000000000000000000abcd"
frost_identifier = "01000000000000000000000000000000000000000000000000000000000000abcd"
running_authority = "Lightspark"
is_coordinator = true
Environment Variables
When running tests, you can specify a custom configuration path using the SPARK_CONFIG_PATH
environment variable:
SPARK_CONFIG_PATH=/path/to/test/config.toml cargo test
License
Licensed under either of:
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
Dependencies
~8MB
~157K SLoC