nostr-bbs-core 3.0.0-rc7

Shared Nostr protocol primitives for the nostr-bbs forum (client + workers)
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
//! NIP-98: HTTP Auth via Nostr events (kind 27235).
//!
//! **Canonical Rust implementation for the DreamLab ecosystem.**
//!
//! This module is the single source of truth for NIP-98 verification across
//! nostr-rust-forum, solid-pod-rs, VisionClaw, and any future Rust consumers.
//! All verification paths converge here; downstream crates should depend on
//! `nostr_bbs_core::nip98` rather than rolling their own.
//!
//! Wire format: `Authorization: Nostr base64(json(signed_event))`
//!
//! ## Verification checks (in order)
//!
//! 1. Strip `Nostr ` prefix from the Authorization header
//! 2. Size-gate the encoded token (max 64 KiB)
//! 3. Base64-decode and JSON-parse into a [`NostrEvent`]
//! 4. Assert `kind == 27235`
//! 5. Validate pubkey format (64 hex chars)
//! 6. Check timestamp freshness within `max_age_secs` (default 60s)
//! 7. Recompute event ID from canonical NIP-01 serialisation and verify
//!    the BIP-340 Schnorr signature (never trusts the client-provided id)
//! 8. Match the `u` tag against the expected URL
//! 9. Match the `method` tag against the expected HTTP method (case-insensitive)
//! 10. If a request body is provided, verify the `payload` tag contains its SHA-256
//!
//! ## Replay protection
//!
//! The [`Nip98ReplayStore`] trait abstracts the storage backend for replay
//! detection. The `nostr-bbs-rate-limit` crate provides a D1-backed
//! implementation; tests use an in-memory map. Replay checking is performed
//! **after** all cryptographic checks pass so that the store is never
//! polluted by forged or expired events.
//!
//! ## Quick start
//!
//! ```rust,ignore
//! use nostr_bbs_core::nip98::{verify_nip98, Nip98Token, Nip98Error};
//!
//! // Stateless verification (no replay protection)
//! let token: Nip98Token = verify_nip98(
//!     auth_header,    // "Nostr base64..."
//!     expected_url,   // "https://api.example.com/endpoint"
//!     expected_method,// "POST"
//!     None,           // max_age_secs — defaults to 60
//! )?;
//! println!("Authenticated pubkey: {}", token.pubkey);
//! ```
//!
//! [`Nip98Token`] carries the validated `event_id` (recomputed by
//! `verify_event_strict`, never trusted from the client), enabling
//! callers to key replay caches on the canonical ID.

use async_trait::async_trait;
use base64::{engine::general_purpose::STANDARD as BASE64, Engine};
use k256::schnorr::SigningKey;
use sha2::{Digest, Sha256};
use thiserror::Error;

use crate::event::{sign_event, verify_event, NostrEvent, UnsignedEvent};

/// NIP-98 event kind.
const HTTP_AUTH_KIND: u64 = 27235;

/// Maximum allowed clock skew in seconds.
pub const TIMESTAMP_TOLERANCE: u64 = 60;

/// Recommended replay-cache TTL: 2x the timestamp tolerance window.
///
/// Stores accepting older event IDs only need to outlive the tolerance, so
/// `2 * TIMESTAMP_TOLERANCE` is a safe upper bound for replay-cache entries.
pub const REPLAY_CACHE_TTL_SECS: u64 = 2 * TIMESTAMP_TOLERANCE;

/// Maximum encoded event size in bytes (64 KiB, matching TS implementation).
const MAX_EVENT_SIZE: usize = 64 * 1024;

/// Authorization header prefix.
const NOSTR_PREFIX: &str = "Nostr ";

/// Replay-cache abstraction for NIP-98 event IDs.
///
/// Implementations record-and-check whether a NIP-98 event id has been seen
/// within the tolerance window. Workers wire this to KV with TTL =
/// [`REPLAY_CACHE_TTL_SECS`]; tests use an in-memory map.
///
/// Semantics: [`Nip98ReplayStore::seen_or_record`] MUST be atomic from the
/// caller's point of view — it returns `Ok(true)` on first observation and
/// records the id, returning `Ok(false)` on subsequent observations within
/// the TTL. A storage error is surfaced as `Err`.
#[async_trait(?Send)]
pub trait Nip98ReplayStore {
    /// Record `event_id` if absent and return `true` on first observation.
    /// Return `false` if `event_id` was already recorded within the TTL.
    async fn seen_or_record(&self, event_id: &str) -> Result<bool, String>;
}

/// A verified NIP-98 token with extracted fields.
///
/// Returned by all `verify_*` functions ([`verify_nip98`], [`verify_token`],
/// [`verify_token_at`], [`verify_token_full`], [`verify_token_at_with_replay`],
/// [`verify_nip98_with_replay`]) after successful verification of an
/// `Authorization: Nostr <base64(event)>` header.
///
/// The `event_id` field contains the **canonical** event ID recomputed from
/// the NIP-01 serialisation — it is never taken from the client-provided
/// payload. This makes it safe to use as a replay-cache key.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Nip98Token {
    /// Canonical event ID (validated via `verify_event_strict` before population).
    pub event_id: String,
    /// Hex-encoded x-only public key of the signer.
    pub pubkey: String,
    /// The URL this token authorizes.
    pub url: String,
    /// The HTTP method this token authorizes.
    pub method: String,
    /// SHA-256 hex hash of the request body, if present.
    pub payload_hash: Option<String>,
    /// Unix timestamp when the token was created.
    pub created_at: u64,
}

/// Errors that can occur during NIP-98 token creation or verification.
#[derive(Debug, Error)]
pub enum Nip98Error {
    #[error("invalid secret key: {0}")]
    InvalidKey(#[from] k256::schnorr::Error),

    #[error("JSON serialization failed: {0}")]
    Json(#[from] serde_json::Error),

    #[error("base64 decode failed: {0}")]
    Base64(#[from] base64::DecodeError),

    #[error("missing 'Nostr ' prefix in Authorization header")]
    MissingPrefix,

    #[error("event exceeds maximum size ({MAX_EVENT_SIZE} bytes)")]
    EventTooLarge,

    #[error("wrong event kind: expected {HTTP_AUTH_KIND}, got {0}")]
    WrongKind(u64),

    #[error("invalid pubkey: expected 64 hex chars")]
    InvalidPubkey,

    #[error(
        "timestamp expired: event created_at {event_ts} is more than {tolerance}s from now ({now})"
    )]
    TimestampExpired {
        event_ts: u64,
        now: u64,
        tolerance: u64,
    },

    #[error("missing required tag: {0}")]
    MissingTag(String),

    #[error("URL mismatch: token={token_url}, expected={expected_url}")]
    UrlMismatch {
        token_url: String,
        expected_url: String,
    },

    #[error("method mismatch: token={token_method}, expected={expected_method}")]
    MethodMismatch {
        token_method: String,
        expected_method: String,
    },

    #[error("payload hash mismatch")]
    PayloadMismatch,

    #[error("body present but no payload tag in signed event")]
    MissingPayloadTag,

    #[error("event signature verification failed")]
    InvalidSignature,

    #[error("replay detected: event id has already been seen within the tolerance window")]
    Replayed,

    #[error("replay store backend error: {0}")]
    ReplayBackend(String),
}

/// Create a NIP-98 authorization token for an HTTP request.
///
/// Returns a base64-encoded JSON string suitable for the `Authorization: Nostr <token>` header.
///
/// # Arguments
/// * `secret_key` - 32-byte secp256k1 secret key
/// * `url` - The full request URL
/// * `method` - HTTP method (GET, POST, etc.)
/// * `body` - Optional request body (will be SHA-256 hashed for the payload tag)
pub fn create_token(
    secret_key: &[u8; 32],
    url: &str,
    method: &str,
    body: Option<&[u8]>,
) -> Result<String, Nip98Error> {
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .expect("system clock before epoch")
        .as_secs();
    create_token_at(secret_key, url, method, body, now)
}

/// Like [`create_token`] but accepts an explicit Unix timestamp.
///
/// Use this variant in WASM environments where `SystemTime::now()` is unavailable.
/// The caller is responsible for providing a current timestamp (e.g., from `Date.now() / 1000`).
pub fn create_token_at(
    secret_key: &[u8; 32],
    url: &str,
    method: &str,
    body: Option<&[u8]>,
    created_at: u64,
) -> Result<String, Nip98Error> {
    let sk = SigningKey::from_bytes(secret_key)?;
    let pubkey = hex::encode(sk.verifying_key().to_bytes());

    let mut tags = vec![
        vec!["u".to_string(), url.to_string()],
        vec!["method".to_string(), method.to_string()],
    ];

    if let Some(body_bytes) = body {
        let hash = Sha256::digest(body_bytes);
        tags.push(vec!["payload".to_string(), hex::encode(hash)]);
    }

    let unsigned = UnsignedEvent {
        pubkey,
        created_at,
        kind: HTTP_AUTH_KIND,
        tags,
        content: String::new(),
    };

    // Safety: pubkey is derived from sk above, so sign_event cannot fail with PubkeyMismatch
    let signed = sign_event(unsigned, &sk)
        .expect("pubkey derived from same signing key — mismatch impossible");
    let json = serde_json::to_string(&signed)?;
    Ok(BASE64.encode(json.as_bytes()))
}

// ── Canonical verification API ─────────────────────────────────────────────
//
// The public API is layered for ergonomics and backward compatibility:
//
//   verify_nip98()                — canonical entry point (system clock, configurable tolerance)
//   verify_token()               — legacy alias using default tolerance + system clock
//   verify_token_at()            — explicit timestamp, default tolerance
//   verify_token_full()          — full control: explicit timestamp + custom tolerance
//   verify_token_at_with_replay()— full control + async replay protection
//
// All paths converge on `verify_token_full`.

/// **Canonical NIP-98 verification entry point.**
///
/// Verifies the `Authorization: Nostr <base64(event)>` header against the
/// expected URL and HTTP method. Uses the system clock for timestamp checking.
///
/// This is the recommended function for all new code across the DreamLab
/// ecosystem. It performs every check specified by NIP-98:
///
/// - Kind == 27235
/// - URL tag matches `expected_url`
/// - Method tag matches `expected_method` (case-insensitive)
/// - Timestamp within `max_age_secs` of the current time
/// - BIP-340 Schnorr signature over the canonical NIP-01 event hash
/// - Optional payload SHA-256 verification (when a request body is present)
///
/// # Arguments
///
/// * `auth_header` - The full `Authorization` header value (e.g. `"Nostr base64..."`)
/// * `expected_url` - The URL that should appear in the `u` tag
/// * `expected_method` - The HTTP method that should appear in the `method` tag
/// * `max_age_secs` - Maximum allowed clock skew in seconds. Pass `None` to use
///   the default [`TIMESTAMP_TOLERANCE`] (60 seconds).
///
/// # Returns
///
/// A [`Nip98Token`] containing the verified pubkey, URL, method, payload hash,
/// and canonical event ID (safe to use as a replay-cache key).
///
/// # Errors
///
/// Returns [`Nip98Error`] on any verification failure. The error variants are
/// specific enough for callers to distinguish authentication failures (401)
/// from authorisation failures (403) and replay attacks.
///
/// # Example
///
/// ```rust,ignore
/// use nostr_bbs_core::nip98::{verify_nip98, Nip98Token};
///
/// let token = verify_nip98(
///     "Nostr eyJpZC...",
///     "https://api.example.com/v1/data",
///     "POST",
///     None, // use default 60s tolerance
/// )?;
/// println!("Authenticated: {}", token.pubkey);
/// ```
pub fn verify_nip98(
    auth_header: &str,
    expected_url: &str,
    expected_method: &str,
    max_age_secs: Option<u64>,
) -> Result<Nip98Token, Nip98Error> {
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .expect("system clock before epoch")
        .as_secs();
    let tolerance = max_age_secs.unwrap_or(TIMESTAMP_TOLERANCE);
    verify_token_full(
        auth_header,
        expected_url,
        expected_method,
        None,
        now,
        tolerance,
    )
}

/// Verify a NIP-98 `Authorization` header value using the system clock
/// and the default [`TIMESTAMP_TOLERANCE`].
///
/// Delegates to [`verify_token_full`] with `tolerance = TIMESTAMP_TOLERANCE`.
/// For new code, prefer [`verify_nip98`] which exposes the tolerance parameter.
///
/// # Arguments
/// * `auth_header` - The full `Authorization` header value (e.g. `"Nostr base64..."`)
/// * `expected_url` - The URL that should appear in the `u` tag
/// * `expected_method` - The HTTP method that should appear in the `method` tag
/// * `body` - Optional request body bytes to verify against the `payload` tag
pub fn verify_token(
    auth_header: &str,
    expected_url: &str,
    expected_method: &str,
    body: Option<&[u8]>,
) -> Result<Nip98Token, Nip98Error> {
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .expect("system clock before epoch")
        .as_secs();
    verify_token_full(
        auth_header,
        expected_url,
        expected_method,
        body,
        now,
        TIMESTAMP_TOLERANCE,
    )
}

/// Verify a NIP-98 `Authorization` header value against an explicit timestamp
/// using the default [`TIMESTAMP_TOLERANCE`].
///
/// Use this variant in tests or worker environments where you want
/// deterministic timestamp validation instead of relying on `SystemTime::now()`.
///
/// # Arguments
/// * `auth_header` - The full `Authorization` header value (e.g. `"Nostr base64..."`)
/// * `expected_url` - The URL that should appear in the `u` tag
/// * `expected_method` - The HTTP method that should appear in the `method` tag
/// * `body` - Optional request body bytes to verify against the `payload` tag
/// * `now` - The current Unix timestamp (seconds) used for tolerance checking
pub fn verify_token_at(
    auth_header: &str,
    expected_url: &str,
    expected_method: &str,
    body: Option<&[u8]>,
    now: u64,
) -> Result<Nip98Token, Nip98Error> {
    verify_token_full(
        auth_header,
        expected_url,
        expected_method,
        body,
        now,
        TIMESTAMP_TOLERANCE,
    )
}

/// Full-control NIP-98 verification with explicit timestamp and tolerance.
///
/// This is the core verification function that all other `verify_*` functions
/// delegate to. It performs every check listed in the module-level documentation.
///
/// # Arguments
/// * `auth_header` - The full `Authorization` header value (e.g. `"Nostr base64..."`)
/// * `expected_url` - The URL that should appear in the `u` tag
/// * `expected_method` - The HTTP method that should appear in the `method` tag
/// * `body` - Optional request body bytes to verify against the `payload` tag
/// * `now` - The current Unix timestamp (seconds) used for tolerance checking
/// * `max_age_secs` - Maximum allowed absolute difference between `now` and the
///   event's `created_at` timestamp. Use [`TIMESTAMP_TOLERANCE`] for the default.
pub fn verify_token_full(
    auth_header: &str,
    expected_url: &str,
    expected_method: &str,
    body: Option<&[u8]>,
    now: u64,
    max_age_secs: u64,
) -> Result<Nip98Token, Nip98Error> {
    // 1. Strip "Nostr " prefix
    let token = auth_header
        .strip_prefix(NOSTR_PREFIX)
        .ok_or(Nip98Error::MissingPrefix)?
        .trim();

    // 2. Size check on encoded token
    if token.len() > MAX_EVENT_SIZE {
        return Err(Nip98Error::EventTooLarge);
    }

    // 3. Base64 decode and parse JSON
    let json_bytes = BASE64.decode(token)?;
    if json_bytes.len() > MAX_EVENT_SIZE {
        return Err(Nip98Error::EventTooLarge);
    }
    let event: NostrEvent = serde_json::from_slice(&json_bytes)?;

    // 4. Check kind
    if event.kind != HTTP_AUTH_KIND {
        return Err(Nip98Error::WrongKind(event.kind));
    }

    // 5. Check pubkey format
    if event.pubkey.len() != 64 || hex::decode(&event.pubkey).is_err() {
        return Err(Nip98Error::InvalidPubkey);
    }

    // 6. Timestamp within tolerance
    if now.abs_diff(event.created_at) > max_age_secs {
        return Err(Nip98Error::TimestampExpired {
            event_ts: event.created_at,
            now,
            tolerance: max_age_secs,
        });
    }

    // 7. Verify event integrity (recomputes ID from scratch + Schnorr sig check)
    if !verify_event(&event) {
        return Err(Nip98Error::InvalidSignature);
    }

    // 8. Extract and verify URL tag
    let token_url = get_tag(&event, "u").ok_or_else(|| Nip98Error::MissingTag("u".into()))?;
    if token_url != expected_url {
        return Err(Nip98Error::UrlMismatch {
            token_url: token_url.clone(),
            expected_url: expected_url.to_string(),
        });
    }

    // 9. Extract and verify method tag
    let token_method =
        get_tag(&event, "method").ok_or_else(|| Nip98Error::MissingTag("method".into()))?;
    if token_method.to_uppercase() != expected_method.to_uppercase() {
        return Err(Nip98Error::MethodMismatch {
            token_method,
            expected_method: expected_method.to_string(),
        });
    }

    // 10. Payload hash verification
    let payload_tag = get_tag(&event, "payload");

    let verified_payload_hash = if let Some(body_bytes) = body {
        if !body_bytes.is_empty() {
            // Body present — payload tag MUST exist
            let expected_hash = payload_tag.as_ref().ok_or(Nip98Error::MissingPayloadTag)?;
            let actual_hash = hex::encode(Sha256::digest(body_bytes));
            if expected_hash.to_lowercase() != actual_hash.to_lowercase() {
                return Err(Nip98Error::PayloadMismatch);
            }
            Some(expected_hash.clone())
        } else {
            payload_tag
        }
    } else {
        payload_tag
    };

    Ok(Nip98Token {
        event_id: event.id,
        pubkey: event.pubkey,
        url: token_url,
        method: token_method,
        payload_hash: verified_payload_hash,
        created_at: event.created_at,
    })
}

/// Verify a NIP-98 token AND enforce replay protection via [`Nip98ReplayStore`].
///
/// This is the recommended verification path for all worker endpoints. The
/// store is consulted only AFTER the cryptographic checks pass — replay
/// recording is a side effect of a structurally-valid event, never of a
/// forged or expired one. If the event id has already been seen within the
/// store's TTL, returns [`Nip98Error::Replayed`].
///
/// Uses the default [`TIMESTAMP_TOLERANCE`]. For custom tolerance, use
/// [`verify_nip98_with_replay`].
///
/// # Arguments
/// As [`verify_token_at`], plus `replay_store` — a store keyed by the
/// 32-byte hex event id with a TTL of at least [`REPLAY_CACHE_TTL_SECS`].
pub async fn verify_token_at_with_replay(
    auth_header: &str,
    expected_url: &str,
    expected_method: &str,
    body: Option<&[u8]>,
    now: u64,
    replay_store: &dyn Nip98ReplayStore,
) -> Result<Nip98Token, Nip98Error> {
    verify_nip98_with_replay(
        auth_header,
        expected_url,
        expected_method,
        body,
        now,
        TIMESTAMP_TOLERANCE,
        replay_store,
    )
    .await
}

/// Full-control NIP-98 verification with replay protection.
///
/// Combines [`verify_token_full`] with async replay detection. The replay
/// store is only consulted after all stateless cryptographic checks pass,
/// so the store is never polluted by forged or expired events.
///
/// # Arguments
///
/// * `auth_header` - Full `Authorization` header value
/// * `expected_url` - URL that should appear in the `u` tag
/// * `expected_method` - HTTP method that should appear in the `method` tag
/// * `body` - Optional request body for payload hash verification
/// * `now` - Current Unix timestamp (seconds)
/// * `max_age_secs` - Maximum allowed timestamp skew
/// * `replay_store` - Backend for atomic replay detection (D1, KV, in-memory, etc.)
pub async fn verify_nip98_with_replay(
    auth_header: &str,
    expected_url: &str,
    expected_method: &str,
    body: Option<&[u8]>,
    now: u64,
    max_age_secs: u64,
    replay_store: &dyn Nip98ReplayStore,
) -> Result<Nip98Token, Nip98Error> {
    // Run all stateless cryptographic + structural checks first.
    // verify_token_full recomputes and validates the event ID, so
    // token.event_id is the canonical ID — not client-supplied.
    let token = verify_token_full(
        auth_header,
        expected_url,
        expected_method,
        body,
        now,
        max_age_secs,
    )?;

    let first_seen = replay_store
        .seen_or_record(&token.event_id)
        .await
        .map_err(Nip98Error::ReplayBackend)?;
    if !first_seen {
        return Err(Nip98Error::Replayed);
    }

    Ok(token)
}

/// Helper: extract the first value for a tag name from an event's tags.
fn get_tag(event: &NostrEvent, name: &str) -> Option<String> {
    event
        .tags
        .iter()
        .find(|t| t.first().map(|s| s.as_str()) == Some(name))
        .and_then(|t| t.get(1).cloned())
}

/// Build a full `Authorization` header value from a token string.
pub fn authorization_header(token: &str) -> String {
    format!("{NOSTR_PREFIX}{token}")
}

/// Build a ready-to-send `Authorization: Nostr <base64>` header value for
/// an outbound HTTP request.
///
/// Convenience wrapper for CLI/worker clients that need the full header
/// value in one call. Reuses [`create_token`] internally — the signing
/// logic lives in a single place.
///
/// # Arguments
/// * `secret_key` - 32-byte secp256k1 secret key
/// * `url` - The full request URL
/// * `method` - HTTP method (GET, POST, etc.)
/// * `body` - Optional request body (hashed into the payload tag if present)
pub fn sign_request_header(
    secret_key: &[u8; 32],
    url: &str,
    method: &str,
    body: Option<&[u8]>,
) -> Result<String, Nip98Error> {
    let token = create_token(secret_key, url, method, body)?;
    Ok(authorization_header(&token))
}

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

    fn test_secret_key() -> [u8; 32] {
        // Deterministic key for tests
        let mut key = [0u8; 32];
        key[0] = 0x01;
        key[31] = 0x01;
        key
    }

    fn test_signing_key() -> SigningKey {
        SigningKey::from_bytes(&test_secret_key()).unwrap()
    }

    // ── Roundtrip ──────────────────────────────────────────────────────

    #[test]
    fn nip98_create_verify_roundtrip_no_body() {
        let sk = test_secret_key();
        let url = "https://api.example.com/upload";
        let method = "GET";

        let token = create_token(&sk, url, method, None).unwrap();
        let header = authorization_header(&token);
        let result = verify_token(&header, url, method, None).unwrap();

        let expected_pubkey = hex::encode(test_signing_key().verifying_key().to_bytes());
        assert_eq!(result.pubkey, expected_pubkey);
        assert_eq!(result.url, url);
        assert_eq!(result.method, method);
        assert!(result.payload_hash.is_none());
    }

    #[test]
    fn nip98_create_verify_roundtrip_with_body() {
        let sk = test_secret_key();
        let url = "https://api.example.com/data";
        let method = "POST";
        let body = b"hello world";

        let token = create_token(&sk, url, method, Some(body)).unwrap();
        let header = authorization_header(&token);
        let result = verify_token(&header, url, method, Some(body)).unwrap();

        assert_eq!(result.method, method);
        assert!(result.payload_hash.is_some());

        // Verify the payload hash matches SHA-256 of body
        let expected_hash = hex::encode(Sha256::digest(body));
        assert_eq!(result.payload_hash.unwrap(), expected_hash);
    }

    // ── Rejection tests ────────────────────────────────────────────────

    #[test]
    fn nip98_reject_wrong_url() {
        let sk = test_secret_key();
        let token = create_token(&sk, "https://good.com/api", "GET", None).unwrap();
        let header = authorization_header(&token);

        let err = verify_token(&header, "https://evil.com/api", "GET", None).unwrap_err();
        assert!(matches!(err, Nip98Error::UrlMismatch { .. }));
    }

    #[test]
    fn nip98_reject_wrong_method() {
        let sk = test_secret_key();
        let token = create_token(&sk, "https://api.example.com/x", "GET", None).unwrap();
        let header = authorization_header(&token);

        let err = verify_token(&header, "https://api.example.com/x", "POST", None).unwrap_err();
        assert!(matches!(err, Nip98Error::MethodMismatch { .. }));
    }

    #[test]
    fn nip98_reject_missing_prefix() {
        let err = verify_token("Bearer abc123", "https://x.com", "GET", None).unwrap_err();
        assert!(matches!(err, Nip98Error::MissingPrefix));
    }

    #[test]
    fn nip98_reject_invalid_base64() {
        let err = verify_token("Nostr !!!not-base64!!!", "https://x.com", "GET", None).unwrap_err();
        // Could be base64 decode error or JSON parse error
        assert!(
            matches!(err, Nip98Error::Base64(_)) || matches!(err, Nip98Error::Json(_)),
            "expected Base64 or Json error, got: {err}"
        );
    }

    #[test]
    fn nip98_reject_tampered_signature() {
        let sk = test_secret_key();
        let url = "https://api.example.com/test";
        let token_b64 = create_token(&sk, url, "GET", None).unwrap();

        // Decode, tamper with sig, re-encode
        let json_bytes = BASE64.decode(&token_b64).unwrap();
        let mut event: NostrEvent = serde_json::from_slice(&json_bytes).unwrap();
        // Flip a byte in the signature
        let mut sig_bytes = hex::decode(&event.sig).unwrap();
        sig_bytes[0] ^= 0xFF;
        event.sig = hex::encode(&sig_bytes);

        let tampered_json = serde_json::to_string(&event).unwrap();
        let tampered_b64 = BASE64.encode(tampered_json.as_bytes());
        let header = authorization_header(&tampered_b64);

        let err = verify_token(&header, url, "GET", None).unwrap_err();
        assert!(matches!(err, Nip98Error::InvalidSignature));
    }

    #[test]
    fn nip98_reject_payload_mismatch() {
        let sk = test_secret_key();
        let url = "https://api.example.com/upload";
        let original_body = b"original content";
        let tampered_body = b"tampered content";

        let token = create_token(&sk, url, "POST", Some(original_body)).unwrap();
        let header = authorization_header(&token);

        let err = verify_token(&header, url, "POST", Some(tampered_body)).unwrap_err();
        assert!(matches!(err, Nip98Error::PayloadMismatch));
    }

    #[test]
    fn nip98_reject_body_without_payload_tag() {
        let sk = test_secret_key();
        let url = "https://api.example.com/upload";

        // Create token WITHOUT body (no payload tag)
        let token = create_token(&sk, url, "POST", None).unwrap();
        let header = authorization_header(&token);

        // Verify WITH body — should reject because payload tag is missing
        let err = verify_token(&header, url, "POST", Some(b"sneaky body")).unwrap_err();
        assert!(matches!(err, Nip98Error::MissingPayloadTag));
    }

    #[test]
    fn nip98_reject_expired_timestamp() {
        let signing_key = test_signing_key();
        let pubkey = hex::encode(signing_key.verifying_key().to_bytes());
        let url = "https://api.example.com/old";

        // Manually create an event with an old timestamp
        let old_time = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs()
            - 120; // 2 minutes ago, beyond 60s tolerance

        let unsigned = UnsignedEvent {
            pubkey,
            created_at: old_time,
            kind: HTTP_AUTH_KIND,
            tags: vec![
                vec!["u".to_string(), url.to_string()],
                vec!["method".to_string(), "GET".to_string()],
            ],
            content: String::new(),
        };

        let signed = sign_event(unsigned, &signing_key).expect("pubkey derived from same key");
        let json = serde_json::to_string(&signed).unwrap();
        let b64 = BASE64.encode(json.as_bytes());
        let header = authorization_header(&b64);

        let err = verify_token(&header, url, "GET", None).unwrap_err();
        assert!(matches!(err, Nip98Error::TimestampExpired { .. }));
    }

    // ── verify_token_at tests ─────────────────────────────────────────

    #[test]
    fn nip98_verify_token_at_accepts_matching_timestamp() {
        let sk = test_secret_key();
        let url = "https://api.example.com/at";
        let created_at = 1_700_000_000u64;

        let token = create_token_at(&sk, url, "GET", None, created_at).unwrap();
        let header = authorization_header(&token);

        // Verify at the same timestamp
        let result = verify_token_at(&header, url, "GET", None, created_at).unwrap();
        assert_eq!(result.created_at, created_at);
    }

    #[test]
    fn nip98_verify_token_at_accepts_within_tolerance() {
        let sk = test_secret_key();
        let url = "https://api.example.com/at";
        let created_at = 1_700_000_000u64;

        let token = create_token_at(&sk, url, "POST", Some(b"body"), created_at).unwrap();
        let header = authorization_header(&token);

        // 30 seconds later -- within the 60s tolerance
        let result = verify_token_at(&header, url, "POST", Some(b"body"), created_at + 30).unwrap();
        assert_eq!(result.created_at, created_at);
    }

    #[test]
    fn nip98_verify_token_at_rejects_beyond_tolerance() {
        let sk = test_secret_key();
        let url = "https://api.example.com/at";
        let created_at = 1_700_000_000u64;

        let token = create_token_at(&sk, url, "GET", None, created_at).unwrap();
        let header = authorization_header(&token);

        // 120 seconds later -- beyond the 60s tolerance
        let err = verify_token_at(&header, url, "GET", None, created_at + 120).unwrap_err();
        assert!(matches!(err, Nip98Error::TimestampExpired { .. }));
    }

    #[test]
    fn nip98_verify_token_at_rejects_future_beyond_tolerance() {
        let sk = test_secret_key();
        let url = "https://api.example.com/at";
        let created_at = 1_700_000_100u64;

        let token = create_token_at(&sk, url, "GET", None, created_at).unwrap();
        let header = authorization_header(&token);

        // Verify at a time 120s BEFORE the event -- future event beyond tolerance
        let err = verify_token_at(&header, url, "GET", None, created_at - 120).unwrap_err();
        assert!(matches!(err, Nip98Error::TimestampExpired { .. }));
    }

    // ── Edge cases ─────────────────────────────────────────────────────

    #[test]
    fn nip98_url_trailing_slash_mismatch_rejected() {
        let sk = test_secret_key();
        let url_with_slash = "https://api.example.com/path/";
        let url_without_slash = "https://api.example.com/path";

        let token = create_token(&sk, url_with_slash, "GET", None).unwrap();
        let header = authorization_header(&token);

        let err = verify_token(&header, url_without_slash, "GET", None).unwrap_err();
        assert!(matches!(err, Nip98Error::UrlMismatch { .. }));
    }

    #[test]
    fn nip98_method_case_insensitive() {
        let sk = test_secret_key();
        let url = "https://api.example.com/test";

        let token = create_token(&sk, url, "post", None).unwrap();
        let header = authorization_header(&token);

        // Should succeed: method comparison is case-insensitive
        let result = verify_token(&header, url, "POST", None).unwrap();
        assert_eq!(result.method, "post");
    }

    #[test]
    fn nip98_empty_body_no_payload_tag_required() {
        let sk = test_secret_key();
        let url = "https://api.example.com/get";

        let token = create_token(&sk, url, "GET", None).unwrap();
        let header = authorization_header(&token);

        // No body on verify side either — should pass
        let result = verify_token(&header, url, "GET", None).unwrap();
        assert!(result.payload_hash.is_none());
    }

    #[test]
    fn nip98_authorization_header_format() {
        let token = "abc123";
        let header = authorization_header(token);
        assert_eq!(header, "Nostr abc123");
    }

    // ── Additional edge-case tests ──────────────────────────────────────

    #[test]
    fn nip98_reject_wrong_kind() {
        let sk = test_signing_key();
        let pubkey = hex::encode(sk.verifying_key().to_bytes());
        let url = "https://api.example.com/test";
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();

        // Manually create an event with wrong kind (kind 1 instead of 27235)
        let unsigned = UnsignedEvent {
            pubkey,
            created_at: now,
            kind: 1, // wrong kind
            tags: vec![
                vec!["u".to_string(), url.to_string()],
                vec!["method".to_string(), "GET".to_string()],
            ],
            content: String::new(),
        };
        let signed = sign_event(unsigned, &sk).unwrap();
        let json = serde_json::to_string(&signed).unwrap();
        let b64 = BASE64.encode(json.as_bytes());
        let header = authorization_header(&b64);

        let err = verify_token(&header, url, "GET", None).unwrap_err();
        assert!(matches!(err, Nip98Error::WrongKind(1)));
    }

    #[test]
    fn nip98_reject_missing_url_tag() {
        let sk = test_signing_key();
        let pubkey = hex::encode(sk.verifying_key().to_bytes());
        let url = "https://api.example.com/test";
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();

        // Event with method tag but no u tag
        let unsigned = UnsignedEvent {
            pubkey,
            created_at: now,
            kind: HTTP_AUTH_KIND,
            tags: vec![vec!["method".to_string(), "GET".to_string()]],
            content: String::new(),
        };
        let signed = sign_event(unsigned, &sk).unwrap();
        let json = serde_json::to_string(&signed).unwrap();
        let b64 = BASE64.encode(json.as_bytes());
        let header = authorization_header(&b64);

        let err = verify_token(&header, url, "GET", None).unwrap_err();
        assert!(matches!(err, Nip98Error::MissingTag(_)));
    }

    #[test]
    fn nip98_reject_missing_method_tag() {
        let sk = test_signing_key();
        let pubkey = hex::encode(sk.verifying_key().to_bytes());
        let url = "https://api.example.com/test";
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_secs();

        // Event with u tag but no method tag
        let unsigned = UnsignedEvent {
            pubkey,
            created_at: now,
            kind: HTTP_AUTH_KIND,
            tags: vec![vec!["u".to_string(), url.to_string()]],
            content: String::new(),
        };
        let signed = sign_event(unsigned, &sk).unwrap();
        let json = serde_json::to_string(&signed).unwrap();
        let b64 = BASE64.encode(json.as_bytes());
        let header = authorization_header(&b64);

        let err = verify_token(&header, url, "GET", None).unwrap_err();
        assert!(matches!(err, Nip98Error::MissingTag(_)));
    }

    #[test]
    fn nip98_empty_body_with_body_bytes_passes() {
        let sk = test_secret_key();
        let url = "https://api.example.com/test";

        // Create token without body
        let token = create_token(&sk, url, "POST", None).unwrap();
        let header = authorization_header(&token);

        // Verify with empty body slice (not None) -- should pass since body is empty
        let result = verify_token(&header, url, "POST", Some(b"")).unwrap();
        assert!(result.payload_hash.is_none());
    }

    #[test]
    fn nip98_get_tag_returns_none_for_missing() {
        let event = NostrEvent {
            id: "00".repeat(32),
            pubkey: "aa".repeat(32),
            created_at: 0,
            kind: 1,
            tags: vec![vec!["e".into(), "ref1".into()]],
            content: String::new(),
            sig: "00".repeat(64),
        };
        assert!(get_tag(&event, "u").is_none());
        assert_eq!(get_tag(&event, "e"), Some("ref1".to_string()));
    }

    // ── verify_nip98 (canonical API) ─────────────────────────────────────

    #[test]
    fn nip98_verify_nip98_default_tolerance() {
        let sk = test_secret_key();
        let url = "https://api.example.com/canonical";
        let token = create_token(&sk, url, "GET", None).unwrap();
        let header = authorization_header(&token);

        // None means default 60s tolerance — token was just created, so it's fresh.
        let result = verify_nip98(&header, url, "GET", None).unwrap();
        assert_eq!(result.url, url);
        assert_eq!(result.method, "GET");
    }

    #[test]
    fn nip98_verify_nip98_custom_tolerance() {
        let sk = test_secret_key();
        let url = "https://api.example.com/custom";
        let created_at = 1_700_000_000u64;

        let token = create_token_at(&sk, url, "POST", None, created_at).unwrap();
        let header = authorization_header(&token);

        // 90-second offset should fail with default 60s tolerance
        let err = verify_token_at(&header, url, "POST", None, created_at + 90).unwrap_err();
        assert!(matches!(err, Nip98Error::TimestampExpired { .. }));

        // But should succeed with a 120s tolerance via verify_token_full
        let result = verify_token_full(&header, url, "POST", None, created_at + 90, 120).unwrap();
        assert_eq!(result.url, url);
        assert_eq!(result.created_at, created_at);
    }

    #[test]
    fn nip98_verify_token_full_strict_tolerance() {
        let sk = test_secret_key();
        let url = "https://api.example.com/strict";
        let created_at = 1_700_000_000u64;

        let token = create_token_at(&sk, url, "GET", None, created_at).unwrap();
        let header = authorization_header(&token);

        // 10-second tolerance: 5s offset should pass
        let result = verify_token_full(&header, url, "GET", None, created_at + 5, 10).unwrap();
        assert_eq!(result.created_at, created_at);

        // 10-second tolerance: 15s offset should fail
        let err = verify_token_full(&header, url, "GET", None, created_at + 15, 10).unwrap_err();
        assert!(matches!(
            err,
            Nip98Error::TimestampExpired { tolerance: 10, .. }
        ));
    }

    #[test]
    fn nip98_timestamp_expired_error_includes_tolerance() {
        let sk = test_secret_key();
        let url = "https://api.example.com/err";
        let created_at = 1_700_000_000u64;

        let token = create_token_at(&sk, url, "GET", None, created_at).unwrap();
        let header = authorization_header(&token);

        let err = verify_token_full(&header, url, "GET", None, created_at + 200, 30).unwrap_err();
        match err {
            Nip98Error::TimestampExpired {
                event_ts,
                now,
                tolerance,
            } => {
                assert_eq!(event_ts, created_at);
                assert_eq!(now, created_at + 200);
                assert_eq!(tolerance, 30);
            }
            other => panic!("expected TimestampExpired, got: {other}"),
        }
    }

    // ── Replay protection ──────────────────────────────────────────────
    //
    // In-memory replay store for unit tests. The trait is `?Send` so it
    // works in both native and wasm32; the cell is a single-threaded
    // `RefCell` which is fine here.

    use std::cell::RefCell;
    use std::collections::HashSet;

    struct InMemoryReplayStore {
        seen: RefCell<HashSet<String>>,
    }

    impl InMemoryReplayStore {
        fn new() -> Self {
            Self {
                seen: RefCell::new(HashSet::new()),
            }
        }
    }

    #[async_trait::async_trait(?Send)]
    impl Nip98ReplayStore for InMemoryReplayStore {
        async fn seen_or_record(&self, event_id: &str) -> Result<bool, String> {
            let mut g = self.seen.borrow_mut();
            if g.contains(event_id) {
                Ok(false)
            } else {
                g.insert(event_id.to_string());
                Ok(true)
            }
        }
    }

    /// Tiny block_on for unit tests (the replay store futures resolve immediately).
    fn block_on<F: std::future::Future>(f: F) -> F::Output {
        use std::task::{Context, Poll, RawWaker, RawWakerVTable, Waker};
        fn noop_clone(p: *const ()) -> RawWaker {
            RawWaker::new(p, &VTAB)
        }
        fn noop(_: *const ()) {}
        static VTAB: RawWakerVTable = RawWakerVTable::new(noop_clone, noop, noop, noop);
        let waker = unsafe { Waker::from_raw(RawWaker::new(std::ptr::null(), &VTAB)) };
        let mut cx = Context::from_waker(&waker);
        let mut pinned = Box::pin(f);
        loop {
            match pinned.as_mut().poll(&mut cx) {
                Poll::Ready(v) => return v,
                Poll::Pending => continue, // not used here, but safe
            }
        }
    }

    #[test]
    fn nip98_replay_first_use_succeeds() {
        let sk = test_secret_key();
        let url = "https://api.example.com/replay";
        let ts = 1_700_000_000u64;
        let token = create_token_at(&sk, url, "POST", Some(b"x"), ts).unwrap();
        let header = authorization_header(&token);

        let store = InMemoryReplayStore::new();
        let result = block_on(verify_token_at_with_replay(
            &header,
            url,
            "POST",
            Some(b"x"),
            ts,
            &store,
        ));
        assert!(result.is_ok(), "first use must succeed");
    }

    #[test]
    fn nip98_replay_second_use_rejected() {
        let sk = test_secret_key();
        let url = "https://api.example.com/replay";
        let ts = 1_700_000_000u64;
        let token = create_token_at(&sk, url, "POST", Some(b"y"), ts).unwrap();
        let header = authorization_header(&token);

        let store = InMemoryReplayStore::new();

        // First use records the id.
        let _ = block_on(verify_token_at_with_replay(
            &header,
            url,
            "POST",
            Some(b"y"),
            ts,
            &store,
        ))
        .unwrap();

        // Second use of the SAME header (within tolerance) MUST be rejected.
        let err = block_on(verify_token_at_with_replay(
            &header,
            url,
            "POST",
            Some(b"y"),
            ts,
            &store,
        ))
        .unwrap_err();
        assert!(
            matches!(err, Nip98Error::Replayed),
            "second use must be Replayed, got {err}"
        );
    }

    #[test]
    fn nip98_replay_different_events_independent() {
        let sk = test_secret_key();
        let url = "https://api.example.com/replay";
        let ts = 1_700_000_000u64;
        let t1 = create_token_at(&sk, url, "POST", Some(b"a"), ts).unwrap();
        let t2 = create_token_at(&sk, url, "POST", Some(b"b"), ts).unwrap();
        let h1 = authorization_header(&t1);
        let h2 = authorization_header(&t2);

        let store = InMemoryReplayStore::new();
        assert!(block_on(verify_token_at_with_replay(
            &h1,
            url,
            "POST",
            Some(b"a"),
            ts,
            &store,
        ))
        .is_ok());
        // Different body -> different event id -> independent record.
        assert!(block_on(verify_token_at_with_replay(
            &h2,
            url,
            "POST",
            Some(b"b"),
            ts,
            &store,
        ))
        .is_ok());
    }

    #[test]
    fn nip98_create_token_at_deterministic() {
        let sk = test_secret_key();
        let url = "https://api.example.com/test";
        let ts = 1_700_000_000u64;

        // Two tokens created at the same timestamp for same inputs
        let t1 = create_token_at(&sk, url, "GET", None, ts).unwrap();
        let t2 = create_token_at(&sk, url, "GET", None, ts).unwrap();

        // Decode both and check they have the same pubkey, url, method, created_at
        let b1 = BASE64.decode(&t1).unwrap();
        let e1: NostrEvent = serde_json::from_slice(&b1).unwrap();
        let b2 = BASE64.decode(&t2).unwrap();
        let e2: NostrEvent = serde_json::from_slice(&b2).unwrap();

        assert_eq!(e1.pubkey, e2.pubkey);
        assert_eq!(e1.created_at, e2.created_at);
        assert_eq!(e1.kind, e2.kind);
        assert_eq!(e1.tags, e2.tags);
    }
}

// ── Property-based tests (native only, proptest not available in wasm32) ─

#[cfg(test)]
#[cfg(not(target_arch = "wasm32"))]
mod proptests {
    use super::*;
    use proptest::prelude::*;

    fn test_secret_key() -> [u8; 32] {
        let mut key = [0u8; 32];
        key[0] = 0x01;
        key[31] = 0x01;
        key
    }

    proptest! {
        #[test]
        fn nip98_roundtrip_arbitrary_url(
            path in "[a-z]{1,20}(/[a-z0-9]{1,10}){0,5}"
        ) {
            let sk = test_secret_key();
            let url = format!("https://api.example.com/{path}");
            let ts = 1_700_000_000u64;

            let token = create_token_at(&sk, &url, "GET", None, ts).unwrap();
            let header = authorization_header(&token);
            let result = verify_token_at(&header, &url, "GET", None, ts);
            prop_assert!(result.is_ok(), "Failed for URL: {url}");
            let verified = result.unwrap();
            prop_assert_eq!(verified.url, url);
        }

        #[test]
        fn nip98_roundtrip_arbitrary_method(
            method in "(GET|POST|PUT|DELETE|PATCH|HEAD)"
        ) {
            let sk = test_secret_key();
            let url = "https://api.example.com/test";
            let ts = 1_700_000_000u64;

            let token = create_token_at(&sk, url, &method, None, ts).unwrap();
            let header = authorization_header(&token);
            let result = verify_token_at(&header, url, &method, None, ts);
            prop_assert!(result.is_ok());
        }

        #[test]
        fn nip98_roundtrip_arbitrary_body(
            body in prop::collection::vec(any::<u8>(), 1..200)
        ) {
            let sk = test_secret_key();
            let url = "https://api.example.com/upload";
            let ts = 1_700_000_000u64;

            let token = create_token_at(&sk, url, "POST", Some(&body), ts).unwrap();
            let header = authorization_header(&token);
            let result = verify_token_at(&header, url, "POST", Some(&body), ts);
            prop_assert!(result.is_ok());
        }

        #[test]
        fn nip98_timestamp_within_tolerance_always_passes(
            offset in 0u64..=60
        ) {
            let sk = test_secret_key();
            let url = "https://api.example.com/time";
            let created_at = 1_700_000_000u64;

            let token = create_token_at(&sk, url, "GET", None, created_at).unwrap();
            let header = authorization_header(&token);

            // Verify at created_at + offset (within 60s tolerance)
            let result = verify_token_at(&header, url, "GET", None, created_at + offset);
            prop_assert!(result.is_ok(), "Failed at offset {offset}s");
        }

        #[test]
        fn nip98_timestamp_beyond_tolerance_always_fails(
            offset in 61u64..=600
        ) {
            let sk = test_secret_key();
            let url = "https://api.example.com/time";
            let created_at = 1_700_000_000u64;

            let token = create_token_at(&sk, url, "GET", None, created_at).unwrap();
            let header = authorization_header(&token);

            // Verify at created_at + offset (beyond 60s tolerance)
            let result = verify_token_at(&header, url, "GET", None, created_at + offset);
            prop_assert!(result.is_err(), "Should fail at offset {offset}s");
        }
    }
}