All files / lib/identity validate.ts

90.9% Statements 60/66
88.23% Branches 15/17
100% Functions 4/4
90.9% Lines 60/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 1251x                                         1x   14x     14x 1x 1x 1x 1x 1x 1x     13x 14x                 14x 1x 1x 1x 1x 1x 1x     12x 14x 1x 1x 1x 1x 1x 1x   11x 11x 11x 11x 11x 11x         1x 10x 10x               1x   8x     8x 8x     8x     8x   3x 3x 3x 3x 3x 1x 1x 3x     8x 6x 6x   2x 2x           1x 1x 1x   1x 1x  
/**
 * ProofID Identity Code Validator
 *
 * Validates identity codes by checking:
 * 1. Format (regex pattern match)
 * 2. Checksum (CRC16 verification)
 */
 
import { IDENTITY_PREFIX, IDENTITY_CODE_PATTERN, RESERVED_IDENTITY_CODES } from './constants';
import { generateChecksum, parseIdentityCode } from './generate';
 
export type ValidationResult =
  | { valid: true; code: string; payload: string }
  | { valid: false; error: string; code: 'INVALID_FORMAT' | 'INVALID_CHECKSUM' | 'RESERVED_CODE' };
 
/**
 * Validate an identity code
 *
 * @param code - The identity code to validate
 * @returns Validation result with details
 */
export function validateIdentityCode(code: string): ValidationResult {
  // Normalize: trim whitespace and convert to uppercase for consistent validation
  const normalizedCode = code.trim().toUpperCase();
 
  // Check format
  if (!IDENTITY_CODE_PATTERN.test(normalizedCode)) {
    return {
      valid: false,
      error: `Invalid format. Expected: ${IDENTITY_PREFIX}-XXXX-XXXX`,
      code: 'INVALID_FORMAT',
    };
  }
 
  // Parse the code
  const parsed = parseIdentityCode(normalizedCode);
  if (!parsed) {
    return {
      valid: false,
      error: 'Could not parse identity code',
      code: 'INVALID_FORMAT',
    };
  }
 
  // Check if reserved
  if (RESERVED_IDENTITY_CODES.includes(normalizedCode)) {
    return {
      valid: false,
      error: 'This is a reserved identity code',
      code: 'RESERVED_CODE',
    };
  }
 
  // Verify checksum
  const expectedChecksum = generateChecksum(parsed.payload);
  if (parsed.checksum !== expectedChecksum) {
    return {
      valid: false,
      error: 'Invalid checksum - the identity code may be corrupted',
      code: 'INVALID_CHECKSUM',
    };
  }
 
  return {
    valid: true,
    code: normalizedCode,
    payload: parsed.payload,
  };
}
 
/**
 * Quick validation check (boolean only)
 */
export function isValidIdentityCode(code: string): boolean {
  return validateIdentityCode(code).valid;
}
 
/**
 * Normalize an identity code (handles common input variations)
 *
 * @param input - User input that might be an identity code
 * @returns Normalized code (uppercase) or null if not recognizable
 */
export function normalizeIdentityCode(input: string): string | null {
  // Remove whitespace
  let normalized = input.trim();
 
  // Remove common prefixes users might include (case-insensitive)
  normalized = normalized.replace(/^(https?:\/\/)?(www\.)?proofid\.in\/verify\//i, '');
  normalized = normalized.replace(/^verify\//i, '');
 
  // Convert to uppercase for consistent handling
  normalized = normalized.toUpperCase();
 
  // Handle missing prefix
  if (!normalized.startsWith(IDENTITY_PREFIX)) {
    // Check if it looks like the payload part (XXXX-XXXX)
    if (
      /^[123456789ABCDEFGHJKLMNPQRSTUVWXYZ]{4}-[123456789ABCDEFGHJKLMNPQRSTUVWXYZ]{4}$/.test(
        normalized
      )
    ) {
      normalized = `${IDENTITY_PREFIX}-${normalized}`;
    }
  }
 
  // Validate the normalized code (pattern is case-insensitive)
  if (IDENTITY_CODE_PATTERN.test(normalized)) {
    return normalized;
  }
 
  return null;
}
 
/**
 * Format an identity code for display
 * (ensures consistent casing and formatting)
 */
export function formatIdentityCode(code: string): string {
  const parsed = parseIdentityCode(code);
  if (!parsed) return code;
 
  return `${parsed.prefix}-${parsed.payload}-${parsed.checksum}`;
}