Skip to content

Commit

Permalink
Add expiry time to cached SuiNS name validation (#19048)
Browse files Browse the repository at this point in the history
## Description 

Describe the changes or additions included in this PR.

## Test plan 

How did you test the new or updated feature?

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
Jibz-Mysten authored Aug 20, 2024
1 parent 7802581 commit bd887c6
Showing 1 changed file with 14 additions and 3 deletions.
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 }>();

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

0 comments on commit bd887c6

Please sign in to comment.