Skip to content

Commit

Permalink
Merge pull request #2596 from IntersectMBO/chore/update-metadata-format
Browse files Browse the repository at this point in the history
feat: enhance dRep metadata handling with image and language support
  • Loading branch information
kneerose authored Jan 2, 2025
2 parents 81509e7 + 91a2e3d commit fe20807
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
12 changes: 10 additions & 2 deletions tests/govtool-frontend/playwright/lib/_mock/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { faker } from "@faker-js/faker";
import { generateExactLengthText } from "@helpers/string";
import { imageObject } from "@types";

export const invalid = {
url: (isSupportedGreaterThan128Words = true) => {
Expand Down Expand Up @@ -139,8 +140,9 @@ export const valid = {
return `ipfs://${randomCID}`;
},

metadata: (paymentAddress: string) => ({
metadata: (paymentAddress: string, imageObject: imageObject) => ({
"@context": {
"@language": "en-us",
CIP100:
"https://github.com/cardano-foundation/CIPs/blob/master/CIP-0100/README.md#",
CIP119:
Expand Down Expand Up @@ -170,7 +172,12 @@ export const valid = {
},
paymentAddress: "CIP119:paymentAddress",
givenName: "CIP119:givenName",
image: "CIP119:image",
image: {
"@id": "CIP119:image",
"@context": {
ImageObject: "https://schema.org/ImageObject",
},
},
objectives: "CIP119:objectives",
motivations: "CIP119:motivations",
qualifications: "CIP119:qualifications",
Expand All @@ -197,6 +204,7 @@ export const valid = {
hashAlgorithm: "blake2b-256",
body: {
givenName: faker.person.firstName(),
image: imageObject,
motivations: faker.lorem.paragraph(2),
objectives: faker.lorem.paragraph(2),
paymentAddress: paymentAddress,
Expand Down
23 changes: 23 additions & 0 deletions tests/govtool-frontend/playwright/lib/helpers/dRep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,26 @@ export async function fetchFirstActiveDRepDetails(page: Page) {
await dRepDirectoryPage.searchInput.click();
return { dRepGivenName, dRepId, dRepDirectoryPage };
}

export async function calculateImageSHA256(imageUrl: string) {
const toHex = (buffer: ArrayBuffer) => {
return Array.from(new Uint8Array(buffer))
.map((byte) => byte.toString(16).padStart(2, "0"))
.join("");
};
try {
if (imageUrl == "") {
return "";
}
const response = await fetch(imageUrl);
if (!response.ok) {
throw new Error(`Failed to fetch image: ${response.statusText}`);
}
const arrayBuffer = await response.arrayBuffer();
const hashBuffer = await crypto.subtle.digest("SHA-256", arrayBuffer);
return toHex(hashBuffer);
} catch (error) {
console.error("Error calculating SHA256:", error);
return null;
}
}
15 changes: 14 additions & 1 deletion tests/govtool-frontend/playwright/lib/helpers/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const blake = require("blakejs");

import * as fs from "fs";
import { ShelleyWallet } from "./crypto";
import { calculateImageSHA256 } from "./dRep";
import { imageObject } from "@types";

export async function downloadMetadata(download: Download): Promise<{
name: string;
Expand All @@ -21,7 +23,18 @@ export async function downloadMetadata(download: Download): Promise<{
async function calculateMetadataHash() {
try {
const paymentAddress = (await ShelleyWallet.generate()).addressBech32(0);
const data = JSON.stringify(mockValid.metadata(paymentAddress), null, 2);
const imageUrl = faker.image.avatarGitHub();
const imageSHA256 = (await calculateImageSHA256(imageUrl)) || "";
const imageObject: imageObject = {
"@type": "ImageObject",
contentUrl: imageUrl,
sha256: imageSHA256,
};
const data = JSON.stringify(
mockValid.metadata(paymentAddress, imageObject),
null,
2
);

const buffer = Buffer.from(data, "utf8");
const hexDigest = blake.blake2bHex(buffer, null, 32);
Expand Down
6 changes: 6 additions & 0 deletions tests/govtool-frontend/playwright/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,9 @@ export type EpochParams = {
pvtpp_security_group: number | null;
treasury_growth_rate: number | null;
};

export interface imageObject {
"@type": "ImageObject";
contentUrl: string;
sha256: string;
}

0 comments on commit fe20807

Please sign in to comment.