Skip to main content
The CodeGenerator class provides static utilities for generating, validating, and normalizing action codes and prefixes in the Action Codes Protocol. It ensures codes are deterministic, secure, and conform to protocol standards.

Constants

  • TIME_WINDOW_MS: Code validity window in milliseconds (default: protocol TTL)
  • CODE_DIGITS: Number of digits in the code (default: 8)
  • MIN_PREFIX_LENGTH: Minimum allowed prefix length
  • MAX_PREFIX_LENGTH: Maximum allowed prefix length

Static Methods

validatePrefix

Validates the format of a prefix. Returns true if valid, false otherwise.

validateCodeFormat

Validates that a code is in the correct format (prefix + exactly 8 digits). Returns true if valid.

validateCodeDigits

Checks that the numeric part of a code is exactly 8 digits. Returns true if valid.

normalizePrefix

Normalizes a prefix (converts protocol default to empty string, validates others). Throws if invalid.

generateCode

Generates a deterministic 8-digit code based on public key, prefix, and timestamp. Returns the code, issuedAt, and expiresAt.

deriveCodeHash

Derives a full SHA-256 hash for storage or encryption key generation.

getExpectedCode

Returns the expected code for a given public key and timestamp.

validateCode

Validates if a code matches the expected code for a given public key and timestamp.

isValidTimestamp

Checks if a timestamp falls within the valid time window.

Example