darkpool-client 0.1.0

DarkPool SDK: notes, proofs, UTXO state, and transaction building
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
//! Unified client interface for the `DarkPool` protocol.
//! Combines key management, UTXO tracking, proof generation, and transport into a single API.

use ethers::prelude::*;
#[cfg(feature = "mixnet")]
use ethers::types::transaction::eip2718::TypedTransaction;
use ethers::types::{Address, U256};
use std::collections::HashSet;
use std::sync::Arc;
use std::time::Duration;
use thiserror::Error;
use tracing::{debug, info, warn};

use crate::builder::TransactionBuilder;
#[cfg(feature = "mixnet")]
use crate::economics::PriceData;
use crate::identity::DarkAccount;
use crate::key_repository::KeyRepository;
use crate::merkle_tree::LocalMerkleTree;
use crate::note_factory::{ChangeNoteResult, NoteFactory, SpendingInputs};
#[cfg(feature = "mixnet")]
use crate::note_processor::WalletNote;
use crate::prover::ClientProver;
use crate::scan_engine::{ScanEngine, ScanResult};
use crate::utxo_store::{OwnedNote, UtxoStore};
use nox_core::traits::interfaces::IProverService;

#[cfg(feature = "mixnet")]
use nox_client::mixnet_client::{MixnetClient, MixnetClientError};

/// Privacy client errors
#[derive(Debug, Error)]
pub enum PrivacyClientError {
    #[error("Insufficient balance: need {needed}, have {have}")]
    InsufficientBalance { needed: U256, have: U256 },
    #[error("No spendable notes found")]
    NoSpendableNotes,
    #[error("Note selection failed: {0}")]
    NoteSelectionFailed(String),
    #[error("Proof generation failed: {0}")]
    ProofFailed(String),
    #[error("Transaction failed: {0}")]
    TransactionFailed(String),
    #[error("Scan error: {0}")]
    ScanError(String),
    #[error("Provider error: {0}")]
    ProviderError(String),
    #[error("Tree sync mismatch: local root {local:?} != on-chain root {onchain:?}")]
    TreeMismatch { local: U256, onchain: U256 },
    #[error("Mixnet error: {0}")]
    MixnetError(String),
    #[error("Invalid memo: {0}")]
    InvalidMemo(String),
    #[error("Cryptographic operation failed: {0}")]
    CryptoFailed(String),
    #[error("Configuration error: {0}")]
    Config(String),
    #[error("Gas fee {fee} exceeds payment note value {note_value}")]
    GasFeeExceedsNoteValue { fee: U256, note_value: U256 },
    #[error("Persistence error: {0}")]
    Persistence(#[from] crate::persistence::PersistenceError),
}

#[cfg(feature = "mixnet")]
impl From<MixnetClientError> for PrivacyClientError {
    fn from(e: MixnetClientError) -> Self {
        PrivacyClientError::MixnetError(e.to_string())
    }
}

pub type PrivacyClientConfig = crate::config::DarkPoolConfig;
pub use crate::config::PrivacyTxResult;

/// Determines how a transaction is submitted to the blockchain.
pub enum Transport<'a> {
    Direct,
    /// Gas payment ZK proof + mixnet submission. Change notes are discoverable
    /// via pre-registered ephemeral keys.
    #[cfg(feature = "mixnet")]
    PaidMixnet {
        client: &'a MixnetClient,
        payment_asset: Address,
        prices: &'a PriceData,
        relayer_address: Address,
    },
    /// User signs TX locally; exit node calls `eth_sendRawTransaction`.
    /// IP privacy via Sphinx routing, no gas notes needed.
    #[cfg(feature = "mixnet")]
    SignedBroadcast {
        client: &'a MixnetClient,
    },
    /// Phantom variant to avoid unused lifetime warning when mixnet feature is disabled.
    #[cfg(not(feature = "mixnet"))]
    #[doc(hidden)]
    _Phantom(std::marker::PhantomData<&'a ()>),
}

struct SubmitResult {
    tx_hash: H256,
    block_num: u64,
    gas_used: U256,
    payment_nullifier: Option<U256>,
    /// Used for local state sync; `eth_getLogs` returns 0 on Anvil for recently mined blocks.
    receipt_logs: Vec<ethers::types::Log>,
}

/// Main entry point for `DarkPool` operations. Queries can optionally route through the Mixnet.
pub struct PrivacyClient<M: Middleware + Clone + 'static> {
    signer: Arc<SignerMiddleware<M, LocalWallet>>,
    keys: KeyRepository,
    utxos: UtxoStore,
    tree: LocalMerkleTree,
    builder: TransactionBuilder,
    note_factory: NoteFactory,
    config: PrivacyClientConfig,
    last_synced_block: u64,
}

impl<M: Middleware + Clone + 'static> PrivacyClient<M> {
    pub async fn new(
        provider: Arc<M>,
        wallet: LocalWallet,
        dark_account: DarkAccount,
        config: PrivacyClientConfig,
        prover: Arc<dyn IProverService>,
    ) -> Result<Self, PrivacyClientError> {
        let timeout_ms = config.provider_timeout_ms;
        let chain_id =
            tokio::time::timeout(Duration::from_millis(timeout_ms), provider.get_chainid())
                .await
                .map_err(|_| {
                    PrivacyClientError::ProviderError(format!(
                        "get_chainid timed out after {timeout_ms}ms"
                    ))
                })?
                .map_err(|e| PrivacyClientError::ProviderError(format!("get_chainid: {e}")))?
                .as_u64();

        let wallet_with_chain = wallet.with_chain_id(chain_id);
        let signer = Arc::new(SignerMiddleware::new(
            (*provider).clone(),
            wallet_with_chain,
        ));

        if config.darkpool_address == Address::zero() {
            return Err(PrivacyClientError::Config(
                "darkpool_address is zero -- all transactions would be sent to the zero address"
                    .into(),
            ));
        }

        let mut keys = KeyRepository::new(dark_account, config.compliance_pk);
        // Pre-register a default lookahead window of incoming keys so that
        // the scan engine can detect transfers on the very first sync.
        // Without this, the recipient_key_map is empty and no transfer memos
        // can be matched until notes are found (chicken-and-egg problem).
        keys.advance_incoming_keys(crate::key_repository::DEFAULT_LOOKAHEAD);
        let client_prover = Arc::new(ClientProver::with_service(prover));

        let mut builder_config = config.builder_config.clone();
        builder_config.compliance_pk = config.compliance_pk;
        builder_config.darkpool_address = config.darkpool_address;
        let builder = TransactionBuilder::new(client_prover, builder_config);

        let note_factory = NoteFactory::new(config.compliance_pk);

        info!(
            "Privacy Client initialized. DarkPool: {:?}",
            config.darkpool_address
        );

        Ok(Self {
            signer,
            keys,
            utxos: UtxoStore::new(),
            tree: LocalMerkleTree::new(),
            builder,
            note_factory,
            config,
            last_synced_block: 0,
        })
    }

    /// Alias for [`new()`](Self::new).
    pub async fn with_prover(
        provider: Arc<M>,
        wallet: LocalWallet,
        dark_account: DarkAccount,
        config: PrivacyClientConfig,
        prover: Arc<dyn IProverService>,
    ) -> Result<Self, PrivacyClientError> {
        Self::new(provider, wallet, dark_account, config, prover).await
    }

    #[must_use]
    pub fn balance(&self, asset: Address) -> U256 {
        self.utxos.get_balance(asset)
    }

    #[must_use]
    pub fn merkle_root(&self) -> U256 {
        self.tree.root()
    }

    #[must_use]
    pub fn note_count(&self) -> usize {
        self.utxos.count()
    }

    pub fn receiving_key(&mut self) -> Result<(U256, U256), PrivacyClientError> {
        self.keys
            .get_public_incoming_key()
            .map_err(|e| PrivacyClientError::CryptoFailed(e.to_string()))
    }

    pub fn advance_keys(&mut self, count: u64) {
        self.keys.advance_ephemeral_keys(count);
        self.keys.advance_incoming_keys(count);
    }

    /// Atomic write (temp file + rename). Pending spends are not persisted.
    pub fn save_state(&self, path: &std::path::Path) -> Result<(), PrivacyClientError> {
        crate::persistence::save_wallet_state(path, &self.utxos, &self.tree, self.last_synced_block)
            .map_err(PrivacyClientError::Persistence)
    }

    /// Returns `Ok(true)` if loaded, `Ok(false)` if file doesn't exist (fresh wallet).
    pub fn load_state(&mut self, path: &std::path::Path) -> Result<bool, PrivacyClientError> {
        match crate::persistence::load_wallet_state(path)? {
            Some((utxos, tree, block)) => {
                self.utxos = utxos;
                self.tree = tree;
                self.last_synced_block = block;
                Ok(true)
            }
            None => Ok(false),
        }
    }

    /// Requires prior ERC-20 approval for the `DarkPool` contract.
    pub async fn deposit(
        &mut self,
        amount: U256,
        asset: Address,
    ) -> Result<PrivacyTxResult, PrivacyClientError> {
        info!("Depositing {} of {:?}", amount, asset);

        let deposit_result = self
            .note_factory
            .create_deposit_note(amount, asset, &mut self.keys)
            .map_err(|e| PrivacyClientError::ProofFailed(e.to_string()))?;

        let proof_bundle = self
            .builder
            .build_deposit(&deposit_result)
            .await
            .map_err(|e| PrivacyClientError::ProofFailed(e.to_string()))?;

        debug!(
            "Deposit proof generated. Commitment: {:?}",
            deposit_result.commitment
        );

        let tx = TransactionRequest::new()
            .to(self.config.darkpool_address)
            .data(proof_bundle.calldata.clone());

        let pending = self
            .timed_provider_call(
                "deposit send_transaction",
                self.signer.send_transaction(tx, None),
            )
            .await?;

        let receipt = self
            .timed_provider_call("deposit pending confirmation", pending)
            .await?
            .ok_or_else(|| {
                PrivacyClientError::TransactionFailed("No receipt received".to_string())
            })?;

        info!(
            "Deposit successful. TxHash: {:?}, Commitment: {:?}",
            receipt.transaction_hash, deposit_result.commitment
        );

        let leaf_index = self.tree.insert(deposit_result.commitment);

        let shared_secret = crate::crypto_helpers::derive_shared_secret_bjj(
            deposit_result.ephemeral_sk,
            self.config.compliance_pk,
        )
        .map_err(|e| PrivacyClientError::CryptoFailed(format!("ECDH failed: {e}")))?;

        let block_num = if let Some(b) = receipt.block_number {
            b.as_u64()
        } else {
            warn!(
                "Receipt missing block_number for tx {:?}, using last_synced_block={}",
                receipt.transaction_hash, self.last_synced_block
            );
            self.last_synced_block
        };

        self.utxos.add_note(
            OwnedNote {
                plaintext: deposit_result.note.clone(),
                commitment: deposit_result.commitment,
                leaf_index,
                spending_secret: shared_secret,
                is_transfer: false,
                received_block: block_num,
            },
            crate::crypto_helpers::derive_nullifier_path_a(deposit_result.note.nullifier),
        );

        self.last_synced_block = block_num;

        Ok(PrivacyTxResult {
            tx_hash: receipt.transaction_hash,
            new_commitments: vec![deposit_result.commitment],
            spent_nullifiers: vec![],
            gas_used: receipt.gas_used.unwrap_or_else(|| {
                warn!(
                    "Receipt missing gas_used for tx {:?}",
                    receipt.transaction_hash
                );
                U256::zero()
            }),
        })
    }

    pub async fn withdraw_with_transport(
        &mut self,
        transport: &Transport<'_>,
        amount: U256,
        asset: Address,
        recipient: Address,
        intent_hash: Option<U256>,
    ) -> Result<PrivacyTxResult, PrivacyClientError> {
        info!("Withdrawing {} of {:?} to {:?}", amount, asset, recipient);

        let balance = self.balance(asset);
        if balance < amount {
            return Err(PrivacyClientError::InsufficientBalance {
                needed: amount,
                have: balance,
            });
        }

        let selected = self
            .utxos
            .select_notes(asset, amount)
            .ok_or(PrivacyClientError::NoSpendableNotes)?;
        let note = (*selected
            .first()
            .ok_or(PrivacyClientError::NoSpendableNotes)?)
        .clone();
        let excluded: HashSet<U256> = selected.iter().map(|n| n.commitment).collect();
        drop(selected);

        let spending_inputs = self.create_spending_inputs(&note)?;
        let nullifier_hash = Self::derive_nullifier_hash(&note);

        for c in &excluded {
            self.utxos.mark_pending_spend(c);
        }

        let change_value = note.plaintext.value.saturating_sub(amount);
        let change_result = if change_value.is_zero() {
            None
        } else {
            Some(
                self.note_factory
                    .create_change_note(change_value, asset, &mut self.keys)
                    .map_err(|e| PrivacyClientError::ProofFailed(e.to_string()))?,
            )
        };

        self.ensure_synced_with_transport(transport).await?;

        let merkle_root = self.tree.root();
        let proof_bundle = self
            .builder
            .build_withdraw(
                &spending_inputs,
                amount,
                recipient,
                merkle_root,
                change_result.as_ref(),
                intent_hash,
                0,
            )
            .await
            .map_err(|e| PrivacyClientError::ProofFailed(e.to_string()))?;

        debug!("Withdraw proof generated for {} to {:?}", amount, recipient);

        let submit = self
            .submit_and_confirm(
                transport,
                proof_bundle.calldata.clone(),
                merkle_root,
                &excluded,
                U256::from(self.config.gas_limits.withdraw),
            )
            .await?;

        let mut new_commitments = vec![];
        if let Some(ref change) = change_result {
            new_commitments.push(change.commitment);
        }

        if submit.payment_nullifier.is_some() {
            // Gas payment change note is inserted on-chain BEFORE action output notes
            self.sync_from_receipt_logs(&submit.receipt_logs, submit.block_num)?;
        } else {
            if let Some(ref change) = change_result {
                self.register_self_note(change, submit.block_num)?;
            }
            self.last_synced_block = submit.block_num;
        }

        self.utxos.mark_spent(nullifier_hash);
        let mut spent_nullifiers = vec![nullifier_hash];
        if let Some(pn) = submit.payment_nullifier {
            self.utxos.mark_spent(pn);
            spent_nullifiers.push(pn);
        }

        for c in &excluded {
            self.utxos.clear_pending_spend(c);
        }

        Ok(PrivacyTxResult {
            tx_hash: submit.tx_hash,
            new_commitments,
            spent_nullifiers,
            gas_used: submit.gas_used,
        })
    }

    pub async fn split_with_transport(
        &mut self,
        transport: &Transport<'_>,
        amount_a: U256,
        amount_b: U256,
        asset: Address,
    ) -> Result<PrivacyTxResult, PrivacyClientError> {
        let total = amount_a + amount_b;
        info!("Splitting note: {} + {} of {:?}", amount_a, amount_b, asset);

        // Split circuit enforces strict conservation: input == output_a + output_b
        let note = self
            .utxos
            .select_note_exact(asset, total)
            .ok_or(PrivacyClientError::NoSpendableNotes)?
            .clone();
        let excluded: HashSet<U256> = [note.commitment].into_iter().collect();

        let spending_inputs = self.create_spending_inputs(&note)?;
        let nullifier_hash = Self::derive_nullifier_hash(&note);

        for c in &excluded {
            self.utxos.mark_pending_spend(c);
        }

        let (note_a, note_b) = self
            .note_factory
            .create_split_notes(amount_a, amount_b, asset, &mut self.keys)
            .map_err(|e| PrivacyClientError::ProofFailed(e.to_string()))?;

        self.ensure_synced_with_transport(transport).await?;

        let merkle_root = self.tree.root();
        let proof_bundle = self
            .builder
            .build_split(&spending_inputs, merkle_root, &note_a, &note_b, 0)
            .await
            .map_err(|e| PrivacyClientError::ProofFailed(e.to_string()))?;

        debug!(
            "Split proof generated. Outputs: {:?}, {:?}",
            note_a.commitment, note_b.commitment
        );

        let submit = self
            .submit_and_confirm(
                transport,
                proof_bundle.calldata.clone(),
                merkle_root,
                &excluded,
                U256::from(self.config.gas_limits.split),
            )
            .await?;

        if submit.payment_nullifier.is_some() {
            // Gas payment change note is inserted on-chain BEFORE action output notes
            self.sync_from_receipt_logs(&submit.receipt_logs, submit.block_num)?;
        } else {
            self.register_self_note(&note_a, submit.block_num)?;
            self.register_self_note(&note_b, submit.block_num)?;
            self.last_synced_block = submit.block_num;
        }

        self.utxos.mark_spent(nullifier_hash);
        let mut spent_nullifiers = vec![nullifier_hash];
        if let Some(pn) = submit.payment_nullifier {
            self.utxos.mark_spent(pn);
            spent_nullifiers.push(pn);
        }

        for c in &excluded {
            self.utxos.clear_pending_spend(c);
        }

        Ok(PrivacyTxResult {
            tx_hash: submit.tx_hash,
            new_commitments: vec![note_a.commitment, note_b.commitment],
            spent_nullifiers,
            gas_used: submit.gas_used,
        })
    }

    pub async fn join_with_transport(
        &mut self,
        transport: &Transport<'_>,
        asset: Address,
    ) -> Result<PrivacyTxResult, PrivacyClientError> {
        info!("Joining notes for {:?}", asset);

        let unspent: Vec<_> = self
            .utxos
            .get_unspent()
            .into_iter()
            .filter(|n| n.plaintext.asset_id == crate::crypto_helpers::address_to_field(asset))
            .cloned()
            .collect();
        if unspent.len() < 2 {
            return Err(PrivacyClientError::NoteSelectionFailed(
                "Need at least 2 notes to join".to_string(),
            ));
        }

        let note_a = &unspent[0];
        let note_b = &unspent[1];

        let excluded: HashSet<U256> = [note_a.commitment, note_b.commitment].into_iter().collect();

        let spending_inputs_a = self.create_spending_inputs(note_a)?;
        let spending_inputs_b = self.create_spending_inputs(note_b)?;
        let nullifier_a = Self::derive_nullifier_hash(note_a);
        let nullifier_b = Self::derive_nullifier_hash(note_b);

        for c in &excluded {
            self.utxos.mark_pending_spend(c);
        }

        let total_value = note_a.plaintext.value + note_b.plaintext.value;

        let output = self
            .note_factory
            .create_join_output_note(total_value, asset, &mut self.keys)
            .map_err(|e| PrivacyClientError::ProofFailed(e.to_string()))?;

        self.ensure_synced_with_transport(transport).await?;

        let merkle_root = self.tree.root();
        let proof_bundle = self
            .builder
            .build_join(
                &spending_inputs_a,
                &spending_inputs_b,
                merkle_root,
                &output,
                0,
            )
            .await
            .map_err(|e| PrivacyClientError::ProofFailed(e.to_string()))?;

        debug!("Join proof generated. Output: {:?}", output.commitment);

        let submit = self
            .submit_and_confirm(
                transport,
                proof_bundle.calldata.clone(),
                merkle_root,
                &excluded,
                U256::from(self.config.gas_limits.join),
            )
            .await?;

        if submit.payment_nullifier.is_some() {
            // Gas payment change note is inserted on-chain BEFORE action output notes
            self.sync_from_receipt_logs(&submit.receipt_logs, submit.block_num)?;
        } else {
            self.register_self_note(&output, submit.block_num)?;
            self.last_synced_block = submit.block_num;
        }

        self.utxos.mark_spent(nullifier_a);
        self.utxos.mark_spent(nullifier_b);
        let mut spent_nullifiers = vec![nullifier_a, nullifier_b];
        if let Some(pn) = submit.payment_nullifier {
            self.utxos.mark_spent(pn);
            spent_nullifiers.push(pn);
        }

        for c in &excluded {
            self.utxos.clear_pending_spend(c);
        }

        Ok(PrivacyTxResult {
            tx_hash: submit.tx_hash,
            new_commitments: vec![output.commitment],
            spent_nullifiers,
            gas_used: submit.gas_used,
        })
    }

    #[allow(clippy::too_many_arguments)]
    pub async fn transfer_with_transport(
        &mut self,
        transport: &Transport<'_>,
        amount: U256,
        asset: Address,
        recipient_b: (U256, U256),
        recipient_p: (U256, U256),
        recipient_proof: crate::proof_inputs::DLEQProof,
    ) -> Result<PrivacyTxResult, PrivacyClientError> {
        use crate::crypto_helpers::random_bjj_scalar;
        use crate::proof_inputs::NotePlaintext;

        info!("Transferring {} of {:?}", amount, asset);

        let balance = self.balance(asset);
        if balance < amount {
            return Err(PrivacyClientError::InsufficientBalance {
                needed: amount,
                have: balance,
            });
        }

        let selected = self
            .utxos
            .select_notes(asset, amount)
            .ok_or(PrivacyClientError::NoSpendableNotes)?;
        let note = (*selected
            .first()
            .ok_or(PrivacyClientError::NoSpendableNotes)?)
        .clone();
        let excluded: HashSet<U256> = selected.iter().map(|n| n.commitment).collect();
        drop(selected);

        let spending_inputs = self.create_spending_inputs(&note)?;
        let nullifier_hash = Self::derive_nullifier_hash(&note);

        for c in &excluded {
            self.utxos.mark_pending_spend(c);
        }

        // Memo note: nullifier=0, secret=0 because recipient uses Path B for spending
        let memo_note = NotePlaintext {
            asset_id: crate::crypto_helpers::address_to_field(asset),
            value: amount,
            secret: U256::zero(),
            nullifier: U256::zero(),
            timelock: U256::zero(),
            hashlock: U256::zero(),
        };
        let memo_ephemeral_sk = random_bjj_scalar();

        let change_value = note.plaintext.value.saturating_sub(amount);
        let change_note = NotePlaintext::random(change_value, asset);
        let change_ephemeral_sk = random_bjj_scalar();

        self.ensure_synced_with_transport(transport).await?;

        let merkle_root = self.tree.root();
        let proof_bundle = self
            .builder
            .build_transfer(
                &spending_inputs,
                merkle_root,
                recipient_b,
                recipient_p,
                recipient_proof,
                memo_note,
                memo_ephemeral_sk,
                change_note.clone(),
                change_ephemeral_sk,
                0,
            )
            .await
            .map_err(|e| PrivacyClientError::ProofFailed(e.to_string()))?;

        debug!(
            "Transfer proof generated. Memo: {:?}, Change: {:?}",
            proof_bundle.memo_commitment, proof_bundle.change_commitment
        );

        let submit = self
            .submit_and_confirm(
                transport,
                proof_bundle.calldata.clone(),
                merkle_root,
                &excluded,
                U256::from(self.config.gas_limits.transfer),
            )
            .await?;

        let mut new_commitments = vec![proof_bundle.memo_commitment];
        if !change_value.is_zero() {
            new_commitments.push(proof_bundle.change_commitment);
        }

        if submit.payment_nullifier.is_some() {
            // Gas payment change note is inserted on-chain BEFORE action output notes
            self.sync_from_receipt_logs(&submit.receipt_logs, submit.block_num)?;
        } else {
            // Memo FIRST, then change (matches DarkPool.sol insertion order)
            self.tree.insert(proof_bundle.memo_commitment);

            if !change_value.is_zero() {
                let change_leaf_index = self.tree.insert(proof_bundle.change_commitment);

                let change_shared_secret = crate::crypto_helpers::derive_shared_secret_bjj(
                    change_ephemeral_sk,
                    self.config.compliance_pk,
                )
                .map_err(|e| PrivacyClientError::CryptoFailed(format!("ECDH failed: {e}")))?;

                self.utxos.add_note(
                    OwnedNote {
                        plaintext: change_note.clone(),
                        commitment: proof_bundle.change_commitment,
                        leaf_index: change_leaf_index,
                        spending_secret: change_shared_secret,
                        is_transfer: false,
                        received_block: submit.block_num,
                    },
                    crate::crypto_helpers::derive_nullifier_path_a(change_note.nullifier),
                );
            }

            self.last_synced_block = submit.block_num;
        }

        self.utxos.mark_spent(nullifier_hash);
        let mut spent_nullifiers = vec![nullifier_hash];
        if let Some(pn) = submit.payment_nullifier {
            self.utxos.mark_spent(pn);
            spent_nullifiers.push(pn);
        }

        for c in &excluded {
            self.utxos.clear_pending_spend(c);
        }

        Ok(PrivacyTxResult {
            tx_hash: submit.tx_hash,
            new_commitments,
            spent_nullifiers,
            gas_used: submit.gas_used,
        })
    }

    #[allow(clippy::too_many_arguments)]
    pub async fn public_claim(
        &mut self,
        memo_id: U256,
        value: U256,
        asset: Address,
        timelock: U256,
        owner_pk: (U256, U256),
        salt: U256,
        recipient_sk: U256,
    ) -> Result<PrivacyTxResult, PrivacyClientError> {
        self.public_claim_with_transport(
            &Transport::Direct,
            memo_id,
            value,
            asset,
            timelock,
            owner_pk,
            salt,
            recipient_sk,
        )
        .await
    }

    /// `DarkPool.publicClaim()` verifies proof only (no `msg.sender` check), so it can be relayed.
    #[allow(clippy::too_many_arguments)]
    pub async fn public_claim_with_transport(
        &mut self,
        transport: &Transport<'_>,
        memo_id: U256,
        value: U256,
        asset: Address,
        timelock: U256,
        owner_pk: (U256, U256),
        salt: U256,
        recipient_sk: U256,
    ) -> Result<PrivacyTxResult, PrivacyClientError> {
        use crate::crypto_helpers::{address_to_field, calculate_public_memo_id};

        info!("Claiming public memo via transport: {:?}", memo_id);

        let asset_id = address_to_field(asset);
        let calculated_memo_id =
            calculate_public_memo_id(value, asset_id, timelock, owner_pk.0, owner_pk.1, salt);
        if calculated_memo_id != memo_id {
            return Err(PrivacyClientError::InvalidMemo(
                "Calculated memo ID does not match provided memo ID".to_string(),
            ));
        }

        let note_out_result = self
            .note_factory
            .create_change_note(value, asset, &mut self.keys)
            .map_err(|e| PrivacyClientError::ProofFailed(e.to_string()))?;

        self.ensure_synced_with_transport(transport).await?;

        let merkle_root = self.tree.root();

        let proof_bundle = self
            .builder
            .build_public_claim(
                memo_id,
                value,
                asset_id,
                timelock,
                owner_pk,
                salt,
                recipient_sk,
                &note_out_result,
            )
            .await
            .map_err(|e| PrivacyClientError::ProofFailed(e.to_string()))?;

        debug!(
            "Public claim proof generated. Output commitment: {:?}",
            proof_bundle.note_out.commitment
        );

        let excluded = HashSet::new();

        let submit = self
            .submit_and_confirm(
                transport,
                proof_bundle.calldata.clone(),
                merkle_root,
                &excluded,
                U256::from(self.config.gas_limits.public_claim),
            )
            .await?;

        if submit.payment_nullifier.is_some() {
            self.sync_from_receipt_logs(&submit.receipt_logs, submit.block_num)?;
        } else {
            self.register_self_note(&note_out_result, submit.block_num)?;
            self.last_synced_block = submit.block_num;
        }

        let mut spent_nullifiers = vec![];
        if let Some(pn) = submit.payment_nullifier {
            self.utxos.mark_spent(pn);
            spent_nullifiers.push(pn);
        }

        Ok(PrivacyTxResult {
            tx_hash: submit.tx_hash,
            new_commitments: vec![note_out_result.commitment],
            spent_nullifiers,
            gas_used: submit.gas_used,
        })
    }

    pub async fn sync(&mut self) -> Result<ScanResult, PrivacyClientError> {
        let current_block = self
            .timed_provider_call("sync get_block_number", self.signer.get_block_number())
            .await?
            .as_u64();

        if current_block <= self.last_synced_block {
            return Ok(ScanResult::default());
        }

        info!(
            "Syncing from block {} to {}",
            self.last_synced_block + 1,
            current_block
        );

        let taken_utxos = std::mem::take(&mut self.utxos);
        let taken_tree = std::mem::take(&mut self.tree);

        let provider_arc = Arc::new(self.signer.inner().clone());
        let mut scan_engine = ScanEngine::with_state(
            provider_arc,
            self.config.darkpool_address,
            self.keys.clone(),
            taken_utxos,
            taken_tree,
            self.config.compliance_pk,
            self.last_synced_block,
        );

        let result = match scan_engine
            .scan_blocks(self.last_synced_block + 1, current_block)
            .await
        {
            Ok(result) => result,
            Err(e) => {
                self.utxos = std::mem::take(scan_engine.utxos_mut());
                self.tree = std::mem::take(scan_engine.tree_mut());
                return Err(PrivacyClientError::ScanError(e.to_string()));
            }
        };

        self.utxos = std::mem::take(scan_engine.utxos_mut());
        self.tree = std::mem::take(scan_engine.tree_mut());
        self.last_synced_block = current_block;

        if !result.new_notes.is_empty() {
            self.advance_keys(10);
        }

        info!(
            "Sync complete: {} new notes, {} nullifiers spent",
            result.new_notes.len(),
            result.spent_nullifiers.len()
        );

        // Post-sync root verification: check that the local root is recognized
        // on-chain. This is non-fatal because the chain may have advanced past
        // our sync range (new leaves inserted after `current_block`). The pre-proof
        // check in `ensure_synced_with_transport` is the hard gate.
        if let Err(e) = self.verify_root_is_known().await {
            warn!("Post-sync root verification failed (may need re-sync): {e}");
        }

        Ok(result)
    }

    /// Syncs via mixnet when transport carries a mixnet client (avoids metadata leaks).
    #[cfg_attr(not(feature = "mixnet"), allow(unused_variables))]
    async fn ensure_synced_with_transport(
        &mut self,
        transport: &Transport<'_>,
    ) -> Result<(), PrivacyClientError> {
        #[cfg(feature = "mixnet")]
        match transport {
            Transport::PaidMixnet { client, .. } | Transport::SignedBroadcast { client } => {
                let current_block = client.block_number().await?;
                if current_block > self.last_synced_block {
                    let behind = current_block - self.last_synced_block;
                    warn!(
                        "Merkle tree is {} blocks behind chain tip ({} vs {}). Auto-syncing via mixnet.",
                        behind, self.last_synced_block, current_block
                    );
                    self.sync_via_mixnet(client).await?;
                }
                self.verify_root_is_known().await?;
                return Ok(());
            }
            Transport::Direct => {}
        }

        let current_block = self
            .timed_provider_call(
                "ensure_synced get_block_number",
                self.signer.get_block_number(),
            )
            .await?
            .as_u64();

        if current_block > self.last_synced_block {
            let behind = current_block - self.last_synced_block;
            warn!(
                "Merkle tree is {} blocks behind chain tip ({} vs {}). Auto-syncing before proof generation.",
                behind, self.last_synced_block, current_block
            );
            self.sync().await?;
        }

        self.verify_root_is_known().await?;
        Ok(())
    }

    /// Wraps provider calls with a timeout to prevent indefinite hangs.
    async fn timed_provider_call<T, E, F>(
        &self,
        op_name: &str,
        future: F,
    ) -> Result<T, PrivacyClientError>
    where
        F: std::future::Future<Output = Result<T, E>>,
        E: std::fmt::Display,
    {
        tokio::time::timeout(
            Duration::from_millis(self.config.provider_timeout_ms),
            future,
        )
        .await
        .map_err(|_| {
            PrivacyClientError::ProviderError(format!(
                "{op_name} timed out after {}ms",
                self.config.provider_timeout_ms
            ))
        })?
        .map_err(|e| PrivacyClientError::ProviderError(format!("{op_name}: {e}")))
    }

    /// Sync from TX receipt logs (bypasses `eth_getLogs` which returns 0 on Anvil).
    fn sync_from_receipt_logs(
        &mut self,
        logs: &[ethers::types::Log],
        block_num: u64,
    ) -> Result<ScanResult, PrivacyClientError> {
        if logs.is_empty() {
            warn!("Paid TX receipt has 0 logs -- TX may have reverted silently");
            return Ok(ScanResult::default());
        }

        let provider_arc = Arc::new(self.signer.inner().clone());
        let mut scan_engine = ScanEngine::with_state(
            provider_arc,
            self.config.darkpool_address,
            self.keys.clone(),
            std::mem::take(&mut self.utxos),
            std::mem::take(&mut self.tree),
            self.config.compliance_pk,
            self.last_synced_block,
        );

        let result = scan_engine
            .process_logs_directly(logs)
            .map_err(|e| PrivacyClientError::ScanError(e.to_string()))?;

        self.utxos = std::mem::take(scan_engine.utxos_mut());
        self.tree = std::mem::take(scan_engine.tree_mut());
        self.last_synced_block = block_num;

        if !result.new_notes.is_empty() {
            self.advance_keys(10);
        }

        info!(
            "Receipt log sync: {} new notes, {} nullifiers spent, {} commitments added",
            result.new_notes.len(),
            result.spent_nullifiers.len(),
            result.new_commitments.len()
        );

        Ok(result)
    }

    #[cfg(feature = "mixnet")]
    pub async fn sync_via_mixnet(
        &mut self,
        mixnet: &MixnetClient,
    ) -> Result<ScanResult, PrivacyClientError> {
        let current_block = mixnet.block_number().await?;

        if current_block <= self.last_synced_block {
            return Ok(ScanResult::default());
        }

        info!(
            "Syncing via mixnet from block {} to {}",
            self.last_synced_block + 1,
            current_block
        );

        let filter = Filter::new()
            .address(self.config.darkpool_address)
            .from_block(self.last_synced_block + 1)
            .to_block(current_block);

        let logs = mixnet.get_logs(&filter).await?;

        info!("Received {} logs via mixnet", logs.len());

        let provider_arc = Arc::new(self.signer.inner().clone());
        let mut scan_engine = ScanEngine::with_state(
            provider_arc,
            self.config.darkpool_address,
            self.keys.clone(),
            std::mem::take(&mut self.utxos),
            std::mem::take(&mut self.tree),
            self.config.compliance_pk,
            self.last_synced_block,
        );

        let result = scan_engine
            .process_logs_directly(&logs)
            .map_err(|e| PrivacyClientError::ScanError(e.to_string()))?;

        self.utxos = std::mem::take(scan_engine.utxos_mut());
        self.tree = std::mem::take(scan_engine.tree_mut());
        self.last_synced_block = current_block;

        if !result.new_notes.is_empty() {
            self.advance_keys(10);
        }

        info!(
            "Mixnet sync complete: {} new notes, {} nullifiers spent",
            result.new_notes.len(),
            result.spent_nullifiers.len()
        );

        Ok(result)
    }

    /// Direct-submission wrapper for [`withdraw_with_transport`](Self::withdraw_with_transport).
    pub async fn withdraw(
        &mut self,
        amount: U256,
        asset: Address,
        recipient: Address,
        intent_hash: Option<U256>,
    ) -> Result<PrivacyTxResult, PrivacyClientError> {
        self.withdraw_with_transport(&Transport::Direct, amount, asset, recipient, intent_hash)
            .await
    }

    /// Direct-submission wrapper for [`split_with_transport`](Self::split_with_transport).
    pub async fn split(
        &mut self,
        amount_a: U256,
        amount_b: U256,
        asset: Address,
    ) -> Result<PrivacyTxResult, PrivacyClientError> {
        self.split_with_transport(&Transport::Direct, amount_a, amount_b, asset)
            .await
    }

    /// Direct-submission wrapper for [`join_with_transport`](Self::join_with_transport).
    pub async fn join(&mut self, asset: Address) -> Result<PrivacyTxResult, PrivacyClientError> {
        self.join_with_transport(&Transport::Direct, asset).await
    }

    /// Direct-submission wrapper for [`transfer_with_transport`](Self::transfer_with_transport).
    pub async fn transfer(
        &mut self,
        amount: U256,
        asset: Address,
        recipient_b: (U256, U256),
        recipient_p: (U256, U256),
        recipient_proof: crate::proof_inputs::DLEQProof,
    ) -> Result<PrivacyTxResult, PrivacyClientError> {
        self.transfer_with_transport(
            &Transport::Direct,
            amount,
            asset,
            recipient_b,
            recipient_p,
            recipient_proof,
        )
        .await
    }

    /// Path A for self-created notes, Path B for received transfer notes.
    fn derive_nullifier_hash(note: &OwnedNote) -> U256 {
        if note.is_transfer {
            crate::crypto_helpers::derive_nullifier_path_b(
                note.spending_secret,
                note.commitment,
                note.leaf_index,
            )
        } else {
            crate::crypto_helpers::derive_nullifier_path_a(note.plaintext.nullifier)
        }
    }

    /// Insert a self-created note (Path A) into the Merkle tree and UTXO store.
    fn register_self_note(
        &mut self,
        note_result: &ChangeNoteResult,
        block_num: u64,
    ) -> Result<u64, PrivacyClientError> {
        let leaf_index = self.tree.insert(note_result.commitment);

        let shared_secret = crate::crypto_helpers::derive_shared_secret_bjj(
            note_result.ephemeral_sk,
            self.config.compliance_pk,
        )
        .map_err(|e| PrivacyClientError::CryptoFailed(format!("ECDH failed: {e}")))?;

        self.utxos.add_note(
            OwnedNote {
                plaintext: note_result.note.clone(),
                commitment: note_result.commitment,
                leaf_index,
                spending_secret: shared_secret,
                is_transfer: false,
                received_block: block_num,
            },
            crate::crypto_helpers::derive_nullifier_path_a(note_result.note.nullifier),
        );

        Ok(leaf_index)
    }

    /// Submit and confirm via the appropriate transport.
    #[cfg_attr(not(feature = "mixnet"), allow(unused_variables))]
    async fn submit_and_confirm(
        &mut self,
        transport: &Transport<'_>,
        calldata: Bytes,
        merkle_root: U256,
        excluded: &HashSet<U256>,
        gas_limit: U256,
    ) -> Result<SubmitResult, PrivacyClientError> {
        match transport {
            Transport::Direct => {
                let tx = TransactionRequest::new()
                    .to(self.config.darkpool_address)
                    .data(calldata);

                let pending = self
                    .timed_provider_call(
                        "submit send_transaction",
                        self.signer.send_transaction(tx, None),
                    )
                    .await?;

                let receipt = self
                    .timed_provider_call("submit pending confirmation", pending)
                    .await?
                    .ok_or_else(|| {
                        PrivacyClientError::TransactionFailed("No receipt received".to_string())
                    })?;

                info!(
                    "Transaction confirmed. TxHash: {:?}",
                    receipt.transaction_hash
                );

                let block_num = if let Some(b) = receipt.block_number {
                    b.as_u64()
                } else {
                    warn!(
                        "Receipt missing block_number for tx {:?}, using last_synced_block={}",
                        receipt.transaction_hash, self.last_synced_block
                    );
                    self.last_synced_block
                };
                let gas_used = receipt.gas_used.unwrap_or_else(|| {
                    warn!(
                        "Receipt missing gas_used for tx {:?}",
                        receipt.transaction_hash
                    );
                    U256::zero()
                });

                Ok(SubmitResult {
                    tx_hash: receipt.transaction_hash,
                    block_num,
                    gas_used,
                    payment_nullifier: None,
                    receipt_logs: vec![],
                })
            }
            #[cfg(feature = "mixnet")]
            Transport::PaidMixnet {
                client,
                payment_asset,
                prices,
                relayer_address,
            } => {
                // ZK verification gas varies hugely between Anvil and mainnet
                let simulated_gas = self
                    .timed_provider_call(
                        "action eth_estimateGas",
                        self.signer.estimate_gas(
                            &ethers::types::transaction::eip2718::TypedTransaction::Legacy(
                                TransactionRequest::new()
                                    .to(self.config.darkpool_address)
                                    .data(calldata.clone()),
                            ),
                            None,
                        ),
                    )
                    .await
                    .unwrap_or_else(|e| {
                        warn!(
                            "eth_estimateGas failed ({}), falling back to config gas_limit={}",
                            e, gas_limit
                        );
                        gas_limit
                    });

                // 20% buffer + gas_payment verification overhead
                let action_gas_buffered = simulated_gas + simulated_gas / U256::from(5);
                let total_gas = action_gas_buffered + simulated_gas;
                info!(
                    "Gas estimate: simulated={}, buffered={}, total_with_payment={}",
                    simulated_gas, action_gas_buffered, total_gas
                );

                let payment_note = self.select_payment_note_excluding(
                    *payment_asset,
                    prices,
                    excluded,
                    total_gas,
                )?;

                // Registered ephemeral key so scan engine can discover the change note
                let fee_estimate = self.builder.estimate_fee(total_gas, prices);
                let change_value = payment_note
                    .note
                    .value
                    .checked_sub(fee_estimate.fee_amount)
                    .ok_or(PrivacyClientError::GasFeeExceedsNoteValue {
                        fee: fee_estimate.fee_amount,
                        note_value: payment_note.note.value,
                    })?;
                let gas_change_note = self
                    .note_factory
                    .create_change_note(change_value, *payment_asset, &mut self.keys)
                    .map_err(|e| {
                        PrivacyClientError::ProofFailed(format!(
                            "Gas change note creation failed: {e}"
                        ))
                    })?;

                let bundle = self
                    .builder
                    .build_paid_action(
                        &payment_note,
                        merkle_root,
                        self.tree.get_path(payment_note.leaf_index).siblings_vec(),
                        self.config.darkpool_address,
                        calldata,
                        prices,
                        *relayer_address,
                        total_gas,
                        Some(gas_change_note),
                        0,
                    )
                    .await
                    .map_err(|e| PrivacyClientError::ProofFailed(e.to_string()))?;

                debug!(
                    "Paid action bundle built: fee={}, multicall_target={:?}",
                    bundle.gas_payment.fee_amount, bundle.multicall_target
                );

                let tx_hash = client
                    .submit_transaction(bundle.multicall_target, bundle.multicall_data.clone())
                    .await?;

                info!(
                    "Paid transaction submitted via mixnet. TxHash: {:?}",
                    tx_hash
                );

                let block_num = self.wait_for_receipt_via_mixnet(client, tx_hash).await?;

                // Direct receipt fetch: verify success + capture logs (eth_getLogs unreliable on Anvil)
                let receipt = self
                    .timed_provider_call(
                        "paid TX get_transaction_receipt",
                        self.signer.get_transaction_receipt(tx_hash),
                    )
                    .await?
                    .ok_or_else(|| {
                        PrivacyClientError::TransactionFailed(
                            "Paid TX receipt not found via direct provider".to_string(),
                        )
                    })?;

                if receipt.status != Some(U64::from(1)) {
                    return Err(PrivacyClientError::TransactionFailed(format!(
                        "Paid TX reverted on-chain (status={:?}, tx={:?})",
                        receipt.status, tx_hash
                    )));
                }

                info!(
                    "Paid TX confirmed: block={}, logs={}, gas_used={:?}",
                    block_num,
                    receipt.logs.len(),
                    receipt.gas_used
                );

                // Events may be at a different block than what the mixnet receipt reported
                let final_logs = if receipt.logs.is_empty() {
                    warn!(
                        "Receipt has 0 logs despite status=1 and gas_used={:?}. \
                         Direct receipt block={:?}. Checking surrounding blocks...",
                        receipt.gas_used, receipt.block_number
                    );

                    let search_from = block_num.saturating_sub(5);
                    let current = self
                        .timed_provider_call(
                            "paid TX fallback get_block_number",
                            self.signer.get_block_number(),
                        )
                        .await
                        .map(|b| b.as_u64())
                        .unwrap_or(block_num + 5);
                    let search_to = current.max(block_num + 5);

                    let wide_filter = Filter::new()
                        .address(self.config.darkpool_address)
                        .from_block(search_from)
                        .to_block(search_to);
                    match self
                        .timed_provider_call(
                            "paid TX fallback get_logs",
                            self.signer.get_logs(&wide_filter),
                        )
                        .await
                    {
                        Ok(all_logs) => {
                            info!(
                                "Blocks {}-{} have {} DarkPool logs",
                                search_from,
                                search_to,
                                all_logs.len()
                            );
                            for (i, log) in all_logs.iter().take(10).enumerate() {
                                info!(
                                    "  Log[{}]: block={:?}, addr={:?}, topics={}, tx={:?}",
                                    i,
                                    log.block_number,
                                    log.address,
                                    log.topics.len(),
                                    log.transaction_hash
                                );
                            }

                            let tx_logs: Vec<_> = all_logs
                                .into_iter()
                                .filter(|l| l.transaction_hash == Some(tx_hash))
                                .collect();

                            if tx_logs.is_empty() {
                                warn!(
                                    "No logs found for TX {:?} in blocks {}-{}",
                                    tx_hash, search_from, search_to
                                );
                                vec![]
                            } else {
                                info!(
                                    "Found {} logs for TX {:?} in wider search",
                                    tx_logs.len(),
                                    tx_hash
                                );
                                tx_logs
                            }
                        }
                        Err(e) => {
                            warn!("Failed to query wide log range: {}", e);
                            vec![]
                        }
                    }
                } else {
                    receipt.logs
                };

                Ok(SubmitResult {
                    tx_hash,
                    block_num,
                    gas_used: receipt.gas_used.unwrap_or_default(),
                    payment_nullifier: Some(payment_note.nullifier),
                    receipt_logs: final_logs,
                })
            }
            #[cfg(feature = "mixnet")]
            Transport::SignedBroadcast { client } => {
                let tx = TransactionRequest::new()
                    .to(self.config.darkpool_address)
                    .data(calldata);

                let mut typed_tx = TypedTransaction::Legacy(tx);
                self.signer
                    .fill_transaction(&mut typed_tx, None)
                    .await
                    .map_err(|e| {
                        PrivacyClientError::TransactionFailed(format!(
                            "fill_transaction for signed broadcast: {e}"
                        ))
                    })?;

                let signature = self
                    .signer
                    .signer()
                    .sign_transaction(&typed_tx)
                    .await
                    .map_err(|e| {
                        PrivacyClientError::TransactionFailed(format!(
                            "sign_transaction for signed broadcast: {e}"
                        ))
                    })?;

                let raw_tx = typed_tx.rlp_signed(&signature);
                let tx_hash = client.broadcast_signed_transaction(raw_tx).await?;

                info!(
                    "Signed broadcast submitted via mixnet. TxHash: {:?}",
                    tx_hash
                );

                let block_num = self.wait_for_receipt_via_mixnet(client, tx_hash).await?;

                let receipt = self
                    .timed_provider_call(
                        "signed_broadcast get_transaction_receipt",
                        self.signer.get_transaction_receipt(tx_hash),
                    )
                    .await?
                    .ok_or_else(|| {
                        PrivacyClientError::TransactionFailed(
                            "Signed broadcast receipt not found".into(),
                        )
                    })?;

                if receipt.status != Some(U64::from(1)) {
                    return Err(PrivacyClientError::TransactionFailed(format!(
                        "Signed broadcast TX reverted (status={:?}, tx={:?})",
                        receipt.status, tx_hash
                    )));
                }

                let block_num = receipt.block_number.map_or(block_num, |b| b.as_u64());
                let gas_used = receipt.gas_used.unwrap_or_default();

                Ok(SubmitResult {
                    tx_hash,
                    block_num,
                    gas_used,
                    payment_nullifier: None,
                    receipt_logs: vec![],
                })
            }
            #[cfg(not(feature = "mixnet"))]
            Transport::_Phantom(_) => unreachable!(),
        }
    }

    fn create_spending_inputs(
        &self,
        note: &OwnedNote,
    ) -> Result<SpendingInputs, PrivacyClientError> {
        let merkle_path = self.tree.get_path(note.leaf_index);
        Ok(SpendingInputs::from_owned_note(note, merkle_path))
    }

    #[cfg(feature = "mixnet")]
    async fn wait_for_receipt_via_mixnet(
        &self,
        mixnet: &MixnetClient,
        tx_hash: H256,
    ) -> Result<u64, PrivacyClientError> {
        let max_attempts = 30;
        let poll_interval = std::time::Duration::from_secs(2);

        for attempt in 0..max_attempts {
            match mixnet.get_transaction_receipt(tx_hash).await {
                Ok(Some(receipt)) => {
                    if let Some(block_num) = receipt.get("blockNumber") {
                        if let Some(hex_str) = block_num.as_str() {
                            match u64::from_str_radix(hex_str.trim_start_matches("0x"), 16) {
                                Ok(num) => return Ok(num),
                                Err(e) => {
                                    warn!(
                                        "Malformed blockNumber '{}' in receipt: {}. Falling back to current block.",
                                        hex_str, e
                                    );
                                }
                            }
                        }
                    }
                    return Ok(mixnet.block_number().await?);
                }
                Ok(None) => {
                    debug!(
                        "Waiting for receipt via mixnet (attempt {}/{})",
                        attempt + 1,
                        max_attempts
                    );
                    tokio::time::sleep(poll_interval).await;
                }
                Err(e) => {
                    warn!("Error fetching receipt via mixnet: {}", e);
                    tokio::time::sleep(poll_interval).await;
                }
            }
        }

        Err(PrivacyClientError::TransactionFailed(
            "Timed out waiting for receipt".to_string(),
        ))
    }

    /// Excludes `exclude` commitments to prevent double-spend when asset == `payment_asset`.
    #[cfg(feature = "mixnet")]
    fn select_payment_note_excluding(
        &self,
        payment_asset: Address,
        prices: &PriceData,
        exclude: &HashSet<U256>,
        gas_limit: U256,
    ) -> Result<WalletNote, PrivacyClientError> {
        use crate::economics::FeeManager;

        let fee_manager = FeeManager::default();
        let estimate = fee_manager.calculate_fee(gas_limit, prices);

        let payment_notes: Vec<_> = self
            .utxos
            .get_unspent_excluding(exclude)
            .into_iter()
            .filter(|n| {
                n.plaintext.asset_id == crate::crypto_helpers::address_to_field(payment_asset)
                    && n.plaintext.value >= estimate.fee_amount
            })
            .cloned()
            .collect();

        let selected = payment_notes
            .first()
            .ok_or(PrivacyClientError::NoteSelectionFailed(format!(
                "No note for gas payment (need {}, excluding {} notes). User may need to deposit more tokens or use a different payment asset.",
                estimate.fee_amount, exclude.len()
            )))?;

        Ok(self.owned_note_to_wallet_note(selected))
    }

    /// Derives nullifier using the correct path (A or B) based on note origin.
    #[cfg(feature = "mixnet")]
    fn owned_note_to_wallet_note(&self, owned: &OwnedNote) -> WalletNote {
        let nullifier = Self::derive_nullifier_hash(owned);

        WalletNote {
            note: owned.plaintext.clone(),
            commitment: owned.commitment,
            leaf_index: owned.leaf_index,
            nullifier,
            spending_secret: owned.spending_secret,
            is_transfer: owned.is_transfer,
            derivation_index: 0,
            spent: false,
        }
    }

    /// Post-sync sanity check: compares local root against `getCurrentRoot()` on-chain.
    pub async fn verify_root_matches_chain(&self) -> Result<(), PrivacyClientError> {
        let onchain_root = self.fetch_current_root().await?;
        let local_root = self.tree.root();

        if local_root != onchain_root {
            return Err(PrivacyClientError::TreeMismatch {
                local: local_root,
                onchain: onchain_root,
            });
        }

        debug!(root = ?local_root, "Local Merkle root matches on-chain root");
        Ok(())
    }

    /// Pre-proof check: verifies local root is in the contract's root history ring buffer.
    pub async fn verify_root_is_known(&self) -> Result<(), PrivacyClientError> {
        let local_root = self.tree.root();
        let is_known = self.fetch_is_known_root(local_root).await?;

        if !is_known {
            // Fetch the current root to provide a useful error message
            let onchain_root = self.fetch_current_root().await.unwrap_or(U256::zero());
            return Err(PrivacyClientError::TreeMismatch {
                local: local_root,
                onchain: onchain_root,
            });
        }

        debug!(root = ?local_root, "Local Merkle root is recognized on-chain");
        Ok(())
    }

    /// Selector `0x8270482d` = `getCurrentRoot()`, returns big-endian U256.
    async fn fetch_current_root(&self) -> Result<U256, PrivacyClientError> {
        let selector: [u8; 4] = [0x82, 0x70, 0x48, 0x2d]; // getCurrentRoot()

        let tx = TransactionRequest::new()
            .to(self.config.darkpool_address)
            .data(selector.to_vec());

        let result = self
            .timed_provider_call("getCurrentRoot", self.signer.call(&tx.into(), None))
            .await?;

        if result.len() < 32 {
            return Err(PrivacyClientError::ProviderError(format!(
                "getCurrentRoot returned {} bytes, expected 32",
                result.len()
            )));
        }

        Ok(U256::from_big_endian(&result[..32]))
    }

    /// Selector `0x6d9833e3` = `isKnownRoot(bytes32)`, returns ABI-encoded bool.
    async fn fetch_is_known_root(&self, root: U256) -> Result<bool, PrivacyClientError> {
        let selector: [u8; 4] = [0x6d, 0x98, 0x33, 0xe3]; // isKnownRoot(bytes32)

        let mut calldata = Vec::with_capacity(36);
        calldata.extend_from_slice(&selector);
        let mut root_bytes = [0u8; 32];
        root.to_big_endian(&mut root_bytes);
        calldata.extend_from_slice(&root_bytes);

        let tx = TransactionRequest::new()
            .to(self.config.darkpool_address)
            .data(calldata);

        let result = self
            .timed_provider_call("isKnownRoot", self.signer.call(&tx.into(), None))
            .await?;

        if result.len() < 32 {
            return Err(PrivacyClientError::ProviderError(format!(
                "isKnownRoot returned {} bytes, expected 32",
                result.len()
            )));
        }

        Ok(result[31] != 0)
    }

    #[must_use]
    pub fn utxos(&self) -> &UtxoStore {
        &self.utxos
    }

    #[must_use]
    pub fn tree(&self) -> &LocalMerkleTree {
        &self.tree
    }

    #[must_use]
    pub fn keys(&self) -> &KeyRepository {
        &self.keys
    }

    #[must_use]
    pub fn darkpool_address(&self) -> Address {
        self.config.darkpool_address
    }

    #[must_use]
    pub fn builder(&self) -> &TransactionBuilder {
        &self.builder
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_privacy_client_config_default() {
        let config = PrivacyClientConfig::default();
        assert_eq!(config.darkpool_address, Address::zero());
        assert_eq!(config.start_block, 0);
    }

    #[test]
    fn test_privacy_tx_result() {
        let result = PrivacyTxResult {
            tx_hash: H256::zero(),
            new_commitments: vec![U256::from(1), U256::from(2)],
            spent_nullifiers: vec![U256::from(3)],
            gas_used: U256::from(21000),
        };

        assert_eq!(result.new_commitments.len(), 2);
        assert_eq!(result.spent_nullifiers.len(), 1);
    }
}