Skip to content

Commit bcfa000

Browse files
committed
feat: add bdk_sqlite_store crate implementing PersistBackend backed by a SQLite database
1 parent c20a4da commit bcfa000

File tree

6 files changed

+412
-0
lines changed

6 files changed

+412
-0
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ members = [
33
"crates/bdk",
44
"crates/chain",
55
"crates/file_store",
6+
"crates/sqlite_store",
67
"crates/electrum",
78
"crates/esplora",
89
"example-crates/example_cli",

crates/sqlite_store/Cargo.toml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "bdk_sqlite_store"
3+
version = "0.2.0"
4+
edition = "2021"
5+
license = "MIT OR Apache-2.0"
6+
repository = "https://github.com/bitcoindevkit/bdk"
7+
documentation = "https://docs.rs/bdk_file_store"
8+
description = "A simple append-only SQLite based implementation of Persist for Bitcoin Dev Kit."
9+
keywords = ["bitcoin", "persist", "persistence", "bdk", "sqlite"]
10+
authors = ["Bitcoin Dev Kit Developers"]
11+
readme = "README.md"
12+
13+
[dependencies]
14+
bdk_chain = { path = "../chain", version = "0.5.0", features = [ "serde", "miniscript" ] }
15+
rusqlite = { version = "0.29.0", features = ["bundled"]}
16+
serde = { version = "1", features = ["derive"] }
17+
log = { version = "0.4", features = [] }
18+
serde_json = "1.0.107"
19+
20+
[dev-dependencies]
21+
tempfile = "3"

crates/sqlite_store/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# BDK SQLite Store
2+
3+
This is a simple append-only [SQLite] database backed implementation of
4+
[`Persist`](`bdk_chain::Persist`).
5+
6+
The main structure is [`Store`](`crate::Store`), which can be used with [`bdk`]'s
7+
`Wallet` to persist wallet data into a SQLite database file.
8+
9+
[`bdk`]: https://docs.rs/bdk/latest
10+
[`bdk_chain`]: https://docs.rs/bdk_chain/latest
11+
[SQLite]: https://www.sqlite.org/index.html

crates/sqlite_store/src/lib.rs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#![doc = include_str!("../README.md")]
2+
3+
mod store;
4+
5+
pub use store::*;
6+
7+
/// Error that occurs while appending new change sets to the DB.
8+
#[derive(Debug)]
9+
pub enum AppendError {
10+
/// JSON encoding error.
11+
Json(serde_json::Error),
12+
/// SQLite error.
13+
Sqlite(rusqlite::Error),
14+
}
15+
16+
impl<'a> core::fmt::Display for AppendError {
17+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
18+
match self {
19+
Self::Json(e) => write!(f, "json error trying to append change set: {}", e),
20+
Self::Sqlite(e) => write!(f, "sqlite error trying to append change set: {}", e),
21+
}
22+
}
23+
}
24+
25+
impl<'a> From<serde_json::Error> for AppendError {
26+
fn from(error: serde_json::Error) -> Self {
27+
Self::Json(error)
28+
}
29+
}
30+
31+
impl<'a> std::error::Error for AppendError {}
32+
33+
/// Error the occurs while iterating stored change sets from the DB.
34+
#[derive(Debug)]
35+
pub enum IterError {
36+
/// Json decoding
37+
Json {
38+
rowid: usize,
39+
err: serde_json::Error,
40+
},
41+
/// Sqlite error
42+
Sqlite(rusqlite::Error),
43+
/// FromSql error
44+
FromSql(rusqlite::types::FromSqlError),
45+
}
46+
47+
impl core::fmt::Display for IterError {
48+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
49+
match self {
50+
IterError::Json { rowid, err } => write!(
51+
f,
52+
"json error trying to decode change set {}, {}",
53+
rowid, err
54+
),
55+
IterError::Sqlite(err) => write!(f, "Sqlite error {}", err),
56+
IterError::FromSql(err) => write!(f, "FromSql error {}", err),
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)