Skip to content

Commit

Permalink
fix: delegate schema validation to settings
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Dec 4, 2023
1 parent c4c7289 commit 219020b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 30 deletions.
12 changes: 0 additions & 12 deletions src/writer/proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,6 @@ async function validateSpace(originalSpace: any) {
delete space.verified;
delete space.id;

const schemaIsValid: any = snapshot.utils.validateSchema(snapshot.schemas.space, space, {
snapshotEnv: SNAPSHOT_ENV
});

if (schemaIsValid !== true) {
const firstErrorObject: any = Object.values(schemaIsValid)[0];
if (firstErrorObject.message === 'network not allowed') {
return Promise.reject(firstErrorObject.message);
}
return Promise.reject('wrong space format');
}

try {
await validateSpaceSettings(space);
} catch (e) {
Expand Down
36 changes: 18 additions & 18 deletions src/writer/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ const SNAPSHOT_ENV = process.env.NETWORK || 'testnet';
const broviderUrl = process.env.BROVIDER_URL || 'https://rpc.snapshot.org';

export async function validateSpaceSettings(space: any) {
const schemaIsValid: any = snapshot.utils.validateSchema(snapshot.schemas.space, space, {
snapshotEnv: SNAPSHOT_ENV
});

if (schemaIsValid !== true) {
log.warn('[writer] Wrong space format', schemaIsValid);
const firstErrorObject: any = Object.values(schemaIsValid)[0];
if (firstErrorObject.message === 'network not allowed') {
return Promise.reject(firstErrorObject.message);
}
return Promise.reject('wrong space format');
}

if (SNAPSHOT_ENV !== 'testnet') {
const hasTicket = space.strategies.some(strategy => strategy.name === 'ticket');
const hasVotingValidation =
Expand All @@ -31,24 +44,6 @@ export async function validateSpaceSettings(space: any) {

export async function verify(body): Promise<any> {
const msg = jsonParse(body.msg);

const schemaIsValid: any = snapshot.utils.validateSchema(snapshot.schemas.space, msg.payload, {
snapshotEnv: SNAPSHOT_ENV
});

if (schemaIsValid !== true) {
log.warn('[writer] Wrong space format', schemaIsValid);
const firstErrorObject: any = Object.values(schemaIsValid)[0];
if (firstErrorObject.message === 'network not allowed') {
return Promise.reject(firstErrorObject.message);
}
return Promise.reject('wrong space format');
}

const controller = await snapshot.utils.getSpaceController(msg.space, DEFAULT_NETWORK, {
broviderUrl
});
const isController = controller === body.address;
const space = await getSpace(msg.space, true);

try {
Expand All @@ -57,6 +52,11 @@ export async function verify(body): Promise<any> {
return Promise.reject(e);
}

const controller = await snapshot.utils.getSpaceController(msg.space, DEFAULT_NETWORK, {
broviderUrl
});
const isController = controller === body.address;

if (space?.deleted) return Promise.reject('space deleted, contact admin');
const admins = (space?.admins || []).map(admin => admin.toLowerCase());
const isAdmin = admins.includes(body.address.toLowerCase());
Expand Down

0 comments on commit 219020b

Please sign in to comment.