4 releases
Uses new Rust 2024
| 0.0.4 | May 7, 2025 |
|---|---|
| 0.0.3 | May 7, 2025 |
| 0.0.2 | May 7, 2025 |
| 0.0.1 | May 6, 2025 |
#3 in #electrs
80 downloads per month
19KB
274 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)
Feature Flags
The crate provides the following optional features that can be enabled in your Cargo.toml:
with-serde: Enables serialization and deserialization support usingserde. This is required for loading configuration from TOML files.bitcoin-conversion: Enables conversion betweenBitcoinNetworkandbitcoin::Networktypes from thebitcoincrate.
Example usage in your Cargo.toml:
[dependencies]
spark-config = { version = "0.0.1", features = ["with-serde", "bitcoin-conversion"] }
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;
use anyhow::Result;
// 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 returns a
Resulttype using theanyhowcrate for error handling. Errors can occur 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
You should handle these errors appropriately in your application.
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
~12–17MB
~235K SLoC