Class VoucherValidator

java.lang.Object
xyz.tcheeric.cashu.voucher.domain.VoucherValidator

public final class VoucherValidator extends Object
Validates vouchers for correctness and authenticity.

This class provides comprehensive validation logic for signed vouchers, including:

  • Cryptographic signature verification
  • Expiry validation
  • Face value validation
  • Business rule validation

Validation Rules

A voucher is considered valid if ALL of the following conditions are met:

  1. The issuer's signature is cryptographically valid (ED25519 verification)
  2. The voucher has not expired (if expiry is set)
  3. The face value is positive

Usage Examples

 // Basic validation
 ValidationResult result = VoucherValidator.validate(signedVoucher);
 if (result.isValid()) {
     // Proceed with redemption
 } else {
     // Handle errors
     result.getErrors().forEach(System.err::println);
 }

 // Validation with issuer check
 ValidationResult result = VoucherValidator.validateWithIssuer(
     signedVoucher,
     expectedIssuerId
 );
 

Thread Safety

All methods are stateless and thread-safe.

See Also:
  • Method Details

    • validate

      public static VoucherValidator.ValidationResult validate(@NonNull @NonNull SignedVoucher voucher)
      Validates a signed voucher.

      Performs comprehensive validation including:

      • Signature verification
      • Expiry check
      • Face value validation
      Parameters:
      voucher - the signed voucher to validate (must not be null)
      Returns:
      validation result with success status and any errors
    • validateWithIssuer

      public static VoucherValidator.ValidationResult validateWithIssuer(@NonNull @NonNull SignedVoucher voucher, @NonNull @NonNull String expectedIssuerId)
      Validates a signed voucher and checks that it was issued by the expected issuer.

      This is useful for merchant-side validation to ensure the voucher was issued by their own merchant account.

      Parameters:
      voucher - the signed voucher to validate (must not be null)
      expectedIssuerId - the expected issuer ID (must not be null)
      Returns:
      validation result with success status and any errors
    • validateSignatureOnly

      public static VoucherValidator.ValidationResult validateSignatureOnly(@NonNull @NonNull SignedVoucher voucher)
      Validates just the cryptographic signature of a voucher.

      This is a lightweight check that only verifies the signature, without checking expiry or other business rules.

      Parameters:
      voucher - the signed voucher to validate (must not be null)
      Returns:
      validation result for signature only
    • validateExpiryOnly

      public static VoucherValidator.ValidationResult validateExpiryOnly(@NonNull @NonNull SignedVoucher voucher)
      Validates just the expiry of a voucher.

      This is useful for periodic checks of voucher validity without re-verifying the signature.

      Parameters:
      voucher - the signed voucher to validate (must not be null)
      Returns:
      validation result for expiry only
    • isValid

      public static boolean isValid(@NonNull @NonNull SignedVoucher voucher)
      Quick check if a voucher is valid (no detailed errors).

      This is a convenience method equivalent to validate(voucher).isValid().

      Parameters:
      voucher - the signed voucher to check (must not be null)
      Returns:
      true if voucher is valid, false otherwise