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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
//! WalletStorageManager -- multi-provider storage coordinator with hierarchical locking.
//!
//! Implements the TS-parity manager architecture:
//! - Multiple providers wrapped in `ManagedStorage` with cached settings/user
//! - `make_available()` partitions stores into active/backups/conflicting_actives
//! - Four-level hierarchical lock system (reader < writer < sync < storage_provider)
//! - All writes route to active only; backups sync via `update_backups()` (Plan 02)

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use tracing::warn;

use bsv::wallet::interfaces::{
    AbortActionArgs, AbortActionResult, ListActionsArgs, ListActionsResult, ListCertificatesArgs,
    ListCertificatesResult, ListOutputsArgs, ListOutputsResult, RelinquishCertificateArgs,
    RelinquishOutputArgs,
};

use crate::error::{WalletError, WalletResult};
use crate::services::traits::WalletServices;
use crate::status::TransactionStatus;
use crate::storage::action_types::{
    StorageCreateActionArgs, StorageCreateActionResult, StorageInternalizeActionArgs,
    StorageInternalizeActionResult, StorageProcessActionArgs, StorageProcessActionResult,
};
use crate::storage::find_args::{
    FindCertificateFieldsArgs, FindCertificatesArgs, FindMonitorEventsArgs, FindOutputBasketsArgs,
    FindOutputsArgs, FindProvenTxReqsArgs, FindProvenTxsArgs, FindTransactionsArgs, OutputPartial,
    ProvenTxPartial, ProvenTxReqPartial, PurgeParams, TransactionPartial,
};
use crate::storage::sync::request_args::{RequestSyncChunkArgs, SyncChunkOffset};
use crate::storage::sync::sync_map::SyncMap;
use crate::storage::sync::{ProcessSyncChunkResult, SyncChunk};
use crate::storage::traits::wallet_provider::WalletStorageProvider;
use crate::storage::TrxToken;
use crate::tables::SyncState;
use crate::tables::{
    Certificate, MonitorEvent, Output, OutputBasket, ProvenTx, ProvenTxReq, Settings, Transaction,
    User,
};
use crate::wallet::types::{
    AdminStatsResult, AuthId, ReproveHeaderResult, ReproveProvenResult, ReproveProvenUpdate,
    WalletStorageInfo,
};

// ---------------------------------------------------------------------------
// make_request_sync_chunk_args -- free helper for sync loop
// ---------------------------------------------------------------------------

/// Build `RequestSyncChunkArgs` from an existing `SyncState`.
///
/// The `SyncState.sync_map` JSON is deserialized to extract per-entity
/// offsets (count field) and the `when` timestamp becomes the `since` filter.
///
/// # Arguments
/// * `sync_state` - The sync state record for this storage pair.
/// * `identity_key` - The wallet owner's identity key.
/// * `writer_storage_identity_key` - The destination storage's identity key.
pub fn make_request_sync_chunk_args(
    sync_state: &SyncState,
    identity_key: &str,
    writer_storage_identity_key: &str,
) -> WalletResult<RequestSyncChunkArgs> {
    // Parse the stored JSON sync_map to extract per-entity offsets.
    // A newly-inserted SyncState has sync_map = "{}" which cannot deserialize
    // into SyncMap (all fields required). Fall back to a fresh SyncMap in that case.
    let sync_map: SyncMap = if sync_state.sync_map.is_empty() || sync_state.sync_map == "{}" {
        SyncMap::new()
    } else {
        serde_json::from_str(&sync_state.sync_map).map_err(|e| {
            WalletError::Internal(format!(
                "make_request_sync_chunk_args: failed to parse sync_map JSON: {e}"
            ))
        })?
    };

    // Build offsets from each EntitySyncMap's count field.
    // The `count` tracks cumulative items received in the current sync window
    // — this IS the pagination offset, matching TS `ess.count`.
    let offsets = vec![
        SyncChunkOffset {
            name: sync_map.proven_tx.entity_name.clone(),
            offset: sync_map.proven_tx.count,
        },
        SyncChunkOffset {
            name: sync_map.output_basket.entity_name.clone(),
            offset: sync_map.output_basket.count,
        },
        SyncChunkOffset {
            name: sync_map.transaction.entity_name.clone(),
            offset: sync_map.transaction.count,
        },
        SyncChunkOffset {
            name: sync_map.output.entity_name.clone(),
            offset: sync_map.output.count,
        },
        SyncChunkOffset {
            name: sync_map.tx_label.entity_name.clone(),
            offset: sync_map.tx_label.count,
        },
        SyncChunkOffset {
            name: sync_map.tx_label_map.entity_name.clone(),
            offset: sync_map.tx_label_map.count,
        },
        SyncChunkOffset {
            name: sync_map.output_tag.entity_name.clone(),
            offset: sync_map.output_tag.count,
        },
        SyncChunkOffset {
            name: sync_map.output_tag_map.entity_name.clone(),
            offset: sync_map.output_tag_map.count,
        },
        SyncChunkOffset {
            name: sync_map.certificate.entity_name.clone(),
            offset: sync_map.certificate.count,
        },
        SyncChunkOffset {
            name: sync_map.certificate_field.entity_name.clone(),
            offset: sync_map.certificate_field.count,
        },
        SyncChunkOffset {
            name: sync_map.commission.entity_name.clone(),
            offset: sync_map.commission.count,
        },
        SyncChunkOffset {
            name: sync_map.proven_tx_req.entity_name.clone(),
            offset: sync_map.proven_tx_req.count,
        },
    ];

    Ok(RequestSyncChunkArgs {
        // from = the reader (sync_state records the reader's identity key)
        from_storage_identity_key: sync_state.storage_identity_key.clone(),
        // to = the writer we are syncing into
        to_storage_identity_key: writer_storage_identity_key.to_string(),
        identity_key: identity_key.to_string(),
        since: sync_state.when,
        max_rough_size: 10_000_000,
        max_items: 1000,
        offsets,
    })
}

// ---------------------------------------------------------------------------
// ManagedStorage -- wraps a single WalletStorageProvider with cached state
// ---------------------------------------------------------------------------

/// Per-provider wrapper that caches settings and user after `make_available`.
///
/// Private to this module — only `WalletStorageManager` creates these.
struct ManagedStorage {
    /// The underlying storage provider.
    storage: Arc<dyn WalletStorageProvider>,
    /// Set once at construction from `storage.is_storage_provider()`.
    is_storage_provider: bool,
    /// Becomes true after `make_available()` succeeds for this store.
    is_available: AtomicBool,
    /// Cached `Settings` populated during `make_available`.
    settings: tokio::sync::Mutex<Option<Settings>>,
    /// Cached `User` populated during `make_available`.
    user: tokio::sync::Mutex<Option<User>>,
}

impl ManagedStorage {
    fn new(storage: Arc<dyn WalletStorageProvider>) -> Self {
        let is_sp = storage.is_storage_provider();
        Self {
            storage,
            is_storage_provider: is_sp,
            is_available: AtomicBool::new(false),
            settings: tokio::sync::Mutex::new(None),
            user: tokio::sync::Mutex::new(None),
        }
    }

    /// Get a clone of the cached settings, if available.
    async fn get_settings_cached(&self) -> Option<Settings> {
        self.settings.lock().await.clone()
    }

    /// Get a clone of the cached user, if available.
    async fn get_user_cached(&self) -> Option<User> {
        self.user.lock().await.clone()
    }
}

// ---------------------------------------------------------------------------
// ManagerState -- partition state for active/backup/conflicting stores
// ---------------------------------------------------------------------------

/// Guards the partition state: which store is active, which are backups, etc.
///
/// Protected by a `tokio::sync::Mutex` inside `WalletStorageManager`.
struct ManagerState {
    /// All providers, with active (if any) first at construction.
    stores: Vec<ManagedStorage>,
    /// Index into `stores` for the currently-active provider.
    active_index: Option<usize>,
    /// Indices of backup stores (after active is determined).
    backup_indices: Vec<usize>,
    /// Indices of stores whose `user.active_storage` doesn't match active.
    conflicting_active_indices: Vec<usize>,
}

impl ManagerState {
    fn new(stores: Vec<ManagedStorage>) -> Self {
        Self {
            stores,
            active_index: None,
            backup_indices: Vec::new(),
            conflicting_active_indices: Vec::new(),
        }
    }

    /// Returns the active index, or an error if not yet partitioned.
    fn require_active(&self) -> WalletResult<usize> {
        self.active_index.ok_or_else(|| {
            WalletError::InvalidOperation(
                "WalletStorageManager not yet available — call make_available() first".to_string(),
            )
        })
    }
}

// ---------------------------------------------------------------------------
// WalletStorageManager -- public struct
// ---------------------------------------------------------------------------

/// Multi-provider storage manager with hierarchical locking.
///
/// Wraps one or more `WalletStorageProvider` implementations. On first use
/// (or explicit `make_available()` call) partitions providers into
/// active / backups / conflicting_actives based on stored user preferences.
///
/// # Lock hierarchy
///
/// Four `tokio::sync::Mutex<()>` must be acquired in strict ascending order:
/// reader < writer < sync < storage_provider.
///
/// - `acquire_reader` — read-only operations
/// - `acquire_writer` — mutations (create/process/abort action, etc.)
/// - `acquire_sync` — sync loop operations
/// - `acquire_storage_provider` — storage-provider-level operations
pub struct WalletStorageManager {
    /// Partition state — holds all ManagedStorage instances.
    state: tokio::sync::Mutex<ManagerState>,
    /// Wallet owner's identity key (compressed public key hex).
    identity_key: String,
    /// Original active provider (the first store passed to constructor).
    /// Kept for sync access before make_available() completes.
    initial_active: Option<Arc<dyn WalletStorageProvider>>,
    /// Original backup providers (remaining stores passed to constructor).
    /// Kept for sync access before make_available() completes.
    initial_backups: Vec<Arc<dyn WalletStorageProvider>>,
    /// Level-1 lock (outermost). All operations acquire this.
    pub(crate) reader_lock: tokio::sync::Mutex<()>,
    /// Level-2 lock. Writers + sync + sp acquire this.
    pub(crate) writer_lock: tokio::sync::Mutex<()>,
    /// Level-3 lock. Sync + sp acquire this.
    pub(crate) sync_lock: tokio::sync::Mutex<()>,
    /// Level-4 lock (innermost). Only sp-level operations acquire this.
    pub(crate) sp_lock: tokio::sync::Mutex<()>,
    /// Manager-level spend lock. Serializes UTXO-mutating operations
    /// (createAction/signAction/internalizeAction/abortAction/relinquishOutput)
    /// across ALL `Wallet` instances sharing this manager, preventing
    /// double-spend from concurrent callers on shared storage.
    pub(crate) spend_lock: tokio::sync::Mutex<()>,
    /// True once `make_available()` completes successfully.
    is_available_flag: AtomicBool,
    /// Optional wallet services reference.
    services: tokio::sync::Mutex<Option<Arc<dyn WalletServices>>>,
}

impl WalletStorageManager {
    // -----------------------------------------------------------------------
    // Constructor
    // -----------------------------------------------------------------------

    /// Create a new manager from an optional active provider and zero or more backups.
    ///
    /// Providers are stored in order: active first (if Some), then backups.
    /// No partitioning happens at construction — call `make_available()` to initialize.
    pub fn new(
        identity_key: String,
        active: Option<Arc<dyn WalletStorageProvider>>,
        backups: Vec<Arc<dyn WalletStorageProvider>>,
    ) -> Self {
        let initial_active = active.clone();
        let initial_backups = backups.clone();

        let mut stores = Vec::with_capacity(active.is_some() as usize + backups.len());
        if let Some(a) = active {
            stores.push(ManagedStorage::new(a));
        }
        for b in backups {
            stores.push(ManagedStorage::new(b));
        }

        Self {
            state: tokio::sync::Mutex::new(ManagerState::new(stores)),
            identity_key,
            initial_active,
            initial_backups,
            reader_lock: tokio::sync::Mutex::new(()),
            writer_lock: tokio::sync::Mutex::new(()),
            sync_lock: tokio::sync::Mutex::new(()),
            sp_lock: tokio::sync::Mutex::new(()),
            spend_lock: tokio::sync::Mutex::new(()),
            is_available_flag: AtomicBool::new(false),
            services: tokio::sync::Mutex::new(None),
        }
    }

    // -----------------------------------------------------------------------
    // Zero-lock sync accessors
    // -----------------------------------------------------------------------

    /// Returns the wallet owner's identity key.
    pub fn get_user_identity_key(&self) -> &str {
        &self.identity_key
    }

    /// Alias for `get_user_identity_key()`.
    pub fn auth_id(&self) -> &str {
        &self.identity_key
    }

    /// Returns `true` after `make_available()` has successfully completed.
    pub fn is_available(&self) -> bool {
        self.is_available_flag.load(Ordering::Acquire)
    }

    /// The manager itself is NOT a storage provider — always returns `false`.
    ///
    /// Matches TS `WalletStorageManager.isStorageProvider = false`.
    pub fn is_storage_provider(&self) -> bool {
        false
    }

    /// Returns the initial active provider (the first provider passed to the constructor).
    ///
    /// This is a sync accessor that returns the provider as passed at construction,
    /// before `make_available()` determines the actual active via partition logic.
    /// Primarily used for backward-compatibility in sync contexts (Wallet::new, signer setup).
    pub fn active(&self) -> Option<&Arc<dyn WalletStorageProvider>> {
        self.initial_active.as_ref()
    }

    /// Returns the initial backup providers (the remaining providers from the constructor).
    ///
    /// Sync accessor for backward-compatibility.
    pub fn backups(&self) -> &[Arc<dyn WalletStorageProvider>] {
        &self.initial_backups
    }

    /// Returns the first backup provider if exactly one backup was provided.
    ///
    /// Backward-compatibility alias for the old `backup()` method.
    pub fn backup(&self) -> Option<&Arc<dyn WalletStorageProvider>> {
        self.initial_backups.first()
    }

    /// Returns true if at least one backup provider was provided.
    pub fn has_backup(&self) -> bool {
        !self.initial_backups.is_empty()
    }

    // -----------------------------------------------------------------------
    // Async state accessors (lock state mutex)
    // -----------------------------------------------------------------------

    /// Returns `true` if there is at least one provider registered.
    pub async fn can_make_available(&self) -> bool {
        let state = self.state.lock().await;
        !state.stores.is_empty()
    }

    /// Returns the active store's `settings.storage_identity_key`.
    pub async fn get_active_store(&self) -> WalletResult<String> {
        let state = self.state.lock().await;
        let idx = state.require_active()?;
        let settings = state.stores[idx]
            .get_settings_cached()
            .await
            .ok_or_else(|| {
                WalletError::InvalidOperation("Active settings not cached".to_string())
            })?;
        Ok(settings.storage_identity_key)
    }

    /// Returns the active store's `settings.storage_name`.
    pub async fn get_active_store_name(&self) -> WalletResult<String> {
        let state = self.state.lock().await;
        let idx = state.require_active()?;
        let settings = state.stores[idx]
            .get_settings_cached()
            .await
            .ok_or_else(|| {
                WalletError::InvalidOperation("Active settings not cached".to_string())
            })?;
        Ok(settings.storage_name)
    }

    /// Returns a clone of the active store's cached `Settings`.
    pub async fn get_active_settings(&self) -> WalletResult<Settings> {
        let state = self.state.lock().await;
        let idx = state.require_active()?;
        state.stores[idx]
            .get_settings_cached()
            .await
            .ok_or_else(|| WalletError::InvalidOperation("Active settings not cached".to_string()))
    }

    /// Alias for `get_active_settings()` (TS parity name: `getSettings`).
    pub async fn get_settings(&self) -> WalletResult<Settings> {
        self.get_active_settings().await
    }

    /// Returns a clone of the active store's cached `User`.
    pub async fn get_active_user(&self) -> WalletResult<User> {
        let state = self.state.lock().await;
        let idx = state.require_active()?;
        state.stores[idx]
            .get_user_cached()
            .await
            .ok_or_else(|| WalletError::InvalidOperation("Active user not cached".to_string()))
    }

    /// Returns `storage_identity_key` strings for all backup stores.
    pub async fn get_backup_stores(&self) -> Vec<String> {
        let state = self.state.lock().await;
        let mut result = Vec::with_capacity(state.backup_indices.len());
        for &idx in &state.backup_indices {
            if let Some(settings) = state.stores[idx].get_settings_cached().await {
                result.push(settings.storage_identity_key);
            }
        }
        result
    }

    /// Returns `storage_identity_key` strings for all conflicting active stores.
    pub async fn get_conflicting_stores(&self) -> Vec<String> {
        let state = self.state.lock().await;
        let mut result = Vec::with_capacity(state.conflicting_active_indices.len());
        for &idx in &state.conflicting_active_indices {
            if let Some(settings) = state.stores[idx].get_settings_cached().await {
                result.push(settings.storage_identity_key);
            }
        }
        result
    }

    /// Returns `storage_identity_key` strings for ALL stores.
    ///
    /// Before `make_available()`, returns empty strings as placeholders so callers
    /// can check the count without waiting for initialization.
    pub async fn get_all_stores(&self) -> Vec<String> {
        let state = self.state.lock().await;
        let mut result = Vec::with_capacity(state.stores.len());
        for store in &state.stores {
            if let Some(settings) = store.get_settings_cached().await {
                result.push(settings.storage_identity_key);
            } else {
                result.push(String::new());
            }
        }
        result
    }

    /// Returns `true` if the active store's `is_storage_provider` flag is set.
    pub async fn is_active_storage_provider(&self) -> WalletResult<bool> {
        let state = self.state.lock().await;
        let idx = state.require_active()?;
        Ok(state.stores[idx].is_storage_provider)
    }

    /// Returns true if active.settings.storage_identity_key == active.user.active_storage
    /// AND conflicting_active_indices is empty.
    pub async fn is_active_enabled(&self) -> bool {
        let state = self.state.lock().await;
        let Some(idx) = state.active_index else {
            return false;
        };
        let settings = match state.stores[idx].get_settings_cached().await {
            Some(s) => s,
            None => return false,
        };
        let user = match state.stores[idx].get_user_cached().await {
            Some(u) => u,
            None => return false,
        };
        state.conflicting_active_indices.is_empty()
            && settings.storage_identity_key == user.active_storage
    }

    /// Builds an `AuthId` for the manager's identity key.
    ///
    /// If `must_be_active` is true and the manager is not yet available,
    /// returns `WERR_NOT_ACTIVE`.
    pub async fn get_auth(&self, must_be_active: bool) -> WalletResult<AuthId> {
        if must_be_active && !self.is_available() {
            // Auto-initialize if not yet available (same pattern as acquire_reader)
            self.make_available().await?;
        }

        let state = self.state.lock().await;
        let user_id = if let Some(idx) = state.active_index {
            state.stores[idx].get_user_cached().await.map(|u| u.user_id)
        } else {
            None
        };

        if must_be_active {
            let Some(idx) = state.active_index else {
                return Err(WalletError::NotActive(
                    "No active storage — call make_available() first".to_string(),
                ));
            };
            let settings = state.stores[idx]
                .get_settings_cached()
                .await
                .ok_or_else(|| WalletError::NotActive("Active settings not cached".to_string()))?;
            let user = state.stores[idx]
                .get_user_cached()
                .await
                .ok_or_else(|| WalletError::NotActive("Active user not cached".to_string()))?;
            if !state.conflicting_active_indices.is_empty()
                || settings.storage_identity_key != user.active_storage
            {
                return Err(WalletError::NotActive(
                    "Active storage is not enabled — conflicting stores or mismatched active_storage".to_string(),
                ));
            }
        }

        Ok(AuthId {
            identity_key: self.identity_key.clone(),
            user_id,
            is_active: Some(self.is_available()),
        })
    }

    /// Returns the user_id from the active store's cached user.
    pub async fn get_user_id(&self) -> WalletResult<i64> {
        let auth = self.get_auth(false).await?;
        auth.user_id.ok_or_else(|| {
            WalletError::InvalidOperation(
                "user_id not available — call make_available() first".to_string(),
            )
        })
    }

    // -----------------------------------------------------------------------
    // get_active — returns the active WalletStorageProvider
    // -----------------------------------------------------------------------

    /// Returns a clone of the active `Arc<dyn WalletStorageProvider>`.
    ///
    /// Errors if `make_available()` has not been called.
    pub async fn get_active(&self) -> WalletResult<Arc<dyn WalletStorageProvider>> {
        let state = self.state.lock().await;
        let idx = state.require_active()?;
        Ok(state.stores[idx].storage.clone())
    }

    // -----------------------------------------------------------------------
    // makeAvailable — initialize and partition stores
    // -----------------------------------------------------------------------

    /// Initialize all stores and partition them into active/backups/conflicting.
    ///
    /// Idempotent — returns cached active settings on subsequent calls.
    /// Uses `reader_lock` as the idempotency guard to prevent concurrent re-init.
    pub async fn make_available(&self) -> WalletResult<Settings> {
        // Fast path: already available
        if self.is_available() {
            return self.get_active_settings().await;
        }

        // Acquire reader lock to serialize concurrent makeAvailable calls (Pitfall 3)
        let _reader_guard = self.reader_lock.lock().await;

        // Re-check after acquiring lock (another task may have finished first)
        if self.is_available() {
            return self.get_active_settings().await;
        }

        self.do_make_available().await
    }

    /// Internal make_available that runs under the reader lock.
    ///
    /// Separated from `make_available()` so `acquire_reader()` can avoid
    /// re-acquiring the reader_lock that it is about to return to the caller.
    async fn do_make_available(&self) -> WalletResult<Settings> {
        // Collect storage Arcs under a brief state lock, then release so that
        // network I/O (make_available, find_or_insert_user) does not block
        // concurrent operations on the manager.
        let providers: Vec<Arc<dyn WalletStorageProvider>> = {
            let state = self.state.lock().await;
            if state.stores.is_empty() {
                return Err(WalletError::InvalidParameter {
                    parameter: "stores".to_string(),
                    must_be: "non-empty — provide at least one storage provider".to_string(),
                });
            }
            state.stores.iter().map(|s| s.storage.clone()).collect()
        };

        // Step 1: Call make_available() + find_or_insert_user() on each store
        // without holding the state lock.
        let mut init_results: Vec<(Settings, User)> = Vec::with_capacity(providers.len());
        for provider in &providers {
            let settings = provider.make_available().await?;
            let (user, _) = provider.find_or_insert_user(&self.identity_key).await?;
            init_results.push((settings, user));
        }

        // Re-acquire the state lock to write results and partition.
        let mut state = self.state.lock().await;
        for (i, (settings, user)) in init_results.into_iter().enumerate() {
            *state.stores[i].settings.lock().await = Some(settings);
            *state.stores[i].user.lock().await = Some(user);
            state.stores[i].is_available.store(true, Ordering::Release);
        }

        // Step 2: Partition stores — first store is default active
        let num_stores = state.stores.len();
        state.active_index = Some(0);
        state.backup_indices.clear();
        state.conflicting_active_indices.clear();

        for i in 1..num_stores {
            let store_sik = {
                let s = state.stores[i].settings.lock().await;
                s.as_ref()
                    .map(|s| s.storage_identity_key.clone())
                    .unwrap_or_default()
            };
            let store_user_active = {
                let u = state.stores[i].user.lock().await;
                u.as_ref()
                    .map(|u| u.active_storage.clone())
                    .unwrap_or_default()
            };

            // Check if current active is enabled (settings.sik == user.active_storage)
            let current_active_enabled = {
                let active_idx = state.active_index.unwrap();
                let active_sik = {
                    let s = state.stores[active_idx].settings.lock().await;
                    s.as_ref()
                        .map(|s| s.storage_identity_key.clone())
                        .unwrap_or_default()
                };
                let active_user_active = {
                    let u = state.stores[active_idx].user.lock().await;
                    u.as_ref()
                        .map(|u| u.active_storage.clone())
                        .unwrap_or_default()
                };
                active_sik == active_user_active
            };

            if store_user_active == store_sik && !current_active_enabled {
                // Swap: old active becomes backup, this store becomes new active
                let old_active = state.active_index.unwrap();
                state.backup_indices.push(old_active);
                state.active_index = Some(i);
            } else {
                state.backup_indices.push(i);
            }
        }

        // Step 3: Second pass — partition backups into plain backups vs conflicting_actives
        let active_sik = {
            let active_idx = state.active_index.unwrap();
            let s = state.stores[active_idx].settings.lock().await;
            s.as_ref()
                .map(|s| s.storage_identity_key.clone())
                .unwrap_or_default()
        };

        let old_backups = std::mem::take(&mut state.backup_indices);
        for backup_idx in old_backups {
            let backup_user_active = {
                let u = state.stores[backup_idx].user.lock().await;
                u.as_ref()
                    .map(|u| u.active_storage.clone())
                    .unwrap_or_default()
            };
            if backup_user_active != active_sik {
                state.conflicting_active_indices.push(backup_idx);
            } else {
                state.backup_indices.push(backup_idx);
            }
        }

        // Step 4: Mark manager as available
        self.is_available_flag.store(true, Ordering::Release);

        // Return active settings
        let active_idx = state.active_index.unwrap();
        let settings = state.stores[active_idx]
            .settings
            .lock()
            .await
            .clone()
            .unwrap();
        Ok(settings)
    }

    // -----------------------------------------------------------------------
    // Hierarchical lock acquire helpers
    // -----------------------------------------------------------------------

    /// Acquire the reader lock (level 1).
    ///
    /// Auto-initializes via `make_available()` if not yet available.
    /// The reader lock is acquired AFTER initialization so that initialization
    /// itself can serialize via the same reader_lock.
    pub async fn acquire_reader(&self) -> WalletResult<tokio::sync::MutexGuard<'_, ()>> {
        if !self.is_available() {
            // make_available() acquires+releases reader_lock internally for idempotency.
            // After it returns, is_available is true and reader_lock is free.
            self.make_available().await?;
        }
        Ok(self.reader_lock.lock().await)
    }

    /// Acquire the reader + writer locks (levels 1 and 2).
    ///
    /// Auto-initializes via `make_available()` if not yet available.
    pub async fn acquire_writer(
        &self,
    ) -> WalletResult<(
        tokio::sync::MutexGuard<'_, ()>,
        tokio::sync::MutexGuard<'_, ()>,
    )> {
        if !self.is_available() {
            self.make_available().await?;
        }
        let reader = self.reader_lock.lock().await;
        let writer = self.writer_lock.lock().await;
        Ok((reader, writer))
    }

    /// Acquire reader + writer + sync locks (levels 1, 2, and 3).
    ///
    /// Auto-initializes via `make_available()` if not yet available.
    pub async fn acquire_sync(
        &self,
    ) -> WalletResult<(
        tokio::sync::MutexGuard<'_, ()>,
        tokio::sync::MutexGuard<'_, ()>,
        tokio::sync::MutexGuard<'_, ()>,
    )> {
        if !self.is_available() {
            self.make_available().await?;
        }
        let reader = self.reader_lock.lock().await;
        let writer = self.writer_lock.lock().await;
        let sync = self.sync_lock.lock().await;
        Ok((reader, writer, sync))
    }

    /// Acquire all four locks (levels 1, 2, 3, and 4).
    ///
    /// Auto-initializes via `make_available()` if not yet available.
    pub async fn acquire_storage_provider(
        &self,
    ) -> WalletResult<(
        tokio::sync::MutexGuard<'_, ()>,
        tokio::sync::MutexGuard<'_, ()>,
        tokio::sync::MutexGuard<'_, ()>,
        tokio::sync::MutexGuard<'_, ()>,
    )> {
        if !self.is_available() {
            self.make_available().await?;
        }
        let reader = self.reader_lock.lock().await;
        let writer = self.writer_lock.lock().await;
        let sync = self.sync_lock.lock().await;
        let sp = self.sp_lock.lock().await;
        Ok((reader, writer, sync, sp))
    }

    /// Acquire the manager-level spend lock.
    ///
    /// Held across createAction/signAction/internalizeAction/abortAction/
    /// relinquishOutput to serialize UTXO-mutating operations across all
    /// `Wallet` instances sharing this manager. Cross-wallet UTXO contention
    /// on shared storage requires a manager-level lock; per-`Wallet` locks
    /// would not serialize.
    ///
    /// Independent of the hierarchical reader/writer/sync/sp locks: callers
    /// take this lock at the wallet layer, then perform multiple WSM calls
    /// (each of which acquires/releases writer/reader locks) under its
    /// protection. Do not hold it while also holding the reader lock from
    /// this manager — acquire it first (or release reader first).
    ///
    /// Auto-initializes via `make_available()` if not yet available.
    pub async fn acquire_spend_lock(&self) -> WalletResult<tokio::sync::MutexGuard<'_, ()>> {
        if !self.is_available() {
            self.make_available().await?;
        }
        Ok(self.spend_lock.lock().await)
    }

    // -----------------------------------------------------------------------
    // Lifecycle methods
    // -----------------------------------------------------------------------

    /// Destroy all stores, cleaning up all resources.
    pub async fn destroy(&self) -> WalletResult<()> {
        let state = self.state.lock().await;
        for store in &state.stores {
            store.storage.destroy().await?;
        }
        Ok(())
    }

    /// Get wallet services.
    pub async fn get_services_ref(&self) -> WalletResult<Arc<dyn WalletServices>> {
        self.services.lock().await.clone().ok_or_else(|| {
            WalletError::NotImplemented("services not configured on manager".to_string())
        })
    }

    /// Set wallet services.
    pub async fn set_services(&self, services: Arc<dyn WalletServices>) {
        *self.services.lock().await = Some(services);
    }

    // -----------------------------------------------------------------------
    // WalletStorageProvider delegation — read operations
    //
    // All read operations acquire the reader lock (level 1) before delegating
    // to the active provider. This matches the TS `runAsReader` pattern.
    // -----------------------------------------------------------------------

    /// List wallet actions (transactions) with filtering and pagination.
    pub async fn list_actions(
        &self,
        auth: &AuthId,
        args: &ListActionsArgs,
    ) -> WalletResult<ListActionsResult> {
        let _rg = self.acquire_reader().await?;
        let active = self.get_active().await?;
        active.list_actions(auth, args).await
    }

    /// List outputs with basket/tag filtering.
    pub async fn list_outputs(
        &self,
        auth: &AuthId,
        args: &ListOutputsArgs,
    ) -> WalletResult<ListOutputsResult> {
        let _rg = self.acquire_reader().await?;
        let active = self.get_active().await?;
        active.list_outputs(auth, args).await
    }

    /// List certificates with certifier/type filtering.
    pub async fn list_certificates(
        &self,
        auth: &AuthId,
        args: &ListCertificatesArgs,
    ) -> WalletResult<ListCertificatesResult> {
        let _rg = self.acquire_reader().await?;
        let active = self.get_active().await?;
        active.list_certificates(auth, args).await
    }

    /// Find certificates scoped to the given auth identity.
    pub async fn find_certificates_auth(
        &self,
        auth: &AuthId,
        args: &FindCertificatesArgs,
    ) -> WalletResult<Vec<Certificate>> {
        let _rg = self.acquire_reader().await?;
        let active = self.get_active().await?;
        active.find_certificates_auth(auth, args).await
    }

    /// Find output baskets scoped to the given auth identity.
    pub async fn find_output_baskets_auth(
        &self,
        auth: &AuthId,
        args: &FindOutputBasketsArgs,
    ) -> WalletResult<Vec<OutputBasket>> {
        let _rg = self.acquire_reader().await?;
        let active = self.get_active().await?;
        active.find_output_baskets_auth(auth, args).await
    }

    /// Find outputs scoped to the given auth identity.
    pub async fn find_outputs_auth(
        &self,
        auth: &AuthId,
        args: &FindOutputsArgs,
    ) -> WalletResult<Vec<Output>> {
        let _rg = self.acquire_reader().await?;
        let active = self.get_active().await?;
        active.find_outputs_auth(auth, args).await
    }

    /// Find proven transaction requests.
    pub async fn find_proven_tx_reqs(
        &self,
        args: &FindProvenTxReqsArgs,
    ) -> WalletResult<Vec<ProvenTxReq>> {
        let _rg = self.acquire_reader().await?;
        let active = self.get_active().await?;
        active.find_proven_tx_reqs(args).await
    }

    // -----------------------------------------------------------------------
    // WalletStorageProvider delegation — write operations
    //
    // All write operations acquire reader + writer locks (levels 1 and 2)
    // before delegating to the active provider. This matches the TS
    // `runAsWriter` pattern.
    // -----------------------------------------------------------------------

    /// Abort (cancel) a transaction by reference.
    pub async fn abort_action(
        &self,
        auth: &AuthId,
        args: &AbortActionArgs,
    ) -> WalletResult<AbortActionResult> {
        let (_rg, _wg) = self.acquire_writer().await?;
        let active = self.get_active().await?;
        active.abort_action(auth, args).await
    }

    /// Create a new transaction in storage.
    pub async fn create_action(
        &self,
        auth: &AuthId,
        args: &StorageCreateActionArgs,
    ) -> WalletResult<StorageCreateActionResult> {
        let (_rg, _wg) = self.acquire_writer().await?;
        let active = self.get_active().await?;
        active.create_action(auth, args).await
    }

    /// Process (commit) a signed transaction to storage.
    pub async fn process_action(
        &self,
        auth: &AuthId,
        args: &StorageProcessActionArgs,
    ) -> WalletResult<StorageProcessActionResult> {
        let (_rg, _wg) = self.acquire_writer().await?;
        let active = self.get_active().await?;
        active.process_action(auth, args).await
    }

    /// Internalize outputs from an external transaction.
    pub async fn internalize_action(
        &self,
        auth: &AuthId,
        args: &StorageInternalizeActionArgs,
        services: &dyn WalletServices,
    ) -> WalletResult<StorageInternalizeActionResult> {
        let (_rg, _wg) = self.acquire_writer().await?;
        let active = self.get_active().await?;
        active.internalize_action(auth, args, services).await
    }

    /// Insert a certificate scoped to the given auth identity.
    pub async fn insert_certificate_auth(
        &self,
        auth: &AuthId,
        certificate: &Certificate,
    ) -> WalletResult<i64> {
        let (_rg, _wg) = self.acquire_writer().await?;
        let active = self.get_active().await?;
        active.insert_certificate_auth(auth, certificate).await
    }

    /// Relinquish (soft-delete) a certificate by marking it deleted.
    pub async fn relinquish_certificate(
        &self,
        auth: &AuthId,
        args: &RelinquishCertificateArgs,
    ) -> WalletResult<i64> {
        let (_rg, _wg) = self.acquire_writer().await?;
        let active = self.get_active().await?;
        active.relinquish_certificate(auth, args).await
    }

    /// Relinquish (remove from basket) an output by outpoint.
    pub async fn relinquish_output(
        &self,
        auth: &AuthId,
        args: &RelinquishOutputArgs,
    ) -> WalletResult<i64> {
        let (_rg, _wg) = self.acquire_writer().await?;
        let active = self.get_active().await?;
        active.relinquish_output(auth, args).await
    }

    // -----------------------------------------------------------------------
    // Sync-level delegation
    // -----------------------------------------------------------------------

    pub async fn find_or_insert_user(&self, identity_key: &str) -> WalletResult<(User, bool)> {
        let active = self.get_active().await?;
        let result = active.find_or_insert_user(identity_key).await?;

        // Consistency guard: if the manager already has a cached user_id for this identity,
        // verify it matches the returned record. A mismatch indicates a storage corruption
        // or identity key collision that must not silently proceed.
        if let Ok(cached_auth) = self.get_auth(false).await {
            if let Some(cached_id) = cached_auth.user_id {
                if result.0.user_id != cached_id {
                    return Err(WalletError::Internal(
                        "find_or_insert_user: returned user_id does not match cached user_id — identity consistency violation".to_string(),
                    ));
                }
            }
        }

        Ok(result)
    }

    pub async fn get_sync_chunk(&self, args: &RequestSyncChunkArgs) -> WalletResult<SyncChunk> {
        let active = self.get_active().await?;
        active.get_sync_chunk(args).await
    }

    pub async fn process_sync_chunk(
        &self,
        args: &RequestSyncChunkArgs,
        chunk: &SyncChunk,
    ) -> WalletResult<ProcessSyncChunkResult> {
        let active = self.get_active().await?;
        active.process_sync_chunk(args, chunk).await
    }

    // -----------------------------------------------------------------------
    // Low-level CRUD operations (certificates, signer, beef helper use these)
    // -----------------------------------------------------------------------

    pub async fn find_user_by_identity_key(&self, key: &str) -> WalletResult<Option<User>> {
        let active = self.get_active().await?;
        active.find_user_by_identity_key(key).await
    }

    pub async fn find_certificates_storage(
        &self,
        args: &FindCertificatesArgs,
    ) -> WalletResult<Vec<Certificate>> {
        let active = self.get_active().await?;
        active.find_certificates_storage(args).await
    }

    pub async fn find_certificate_fields(
        &self,
        args: &FindCertificateFieldsArgs,
    ) -> WalletResult<Vec<crate::tables::CertificateField>> {
        let active = self.get_active().await?;
        active.find_certificate_fields(args).await
    }

    pub async fn find_outputs_storage(&self, args: &FindOutputsArgs) -> WalletResult<Vec<Output>> {
        let active = self.get_active().await?;
        active.find_outputs_storage(args).await
    }

    /// Find outputs (signer pipeline alias).
    pub async fn find_outputs(&self, args: &FindOutputsArgs) -> WalletResult<Vec<Output>> {
        self.find_outputs_storage(args).await
    }

    /// Update an output by ID.
    pub async fn update_output(&self, id: i64, update: &OutputPartial) -> WalletResult<i64> {
        let active = self.get_active().await?;
        active.update_output(id, update).await
    }

    pub async fn insert_certificate_storage(&self, cert: &Certificate) -> WalletResult<i64> {
        let active = self.get_active().await?;
        active.insert_certificate_storage(cert).await
    }

    pub async fn insert_certificate_field_storage(
        &self,
        field: &crate::tables::CertificateField,
    ) -> WalletResult<()> {
        let active = self.get_active().await?;
        active.insert_certificate_field_storage(field).await
    }

    // -----------------------------------------------------------------------
    // Internal storage operations (monitor, signer, beef helper use these)
    //
    // These delegate to the active WalletStorageProvider's internal methods.
    // The active provider (SqliteStorage) implements these via the blanket impl.
    // StorageClient returns NotImplemented for these methods.
    // -----------------------------------------------------------------------

    pub async fn find_proven_txs(&self, args: &FindProvenTxsArgs) -> WalletResult<Vec<ProvenTx>> {
        let active = self.get_active().await?;
        active.find_proven_txs(args).await
    }

    pub async fn find_transactions(
        &self,
        args: &FindTransactionsArgs,
    ) -> WalletResult<Vec<Transaction>> {
        let active = self.get_active().await?;
        active.find_transactions(args).await
    }

    pub async fn update_proven_tx_req(
        &self,
        id: i64,
        update: &ProvenTxReqPartial,
    ) -> WalletResult<i64> {
        let active = self.get_active().await?;
        active.update_proven_tx_req(id, update).await
    }

    pub async fn update_proven_tx(&self, id: i64, update: &ProvenTxPartial) -> WalletResult<i64> {
        let active = self.get_active().await?;
        active.update_proven_tx(id, update).await
    }

    pub async fn update_transaction(
        &self,
        id: i64,
        update: &TransactionPartial,
    ) -> WalletResult<i64> {
        let active = self.get_active().await?;
        active.update_transaction(id, update).await
    }

    pub async fn update_transaction_status(
        &self,
        txid: &str,
        new_status: TransactionStatus,
    ) -> WalletResult<()> {
        let active = self.get_active().await?;
        active.update_transaction_status(txid, new_status).await
    }

    pub async fn update_proven_tx_req_with_new_proven_tx(
        &self,
        req_id: i64,
        proven_tx: &ProvenTx,
    ) -> WalletResult<i64> {
        let active = self.get_active().await?;
        active
            .update_proven_tx_req_with_new_proven_tx(req_id, proven_tx)
            .await
    }

    // -----------------------------------------------------------------------
    // Transaction management + trx-aware CRUD passthroughs
    //
    // These let callers (e.g. handle_permanent_broadcast_failure) group a
    // sequence of writes under one atomic DB transaction on the active
    // provider. Callers are responsible for matching begin/commit (or
    // rollback) calls and for holding the trx only across the active
    // provider returned by `get_active()`.
    // -----------------------------------------------------------------------

    pub async fn begin_transaction(&self) -> WalletResult<TrxToken> {
        let active = self.get_active().await?;
        active.begin_transaction().await
    }

    pub async fn commit_transaction(&self, trx: TrxToken) -> WalletResult<()> {
        let active = self.get_active().await?;
        active.commit_transaction(trx).await
    }

    pub async fn rollback_transaction(&self, trx: TrxToken) -> WalletResult<()> {
        let active = self.get_active().await?;
        active.rollback_transaction(trx).await
    }

    pub async fn find_outputs_trx(
        &self,
        args: &FindOutputsArgs,
        trx: Option<&TrxToken>,
    ) -> WalletResult<Vec<Output>> {
        let active = self.get_active().await?;
        active.find_outputs_trx(args, trx).await
    }

    pub async fn find_transactions_trx(
        &self,
        args: &FindTransactionsArgs,
        trx: Option<&TrxToken>,
    ) -> WalletResult<Vec<Transaction>> {
        let active = self.get_active().await?;
        active.find_transactions_trx(args, trx).await
    }

    pub async fn find_proven_tx_reqs_trx(
        &self,
        args: &FindProvenTxReqsArgs,
        trx: Option<&TrxToken>,
    ) -> WalletResult<Vec<ProvenTxReq>> {
        let active = self.get_active().await?;
        active.find_proven_tx_reqs_trx(args, trx).await
    }

    pub async fn update_output_trx(
        &self,
        id: i64,
        update: &OutputPartial,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64> {
        let active = self.get_active().await?;
        active.update_output_trx(id, update, trx).await
    }

    pub async fn update_proven_tx_req_trx(
        &self,
        id: i64,
        update: &ProvenTxReqPartial,
        trx: Option<&TrxToken>,
    ) -> WalletResult<i64> {
        let active = self.get_active().await?;
        active.update_proven_tx_req_trx(id, update, trx).await
    }

    pub async fn update_transaction_status_trx(
        &self,
        txid: &str,
        new_status: TransactionStatus,
        trx: Option<&TrxToken>,
    ) -> WalletResult<()> {
        let active = self.get_active().await?;
        active
            .update_transaction_status_trx(txid, new_status, trx)
            .await
    }

    /// Restore every output consumed by `tx_id` (rows with `spent_by = tx_id`)
    /// back to `spendable=true, spent_by=NULL`. Callers that previously relied
    /// on the implicit cascade in `update_transaction_status(.., Failed, ..)`
    /// must now call this explicitly after the status update.
    pub async fn restore_consumed_inputs(&self, tx_id: i64) -> WalletResult<u64> {
        let active = self.get_active().await?;
        active.restore_consumed_inputs_trx(tx_id, None).await
    }

    /// Trx-scoped variant of `restore_consumed_inputs`.
    pub async fn restore_consumed_inputs_trx(
        &self,
        tx_id: i64,
        trx: Option<&TrxToken>,
    ) -> WalletResult<u64> {
        let active = self.get_active().await?;
        active.restore_consumed_inputs_trx(tx_id, trx).await
    }

    pub async fn insert_monitor_event(&self, event: &MonitorEvent) -> WalletResult<i64> {
        let active = self.get_active().await?;
        active.insert_monitor_event(event).await
    }

    pub async fn delete_monitor_events_before_id(
        &self,
        event_name: &str,
        before_id: i64,
    ) -> WalletResult<u64> {
        let active = self.get_active().await?;
        active
            .delete_monitor_events_before_id(event_name, before_id)
            .await
    }

    pub async fn find_monitor_events(
        &self,
        args: &FindMonitorEventsArgs,
    ) -> WalletResult<Vec<MonitorEvent>> {
        let active = self.get_active().await?;
        active.find_monitor_events(args).await
    }

    pub async fn count_monitor_events(&self, args: &FindMonitorEventsArgs) -> WalletResult<i64> {
        let active = self.get_active().await?;
        active.count_monitor_events(args).await
    }

    pub async fn admin_stats(&self, auth_id: &str) -> WalletResult<AdminStatsResult> {
        let active = self.get_active().await?;
        active.admin_stats(auth_id).await
    }

    pub async fn purge_data(&self, params: &PurgeParams) -> WalletResult<String> {
        let active = self.get_active().await?;
        active.purge_data(params).await
    }

    pub async fn review_status(&self, aged_limit: chrono::NaiveDateTime) -> WalletResult<String> {
        let active = self.get_active().await?;
        active.review_status(aged_limit).await
    }

    pub async fn get_storage_identity_key(&self) -> WalletResult<String> {
        let active = self.get_active().await?;
        active.get_storage_identity_key().await
    }

    // -----------------------------------------------------------------------
    // TS-parity getter methods
    // -----------------------------------------------------------------------

    /// Returns metadata for all registered storage providers.
    ///
    /// Matches TS `getStores()`: iterates all stores and returns a `WalletStorageInfo`
    /// for each, including which is active, which are backups, and which are conflicting.
    pub async fn get_stores(&self) -> Vec<WalletStorageInfo> {
        let state = self.state.lock().await;
        let mut result = Vec::with_capacity(state.stores.len());

        let is_active_enabled = {
            if let Some(idx) = state.active_index {
                let sik = state.stores[idx]
                    .settings
                    .lock()
                    .await
                    .as_ref()
                    .map(|s| s.storage_identity_key.clone())
                    .unwrap_or_default();
                let user_active = state.stores[idx]
                    .user
                    .lock()
                    .await
                    .as_ref()
                    .map(|u| u.active_storage.clone())
                    .unwrap_or_default();
                state.conflicting_active_indices.is_empty() && sik == user_active
            } else {
                false
            }
        };

        for (i, store) in state.stores.iter().enumerate() {
            let settings_guard = store.settings.lock().await;
            let user_guard = store.user.lock().await;

            let storage_identity_key = settings_guard
                .as_ref()
                .map(|s| s.storage_identity_key.clone())
                .unwrap_or_default();
            let storage_name = settings_guard
                .as_ref()
                .map(|s| s.storage_name.clone())
                .unwrap_or_default();
            let user_id = user_guard.as_ref().map(|u| u.user_id);
            let endpoint_url = store.storage.get_endpoint_url();

            let is_active = state.active_index == Some(i);
            let is_backup = state.backup_indices.contains(&i);
            let is_conflicting = state.conflicting_active_indices.contains(&i);
            // A store is "enabled" only when it is the active store and active is enabled.
            let is_enabled = is_active && is_active_enabled;

            result.push(WalletStorageInfo {
                storage_identity_key,
                storage_name,
                user_id,
                is_active,
                is_enabled,
                is_backup,
                is_conflicting,
                endpoint_url,
            });
        }

        result
    }

    /// Returns the remote endpoint URL for the store with the given identity key.
    ///
    /// Matches TS `getStoreEndpointURL(storageIdentityKey)`.
    /// Returns None for local SQLite stores and for unknown identity keys.
    pub async fn get_store_endpoint_url(&self, storage_identity_key: &str) -> Option<String> {
        let state = self.state.lock().await;
        for store in &state.stores {
            let sik = store
                .settings
                .lock()
                .await
                .as_ref()
                .map(|s| s.storage_identity_key.clone())
                .unwrap_or_default();
            if sik == storage_identity_key {
                return store.storage.get_endpoint_url();
            }
        }
        None
    }

    // -----------------------------------------------------------------------
    // Reprove methods (chain reorganization proof re-validation)
    // -----------------------------------------------------------------------

    /// Re-validate the merkle proof for a single ProvenTx record.
    ///
    /// Matches TS `reproveProven(ptx, noUpdate?)`:
    /// - Fetches a fresh merkle path from WalletServices.
    /// - Validates it against the current chain tracker.
    /// - If valid and `no_update` is false, persists the updated record to active storage.
    /// - Returns a `ReproveProvenResult` indicating updated/unchanged/unavailable.
    ///
    /// This method must be called while the StorageProvider lock is already held
    /// (the caller's responsibility — `reprove_header` acquires it before calling here).
    pub async fn reprove_proven(
        &self,
        ptx: &ProvenTx,
        no_update: bool,
    ) -> WalletResult<ReproveProvenResult> {
        let services = self.get_services_ref().await?;

        // Attempt to get a fresh merkle proof for this transaction.
        let merkle_result = services.get_merkle_path(&ptx.txid, false).await;

        // If no proof available, return unavailable.
        let (merkle_path_bytes, header) = match (merkle_result.merkle_path, merkle_result.header) {
            (Some(mp), Some(hdr)) => (mp, hdr),
            _ => {
                return Ok(ReproveProvenResult {
                    updated: None,
                    unchanged: false,
                    unavailable: true,
                });
            }
        };

        // Check if proof data is unchanged.
        let height = header.height as i32;
        let block_hash = header.hash.clone();
        let merkle_root = header.merkle_root.clone();

        if ptx.height == height
            && ptx.block_hash == block_hash
            && ptx.merkle_root == merkle_root
            && ptx.merkle_path == merkle_path_bytes
        {
            return Ok(ReproveProvenResult {
                updated: None,
                unchanged: true,
                unavailable: false,
            });
        }

        // Build the updated record.
        let mut updated_ptx = ptx.clone();
        updated_ptx.height = height;
        updated_ptx.block_hash = block_hash.clone();
        updated_ptx.merkle_root = merkle_root.clone();
        updated_ptx.merkle_path = merkle_path_bytes;
        // index comes from the chain tracker header if available; fall back to existing.
        // (TS updates index via MerklePath.index — we leave it unchanged unless we have
        // the actual index from a decoded path, which requires the bsv library. Acceptable
        // for now: reprove_header primarily needs height + block_hash updated.)

        let log_update = format!(
            "reproved txid={} old_block={} new_block={} height={}",
            ptx.txid, ptx.block_hash, block_hash, height
        );

        // Persist update unless caller requested dry-run mode.
        if !no_update {
            let active = self.get_active().await?;
            let partial = ProvenTxPartial {
                height: Some(updated_ptx.height),
                block_hash: Some(updated_ptx.block_hash.clone()),
                ..Default::default()
            };
            active.update_proven_tx(ptx.proven_tx_id, &partial).await?;
        }

        Ok(ReproveProvenResult {
            updated: Some(ReproveProvenUpdate {
                update: updated_ptx,
                log_update,
            }),
            unchanged: false,
            unavailable: false,
        })
    }

    /// Re-prove all ProvenTx records associated with a deactivated block hash.
    ///
    /// Matches TS `reproveHeader(deactivatedHash)`:
    /// - Runs under StorageProvider lock (all four locks).
    /// - Finds all ProvenTx records with block_hash == deactivated_hash.
    /// - Calls `reprove_proven` for each (no_update=true for individual calls).
    /// - Bulk-updates all successfully reproved records in a single pass.
    /// - Returns `ReproveHeaderResult` with updated/unchanged/unavailable partitions.
    pub async fn reprove_header(
        &self,
        deactivated_hash: &str,
    ) -> WalletResult<ReproveHeaderResult> {
        // Acquire all four locks (StorageProvider level).
        let _guards = self.acquire_storage_provider().await?;

        let proven_txs = {
            let active = self.get_active().await?;
            active
                .find_proven_txs(&FindProvenTxsArgs {
                    partial: ProvenTxPartial {
                        block_hash: Some(deactivated_hash.to_string()),
                        ..Default::default()
                    },
                    ..Default::default()
                })
                .await?
        };

        let mut updated_vec = Vec::new();
        let mut unchanged_vec = Vec::new();
        let mut unavailable_vec = Vec::new();

        let mut updates_to_persist: Vec<(i64, ProvenTxPartial, ProvenTx)> = Vec::new();

        for ptx in &proven_txs {
            // Call reprove_proven in dry-run mode (no_update=true); we bulk-persist below.
            let result = self.reprove_proven(ptx, true).await?;

            if result.unavailable {
                unavailable_vec.push(ptx.clone());
            } else if result.unchanged {
                unchanged_vec.push(ptx.clone());
            } else if let Some(ru) = result.updated {
                let partial = ProvenTxPartial {
                    height: Some(ru.update.height),
                    block_hash: Some(ru.update.block_hash.clone()),
                    ..Default::default()
                };
                updates_to_persist.push((ptx.proven_tx_id, partial, ru.update));
            }
        }

        // Bulk-persist all updates.
        if !updates_to_persist.is_empty() {
            let active = self.get_active().await?;
            for (id, partial, updated_ptx) in updates_to_persist {
                active.update_proven_tx(id, &partial).await?;
                updated_vec.push(updated_ptx);
            }
        }

        Ok(ReproveHeaderResult {
            updated: updated_vec,
            unchanged: unchanged_vec,
            unavailable: unavailable_vec,
        })
    }

    // -----------------------------------------------------------------------
    // Sync loop methods (Plan 02)
    // -----------------------------------------------------------------------

    /// Sync entities from `reader` into `writer` using chunked getSyncChunk/processSyncChunk.
    ///
    /// Loops until `processSyncChunk` returns `done=true`, accumulating insert/update counts.
    /// Caller must hold the sync lock (acquired by `update_backups` or `sync_from_reader`).
    ///
    /// # Arguments
    /// * `writer` - Storage receiving the sync data.
    /// * `reader` - Storage providing the sync data.
    /// * `writer_settings` - Cached settings for the writer (to_storage_identity_key).
    /// * `reader_settings` - Cached settings for the reader (from_storage_identity_key).
    /// * `prog_log` - Optional progress callback. If Some, called after each chunk with a
    ///   progress string. Returns the accumulated log of all callback return values.
    ///
    /// Returns `(total_inserts, total_updates, log_string)`.
    pub async fn sync_to_writer(
        &self,
        writer: &dyn WalletStorageProvider,
        reader: &dyn WalletStorageProvider,
        writer_settings: &Settings,
        reader_settings: &Settings,
        prog_log: Option<&(dyn Fn(&str) -> String + Send + Sync)>,
    ) -> WalletResult<(i64, i64, String)> {
        let auth = self.get_auth(false).await?;
        let mut total_inserts: i64 = 0;
        let mut total_updates: i64 = 0;
        let mut log = String::new();

        let mut i = 0usize;
        loop {
            // Re-query sync state on every iteration — processSyncChunk updates it.
            let (ss, _) = writer
                .find_or_insert_sync_state_auth(
                    &auth,
                    &reader_settings.storage_identity_key,
                    &reader_settings.storage_name,
                )
                .await?;

            let args = make_request_sync_chunk_args(
                &ss,
                &self.identity_key,
                &writer_settings.storage_identity_key,
            )?;

            let chunk = reader.get_sync_chunk(&args).await?;
            let r = writer.process_sync_chunk(&args, &chunk).await?;

            total_inserts += r.inserts;
            total_updates += r.updates;

            if let Some(cb) = prog_log {
                let msg = format!(
                    "sync chunk {} inserts={} updates={}",
                    i, r.inserts, r.updates
                );
                let s = cb(&msg);
                log.push_str(&s);
                log.push('\n');
            }

            if r.done {
                break;
            }
            i += 1;
        }

        Ok((total_inserts, total_updates, log))
    }

    /// Sync entities from `reader` into `writer` with active_storage override protection.
    ///
    /// Identical to `sync_to_writer` except that before calling `writer.process_sync_chunk`,
    /// the `chunk.user.active_storage` field is overridden with the writer's cached
    /// `user.active_storage`. This prevents the reader's active_storage from clobbering
    /// the writer's during sync (Pitfall 5 from the research).
    ///
    /// `reader_identity_key` must match this manager's identity key; otherwise returns
    /// `WERR_UNAUTHORIZED`. This mirrors the TS WalletStorageManager.syncFromReader check.
    pub async fn sync_from_reader(
        &self,
        reader_identity_key: &str,
        writer: &dyn WalletStorageProvider,
        reader: &dyn WalletStorageProvider,
        writer_settings: &Settings,
        reader_settings: &Settings,
        prog_log: Option<&(dyn Fn(&str) -> String + Send + Sync)>,
    ) -> WalletResult<(i64, i64, String)> {
        if reader_identity_key != self.identity_key {
            return Err(WalletError::Unauthorized(
                "sync_from_reader: reader identity key does not match manager identity key"
                    .to_string(),
            ));
        }
        let auth = self.get_auth(false).await?;
        let mut total_inserts: i64 = 0;
        let mut total_updates: i64 = 0;
        let mut log = String::new();

        // Fetch writer's cached user.active_storage for the override below.
        let writer_active_storage = {
            let state = self.state.lock().await;
            if let Some(idx) = state.active_index {
                state.stores[idx]
                    .get_user_cached()
                    .await
                    .map(|u| u.active_storage)
            } else {
                None
            }
        };

        let mut i = 0usize;
        loop {
            let (ss, _) = writer
                .find_or_insert_sync_state_auth(
                    &auth,
                    &reader_settings.storage_identity_key,
                    &reader_settings.storage_name,
                )
                .await?;

            let args = make_request_sync_chunk_args(
                &ss,
                &self.identity_key,
                &writer_settings.storage_identity_key,
            )?;

            let mut chunk = reader.get_sync_chunk(&args).await?;

            // Pitfall 5: override chunk.user.active_storage with writer's cached value
            // to prevent the reader's active_storage from clobbering the writer's.
            if let (Some(ref mut chunk_user), Some(ref writer_as)) =
                (&mut chunk.user, &writer_active_storage)
            {
                chunk_user.active_storage = writer_as.clone();
            }

            let r = writer.process_sync_chunk(&args, &chunk).await?;

            total_inserts += r.inserts;
            total_updates += r.updates;

            if let Some(cb) = prog_log {
                let msg = format!(
                    "sync chunk {} inserts={} updates={}",
                    i, r.inserts, r.updates
                );
                let s = cb(&msg);
                log.push_str(&s);
                log.push('\n');
            }

            if r.done {
                break;
            }
            i += 1;
        }

        Ok((total_inserts, total_updates, log))
    }

    /// Sync all backup stores from the active store.
    ///
    /// Acquires sync-level locks, iterates `backup_indices`, and calls
    /// `sync_to_writer(backup, active, ...)` for each backup. Per-backup errors
    /// are logged as warnings and do not block other backups.
    ///
    /// Returns `(total_inserts, total_updates, accumulated_log)`.
    pub async fn update_backups(
        &self,
        prog_log: Option<&(dyn Fn(&str) -> String + Send + Sync)>,
    ) -> WalletResult<(i64, i64, String)> {
        // Acquire sync-level lock (reader + writer + sync).
        let _guards = self.acquire_sync().await?;

        let (active_arc, active_settings, backup_arcs_and_settings) = {
            let state = self.state.lock().await;
            let idx = state.require_active()?;
            let active_arc = state.stores[idx].storage.clone();
            let active_settings =
                state.stores[idx]
                    .get_settings_cached()
                    .await
                    .ok_or_else(|| {
                        WalletError::InvalidOperation("Active settings not cached".to_string())
                    })?;

            let mut backups = Vec::with_capacity(state.backup_indices.len());
            for &bi in &state.backup_indices {
                let backup_arc = state.stores[bi].storage.clone();
                let backup_settings = state.stores[bi].get_settings_cached().await;
                backups.push((backup_arc, backup_settings));
            }
            (active_arc, active_settings, backups)
        };

        let mut total_inserts: i64 = 0;
        let mut total_updates: i64 = 0;
        let mut full_log = String::new();

        for (backup_arc, maybe_backup_settings) in &backup_arcs_and_settings {
            let backup_settings = match maybe_backup_settings {
                Some(s) => s.clone(),
                None => {
                    warn!("update_backups: backup has no cached settings, skipping");
                    continue;
                }
            };

            match self
                .sync_to_writer(
                    backup_arc.as_ref(),
                    active_arc.as_ref(),
                    &backup_settings,
                    &active_settings,
                    prog_log,
                )
                .await
            {
                Ok((ins, upd, log)) => {
                    total_inserts += ins;
                    total_updates += upd;
                    full_log.push_str(&log);
                }
                Err(e) => {
                    // Per-backup failure must not block other backups (TS parity).
                    warn!(
                        "update_backups: sync to backup '{}' failed: {e}",
                        backup_settings.storage_identity_key
                    );
                }
            }
        }

        Ok((total_inserts, total_updates, full_log))
    }

    /// Switch the active store to the one identified by `storage_identity_key`.
    ///
    /// Implements the TS `setActive(storageIdentityKey, progLog?)` algorithm (8 steps):
    ///
    /// 1. Validate the target store is registered.
    /// 2. Early-return "unchanged" if already active and enabled.
    /// 3. Acquire sync lock.
    /// 4. Merge any conflicting-active stores into the new active via `sync_to_writer`.
    /// 5. Determine backup_source (new_active if conflicts existed, else current_active).
    /// 6. Call provider-level `set_active` on backup_source to persist `user.active_storage`.
    /// 7. Propagate state to all other stores via `sync_to_writer`.
    /// 8. Reset `is_available` and re-partition via `do_make_available`.
    ///
    /// Returns accumulated progress log string on success.
    pub async fn set_active(
        &self,
        storage_identity_key: &str,
        prog_log: Option<&(dyn Fn(&str) -> String + Send + Sync)>,
    ) -> WalletResult<String> {
        // Step 0: Auto-init if needed
        if !self.is_available() {
            self.make_available().await?;
        }

        // Step 1: Validate target store exists — find its index
        let new_active_idx = {
            let state = self.state.lock().await;
            let mut found = None;
            for (i, store) in state.stores.iter().enumerate() {
                let sik = store
                    .get_settings_cached()
                    .await
                    .map(|s| s.storage_identity_key)
                    .unwrap_or_default();
                if sik == storage_identity_key {
                    found = Some(i);
                    break;
                }
            }
            found
        };

        let new_active_idx = new_active_idx.ok_or_else(|| WalletError::InvalidParameter {
            parameter: "storage_identity_key".to_string(),
            must_be: format!(
                "registered with this WalletStorageManager. {} does not match any managed store.",
                storage_identity_key
            ),
        })?;

        // Step 2: Early return if already active and enabled
        let current_active_sik = self.get_active_store().await?;
        if current_active_sik == storage_identity_key && self.is_active_enabled().await {
            return Ok(format!("{} unchanged\n", storage_identity_key));
        }

        let log = {
            // Step 3: Acquire sync lock (reader + writer + sync)
            // IMPORTANT: acquire_sync already holds reader_lock — must use do_make_available()
            // at re-partition step, NOT make_available().
            let _guards = self.acquire_sync().await?;

            let mut log = String::new();

            // Step 4: Conflict resolution — merge each conflicting active into the new active
            // Clone Arcs and Settings before releasing state lock (never hold state lock across async I/O)
            let had_conflicts;
            let (new_active_arc, new_active_settings) = {
                let state = self.state.lock().await;
                let arc = state.stores[new_active_idx].storage.clone();
                let settings = state.stores[new_active_idx]
                    .get_settings_cached()
                    .await
                    .ok_or_else(|| {
                        WalletError::InvalidOperation(
                            "set_active: new active settings not cached".to_string(),
                        )
                    })?;
                (arc, settings)
            };

            let conflict_sources = {
                let state = self.state.lock().await;
                if state.conflicting_active_indices.is_empty() {
                    had_conflicts = false;
                    Vec::new()
                } else {
                    had_conflicts = true;
                    // Build list: all conflicting_active_indices + active_index, minus new_active_idx
                    let mut sources = state.conflicting_active_indices.clone();
                    if let Some(ai) = state.active_index {
                        sources.push(ai);
                    }
                    // Remove the new_active_idx from this list (it's the destination)
                    sources.retain(|&i| i != new_active_idx);

                    // Clone arcs and settings for each conflict source
                    let mut result = Vec::with_capacity(sources.len());
                    for idx in sources {
                        let arc = state.stores[idx].storage.clone();
                        let settings = state.stores[idx].get_settings_cached().await;
                        result.push((arc, settings));
                    }
                    result
                }
            };

            // Now do the async sync_to_writer calls outside the state lock
            for (conflict_arc, maybe_conflict_settings) in &conflict_sources {
                let conflict_settings = match maybe_conflict_settings {
                    Some(s) => s.clone(),
                    None => {
                        warn!("set_active: conflict source has no cached settings, skipping");
                        continue;
                    }
                };
                // Merge conflict into new_active (new_active is the writer/destination)
                let (ins, upd, chunk_log) = self
                    .sync_to_writer(
                        new_active_arc.as_ref(),
                        conflict_arc.as_ref(),
                        &new_active_settings,
                        &conflict_settings,
                        prog_log,
                    )
                    .await?;
                if let Some(cb) = prog_log {
                    let msg = format!(
                        "set_active: merged conflict {} inserts={} updates={}",
                        conflict_settings.storage_identity_key, ins, upd
                    );
                    let s = cb(&msg);
                    log.push_str(&s);
                    log.push('\n');
                }
                log.push_str(&chunk_log);
            }

            // Step 5: Determine backup_source
            // If conflicts existed → backup_source = new_active
            // Else → backup_source = current_active
            let (backup_source_arc, backup_source_settings, backup_source_user_id) = {
                let state = self.state.lock().await;
                if had_conflicts {
                    // backup_source is the new active
                    let user_id = state.stores[new_active_idx]
                        .get_user_cached()
                        .await
                        .map(|u| u.user_id)
                        .ok_or_else(|| {
                            WalletError::InvalidOperation(
                                "set_active: new active user not cached".to_string(),
                            )
                        })?;
                    (new_active_arc.clone(), new_active_settings.clone(), user_id)
                } else {
                    // backup_source is the current active
                    let ai = state.require_active()?;
                    let arc = state.stores[ai].storage.clone();
                    let settings =
                        state.stores[ai]
                            .get_settings_cached()
                            .await
                            .ok_or_else(|| {
                                WalletError::InvalidOperation(
                                    "set_active: current active settings not cached".to_string(),
                                )
                            })?;
                    let user_id = state.stores[ai]
                        .get_user_cached()
                        .await
                        .map(|u| u.user_id)
                        .ok_or_else(|| {
                            WalletError::InvalidOperation(
                                "set_active: current active user not cached".to_string(),
                            )
                        })?;
                    (arc, settings, user_id)
                }
            };

            // Step 6: Provider-level set_active on backup_source
            // This persists user.active_storage = storage_identity_key in the backup_source DB
            let auth = AuthId {
                identity_key: self.identity_key.clone(),
                user_id: Some(backup_source_user_id),
                is_active: Some(false),
            };
            backup_source_arc
                .set_active(&auth, storage_identity_key)
                .await?;

            if let Some(cb) = prog_log {
                let msg = format!(
                    "set_active: provider-level set_active on {} complete",
                    backup_source_settings.storage_identity_key
                );
                let s = cb(&msg);
                log.push_str(&s);
                log.push('\n');
            }

            // Step 7: Propagate state to all other stores via sync_to_writer
            // First update in-memory user.active_storage cache for all stores
            {
                let state = self.state.lock().await;
                for store in &state.stores {
                    let mut user_guard = store.user.lock().await;
                    if let Some(ref mut u) = *user_guard {
                        u.active_storage = storage_identity_key.to_string();
                    }
                }
            }

            // Collect list of (store_arc, store_settings) for stores != backup_source
            let backup_source_sik = backup_source_settings.storage_identity_key.clone();
            let propagation_targets = {
                let state = self.state.lock().await;
                let mut targets = Vec::new();
                for store in &state.stores {
                    let sik = store
                        .get_settings_cached()
                        .await
                        .map(|s| s.storage_identity_key)
                        .unwrap_or_default();
                    if sik != backup_source_sik {
                        let arc = store.storage.clone();
                        let settings = store.get_settings_cached().await;
                        targets.push((arc, settings));
                    }
                }
                targets
            };

            for (store_arc, maybe_store_settings) in &propagation_targets {
                let store_settings = match maybe_store_settings {
                    Some(s) => s.clone(),
                    None => {
                        warn!("set_active: propagation target has no cached settings, skipping");
                        continue;
                    }
                };
                let (ins, upd, chunk_log) = self
                    .sync_to_writer(
                        store_arc.as_ref(),
                        backup_source_arc.as_ref(),
                        &store_settings,
                        &backup_source_settings,
                        prog_log,
                    )
                    .await?;
                if let Some(cb) = prog_log {
                    let msg = format!(
                        "set_active: propagated to {} inserts={} updates={}",
                        store_settings.storage_identity_key, ins, upd
                    );
                    let s = cb(&msg);
                    log.push_str(&s);
                    log.push('\n');
                }
                log.push_str(&chunk_log);
            }

            // Step 8: Re-partition — reset flag and re-run do_make_available
            // MUST use do_make_available(), NOT make_available(): acquire_sync holds reader_lock
            // and make_available() would try to acquire it again → deadlock.
            self.is_available_flag.store(false, Ordering::Release);
            self.do_make_available().await?;

            if let Some(cb) = prog_log {
                let msg = format!(
                    "set_active: complete, new active is {}",
                    storage_identity_key
                );
                let s = cb(&msg);
                log.push_str(&s);
                log.push('\n');
            }

            log
        };
        // Sync lock released — push changes to backups. Propagate the
        // error so callers learn when replication failed instead of silently
        // leaving backups stale. Matches Calhooon's pattern and the TS
        // setActive contract (backup sync is part of the switch).
        self.update_backups(None).await?;

        Ok(log)
    }

    /// Add a new storage provider at runtime.
    ///
    /// Acquires all four locks (storage-provider level), appends the provider
    /// to the `stores` vec, resets `is_available`, and re-runs `make_available()`
    /// to re-partition the updated store list.
    ///
    /// Matches TS `addWalletStorageProvider(storage)`.
    pub async fn add_wallet_storage_provider(
        &self,
        provider: Arc<dyn WalletStorageProvider>,
    ) -> WalletResult<()> {
        {
            // Acquire all four locks (storage-provider level).
            let _guards = self.acquire_storage_provider().await?;

            {
                let mut state = self.state.lock().await;
                state.stores.push(ManagedStorage::new(provider));
                // Reset is_available so make_available() will re-run the full partition.
                self.is_available_flag.store(false, Ordering::Release);
            }

            // Re-run make_available() to initialize the new store and re-partition.
            // We must NOT call self.make_available() here because it would try to
            // acquire reader_lock which is already held by acquire_storage_provider.
            // Instead call do_make_available() which runs without acquiring reader_lock.
            self.do_make_available().await?;
        }
        // All locks released — sync active state to the newly added backup.
        // Propagate the error so callers learn when the newly-added backup
        // failed to initialize rather than leaving it silently out of sync.
        self.update_backups(None).await?;

        Ok(())
    }

    // -----------------------------------------------------------------------
    // Low-level seeding helpers (used by tests and setup code)
    //
    // These bypass auth and write directly to the active provider.
    // -----------------------------------------------------------------------

    /// Find or create an output basket by user ID and name.
    pub async fn find_output_baskets(
        &self,
        args: &FindOutputBasketsArgs,
    ) -> WalletResult<Vec<OutputBasket>> {
        let active = self.get_active().await?;
        active.find_output_baskets(args).await
    }

    /// Insert an output basket directly (no auth check).
    pub async fn insert_output_basket(&self, basket: &OutputBasket) -> WalletResult<i64> {
        let active = self.get_active().await?;
        active.insert_output_basket(basket).await
    }

    /// Insert a transaction directly (no auth check).
    pub async fn insert_transaction(&self, tx: &Transaction) -> WalletResult<i64> {
        let active = self.get_active().await?;
        active.insert_transaction(tx).await
    }

    /// Insert an output directly (no auth check).
    pub async fn insert_output(&self, output: &Output) -> WalletResult<i64> {
        let active = self.get_active().await?;
        active.insert_output(output).await
    }
}