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: Require admin to transfer/assign reserved usernames #117

Merged
merged 1 commit into from
Oct 11, 2023
Merged
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
110 changes: 110 additions & 0 deletions src/reserved.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Usernames that we want only admins to be able to assign to an FID, in order
// to prevent abuse and obvious username squatting.
const RESERVED_USERNAMES = new Set([
'adidas',
'amazon',
'apple',
'arbitrum',
'barackobama',
'bbc',
'billgates',
'binance',
'bitcoin',
'blockchain',
'bmw',
'cia',
'cloud',
'cnn',
'coinbase',
'consensys',
'context',
'crypto',
'data',
'dell',
'disney',
'drake',
'elonmusk',
'ethereum',
'f1',
'facebook',
'fashion',
'fbi',
'fifa',
'finance',
'fitness',
'food',
'fwb',
'fwbdao',
'gallery',
'games',
'ge',
'gem',
'gm',
'gnosis',
'gnosissafe',
'google',
'health',
'hp',
'hyperverse',
'ibm',
'intel',
'invest',
'jpmorgan',
'kraken',
'kucoin',
'lebronjames',
'lionelmessi',
'luxury',
'messi',
'metamask',
'microsoft',
'midjourney',
'mirror',
'ml',
'money',
'nasa',
'nba',
'netflix',
'news',
'nfl',
'nike',
'nounsdao',
'obsidian',
'openai',
'opensea',
'optimism',
'oracle',
'ourzora',
'pga',
'poap',
'polygon',
'privacy',
'rainbow',
'rarible',
'ronaldo',
'samsung',
'sony',
'starknet',
'stripe',
'sudoswap',
'superrare',
'sushiswap',
'syndicate',
'tesla',
'tezos',
'travel',
'twitter',
'uber',
'un',
'uniswap',
'urbit',
'viamirror',
'vitalik',
'youtube',
'zora',
'zoraco',
]);

export function reservedUsername(username: string) {
return RESERVED_USERNAMES.has(username);
}
7 changes: 6 additions & 1 deletion src/transfers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Kysely, Selectable } from 'kysely';
import { Database, TransfersTable } from './db.js';
import { ADMIN_KEYS, generateSignature, signer, verifySignature } from './signature.js';
import { reservedUsername } from './reserved.js';
import { bytesToHex, currentTimestamp, hexToBytes } from './util.js';
import { bytesCompare, validations } from '@farcaster/hub-nodejs';
import { log } from './log.js';
Expand Down Expand Up @@ -30,6 +31,7 @@ export type TransferHistoryFilter = {

type ErrorCode =
| 'USERNAME_TAKEN'
| 'USERNAME_RESERVED'
| 'TOO_MANY_NAMES'
| 'UNAUTHORIZED'
| 'USERNAME_NOT_FOUND'
Expand Down Expand Up @@ -100,10 +102,13 @@ async function getAndValidateVerifierAddress(req: TransferRequest, idContract: I
export async function validateTransfer(req: TransferRequest, db: Kysely<Database>, idContract: IdRegistry) {
const verifierAddress = await getAndValidateVerifierAddress(req, idContract);
if (!verifierAddress) {
// Only admin transfers are allowed until we finish migrating
throw new ValidationError('UNAUTHORIZED');
}

if (reservedUsername(req.username) && !ADMIN_KEYS[req.userFid]) {
throw new ValidationError('USERNAME_RESERVED');
}

if (!verifySignature(req.username, req.timestamp, req.owner, req.userSignature, verifierAddress)) {
log.error(`Invalid signature for req ${JSON.stringify(req)}`);
throw new ValidationError('INVALID_SIGNATURE');
Expand Down
20 changes: 20 additions & 0 deletions tests/transfers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@ describe('transfers', () => {
await expect(createTestTransfer(db, { username: 'test3', to: 4 })).rejects.toThrow('USERNAME_TAKEN');
});

test('cannot register a reserved name with a non-admin account', async () => {
await expect(
createTestTransfer(
db,
{
username: 'apple',
owner: '0xd469E0504c20185941E73029C6A400bD2dD28A1A',
from: 0,
to: 123,
userFid: 123,
},
123
)
).rejects.toThrow('USERNAME_RESERVED');
});

test('can register a reserved name with an admin account', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

It was difficult to test the other case because we actually check the address against the live contract, and it seemed wrong to hard code values tied to a specific FID that might change in future (e.g. if the FID was transferred).

Copy link
Contributor

Choose a reason for hiding this comment

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

You mean that a normal user cannot register a reserved name? We could pick bitcoin highly unlikely anyone has a legitimate claim to that.

Copy link
Contributor

Choose a reason for hiding this comment

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

If satoshi does show up, fixing one failing test is going to be a relatively minor issue :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh, I see what you mean. If you pass in idOfOwner to createTestTransfer it mocks the contract call.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, nice. I've added a test.

expect(await createTestTransfer(db, { username: 'apple', to: 123 }));
});

test('same fid cannot register twice', async () => {
await expect(createTestTransfer(db, { username: 'test1234', to: 2 })).rejects.toThrow('TOO_MANY_NAMES');
});
Expand Down