Skip to content

Commit

Permalink
fix(#1894): fix metadata hash generation
Browse files Browse the repository at this point in the history
  • Loading branch information
MSzalowski committed Sep 3, 2024
1 parent d7e9fa4 commit 3b53f3d
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,15 @@ export const DRepDashboardCard = ({
title: t("dashboard.cards.drep.dRepRegistration"),
}),
...(pendingTransaction.retireAsDrep && {
description: (
description: voter?.givenName ? (
<Trans
i18nKey="dashboard.cards.drep.retirementInProgressWithGivenName"
values={{
deposit: correctAdaFormat(voter?.deposit),
name: voter?.givenName,
}}
/>
) : (
<Trans
i18nKey="dashboard.cards.drep.retirementInProgress"
values={{ deposit: correctAdaFormat(voter?.deposit) }}
Expand Down
2 changes: 1 addition & 1 deletion govtool/frontend/src/context/governanceAction.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe("GovernanceActionProvider", () => {
const hash = await createHash(jsonld!);
expect(hash).toBeDefined();
expect(hash).toBe(
"bbdbbe163d1b8e4d6c10180df515cc7d58109412d90a42e898fc66850b3fc98c",
"72b37e2f5e64e7de57b85558ba00885c848f700fbb37fbed3197e603873fa976",
);
};
test();
Expand Down
6 changes: 5 additions & 1 deletion govtool/frontend/src/context/governanceAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ const GovernanceActionProvider = ({ children }: PropsWithChildren) => {
*/
const createHash = useCallback(async (jsonLD: NodeObject) => {
try {
const jsonHash = blake2bHex(JSON.stringify(jsonLD), undefined, 32);
const jsonHash = blake2bHex(
JSON.stringify(jsonLD, null, 2),
undefined,
32,
);
return jsonHash;
} catch (error) {
Sentry.captureException(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export const useCreateGovernanceActionForm = (

const jsonld = await generateJsonld(body, GOVERNANCE_ACTION_CONTEXT);

const jsonHash = blake2bHex(JSON.stringify(jsonld), undefined, 32);
const jsonHash = blake2bHex(JSON.stringify(jsonld, null, 2), undefined, 32);

// That allows to validate metadata hash
setHash(jsonHash);
Expand Down
2 changes: 1 addition & 1 deletion govtool/frontend/src/hooks/forms/useEditDRepInfoForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const useEditDRepInfoForm = (

const jsonld = await generateJsonld(body, DREP_CONTEXT, CIP_119);

const jsonHash = blake2bHex(JSON.stringify(jsonld), undefined, 32);
const jsonHash = blake2bHex(JSON.stringify(jsonld, null, 2), undefined, 32);

setHash(jsonHash);
setJson(jsonld);
Expand Down
3 changes: 1 addition & 2 deletions govtool/frontend/src/hooks/forms/useRegisterAsdRepForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,9 @@ export const useRegisterAsdRepForm = (
],
standardReference: CIP_119,
});

const jsonld = await generateJsonld(body, DREP_CONTEXT, CIP_119);

const jsonHash = blake2bHex(JSON.stringify(jsonld), undefined, 32);
const jsonHash = blake2bHex(JSON.stringify(jsonld, null, 2), undefined, 32);

setHash(jsonHash);
setJson(jsonld);
Expand Down
2 changes: 1 addition & 1 deletion govtool/frontend/src/hooks/forms/useVoteContextForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const useVoteContextForm = (
});
const jsonld = await generateJsonld(body, CIP_100_CONTEXT, CIP_100);

const jsonHash = blake2bHex(JSON.stringify(jsonld), undefined, 32);
const jsonHash = blake2bHex(JSON.stringify(jsonld, null, 2), undefined, 32);

// That allows to validate metadata hash
setHash(jsonHash);
Expand Down
12 changes: 7 additions & 5 deletions govtool/frontend/src/i18n/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ export const en = {
reRegister: "Re-register as a DRep",
retire: "Retire as a DRep",
retirementInProgress:
"You are being retired as <strong>MrDRep</strong>. You will receive a refund of <strong>{{deposit}} ADA</strong> when the transaction completes.",
"You are being retired. You will receive a refund of <strong>{{deposit}} ADA</strong> when the transaction completes.",
retirementInProgressWithGivenName:
"You are being retired as <strong>{{givenName}}</strong>. You will receive a refund of <strong>{{deposit}} ADA</strong> when the transaction completes.",
viewDetails: "View your DRep details",
youAreRegistered: "You are Registered as a DRep",
yourDRepId: "Your DRep ID",
Expand Down Expand Up @@ -361,7 +363,8 @@ export const en = {
},
dRepData: {
givenName: "DRep Name",
givenNameHelpfulText: "This is the name that will be shown on your DRep profile",
givenNameHelpfulText:
"This is the name that will be shown on your DRep profile",
objectives: "Objectives",
objectivesHelpfulText:
"What you believe and what you want to achieve as a DRep.",
Expand Down Expand Up @@ -391,10 +394,9 @@ export const en = {
},
references: "References",
referenceDescription: "Description",
referenceDescriptionHelpfulText:
"Limit: 80 characters",
referenceDescriptionHelpfulText: "Limit: 80 characters",
referenceURL: "URL",
},
},
errors: {
tooLongUrl: "Url must be less than 128 bytes",
mustBeStakeAddress: "It must be reward address in bech32 format",
Expand Down
7 changes: 6 additions & 1 deletion govtool/metadata-validation/src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ export class AppService {
),
);

const parsedData = JSON.parse(rawData);
let parsedData;
try {
parsedData = JSON.parse(rawData);
} catch (error) {
throw MetadataValidationStatus.INCORRECT_FORMAT;
}

if (!parsedData?.body) {
throw MetadataValidationStatus.INCORRECT_FORMAT;
Expand Down
24 changes: 6 additions & 18 deletions govtool/metadata-validation/src/utils/validateCIP108body.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
import * as Joi from 'joi';

import { MetadataValidationStatus } from '@/enums';

import { getFieldValue } from './getFieldValue';

const CIP108FieldRules = {
title: Joi.string().allow('').max(80),
abstract: Joi.string().allow('').max(2500),
motivation: Joi.string().allow(''),
rationale: Joi.string().allow(''),
};

/**
* Validates the body of a CIP108 standard.
*
Expand All @@ -23,15 +14,12 @@ export const validateCIP108body = (body: Record<string, unknown>) => {
const abstract = getFieldValue(body, 'abstract');
const motivation = getFieldValue(body, 'motivation');
const rationale = getFieldValue(body, 'rationale');

try {
CIP108FieldRules.title.validate(title);
CIP108FieldRules.abstract.validate(abstract);
CIP108FieldRules.motivation.validate(motivation);
CIP108FieldRules.rationale.validate(rationale);

return true;
} catch (error) {
if (!title || !abstract || !motivation || !rationale) {
throw MetadataValidationStatus.INCORRECT_FORMAT;
}
if (String(title).length > 80 || String(abstract).length > 2500) {
throw MetadataValidationStatus.INCORRECT_FORMAT;
}

return true;
};

0 comments on commit 3b53f3d

Please sign in to comment.