fips-identity 0.3.1

FIPS identity, npub, node address, and FIPS address primitives
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
use std::collections::HashSet;
use std::net::Ipv6Addr;

use secp256k1::{Keypair, Secp256k1, SecretKey};

use super::*;

#[test]
fn test_identity_generation() {
    let identity = Identity::generate();

    // NodeAddr should be 16 bytes
    assert_eq!(identity.node_addr().as_bytes().len(), 16);

    // Address should start with 0xfd
    assert_eq!(identity.address().as_bytes()[0], 0xfd);

    // Address bytes 1-15 should match node_addr bytes 0-14
    assert_eq!(
        &identity.address().as_bytes()[1..16],
        &identity.node_addr().as_bytes()[0..15]
    );
}

#[test]
fn test_node_addr_from_pubkey_deterministic() {
    let identity = Identity::generate();
    let pubkey = identity.pubkey();

    let node_addr1 = NodeAddr::from_pubkey(&pubkey);
    let node_addr2 = NodeAddr::from_pubkey(&pubkey);

    assert_eq!(node_addr1, node_addr2);
}

#[test]
fn test_fips_address_ipv6_format() {
    let identity = Identity::generate();
    let ipv6 = identity.address().to_ipv6();
    let addr_str = ipv6.to_string();

    // Should start with fd (ULA prefix)
    assert!(addr_str.starts_with("fd"));

    // Conversion should be lossless
    let octets = ipv6.octets();
    assert_eq!(&octets, identity.address().as_bytes());
}

#[test]
fn test_auth_challenge_verify_success() {
    let identity = Identity::generate();
    let challenge = AuthChallenge::generate();
    let timestamp = 1234567890u64;

    let response = identity.sign_challenge(challenge.as_bytes(), timestamp);
    let result = challenge.verify(&response);

    assert!(result.is_ok());
    assert_eq!(result.unwrap(), *identity.node_addr());
}

#[test]
fn test_auth_challenge_verify_wrong_challenge() {
    let identity = Identity::generate();
    let challenge1 = AuthChallenge::generate();
    let challenge2 = AuthChallenge::generate();
    let timestamp = 1234567890u64;

    let response = identity.sign_challenge(challenge1.as_bytes(), timestamp);
    let result = challenge2.verify(&response);

    assert!(matches!(
        result,
        Err(IdentityError::SignatureVerificationFailed)
    ));
}

#[test]
fn test_auth_challenge_verify_wrong_timestamp() {
    let identity = Identity::generate();
    let challenge = AuthChallenge::generate();

    let response = identity.sign_challenge(challenge.as_bytes(), 1234567890);

    // Modify the timestamp in the response
    let bad_response = AuthResponse {
        pubkey: response.pubkey,
        timestamp: 9999999999,
        signature: response.signature,
    };

    let result = challenge.verify(&bad_response);
    assert!(matches!(
        result,
        Err(IdentityError::SignatureVerificationFailed)
    ));
}

#[test]
fn test_node_addr_ordering() {
    let id1 = Identity::generate();
    let id2 = Identity::generate();

    // NodeAddrs should be comparable for root election
    let _cmp = id1.node_addr().cmp(id2.node_addr());
}

#[test]
fn test_identity_from_secret_bytes() {
    // A known secret key (32 bytes)
    let secret_bytes: [u8; 32] = [
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
        0x1f, 0x20,
    ];

    let identity1 = Identity::from_secret_bytes(&secret_bytes).unwrap();
    let identity2 = Identity::from_secret_bytes(&secret_bytes).unwrap();

    // Same secret key should produce same node_addr
    assert_eq!(identity1.node_addr(), identity2.node_addr());
    assert_eq!(identity1.address(), identity2.address());
}

#[test]
fn test_node_addr_from_slice() {
    let bytes = [0u8; 16];
    let node_addr = NodeAddr::from_slice(&bytes).unwrap();
    assert_eq!(node_addr.as_bytes(), &bytes);

    // Wrong length should fail
    let short = [0u8; 8];
    assert!(matches!(
        NodeAddr::from_slice(&short),
        Err(IdentityError::InvalidNodeAddrLength(8))
    ));
}

#[test]
fn test_fips_address_validation() {
    // Valid address with fd prefix
    let mut valid = [0u8; 16];
    valid[0] = 0xfd;
    assert!(FipsAddress::from_bytes(valid).is_ok());

    // Invalid prefix
    let mut invalid = [0u8; 16];
    invalid[0] = 0xfe;
    assert!(matches!(
        FipsAddress::from_bytes(invalid),
        Err(IdentityError::InvalidAddressPrefix(0xfe))
    ));
}

#[test]
fn test_identity_sign() {
    let identity = Identity::generate();
    let data = b"test message";

    let sig = identity.sign(data);

    // Verify the signature manually
    let secp = secp256k1::Secp256k1::new();
    let digest = super::sha256(data);
    assert!(
        secp.verify_schnorr(&sig, &digest, &identity.pubkey())
            .is_ok()
    );
}

#[test]
fn test_npub_encoding() {
    let identity = Identity::generate();
    let npub = identity.npub();

    // Should start with "npub1"
    assert!(npub.starts_with("npub1"));

    // Should be 63 characters (npub1 + 58 chars of bech32 data)
    assert_eq!(npub.len(), 63);
}

#[test]
fn test_npub_roundtrip() {
    let identity = Identity::generate();
    let npub = identity.npub();

    let decoded = decode_npub(&npub).unwrap();
    assert_eq!(decoded, identity.pubkey());
}

#[test]
fn test_npub_known_vector() {
    // Test against a known npub (from NIP-19 test vectors or generated externally)
    let secret_bytes: [u8; 32] = [
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
        0x1f, 0x20,
    ];

    let identity = Identity::from_secret_bytes(&secret_bytes).unwrap();
    let npub = identity.npub();

    // Decode and verify it matches the original pubkey
    let decoded = decode_npub(&npub).unwrap();
    assert_eq!(decoded, identity.pubkey());

    // npub should be deterministic
    let npub2 = encode_npub(&identity.pubkey());
    assert_eq!(npub, npub2);
}

#[test]
fn test_decode_npub_invalid_prefix() {
    // nsec instead of npub
    let nsec = "nsec1vl029mgpspedva04g90vltkh6fvh240zqtv9k0t9af8935ke9laqsnlfe5";
    let result = decode_npub(nsec);
    assert!(matches!(result, Err(IdentityError::InvalidNpubPrefix(_))));
}

#[test]
fn test_decode_npub_invalid_checksum() {
    // Valid npub with corrupted checksum
    let bad_npub = "npub1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq";
    let result = decode_npub(bad_npub);
    assert!(result.is_err());
}

#[test]
fn test_peer_identity_from_npub() {
    let identity = Identity::generate();
    let npub = identity.npub();

    let peer = PeerIdentity::from_npub(&npub).unwrap();

    assert_eq!(peer.pubkey(), identity.pubkey());
    assert_eq!(peer.node_addr(), identity.node_addr());
    assert_eq!(peer.address(), identity.address());
    assert_eq!(peer.npub(), npub);
}

#[test]
fn test_peer_identity_verify_signature() {
    let identity = Identity::generate();
    let peer = PeerIdentity::from_pubkey(identity.pubkey());

    let data = b"hello world";
    let signature = identity.sign(data);

    assert!(peer.verify(data, &signature));
    assert!(!peer.verify(b"wrong data", &signature));
}

#[test]
fn test_peer_identity_from_invalid_npub() {
    let result = PeerIdentity::from_npub("npub1invalid");
    assert!(result.is_err());

    let result =
        PeerIdentity::from_npub("nsec1vl029mgpspedva04g90vltkh6fvh240zqtv9k0t9af8935ke9laqsnlfe5");
    assert!(matches!(result, Err(IdentityError::InvalidNpubPrefix(_))));
}

#[test]
fn test_peer_identity_display() {
    let identity = Identity::generate();
    let peer = PeerIdentity::from_pubkey(identity.pubkey());

    let display = format!("{}", peer);
    assert!(display.starts_with("npub1"));
    assert_eq!(display, identity.npub());
}

#[test]
fn test_nsec_roundtrip() {
    let secret_bytes: [u8; 32] = [
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
        0x1f, 0x20,
    ];

    let secret_key = SecretKey::from_slice(&secret_bytes).unwrap();
    let nsec = encode_nsec(&secret_key);

    assert!(nsec.starts_with("nsec1"));
    assert_eq!(nsec.len(), 63);

    let decoded = decode_nsec(&nsec).unwrap();
    assert_eq!(decoded.secret_bytes(), secret_bytes);
}

#[test]
fn test_decode_nsec_invalid_prefix() {
    // Use a valid npub (from a generated identity) to test prefix rejection
    let identity = Identity::generate();
    let npub = identity.npub();
    let result = decode_nsec(&npub);
    assert!(matches!(result, Err(IdentityError::InvalidNsecPrefix(_))));
}

#[test]
fn test_decode_secret_nsec() {
    let secret_bytes: [u8; 32] = [
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
        0x1f, 0x20,
    ];

    let secret_key = SecretKey::from_slice(&secret_bytes).unwrap();
    let nsec = encode_nsec(&secret_key);

    let decoded = decode_secret(&nsec).unwrap();
    assert_eq!(decoded.secret_bytes(), secret_bytes);
}

#[test]
fn test_decode_secret_hex() {
    let hex_str = "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20";
    let decoded = decode_secret(hex_str).unwrap();

    let expected: [u8; 32] = [
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
        0x1f, 0x20,
    ];
    assert_eq!(decoded.secret_bytes(), expected);
}

#[test]
fn test_identity_from_secret_str_nsec() {
    let secret_bytes: [u8; 32] = [
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
        0x1f, 0x20,
    ];

    let secret_key = SecretKey::from_slice(&secret_bytes).unwrap();
    let nsec = encode_nsec(&secret_key);

    let identity = Identity::from_secret_str(&nsec).unwrap();
    let identity_from_bytes = Identity::from_secret_bytes(&secret_bytes).unwrap();

    assert_eq!(identity.node_addr(), identity_from_bytes.node_addr());
}

#[test]
fn test_identity_from_secret_str_hex() {
    let hex_str = "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20";
    let secret_bytes: [u8; 32] = [
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
        0x1f, 0x20,
    ];

    let identity = Identity::from_secret_str(hex_str).unwrap();
    let identity_from_bytes = Identity::from_secret_bytes(&secret_bytes).unwrap();

    assert_eq!(identity.node_addr(), identity_from_bytes.node_addr());
}

#[test]
fn test_hex_conversion_case1() {
    let hex_str = "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20";
    let identity = Identity::from_secret_str(hex_str).unwrap();
    let npub = identity.npub();
    assert!(npub.starts_with("npub1"));
}

#[test]
fn test_hex_conversion_case2() {
    let hex_str = "b102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1fb0";
    let identity = Identity::from_secret_str(hex_str).unwrap();
    let npub = identity.npub();
    assert!(npub.starts_with("npub1"));
}

// ===== encoding.rs error path tests =====

#[test]
fn test_decode_npub_invalid_length() {
    // Encode 16 bytes (too short) as bech32 with npub prefix
    let short =
        bech32::encode::<bech32::Bech32>(bech32::Hrp::parse_unchecked("npub"), &[0u8; 16]).unwrap();
    let result = decode_npub(&short);
    assert!(matches!(result, Err(IdentityError::InvalidNpubLength(16))));
}

#[test]
fn test_decode_nsec_invalid_length() {
    // Encode 16 bytes (too short) as bech32 with nsec prefix
    let short =
        bech32::encode::<bech32::Bech32>(bech32::Hrp::parse_unchecked("nsec"), &[0u8; 16]).unwrap();
    let result = decode_nsec(&short);
    assert!(matches!(result, Err(IdentityError::InvalidNsecLength(16))));
}

#[test]
fn test_decode_secret_hex_wrong_length() {
    // 16 hex bytes (too short for a secret key)
    let result = decode_secret("0102030405060708090a0b0c0d0e0f10");
    assert!(matches!(result, Err(IdentityError::InvalidNsecLength(16))));
}

#[test]
fn test_decode_secret_hex_invalid_chars() {
    let result = decode_secret("zzzz");
    assert!(matches!(result, Err(IdentityError::InvalidHex(_))));
}

// ===== node_addr.rs tests =====

#[test]
fn test_node_addr_debug() {
    let bytes = [
        0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x00,
    ];
    let node_addr = NodeAddr::from_bytes(bytes);
    let debug = format!("{:?}", node_addr);
    assert_eq!(debug, "NodeAddr(0123456789abcdef)");
}

#[test]
fn test_node_addr_display() {
    let bytes = [
        0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32,
        0x10,
    ];
    let node_addr = NodeAddr::from_bytes(bytes);
    let display = format!("{}", node_addr);
    assert_eq!(display, "0123456789abcdeffedcba9876543210");
}

#[test]
fn test_node_addr_as_slice() {
    let bytes = [0xaa; 16];
    let node_addr = NodeAddr::from_bytes(bytes);
    assert_eq!(node_addr.as_slice(), &bytes[..]);
    assert_eq!(node_addr.as_slice().len(), 16);
}

#[test]
fn test_node_addr_as_ref() {
    let bytes = [0xbb; 16];
    let node_addr = NodeAddr::from_bytes(bytes);
    let r: &[u8] = node_addr.as_ref();
    assert_eq!(r, &bytes[..]);
}

#[test]
fn test_node_addr_hash() {
    let id1 = Identity::generate();
    let id2 = Identity::generate();
    let mut set = HashSet::new();
    set.insert(*id1.node_addr());
    set.insert(*id2.node_addr());
    assert_eq!(set.len(), 2);
    // Re-inserting the same address doesn't grow the set
    set.insert(*id1.node_addr());
    assert_eq!(set.len(), 2);
}

// ===== address.rs tests =====

#[test]
fn test_fips_address_from_slice_success() {
    let mut bytes = [0u8; 16];
    bytes[0] = 0xfd;
    bytes[1] = 0x42;
    let addr = FipsAddress::from_slice(&bytes).unwrap();
    assert_eq!(addr.as_bytes(), &bytes);
}

#[test]
fn test_fips_address_from_slice_wrong_length() {
    let short = [0xfd; 8];
    assert!(matches!(
        FipsAddress::from_slice(&short),
        Err(IdentityError::InvalidAddressLength(8))
    ));

    let long = [0xfd; 20];
    assert!(matches!(
        FipsAddress::from_slice(&long),
        Err(IdentityError::InvalidAddressLength(20))
    ));
}

#[test]
fn test_fips_address_from_slice_wrong_prefix() {
    let mut bytes = [0u8; 16];
    bytes[0] = 0xfe;
    assert!(matches!(
        FipsAddress::from_slice(&bytes),
        Err(IdentityError::InvalidAddressPrefix(0xfe))
    ));
}

#[test]
fn test_fips_address_into_ipv6() {
    let identity = Identity::generate();
    let addr = *identity.address();
    // Test the From trait (not to_ipv6 method)
    let ipv6: Ipv6Addr = addr.into();
    assert_eq!(ipv6.octets(), *addr.as_bytes());
}

#[test]
fn test_fips_address_debug() {
    let mut bytes = [0u8; 16];
    bytes[0] = 0xfd;
    let addr = FipsAddress::from_bytes(bytes).unwrap();
    let debug = format!("{:?}", addr);
    assert!(debug.starts_with("FipsAddress("));
    assert!(debug.contains("fd"));
}

#[test]
fn test_fips_address_display() {
    let mut bytes = [0u8; 16];
    bytes[0] = 0xfd;
    let addr = FipsAddress::from_bytes(bytes).unwrap();
    let display = format!("{}", addr);
    // Display is the IPv6 representation
    assert!(display.starts_with("fd"));
}

#[test]
fn test_fips_address_eq_hash() {
    let identity = Identity::generate();
    let addr1 = *identity.address();
    let addr2 = FipsAddress::from_node_addr(identity.node_addr());
    assert_eq!(addr1, addr2);

    let mut set = HashSet::new();
    set.insert(addr1);
    set.insert(addr2);
    assert_eq!(set.len(), 1);
}

// ===== auth.rs tests =====

#[test]
fn test_auth_challenge_from_bytes() {
    let bytes = [0x42u8; 32];
    let challenge = AuthChallenge::from_bytes(bytes);
    assert_eq!(challenge.as_bytes(), &bytes);
}

// ===== peer.rs tests =====

#[test]
fn test_peer_identity_from_pubkey_full() {
    let identity = Identity::generate();
    let full_pubkey = identity.pubkey_full();

    let peer = PeerIdentity::from_pubkey_full(full_pubkey);

    // x-only key should match
    assert_eq!(peer.pubkey(), identity.pubkey());
    // Full key should be preserved (not derived)
    assert_eq!(peer.pubkey_full(), full_pubkey);
    // Derived identifiers should match
    assert_eq!(peer.node_addr(), identity.node_addr());
    assert_eq!(peer.address(), identity.address());
}

#[test]
fn test_peer_identity_pubkey_full_even_parity_fallback() {
    let identity = Identity::generate();
    // from_pubkey only stores x-only, so pubkey_full must derive with even parity
    let peer = PeerIdentity::from_pubkey(identity.pubkey());
    let full = peer.pubkey_full();
    // The derived full key's x-only component should match
    let (x_only, _parity) = full.x_only_public_key();
    assert_eq!(x_only, identity.pubkey());
}

#[test]
fn test_peer_identity_pubkey_full_preserved_parity() {
    // Create two identities and find one with odd parity to make this test meaningful
    let secp = Secp256k1::new();
    let secret_bytes: [u8; 32] = [
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
        0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
        0x1f, 0x20,
    ];
    let keypair = Keypair::from_seckey_slice(&secp, &secret_bytes).unwrap();
    let full_pubkey = keypair.public_key();

    let peer = PeerIdentity::from_pubkey_full(full_pubkey);
    // pubkey_full should return the exact key provided, preserving parity
    assert_eq!(peer.pubkey_full(), full_pubkey);
}

#[test]
fn test_peer_identity_debug() {
    let identity = Identity::generate();
    let peer = PeerIdentity::from_pubkey(identity.pubkey());
    let debug = format!("{:?}", peer);
    assert!(debug.starts_with("PeerIdentity {"));
    assert!(debug.contains("node_addr"));
    assert!(debug.contains("address"));
}

// ===== local.rs tests =====

#[test]
fn test_identity_keypair() {
    let identity = Identity::generate();
    let keypair = identity.keypair();
    // keypair's public key should match identity's pubkey
    let (x_only, _) = keypair.x_only_public_key();
    assert_eq!(x_only, identity.pubkey());
}

#[test]
fn test_identity_pubkey_full() {
    let identity = Identity::generate();
    let full = identity.pubkey_full();
    // Full key's x-only component should match pubkey
    let (x_only, _) = full.x_only_public_key();
    assert_eq!(x_only, identity.pubkey());
}

#[test]
fn test_identity_debug() {
    let identity = Identity::generate();
    let debug = format!("{:?}", identity);
    assert!(debug.starts_with("Identity {"));
    assert!(debug.contains("node_addr"));
    assert!(debug.contains("address"));
    // Should NOT contain the secret key
    assert!(!debug.contains("keypair"));
    assert!(debug.contains(".."));
}