diff --git a/package.json b/package.json index e55fa08..38e3b5d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@thebadge/sdk", - "version": "0.2.15", + "version": "0.2.16", "license": "MIT", "main": "dist/index.js", "module": "dist/index.es.js", diff --git a/src/business-logic/kleros/types.ts b/src/business-logic/kleros/types.ts index 923730c..186b63e 100644 --- a/src/business-logic/kleros/types.ts +++ b/src/business-logic/kleros/types.ts @@ -26,6 +26,47 @@ export enum KLEROS_LIST_TYPES { LONG_TEXT = 'long text', } +export type BadgeModelKlerosMetadata = { + ipfsHash: string + ipfsUrl: string + s3Url: string + extension: string + content: { + name: string + title: string + description: string + rulingOptions: { titles: Record; descriptions: Record } + category: string + question: string + fileURI: { + ipfsHash: string + ipfsUrl: string + s3Url: string + extension: string + } + fileTypeExtension: string + evidenceDisplayInterfaceURI: string + dynamicScriptURI: { + ipfsHash: string + ipfsUrl: string + s3Url: string + extension: string + } + dynamicScriptRequiredParams: string[] + metadata: { + tcrTitle: string + tcrDescription: string + columns: MetadataColumn[] + itemName: string + itemNamePlural: string + logoURI: string + requireRemovalEvidence: boolean + isTCRofTCRs: boolean + relTcrDisabled: boolean + } + } +} + export type MetadataColumn = { label: string description: string diff --git a/src/services/badgeModels/badgeModels.spec.ts b/src/services/badgeModels/badgeModels.spec.ts index fbd8a75..b13a255 100644 --- a/src/services/badgeModels/badgeModels.spec.ts +++ b/src/services/badgeModels/badgeModels.spec.ts @@ -40,4 +40,18 @@ describe('BadgeModelsService', () => { expect('get' in badgeModelsService).toBeTruthy() expect('notExistingMethod' in badgeModelsService).toBeFalsy() }) + it('should return the evidences list for tpBadgeModel', async () => { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + const badgeModelsService = new BadgeModelsService(11155111, { + rpcProviderConfig: { name: RPCProvider.infura, apiKey: '' }, + }) + + const metadata = await badgeModelsService.getEvidenceRequirementsOfBadgeModel(2) + + expect(metadata.length).toBe(1) + expect(badgeModelsService).not.toBeUndefined() + expect('get' in badgeModelsService).toBeTruthy() + expect('notExistingMethod' in badgeModelsService).toBeFalsy() + }) }) diff --git a/src/services/badgeModels/badgeModels.ts b/src/services/badgeModels/badgeModels.ts index af41166..57ae566 100644 --- a/src/services/badgeModels/badgeModels.ts +++ b/src/services/badgeModels/badgeModels.ts @@ -20,7 +20,7 @@ import { BadgeModelThirdPartyMetaDataByIdQuery as BadgeModelThirdPartyMetaDataByIdQuery_PROD, } from '@subgraph/prod/generated/subgraph' import { TheBadgeSDKConfig } from '../../config' -import { MetadataColumn, ThirdPartyMetadataColumn } from '@businessLogic/kleros/types' +import { BadgeModelKlerosMetadata, MetadataColumn, ThirdPartyMetadataColumn } from '@businessLogic/kleros/types' import { getFromIPFS } from '@utils/ipfs' type BadgeModel_Filter = BadgeModel_Filter_DEV | BadgeModel_Filter_STAGING | BadgeModel_Filter_PROD @@ -90,55 +90,72 @@ export class BadgeModelsService extends TheBadgeSDKConfig implements BadgeModels } } + private async getKlerosEvidenceRequirementsOfBadgeModel( + evidencesListIpfsHash?: string, + ): Promise> { + if (!evidencesListIpfsHash) { + throw new Error('TheBadge SDK: Missing registrationUri for the given badge model id, provide a valid model id.') + } + + // obtain evidences required + const { result, error } = await getFromIPFS(evidencesListIpfsHash, this.env) + + const evidencesList = (result as BadgeModelKlerosMetadata)?.content?.metadata.columns + + if (error || !evidencesList) { + throw new Error('TheBadge SDK: Error obtaining required evidences list from IPFS, please retry.') + } + + return evidencesList + } + + private async getThirdPartyEvidenceRequirementsOfBadgeModel( + evidencesListIpfsHash?: string, + ): Promise> { + if (!evidencesListIpfsHash) { + throw new Error('TheBadge SDK: Missing registrationUri for the given badge model id, provide a valid model id.') + } + + // obtain evidences required + const { result, error } = await getFromIPFS<{ + requirementsColumns: ThirdPartyMetadataColumn[] + }>(evidencesListIpfsHash, this.env) + + const evidencesList = result?.content?.requirementsColumns + + if (error || !evidencesList) { + throw new Error('TheBadge SDK: Error obtaining required evidences list from IPFS, please retry.') + } + + return evidencesList + } + /** * Get evidence requirements of a badge model giving its id * * @param badgeModelId * @returns Array */ - public async getEvidenceRequirementsOfBadgeModel( - badgeModelId: string, - ): Promise> { + public async getEvidenceRequirementsOfBadgeModel(badgeModelId: string): Promise> { // take ipfs uri from metadata of the badge model + let evidencesList + const badgeModelMetadataResponse = await this.getMetadataOfBadgeModel(badgeModelId) + if ('badgeModelThirdPartyMetaData' in badgeModelMetadataResponse) { + evidencesList = await this.getThirdPartyEvidenceRequirementsOfBadgeModel( + badgeModelMetadataResponse?.badgeModelThirdPartyMetaData?.requirementsIPFSHash, + ) + } if ('badgeModelKlerosMetaData' in badgeModelMetadataResponse) { - const ipfsDataUri = badgeModelMetadataResponse?.badgeModelKlerosMetaData?.registrationUri - if (!ipfsDataUri) { - throw new Error('TheBadge SDK: Missing registrationUri for the given badge model id, provide a valid model id.') - } - - // obtain evidences required - const { result, error } = await getFromIPFS<{ columns: MetadataColumn[] }>(ipfsDataUri, this.env) - const evidencesList = result?.content?.columns - if (error || !evidencesList) { - throw new Error('TheBadge SDK: Error obtaining required evidences list from IPFS, please retry.') - } - - // return the list of evidences required - return evidencesList + evidencesList = await this.getKlerosEvidenceRequirementsOfBadgeModel( + badgeModelMetadataResponse?.badgeModelKlerosMetaData?.registrationUri, + ) } - if ('badgeModelThirdPartyMetaData' in badgeModelMetadataResponse) { - const ipfsDataUri = badgeModelMetadataResponse?.badgeModelThirdPartyMetaData?.requirementsIPFSHash - if (!ipfsDataUri) { - throw new Error( - 'TheBadge SDK: Missing requirementsIPFSHash for the given badge model id, provide a valid model id.', - ) - } - - // obtain evidences required - const { result, error } = await getFromIPFS<{ - columns: ThirdPartyMetadataColumn[] - }>(ipfsDataUri, this.env) - - const evidencesList = result?.content?.columns - if (error || !evidencesList) { - throw new Error('TheBadge SDK: Error obtaining required evidences list from IPFS, please retry.') - } - - // return the list of evidences required - return evidencesList + + if (!evidencesList) { + throw new Error('TheBadge SDK: Error obtaining required evidences list from IPFS, please retry.') } - throw new Error('TheBadge SDK: Error obtaining required evidences for given badgeModelId.') + return evidencesList } }