Skip to content

Commit

Permalink
feat: labels on proposals (#1065)
Browse files Browse the repository at this point in the history
* feat: labels on proposals

* remove unwanted log

* add more condtions for labels like max length and pattern

* fix update proposal

* v0.12.20
  • Loading branch information
ChaituVR authored Oct 4, 2024
1 parent 8bf3023 commit 4220cac
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snapshot-labs/snapshot.js",
"version": "0.12.19",
"version": "0.12.20",
"repository": "snapshot-labs/snapshot.js",
"license": "MIT",
"main": "dist/snapshot.cjs.js",
Expand Down
12 changes: 12 additions & 0 deletions src/schemas/proposal.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@
"turbo": 1000
}
},
"labels": {
"type": "array",
"title": "labels",
"maxItems": 10,
"uniqueItems": true,
"items": {
"type": "string",
"minLength": 1,
"maxLength": 8,
"pattern": "^[a-zA-Z0-9]+$"
}
},
"type": {
"type": "string",
"enum": [
Expand Down
12 changes: 12 additions & 0 deletions src/schemas/update-proposal.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@
"turbo": 1000
}
},
"labels": {
"type": "array",
"title": "labels",
"maxItems": 10,
"uniqueItems": true,
"items": {
"type": "string",
"minLength": 1,
"maxLength": 8,
"pattern": "^[a-zA-Z0-9]+$"
}
},
"type": {
"enum": [
"single-choice",
Expand Down
4 changes: 3 additions & 1 deletion src/sign/hashedTypes.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,7 @@
"42f8858a21d4aa232721cb97074851e729829ea362b88bb21f3879899663b586": "follow",
"2bb75450e28b06f259ea764cd669de6bde0ba70ce729b0ff05ab9df56e0ff21d": "unfollow",
"2ffbebcbd22ef48fd2f4a1182ff1feda7795b57689bd6f0dd73c89e925e7fefb": "profile",
"4288d50b713081aae77d60d596d75864bff7acf7791a00183401e58658ee9da5": "statement"
"4288d50b713081aae77d60d596d75864bff7acf7791a00183401e58658ee9da5": "statement",
"d56782e3b50ac86c25ae292923da8c367e3c9e8e7ea9d8baa435051fe2f430fa": "proposal",
"df10a7eeabe19301d6018be8b6c5d13231320d7ece64d021043fa172b64f3796": "update-proposal"
}
4 changes: 4 additions & 0 deletions src/sign/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface Proposal {
body: string;
discussion: string;
choices: string[];
labels?: string[];
start: number;
end: number;
snapshot: number;
Expand All @@ -39,6 +40,7 @@ export interface UpdateProposal {
body: string;
discussion: string;
choices: string[];
labels?: string[];
plugins: string;
}

Expand Down Expand Up @@ -143,6 +145,7 @@ export const proposalTypes = {
{ name: 'body', type: 'string' },
{ name: 'discussion', type: 'string' },
{ name: 'choices', type: 'string[]' },
{ name: 'labels', type: 'string[]' },
{ name: 'start', type: 'uint64' },
{ name: 'end', type: 'uint64' },
{ name: 'snapshot', type: 'uint64' },
Expand All @@ -162,6 +165,7 @@ export const updateProposalTypes = {
{ name: 'body', type: 'string' },
{ name: 'discussion', type: 'string' },
{ name: 'choices', type: 'string[]' },
{ name: 'labels', type: 'string[]' },
{ name: 'plugins', type: 'string' }
]
};
Expand Down
32 changes: 32 additions & 0 deletions test/e2e/sign/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, test } from 'vitest';
const { createHash } = require('crypto');
import * as types from '../../../src/sign/types';
import hashedTypes from '../../../src/sign/hashedTypes.json';
import kebabCase from 'lodash/kebabCase';

function sha256(str) {
return createHash('sha256').update(str).digest('hex');
}
describe('sign types', () => {
Object.keys(types).forEach((key) => {
test(`hashed type should contain with type ${key}`, () => {
const hash = sha256(JSON.stringify(types[key]));
expect(hash).toBeTruthy();
const derivedKey = kebabCase(
key
.replace('2Types', '')
.replace('Types', '')
.replace('Type', '')
.replace('space', 'settings')
.replace('cancel', 'delete')
);
try {
expect(hashedTypes[hash]).toBe(derivedKey);
} catch (error) {
throw new Error(
`Hash ${hash} does not match the derived key ${derivedKey}`
);
}
});
});
});
6 changes: 6 additions & 0 deletions test/examples/proposal.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
"Add 20,000 $YAM to Gen Exp Fund",
"Do not add $YAM"
],
"labels": [
"12345678",
"12a4b678",
"abcdefgh",
"a1b2c3d4"
],
"start": 1619884800,
"end": 1620316800,
"snapshot": 12345167,
Expand Down

0 comments on commit 4220cac

Please sign in to comment.