Skip to content

Latest commit

 

History

History
188 lines (146 loc) · 5.46 KB

File metadata and controls

188 lines (146 loc) · 5.46 KB

Commit Message

feat: add Stellar public key validation across request schemas

Implement comprehensive validation for sender and recipient fields to ensure
they conform to Stellar public key format (G followed by 55 base32 characters).

- Add STELLAR_PUBLIC_KEY_REGEX and stellarPublicKeyField() helper in validation schemas
- Integrate Zod validation in streams route with clear error messages
- Add comprehensive test coverage for valid and invalid key formats
- Update OpenAPI spec with detailed format requirements and examples
- Export _resetStreams() for test compatibility

Validation enforces:
- Keys must start with 'G'
- Exactly 56 characters total
- Base32 alphabet only [A-Z2-7]

All changes are backward compatible and maintain existing security guarantees.

Pull Request Description

🎯 Summary

Add Stellar public key validation across request schemas to ensure all sender and recipient fields contain valid Stellar public keys before processing.

📝 Changes

Core Implementation

  • src/validation/schemas.ts

    • Added STELLAR_PUBLIC_KEY_REGEX constant: /^G[A-Z2-7]{55}$/
    • Created stellarPublicKeyField() helper for reusable Zod validation
    • Updated CreateStreamSchema to validate sender/recipient as Stellar keys
    • Added .refine() validators for positive amount checks
  • src/routes/streams.ts

    • Integrated Zod schema validation in normalizeCreateStreamInput()
    • Added _resetStreams() export for test compatibility
    • Imported validation utilities from schemas module

Testing

  • tests/routes/streams.test.ts
    • Added 10+ new test cases covering:
      • Valid Stellar public keys ✓
      • Missing/empty fields ✓
      • Too short keys ✓
      • Wrong prefix (non-G) ✓
      • Invalid characters (non-base32) ✓
      • Generic strings ✓

Documentation

  • openapi.yaml
    • Enhanced sender/recipient descriptions with format details
    • Added regex pattern validation to schemas
    • Included clear examples of valid Stellar keys

🔒 Validation Rules

Stellar Public Key Format:

  • Prefix: Must start with G
  • Length: Exactly 56 characters
  • Character Set: Base32 alphabet [A-Z2-7]
  • Regex: ^G[A-Z2-7]{55}$

Valid Examples:

GAAZI4TCR3TY5OJHCTJC2A4QSY6CJWJH5IAJTGKIN2ER7LBNVKOCCWN7
GBDEVU63Y6NTHJQQZIKVTC23NWLQVP3WJ2RI2OTSJTNYOIGICST6DUXR

Invalid Examples:

GABC123                    ❌ Too short
AAAZI4TCR3...              ❌ Wrong prefix
G1111111...                ❌ Invalid characters
not-a-stellar-key          ❌ Not a Stellar key

🧪 Testing

All tests pass with comprehensive coverage:

✓ Valid Stellar public keys accepted
✓ Missing sender/recipient rejected
✓ Empty sender/recipient rejected
✓ Invalid formats rejected (short, wrong prefix, invalid chars)
✓ Generic strings rejected
✓ Error messages are clear and actionable

TypeScript Diagnostics: ✅ No errors

📊 Error Response Example

{
  "error": "Validation failed",
  "details": "sender must be a valid Stellar public key (G...)",
  "status": 400
}

✨ Key Features

  • Type Safety: Full TypeScript + Zod runtime validation
  • Reusability: stellarPublicKeyField() helper for DRY principle
  • Security: Validation at API boundary before processing
  • Clarity: Descriptive error messages for debugging
  • Documentation: Updated OpenAPI spec with format details
  • Testing: Comprehensive coverage of edge cases
  • Backward Compatible: No breaking changes

🔄 Backward Compatibility

  • ✅ Existing valid requests continue to work
  • ✅ Only rejects previously invalid Stellar key formats
  • ✅ All existing functionality preserved
  • ✅ Maintains decimal string serialization guarantees
  • ✅ No changes to existing API contracts

📚 Documentation

Created comprehensive documentation:

  • IMPLEMENTATION_SUMMARY.md - Complete implementation overview
  • STELLAR_KEY_VALIDATION_GUIDE.md - Quick reference for developers
  • CHANGES_DETAILED.md - File-by-file change details

🎯 Checklist

  • Code follows project style guidelines
  • Self-review completed
  • Code is well-commented
  • Documentation updated (OpenAPI spec)
  • Tests added for new functionality
  • All tests pass
  • No TypeScript errors
  • Backward compatible
  • Security best practices followed
  • Error messages are clear and actionable

🔗 Related Issues

Implements: Add Stellar public key validation across request schemas

👨‍💻 Reviewer Notes

Focus Areas:

  1. Regex pattern correctness for Stellar keys
  2. Error message clarity
  3. Test coverage completeness
  4. OpenAPI spec accuracy
  5. Backward compatibility

Testing:

# Run tests
npm test -- tests/routes/streams.test.ts

# Check TypeScript
npx tsc --noEmit

# Test API endpoint
curl -X POST http://localhost:3000/api/streams \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: test-123" \
  -d '{
    "sender": "GAAZI4TCR3TY5OJHCTJC2A4QSY6CJWJH5IAJTGKIN2ER7LBNVKOCCWN7",
    "recipient": "GBDEVU63Y6NTHJQQZIKVTC23NWLQVP3WJ2RI2OTSJTNYOIGICST6DUXR",
    "depositAmount": "1000",
    "ratePerSecond": "10"
  }'

📈 Impact

  • Security: ✅ Prevents malformed addresses at API boundary
  • Developer Experience: ✅ Clear validation errors
  • API Quality: ✅ Enforces Stellar standards
  • Maintainability: ✅ Centralized validation logic
  • Performance: ✅ No impact (validation is fast)

Ready for review