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

feat: blacklist wildcards #3091

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
25 changes: 23 additions & 2 deletions packages/frontend/src/hooks/useTokenBlacklist.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
import { useMemo } from 'react';

/**
* Determine whether a fungible token contract should be included given a token blacklist
* @param tokenContract {string} token contract name under consideration
* @param blacklist {string[]} list of blacklisted tokens; either the full name or a leading wildcard ('*')
* @returns {boolean}
*/
export function isTokenIncluded(tokenContract, blacklist) {
for (let blacklistedToken of blacklist) {
if (tokenContract === blacklistedToken) {
return false;
}

// e.g. *.mallory.near would exclude a.mallory.near and b.mallory.near
if (blacklistedToken.startsWith('*') && tokenContract.endsWith(blacklistedToken.slice(1))) {
return false;
}
}

return true;
}

export function useTokenBlacklist({ tokens }) {
// TODO: make list dynamic, fetch from db
const blacklistedTokens = [
'kusama-airdrop.near',
'youwon400neartoclaimyourgainwwwlotte.laboratory.jumpfinance.near',
'*.laboratory.jumpfinance.near',
];

const allowedTokens = useMemo(() => {
if (!tokens) {
return tokens;
}

return tokens.filter((token) => !blacklistedTokens.includes(token.contractName));
return tokens.filter((token) => isTokenIncluded(token.contractName, blacklistedTokens));
}, [tokens]);

return { blacklistedTokens, allowedTokens };
Expand Down
22 changes: 22 additions & 0 deletions packages/frontend/test/token_blacklist.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const { isTokenIncluded } = require('../src/hooks/useTokenBlacklist');

describe('Fungible Token Blacklist', () => {
test('non-blacklisted tokens are included', () => {
expect(isTokenIncluded('test.near', ['not.near'])).toBe(true);
});

test('explicitly blacklisted tokens are excluded', () => {
expect(isTokenIncluded('test.near', ['test.near'])).toBe(false);
});

test('wildcard-prefixed blacklisted tokens are excluded', () => {
expect(isTokenIncluded('mal.test.near', ['*.test.near'])).toBe(false);
expect(isTokenIncluded('mal.test.near', ['xyz.near', '*.test.near'])).toBe(false);
});

test('non-matching tokens are included despite wildcard-prefixed blacklisted tokens', () => {
expect(isTokenIncluded('test.near', ['*.test.near'])).toBe(true);
expect(isTokenIncluded('xyz.near', ['*.test.near'])).toBe(true);
expect(isTokenIncluded('xyz.near', ['*.test.near', 'abc.near'])).toBe(true);
});
});