Expand description
§redb-wallet-storage
A redb storage backend for Bitcoin Development Kit (BDK).
This crate provides an efficient, pure-Rust implementation of the WalletPersister
and AsyncWalletPersister
traits from the bdk_wallet
crate using the redb embedded key-value database.
§Features
- Fast, reliable wallet data persistence using redb’s ACID-compliant storage
- Support for both synchronous and asynchronous wallet operations
- Simple, lightweight implementation with minimal dependencies
- Configurable database options
- Robust error handling
§Usage
use bdk_wallet::{CreateParams, LoadParams, PersistedWallet};
use bitcoin::Network;
use redb_wallet_storage::RedbStore;
// Example descriptors (use your own securely generated ones)
const DESCRIPTOR: &str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)";
const CHANGE_DESCRIPTOR: &str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)";
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create or open a wallet store
let mut store = RedbStore::open_or_create("wallet.redb")?;
// Try to load an existing wallet
let wallet = match PersistedWallet::load(&mut store, LoadParams::default())? {
Some(wallet) => wallet,
None => {
// Create a new wallet if one doesn't exist
let create_params = CreateParams::new(DESCRIPTOR, CHANGE_DESCRIPTOR)
.network(Network::Testnet);
PersistedWallet::create(&mut store, create_params)?
}
};
println!("Wallet loaded successfully!");
Ok(())
}
§Async Usage
use bdk_wallet::{CreateParams, LoadParams, PersistedWallet};
use bitcoin::Network;
use redb_wallet_storage::RedbStore;
// Example descriptors (use your own securely generated ones)
const DESCRIPTOR: &str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/0/*)";
const CHANGE_DESCRIPTOR: &str = "wpkh(tprv8ZgxMBicQKsPdy6LMhUtFHAgpocR8GC6QmwMSFpZs7h6Eziw3SpThFfczTDh5rW2krkqffa11UpX3XkeTTB2FvzZKWXqPY54Y6Rq4AQ5R8L/84'/1'/0'/1/*)";
async fn async_example() -> Result<(), Box<dyn std::error::Error>> {
// Create or open a wallet store
let mut store = RedbStore::open_or_create("wallet_async.redb")?;
// Try to load an existing wallet asynchronously
let wallet = match PersistedWallet::load_async(&mut store, LoadParams::default()).await? {
Some(wallet) => wallet,
None => {
// Create a new wallet if one doesn't exist
let create_params = CreateParams::new(DESCRIPTOR, CHANGE_DESCRIPTOR)
.network(Network::Testnet);
PersistedWallet::create_async(&mut store, create_params).await?
}
};
println!("Wallet loaded successfully!");
Ok(())
}
§Database Configuration
The RedbStore
provides methods for fine-tuning database settings:
use redb_wallet_storage::RedbStore;
fn custom_config_example() -> Result<(), Box<dyn std::error::Error>> {
// Create a custom database configuration
let mut config = redb::Builder::new();
config.set_cache_size(1024 * 1024 * 10); // 10 MB cache
// Create a store with custom configuration
let store = RedbStore::create_with_config("custom_wallet.redb", &mut config)?;
Ok(())
}
§Error Handling
The crate provides a comprehensive RedbError
type that wraps all potential errors:
use redb_wallet_storage::{RedbStore, RedbError};
fn error_handling_example() {
match RedbStore::open("nonexistent.redb") {
Ok(store) => {
println!("Store opened successfully");
},
Err(RedbError::Database(e)) => {
println!("Database error: {}", e);
},
Err(RedbError::Io(e)) => {
println!("I/O error: {}", e);
},
Err(e) => {
println!("Other error: {}", e);
}
}
}
Structs§
- Redb
Store - Persists a wallet changeset in a redb database.
Enums§
- Redb
Error - Error type for redb storage operations
This enum represents all possible errors that can occur when using the
RedbStore
. It wraps errors from the underlying redb database, serialization/deserialization errors, and I/O errors.