This repository was archived by the owner on May 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
refactor: always allow alias for all spaces #634
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can add more test cases:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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,proposaletc? maybe in next PR?There was a problem hiding this comment.
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.