exact_recovery_tests.rs (4164B)
1 //! Real local signatures with the original issuance window kept intact. 2 use super::*; 3 use radroots_signing::issuance::{ 4 ExactRecoveryCapability, ExactRecoveryKind, ExactRecoveryRequest, 5 }; 6 7 fn request(original: SigningIntent, cancellation: CancellationSignal) -> ExactRecoveryRequest { 8 ExactRecoveryRequest::new( 9 original, 10 ProviderCall::new( 11 AUTHOR.parse().unwrap(), 12 NonZeroU64::new(DEADLINE + 10_000).unwrap(), 13 cancellation, 14 ), 15 ) 16 .unwrap() 17 } 18 19 #[tokio::test] 20 async fn local_recovery_returns_only_original_exact_fields_and_keeps_obtained_bytes_after_observation_failure() 21 { 22 for mode in [0, 1, 2, 6, 7, 9] { 23 let (signer, original) = setup(mode); 24 let expected = original.binding(); 25 let event_id = original.with_event(|_, id| id); 26 let state = signer.provider_state().await.unwrap(); 27 assert_eq!( 28 state.exact_issuance(), 29 Some(ReplayCapability::NonReplayable) 30 ); 31 assert_eq!( 32 state.exact_recovery(), 33 ExactRecoveryCapability::LocalExactReplay 34 ); 35 let outcome = signer 36 .recover_exact(request(original, CancellationSignal::new())) 37 .await; 38 assert_eq!(outcome.binding(), expected); 39 let ExactRecoveryKind::Obtained(artifact) = outcome.kind() else { 40 panic!("actual original signature lost: {outcome:?}") 41 }; 42 artifact.with_json(|raw| { 43 let event = nostr::Event::from_json(raw).unwrap(); 44 event.verify().unwrap(); 45 assert_eq!(event.id.to_hex(), event_id.to_hex()); 46 assert_eq!(event.pubkey.to_hex(), AUTHOR); 47 assert_eq!(event.content, "synthetic unchanged exact preimage"); 48 }); 49 assert_eq!( 50 artifact.observed_at_unix_ms().is_none(), 51 matches!(mode, 1 | 7 | 9) 52 ); 53 CLOCK.with(|state| assert_eq!(state.borrow().calls, 2)); 54 } 55 } 56 57 #[tokio::test] 58 async fn fresh_recovery_budget_never_renews_expired_original_or_proves_it_unissued() { 59 for mode in [3, 4, 5, 8] { 60 let (signer, original) = setup(mode); 61 let expected = original.binding(); 62 let outcome = signer 63 .recover_exact(request(original, CancellationSignal::new())) 64 .await; 65 assert_eq!(outcome.binding(), expected); 66 assert!(matches!( 67 outcome.kind(), 68 ExactRecoveryKind::Indeterminate( 69 ProviderError::ProviderFailure | ProviderError::DeadlineExceeded 70 ) 71 )); 72 CLOCK.with(|state| assert_eq!(state.borrow().calls, 1)); 73 } 74 } 75 76 #[tokio::test] 77 async fn current_cancellation_wrong_actual_key_and_shorter_recovery_budget_refuse_before_crypto() { 78 let (signer, original) = setup(0); 79 let signal = CancellationSignal::new(); 80 signal.cancel(); 81 let outcome = signer.recover_exact(request(original, signal)).await; 82 assert!(matches!( 83 outcome.kind(), 84 ExactRecoveryKind::Indeterminate(ProviderError::Cancelled) 85 )); 86 CLOCK.with(|state| assert_eq!(state.borrow().calls, 1)); 87 let (_, original) = setup(0); 88 let other = LocalSigner::with_clock( 89 parse_secret_key("0000000000000000000000000000000000000000000000000000000000000002") 90 .unwrap(), 91 clock, 92 ) 93 .unwrap(); 94 let outcome = other 95 .recover_exact(request(original, CancellationSignal::new())) 96 .await; 97 assert!(matches!( 98 outcome.kind(), 99 ExactRecoveryKind::Indeterminate(ProviderError::IdentityChanged) 100 )); 101 CLOCK.with(|state| assert_eq!(state.borrow().calls, 0)); 102 let (signer, original) = setup(0); 103 let call = ProviderCall::new( 104 signer.public_key(), 105 NonZeroU64::new(CREATED * 1000).unwrap(), 106 CancellationSignal::new(), 107 ); 108 let outcome = signer 109 .recover_exact(ExactRecoveryRequest::new(original, call).unwrap()) 110 .await; 111 assert!(matches!( 112 outcome.kind(), 113 ExactRecoveryKind::Indeterminate(ProviderError::DeadlineExceeded) 114 )); 115 CLOCK.with(|state| assert_eq!(state.borrow().calls, 1)); 116 }