bsv-wallet-toolbox 0.2.23

Pure Rust BSV wallet-toolbox implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//! StorageReaderWriter trait -- extends StorageReader with mutation operations.
//!
//! Provides abstract insert/update methods for all table types,
//! transaction management (begin/commit/rollback), and default
//! find-or-insert helper methods.

use async_trait::async_trait;

use crate::error::WalletResult;
use crate::storage::find_args::*;
use crate::storage::traits::reader::StorageReader;
use crate::storage::verify_one_or_none;
use crate::storage::TrxToken;
use crate::tables::*;
use crate::wallet::types::AdminStatsResult;

/// Read-write storage interface extending StorageReader with insert, update,
/// and transaction management operations.
#[async_trait]
pub trait StorageReaderWriter: StorageReader {
    // -----------------------------------------------------------------------
    // Transaction management
    // -----------------------------------------------------------------------

    /// Begin a new database transaction. Returns a type-erased TrxToken.
    async fn begin_transaction(&self) -> WalletResult<TrxToken>;

    /// Commit a previously begun transaction.
    async fn commit_transaction(&self, trx: TrxToken) -> WalletResult<()>;

    /// Rollback a previously begun transaction.
    async fn rollback_transaction(&self, trx: TrxToken) -> WalletResult<()>;

    // -----------------------------------------------------------------------
    // Abstract insert methods
    // -----------------------------------------------------------------------

    /// Insert a user and return the new user_id.
    async fn insert_user(&self, user: &User, trx: Option<&TrxToken>) -> WalletResult<i64>;

    /// Insert a certificate and return the new certificate_id.
    async fn insert_certificate(
        &self,
        certificate: &Certificate,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Insert a certificate field.
    async fn insert_certificate_field(
        &self,
        field: &CertificateField,
        trx: Option<&TrxToken>,
    ) -> WalletResult<()>;

    /// Insert a commission and return the new commission_id.
    async fn insert_commission(
        &self,
        commission: &Commission,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Insert a monitor event and return the new id.
    async fn insert_monitor_event(
        &self,
        event: &MonitorEvent,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Delete monitor events matching `event` name with `id < before_id`.
    ///
    /// Used to prune stale checkpoint entries after a complete review cycle.
    /// Returns the number of rows deleted.
    async fn delete_monitor_events_before_id(
        &self,
        event_name: &str,
        before_id: i64,
        trx: Option<&TrxToken>,
    ) -> WalletResult<u64>;

    /// Insert an output basket and return the new basket_id.
    async fn insert_output_basket(
        &self,
        basket: &OutputBasket,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Insert an output tag and return the new output_tag_id.
    async fn insert_output_tag(&self, tag: &OutputTag, trx: Option<&TrxToken>)
        -> WalletResult<i64>;

    /// Insert an output tag map entry.
    async fn insert_output_tag_map(
        &self,
        tag_map: &OutputTagMap,
        trx: Option<&TrxToken>,
    ) -> WalletResult<()>;

    /// Insert an output and return the new output_id.
    async fn insert_output(&self, output: &Output, trx: Option<&TrxToken>) -> WalletResult<i64>;

    /// Insert a proven transaction and return the new proven_tx_id.
    async fn insert_proven_tx(
        &self,
        proven_tx: &ProvenTx,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Insert a proven transaction request and return the new proven_tx_req_id.
    async fn insert_proven_tx_req(
        &self,
        proven_tx_req: &ProvenTxReq,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Insert a transaction and return the new transaction_id.
    async fn insert_transaction(
        &self,
        transaction: &Transaction,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Insert a transaction label and return the new tx_label_id.
    async fn insert_tx_label(&self, label: &TxLabel, trx: Option<&TrxToken>) -> WalletResult<i64>;

    /// Insert a transaction label map entry.
    async fn insert_tx_label_map(
        &self,
        label_map: &TxLabelMap,
        trx: Option<&TrxToken>,
    ) -> WalletResult<()>;

    /// Insert a sync state and return the new sync_state_id.
    async fn insert_sync_state(
        &self,
        sync_state: &SyncState,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    // -----------------------------------------------------------------------
    // Abstract update methods
    // -----------------------------------------------------------------------

    /// Update a user by ID. Returns the number of rows affected.
    async fn update_user(
        &self,
        id: i64,
        update: &UserPartial,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Update a certificate by ID. Returns the number of rows affected.
    async fn update_certificate(
        &self,
        id: i64,
        update: &CertificatePartial,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Update a certificate field by certificate_id and field_name.
    /// Returns the number of rows affected.
    async fn update_certificate_field(
        &self,
        certificate_id: i64,
        field_name: &str,
        update: &CertificateFieldPartial,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Update a commission by ID. Returns the number of rows affected.
    async fn update_commission(
        &self,
        id: i64,
        update: &CommissionPartial,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Update a monitor event by ID. Returns the number of rows affected.
    async fn update_monitor_event(
        &self,
        id: i64,
        update: &MonitorEventPartial,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Update an output basket by ID. Returns the number of rows affected.
    async fn update_output_basket(
        &self,
        id: i64,
        update: &OutputBasketPartial,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Update an output tag by ID. Returns the number of rows affected.
    async fn update_output_tag(
        &self,
        id: i64,
        update: &OutputTagPartial,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Update an output tag map by output_id and tag_id.
    /// Returns the number of rows affected.
    async fn update_output_tag_map(
        &self,
        output_id: i64,
        tag_id: i64,
        update: &OutputTagMapPartial,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Update an output by ID. Returns the number of rows affected.
    async fn update_output(
        &self,
        id: i64,
        update: &OutputPartial,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Update a proven transaction by ID. Returns the number of rows affected.
    async fn update_proven_tx(
        &self,
        id: i64,
        update: &ProvenTxPartial,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Update proven transaction request(s) by ID or IDs.
    /// Returns the number of rows affected.
    async fn update_proven_tx_req(
        &self,
        id: i64,
        update: &ProvenTxReqPartial,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Update settings. Returns the number of rows affected.
    async fn update_settings(
        &self,
        update: &SettingsPartial,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Update a transaction by ID. Returns the number of rows affected.
    async fn update_transaction(
        &self,
        id: i64,
        update: &TransactionPartial,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Update a transaction label by ID. Returns the number of rows affected.
    async fn update_tx_label(
        &self,
        id: i64,
        update: &TxLabelPartial,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Update a transaction label map by transaction_id and tx_label_id.
    /// Returns the number of rows affected.
    async fn update_tx_label_map(
        &self,
        transaction_id: i64,
        tx_label_id: i64,
        update: &TxLabelMapPartial,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    /// Update a sync state by ID. Returns the number of rows affected.
    async fn update_sync_state(
        &self,
        id: i64,
        update: &SyncStatePartial,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64>;

    // -----------------------------------------------------------------------
    // Monitor-related composite methods (Phase 6)
    // -----------------------------------------------------------------------

    /// Update a transaction's status by txid.
    ///
    /// NOTE: as of the broadcast-outcome correctness fix, this method only
    /// updates the transaction row's status. Callers that need to also
    /// release locked UTXOs (the inputs this tx consumed) must call
    /// [`Self::restore_consumed_inputs`] explicitly. That separation is required
    /// because some failure paths (e.g. DoubleSpend) need to restore ONLY
    /// chain-verified-unspent inputs, not every `spent_by = tx_id` row.
    async fn update_transaction_status(
        &self,
        _txid: &str,
        _new_status: crate::status::TransactionStatus,
        _trx: Option<&TrxToken>,
    ) -> WalletResult<()> {
        Err(crate::error::WalletError::NotImplemented(
            "update_transaction_status".to_string(),
        ))
    }

    /// Batch version of update_transaction_status.
    async fn update_transactions_status(
        &self,
        _txids: &[String],
        _new_status: crate::status::TransactionStatus,
        _trx: Option<&TrxToken>,
    ) -> WalletResult<()> {
        Err(crate::error::WalletError::NotImplemented(
            "update_transactions_status".to_string(),
        ))
    }

    /// Restore every output this transaction consumed (rows with
    /// `spent_by = tx_id`) back to `spendable=true, spent_by=NULL`.
    ///
    /// Returns the number of output rows updated.
    ///
    /// This is the "release locked UTXOs" side-effect that used to be
    /// implicit in `update_transaction_status(..., Failed, ..)`. It is now
    /// explicit so callers with partial-restore semantics (e.g. DoubleSpend
    /// recovery, which only restores chain-verified-unspent inputs) can
    /// avoid the blanket cascade.
    async fn restore_consumed_inputs(
        &self,
        _tx_id: i64,
        _trx: Option<&TrxToken>,
    ) -> WalletResult<u64> {
        Err(crate::error::WalletError::NotImplemented(
            "restore_consumed_inputs".to_string(),
        ))
    }

    /// Composite: inserts ProvenTx, then updates ProvenTxReq with proven_tx_id
    /// and status=completed.
    async fn update_proven_tx_req_with_new_proven_tx(
        &self,
        _req_id: i64,
        _proven_tx: &crate::tables::ProvenTx,
        _trx: Option<&TrxToken>,
    ) -> WalletResult<i64> {
        Err(crate::error::WalletError::NotImplemented(
            "update_proven_tx_req_with_new_proven_tx".to_string(),
        ))
    }

    /// Finds reqs with stale statuses (e.g., unsent/sending older than aged_limit)
    /// and logs/corrects them. Returns summary string.
    async fn review_status(
        &self,
        _aged_limit: chrono::NaiveDateTime,
        _trx: Option<&TrxToken>,
    ) -> WalletResult<String> {
        Err(crate::error::WalletError::NotImplemented(
            "review_status".to_string(),
        ))
    }

    /// Deletes old records based on purge params. Returns summary string.
    async fn purge_data(
        &self,
        _params: &crate::storage::find_args::PurgeParams,
        _trx: Option<&TrxToken>,
    ) -> WalletResult<String> {
        Err(crate::error::WalletError::NotImplemented(
            "purge_data".to_string(),
        ))
    }

    /// Returns aggregate deployment statistics for the admin dashboard.
    ///
    /// Default implementation returns `NotImplemented`. Storage backends
    /// (e.g., SQLite) can override with real SQL aggregate queries.
    async fn admin_stats(&self, _auth_id: &str) -> WalletResult<AdminStatsResult> {
        Err(crate::error::WalletError::NotImplemented(
            "admin_stats".to_string(),
        ))
    }

    // -----------------------------------------------------------------------
    // Default find-or-insert helper methods
    // -----------------------------------------------------------------------

    /// Find a user by identity key, or insert a new one if not found.
    async fn find_or_insert_user(
        &self,
        identity_key: &str,
        trx: Option<&TrxToken>,
    ) -> WalletResult<(User, bool)> {
        let args = FindUsersArgs {
            partial: UserPartial {
                identity_key: Some(identity_key.to_string()),
                ..Default::default()
            },
            ..Default::default()
        };
        let existing = verify_one_or_none(self.find_users(&args, trx).await?)?;
        if let Some(user) = existing {
            return Ok((user, false));
        }
        let now = chrono::Utc::now().naive_utc();
        let new_user = User {
            created_at: now,
            updated_at: chrono::NaiveDate::from_ymd_opt(1971, 1, 1)
                .unwrap()
                .and_hms_opt(0, 0, 0)
                .unwrap(),
            user_id: 0,
            identity_key: identity_key.to_string(),
            active_storage: String::new(),
        };
        let user_id = self.insert_user(&new_user, trx).await?;
        let mut user = new_user;
        user.user_id = user_id;
        Ok((user, true))
    }

    /// Find an output basket by user and name, or insert a new one if not found.
    async fn find_or_insert_output_basket(
        &self,
        user_id: i64,
        name: &str,
        trx: Option<&TrxToken>,
    ) -> WalletResult<OutputBasket> {
        let args = FindOutputBasketsArgs {
            partial: OutputBasketPartial {
                user_id: Some(user_id),
                name: Some(name.to_string()),
                ..Default::default()
            },
            ..Default::default()
        };
        let existing = verify_one_or_none(self.find_output_baskets(&args, trx).await?)?;
        if let Some(basket) = existing {
            return Ok(basket);
        }
        let now = chrono::Utc::now().naive_utc();
        let new_basket = OutputBasket {
            created_at: now,
            updated_at: now,
            basket_id: 0,
            user_id,
            name: name.to_string(),
            number_of_desired_utxos: 0,
            minimum_desired_utxo_value: 0,
            is_deleted: false,
        };
        let basket_id = self.insert_output_basket(&new_basket, trx).await?;
        let mut basket = new_basket;
        basket.basket_id = basket_id;
        Ok(basket)
    }

    /// Find a transaction label by user and label text, or insert if not found.
    async fn find_or_insert_tx_label(
        &self,
        user_id: i64,
        label: &str,
        trx: Option<&TrxToken>,
    ) -> WalletResult<TxLabel> {
        let args = FindTxLabelsArgs {
            partial: TxLabelPartial {
                user_id: Some(user_id),
                label: Some(label.to_string()),
                ..Default::default()
            },
            ..Default::default()
        };
        let existing = verify_one_or_none(self.find_tx_labels(&args, trx).await?)?;
        if let Some(tx_label) = existing {
            return Ok(tx_label);
        }
        let now = chrono::Utc::now().naive_utc();
        let new_label = TxLabel {
            created_at: now,
            updated_at: now,
            tx_label_id: 0,
            user_id,
            label: label.to_string(),
            is_deleted: false,
        };
        let tx_label_id = self.insert_tx_label(&new_label, trx).await?;
        let mut result = new_label;
        result.tx_label_id = tx_label_id;
        Ok(result)
    }

    /// Find an output tag by user and tag text, or insert if not found.
    async fn find_or_insert_output_tag(
        &self,
        user_id: i64,
        tag: &str,
        trx: Option<&TrxToken>,
    ) -> WalletResult<OutputTag> {
        let args = FindOutputTagsArgs {
            partial: OutputTagPartial {
                user_id: Some(user_id),
                tag: Some(tag.to_string()),
                ..Default::default()
            },
            ..Default::default()
        };
        let existing = verify_one_or_none(self.find_output_tags(&args, trx).await?)?;
        if let Some(output_tag) = existing {
            return Ok(output_tag);
        }
        let now = chrono::Utc::now().naive_utc();
        let new_tag = OutputTag {
            created_at: now,
            updated_at: now,
            output_tag_id: 0,
            user_id,
            tag: tag.to_string(),
            is_deleted: false,
        };
        let output_tag_id = self.insert_output_tag(&new_tag, trx).await?;
        let mut result = new_tag;
        result.output_tag_id = output_tag_id;
        Ok(result)
    }
}