Class VoucherSecret
- All Implemented Interfaces:
xyz.tcheeric.cashu.common.Secret
VoucherSecret represents a non-deterministic gift card voucher that follows the Cashu
protocol's secret structure. Unlike DeterministicSecret,
vouchers are NOT deterministic and MUST be backed up to Nostr (or other storage) for recovery.
Model B Constraint
Vouchers can only be redeemed (for goods/services) at the issuing merchant. Swaps at the mint are allowed and essential for P2P transfers and double-spend prevention. Model B enforcement (merchant-only redemption) happens at the application layer.
Immutability
This class is immutable once created. All fields are final and the canonical byte representation is deterministic for signature generation.
Serialization
VoucherSecret uses canonical CBOR serialization (via VoucherSerializationUtils)
to ensure deterministic byte representation for ED25519 signature generation. Field ordering
is alphabetical to guarantee consistency.
Jackson JSON serialization uses VoucherSecretSerializer to serialize as hex string.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for VoucherSecret with fluent API. -
Field Summary
Fields inherited from class xyz.tcheeric.cashu.common.BaseKey
COMPRESSED_KEY_HEX_LENGTH, PRIVATE_KEY_HEX_LENGTH, PRIVATE_KEY_LENGTH, PUBLIC_KEY_LENGTH_COMPRESSED, PUBLIC_KEY_LENGTH_UNCOMPRESSED, SECRET_HEX_LENGTH, SECRET_LENGTH, UNCOMPRESSED_XY_HEX_LENGTH, X_COORDINATE_HEX_LENGTH -
Method Summary
Modifier and TypeMethodDescriptionstatic VoucherSecret.Builderbuilder()Returns a builder for creating VoucherSecret instances with fluent API.static VoucherSecretcreate(@NonNull String issuerId, @NonNull String unit, long faceValue, Long expiresAt, String memo, @NonNull BackingStrategy backingStrategy, double issuanceRatio, int faceDecimals, Map<String, Object> merchantMetadata) Creates a new voucher with auto-generated UUID.static VoucherSecretcreate(@NonNull String voucherId, @NonNull String issuerId, @NonNull String unit, long faceValue, Long expiresAt, String memo, @NonNull BackingStrategy backingStrategy, double issuanceRatio, int faceDecimals, Map<String, Object> merchantMetadata) Creates a voucher with specified voucher ID.booleanbyte[]getData()Returns canonical bytes for Secret interface.inthashCode()booleanChecks if this voucher has expired.booleanisValid()Checks if this voucher is valid (not expired).voidsetData(@lombok.NonNull byte[] data) VoucherSecret is immutable - data cannot be modified after creation.byte[]toBytes()Alias fortoCanonicalBytes()for Secret interface.byte[]Canonical serialization for deterministic signing.Returns hex-encoded canonical representation.toString()Returns hex-encoded string representation.Returns a detailed string representation including metadata.Methods inherited from class xyz.tcheeric.cashu.common.BaseKey
canEqual, equalsIgnoreCase, getBytes, setBytes
-
Method Details
-
create
public static VoucherSecret create(@NonNull @NonNull String issuerId, @NonNull @NonNull String unit, long faceValue, Long expiresAt, String memo, @NonNull @NonNull BackingStrategy backingStrategy, double issuanceRatio, int faceDecimals, Map<String, Object> merchantMetadata) Creates a new voucher with auto-generated UUID.- Parameters:
issuerId- issuer identifierunit- currency unitfaceValue- face value (must be positive)expiresAt- optional expiry timestamp (Unix epoch seconds)memo- optional memobackingStrategy- backing strategy (required)issuanceRatio- face value per sat (must be positive)faceDecimals- decimal places for face value (must be non-negative)merchantMetadata- optional merchant-defined metadata- Returns:
- new VoucherSecret instance
- Throws:
IllegalArgumentException- if parameters are invalid
-
create
public static VoucherSecret create(@NonNull @NonNull String voucherId, @NonNull @NonNull String issuerId, @NonNull @NonNull String unit, long faceValue, Long expiresAt, String memo, @NonNull @NonNull BackingStrategy backingStrategy, double issuanceRatio, int faceDecimals, Map<String, Object> merchantMetadata) Creates a voucher with specified voucher ID.This method is primarily for deserialization and testing.
- Parameters:
voucherId- voucher identifierissuerId- issuer identifierunit- currency unitfaceValue- face value (must be positive)expiresAt- optional expiry timestamp (Unix epoch seconds)memo- optional memobackingStrategy- backing strategy (required)issuanceRatio- face value per sat (must be positive)faceDecimals- decimal places for face value (must be non-negative)merchantMetadata- optional merchant-defined metadata- Returns:
- new VoucherSecret instance
- Throws:
IllegalArgumentException- if parameters are invalid
-
builder
Returns a builder for creating VoucherSecret instances with fluent API.- Returns:
- new builder instance
-
toCanonicalBytes
public byte[] toCanonicalBytes()Canonical serialization for deterministic signing.Fields are serialized to CBOR in alphabetical order:
- backingStrategy
- expiresAt (if present)
- faceDecimals
- faceValue
- issuanceRatio
- issuerId
- memo (if present)
- merchantMetadata (if not empty)
- unit
- voucherId
- Returns:
- CBOR-encoded bytes representing this voucher
-
toHexString
Returns hex-encoded canonical representation.This is the serialization format used in Cashu tokens. For JSON serialization, see
VoucherSecretSerializer.- Returns:
- hex-encoded string of canonical CBOR bytes
-
toBytes
public byte[] toBytes()Alias fortoCanonicalBytes()for Secret interface.- Specified by:
toBytesin interfacexyz.tcheeric.cashu.common.Secret- Overrides:
toBytesin classxyz.tcheeric.cashu.common.BaseKey- Returns:
- canonical CBOR bytes
-
getData
public byte[] getData()Returns canonical bytes for Secret interface.- Specified by:
getDatain interfacexyz.tcheeric.cashu.common.Secret- Returns:
- canonical CBOR bytes
-
setData
public void setData(@NonNull @lombok.NonNull byte[] data) VoucherSecret is immutable - data cannot be modified after creation.- Specified by:
setDatain interfacexyz.tcheeric.cashu.common.Secret- Parameters:
data- ignored- Throws:
UnsupportedOperationException- always thrown
-
isExpired
public boolean isExpired()Checks if this voucher has expired.- Returns:
- true if expired, false if valid or no expiry set
-
isValid
public boolean isValid()Checks if this voucher is valid (not expired).- Returns:
- true if not expired or no expiry set, false if expired
-
toString
Returns hex-encoded string representation.Note: JSON serialization uses
VoucherSecretSerializerinstead.- Overrides:
toStringin classxyz.tcheeric.cashu.common.BaseKey- Returns:
- hex string for display/logging (without sensitive data)
-
toStringWithMetadata
Returns a detailed string representation including metadata. Useful for debugging and logging.- Returns:
- human-readable string with voucher metadata
-
equals
- Overrides:
equalsin classxyz.tcheeric.cashu.common.BaseKey
-
hashCode
public int hashCode()- Overrides:
hashCodein classxyz.tcheeric.cashu.common.BaseKey
-