Skip to content

Commit

Permalink
fix(#1582): Add support for CIP-100 on vote metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
MSzalowski committed Jul 29, 2024
1 parent fc3e5c3 commit f57b263
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ changes.

- Add PDF_API_URL to the frontend config [Issue 1575](https://github.com/IntersectMBO/govtool/issues/1575)
- Provide DB-Sync Postgres envs to the backend config
- Add support for CIP-100 for context to Governance Action Vote [Issue 1582](https://github.com/IntersectMBO/govtool/issues/1582)

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const VoteContextStoringInformation = ({
validateURL,
watch,
generateMetadata,
onClickDownloadFile,
onClickDownloadJson,
} = useVoteContextForm(setSavedHash, setStep, setErrorMessage);

const openGuideAboutStoringInformation = () =>
Expand Down Expand Up @@ -78,7 +78,7 @@ export const VoteContextStoringInformation = ({
<Step
component={
<Button
onClick={onClickDownloadFile}
onClick={onClickDownloadJson}
size="extraLarge"
startIcon={<img alt="download" src={ICONS.download} />}
sx={{
Expand Down
51 changes: 51 additions & 0 deletions govtool/frontend/src/consts/CIP100Context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
export const CIP_100_CONTEXT = {
"@language": "en-us",
CIP100:
"https://github.com/cardano-foundation/CIPs/blob/master/CIP-0100/README.md#",
hashAlgorithm: "CIP100:hashAlgorithm",
body: {
"@id": "CIP100:body",
"@context": {
references: {
"@id": "CIP100:references",
"@container": "@set" as const,
"@context": {
GovernanceMetadata: "CIP100:GovernanceMetadataReference",
Other: "CIP100:OtherReference",
label: "CIP100:reference-label",
uri: "CIP100:reference-uri",
referenceHash: {
"@id": "CIP100:referenceHash",
"@context": {
hashDigest: "CIP100:hashDigest",
hashAlgorithm: "CIP100:hashAlgorithm",
},
},
},
},
comment: "CIP100:comment",
externalUpdates: {
"@id": "CIP100:externalUpdates",
"@context": {
title: "CIP100:update-title",
uri: "CIP100:uri",
},
},
},
},
authors: {
"@id": "CIP100:authors",
"@container": "@set" as const,
"@context": {
name: "http://xmlns.com/foaf/0.1/name",
witness: {
"@id": "CIP100:witness",
"@context": {
witnessAlgorithm: "CIP100:witnessAlgorithm",
publicKey: "CIP100:publicKey",
signature: "CIP100:signature",
},
},
},
},
};
3 changes: 2 additions & 1 deletion govtool/frontend/src/consts/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export * from "./externalDataModalConfig";
export * from "./CIP100Context";
export * from "./colors";
export * from "./dRepActions";
export * from "./dRepDirectory";
export * from "./externalDataModalConfig";
export * from "./governanceAction";
export * from "./icons";
export * from "./images";
Expand Down
35 changes: 26 additions & 9 deletions govtool/frontend/src/hooks/forms/useVoteContextForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@ import { useFormContext } from "react-hook-form";
import { blake2bHex } from "blakejs";
import * as Sentry from "@sentry/react";

import { downloadTextFile } from "@utils";
import {
canonizeJSON,
downloadJson,
generateJsonld,
generateMetadataBody,
} from "@utils";
import { MetadataValidationStatus } from "@models";
import { NodeObject } from "jsonld";

import { useValidateMutation } from "../mutations";
import { CIP_100, CIP_100_CONTEXT } from "@/consts";

export type VoteContextFormValues = {
voteContextText: string;
Expand All @@ -21,6 +28,7 @@ export const useVoteContextForm = (
) => {
const { validateMetadata } = useValidateMutation();
const [hash, setHash] = useState<string | null>(null);
const [json, setJson] = useState<NodeObject | null>(null);

const {
control,
Expand All @@ -35,19 +43,28 @@ export const useVoteContextForm = (

const generateMetadata = useCallback(async () => {
const { voteContextText } = getValues();

const canonizedJsonHash = blake2bHex(voteContextText, undefined, 32);
const body = generateMetadataBody({
data: {
comment: voteContextText,
},
acceptedKeys: ["comment"],
standardReference: CIP_100,
});
const jsonld = await generateJsonld(body, CIP_100_CONTEXT, CIP_100);

const canonizedJson = await canonizeJSON(jsonld);
const canonizedJsonHash = blake2bHex(canonizedJson, undefined, 32);

// That allows to validate metadata hash
setHash(canonizedJsonHash);
setJson(jsonld);

return voteContextText;
return jsonld;
}, [getValues]);

const onClickDownloadFile = useCallback(() => {
const { voteContextText } = getValues();
if (!voteContextText) return;
downloadTextFile(voteContextText, "Vote_Context");
const onClickDownloadJson = useCallback(() => {
if (!json) return;
downloadJson(json, "Vote_Context");
}, [getValues]);

const validateHash = useCallback(
Expand Down Expand Up @@ -102,7 +119,7 @@ export const useVoteContextForm = (
generateMetadata,
getValues,
isValid,
onClickDownloadFile,
onClickDownloadJson,
register,
reset,
setValue,
Expand Down
2 changes: 1 addition & 1 deletion govtool/frontend/src/i18n/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ export const en = {
viewOtherDetails: "View other details",
viewProposalDetails: "View proposal details",
vote: "Vote",
voteContextFileName: "Vote_Context.txt",
voteContextFileName: "Vote_Context.jsonld",
votedOnByMe: "Voted on by me",
voteOnGovActions: "Vote on Governance Action",
voteSubmitted: "Vote submitted",
Expand Down

0 comments on commit f57b263

Please sign in to comment.