Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add expiry time to cached SuiNS name validation #19048

Merged
merged 2 commits into from
Aug 20, 2024
Merged
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
17 changes: 14 additions & 3 deletions apps/wallet/src/ui/app/components/address-input/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,35 @@ import { isValidSuiAddress, isValidSuiNSName } from '@mysten/sui/utils';
import { useMemo } from 'react';
import * as Yup from 'yup';

const CACHE_EXPIRY_TIME = 60 * 1000; // 1 minute in milliseconds

export function createSuiAddressValidation(client: SuiClient, suiNSEnabled: boolean) {
const resolveCache = new Map<string, boolean>();
const resolveCache = new Map<string, { valid: boolean; expiry: number }>();
Copy link
Contributor

Choose a reason for hiding this comment

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

Could also use an LRU cache to do this but this is fine.


const currentTime = Date.now();
return Yup.string()
.ensure()
.trim()
.required()
.test('is-sui-address', 'Invalid address. Please check again.', async (value) => {
if (suiNSEnabled && isValidSuiNSName(value)) {
if (resolveCache.has(value)) {
return resolveCache.get(value)!;
const cachedEntry = resolveCache.get(value)!;
if (currentTime < cachedEntry.expiry) {
return cachedEntry.valid;
} else {
resolveCache.delete(value); // Remove expired entry
}
}

const address = await client.resolveNameServiceAddress({
name: value,
});

resolveCache.set(value, !!address);
resolveCache.set(value, {
valid: !!address,
expiry: currentTime + CACHE_EXPIRY_TIME,
});

return !!address;
}
Expand Down
Loading