diff --git a/tests/govtool-frontend/playwright/lib/_mock/index.ts b/tests/govtool-frontend/playwright/lib/_mock/index.ts index 23c200604..3aff590cf 100644 --- a/tests/govtool-frontend/playwright/lib/_mock/index.ts +++ b/tests/govtool-frontend/playwright/lib/_mock/index.ts @@ -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) => { @@ -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: @@ -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", @@ -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, diff --git a/tests/govtool-frontend/playwright/lib/helpers/dRep.ts b/tests/govtool-frontend/playwright/lib/helpers/dRep.ts index 33b6b1823..eab856f71 100644 --- a/tests/govtool-frontend/playwright/lib/helpers/dRep.ts +++ b/tests/govtool-frontend/playwright/lib/helpers/dRep.ts @@ -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; + } +} diff --git a/tests/govtool-frontend/playwright/lib/helpers/metadata.ts b/tests/govtool-frontend/playwright/lib/helpers/metadata.ts index 567b147f1..08eb1ee7d 100644 --- a/tests/govtool-frontend/playwright/lib/helpers/metadata.ts +++ b/tests/govtool-frontend/playwright/lib/helpers/metadata.ts @@ -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; @@ -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); diff --git a/tests/govtool-frontend/playwright/lib/types.ts b/tests/govtool-frontend/playwright/lib/types.ts index 5c6853d2a..ed1c6b8bf 100644 --- a/tests/govtool-frontend/playwright/lib/types.ts +++ b/tests/govtool-frontend/playwright/lib/types.ts @@ -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; +}