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
//! Authentication manager for WAB-based wallet authentication.
//!
//! Provides `WalletAuthenticationManager` which wraps an inner wallet with
//! WAB-based authentication flow. Before authentication completes, all
//! WalletInterface methods (except `is_authenticated` and `wait_for_authentication`)
//! return `WERR_NOT_ACTIVE`. After successful auth, calls delegate to the inner wallet.
//!
//! Composes CWI base class logic (UMP tokens, PBKDF2 key derivation, password +
//! presentation key management) and R-Puzzle faucet funding.

/// CWI-style key derivation functions (PBKDF2, XOR, identity key derivation).
pub mod cwi_logic;
/// Authentication state types, snapshot, and profile.
pub mod types;
/// UMP token struct and overlay lookup/creation.
pub mod ump_token;

use std::sync::Arc;

use async_trait::async_trait;
use tokio::sync::{Mutex, RwLock};

use bsv::primitives::big_number::BigNumber;
use bsv::primitives::private_key::PrivateKey;
use bsv::script::templates::r_puzzle::{RPuzzle, RPuzzleType};
use bsv::wallet::interfaces::{
    AbortActionArgs, AbortActionResult, AcquireCertificateArgs, AuthenticatedResult, Certificate,
    CreateActionArgs, CreateActionResult, CreateHmacArgs, CreateHmacResult, CreateSignatureArgs,
    CreateSignatureResult, DecryptArgs, DecryptResult, DiscoverByAttributesArgs,
    DiscoverByIdentityKeyArgs, DiscoverCertificatesResult, EncryptArgs, EncryptResult,
    GetHeaderArgs, GetHeaderResult, GetHeightResult, GetNetworkResult, GetPublicKeyArgs,
    GetPublicKeyResult, GetVersionResult, InternalizeActionArgs, InternalizeActionResult,
    ListActionsArgs, ListActionsResult, ListCertificatesArgs, ListCertificatesResult,
    ListOutputsArgs, ListOutputsResult, ProveCertificateArgs, ProveCertificateResult,
    RelinquishCertificateArgs, RelinquishCertificateResult, RelinquishOutputArgs,
    RelinquishOutputResult, RevealCounterpartyKeyLinkageArgs, RevealCounterpartyKeyLinkageResult,
    RevealSpecificKeyLinkageArgs, RevealSpecificKeyLinkageResult, SignActionArgs, SignActionResult,
    VerifyHmacArgs, VerifyHmacResult, VerifySignatureArgs, VerifySignatureResult, WalletInterface,
};

use crate::wab_client::interactors::AuthMethodInteractor;
use crate::wab_client::types::{FaucetResponse, StartAuthResponse};
use crate::wab_client::WABClient;
use crate::WalletError;

use types::{AuthState, StateSnapshot, WalletBuilderFn};

/// Macro to delegate a WalletInterface method to the inner wallet, or return
/// `WERR_NOT_ACTIVE` if no inner wallet is available (pre-authentication).
macro_rules! delegate_or_not_active {
    ($self:ident, $method:ident, $args:expr, $originator:expr) => {{
        let guard = $self.inner.read().await;
        match guard.as_ref() {
            Some(w) => w.$method($args, $originator).await,
            None => Err(bsv::wallet::error::WalletError::Internal(
                "WERR_NOT_ACTIVE: Wallet is not authenticated".into(),
            )),
        }
    }};
    // Variant without args (for get_height, get_network, get_version)
    ($self:ident, $method:ident, $originator:expr) => {{
        let guard = $self.inner.read().await;
        match guard.as_ref() {
            Some(w) => w.$method($originator).await,
            None => Err(bsv::wallet::error::WalletError::Internal(
                "WERR_NOT_ACTIVE: Wallet is not authenticated".into(),
            )),
        }
    }};
}

/// WalletAuthenticationManager wraps a wallet with WAB-based authentication.
///
/// Before authentication completes, all WalletInterface methods except
/// `is_authenticated` and `wait_for_authentication` return `WERR_NOT_ACTIVE`.
/// After authentication, all calls are delegated to the inner wallet.
///
/// The authentication flow:
/// 1. Application calls `start_auth()` with an auth method interactor and payload
/// 2. WAB server initiates verification (e.g., sends SMS code)
/// 3. Application calls `complete_auth()` with verification payload
/// 4. Manager determines new vs existing user via UMP token lookup
/// 5. Root key derived from presentation key + password logic
/// 6. Inner wallet constructed via `wallet_builder`
/// 7. For new users, optional faucet funding via R-Puzzle redemption
/// 8. `wait_for_authentication` resolvers are notified
pub struct WalletAuthenticationManager {
    /// The inner wallet; `None` before authentication, `Some` after.
    inner: RwLock<Option<Arc<dyn WalletInterface + Send + Sync>>>,
    /// Sender to signal authentication completion.
    auth_tx: tokio::sync::watch::Sender<bool>,
    /// Receiver to wait for authentication completion.
    auth_rx: tokio::sync::watch::Receiver<bool>,
    /// WAB HTTP client for authentication API calls.
    wab_client: WABClient,
    /// Async closure to construct the inner wallet from root key material.
    wallet_builder: WalletBuilderFn,
    /// Admin originator domain for privileged operations.
    admin_originator: String,
    /// Current authentication state.
    state: Mutex<AuthState>,
    /// Generated or restored presentation key (hex).
    presentation_key: Mutex<Option<String>>,
}

impl WalletAuthenticationManager {
    /// Create a new `WalletAuthenticationManager`.
    ///
    /// # Arguments
    /// * `wab_client` - WAB HTTP client for auth API calls
    /// * `wallet_builder` - Async closure to construct inner wallet from root key
    /// * `admin_originator` - Admin domain for privileged operations
    /// * `state_snapshot` - Optional serialized `StateSnapshot` to restore state
    pub fn new(
        wab_client: WABClient,
        wallet_builder: WalletBuilderFn,
        admin_originator: String,
        state_snapshot: Option<Vec<u8>>,
    ) -> Result<Self, WalletError> {
        let (auth_tx, auth_rx) = tokio::sync::watch::channel(false);

        let (initial_state, initial_key) = if let Some(snapshot_bytes) = state_snapshot {
            let snapshot: StateSnapshot = serde_json::from_slice(&snapshot_bytes)?;
            let key = snapshot.presentation_key.clone();
            (snapshot.auth_state, key)
        } else {
            (AuthState::Unauthenticated, None)
        };

        Ok(Self {
            inner: RwLock::new(None),
            auth_tx,
            auth_rx,
            wab_client,
            wallet_builder,
            admin_originator,
            state: Mutex::new(initial_state),
            presentation_key: Mutex::new(initial_key),
        })
    }

    // ========================================================================
    // Auth flow methods (not part of WalletInterface)
    // ========================================================================

    /// Start the WAB authentication flow.
    ///
    /// Generates a temporary presentation key if none is set, then calls
    /// `start_auth_method` on the WAB client with the chosen interactor's method type.
    ///
    /// # Arguments
    /// * `interactor` - The auth method interactor (e.g., Twilio, Persona)
    /// * `payload` - Method-specific payload (e.g., phone number for Twilio)
    pub async fn start_auth(
        &self,
        interactor: &dyn AuthMethodInteractor,
        payload: serde_json::Value,
    ) -> Result<StartAuthResponse, WalletError> {
        // Generate presentation key if not already set
        {
            let mut pk = self.presentation_key.lock().await;
            if pk.is_none() {
                *pk = Some(WABClient::generate_random_presentation_key()?);
            }
        }

        let pk = self.presentation_key.lock().await;
        let presentation_key = pk
            .as_ref()
            .ok_or_else(|| WalletError::Internal("Presentation key missing".to_string()))?;

        // Update state to Authenticating
        {
            let mut state = self.state.lock().await;
            *state = AuthState::Authenticating;
        }

        let method_type = interactor.method_type();
        self.wab_client
            .start_auth_method(presentation_key, method_type, payload)
            .await
    }

    /// Complete the WAB authentication flow.
    ///
    /// On success:
    /// 1. Retrieves final presentation key from WAB
    /// 2. Looks up UMP token to determine new vs existing user
    /// 3. Derives root key from presentation key
    /// 4. Constructs inner wallet via wallet_builder
    /// 5. Optionally redeems faucet for new users
    /// 6. Signals authentication completion
    ///
    /// # Arguments
    /// * `interactor` - The auth method interactor
    /// * `payload` - Verification payload (e.g., SMS code)
    pub async fn complete_auth(
        &self,
        interactor: &dyn AuthMethodInteractor,
        payload: serde_json::Value,
    ) -> Result<(), WalletError> {
        let temp_key = {
            let mut pk = self.presentation_key.lock().await;
            pk.take().ok_or_else(|| {
                WalletError::Internal("No presentation key; call start_auth first".to_string())
            })?
        };

        let method_type = interactor.method_type();
        let result = self
            .wab_client
            .complete_auth_method(&temp_key, method_type, payload)
            .await?;

        if !result.success {
            let msg = result
                .message
                .unwrap_or_else(|| "Failed to complete WAB auth".to_string());
            let mut state = self.state.lock().await;
            *state = AuthState::Failed(msg.clone());
            return Err(WalletError::Internal(msg));
        }

        let presentation_key_hex = result.presentation_key.ok_or_else(|| {
            WalletError::Internal("No presentation key returned from WAB".to_string())
        })?;

        // Store the final presentation key
        {
            let mut pk = self.presentation_key.lock().await;
            *pk = Some(presentation_key_hex.clone());
        }

        // Look up UMP token to determine new vs existing user
        // For now, this always returns None (new user); full overlay integration later
        let presentation_key_bytes = hex_to_bytes(&presentation_key_hex)?;
        let _is_new_user = {
            let guard = self.inner.read().await;
            if let Some(ref _w) = *guard {
                // If we somehow already have a wallet, skip lookup
                false
            } else {
                // No wallet yet; use presentation key bytes as root key placeholder
                true
            }
        };

        // Derive root key from presentation key bytes
        // In the full CWI flow this would combine presentation key with password via UMP token
        // For now, use presentation key bytes directly as root key material
        let root_key = if presentation_key_bytes.len() >= 32 {
            presentation_key_bytes.clone()
        } else {
            return Err(WalletError::Internal(
                "Presentation key too short for root key derivation".to_string(),
            ));
        };

        // Build a placeholder PrivilegedKeyManager from root key
        let priv_key_manager = Arc::new(crate::wallet::privileged::NoOpPrivilegedKeyManager);

        // Construct inner wallet
        let wallet = (self.wallet_builder)(root_key, priv_key_manager).await?;

        // Store inner wallet
        {
            let mut inner = self.inner.write().await;
            *inner = Some(wallet);
        }

        // Update state
        {
            let mut state = self.state.lock().await;
            *state = AuthState::Authenticated;
        }

        // Signal authentication completion
        let _ = self.auth_tx.send(true);

        Ok(())
    }

    /// Serialize current state to a JSON byte vector for persistence.
    ///
    /// The returned bytes can be passed to `new()` as `state_snapshot` to
    /// restore manager state across process restarts.
    pub async fn get_state_snapshot(&self) -> Result<Vec<u8>, WalletError> {
        let state = self.state.lock().await.clone();
        let pk = self.presentation_key.lock().await.clone();
        let snapshot = StateSnapshot {
            presentation_key: pk,
            auth_state: state,
            profile: None,
            is_new_user: None,
        };
        serde_json::to_vec(&snapshot).map_err(|e| {
            WalletError::Internal(format!("Failed to serialize state snapshot: {}", e))
        })
    }

    // ========================================================================
    // R-Puzzle faucet redemption
    // ========================================================================

    /// Redeem a faucet UTXO using R-Puzzle.
    ///
    /// Ports the TS WalletAuthenticationManager faucet logic:
    /// 1. Creates an action with the faucet UTXO as input (unlockingScriptLength 108)
    /// 2. Signs with RPuzzle::from_k() using the k-value from the faucet response
    /// 3. Completes signing via wallet.sign_action()
    ///
    /// # Arguments
    /// * `wallet` - The inner wallet to use for create_action/sign_action
    /// * `faucet_response` - Response from WABClient::request_faucet containing tx, txid, and k
    #[allow(dead_code)]
    async fn redeem_faucet(
        &self,
        wallet: &(dyn WalletInterface + Send + Sync),
        faucet_response: &FaucetResponse,
    ) -> Result<(), WalletError> {
        let k_hex = faucet_response
            .k
            .as_ref()
            .ok_or_else(|| WalletError::Internal("Faucet response missing k value".to_string()))?;
        let txid = faucet_response
            .txid
            .as_ref()
            .ok_or_else(|| WalletError::Internal("Faucet response missing txid".to_string()))?;
        let tx_hex = faucet_response
            .tx
            .as_ref()
            .ok_or_else(|| WalletError::Internal("Faucet response missing tx data".to_string()))?;

        // Parse the faucet transaction BEEF
        let tx_bytes = hex_to_bytes(tx_hex)?;

        // Create action with faucet UTXO as input
        let create_args = CreateActionArgs {
            input_beef: Some(tx_bytes),
            inputs: vec![bsv::wallet::interfaces::CreateActionInput {
                outpoint: format!("{}.0", txid),
                unlocking_script_length: Some(108),
                input_description: "Fund from faucet".to_string(),
                unlocking_script: None,
                sequence_number: None,
            }],
            description: "Fund wallet".to_string(),
            outputs: vec![],
            labels: vec![],
            lock_time: None,
            version: None,
            options: Some(bsv::wallet::interfaces::CreateActionOptions {
                accept_delayed_broadcast: bsv::wallet::types::BooleanDefaultTrue(Some(false)),
                ..Default::default()
            }),
            reference: None,
        };

        let create_result = wallet
            .create_action(create_args, Some(&self.admin_originator))
            .await
            .map_err(|e| WalletError::Internal(format!("Faucet create_action failed: {}", e)))?;

        let signable = create_result.signable_transaction.ok_or_else(|| {
            WalletError::Internal("No signable transaction from faucet create_action".to_string())
        })?;

        // Parse k-value for RPuzzle
        let k = BigNumber::from_hex(k_hex)
            .map_err(|e| WalletError::Internal(format!("Failed to parse faucet k value: {}", e)))?;
        let random_key = PrivateKey::from_random().map_err(|e| {
            WalletError::Internal(format!("Failed to generate random key for RPuzzle: {}", e))
        })?;

        // Create RPuzzle unlocking template with k value
        // The puzzle type is Raw since faucet uses raw R-value matching
        let puzzle = RPuzzle::from_k(RPuzzleType::Raw, vec![], k, random_key);

        // Convert signable.tx bytes to hex for Transaction::from_beef
        let tx_hex_str: String = signable.tx.iter().map(|b| format!("{:02x}", b)).collect();

        // Parse the signable transaction BEEF
        let tx = bsv::transaction::Transaction::from_beef(&tx_hex_str).map_err(|e| {
            WalletError::Internal(format!("Failed to parse signable transaction BEEF: {}", e))
        })?;

        // Get the source output's locking script and satoshis from the transaction's
        // source_transaction on input 0 (set by the wallet during create_action)
        let (source_sats, source_script) = tx
            .inputs
            .first()
            .and_then(|inp| {
                inp.source_transaction.as_ref().map(|stx| {
                    let vout = inp.source_output_index as usize;
                    let output = &stx.outputs[vout];
                    (output.satoshis, output.locking_script.clone())
                })
            })
            .ok_or_else(|| {
                WalletError::Internal(
                    "Faucet signable tx missing source transaction on input 0".to_string(),
                )
            })?;

        // Generate the sighash preimage for input 0
        let preimage = tx
            .sighash_preimage(
                0,
                bsv::primitives::transaction_signature::SIGHASH_ALL
                    | bsv::primitives::transaction_signature::SIGHASH_FORKID,
                source_sats.unwrap_or(0),
                &source_script,
            )
            .map_err(|e| {
                WalletError::Internal(format!("Failed to compute sighash preimage: {}", e))
            })?;

        // Unlock with RPuzzle (produces DER signature + sighash byte)
        let unlocking_script = puzzle
            .unlock(&preimage)
            .map_err(|e| WalletError::Internal(format!("RPuzzle unlock failed: {}", e)))?;

        // Sign the action with the RPuzzle unlocking script (binary bytes)
        let mut spends = std::collections::HashMap::new();
        spends.insert(
            0u32,
            bsv::wallet::interfaces::SignActionSpend {
                unlocking_script: unlocking_script.to_binary(),
                sequence_number: None,
            },
        );

        let _sign_result = wallet
            .sign_action(
                SignActionArgs {
                    reference: signable.reference,
                    spends,
                    options: None,
                },
                Some(&self.admin_originator),
            )
            .await
            .map_err(|e| WalletError::Internal(format!("Faucet sign_action failed: {}", e)))?;

        Ok(())
    }
}

/// Convert a hex string to bytes.
fn hex_to_bytes(hex: &str) -> Result<Vec<u8>, WalletError> {
    if !hex.len().is_multiple_of(2) {
        return Err(WalletError::Internal(format!(
            "Invalid hex string length: {}",
            hex.len()
        )));
    }
    (0..hex.len())
        .step_by(2)
        .map(|i| {
            u8::from_str_radix(&hex[i..i + 2], 16)
                .map_err(|e| WalletError::Internal(format!("Invalid hex at position {}: {}", i, e)))
        })
        .collect()
}

// ============================================================================
// WalletInterface implementation
// ============================================================================

#[async_trait]
impl WalletInterface for WalletAuthenticationManager {
    async fn is_authenticated(
        &self,
        _originator: Option<&str>,
    ) -> Result<AuthenticatedResult, bsv::wallet::error::WalletError> {
        let guard = self.inner.read().await;
        Ok(AuthenticatedResult {
            authenticated: guard.is_some(),
        })
    }

    async fn wait_for_authentication(
        &self,
        _originator: Option<&str>,
    ) -> Result<AuthenticatedResult, bsv::wallet::error::WalletError> {
        let mut rx = self.auth_rx.clone();
        // If already authenticated, return immediately
        if *rx.borrow() {
            return Ok(AuthenticatedResult {
                authenticated: true,
            });
        }
        // Wait for auth signal
        while rx.changed().await.is_ok() {
            if *rx.borrow() {
                return Ok(AuthenticatedResult {
                    authenticated: true,
                });
            }
        }
        // Channel closed without auth -- should not happen in normal flow
        Ok(AuthenticatedResult {
            authenticated: false,
        })
    }

    async fn get_height(
        &self,
        originator: Option<&str>,
    ) -> Result<GetHeightResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, get_height, originator)
    }

    async fn get_header_for_height(
        &self,
        args: GetHeaderArgs,
        originator: Option<&str>,
    ) -> Result<GetHeaderResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, get_header_for_height, args, originator)
    }

    async fn get_network(
        &self,
        originator: Option<&str>,
    ) -> Result<GetNetworkResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, get_network, originator)
    }

    async fn get_version(
        &self,
        originator: Option<&str>,
    ) -> Result<GetVersionResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, get_version, originator)
    }

    async fn create_action(
        &self,
        args: CreateActionArgs,
        originator: Option<&str>,
    ) -> Result<CreateActionResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, create_action, args, originator)
    }

    async fn sign_action(
        &self,
        args: SignActionArgs,
        originator: Option<&str>,
    ) -> Result<SignActionResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, sign_action, args, originator)
    }

    async fn abort_action(
        &self,
        args: AbortActionArgs,
        originator: Option<&str>,
    ) -> Result<AbortActionResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, abort_action, args, originator)
    }

    async fn list_actions(
        &self,
        args: ListActionsArgs,
        originator: Option<&str>,
    ) -> Result<ListActionsResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, list_actions, args, originator)
    }

    async fn internalize_action(
        &self,
        args: InternalizeActionArgs,
        originator: Option<&str>,
    ) -> Result<InternalizeActionResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, internalize_action, args, originator)
    }

    async fn list_outputs(
        &self,
        args: ListOutputsArgs,
        originator: Option<&str>,
    ) -> Result<ListOutputsResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, list_outputs, args, originator)
    }

    async fn relinquish_output(
        &self,
        args: RelinquishOutputArgs,
        originator: Option<&str>,
    ) -> Result<RelinquishOutputResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, relinquish_output, args, originator)
    }

    async fn get_public_key(
        &self,
        args: GetPublicKeyArgs,
        originator: Option<&str>,
    ) -> Result<GetPublicKeyResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, get_public_key, args, originator)
    }

    async fn reveal_counterparty_key_linkage(
        &self,
        args: RevealCounterpartyKeyLinkageArgs,
        originator: Option<&str>,
    ) -> Result<RevealCounterpartyKeyLinkageResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, reveal_counterparty_key_linkage, args, originator)
    }

    async fn reveal_specific_key_linkage(
        &self,
        args: RevealSpecificKeyLinkageArgs,
        originator: Option<&str>,
    ) -> Result<RevealSpecificKeyLinkageResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, reveal_specific_key_linkage, args, originator)
    }

    async fn encrypt(
        &self,
        args: EncryptArgs,
        originator: Option<&str>,
    ) -> Result<EncryptResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, encrypt, args, originator)
    }

    async fn decrypt(
        &self,
        args: DecryptArgs,
        originator: Option<&str>,
    ) -> Result<DecryptResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, decrypt, args, originator)
    }

    async fn create_hmac(
        &self,
        args: CreateHmacArgs,
        originator: Option<&str>,
    ) -> Result<CreateHmacResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, create_hmac, args, originator)
    }

    async fn verify_hmac(
        &self,
        args: VerifyHmacArgs,
        originator: Option<&str>,
    ) -> Result<VerifyHmacResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, verify_hmac, args, originator)
    }

    async fn create_signature(
        &self,
        args: CreateSignatureArgs,
        originator: Option<&str>,
    ) -> Result<CreateSignatureResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, create_signature, args, originator)
    }

    async fn verify_signature(
        &self,
        args: VerifySignatureArgs,
        originator: Option<&str>,
    ) -> Result<VerifySignatureResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, verify_signature, args, originator)
    }

    async fn acquire_certificate(
        &self,
        args: AcquireCertificateArgs,
        originator: Option<&str>,
    ) -> Result<Certificate, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, acquire_certificate, args, originator)
    }

    async fn list_certificates(
        &self,
        args: ListCertificatesArgs,
        originator: Option<&str>,
    ) -> Result<ListCertificatesResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, list_certificates, args, originator)
    }

    async fn prove_certificate(
        &self,
        args: ProveCertificateArgs,
        originator: Option<&str>,
    ) -> Result<ProveCertificateResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, prove_certificate, args, originator)
    }

    async fn relinquish_certificate(
        &self,
        args: RelinquishCertificateArgs,
        originator: Option<&str>,
    ) -> Result<RelinquishCertificateResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, relinquish_certificate, args, originator)
    }

    async fn discover_by_identity_key(
        &self,
        args: DiscoverByIdentityKeyArgs,
        originator: Option<&str>,
    ) -> Result<DiscoverCertificatesResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, discover_by_identity_key, args, originator)
    }

    async fn discover_by_attributes(
        &self,
        args: DiscoverByAttributesArgs,
        originator: Option<&str>,
    ) -> Result<DiscoverCertificatesResult, bsv::wallet::error::WalletError> {
        delegate_or_not_active!(self, discover_by_attributes, args, originator)
    }
}

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

    /// Simple mock wallet for auth manager testing.
    /// `is_authenticated` returns true; all other methods return NotImplemented.
    struct MockWallet;

    #[async_trait]
    impl WalletInterface for MockWallet {
        async fn is_authenticated(
            &self,
            _originator: Option<&str>,
        ) -> Result<AuthenticatedResult, bsv::wallet::error::WalletError> {
            Ok(AuthenticatedResult {
                authenticated: true,
            })
        }

        async fn wait_for_authentication(
            &self,
            _originator: Option<&str>,
        ) -> Result<AuthenticatedResult, bsv::wallet::error::WalletError> {
            Ok(AuthenticatedResult {
                authenticated: true,
            })
        }

        async fn get_height(
            &self,
            _o: Option<&str>,
        ) -> Result<GetHeightResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn get_header_for_height(
            &self,
            _a: GetHeaderArgs,
            _o: Option<&str>,
        ) -> Result<GetHeaderResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn get_network(
            &self,
            _o: Option<&str>,
        ) -> Result<GetNetworkResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn get_version(
            &self,
            _o: Option<&str>,
        ) -> Result<GetVersionResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn create_action(
            &self,
            _a: CreateActionArgs,
            _o: Option<&str>,
        ) -> Result<CreateActionResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn sign_action(
            &self,
            _a: SignActionArgs,
            _o: Option<&str>,
        ) -> Result<SignActionResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn abort_action(
            &self,
            _a: AbortActionArgs,
            _o: Option<&str>,
        ) -> Result<AbortActionResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn list_actions(
            &self,
            _a: ListActionsArgs,
            _o: Option<&str>,
        ) -> Result<ListActionsResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn internalize_action(
            &self,
            _a: InternalizeActionArgs,
            _o: Option<&str>,
        ) -> Result<InternalizeActionResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn list_outputs(
            &self,
            _a: ListOutputsArgs,
            _o: Option<&str>,
        ) -> Result<ListOutputsResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn relinquish_output(
            &self,
            _a: RelinquishOutputArgs,
            _o: Option<&str>,
        ) -> Result<RelinquishOutputResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn get_public_key(
            &self,
            _a: GetPublicKeyArgs,
            _o: Option<&str>,
        ) -> Result<GetPublicKeyResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn reveal_counterparty_key_linkage(
            &self,
            _a: RevealCounterpartyKeyLinkageArgs,
            _o: Option<&str>,
        ) -> Result<RevealCounterpartyKeyLinkageResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn reveal_specific_key_linkage(
            &self,
            _a: RevealSpecificKeyLinkageArgs,
            _o: Option<&str>,
        ) -> Result<RevealSpecificKeyLinkageResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn encrypt(
            &self,
            _a: EncryptArgs,
            _o: Option<&str>,
        ) -> Result<EncryptResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn decrypt(
            &self,
            _a: DecryptArgs,
            _o: Option<&str>,
        ) -> Result<DecryptResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn create_hmac(
            &self,
            _a: CreateHmacArgs,
            _o: Option<&str>,
        ) -> Result<CreateHmacResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn verify_hmac(
            &self,
            _a: VerifyHmacArgs,
            _o: Option<&str>,
        ) -> Result<VerifyHmacResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn create_signature(
            &self,
            _a: CreateSignatureArgs,
            _o: Option<&str>,
        ) -> Result<CreateSignatureResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn verify_signature(
            &self,
            _a: VerifySignatureArgs,
            _o: Option<&str>,
        ) -> Result<VerifySignatureResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn acquire_certificate(
            &self,
            _a: AcquireCertificateArgs,
            _o: Option<&str>,
        ) -> Result<Certificate, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn list_certificates(
            &self,
            _a: ListCertificatesArgs,
            _o: Option<&str>,
        ) -> Result<ListCertificatesResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn prove_certificate(
            &self,
            _a: ProveCertificateArgs,
            _o: Option<&str>,
        ) -> Result<ProveCertificateResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn relinquish_certificate(
            &self,
            _a: RelinquishCertificateArgs,
            _o: Option<&str>,
        ) -> Result<RelinquishCertificateResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn discover_by_identity_key(
            &self,
            _a: DiscoverByIdentityKeyArgs,
            _o: Option<&str>,
        ) -> Result<DiscoverCertificatesResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
        async fn discover_by_attributes(
            &self,
            _a: DiscoverByAttributesArgs,
            _o: Option<&str>,
        ) -> Result<DiscoverCertificatesResult, bsv::wallet::error::WalletError> {
            Err(bsv::wallet::error::WalletError::NotImplemented(
                "mock".into(),
            ))
        }
    }

    fn make_builder() -> WalletBuilderFn {
        Box::new(|_root_key, _pkm| {
            Box::pin(async move {
                let mock: Arc<dyn WalletInterface + Send + Sync> = Arc::new(MockWallet);
                Ok(mock)
            })
        })
    }

    #[tokio::test]
    async fn test_auth_manager_not_active_before_auth() {
        let wab = WABClient::new("http://localhost:3000");
        let mgr = WalletAuthenticationManager::new(
            wab,
            make_builder(),
            "admin.example.com".to_string(),
            None,
        )
        .unwrap();

        // create_action should return NotActive before authentication
        let result = mgr
            .create_action(
                CreateActionArgs {
                    description: "test action".to_string(),
                    inputs: vec![],
                    outputs: vec![],
                    labels: vec![],
                    lock_time: None,
                    version: None,
                    options: None,
                    input_beef: None,
                    reference: None,
                },
                Some("test.com"),
            )
            .await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        let err_str = err.to_string();
        assert!(
            err_str.contains("not authenticated") || err_str.contains("NOT_ACTIVE"),
            "Expected NOT_ACTIVE error, got: {}",
            err_str
        );
    }

    #[tokio::test]
    async fn test_is_authenticated_before_and_after() {
        let wab = WABClient::new("http://localhost:3000");
        let mgr = WalletAuthenticationManager::new(
            wab,
            make_builder(),
            "admin.example.com".to_string(),
            None,
        )
        .unwrap();

        // Before auth
        let auth = mgr.is_authenticated(None).await.unwrap();
        assert!(!auth.authenticated, "Should not be authenticated initially");

        // Manually set inner wallet to simulate auth completion
        {
            let mock: Arc<dyn WalletInterface + Send + Sync> = Arc::new(MockWallet);
            let mut inner = mgr.inner.write().await;
            *inner = Some(mock);
        }

        // After auth
        let auth = mgr.is_authenticated(None).await.unwrap();
        assert!(
            auth.authenticated,
            "Should be authenticated after setting inner"
        );
    }
}