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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//! FindArgs structs for all 16 table types.
//!
//! Each `FindXxxArgs` struct follows the TS `FindPartialSincePagedArgs` pattern:
//! - `partial`: A struct with all-Option fields matching table columns for equality filtering.
//! - `since`: Optional datetime for filtering records updated since a given time.
//! - `paged`: Optional pagination with limit and offset.
//!
//! Some args have additional fields matching their TS counterparts
//! (e.g., `status` filter on transactions, `include_basket`/`include_tags` on outputs).

use chrono::NaiveDateTime;
use serde::Serialize;

use crate::status::{ProvenTxReqStatus, SyncStatus, TransactionStatus};
use crate::types::{Chain, StorageProvidedBy};

/// Pagination parameters.
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Paged {
    /// Maximum number of records to return.
    pub limit: i64,
    /// Number of records to skip before returning results.
    pub offset: i64,
}

// ---------------------------------------------------------------------------
// User
// ---------------------------------------------------------------------------

/// Partial filter for the `users` table.
#[derive(Debug, Default)]
pub struct UserPartial {
    /// Filter by user primary key.
    pub user_id: Option<i64>,
    /// Filter by identity key (hex public key).
    pub identity_key: Option<String>,
    /// Filter by active storage name.
    pub active_storage: Option<String>,
}

/// Arguments for querying users.
#[derive(Debug, Default)]
pub struct FindUsersArgs {
    /// Column equality filters.
    pub partial: UserPartial,
    /// Only return records updated after this timestamp.
    pub since: Option<NaiveDateTime>,
    /// Pagination (limit/offset).
    pub paged: Option<Paged>,
}

// ---------------------------------------------------------------------------
// Certificate
// ---------------------------------------------------------------------------

/// Partial filter for the `certificates` table.
#[derive(Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CertificatePartial {
    /// Filter by certificate primary key.
    pub certificate_id: Option<i64>,
    /// Filter by owning user.
    pub user_id: Option<i64>,
    /// Filter by certificate type identifier.
    pub cert_type: Option<String>,
    /// Filter by serial number.
    pub serial_number: Option<String>,
    /// Filter by certifier identity key.
    pub certifier: Option<String>,
    /// Filter by subject identity key.
    pub subject: Option<String>,
    /// Filter by verifier identity key.
    pub verifier: Option<String>,
    /// Filter by revocation outpoint (txid.vout).
    pub revocation_outpoint: Option<String>,
    /// Filter by signature value.
    pub signature: Option<String>,
    /// Filter by soft-delete flag.
    pub is_deleted: Option<bool>,
}

/// Arguments for querying certificates.
#[derive(Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FindCertificatesArgs {
    /// Column equality filters.
    pub partial: CertificatePartial,
    /// Only return records updated after this timestamp.
    pub since: Option<NaiveDateTime>,
    /// Pagination (limit/offset).
    pub paged: Option<Paged>,
}

// ---------------------------------------------------------------------------
// CertificateField
// ---------------------------------------------------------------------------

/// Partial filter for the `certificate_fields` table.
#[derive(Debug, Default)]
pub struct CertificateFieldPartial {
    /// Filter by parent certificate.
    pub certificate_id: Option<i64>,
    /// Filter by owning user.
    pub user_id: Option<i64>,
    /// Filter by field name.
    pub field_name: Option<String>,
    /// Filter by field value.
    pub field_value: Option<String>,
    /// Filter by master key used for encryption.
    pub master_key: Option<String>,
}

/// Arguments for querying certificate fields.
#[derive(Debug, Default)]
pub struct FindCertificateFieldsArgs {
    /// Column equality filters.
    pub partial: CertificateFieldPartial,
    /// Only return records updated after this timestamp.
    pub since: Option<NaiveDateTime>,
    /// Pagination (limit/offset).
    pub paged: Option<Paged>,
}

// ---------------------------------------------------------------------------
// Commission
// ---------------------------------------------------------------------------

/// Partial filter for the `commissions` table.
#[derive(Debug, Default)]
pub struct CommissionPartial {
    /// Filter by commission primary key.
    pub commission_id: Option<i64>,
    /// Filter by owning user.
    pub user_id: Option<i64>,
    /// Filter by parent transaction.
    pub transaction_id: Option<i64>,
    /// Filter by satoshi amount.
    pub satoshis: Option<i64>,
    /// Filter by key offset string.
    pub key_offset: Option<String>,
    /// Filter by redeemed flag.
    pub is_redeemed: Option<bool>,
}

/// Arguments for querying commissions.
#[derive(Debug, Default)]
pub struct FindCommissionsArgs {
    /// Column equality filters.
    pub partial: CommissionPartial,
    /// Only return records updated after this timestamp.
    pub since: Option<NaiveDateTime>,
    /// Pagination (limit/offset).
    pub paged: Option<Paged>,
}

// ---------------------------------------------------------------------------
// MonitorEvent
// ---------------------------------------------------------------------------

/// Partial filter for the `monitor_events` table.
#[derive(Debug, Default)]
pub struct MonitorEventPartial {
    /// Filter by event primary key.
    pub id: Option<i64>,
    /// Filter by event type string.
    pub event: Option<String>,
}

/// Arguments for querying monitor events.
#[derive(Debug, Default)]
pub struct FindMonitorEventsArgs {
    /// Column equality filters.
    pub partial: MonitorEventPartial,
    /// Only return records updated after this timestamp.
    pub since: Option<NaiveDateTime>,
    /// Pagination (limit/offset).
    pub paged: Option<Paged>,
}

// ---------------------------------------------------------------------------
// OutputBasket
// ---------------------------------------------------------------------------

/// Partial filter for the `output_baskets` table.
#[derive(Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OutputBasketPartial {
    /// Filter by basket primary key.
    pub basket_id: Option<i64>,
    /// Filter by owning user.
    pub user_id: Option<i64>,
    /// Filter by basket name.
    pub name: Option<String>,
    /// Filter by soft-delete flag.
    pub is_deleted: Option<bool>,
    /// Number of desired UTXOs for change management.
    pub number_of_desired_utxos: Option<i64>,
    /// Minimum desired UTXO value in satoshis for change management.
    pub minimum_desired_utxo_value: Option<i64>,
}

/// Arguments for querying output baskets.
#[derive(Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FindOutputBasketsArgs {
    /// Column equality filters.
    pub partial: OutputBasketPartial,
    /// Only return records updated after this timestamp.
    pub since: Option<NaiveDateTime>,
    /// Pagination (limit/offset).
    pub paged: Option<Paged>,
}

// ---------------------------------------------------------------------------
// OutputTag
// ---------------------------------------------------------------------------

/// Partial filter for the `output_tags` table.
#[derive(Debug, Default)]
pub struct OutputTagPartial {
    /// Filter by output tag primary key.
    pub output_tag_id: Option<i64>,
    /// Filter by owning user.
    pub user_id: Option<i64>,
    /// Filter by tag name.
    pub tag: Option<String>,
    /// Filter by soft-delete flag.
    pub is_deleted: Option<bool>,
}

/// Arguments for querying output tags.
#[derive(Debug, Default)]
pub struct FindOutputTagsArgs {
    /// Column equality filters.
    pub partial: OutputTagPartial,
    /// Only return records updated after this timestamp.
    pub since: Option<NaiveDateTime>,
    /// Pagination (limit/offset).
    pub paged: Option<Paged>,
}

// ---------------------------------------------------------------------------
// OutputTagMap
// ---------------------------------------------------------------------------

/// Partial filter for the `output_tag_maps` join table.
#[derive(Debug, Default)]
pub struct OutputTagMapPartial {
    /// Filter by tag foreign key.
    pub output_tag_id: Option<i64>,
    /// Filter by output foreign key.
    pub output_id: Option<i64>,
    /// Filter by soft-delete flag.
    pub is_deleted: Option<bool>,
}

/// Arguments for querying output tag mappings.
#[derive(Debug, Default)]
pub struct FindOutputTagMapsArgs {
    /// Column equality filters.
    pub partial: OutputTagMapPartial,
    /// Only return records updated after this timestamp.
    pub since: Option<NaiveDateTime>,
    /// Pagination (limit/offset).
    pub paged: Option<Paged>,
}

// ---------------------------------------------------------------------------
// Output
// ---------------------------------------------------------------------------

/// Partial filter for the `outputs` table.
#[derive(Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OutputPartial {
    /// Filter by output primary key.
    pub output_id: Option<i64>,
    /// Filter by owning user.
    pub user_id: Option<i64>,
    /// Filter by parent transaction.
    pub transaction_id: Option<i64>,
    /// Filter by output basket.
    pub basket_id: Option<i64>,
    /// Filter by spendable flag.
    pub spendable: Option<bool>,
    /// Filter by change output flag.
    pub change: Option<bool>,
    /// Filter by output index within the transaction.
    pub vout: Option<i32>,
    /// Filter by satoshi amount.
    pub satoshis: Option<i64>,
    /// Filter by who provided the storage.
    pub provided_by: Option<StorageProvidedBy>,
    /// Filter by purpose string.
    pub purpose: Option<String>,
    /// Filter by output type descriptor.
    pub output_type: Option<String>,
    /// Filter by transaction ID hex.
    pub txid: Option<String>,
    /// Filter by sender identity key.
    pub sender_identity_key: Option<String>,
    /// Filter by the transaction ID that spent this output.
    pub spent_by: Option<i64>,
}

/// Arguments for querying outputs.
#[derive(Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FindOutputsArgs {
    /// Column equality filters.
    pub partial: OutputPartial,
    /// Only return records updated after this timestamp.
    pub since: Option<NaiveDateTime>,
    /// Pagination (limit/offset).
    pub paged: Option<Paged>,
    /// Filter by transaction status (joined through the transactions table).
    pub tx_status: Option<Vec<TransactionStatus>>,
    /// If true, do not return the locking_script field (performance optimization).
    pub no_script: bool,
    /// If true, include the basket details for each output.
    pub include_basket: bool,
    /// If true, include the tags for each output.
    pub include_tags: bool,
}

// ---------------------------------------------------------------------------
// ProvenTx
// ---------------------------------------------------------------------------

/// Partial filter for the `proven_txs` table.
#[derive(Debug, Default)]
pub struct ProvenTxPartial {
    /// Filter by proven tx primary key.
    pub proven_tx_id: Option<i64>,
    /// Filter by transaction ID hex.
    pub txid: Option<String>,
    /// Filter by block height.
    pub height: Option<i32>,
    /// Filter by block hash hex.
    pub block_hash: Option<String>,
}

/// Arguments for querying proven transactions.
#[derive(Debug, Default)]
pub struct FindProvenTxsArgs {
    /// Column equality filters.
    pub partial: ProvenTxPartial,
    /// Only return records updated after this timestamp.
    pub since: Option<NaiveDateTime>,
    /// Pagination (limit/offset).
    pub paged: Option<Paged>,
}

// ---------------------------------------------------------------------------
// ProvenTxReq
// ---------------------------------------------------------------------------

/// Partial filter for the `proven_tx_reqs` table.
#[derive(Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ProvenTxReqPartial {
    /// Filter by proven tx req primary key.
    pub proven_tx_req_id: Option<i64>,
    /// Filter by associated proven tx.
    pub proven_tx_id: Option<i64>,
    /// Filter by request status.
    pub status: Option<ProvenTxReqStatus>,
    /// Filter by transaction ID hex.
    pub txid: Option<String>,
    /// Filter by batch identifier.
    pub batch: Option<String>,
    /// Filter by notification flag.
    pub notified: Option<bool>,
    /// When used as an update target, overwrite the `attempts` counter.
    /// Filter semantics are unused (updates only). Mirrors the TS
    /// `attemptToPostReqsToNetwork` flow where a ServiceError bumps
    /// `req.attempts` in the same update as the status transition.
    pub attempts: Option<i32>,
}

/// Arguments for querying proven transaction requests.
#[derive(Debug, Default, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FindProvenTxReqsArgs {
    /// Column equality filters.
    pub partial: ProvenTxReqPartial,
    /// Only return records updated after this timestamp.
    pub since: Option<NaiveDateTime>,
    /// Pagination (limit/offset).
    pub paged: Option<Paged>,
    /// Multi-status filter: if set and non-empty, generates `status IN (...)` clause.
    /// Takes precedence over `partial.status` if both are set.
    pub statuses: Option<Vec<ProvenTxReqStatus>>,
}

// ---------------------------------------------------------------------------
// Settings
// ---------------------------------------------------------------------------

/// Partial filter for the `settings` table.
#[derive(Debug, Default)]
pub struct SettingsPartial {
    /// Filter by storage identity key.
    pub storage_identity_key: Option<String>,
    /// Filter by storage name.
    pub storage_name: Option<String>,
    /// Filter by BSV chain type.
    pub chain: Option<Chain>,
    /// Wallet settings JSON blob to write (used by update_settings).
    /// When set to Some("null") during update, clears the stored settings.
    pub wallet_settings_json: Option<String>,
}

/// Arguments for querying settings.
#[derive(Debug, Default)]
pub struct FindSettingsArgs {
    /// Column equality filters.
    pub partial: SettingsPartial,
    /// Only return records updated after this timestamp.
    pub since: Option<NaiveDateTime>,
    /// Pagination (limit/offset).
    pub paged: Option<Paged>,
}

// ---------------------------------------------------------------------------
// SyncState
// ---------------------------------------------------------------------------

/// Partial filter for the `sync_states` table.
#[derive(Debug, Default)]
pub struct SyncStatePartial {
    /// Filter by sync state primary key.
    pub sync_state_id: Option<i64>,
    /// Filter by owning user.
    pub user_id: Option<i64>,
    /// Filter by storage identity key.
    pub storage_identity_key: Option<String>,
    /// Filter by storage name.
    pub storage_name: Option<String>,
    /// Filter by sync status.
    pub status: Option<SyncStatus>,
    /// Filter by initialization flag.
    pub init: Option<bool>,
    /// Updated sync map JSON (used by update_sync_state).
    pub sync_map: Option<String>,
    /// Timestamp of last successful sync (advances the high-watermark for the next sync).
    pub when: Option<chrono::NaiveDateTime>,
}

/// Arguments for querying sync states.
#[derive(Debug, Default)]
pub struct FindSyncStatesArgs {
    /// Column equality filters.
    pub partial: SyncStatePartial,
    /// Only return records updated after this timestamp.
    pub since: Option<NaiveDateTime>,
    /// Pagination (limit/offset).
    pub paged: Option<Paged>,
}

// ---------------------------------------------------------------------------
// Transaction
// ---------------------------------------------------------------------------

/// Partial filter for the `transactions` table.
#[derive(Debug, Default)]
pub struct TransactionPartial {
    /// Filter by transaction primary key.
    pub transaction_id: Option<i64>,
    /// Filter by owning user.
    pub user_id: Option<i64>,
    /// Filter by associated proven tx.
    pub proven_tx_id: Option<i64>,
    /// Filter by transaction status.
    pub status: Option<TransactionStatus>,
    /// Filter by reference string.
    pub reference: Option<String>,
    /// Filter by outgoing direction flag.
    pub is_outgoing: Option<bool>,
    /// Filter by transaction ID hex.
    pub txid: Option<String>,
    /// Update raw transaction bytes.
    pub raw_tx: Option<Vec<u8>>,
}

/// Arguments for querying transactions.
#[derive(Debug, Default)]
pub struct FindTransactionsArgs {
    /// Column equality filters.
    pub partial: TransactionPartial,
    /// Only return records updated after this timestamp.
    pub since: Option<NaiveDateTime>,
    /// Pagination (limit/offset).
    pub paged: Option<Paged>,
    /// Filter by multiple statuses.
    pub status: Option<Vec<TransactionStatus>>,
    /// If true, do not return raw_tx and input_beef fields.
    pub no_raw_tx: bool,
}

// ---------------------------------------------------------------------------
// TxLabel
// ---------------------------------------------------------------------------

/// Partial filter for the `tx_labels` table.
#[derive(Debug, Default)]
pub struct TxLabelPartial {
    /// Filter by tx label primary key.
    pub tx_label_id: Option<i64>,
    /// Filter by owning user.
    pub user_id: Option<i64>,
    /// Filter by label text.
    pub label: Option<String>,
    /// Filter by soft-delete flag.
    pub is_deleted: Option<bool>,
}

/// Arguments for querying transaction labels.
#[derive(Debug, Default)]
pub struct FindTxLabelsArgs {
    /// Column equality filters.
    pub partial: TxLabelPartial,
    /// Only return records updated after this timestamp.
    pub since: Option<NaiveDateTime>,
    /// Pagination (limit/offset).
    pub paged: Option<Paged>,
}

// ---------------------------------------------------------------------------
// TxLabelMap
// ---------------------------------------------------------------------------

/// Partial filter for the `tx_label_maps` join table.
#[derive(Debug, Default)]
pub struct TxLabelMapPartial {
    /// Filter by label foreign key.
    pub tx_label_id: Option<i64>,
    /// Filter by transaction foreign key.
    pub transaction_id: Option<i64>,
    /// Filter by soft-delete flag.
    pub is_deleted: Option<bool>,
}

/// Arguments for querying transaction label mappings.
#[derive(Debug, Default)]
pub struct FindTxLabelMapsArgs {
    /// Column equality filters.
    pub partial: TxLabelMapPartial,
    /// Only return records updated after this timestamp.
    pub since: Option<NaiveDateTime>,
    /// Pagination (limit/offset).
    pub paged: Option<Paged>,
}

// ---------------------------------------------------------------------------
// PurgeParams (used by Monitor purge operations)
// ---------------------------------------------------------------------------

/// Parameters controlling data purge operations.
///
/// Used by the Monitor's TaskPurge to clean up old records.
#[derive(Debug, Clone)]
pub struct PurgeParams {
    /// Whether to purge spent transaction records.
    pub purge_spent: bool,
    /// Whether to purge completed transaction records.
    pub purge_completed: bool,
    /// Whether to purge failed transaction records.
    pub purge_failed: bool,
    /// Age threshold (in msecs) for purging spent records.
    pub purge_spent_age: u64,
    /// Age threshold (in msecs) for purging completed records.
    pub purge_completed_age: u64,
    /// Age threshold (in msecs) for purging failed records.
    pub purge_failed_age: u64,
    /// Whether to purge old monitor event records.
    pub purge_monitor_events: bool,
    /// Age threshold (in msecs) for purging monitor events.
    pub purge_monitor_events_age: u64,
}

impl Default for PurgeParams {
    fn default() -> Self {
        Self {
            purge_spent: false,
            purge_completed: false,
            purge_failed: true,
            purge_spent_age: 2 * 604_800_000, // 2 weeks
            purge_completed_age: 2 * 604_800_000,
            purge_failed_age: 5 * 86_400_000, // 5 days
            purge_monitor_events: true,
            purge_monitor_events_age: 30 * 86_400_000, // 30 days
        }
    }
}

// ---------------------------------------------------------------------------
// ForUser (used by getProvenTxsForUser, etc.)
// ---------------------------------------------------------------------------

/// Arguments for querying records belonging to a specific user with pagination.
#[derive(Debug)]
pub struct FindForUserSincePagedArgs {
    /// The user to query records for.
    pub user_id: i64,
    /// Only return records updated after this timestamp.
    pub since: Option<NaiveDateTime>,
    /// Pagination (limit/offset).
    pub paged: Option<Paged>,
}