Spark Wallet SDK
A Rust-based Bitcoin wallet SDK for the Spark protocol that provides advanced cryptographic operations, transaction management, and Lightning Network capabilities.
Overview
Spark Wallet SDK is a comprehensive toolkit designed to manage Bitcoin transactions through a structured "tree" of UTXOs. It provides a safe, reliable interface for interacting with Bitcoin and Lightning Network functionality, with advanced features like threshold signatures.
Key Features
- Deposit Address Generation - Create secure Bitcoin addresses for receiving funds
- UTXO Tree Management - Organize UTXOs in a hierarchical tree structure for efficient management
- Cooperative Signing - Coordinate transaction signing between multiple parties
- Threshold Signatures - Support for FROST-based threshold Schnorr signatures
- Lightning Network Integration - Invoice creation and payment capabilities
- Robust Error Handling - Comprehensive error types and recovery mechanisms
- Configurable Backend - Connect to mainnet or regtest networks
Installation
Add Spark Wallet SDK to your Cargo.toml
:
[]
= { = "https://github.com/polarity/spark-rs.git", = ["self-signing"] }
Architecture
The SDK is structured around a central SparkSdk
struct that provides access to all functionality through a set of handlers:
spark-sdk/
├── src/
│ ├── wallet/
│ │ ├── handlers/ # Public API for wallet operations
│ │ ├── internal_handlers/ # Lower-level implementation details
│ │ ├── leaf_manager/ # UTXO tree management
│ │ └── client.rs # Main SparkSdk implementation
│ ├── signer/ # Cryptographic signing operations
│ ├── rpc/ # Communication with Spark services
│ └── error/ # Error handling
└── examples/ # Usage examples
Usage
Initializing the SDK
use ;
use OsRng;
async
Generating a Deposit Address
async
Creating a UTXO Tree
use Transaction;
async
Transferring Funds
async
Creating a Lightning Invoice
async
Feature Flags
The SDK supports several feature flags to enable different functionality:
self-signing
(default): Enable local key generation and storageintegration-tests
(default): Enable concurrency and advanced integration testslocal-operator-tests
: Run tests with local Spark operatorslightning
: Enable Lightning Network functionality
Example with specific features:
[]
= { = "https://github.com/polarity/spark-rs.git", = ["self-signing", "lightning"] }
Handler Modules
The SDK is organized into handler modules, each focusing on specific functionality:
Handler | Purpose | Example Methods |
---|---|---|
init |
SDK initialization | new() , new_with_default_config() |
deposit |
Fund reception | generate_deposit_address() |
create_tree |
UTXO structuring | create_tree() |
leaves |
UTXO management | get_available_leaves() |
transfer |
Fund movement | transfer() , claim_transfer() |
lightning |
LN operations | create_lightning_invoice() , pay_invoice() |
split |
UTXO division | split_leaf() |
swap |
Atomic exchanges | initiate_swap() |
Error Handling
The SDK provides a comprehensive error type through SparkSdkError
that covers:
- Connection issues
- Authentication failures
- Transaction validation errors
- Signing problems
- Local storage issues
Example error handling:
match sdk.generate_deposit_address.await
Advanced Usage
Custom Signers
You can implement the SparkSigner
trait to provide your own signing mechanism:
use SparkSigner;
// Then use it with the SDK
let sdk = new.await?;
Working with FROST Threshold Signatures
// Generate threshold signing commitments (for a t-of-n scheme)
let commitments = sdk.new_frost_signing_commitments.await?;
// Distribute commitments to participants
// ...
// Later, receive signature shares and aggregate them
let signature = sdk.aggregate_frost_signature_shares.await?;
Security Considerations
- Seed Management: Properly secure the master seed used to initialize the signer
- Network Traffic: All API calls are encrypted, but be aware of the network you're connected to
- Key Rotation: Consider implementing key rotation for long-lived applications
- Backup: Implement backup functionality for critical wallet data
For Contributors
See the CONTRIBUTING.md file for guidelines on contributing to this project.
Development Setup
# Clone the repository
# Build the SDK
# Run tests
Running the Examples
# Run the new wallet example
# Run the deposit example
License
This project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.