From a0fccc1b21a6b37a02a5e4d7e28c92373080672b Mon Sep 17 00:00:00 2001 From: Fernando Ramirez Date: Thu, 1 Feb 2024 14:37:12 +0100 Subject: [PATCH 1/2] Update/fix urls --- src/services/badgeModels/badgeModels.ts | 2 +- src/services/badges/badges.ts | 8 +++-- src/utils/badges/mintHelpers.ts | 44 +++++++++++++++---------- src/utils/constants.ts | 9 +++-- src/utils/ipfs.ts | 19 +++++++---- 5 files changed, 53 insertions(+), 29 deletions(-) diff --git a/src/services/badgeModels/badgeModels.ts b/src/services/badgeModels/badgeModels.ts index 088d252..da68c66 100644 --- a/src/services/badgeModels/badgeModels.ts +++ b/src/services/badgeModels/badgeModels.ts @@ -87,7 +87,7 @@ export class BadgeModelsService extends TheBadgeSDKConfig implements BadgeModels } // obtain evidences required - const { result, error } = await getFromIPFS<{ metadata: { columns: MetadataColumn[] } }>(ipfsDataUri) + const { result, error } = await getFromIPFS<{ metadata: { columns: MetadataColumn[] } }>(ipfsDataUri, this.env) const evidencesList = result?.content?.metadata?.columns if (error || !evidencesList) { throw new Error('TheBadge SDK: Error obtaining required evidences list from IPFS, please retry.') diff --git a/src/services/badges/badges.ts b/src/services/badges/badges.ts index 0c1410b..96f8bb6 100644 --- a/src/services/badges/badges.ts +++ b/src/services/badges/badges.ts @@ -264,7 +264,7 @@ export class BadgesService extends TheBadgeSDKConfig implements BadgesServiceMet } // obtain badge image data - const { result, error } = await getFromIPFS<{ image: { ipfsHash: string } }>(ipfsDataUri) + const { result, error } = await getFromIPFS<{ image: { ipfsHash: string } }>(ipfsDataUri, this.env) const badgeImageIPFSHash = result?.content?.image?.ipfsHash if (error || !badgeImageIPFSHash) { @@ -304,7 +304,7 @@ export class BadgesService extends TheBadgeSDKConfig implements BadgesServiceMet // get ipfs data of badge model const { result: badgeModelIPFSDataResult, error: badgeModelIPFSDataError } = await getFromIPFS< BadgeModelMetadata - >(badgeModel?.uri) + >(badgeModel?.uri, this.env) if (badgeModelIPFSDataError) { throw new Error('TheBadge SDK: Error obtaining IPFS data of badge model') } @@ -320,6 +320,7 @@ export class BadgesService extends TheBadgeSDKConfig implements BadgesServiceMet } const { result: registrationUriResult, error: registrationUriError } = await getFromIPFS( registrationUri, + this.env, ) if (registrationUriError) { throw new Error('TheBadge SDK: Could not obtain registration data from IPFS, please enter a valid model id') @@ -340,7 +341,7 @@ export class BadgesService extends TheBadgeSDKConfig implements BadgesServiceMet // upload evidence to IPFS const evidencesValues = createEvidencesValuesObject(evidencesParsedObject, requiredEvidencesList) - const evidenceIPFSHash = await createAndUploadBadgeEvidence(requiredEvidencesList, evidencesValues) + const evidenceIPFSHash = await createAndUploadBadgeEvidence(requiredEvidencesList, evidencesValues, this.env) if (!evidenceIPFSHash) { throw new Error('TheBadge SDK: No evidence IPFS hash, could not upload evidence to IPFS') } @@ -356,6 +357,7 @@ export class BadgesService extends TheBadgeSDKConfig implements BadgesServiceMet badgeModelIPFSData as BadgeModelMetadata, userAddress, { imageBase64File: base64PreviewImage }, + this.env, ) // mint badge diff --git a/src/utils/badges/mintHelpers.ts b/src/utils/badges/mintHelpers.ts index 227b86c..2a685f5 100644 --- a/src/utils/badges/mintHelpers.ts +++ b/src/utils/badges/mintHelpers.ts @@ -1,9 +1,10 @@ import { convertHashToValidIPFSKlerosHash, uploadToIPFS } from '@utils/ipfs' -import { THE_BADGE_DAPP_URL } from '@utils/constants' +import { THE_BADGE_DAPP_URL_PROD, THE_BADGE_DAPP_URL_STAGING } from '@utils/constants' import { BadgeEvidenceMetadata, BadgeMetadata, BadgeModelMetadata } from '@businessLogic/theBadge/BadgeMetadata' import { BackendFileUpload } from '@businessLogic/types' import { MetadataColumn } from '@businessLogic/kleros/types' import { defaultAbiCoder } from 'ethers/lib/utils' +import { TheBadgeSDKEnv } from '../../config' export async function createAndUploadBadgeMetadata( badgeModelMetadata: BadgeModelMetadata, @@ -11,17 +12,22 @@ export async function createAndUploadBadgeMetadata( additionalArgs: { imageBase64File: string }, + env: TheBadgeSDKEnv, ) { - const badgeMetadataIPFSUploaded = await uploadToIPFS>({ - attributes: { - name: badgeModelMetadata?.name || '', - description: badgeModelMetadata?.description || '', - external_link: `${THE_BADGE_DAPP_URL}/profile/${minterAddress}`, - attributes: [], - image: { mimeType: 'image/png', base64File: additionalArgs.imageBase64File }, + const dappUrl = env === TheBadgeSDKEnv.PRODUCTION ? THE_BADGE_DAPP_URL_PROD : THE_BADGE_DAPP_URL_STAGING + const badgeMetadataIPFSUploaded = await uploadToIPFS>( + { + attributes: { + name: badgeModelMetadata?.name || '', + description: badgeModelMetadata?.description || '', + external_link: `${dappUrl}/profile/${minterAddress}`, + attributes: [], + image: { mimeType: 'image/png', base64File: additionalArgs.imageBase64File }, + }, + filePaths: ['image'], }, - filePaths: ['image'], - }) + env, + ) return `ipfs://${badgeMetadataIPFSUploaded.result?.ipfsHash}` } @@ -29,16 +35,20 @@ export async function createAndUploadBadgeMetadata( export async function createAndUploadBadgeEvidence( columns: MetadataColumn[], values: Record, + env: TheBadgeSDKEnv, ): Promise { const filePaths = getFilePathsFromValues(values) - const evidenceIPFSUploaded = await uploadToIPFS({ - attributes: { - columns, - values, - submittedAt: Date.now(), + const evidenceIPFSUploaded = await uploadToIPFS( + { + attributes: { + columns, + values, + submittedAt: Date.now(), + }, + filePaths: filePaths, }, - filePaths: filePaths, - }) + env, + ) const ipfsHash = evidenceIPFSUploaded.result?.ipfsHash if (!ipfsHash) { diff --git a/src/utils/constants.ts b/src/utils/constants.ts index 641b990..a3198d4 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -1,2 +1,7 @@ -export const THE_BADGE_DAPP_URL = 'https://staging-app.thebadge.xyz' -export const THE_BADGE_BACKEND_URL = 'https://api.thebadge.xyz/staging' +// dApp URLs +export const THE_BADGE_DAPP_URL_PROD = 'https://app.thebadge.xyz' +export const THE_BADGE_DAPP_URL_STAGING = 'https://staging-app.thebadge.xyz' + +// IPFS URLs +export const THE_BADGE_IPFS_URL_PROD = 'https://ipfs.thebadge.xyz' +export const THE_BADGE_IPFS_URL_STAGING = 'https://staging-ipfs.thebadge.xyz' diff --git a/src/utils/ipfs.ts b/src/utils/ipfs.ts index 953c64d..5cbdc3d 100644 --- a/src/utils/ipfs.ts +++ b/src/utils/ipfs.ts @@ -1,6 +1,7 @@ import axios from 'axios' import { BackendResponse } from '@businessLogic/types' -import { THE_BADGE_BACKEND_URL } from '@utils/constants' +import { THE_BADGE_IPFS_URL_PROD, THE_BADGE_IPFS_URL_STAGING } from '@utils/constants' +import { TheBadgeSDKEnv } from '../config' /** * Retrieves data from IPFS based on the given hash, using The Badge's backend. @@ -8,9 +9,13 @@ import { THE_BADGE_BACKEND_URL } from '@utils/constants' * @template T - The type of the content data to retrieve from IPFS. * @template X - Additional type parameter that can be optionally provided to extend the backend response type. * @param {string} hash - The IPFS hash representing the content to retrieve. + * @param env */ // eslint-disable-next-line @typescript-eslint/ban-types -export async function getFromIPFS(hash: string): Promise> { +export async function getFromIPFS( + hash: string, + env: TheBadgeSDKEnv, +): Promise> { const errorResponse: BackendResponse<{ content: T } & X> = { error: true, statusCode: 404, @@ -21,9 +26,8 @@ export async function getFromIPFS(hash: string): Promise>( - `${THE_BADGE_BACKEND_URL}/api/ipfs/${cleanedHash}`, - ) + const apiUrl = env === TheBadgeSDKEnv.PRODUCTION ? THE_BADGE_IPFS_URL_PROD : THE_BADGE_IPFS_URL_STAGING + const response = await axios.get>(`${apiUrl}/api/ipfs/${cleanedHash}`) return response.data } @@ -37,12 +41,15 @@ type Args> = { * Uploads data to IPFS, using The Badge's backend. * @async * @param {attributes: Record, filePaths?: string[]} metadata + * @param env */ export async function uploadToIPFS( metadata: Args, + env: TheBadgeSDKEnv, ): Promise> { + const apiUrl = env === TheBadgeSDKEnv.PRODUCTION ? THE_BADGE_IPFS_URL_PROD : THE_BADGE_IPFS_URL_STAGING const res = await axios.post>( - `${THE_BADGE_BACKEND_URL}/api/ipfs/pin`, + `${apiUrl}/api/ipfs/pin`, metadata, ) return res.data From fc23d0747ca8c9b32c16b25dedd59616658c1193 Mon Sep 17 00:00:00 2001 From: Fernando Ramirez Date: Thu, 1 Feb 2024 14:40:54 +0100 Subject: [PATCH 2/2] Fix lint --- src/utils/ipfs.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/utils/ipfs.ts b/src/utils/ipfs.ts index 5cbdc3d..6e04239 100644 --- a/src/utils/ipfs.ts +++ b/src/utils/ipfs.ts @@ -48,10 +48,7 @@ export async function uploadToIPFS( env: TheBadgeSDKEnv, ): Promise> { const apiUrl = env === TheBadgeSDKEnv.PRODUCTION ? THE_BADGE_IPFS_URL_PROD : THE_BADGE_IPFS_URL_STAGING - const res = await axios.post>( - `${apiUrl}/api/ipfs/pin`, - metadata, - ) + const res = await axios.post>(`${apiUrl}/api/ipfs/pin`, metadata) return res.data }