bsv-rs 0.3.13

BSV blockchain SDK for Rust - primitives, script, transactions, and more
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
//! Integration tests that verify cross-module functionality.
//!
//! These tests validate that all modules work together correctly,
//! testing complete workflows from key generation to transaction signing.

use bsv_rs::primitives::bsv::schnorr::Schnorr;
use bsv_rs::primitives::bsv::shamir::{split_private_key, KeyShares};
use bsv_rs::primitives::bsv::sighash::{
    compute_sighash, SighashParams, TxInput, TxOutput, SIGHASH_ALL, SIGHASH_FORKID,
};
use bsv_rs::primitives::ec::{recover_public_key, PrivateKey};
use bsv_rs::primitives::hash::{hash160, sha256, sha256_hmac, sha256d, sha512_hmac};
use bsv_rs::primitives::p256::{P256PrivateKey, P256PublicKey};
use bsv_rs::primitives::symmetric::SymmetricKey;
use bsv_rs::primitives::{from_base58, from_base64, from_hex, to_base58, to_base64, to_hex};

// ============================================================================
// Full Key Derivation Workflow Tests
// ============================================================================

#[test]
fn test_full_key_derivation_workflow() {
    // Generate keys for Sender and Recipient
    // BRC-42: Recipient derives child private key, Sender derives matching public key
    let sender = PrivateKey::random();
    let recipient = PrivateKey::random();

    // Derive child keys for a specific invoice
    let invoice = "order-12345";
    // Recipient derives child private key using sender's public key
    let recipient_child = recipient
        .derive_child(&sender.public_key(), invoice)
        .unwrap();
    // Sender derives the corresponding public key using recipient's public key
    let sender_derived_pub = recipient
        .public_key()
        .derive_child(&sender, invoice)
        .unwrap();

    // The derived keys should match
    assert_eq!(
        recipient_child.public_key().to_compressed(),
        sender_derived_pub.to_compressed(),
        "BRC-42 key derivation should produce matching keys"
    );
}

#[test]
fn test_key_derivation_multiple_invoices() {
    let alice = PrivateKey::random();
    let bob = PrivateKey::random();

    // Different invoices should produce different keys
    let invoices = ["invoice-001", "invoice-002", "payment-abc", ""];
    let mut derived_keys = Vec::new();

    for invoice in invoices {
        let child = alice.derive_child(&bob.public_key(), invoice).unwrap();
        derived_keys.push(child.public_key().to_compressed());
    }

    // All derived keys should be unique
    for i in 0..derived_keys.len() {
        for j in (i + 1)..derived_keys.len() {
            assert_ne!(
                derived_keys[i], derived_keys[j],
                "Different invoices should produce different keys"
            );
        }
    }
}

// ============================================================================
// Sign and Verify with Derived Keys
// ============================================================================

#[test]
fn test_sign_and_verify_with_derived_keys() {
    let alice = PrivateKey::random();
    let bob = PrivateKey::random();

    let child = alice
        .derive_child(&bob.public_key(), "test-invoice")
        .unwrap();
    let msg_hash = sha256(b"Hello, BSV!");

    let sig = child.sign(&msg_hash).unwrap();
    assert!(
        child.public_key().verify(&msg_hash, &sig),
        "Signature should verify with derived key"
    );
    assert!(
        sig.is_low_s(),
        "Signature should be low-S (BIP 62 compliant)"
    );
}

#[test]
fn test_signature_recovery_with_derived_keys() {
    let alice = PrivateKey::random();
    let bob = PrivateKey::random();

    let child = alice
        .derive_child(&bob.public_key(), "recovery-test")
        .unwrap();
    let child_pub = child.public_key();

    let msg_hash = sha256(b"test message for recovery");
    let signature = child.sign(&msg_hash).unwrap();

    // One of the recovery IDs should give us the correct public key
    let mut found = false;
    for recovery_id in 0..2u8 {
        if let Ok(recovered) = recover_public_key(&msg_hash, &signature, recovery_id) {
            if recovered.to_compressed() == child_pub.to_compressed() {
                found = true;
                break;
            }
        }
    }

    assert!(found, "Should recover the correct public key");
}

// ============================================================================
// Symmetric Encryption with Derived Key
// ============================================================================

#[test]
fn test_symmetric_encryption_with_derived_key() {
    let alice = PrivateKey::random();
    let bob = PrivateKey::random();

    // Derive shared secret using ECDH
    let shared = alice.derive_shared_secret(&bob.public_key()).unwrap();

    // Use X coordinate as symmetric key
    let x = shared.x();
    let sym_key = SymmetricKey::from_bytes(&x).unwrap();

    // Encrypt and decrypt
    let plaintext = b"Secret message";
    let ciphertext = sym_key.encrypt(plaintext).unwrap();
    let decrypted = sym_key.decrypt(&ciphertext).unwrap();

    assert_eq!(
        plaintext.as_slice(),
        &decrypted[..],
        "Decrypted message should match original"
    );
}

#[test]
fn test_symmetric_encryption_bidirectional() {
    let alice = PrivateKey::random();
    let bob = PrivateKey::random();

    // Both parties derive the same shared secret
    let alice_shared = alice.derive_shared_secret(&bob.public_key()).unwrap();
    let bob_shared = bob.derive_shared_secret(&alice.public_key()).unwrap();

    assert_eq!(
        alice_shared.to_compressed(),
        bob_shared.to_compressed(),
        "ECDH shared secrets should match"
    );

    // Both can encrypt/decrypt using the same key
    let alice_key = SymmetricKey::from_bytes(&alice_shared.x()).unwrap();
    let bob_key = SymmetricKey::from_bytes(&bob_shared.x()).unwrap();

    let message = b"Hello from Alice";
    let ciphertext = alice_key.encrypt(message).unwrap();
    let decrypted = bob_key.decrypt(&ciphertext).unwrap();

    assert_eq!(message.as_slice(), &decrypted[..]);
}

// ============================================================================
// WIF and Address Roundtrip
// ============================================================================

#[test]
fn test_wif_address_roundtrip() {
    let key = PrivateKey::random();

    // Export to WIF
    let wif = key.to_wif();

    // Re-import
    let recovered = PrivateKey::from_wif(&wif).unwrap();

    // Generate address
    let address = recovered.public_key().to_address();

    // Verify same key
    assert_eq!(
        key.to_bytes(),
        recovered.to_bytes(),
        "WIF roundtrip should preserve key"
    );

    // Address should start with '1' (mainnet P2PKH)
    assert!(
        address.starts_with('1'),
        "Mainnet address should start with '1'"
    );
}

#[test]
fn test_testnet_wif_address() {
    let key = PrivateKey::random();

    // Export as testnet WIF (prefix 0xEF)
    let wif_testnet = key.to_wif_with_prefix(0xEF);

    // Re-import
    let recovered = PrivateKey::from_wif(&wif_testnet).unwrap();

    // Generate testnet address (prefix 0x6F)
    let address = recovered.public_key().to_address_with_prefix(0x6F);

    assert_eq!(key.to_bytes(), recovered.to_bytes());
    // Testnet addresses start with 'm' or 'n'
    assert!(
        address.starts_with('m') || address.starts_with('n'),
        "Testnet address should start with 'm' or 'n'"
    );
}

// ============================================================================
// Schnorr Proof with Derived Keys
// ============================================================================

#[test]
fn test_schnorr_proof_with_derived_keys() {
    let alice = PrivateKey::random();
    let bob = PrivateKey::random();

    let alice_pub = alice.public_key();
    let bob_pub = bob.public_key();

    // Alice computes shared secret
    let shared = alice.derive_shared_secret(&bob_pub).unwrap();

    // Generate and verify Schnorr proof
    let proof = Schnorr::generate_proof(&alice, &alice_pub, &bob_pub, &shared).unwrap();
    assert!(
        Schnorr::verify_proof(&alice_pub, &bob_pub, &shared, &proof),
        "Schnorr proof should verify"
    );
}

#[test]
fn test_schnorr_proof_fails_with_wrong_secret() {
    let alice = PrivateKey::random();
    let bob = PrivateKey::random();
    let carol = PrivateKey::random();

    let shared = alice.derive_shared_secret(&bob.public_key()).unwrap();
    let wrong_shared = alice.derive_shared_secret(&carol.public_key()).unwrap();

    let proof =
        Schnorr::generate_proof(&alice, &alice.public_key(), &bob.public_key(), &shared).unwrap();

    // Verification should fail with wrong shared secret
    assert!(
        !Schnorr::verify_proof(
            &alice.public_key(),
            &bob.public_key(),
            &wrong_shared,
            &proof
        ),
        "Proof should not verify with wrong shared secret"
    );
}

// ============================================================================
// Shamir Secret Sharing with WIF Export
// ============================================================================

#[test]
fn test_shamir_with_wif_export() {
    let original = PrivateKey::random();
    let original_wif = original.to_wif();

    // Split into shares
    let shares = split_private_key(&original, 3, 5).unwrap();
    let backup = shares.to_backup_format();

    // Recover from backup format (using first 3 shares)
    let restored = KeyShares::from_backup_format(&backup[0..3]).unwrap();
    let recovered = restored.recover_private_key().unwrap();

    // WIFs should match
    assert_eq!(
        original_wif,
        recovered.to_wif(),
        "Recovered key should have same WIF"
    );
}

#[test]
fn test_shamir_different_subsets() {
    let key = PrivateKey::random();
    let key_bytes = key.to_bytes();

    let shares = split_private_key(&key, 3, 5).unwrap();
    let backup = shares.to_backup_format();

    // Test recovery with different subsets of 3 shares
    let subsets: Vec<Vec<String>> = vec![
        backup[0..3].to_vec(),                                         // First 3
        backup[1..4].to_vec(),                                         // Middle 3
        backup[2..5].to_vec(),                                         // Last 3
        vec![backup[0].clone(), backup[2].clone(), backup[4].clone()], // Every other
    ];

    for (i, subset) in subsets.iter().enumerate() {
        let restored = KeyShares::from_backup_format(subset).unwrap();
        let recovered = restored.recover_private_key().unwrap();
        assert_eq!(
            key_bytes,
            recovered.to_bytes(),
            "Subset {} should recover the same key",
            i
        );
    }
}

// ============================================================================
// P-256 Integration
// ============================================================================

#[test]
fn test_p256_sign_verify_roundtrip() {
    let private_key = P256PrivateKey::random();
    let public_key = private_key.public_key();

    let message = b"Hello, P-256!";
    let signature = private_key.sign(message);

    assert!(
        public_key.verify(message, &signature),
        "P-256 signature should verify"
    );
}

#[test]
fn test_p256_key_serialization() {
    let private_key = P256PrivateKey::random();
    let public_key = private_key.public_key();

    // Test hex roundtrip
    let priv_hex = private_key.to_hex();
    let recovered_priv = P256PrivateKey::from_hex(&priv_hex).unwrap();
    assert_eq!(private_key.to_bytes(), recovered_priv.to_bytes());

    // Test compressed/uncompressed public key
    let compressed = public_key.to_compressed();
    let uncompressed = public_key.to_uncompressed();

    let from_compressed = P256PublicKey::from_bytes(&compressed).unwrap();
    let from_uncompressed = P256PublicKey::from_bytes(&uncompressed).unwrap();

    assert_eq!(public_key.x(), from_compressed.x());
    assert_eq!(public_key.x(), from_uncompressed.x());
}

// ============================================================================
// Hash Chain Tests
// ============================================================================

#[test]
fn test_hash_chain_for_key_derivation() {
    let seed = b"master seed for key derivation";

    // Chain of hashes for deterministic key generation
    let h1 = sha256(seed);
    let h2 = sha256d(&h1);
    let h3 = hash160(&h2);

    // Each step should produce different output
    assert_ne!(h1.as_slice(), h2.as_slice());
    assert_ne!(h1[..20], h3);

    // Results should be deterministic
    let h1_repeat = sha256(seed);
    assert_eq!(h1, h1_repeat);
}

#[test]
fn test_hmac_for_key_derivation() {
    let master_key = b"master secret key";
    let data = b"child key derivation";

    let hmac256 = sha256_hmac(master_key, data);
    let hmac512 = sha512_hmac(master_key, data);

    assert_eq!(hmac256.len(), 32);
    assert_eq!(hmac512.len(), 64);

    // HMAC should be deterministic
    let hmac256_repeat = sha256_hmac(master_key, data);
    assert_eq!(hmac256, hmac256_repeat);
}

// ============================================================================
// Encoding Integration
// ============================================================================

#[test]
fn test_encoding_roundtrips() {
    let data = b"test data for encoding";

    // Hex roundtrip
    let hex = to_hex(data);
    let from_hex_data = from_hex(&hex).unwrap();
    assert_eq!(data.as_slice(), from_hex_data.as_slice());

    // Base64 roundtrip
    let b64 = to_base64(data);
    let from_b64_data = from_base64(&b64).unwrap();
    assert_eq!(data.as_slice(), from_b64_data.as_slice());

    // Base58 roundtrip
    let b58 = to_base58(data);
    let from_b58_data = from_base58(&b58).unwrap();
    assert_eq!(data.as_slice(), from_b58_data.as_slice());
}

#[test]
fn test_address_encoding_integration() {
    let key = PrivateKey::random();
    let pub_key = key.public_key();

    // Get hash160 of public key
    let h160 = pub_key.hash160();

    // Create address manually
    let manual_address = pub_key.to_address();

    // Address should be Base58Check encoded hash160 with version byte 0x00
    let (version, payload) = bsv_rs::primitives::from_base58_check(&manual_address).unwrap();
    assert_eq!(version, vec![0x00]);
    assert_eq!(payload, h160.to_vec());
}

// ============================================================================
// Sighash Integration
// ============================================================================

#[test]
fn test_sighash_with_signature() {
    // Create a simple transaction structure
    let inputs = vec![TxInput {
        txid: [0u8; 32],
        output_index: 0,
        script: vec![],
        sequence: 0xffffffff,
    }];

    let outputs = vec![TxOutput {
        satoshis: 50000,
        script: vec![0x76, 0xa9, 0x14], // Partial P2PKH script
    }];

    let subscript = vec![0x76, 0xa9, 0x14]; // P2PKH script

    let params = SighashParams {
        version: 1,
        inputs: &inputs,
        outputs: &outputs,
        locktime: 0,
        input_index: 0,
        subscript: &subscript,
        satoshis: 100000,
        scope: SIGHASH_ALL | SIGHASH_FORKID,
    };

    // Compute sighash
    let sighash = compute_sighash(&params);

    // Sign the sighash
    let key = PrivateKey::random();
    let signature = key.sign(&sighash).unwrap();

    // Verify signature
    assert!(key.public_key().verify(&sighash, &signature));
}

// ============================================================================
// Complete Workflow Tests
// ============================================================================

#[test]
fn test_complete_payment_workflow() {
    // Simulate a complete payment workflow:
    // 1. Merchant generates a key pair
    // 2. Customer generates a key pair
    // 3. Both derive a payment-specific key using BRC-42
    // 4. Customer encrypts payment data
    // 5. Merchant can decrypt and verify

    // Step 1: Merchant key pair
    let merchant = PrivateKey::random();
    let merchant_pub = merchant.public_key();

    // Step 2: Customer key pair
    let customer = PrivateKey::random();
    let customer_pub = customer.public_key();

    // Step 3: Derive payment keys
    let invoice = "INV-2024-001";
    let customer_payment_key = customer.derive_child(&merchant_pub, invoice).unwrap();
    let merchant_derived_pub = customer_pub.derive_child(&merchant, invoice).unwrap();

    // Keys should match
    assert_eq!(
        customer_payment_key.public_key().to_compressed(),
        merchant_derived_pub.to_compressed()
    );

    // Step 4: Encrypt payment data using shared secret
    let shared_secret = customer.derive_shared_secret(&merchant_pub).unwrap();
    let encryption_key = SymmetricKey::from_bytes(&shared_secret.x()).unwrap();

    let payment_data = b"Payment of $100 for order #12345";
    let encrypted = encryption_key.encrypt(payment_data).unwrap();

    // Step 5: Merchant derives same shared secret and decrypts
    let merchant_shared = merchant.derive_shared_secret(&customer_pub).unwrap();
    let merchant_key = SymmetricKey::from_bytes(&merchant_shared.x()).unwrap();

    let decrypted = merchant_key.decrypt(&encrypted).unwrap();
    assert_eq!(payment_data.as_slice(), decrypted.as_slice());

    // Verify payment signature
    let payment_hash = sha256(payment_data);
    let signature = customer_payment_key.sign(&payment_hash).unwrap();

    assert!(merchant_derived_pub.verify(&payment_hash, &signature));
}

#[test]
fn test_key_backup_and_recovery_workflow() {
    // Complete key backup workflow:
    // 1. Generate master key
    // 2. Split into shares
    // 3. Export shares in backup format
    // 4. Recover from threshold shares
    // 5. Verify recovered key works for signing

    // Step 1: Generate master key
    let master_key = PrivateKey::random();
    let master_pub = master_key.public_key();

    // Step 2: Split into 5 shares, threshold 3
    let shares = split_private_key(&master_key, 3, 5).unwrap();

    // Step 3: Export to backup format
    let backup_strings = shares.to_backup_format();
    assert_eq!(backup_strings.len(), 5);

    // Step 4: Recover from any 3 shares (simulate user selecting shares 1, 3, 4)
    let selected_backups = vec![
        backup_strings[0].clone(),
        backup_strings[2].clone(),
        backup_strings[3].clone(),
    ];

    let restored_shares = KeyShares::from_backup_format(&selected_backups).unwrap();
    let recovered_key = restored_shares.recover_private_key().unwrap();

    // Step 5: Verify recovered key
    assert_eq!(master_key.to_bytes(), recovered_key.to_bytes());

    // Test that recovered key can sign
    let message_hash = sha256(b"Test message after recovery");
    let signature = recovered_key.sign(&message_hash).unwrap();

    assert!(master_pub.verify(&message_hash, &signature));
}

#[test]
fn test_multi_party_schnorr_verification() {
    // Test that both parties can generate and verify Schnorr proofs

    let alice = PrivateKey::random();
    let bob = PrivateKey::random();

    // Both compute the same shared secret
    let alice_shared = alice.derive_shared_secret(&bob.public_key()).unwrap();
    let bob_shared = bob.derive_shared_secret(&alice.public_key()).unwrap();

    assert_eq!(alice_shared.to_compressed(), bob_shared.to_compressed());

    // Alice generates proof
    let alice_proof = Schnorr::generate_proof(
        &alice,
        &alice.public_key(),
        &bob.public_key(),
        &alice_shared,
    )
    .unwrap();

    // Bob generates proof
    let bob_proof =
        Schnorr::generate_proof(&bob, &bob.public_key(), &alice.public_key(), &bob_shared).unwrap();

    // Cross verification
    assert!(Schnorr::verify_proof(
        &alice.public_key(),
        &bob.public_key(),
        &alice_shared,
        &alice_proof
    ));

    assert!(Schnorr::verify_proof(
        &bob.public_key(),
        &alice.public_key(),
        &bob_shared,
        &bob_proof
    ));
}