Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/services/kyc/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,22 @@ class KeetaKYCVerification {
...this.request
}));
}

/**
* Wait for the certificates to be available, polling at the given interval
* and timing out after the given timeout period.
*/
async waitForCertificates(pollInterval: number = 500, timeout: number = 600000): Promise<KeetaKYCAnchorClientGetCertificateResponse> {
for (const startTime = Date.now(); Date.now() - startTime < timeout; ) {
try {
return(await this.getCertificates());
} catch (getCertificatesError) {
/* XXX:TODO */
Comment on lines +282 to +286
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error handling immediately re-throws all errors, preventing any polling behavior. The method should only re-throw non-recoverable errors and sleep before retrying on expected failures (e.g., certificate not ready yet). The pollInterval parameter should be used to implement the delay between retries.

Suggested change
for (const startTime = Date.now(); Date.now() - startTime < timeout; ) {
try {
return(await this.getCertificates());
} catch (getCertificatesError) {
/* XXX:TODO */
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
try {
return(await this.getCertificates());
} catch (getCertificatesError: any) {
// Only retry on expected recoverable errors (e.g., certificate not ready yet)
// Customize this check as needed for your error types
if (
getCertificatesError?.code === 'CERTIFICATE_NOT_READY' ||
(getCertificatesError?.message && getCertificatesError.message.includes('not ready')) ||
getCertificatesError?.status === 404 // Example: 404 means not found yet
) {
// Wait before retrying
await new Promise(resolve => setTimeout(resolve, pollInterval));
continue;
}
// Non-recoverable error, re-throw

Copilot uses AI. Check for mistakes.
throw(getCertificatesError);
Comment on lines +282 to +287
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The for loop will execute continuously without any delay between iterations, potentially causing high CPU usage and unnecessary API calls. After a failed attempt, the code should wait for 'pollInterval' milliseconds before retrying.

Suggested change
for (const startTime = Date.now(); Date.now() - startTime < timeout; ) {
try {
return(await this.getCertificates());
} catch (getCertificatesError) {
/* XXX:TODO */
throw(getCertificatesError);
const startTime = Date.now();
while (Date.now() - startTime < timeout) {
try {
return(await this.getCertificates());
} catch (getCertificatesError) {
// Wait for pollInterval before retrying
await new Promise(resolve => setTimeout(resolve, pollInterval));

Copilot uses AI. Check for mistakes.
}
}
throw(new Error('Timeout waiting for KYC certificates'));
}
}

/**
Expand Down