Skip to content

Conversation

@scottrepreneur
Copy link
Contributor

Summary

Implements BUILD-1148 by adding comprehensive Ethereum address validation using viem's isAddress function to prevent invalid address records from being saved to the database.

Changes

  • Add address validation utility (src/utils/viem.ts)

    • validateEthereumAddress() function that throws on invalid addresses
    • isValidEthereumAddress() boolean helper function
    • Uses viem's isAddress for robust validation (0x prefix, 42 chars, valid hex, EIP-55 checksum)
  • Update Users Service (src/db/services/users-service.ts)

    • Add validation to all address insertion/processing points:
      • createOrUpdateUser() - validates before adding addresses
      • saveUserAddress() - validates before saving address records
      • updateAddressVerification() - validates before verification updates
      • getUserByAddress() - validates lookup addresses
    • Throws clear errors instead of saving invalid data
  • Update API Routes (src/routes/users.ts)

    • Add validation to address parameters in:
      • GET /address/:address
      • GET /address/:address/permissions
      • GET /verifications?address=...
    • Return proper 400 status codes for invalid addresses

Validation Requirements

  • ✅ Must be 0x prefixed and exactly 42 characters long
  • ✅ Must contain only valid hexadecimal characters
  • ✅ Uses viem's isAddress function as specified
  • ✅ Throws errors instead of saving invalid records

Test Results

Valid addresses accepted:

  • 0x1234567890123456789012345678901234567890
  • 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Invalid addresses rejected:

  • 0xgetuser1234567890123456789012345678901234 ❌ (non-hex chars)
  • 0xnonexistentaddress ❌ (wrong length)
  • invalid-address ❌ (no 0x prefix)

Test plan

  • Verify valid addresses are accepted in all service methods
  • Verify invalid addresses are rejected with clear error messages
  • Verify API routes return proper 400 status codes for invalid addresses
  • Verify database operations throw instead of saving invalid data

🤖 Generated with Claude Code

Addresses BUILD-1144 where Ethereum addresses (42 chars) caused 400 Bad Request
errors when searching users via Neynar API, which requires q param ≤20 chars.

Changes:
- Add input validation in searchUsersByUsername() before API call
- Handle Ethereum addresses (0x + 42 chars) with specific error message
- Handle any query >20 chars with generic length error message
- Update /search route to return 400 with descriptive errors
- Add comprehensive tests for validation edge cases

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@linear
Copy link

linear bot commented Aug 29, 2025

BUILD-1148 Don't save invalid address records, throw instead

  • must be 0x and 42 alpha characters
  • check isAddress from viem before processing

Screenshot 2025-08-28 at 11.44.56.png

…sses

- Add validateEthereumAddress utility using viem's isAddress function
- Validate addresses before database operations in users-service
- Add validation to API route address parameters
- Throw clear errors instead of saving invalid address records
- Addresses must be 0x prefixed, 42 characters, valid hex

Fixes BUILD-1148

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@scottrepreneur scottrepreneur force-pushed the feature/build-1148-dont-save-invalid-address-records-throw-instead branch from cca252b to f343f93 Compare August 29, 2025 18:05
scottrepreneur and others added 2 commits August 29, 2025 13:14
- Replace invalid test addresses containing non-hex characters with valid ones
- Use unique FIDs and addresses to avoid test interference
- Fix test isolation issues where tests were affecting each other
- All 31 tests now pass

Co-authored-by: Claude <noreply@anthropic.com>
- Replace invalid test addresses with valid 42-character hex addresses
- Update test expectations for proper address validation behavior
- Fix non-existent address test to use dynamically generated unique address
- Rename 'service error' test to 'invalid address format' with correct expectations
- All 24 user route tests now pass with proper address validation

Co-authored-by: Claude <noreply@anthropic.com>
@scottrepreneur scottrepreneur merged commit b0cf62e into main Aug 29, 2025
1 check passed
@scottrepreneur scottrepreneur deleted the feature/build-1148-dont-save-invalid-address-records-throw-instead branch August 29, 2025 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants