🔍 KYC Implementation Guide
📝 Introduction
KYC (Know Your Customer) is an essential process for verifying user identity and preventing fraud. In this article, we'll explore how to implement a basic KYC system using the KYC_CHECK library.
🎯 Main Validations
The system implements two main validations:
- First Validation 👤: Verifies if the person is the actual owner of the presented document
- Withdrawal Validation 💰: Verifies if it's the user attempting to make a significant withdrawal
🚀 Implementation for New Users
For new users, we perform the first validation:
- Process 🔄: Comparison between the document sent and the First Selfie
🔄 Subsequent Validations
For future validations, we keep the user's most recent photo updated:
- Process 📸: Comparison between Last Selfie and New Selfie
- Monthly Validation 📅: If the user hasn't added a new photo within 1 month, a new validation will be required
💻 Implementation Example
Let's consider a scenario where a user wants to make a withdrawal greater than 70% of their balance and more than $100.
Note ℹ️: The $100 threshold is implemented to avoid unnecessary validations for small amounts. For example, it wouldn't make sense to request a new selfie to validate a $7 withdrawal from a $10 balance.
interface ValidationResult {
isValid: boolean;
message?: string;
}
function validateWithdrawal(amount: number, userBalance: number) {
const percentageThreshold = 0.7; // 70%
const amountThreshold = 10000; // $100
if (userBalance > amountThreshold && amount > userBalance * amountThreshold ) {
return performNewValidation();
}
return true;
}
🤖 Face Similarity Calculation
The face similarity is calculated using the Euclidean distance between facial descriptors. Here's how it works:
📊 Example Response:
{
"threshold": 0.49,
"rawDistance": 0.5034011876789618,
"faceDetection1": {
"score": 0.9882098436355591,
"box": {
"x": 95.0972925721421,
"y": 76.55234995484352,
"width": 124.7091704960397,
"height": 140.45916613936424
}
},
"faceDetection2": {
"score": 0.9953930974006653,
"box": {
"x": 41.640078008340005,
"y": 106.15492933988571,
"width": 264.0857750636389,
"height": 309.0272338986397
}
},
"processingTimeMs": 291,
"usingMockImplementation": false
}
🧮 Understanding the Calculation:
Euclidean Distance (rawDistance) 📏: 0.5034011876789618
- This is the mathematical measure of the difference between facial descriptors
- Values close to 0 indicate identical faces
- Values close to 1 or higher indicate completely different faces
Similarity Calculation 🔢:
similarity = 1 - euclideanDistance
- In this case: 1 - 0.5034 ≈ 0.4966 (approximately 50%)
Percentage Conversion 📊:
percentage = similarity * 100
- In this case: 0.4966 * 100 ≈ 50%
Match Evaluation ✅:
- The threshold is set to 0.49 (49%)
- Since the similarity (50%) is greater than the threshold (49%), the faces are considered a match
The facial descriptors are 128-dimensional vectors that represent facial characteristics. The Euclidean distance measures the difference between these vectors in multidimensional space.
A value of 50% represents a borderline match - not a very strong match, but it's above the configured minimum threshold.
🎯 Final Considerations
- Percentage and amount thresholds should be adjusted according to specific business rules
- It's important to maintain a history of performed validations
- Consider implementing a notification system for users during the validation process