fuzz_spending_conditions.rs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #![no_main]
  2. use std::str::FromStr;
  3. use libfuzzer_sys::fuzz_target;
  4. use cashu::nuts::nut10::Secret as Nut10Secret;
  5. use cashu::nuts::nut11::{Conditions, SpendingConditions};
  6. use cashu::secret::Secret;
  7. fuzz_target!(|data: &str| {
  8. // Fuzz HTLC creation with preimage (hex string validation)
  9. // This tests the 32-byte preimage requirement and hex decoding
  10. let _ = SpendingConditions::new_htlc(data.to_string(), None);
  11. // Fuzz HTLC creation from hash string directly
  12. let _ = SpendingConditions::new_htlc_hash(data, None);
  13. // Fuzz SpendingConditions extraction from raw Secret
  14. // This exercises the full parsing pipeline: Secret -> Nut10Secret -> SpendingConditions
  15. if let Ok(secret) = Secret::from_str(data) {
  16. let _: Result<SpendingConditions, _> = SpendingConditions::try_from(&secret);
  17. }
  18. // Fuzz SpendingConditions from Nut10Secret
  19. if let Ok(nut10_secret) = serde_json::from_str::<Nut10Secret>(data) {
  20. let _: Result<SpendingConditions, _> = SpendingConditions::try_from(nut10_secret);
  21. }
  22. // Fuzz Conditions JSON deserialization directly
  23. let _: Result<Conditions, _> = serde_json::from_str(data);
  24. // Fuzz Conditions from tags (Vec<Vec<String>>)
  25. // This tests the tag parsing logic for locktime, pubkeys, refund_keys, n_sigs, sigflag
  26. if let Ok(tags) = serde_json::from_str::<Vec<Vec<String>>>(data) {
  27. let _: Result<Conditions, _> = Conditions::try_from(tags);
  28. }
  29. });