Skip to content

Commit

Permalink
feat: add sqlite module and test, update README
Browse files Browse the repository at this point in the history
  • Loading branch information
notmandatory committed Oct 3, 2024
1 parent aac23fa commit 18dd296
Show file tree
Hide file tree
Showing 8 changed files with 712 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/target
Cargo.lock

*.sqlite
.env
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ name = "async_wallet_bdk_sqlx"
path = "src/main.rs"

[dependencies]
sqlx = { version = "0.8.1", default-features = false, features = ["runtime-tokio", "tls-rustls-ring","derive", "postgres", "json", "chrono", "uuid", "sqlx-macros", "migrate"] }
sqlx = { version = "0.8.1", default-features = false, features = ["runtime-tokio", "tls-rustls-ring","derive", "postgres", "sqlite", "json", "chrono", "uuid", "sqlx-macros", "migrate"] }
thiserror = "1"
tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] }
serde = { version = "1.0.208", features = ["derive"] }
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
# bdk-sqlx


## Testing

1. Install postgresql with `psql` tool. For example (macos):
```
brew update
brew install postgresql
```
2. Create empty test database:
```
psql postgres
postgres=# create database test_bdk_wallet;
```
3. Set DATABASE_URL to test database:
```
export DATABASE_TEST_URL=postgresql://localhost/test_bdk_wallet
```
4. Run tests:
```
cargo test
```
73 changes: 73 additions & 0 deletions migrations/sqlite/01_bdk_wallet.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
-- Schema version control
CREATE TABLE IF NOT EXISTS version (
version INTEGER PRIMARY KEY
);

-- Network is the valid network for all other table data
CREATE TABLE IF NOT EXISTS network (
wallet_name TEXT PRIMARY KEY,
name TEXT NOT NULL
);

-- Keychain is the json serialized keychain structure as JSONB,
-- descriptor is the complete descriptor string,
-- descriptor_id is a sha256::Hash id of the descriptor string w/o the checksum,
-- last revealed index is a u32
CREATE TABLE IF NOT EXISTS keychain (
wallet_name TEXT NOT NULL,
keychainkind TEXT NOT NULL,
descriptor TEXT NOT NULL,
descriptor_id BLOB NOT NULL,
last_revealed INTEGER DEFAULT 0,
PRIMARY KEY (wallet_name, keychainkind)

);

-- Hash is block hash hex string,
-- Block height is a u32
CREATE TABLE IF NOT EXISTS block (
wallet_name TEXT,
hash TEXT NOT NULL,
height INTEGER NOT NULL,
PRIMARY KEY (wallet_name, hash)
);
CREATE INDEX idx_block_height ON block (height);

-- Txid is transaction hash hex string (reversed)
-- Whole_tx is a consensus encoded transaction,
-- Last seen is a u64 unix epoch seconds
CREATE TABLE IF NOT EXISTS tx (
wallet_name TEXT,
txid TEXT NOT NULL,
whole_tx BLOB,
last_seen INTEGER,
PRIMARY KEY (wallet_name, txid)
);

-- Outpoint txid hash hex string (reversed)
-- Outpoint vout
-- TxOut value as SATs
-- TxOut script consensus encoded
CREATE TABLE IF NOT EXISTS txout (
wallet_name TEXT,
txid TEXT NOT NULL,
vout INTEGER NOT NULL,
value INTEGER NOT NULL,
script BLOB NOT NULL,
PRIMARY KEY (wallet_name, txid, vout)
);

-- Join table between anchor and tx
-- Block hash hex string
-- Anchor is a json serialized Anchor structure as JSONB,
-- Txid is transaction hash hex string (reversed)
CREATE TABLE IF NOT EXISTS anchor_tx (
wallet_name TEXT,
block_hash TEXT NOT NULL,
anchor BLOB NOT NULL,
txid TEXT NOT NULL,
PRIMARY KEY (wallet_name, block_hash, txid),
FOREIGN KEY (wallet_name, block_hash) REFERENCES block(wallet_name, hash),
FOREIGN KEY (wallet_name, txid) REFERENCES tx(wallet_name, txid)
);
CREATE INDEX idx_anchor_tx_txid ON anchor_tx (txid);
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![warn(missing_docs)]

mod postgres;
mod sqlite;

#[cfg(test)]
mod test;
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ async fn main() -> anyhow::Result<()> {
let wallet_name =
bdk_wallet::wallet_name_from_descriptor(VAULT_DESC, Some(CHANGE_DESC), NETWORK, &secp)?;

let mut store = bdk_sqlx::Store::new_with_url(url.clone(), Some(wallet_name)).await?;
let mut store =
bdk_sqlx::Store::<Postgres>::new_with_url(url.clone(), Some(wallet_name)).await?;

let mut wallet = match Wallet::load().load_wallet_async(&mut store).await? {
Some(wallet) => wallet,
Expand Down
Loading

0 comments on commit 18dd296

Please sign in to comment.