1mod convert;
12mod migrations;
13mod query;
14
15
16use std::path::{Path, PathBuf};
17
18use anyhow::Context;
19use bitcoin::Txid;
20use bitcoin::secp256k1::PublicKey;
21use chrono::DateTime;
22use lightning_invoice::Bolt11Invoice;
23use log::debug;
24use rusqlite::Connection;
25
26use ark::{Vtxo, VtxoId};
27use ark::lightning::{PaymentHash, Preimage};
28use ark::vtxo::Full;
29use bitcoin_ext::BlockDelta;
30
31use crate::WalletProperties;
32use crate::actions::{WalletActionCheckpoint, WalletActionId};
33use crate::exit::ExitTxOrigin;
34use crate::movement::{Movement, MovementId, MovementStatus, MovementSubsystem, PaymentMethod};
35use crate::persist::{BarkPersister, RoundStateId, StoredRoundState, Unlocked};
36use crate::persist::models::{
37 LightningReceive, PaidInvoice, PendingBoard, PendingOffboard, StoredExit,
38};
39use crate::round::RoundState;
40use crate::vtxo::{VtxoState, VtxoStateKind, WalletVtxo};
41
42#[derive(Clone)]
45pub struct SqliteClient {
46 connection_string: PathBuf,
47}
48
49impl SqliteClient {
50 pub fn open(db_file: impl AsRef<Path>) -> anyhow::Result<SqliteClient> {
52 let path = db_file.as_ref().to_path_buf();
53
54 debug!("Opening database at {}", path.display());
55 let mut conn = rusqlite::Connection::open(&path)
56 .with_context(|| format!("Error connecting to database {}", path.display()))?;
57
58 let migrations = migrations::MigrationContext::new();
59 migrations.do_all_migrations(&mut conn)?;
60
61 Ok( Self { connection_string: path })
62 }
63
64 fn connect(&self) -> anyhow::Result<Connection> {
65 rusqlite::Connection::open(&self.connection_string)
66 .with_context(|| format!("Error connecting to database {}", self.connection_string.display()))
67 }
68}
69
70#[async_trait]
71impl BarkPersister for SqliteClient {
72 async fn init_wallet(&self, properties: &WalletProperties) -> anyhow::Result<()> {
73 let mut conn = self.connect()?;
74 let tx = conn.transaction()?;
75
76 query::set_properties(&tx, properties)?;
77
78 tx.commit()?;
79 Ok(())
80 }
81
82 #[cfg(feature = "onchain-bdk")]
83 async fn initialize_bdk_wallet(&self) -> anyhow::Result<bdk_wallet::ChangeSet> {
84 let mut conn = self.connect()?;
85 Ok(bdk_wallet::WalletPersister::initialize(&mut conn)?)
86 }
87
88 #[cfg(feature = "onchain-bdk")]
89 async fn store_bdk_wallet_changeset(&self, changeset: &bdk_wallet::ChangeSet) -> anyhow::Result<()> {
90 let mut conn = self.connect()?;
91 bdk_wallet::WalletPersister::persist(&mut conn, changeset)?;
92 Ok(())
93 }
94
95 async fn read_properties(&self) -> anyhow::Result<Option<WalletProperties>> {
96 let conn = self.connect()?;
97 Ok(query::fetch_properties(&conn)?)
98 }
99
100 async fn set_server_pubkey(&self, server_pubkey: PublicKey) -> anyhow::Result<()> {
101 let conn = self.connect()?;
102 query::set_server_pubkey(&conn, &server_pubkey)?;
103 Ok(())
104 }
105
106 async fn set_server_mailbox_pubkey(&self, server_mailbox_pubkey: PublicKey) -> anyhow::Result<()> {
107 let conn = self.connect()?;
108 query::set_server_mailbox_pubkey(&conn, &server_mailbox_pubkey)?;
109 Ok(())
110 }
111
112 async fn create_new_movement(&self,
113 status: MovementStatus,
114 subsystem: &MovementSubsystem,
115 time: DateTime<chrono::Local>,
116 ) -> anyhow::Result<MovementId> {
117 let mut conn = self.connect()?;
118 let tx = conn.transaction()?;
119 let movement_id = query::create_new_movement(&tx, status, subsystem, time)?;
120 tx.commit()?;
121 Ok(movement_id)
122 }
123
124 async fn update_movement(&self, movement: &Movement) -> anyhow::Result<()> {
125 let mut conn = self.connect()?;
126 let tx = conn.transaction()?;
127 query::update_movement(&tx, movement)?;
128 tx.commit()?;
129 Ok(())
130 }
131
132 async fn get_movement_by_id(&self, movement_id: MovementId) -> anyhow::Result<Movement> {
133 let conn = self.connect()?;
134 query::get_movement_by_id(&conn, movement_id)
135 }
136
137 async fn get_all_movements(&self) -> anyhow::Result<Vec<Movement>> {
138 let conn = self.connect()?;
139 query::get_all_movements(&conn)
140 }
141
142 async fn get_movements_by_payment_method(
143 &self,
144 payment_method: &PaymentMethod,
145 ) -> anyhow::Result<Vec<Movement>> {
146 let conn = self.connect()?;
147 query::get_movements_by_payment_method(&conn, payment_method)
148 }
149
150 async fn store_pending_board(
151 &self,
152 vtxo: &Vtxo<Full>,
153 funding_tx: &bitcoin::Transaction,
154 movement_id: MovementId,
155 ) -> anyhow::Result<()> {
156 let mut conn = self.connect()?;
157 let tx = conn.transaction()?;
158 query::store_new_pending_board(&tx, vtxo, funding_tx, movement_id)?;
159 tx.commit()?;
160 Ok(())
161 }
162
163 async fn remove_pending_board(&self, vtxo_id: &VtxoId) -> anyhow::Result<()> {
164 let mut conn = self.connect()?;
165 let tx = conn.transaction()?;
166 query::remove_pending_board(&tx, vtxo_id)?;
167 tx.commit()?;
168 Ok(())
169 }
170
171 async fn get_all_pending_board_ids(&self) -> anyhow::Result<Vec<VtxoId>> {
172 let conn = self.connect()?;
173 query::get_all_pending_boards_ids(&conn)
174 }
175
176 async fn get_pending_board_by_vtxo_id(&self, vtxo_id: VtxoId) -> anyhow::Result<Option<PendingBoard>> {
177 let conn = self.connect()?;
178 query::get_pending_board_by_vtxo_id(&conn, vtxo_id)
179 }
180
181 async fn store_round_state_lock_vtxos(&self, round_state: &RoundState) -> anyhow::Result<RoundStateId> {
182 let mut conn = self.connect()?;
183 let tx = conn.transaction()?;
184 for vtxo in round_state.participation().inputs.iter() {
185 query::update_vtxo_state_checked(
186 &*tx,
187 vtxo.id(),
188 VtxoState::Locked {
189 holder: round_state.movement_id
190 .map(|id| crate::vtxo::VtxoLockHolder::Movement { id }),
191 },
192 &[VtxoStateKind::Spendable],
193 )?;
194 }
195 let id = query::store_round_state(&tx, round_state)?;
196 tx.commit()?;
197 Ok(id)
198 }
199
200 async fn update_round_state(&self, state: &StoredRoundState) -> anyhow::Result<()> {
201 let conn = self.connect()?;
202 query::update_round_state(&conn, state)?;
203 Ok(())
204 }
205
206 async fn remove_round_state(&self, round_state: &StoredRoundState) -> anyhow::Result<()> {
207 let conn = self.connect()?;
208 query::remove_round_state(&conn, round_state.id())?;
209 Ok(())
210 }
211
212 async fn get_round_state_by_id(&self, id: RoundStateId) -> anyhow::Result<Option<StoredRoundState<Unlocked>>> {
213 let conn = self.connect()?;
214 query::get_round_state_by_id(&conn, id)
215 }
216
217 async fn get_pending_round_state_ids(&self) -> anyhow::Result<Vec<RoundStateId>> {
218 let conn = self.connect()?;
219 query::get_pending_round_state_ids(&conn)
220 }
221
222 async fn store_vtxos(
223 &self,
224 vtxos: &[(&Vtxo<Full>, &VtxoState)],
225 ) -> anyhow::Result<()> {
226 let mut conn = self.connect()?;
227 let tx = conn.transaction()?;
228
229 for (vtxo, state) in vtxos {
230 query::store_vtxo_with_initial_state(&tx, vtxo, state)?;
231 }
232 tx.commit()?;
233 Ok(())
234 }
235
236 async fn get_wallet_vtxo(&self, id: VtxoId) -> anyhow::Result<Option<WalletVtxo>> {
237 let conn = self.connect()?;
238 query::get_wallet_vtxo_by_id(&conn, id)
239 }
240
241 async fn get_all_vtxos(&self) -> anyhow::Result<Vec<WalletVtxo>> {
242 let conn = self.connect()?;
243 query::get_all_vtxos(&conn)
244 }
245
246 async fn get_vtxos_by_state(&self, state: &[VtxoStateKind]) -> anyhow::Result<Vec<WalletVtxo>> {
248 let conn = self.connect()?;
249 query::get_vtxos_by_state(&conn, state)
250 }
251
252 async fn has_spent_vtxo(&self, id: VtxoId) -> anyhow::Result<bool> {
253 let conn = self.connect()?;
254 let state : Option<VtxoState> = query::get_vtxo_state(&conn, id)?;
255 let result = state.map(|s| s == VtxoState::Spent).unwrap_or(false);
256 Ok(result)
257 }
258
259 async fn get_full_vtxo(&self, id: VtxoId) -> anyhow::Result<Option<Vtxo<Full>>> {
260 let conn = self.connect()?;
261 query::get_full_vtxo_by_id(&conn, id)
262 }
263
264 async fn get_full_vtxos(&self, ids: &[VtxoId]) -> anyhow::Result<Vec<Vtxo<Full>>> {
265 let conn = self.connect()?;
266 query::get_full_vtxos_by_ids(&conn, ids)
267 }
268
269 async fn remove_vtxo(&self, id: VtxoId) -> anyhow::Result<Option<Vtxo<Full>>> {
270 let mut conn = self.connect()?;
271 let tx = conn.transaction().context("Failed to start transaction")?;
272 let result = query::delete_vtxo(&tx, id);
273 tx.commit().context("Failed to commit transaction")?;
274 result
275 }
276
277 async fn store_vtxo_key(&self, index: u32, public_key: PublicKey) -> anyhow::Result<()> {
278 let conn = self.connect()?;
279 query::store_vtxo_key(&conn, index, public_key)
280 }
281
282 async fn get_last_vtxo_key_index(&self) -> anyhow::Result<Option<u32>> {
283 let conn = self.connect()?;
284 query::get_last_vtxo_key_index(&conn)
285 }
286
287 async fn get_public_key_idx(&self, public_key: &PublicKey) -> anyhow::Result<Option<u32>> {
288 let conn = self.connect()?;
289 query::get_public_key_idx(&conn, public_key)
290 }
291
292 async fn get_mailbox_checkpoint(&self) -> anyhow::Result<u64> {
293 let conn = self.connect()?;
294 query::get_mailbox_checkpoint(&conn)
295 }
296
297 async fn store_mailbox_checkpoint(&self, checkpoint: u64) -> anyhow::Result<()> {
298 let conn = self.connect()?;
299 query::store_mailbox_checkpoint(&conn, checkpoint)?;
300 Ok(())
301 }
302
303 async fn store_lightning_receive(
305 &self,
306 payment_hash: PaymentHash,
307 preimage: Preimage,
308 invoice: &Bolt11Invoice,
309 htlc_recv_cltv_delta: BlockDelta,
310 ) -> anyhow::Result<()> {
311 let conn = self.connect()?;
312 query::store_lightning_receive(
313 &conn, payment_hash, preimage, invoice, htlc_recv_cltv_delta,
314 )?;
315 Ok(())
316 }
317
318 async fn upsert_wallet_action_checkpoint(
319 &self,
320 id: &WalletActionId,
321 checkpoint: &WalletActionCheckpoint,
322 ) -> anyhow::Result<()> {
323 let conn = self.connect()?;
324 query::upsert_wallet_action_checkpoint(&conn, id, checkpoint)
325 }
326
327 async fn get_wallet_action_checkpoint(
328 &self,
329 id: &WalletActionId,
330 ) -> anyhow::Result<Option<WalletActionCheckpoint>> {
331 let conn = self.connect()?;
332 query::get_wallet_action_checkpoint(&conn, id)
333 }
334
335 async fn get_all_wallet_action_checkpoints(
336 &self,
337 ) -> anyhow::Result<Vec<WalletActionCheckpoint>> {
338 let conn = self.connect()?;
339 query::get_all_wallet_action_checkpoints(&conn)
340 }
341
342 async fn remove_wallet_action_checkpoint(
343 &self,
344 id: &WalletActionId,
345 ) -> anyhow::Result<()> {
346 let conn = self.connect()?;
347 query::remove_wallet_action_checkpoint(&conn, id)
348 }
349
350 async fn record_paid_invoice(
351 &self,
352 payment_hash: PaymentHash,
353 preimage: Preimage,
354 ) -> anyhow::Result<()> {
355 let conn = self.connect()?;
356 query::record_paid_invoice(&conn, payment_hash, preimage)
357 }
358
359 async fn get_paid_invoice(
360 &self,
361 payment_hash: PaymentHash,
362 ) -> anyhow::Result<Option<PaidInvoice>> {
363 let conn = self.connect()?;
364 query::get_paid_invoice(&conn, payment_hash)
365 }
366
367 async fn get_all_pending_lightning_receives(&self) -> anyhow::Result<Vec<LightningReceive>> {
368 let conn = self.connect()?;
369 query::get_all_pending_lightning_receives(&conn)
370 }
371
372 async fn set_preimage_revealed(&self, payment_hash: PaymentHash) -> anyhow::Result<()> {
373 let conn = self.connect()?;
374 query::set_preimage_revealed(&conn, payment_hash)?;
375 Ok(())
376 }
377
378 async fn update_lightning_receive(
379 &self,
380 payment_hash: PaymentHash,
381 htlc_vtxo_ids: &[VtxoId],
382 movement_id: MovementId,
383 ) -> anyhow::Result<()> {
384 let conn = self.connect()?;
385 query::update_lightning_receive(&conn, payment_hash, htlc_vtxo_ids, movement_id)?;
386 Ok(())
387 }
388
389 async fn fetch_lightning_receive_by_payment_hash(
391 &self,
392 payment_hash: PaymentHash,
393 ) -> anyhow::Result<Option<LightningReceive>> {
394 let conn = self.connect()?;
395 query::fetch_lightning_receive_by_payment_hash(&conn, payment_hash)
396 }
397
398 async fn finish_pending_lightning_receive(&self, payment_hash: PaymentHash) -> anyhow::Result<()> {
399 let conn = self.connect()?;
400 query::finish_pending_lightning_receive(&conn, payment_hash)?;
401 Ok(())
402 }
403
404 async fn store_exit_vtxo_entry(&self, exit: &StoredExit) -> anyhow::Result<()> {
405 let mut conn = self.connect()?;
406 let tx = conn.transaction()?;
407 query::store_exit_vtxo_entry(&tx, exit)?;
408 tx.commit()?;
409 Ok(())
410 }
411
412 async fn remove_exit_vtxo_entry(&self, id: &VtxoId) -> anyhow::Result<()> {
413 let mut conn = self.connect()?;
414 let tx = conn.transaction()?;
415 query::remove_exit_vtxo_entry(&tx, &id)?;
416 tx.commit()?;
417 Ok(())
418 }
419
420 async fn get_exit_vtxo_entries(&self) -> anyhow::Result<Vec<StoredExit>> {
421 let conn = self.connect()?;
422 query::get_exit_vtxo_entries(&conn)
423 }
424
425 async fn store_exit_child_tx(
426 &self,
427 exit_txid: Txid,
428 child_tx: &bitcoin::Transaction,
429 origin: ExitTxOrigin,
430 ) -> anyhow::Result<()> {
431 let mut conn = self.connect()?;
432 let tx = conn.transaction()?;
433 query::store_exit_child_tx(&tx, exit_txid, child_tx, origin)?;
434 tx.commit()?;
435 Ok(())
436 }
437
438 async fn get_exit_child_tx(
439 &self,
440 exit_txid: Txid,
441 ) -> anyhow::Result<Option<(bitcoin::Transaction, ExitTxOrigin)>> {
442 let conn = self.connect()?;
443 query::get_exit_child_tx(&conn, exit_txid)
444 }
445
446 async fn update_vtxo_state_checked(
447 &self,
448 vtxo_id: VtxoId,
449 new_state: VtxoState,
450 allowed_old_states: &[VtxoStateKind]
451 ) -> anyhow::Result<WalletVtxo> {
452 let conn = self.connect()?;
453 query::update_vtxo_state_checked(&conn, vtxo_id, new_state, allowed_old_states)
454 }
455
456 async fn store_pending_offboard(
457 &self,
458 pending: &PendingOffboard,
459 ) -> anyhow::Result<()> {
460 let mut conn = self.connect()?;
461 let tx = conn.transaction()?;
462 query::store_pending_offboard(&tx, pending)?;
463 tx.commit()?;
464 Ok(())
465 }
466
467 async fn get_pending_offboards(&self) -> anyhow::Result<Vec<PendingOffboard>> {
468 let conn = self.connect()?;
469 query::get_all_pending_offboards(&conn)
470 }
471
472 async fn remove_pending_offboard(&self, movement_id: MovementId) -> anyhow::Result<()> {
473 let mut conn = self.connect()?;
474 let tx = conn.transaction()?;
475 query::remove_pending_offboard(&tx, movement_id)?;
476 tx.commit()?;
477 Ok(())
478 }
479}
480
481#[cfg(any(test, doc))]
482pub mod helpers {
483 use std::path::PathBuf;
484 use std::str::FromStr;
485
486 use rusqlite::Connection;
487
488 #[cfg(any(test, feature = "rand"))]
495 pub fn in_memory_db() -> (PathBuf, Connection) {
496 use rand::{distr, RngExt};
497
498 let mut rng = rand::rng();
504 let filename: String = (&mut rng).sample_iter(distr::Alphanumeric)
505 .take(16).map(char::from).collect();
506
507 let connection_string = format!("file:{}?mode=memory&cache=shared", filename);
508 let pathbuf = PathBuf::from_str(&connection_string).unwrap();
509
510 let conn = Connection::open(pathbuf.clone()).unwrap();
511 (pathbuf.clone(), conn)
512 }
513}
514
515#[cfg(test)]
516mod test {
517 use ark::ProtocolEncoding;
518 use ark::test_util::VTXO_VECTORS;
519
520 use crate::{persist::sqlite::helpers::in_memory_db, vtxo::VtxoState};
521
522 use super::*;
523
524 #[tokio::test]
525 async fn test_add_and_retrieve_vtxos() {
526 let vtxo_1 = &VTXO_VECTORS.board_vtxo;
527 let vtxo_2 = &VTXO_VECTORS.arkoor_htlc_out_vtxo;
528 let vtxo_3 = &VTXO_VECTORS.round2_vtxo;
529
530 let (cs, conn) = in_memory_db();
531 let db = SqliteClient::open(cs).unwrap();
532
533 db.store_vtxos(&[
534 (vtxo_1, &VtxoState::Spendable), (vtxo_2, &VtxoState::Spendable)
535 ]).await.unwrap();
536
537 let vtxo_1_db = db.get_wallet_vtxo(vtxo_1.id()).await.expect("No error").expect("A vtxo was found");
540 assert_eq!(vtxo_1_db.vtxo, vtxo_1.to_bare());
541
542 let vtxo_1_full = db.get_full_vtxo(vtxo_1.id()).await.unwrap().unwrap();
544 assert_eq!(vtxo_1_full.serialize(), vtxo_1.serialize());
545
546 assert!(db.get_wallet_vtxo(vtxo_3.id()).await.expect("No error").is_none());
548
549 let vtxos = db.get_vtxos_by_state(&[VtxoStateKind::Spendable]).await.unwrap();
551 assert_eq!(vtxos.len(), 2);
552 assert!(vtxos.iter().any(|v| v.vtxo == vtxo_1.to_bare()));
553 assert!(vtxos.iter().any(|v| v.vtxo == vtxo_2.to_bare()));
554 assert!(!vtxos.iter().any(|v| v.vtxo == vtxo_3.to_bare()));
555
556 db.update_vtxo_state_checked(
558 vtxo_1.id(), VtxoState::Spent, &VtxoStateKind::UNSPENT_STATES,
559 ).await.unwrap();
560
561 let vtxos = db.get_vtxos_by_state(&[VtxoStateKind::Spendable]).await.unwrap();
562 assert_eq!(vtxos.len(), 1);
563
564 db.store_vtxos(&[(vtxo_3, &VtxoState::Spendable)]).await.unwrap();
566
567 let vtxos = db.get_vtxos_by_state(&[VtxoStateKind::Spendable]).await.unwrap();
568 assert_eq!(vtxos.len(), 2);
569 assert!(vtxos.iter().any(|v| v.vtxo == vtxo_2.to_bare()));
570 assert!(vtxos.iter().any(|v| v.vtxo == vtxo_3.to_bare()));
571
572 conn.close().unwrap();
573 }
574
575 #[tokio::test]
576 #[cfg(feature = "onchain-bdk")]
577 async fn test_create_wallet_then_load() {
578 use bdk_wallet::chain::DescriptorExt;
579
580 let (connection_string, conn) = in_memory_db();
581
582 let db = SqliteClient::open(connection_string).unwrap();
583 let network = bitcoin::Network::Testnet;
584
585 let seed = bip39::Mnemonic::generate(12).unwrap().to_seed("");
586 let xpriv = bitcoin::bip32::Xpriv::new_master(network, &seed).unwrap();
587
588 let desc = format!("tr({}/84'/0'/0'/*)", xpriv);
589
590 let _ = db.initialize_bdk_wallet().await.unwrap();
592 let mut created = bdk_wallet::Wallet::create_single(desc.clone())
593 .network(network)
594 .create_wallet_no_persist()
595 .unwrap();
596 db.store_bdk_wallet_changeset(&created.take_staged().unwrap()).await.unwrap();
597
598 let loaded = {
599 let changeset = db.initialize_bdk_wallet().await.unwrap();
600 bdk_wallet::Wallet::load()
601 .descriptor(bdk_wallet::KeychainKind::External, Some(desc.clone()))
602 .extract_keys()
603 .check_network(network)
604 .load_wallet_no_persist(changeset)
605 .unwrap()
606 };
607
608 assert!(loaded.is_some());
609 assert_eq!(
610 created.public_descriptor(bdk_wallet::KeychainKind::External).descriptor_id(),
611 loaded.unwrap().public_descriptor(bdk_wallet::KeychainKind::External).descriptor_id()
612 );
613
614 conn.close().unwrap();
617 }
618
619 #[tokio::test]
620 async fn differential_bark_persister_suite() {
621 let (cs, _conn) = helpers::in_memory_db();
622 let sqlite = SqliteClient::open(cs).unwrap();
623 let memory = crate::persist::adaptor::StorageAdaptorWrapper::new(
624 crate::persist::adaptor::memory::MemoryStorageAdaptor::new(),
625 );
626 crate::persist::test_suite::run_all(&sqlite, &memory).await;
627 }
628}