Skip to content
This repository was archived by the owner on May 11, 2026. It is now read-only.

Commit a0704ae

Browse files
committed
fix: allow alias renewal by re-submitting existing pair
Allow the same (address, alias) pair to be submitted again to refresh the expiration. Uses ON DUPLICATE KEY UPDATE to reset created timestamp.
1 parent 8c2caaa commit a0704ae

3 files changed

Lines changed: 57 additions & 13 deletions

File tree

src/writer/alias.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ export async function verify(message): Promise<any> {
1010
log.warn(`[writer] Wrong alias format ${JSON.stringify(schemaIsValid)}`);
1111
return Promise.reject('wrong alias format');
1212
}
13-
if (message.from === msg.payload.alias) {
13+
if (message.address === msg.payload.alias) {
1414
return Promise.reject('alias cannot be the same as the address');
1515
}
16+
1617
return true;
1718
}
1819

@@ -25,5 +26,8 @@ export async function action(message, ipfs, receipt, id): Promise<void> {
2526
alias: msg.payload.alias,
2627
created: msg.timestamp
2728
};
28-
await db.queryAsync('INSERT INTO aliases SET ?', params);
29+
await db.queryAsync(
30+
'INSERT INTO aliases SET ? ON DUPLICATE KEY UPDATE id = VALUES(id), ipfs = VALUES(ipfs), created = VALUES(created)',
31+
params
32+
);
2933
}

test/integration/helpers/alias.test.ts

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,72 @@
11
import { isExistingAlias } from '../../../src/helpers/alias';
22
import db, { sequencerDB } from '../../../src/helpers/mysql';
3+
import { action, verify } from '../../../src/writer/alias';
34
import { aliasesSqlFixtures } from '../../fixtures/alias';
45

56
describe('alias', () => {
67
const seed = Date.now().toFixed(0);
78

8-
beforeAll(async () => {
9-
await db.queryAsync('DELETE from snapshot_sequencer_test.aliases');
9+
beforeEach(async () => {
10+
await db.queryAsync('DELETE from snapshot_sequencer_test.aliases WHERE ipfs = ?', seed);
1011
await Promise.all(
1112
aliasesSqlFixtures.map(alias => {
12-
const values = {
13-
...alias,
14-
ipfs: seed
15-
};
13+
const values = { ...alias, ipfs: seed };
1614
return db.queryAsync('INSERT INTO snapshot_sequencer_test.aliases SET ?', values);
1715
})
1816
);
1917
});
2018

21-
afterEach(async () => {
22-
await db.queryAsync('DELETE from snapshot_sequencer_test.aliases where ipfs = ?', seed);
23-
});
24-
2519
afterAll(async () => {
20+
await db.queryAsync('DELETE from snapshot_sequencer_test.aliases WHERE ipfs = ?', seed);
2621
await db.endAsync();
2722
await sequencerDB.endAsync();
2823
});
2924

25+
describe('verify()', () => {
26+
it('should pass when alias pair already exists (allows renewal)', async () => {
27+
const { address, alias } = aliasesSqlFixtures[0];
28+
const msg = {
29+
version: '0.1.4',
30+
timestamp: Math.floor(Date.now() / 1000),
31+
type: 'alias',
32+
payload: { alias }
33+
};
34+
35+
await expect(verify({ address, msg: JSON.stringify(msg) })).resolves.toBeTruthy();
36+
});
37+
38+
it('should bump created date when re-submitting existing alias', async () => {
39+
const { address, alias } = aliasesSqlFixtures[0];
40+
const newTimestamp = Math.floor(Date.now() / 1000) + 1000;
41+
const msg = {
42+
version: '0.1.4',
43+
timestamp: newTimestamp,
44+
type: 'alias',
45+
payload: { alias }
46+
};
47+
48+
await action({ address, msg: JSON.stringify(msg) }, 'ipfs-new', '', 'new-id');
49+
50+
const [row] = await db.queryAsync(
51+
'SELECT created FROM aliases WHERE address = ? AND alias = ?',
52+
[address, alias]
53+
);
54+
expect(row.created).toBe(newTimestamp);
55+
});
56+
57+
it('should pass when alias does not exist', async () => {
58+
const address = '0x0000000000000000000000000000000000000001';
59+
const msg = {
60+
version: '0.1.4',
61+
timestamp: Math.floor(Date.now() / 1000),
62+
type: 'alias',
63+
payload: { alias: '0x0000000000000000000000000000000000000002' }
64+
};
65+
66+
await expect(verify({ address, msg: JSON.stringify(msg) })).resolves.toBeTruthy();
67+
});
68+
});
69+
3070
describe('isExistingAlias()', () => {
3171
it('should return true for valid alias', () => {
3272
expect(

test/unit/writer/alias.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import omit from 'lodash/omit';
22
import * as writer from '../../../src/writer/alias';
33
import input from '../../fixtures/writer-payload/alias.json';
44

5-
describe('writer/profile', () => {
5+
describe('writer/alias', () => {
66
describe('verify()', () => {
77
const msg = JSON.parse(input.msg);
88
const invalidMsg = [

0 commit comments

Comments
 (0)