near-kit 0.9.0

A clean, ergonomic Rust client for NEAR Protocol
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
//! NEP-413: Off-chain message signing for authentication.
//!
//! NEP-413 enables users to sign messages for authentication and ownership verification
//! without gas fees or blockchain transactions.
//!
//! # Example
//!
//! ```rust,no_run
//! use near_kit::{Near, InMemorySigner, nep413};
//!
//! # async fn example() -> Result<(), near_kit::Error> {
//! let signer = InMemorySigner::new(
//!     "alice.testnet",
//!     "ed25519:..."
//! )?;
//!
//! let near = Near::testnet()
//!     .signer(signer)
//!     .build();
//!
//! // Sign a message
//! let params = nep413::SignMessageParams {
//!     message: "Login to MyApp".to_string(),
//!     recipient: "myapp.com".to_string(),
//!     nonce: nep413::generate_nonce(),
//!     callback_url: None,
//!     state: None,
//! };
//!
//! let signed = near.sign_message(params.clone()).await?;
//!
//! // Verify the signature
//! let is_valid = nep413::verify(&signed, &params, &near, Default::default()).await?;
//! # Ok(())
//! # }
//! ```
//!
//! @see <https://github.com/near/NEPs/blob/master/neps/nep-0413.md>

use std::time::Duration;

use borsh::BorshSerialize;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_with::{hex::Hex, serde_as};

use crate::Near;
use crate::error::Error;
use crate::types::{AccountId, BlockReference, CryptoHash, PublicKey, Signature};

/// NEP-413 tag prefix: 2^31 + 413 = 2147484061
///
/// This prefix ensures that signed messages cannot be confused with valid transactions.
/// The tag makes the message too long to be a valid signer account ID.
pub const NEP413_TAG: u32 = (1 << 31) + 413;

/// Default maximum age for signature validity (5 minutes).
pub const DEFAULT_MAX_AGE: Duration = Duration::from_secs(5 * 60);

// ============================================================================
// Types
// ============================================================================

/// Parameters for signing a NEP-413 message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SignMessageParams {
    /// The message to sign.
    pub message: String,

    /// The recipient identifier (e.g., "alice.near" or "myapp.com").
    pub recipient: String,

    /// A 32-byte nonce for replay protection.
    /// Use [`generate_nonce()`] to create one with an embedded timestamp.
    pub nonce: [u8; 32],

    /// Optional callback URL for web wallets.
    pub callback_url: Option<String>,

    /// Optional state parameter for CSRF protection.
    pub state: Option<String>,
}

/// HTTP request payload for NEP-413 authentication.
///
/// This is the typical JSON structure sent from a frontend to a backend
/// for authentication. Use this to deserialize the HTTP request body.
///
/// # Example
///
/// ```rust,no_run
/// use near_kit::nep413::{AuthPayload, verify_signature, DEFAULT_MAX_AGE};
///
/// // Parse JSON from HTTP request body (in a real app, from req.body)
/// fn handle_login(body: &str) -> bool {
///     let payload: AuthPayload = serde_json::from_str(body).unwrap();
///     let params = payload.to_params();
///     verify_signature(&payload.signed_message, &params, DEFAULT_MAX_AGE)
/// }
/// ```
#[serde_as]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct AuthPayload {
    /// The signed message from the client.
    pub signed_message: SignedMessage,

    /// The nonce as a hex-encoded string (64 characters for 32 bytes).
    #[serde_as(as = "Hex")]
    pub nonce: [u8; 32],

    /// The message that was signed (must match what the client signed).
    pub message: String,

    /// The recipient identifier (must match what the client signed).
    pub recipient: String,

    /// Optional callback URL (must match what the client signed, if any).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub callback_url: Option<String>,
}

impl AuthPayload {
    /// Convert to [`SignMessageParams`] for verification.
    pub fn to_params(&self) -> SignMessageParams {
        SignMessageParams {
            message: self.message.clone(),
            recipient: self.recipient.clone(),
            nonce: self.nonce,
            callback_url: self.callback_url.clone(),
            state: self.signed_message.state.clone(),
        }
    }

    /// Create an `AuthPayload` from a signed message and the original parameters.
    ///
    /// This is useful when you want to generate the same JSON format that a
    /// TypeScript client would produce, for example when testing or when a
    /// Rust client needs to authenticate to a service.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use near_kit::{Near, nep413};
    ///
    /// # async fn example() -> Result<(), near_kit::Error> {
    /// let near = Near::testnet()
    ///     .credentials("ed25519:...", "alice.testnet")?
    ///     .build();
    ///
    /// let params = nep413::SignMessageParams {
    ///     message: "Sign in to My App".to_string(),
    ///     recipient: "myapp.com".to_string(),
    ///     nonce: nep413::generate_nonce(),
    ///     callback_url: None,
    ///     state: None,
    /// };
    ///
    /// let signed = near.sign_message(params.clone()).await?;
    /// let payload = nep413::AuthPayload::from_signed(signed, &params);
    ///
    /// // Serialize to JSON for HTTP request
    /// let json = serde_json::to_string(&payload)?;
    /// println!("POST body: {}", json);
    /// # Ok(())
    /// # }
    /// ```
    pub fn from_signed(signed_message: SignedMessage, params: &SignMessageParams) -> Self {
        Self {
            signed_message,
            nonce: params.nonce,
            message: params.message.clone(),
            recipient: params.recipient.clone(),
            callback_url: params.callback_url.clone(),
        }
    }
}

/// Internal Borsh-serializable payload matching NEP-413 spec.
#[derive(BorshSerialize)]
struct Nep413Payload {
    message: String,
    nonce: [u8; 32],
    recipient: String,
    callback_url: Option<String>,
}

/// A signed NEP-413 message.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SignedMessage {
    /// The account that signed the message.
    pub account_id: AccountId,

    /// The public key used to sign.
    pub public_key: PublicKey,

    /// The signature (base64 encoded in JSON).
    #[serde(
        serialize_with = "serialize_signature_base64",
        deserialize_with = "deserialize_signature_flexible"
    )]
    pub signature: Signature,

    /// Optional state parameter for CSRF protection.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub state: Option<String>,
}

/// Options for signature verification.
#[derive(Debug, Clone)]
pub struct VerifyOptions {
    /// Maximum age for the signature to be considered valid.
    /// Set to `Duration::MAX` to disable expiration checking.
    /// Default: 5 minutes.
    pub max_age: Duration,

    /// Whether to verify that the public key belongs to the account
    /// and has full access permission via RPC.
    /// Default: true.
    pub require_full_access: bool,
}

impl Default for VerifyOptions {
    fn default() -> Self {
        Self {
            max_age: DEFAULT_MAX_AGE,
            require_full_access: true,
        }
    }
}

// ============================================================================
// Core Functions
// ============================================================================

/// Generate a 32-byte nonce with an embedded timestamp for expiration checking.
///
/// The nonce structure:
/// - First 8 bytes: timestamp (milliseconds since epoch, big-endian)
/// - Remaining 24 bytes: cryptographically random data
///
/// # Example
///
/// ```rust
/// use near_kit::nep413;
///
/// let nonce = nep413::generate_nonce();
/// assert_eq!(nonce.len(), 32);
/// ```
pub fn generate_nonce() -> [u8; 32] {
    let mut nonce = [0u8; 32];

    // First 8 bytes: timestamp (ms since epoch) as big-endian u64
    let timestamp = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .expect("Time went backwards")
        .as_millis() as u64;
    nonce[..8].copy_from_slice(&timestamp.to_be_bytes());

    // Remaining 24 bytes: random data
    rand::RngCore::fill_bytes(&mut rand::rngs::OsRng, &mut nonce[8..]);

    nonce
}

/// Extract the timestamp from a nonce (first 8 bytes as big-endian u64 milliseconds).
pub fn extract_timestamp_from_nonce(nonce: &[u8; 32]) -> u64 {
    u64::from_be_bytes(nonce[..8].try_into().unwrap())
}

/// Serialize and hash a NEP-413 message, ready for signing.
///
/// Steps:
/// 1. Serialize the tag (2147484061) as u32 little-endian
/// 2. Serialize the payload with Borsh
/// 3. Concatenate: tag_bytes + payload_bytes
/// 4. Hash with SHA256
///
/// # Example
///
/// ```rust
/// use near_kit::nep413::{self, SignMessageParams};
///
/// let params = SignMessageParams {
///     message: "Hello".to_string(),
///     recipient: "myapp.com".to_string(),
///     nonce: nep413::generate_nonce(),
///     callback_url: None,
///     state: None,
/// };
///
/// let hash = nep413::serialize_message(&params);
/// ```
pub fn serialize_message(params: &SignMessageParams) -> CryptoHash {
    // Serialize tag as u32 little-endian (Borsh uses little-endian)
    let tag_bytes = NEP413_TAG.to_le_bytes();

    // Serialize payload
    let payload = Nep413Payload {
        message: params.message.clone(),
        nonce: params.nonce,
        recipient: params.recipient.clone(),
        callback_url: params.callback_url.clone(),
    };
    let payload_bytes = borsh::to_vec(&payload).expect("Borsh serialization should not fail");

    // Concatenate tag + payload
    let mut combined = Vec::with_capacity(tag_bytes.len() + payload_bytes.len());
    combined.extend_from_slice(&tag_bytes);
    combined.extend_from_slice(&payload_bytes);

    // Hash with SHA256
    CryptoHash::hash(&combined)
}

/// Verify a NEP-413 signature without RPC (cryptographic verification only).
///
/// This checks:
/// - The signature is valid for the message
/// - The signature is not expired (based on nonce timestamp)
///
/// Does NOT check:
/// - Whether the public key belongs to the claimed account
/// - Whether the key has full access permission
///
/// Use [`verify()`] for full verification including RPC checks.
pub fn verify_signature(
    signed: &SignedMessage,
    params: &SignMessageParams,
    max_age: Duration,
) -> bool {
    // Check timestamp expiration if max_age is not infinite
    if max_age != Duration::MAX {
        let timestamp_ms = extract_timestamp_from_nonce(&params.nonce);
        let now_ms = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("Time went backwards")
            .as_millis() as u64;

        let age_ms = now_ms.saturating_sub(timestamp_ms);

        // Check if expired or timestamp is in the future (clock skew/tampering)
        if age_ms > max_age.as_millis() as u64 || timestamp_ms > now_ms {
            return false;
        }
    }

    // Reconstruct the hash
    let hash = serialize_message(params);

    // Verify the signature
    signed.signature.verify(hash.as_bytes(), &signed.public_key)
}

/// Verify a NEP-413 signed message with full verification.
///
/// This checks:
/// - The signature is valid for the message
/// - The signature is not expired (based on nonce timestamp)
/// - The public key belongs to the claimed account (via RPC)
/// - The key has full access permission (not a function call key)
///
/// # Arguments
///
/// * `signed` - The signed message to verify
/// * `params` - Original message parameters (must match what was signed)
/// * `near` - Near client for RPC verification
/// * `options` - Verification options
///
/// # Example
///
/// ```rust,no_run
/// use near_kit::{Near, nep413};
///
/// # async fn example() -> Result<(), near_kit::Error> {
/// let near = Near::testnet().build();
///
/// # let signed = todo!();
/// # let params = todo!();
/// let is_valid = nep413::verify(&signed, &params, &near, Default::default()).await?;
/// # Ok(())
/// # }
/// ```
pub async fn verify(
    signed: &SignedMessage,
    params: &SignMessageParams,
    near: &Near,
    options: VerifyOptions,
) -> Result<bool, Error> {
    // First, do cryptographic verification
    if !verify_signature(signed, params, options.max_age) {
        return Ok(false);
    }

    // If RPC verification is requested, check key ownership
    if options.require_full_access {
        // Query the access key
        let access_key_result = near
            .rpc()
            .view_access_key(
                &signed.account_id,
                &signed.public_key,
                BlockReference::optimistic(),
            )
            .await;

        match access_key_result {
            Ok(access_key) => {
                // Check if it's a full access key
                if !matches!(
                    access_key.permission,
                    crate::types::AccessKeyPermissionView::FullAccess
                ) {
                    return Ok(false);
                }
            }
            Err(_) => {
                // Key not found or RPC error - verification fails
                return Ok(false);
            }
        }
    }

    Ok(true)
}

// ============================================================================
// Serde Helpers
// ============================================================================

/// Serialize a Signature as base64 (NEP-413 spec requirement).
fn serialize_signature_base64<S>(signature: &Signature, serializer: S) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    use base64::prelude::*;
    let base64_str = BASE64_STANDARD.encode(signature.as_bytes());
    serializer.serialize_str(&base64_str)
}

/// Deserialize a Signature from multiple formats for backwards compatibility:
/// - Base64 (NEP-413 spec)
/// - Prefixed base58 (ed25519:... or secp256k1:...)
/// - Plain base58
fn deserialize_signature_flexible<'de, D>(deserializer: D) -> Result<Signature, D::Error>
where
    D: Deserializer<'de>,
{
    use base64::prelude::*;
    use serde::de::Error;

    let s: String = String::deserialize(deserializer)?;

    // Try base64 first (NEP-413 spec)
    if let Ok(bytes) = BASE64_STANDARD.decode(&s) {
        if bytes.len() == 64 {
            return Ok(Signature::ed25519_from_bytes(
                bytes
                    .try_into()
                    .map_err(|_| D::Error::custom("Invalid signature length"))?,
            ));
        }
    }

    // Try prefixed format (ed25519:base58 or secp256k1:base58)
    if let Some(data) = s.strip_prefix("ed25519:") {
        let bytes = bs58::decode(data)
            .into_vec()
            .map_err(|e| D::Error::custom(format!("Invalid base58: {}", e)))?;
        if bytes.len() == 64 {
            return Ok(Signature::ed25519_from_bytes(
                bytes
                    .try_into()
                    .map_err(|_| D::Error::custom("Invalid signature length"))?,
            ));
        }
    }

    // Try plain base58
    if let Ok(bytes) = bs58::decode(&s).into_vec() {
        if bytes.len() == 64 {
            return Ok(Signature::ed25519_from_bytes(
                bytes
                    .try_into()
                    .map_err(|_| D::Error::custom("Invalid signature length"))?,
            ));
        }
    }

    Err(D::Error::custom(
        "Invalid signature format. Expected base64, ed25519:base58, or plain base58",
    ))
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_generate_nonce() {
        let nonce1 = generate_nonce();
        let nonce2 = generate_nonce();

        assert_eq!(nonce1.len(), 32);
        assert_eq!(nonce2.len(), 32);

        // Nonces should be different (random part)
        assert_ne!(nonce1, nonce2);

        // Timestamps should be recent (within 1 second)
        let ts1 = extract_timestamp_from_nonce(&nonce1);
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_millis() as u64;
        assert!(now - ts1 < 1000);
    }

    #[test]
    fn test_serialize_message() {
        let params = SignMessageParams {
            message: "Hello NEAR!".to_string(),
            recipient: "example.near".to_string(),
            nonce: [0u8; 32],
            callback_url: None,
            state: None,
        };

        let hash = serialize_message(&params);

        // Should produce a valid 32-byte hash
        assert_eq!(hash.as_bytes().len(), 32);

        // Same input should produce same hash
        let hash2 = serialize_message(&params);
        assert_eq!(hash, hash2);

        // Different input should produce different hash
        let params2 = SignMessageParams {
            message: "Hello NEAR!".to_string(),
            recipient: "other.near".to_string(),
            nonce: [0u8; 32],
            callback_url: None,
            state: None,
        };
        let hash3 = serialize_message(&params2);
        assert_ne!(hash, hash3);
    }

    #[test]
    fn test_signed_message_json_roundtrip() {
        use crate::types::SecretKey;

        let secret = SecretKey::generate_ed25519();
        let params = SignMessageParams {
            message: "Test".to_string(),
            recipient: "app.near".to_string(),
            nonce: generate_nonce(),
            callback_url: None,
            state: Some("csrf_token".to_string()),
        };

        let hash = serialize_message(&params);
        let signature = secret.sign(hash.as_bytes());

        let signed = SignedMessage {
            account_id: "alice.near".parse().unwrap(),
            public_key: secret.public_key(),
            signature,
            state: params.state.clone(),
        };

        // Serialize to JSON
        let json = serde_json::to_string(&signed).unwrap();

        // The signature should be base64, not ed25519:base58 format
        // Note: public_key still uses ed25519: prefix (that's correct per NEAR format)
        // We check that the signature field specifically is base64 by parsing
        let json_value: serde_json::Value = serde_json::from_str(&json).unwrap();
        let sig_str = json_value["signature"].as_str().unwrap();
        // Base64 signatures should not have a colon (ed25519: prefix would have one)
        assert!(
            !sig_str.contains(':'),
            "Signature should be base64, not prefixed format: {}",
            sig_str
        );

        // Deserialize back
        let deserialized: SignedMessage = serde_json::from_str(&json).unwrap();
        assert_eq!(signed.account_id, deserialized.account_id);
        assert_eq!(signed.public_key, deserialized.public_key);
        assert_eq!(
            signed.signature.as_bytes(),
            deserialized.signature.as_bytes()
        );
        assert_eq!(signed.state, deserialized.state);
    }

    #[test]
    fn test_verify_signature_basic() {
        use crate::types::SecretKey;

        let secret = SecretKey::generate_ed25519();
        let params = SignMessageParams {
            message: "Test message".to_string(),
            recipient: "myapp.com".to_string(),
            nonce: generate_nonce(),
            callback_url: None,
            state: None,
        };

        let hash = serialize_message(&params);
        let signature = secret.sign(hash.as_bytes());

        let signed = SignedMessage {
            account_id: "alice.near".parse().unwrap(),
            public_key: secret.public_key(),
            signature,
            state: None,
        };

        // Should verify successfully
        assert!(verify_signature(&signed, &params, DEFAULT_MAX_AGE));

        // Should fail with wrong message
        let wrong_params = SignMessageParams {
            message: "Wrong message".to_string(),
            ..params.clone()
        };
        assert!(!verify_signature(&signed, &wrong_params, DEFAULT_MAX_AGE));

        // Should fail with wrong public key
        let other_secret = SecretKey::generate_ed25519();
        let wrong_signed = SignedMessage {
            public_key: other_secret.public_key(),
            ..signed.clone()
        };
        assert!(!verify_signature(&wrong_signed, &params, DEFAULT_MAX_AGE));
    }

    #[test]
    fn test_verify_signature_expiration() {
        use crate::types::SecretKey;

        let secret = SecretKey::generate_ed25519();

        // Create a nonce with an old timestamp (10 minutes ago)
        let mut old_nonce = [0u8; 32];
        let old_timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_millis() as u64
            - (10 * 60 * 1000); // 10 minutes ago
        old_nonce[..8].copy_from_slice(&old_timestamp.to_be_bytes());

        let params = SignMessageParams {
            message: "Test".to_string(),
            recipient: "app.com".to_string(),
            nonce: old_nonce,
            callback_url: None,
            state: None,
        };

        let hash = serialize_message(&params);
        let signature = secret.sign(hash.as_bytes());

        let signed = SignedMessage {
            account_id: "alice.near".parse().unwrap(),
            public_key: secret.public_key(),
            signature,
            state: None,
        };

        // Should fail with default max age (5 minutes)
        assert!(!verify_signature(&signed, &params, DEFAULT_MAX_AGE));

        // Should pass with longer max age
        assert!(verify_signature(
            &signed,
            &params,
            Duration::from_secs(15 * 60)
        ));

        // Should pass with infinite max age
        assert!(verify_signature(&signed, &params, Duration::MAX));
    }

    /// Test interoperability with TypeScript near-kit implementation.
    /// These test vectors were verified against real wallet implementations
    /// (Meteor Wallet, MyNearWallet) in near-api-rs.
    #[test]
    fn test_typescript_interoperability() {
        use base64::prelude::*;

        // Test vector from near-api-rs, verified against Meteor wallet
        // Seed phrase: "fatal edge jacket cash hard pass gallery fabric whisper size rain biology"
        // HD path: m/44'/397'/0'
        // This produces public key: ed25519:2RM3EotCzEiVobm6aMjaup43k8cFffR4KHFtrqbZ79Qy

        let nonce_base64 = "KNV0cOpvJ50D5vfF9pqWom8wo2sliQ4W+Wa7uZ3Uk6Y=";
        let nonce_bytes = BASE64_STANDARD.decode(nonce_base64).unwrap();
        let nonce: [u8; 32] = nonce_bytes.try_into().unwrap();

        // Test WITHOUT callback_url (Meteor wallet style)
        let params_no_callback = SignMessageParams {
            message: "Hello NEAR!".to_string(),
            recipient: "example.near".to_string(),
            nonce,
            callback_url: None,
            state: None,
        };

        let expected_sig_no_callback = "NnJgPU1Ql7ccRTITIoOVsIfElmvH1RV7QAT4a9Vh6ShCOnjIzRwxqX54JzoQ/nK02p7VBMI2vJn48rpImIJwAw==";

        // Verify our serialization produces the same hash that the TS impl would sign
        let hash = serialize_message(&params_no_callback);

        // The public key from the seed phrase (derived with m/44'/397'/0')
        let public_key: crate::types::PublicKey =
            "ed25519:2RM3EotCzEiVobm6aMjaup43k8cFffR4KHFtrqbZ79Qy"
                .parse()
                .unwrap();

        // Decode the expected signature
        let sig_bytes = BASE64_STANDARD.decode(expected_sig_no_callback).unwrap();
        let signature = crate::types::Signature::ed25519_from_bytes(
            sig_bytes.try_into().expect("signature should be 64 bytes"),
        );

        // Verify the signature is valid for our computed hash
        assert!(
            signature.verify(hash.as_bytes(), &public_key),
            "Signature verification failed - serialization mismatch with TypeScript"
        );

        // Test WITH callback_url (MyNearWallet style)
        let params_with_callback = SignMessageParams {
            message: "Hello NEAR!".to_string(),
            recipient: "example.near".to_string(),
            nonce,
            callback_url: Some("http://localhost:3000".to_string()),
            state: None,
        };

        let expected_sig_with_callback = "zzZQ/GwAjrZVrTIFlvmmQbDQHllfzrr8urVWHaRt5cPfcXaCSZo35c5LDpPpTKivR6BxLyb3lcPM0FfCW5lcBQ==";

        let hash = serialize_message(&params_with_callback);
        let sig_bytes = BASE64_STANDARD.decode(expected_sig_with_callback).unwrap();
        let signature = crate::types::Signature::ed25519_from_bytes(
            sig_bytes.try_into().expect("signature should be 64 bytes"),
        );

        assert!(
            signature.verify(hash.as_bytes(), &public_key),
            "Signature verification with callback_url failed - serialization mismatch"
        );
    }

    /// Test that we can deserialize a SignedMessage JSON from TypeScript.
    #[test]
    fn test_deserialize_typescript_signed_message() {
        // Simulated JSON from TypeScript near-kit
        // Using the correct public key derived from seed phrase
        let ts_json = r#"{
            "accountId": "alice.testnet",
            "publicKey": "ed25519:2RM3EotCzEiVobm6aMjaup43k8cFffR4KHFtrqbZ79Qy",
            "signature": "NnJgPU1Ql7ccRTITIoOVsIfElmvH1RV7QAT4a9Vh6ShCOnjIzRwxqX54JzoQ/nK02p7VBMI2vJn48rpImIJwAw=="
        }"#;

        // Should deserialize successfully
        let signed: SignedMessage = serde_json::from_str(ts_json).unwrap();

        assert_eq!(signed.account_id.as_str(), "alice.testnet");
        assert_eq!(
            signed.public_key.to_string(),
            "ed25519:2RM3EotCzEiVobm6aMjaup43k8cFffR4KHFtrqbZ79Qy"
        );
        assert_eq!(signed.signature.as_bytes().len(), 64);
        assert!(signed.state.is_none());
    }

    /// Test that we can deserialize legacy ed25519:base58 signature format.
    #[test]
    fn test_deserialize_legacy_base58_signature() {
        // Some older implementations might send signatures in ed25519:base58 format
        let legacy_json = r#"{
            "accountId": "alice.testnet",
            "publicKey": "ed25519:HeEp8gQPzs6rMPRN1hijJ7dXFmZLu3FPNKeLDpmLfFBT",
            "signature": "ed25519:2DzVcjvceXbR6n9ot4C9xA8gVPrZRq8NqJj4b3DaLBmVk1TqXwK8yHcL6M6ezQD4HxXHhZQPbgjdNW7Tx8sjxSFe"
        }"#;

        // Should deserialize successfully (backwards compatibility)
        let signed: SignedMessage = serde_json::from_str(legacy_json).unwrap();

        assert_eq!(signed.account_id.as_str(), "alice.testnet");
        assert_eq!(signed.signature.as_bytes().len(), 64);
    }

    /// Test roundtrip: Rust -> JSON -> TypeScript-compatible -> JSON -> Rust
    #[test]
    fn test_rust_to_typescript_roundtrip() {
        use crate::types::SecretKey;

        let secret = SecretKey::generate_ed25519();
        let params = SignMessageParams {
            message: "Cross-platform test".to_string(),
            recipient: "myapp.com".to_string(),
            nonce: generate_nonce(),
            callback_url: None,
            state: Some("session123".to_string()),
        };

        let hash = serialize_message(&params);
        let signature = secret.sign(hash.as_bytes());

        let signed = SignedMessage {
            account_id: "alice.near".parse().unwrap(),
            public_key: secret.public_key(),
            signature,
            state: params.state.clone(),
        };

        // Serialize to JSON (as if sending to TypeScript)
        let json = serde_json::to_string(&signed).unwrap();

        // Parse as generic JSON to inspect the format
        let json_value: serde_json::Value = serde_json::from_str(&json).unwrap();

        // Verify field names are camelCase (TypeScript compatible)
        assert!(json_value.get("accountId").is_some());
        assert!(json_value.get("publicKey").is_some());
        assert!(json_value.get("signature").is_some());

        // Verify signature is base64 (no colon = not prefixed format)
        let sig_str = json_value["signature"].as_str().unwrap();
        assert!(!sig_str.contains(':'));

        // Deserialize back (as if receiving from TypeScript)
        let roundtrip: SignedMessage = serde_json::from_str(&json).unwrap();
        assert_eq!(signed.account_id, roundtrip.account_id);
        assert_eq!(signed.public_key, roundtrip.public_key);
        assert_eq!(signed.signature.as_bytes(), roundtrip.signature.as_bytes());
        assert_eq!(signed.state, roundtrip.state);

        // Verify signature still works after roundtrip
        assert!(verify_signature(&roundtrip, &params, Duration::MAX));
    }

    /// Test deserializing the full HTTP authentication payload from TypeScript.
    #[test]
    fn test_deserialize_http_auth_payload() {
        // This is what a TypeScript frontend would send to the backend
        // nonce is sent as hex (64 chars for 32 bytes)
        let nonce_hex = "28d57470ea6f279d03e6f7c5f69a96a26f30a36b25890e16f966bbb99dd493a6";

        // Build the JSON as the TS client would
        let http_payload = serde_json::json!({
            "signedMessage": {
                "accountId": "alice.testnet",
                "publicKey": "ed25519:2RM3EotCzEiVobm6aMjaup43k8cFffR4KHFtrqbZ79Qy",
                "signature": "NnJgPU1Ql7ccRTITIoOVsIfElmvH1RV7QAT4a9Vh6ShCOnjIzRwxqX54JzoQ/nK02p7VBMI2vJn48rpImIJwAw=="
            },
            "nonce": nonce_hex,
            "message": "Hello NEAR!",
            "recipient": "example.near"
        });

        // Deserialize the HTTP payload
        let payload: AuthPayload = serde_json::from_value(http_payload).unwrap();

        // Verify we got the right data
        assert_eq!(payload.signed_message.account_id.as_str(), "alice.testnet");
        assert_eq!(payload.message, "Hello NEAR!");
        assert_eq!(payload.recipient, "example.near");
        assert_eq!(payload.nonce.len(), 32);

        // Convert to params and verify the signature
        let params = payload.to_params();
        assert!(verify_signature(
            &payload.signed_message,
            &params,
            Duration::MAX
        ));
    }

    /// Test the complete authentication flow: TS client -> JSON -> Rust server
    #[test]
    fn test_full_auth_flow_interop() {
        // Simulate what a real HTTP request body would look like from near-kit TS
        // Nonce is hex encoded (64 chars for 32 bytes)
        let http_body = r#"{
            "signedMessage": {
                "accountId": "alice.testnet",
                "publicKey": "ed25519:2RM3EotCzEiVobm6aMjaup43k8cFffR4KHFtrqbZ79Qy",
                "signature": "NnJgPU1Ql7ccRTITIoOVsIfElmvH1RV7QAT4a9Vh6ShCOnjIzRwxqX54JzoQ/nK02p7VBMI2vJn48rpImIJwAw=="
            },
            "nonce": "28d57470ea6f279d03e6f7c5f69a96a26f30a36b25890e16f966bbb99dd493a6",
            "message": "Hello NEAR!",
            "recipient": "example.near"
        }"#;

        // Parse as AuthPayload
        let payload: AuthPayload = serde_json::from_str(http_body).unwrap();

        // Verify the signature
        let params = payload.to_params();
        let is_valid = verify_signature(&payload.signed_message, &params, Duration::MAX);

        assert!(is_valid, "Signature should be valid");
        assert_eq!(payload.signed_message.account_id.as_str(), "alice.testnet");
    }

    /// Test generating AuthPayload from Rust and serializing to JSON
    #[test]
    fn test_generate_auth_payload_from_rust() {
        use crate::types::SecretKey;

        let secret = SecretKey::generate_ed25519();
        let params = SignMessageParams {
            message: "Sign in to My App".to_string(),
            recipient: "myapp.com".to_string(),
            nonce: generate_nonce(),
            callback_url: None,
            state: None,
        };

        let hash = serialize_message(&params);
        let signature = secret.sign(hash.as_bytes());

        let signed = SignedMessage {
            account_id: "alice.near".parse().unwrap(),
            public_key: secret.public_key(),
            signature,
            state: None,
        };

        // Create AuthPayload (what would be sent over HTTP)
        let payload = AuthPayload::from_signed(signed.clone(), &params);

        // Serialize to JSON
        let json = serde_json::to_string(&payload).unwrap();

        // Verify nonce is hex (not an array)
        let json_value: serde_json::Value = serde_json::from_str(&json).unwrap();
        let nonce_str = json_value["nonce"].as_str().unwrap();
        assert!(
            nonce_str.len() == 64, // Hex of 32 bytes = 64 chars
            "Nonce should be hex encoded, got: {}",
            nonce_str
        );

        // Deserialize back and verify
        let roundtrip: AuthPayload = serde_json::from_str(&json).unwrap();
        assert_eq!(payload.nonce, roundtrip.nonce);
        assert_eq!(payload.message, roundtrip.message);

        // Verify signature still works
        let roundtrip_params = roundtrip.to_params();
        assert!(verify_signature(
            &roundtrip.signed_message,
            &roundtrip_params,
            Duration::MAX
        ));
    }
}