Skip to content
This repository was archived by the owner on May 11, 2026. It is now read-only.
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
84 changes: 13 additions & 71 deletions src/helpers/alias.ts

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We don't want to track the signer on vote, proposal etc? maybe in next PR?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This would require change in db but I want us to move faster on this and get this merged sooner than later. So yes, that would fit into another PR.

Original file line number Diff line number Diff line change
@@ -1,107 +1,49 @@
import { uniq } from 'lodash';
import db from './mysql';

const DEFAULT_ALIAS_EXPIRY_DAYS = 30;

// These types can always be executed with an alias
const TYPES_EXECUTABLE_BY_ALIAS = [
'follow',
'unfollow',
'subscribe',
'unsubscribe',
'profile',
'statement'
] as const;

// These types can be executed with an alias only when enabled in the space settings
const OPTIONAL_TYPES_EXECUTABLE_BY_ALIAS = [
'statement',
'vote',
'vote-array',
'vote-string',
'proposal',
'update-proposal',
'delete-proposal'
'delete-proposal',
'flag-proposal'
] as const;

// These types can be executed with a Starknet alias
const TYPES_EXECUTABLE_BY_STARKNET_ALIAS = [
'flag-proposal',
...OPTIONAL_TYPES_EXECUTABLE_BY_ALIAS
] as const;

// Memoization cache for getAllowedTypes
const allowedTypesCache = new Map<string, ExecutableType[]>();

// Types
type ExecutableType =
| (typeof TYPES_EXECUTABLE_BY_ALIAS)[number]
| (typeof OPTIONAL_TYPES_EXECUTABLE_BY_ALIAS)[number]
| (typeof TYPES_EXECUTABLE_BY_STARKNET_ALIAS)[number];

/**
* Checks if an alias relationship exists and is not expired
* @param address - The original address
* @param alias - The alias address to check
* @param expiryDays - Number of days after which alias expires (default: 30)
* @returns Promise<boolean> - True if valid alias exists
*/
export async function isExistingAlias(
address: string,
alias: string,
expiryDays = DEFAULT_ALIAS_EXPIRY_DAYS
): Promise<boolean> {
const query = `SELECT 1
FROM aliases
WHERE address = ? AND alias = ?
AND created > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL ? DAY))
LIMIT 1`;

const results = await db.queryAsync(query, [address, alias, expiryDays]);
const results = await db.queryAsync(
`SELECT 1
FROM aliases
WHERE address = ? AND alias = ?
AND created > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL ? DAY))
LIMIT 1`,
[address, alias, expiryDays]
);
return results.length > 0;
}

export async function verifyAlias(type: string, body: any, optionalAlias = false): Promise<void> {
export async function verifyAlias(type: string, body: any): Promise<void> {
const { message } = body.data;

if (body.address === message.from) return;

if (
!getAllowedTypes(optionalAlias, isStarknetAddress(message.from)).includes(
type as ExecutableType
)
) {
if (!TYPES_EXECUTABLE_BY_ALIAS.includes(type as any)) {
return Promise.reject(`alias not allowed for the type: ${type}`);
}

if (!(await isExistingAlias(message.from, body.address))) {
return Promise.reject('wrong alias');
}
}

// Loose checking here, as we're looking for this address in the database later,
// which will always be a formatted starknet address if valid.
export function isStarknetAddress(address: string): boolean {
return /^0x[0-9a-fA-F]{64}$/.test(address);
}

export function getAllowedTypes(withAlias: boolean, forStarknet: boolean): ExecutableType[] {
const cacheKey = `${withAlias}-${forStarknet}`;

if (allowedTypesCache.has(cacheKey)) {
return allowedTypesCache.get(cacheKey)!;
}

const types: ExecutableType[] = [...TYPES_EXECUTABLE_BY_ALIAS];

if (withAlias) {
types.push(...OPTIONAL_TYPES_EXECUTABLE_BY_ALIAS);
}

if (forStarknet) {
types.push(...TYPES_EXECUTABLE_BY_STARKNET_ALIAS);
}

const result = uniq(types);
allowedTypesCache.set(cacheKey, result);
return result;
}
4 changes: 1 addition & 3 deletions src/ingestor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,19 @@ export default async function ingestor(req) {
return Promise.reject('Invalid space id');
}

let aliased = false;
if (!['settings', 'alias', 'profile', 'delete-space'].includes(type)) {
if (!message.space) return Promise.reject('unknown space');

try {
const space = await getSpace(message.space, false, message.network);
if (!space) return Promise.reject('unknown space');
network = space.network;
if (space.voting?.aliased) aliased = true;
} catch (e: any) {
return Promise.reject(e.message);
}
}

await verifyAlias(type, body, aliased);
await verifyAlias(type, body);

// Check if signature is valid
try {
Expand Down
53 changes: 13 additions & 40 deletions test/unit/helpers/alias.test.ts

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we can add more test cases:

  • verifyAlias('vote', ...) with a valid alias in DB → resolves.
  • verifyAlias('proposal', ...) with a valid alias in DB → resolves.
  • verifyAlias('update-proposal', ...) with a valid alias in DB → resolves.
  • verifyAlias('delete-proposal', ...) with a valid alias in DB → resolves.
  • verifyAlias('flag-proposal', ...) with a valid EVM alias in DB → resolves (covers the Starknet → EVM promotion).
  • verifyAlias('vote', ...) when DB returns empty → rejects 'wrong alias'.
  • verifyAlias('settings', ...) with an alias pair → rejects 'alias not allowed for the type: settings' (already covered, keep).
  • verifyAlias('alias', ...) via alias signer → rejects (alias registration itself can't be aliased).
  • verifyAlias('vote', ...) where body.address === message.from → resolves without touching DB (no queryAsync call).

Original file line number Diff line number Diff line change
@@ -1,45 +1,18 @@
import { getAllowedTypes, isStarknetAddress } from '../../../src/helpers/alias';
import { verifyAlias } from '../../../src/helpers/alias';

describe('Alias', () => {
describe('isStarknetAddress()', () => {
it('should return true for a starknet address', () => {
expect(
isStarknetAddress('0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef')
).toBe(true);
});
it('should return false for a non-starknet address', () => {
expect(isStarknetAddress('0x91FD2c8d24767db4Ece7069AA27832ffaf8590f3')).toBe(false);
});
it('should return false for an invalid address', () => {
expect(isStarknetAddress('')).toBe(false);
expect(isStarknetAddress('test')).toBe(false);
});
jest.mock('../../../src/helpers/mysql', () => ({
__esModule: true,
default: { queryAsync: jest.fn() }
}));

describe('verifyAlias()', () => {
it('resolves when the sender is the creator', async () => {
const body = { address: '0xabc', data: { message: { from: '0xabc' } } };
await expect(verifyAlias('vote', body)).resolves.toBeUndefined();
});

describe('getAllowedTypes()', () => {
it('should return the correct types when both withAlias and forStarknet are false', () => {
const result = getAllowedTypes(false, false);
expect(result).toContain('profile');
expect(result).not.toContain('vote');
expect(result).not.toContain('update-proposal');
});
it('should return the correct types when withAlias is true and forStarknet is false', () => {
const result = getAllowedTypes(true, false);
expect(result).toContain('profile');
expect(result).toContain('vote');
expect(result).toContain('update-proposal');
});
it('should return the correct types when withAlias is false and forStarknet is true', () => {
const result = getAllowedTypes(false, true);
expect(result).toContain('profile');
expect(result).toContain('vote');
expect(result).toContain('update-proposal');
});
it('should return the correct types when both withAlias and forStarknet are true', () => {
const result = getAllowedTypes(true, true);
expect(result).toContain('profile');
expect(result).toContain('vote');
expect(result).toContain('update-proposal');
});
it('rejects when the type is not alias-executable', async () => {
const body = { address: '0xaaa', data: { message: { from: '0xbbb' } } };
await expect(verifyAlias('settings', body)).rejects.toMatch('alias not allowed');
});
});
Loading