Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update urls ipfs #44

Merged
merged 3 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/services/badgeModels/badgeModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down
8 changes: 5 additions & 3 deletions src/services/badges/badges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<BackendFileResponse>
>(badgeModel?.uri)
>(badgeModel?.uri, this.env)
if (badgeModelIPFSDataError) {
throw new Error('TheBadge SDK: Error obtaining IPFS data of badge model')
}
Expand All @@ -320,6 +320,7 @@ export class BadgesService extends TheBadgeSDKConfig implements BadgesServiceMet
}
const { result: registrationUriResult, error: registrationUriError } = await getFromIPFS<KlerosListStructure>(
registrationUri,
this.env,
)
if (registrationUriError) {
throw new Error('TheBadge SDK: Could not obtain registration data from IPFS, please enter a valid model id')
Expand All @@ -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')
}
Expand All @@ -356,6 +357,7 @@ export class BadgesService extends TheBadgeSDKConfig implements BadgesServiceMet
badgeModelIPFSData as BadgeModelMetadata,
userAddress,
{ imageBase64File: base64PreviewImage },
this.env,
)

// mint badge
Expand Down
44 changes: 27 additions & 17 deletions src/utils/badges/mintHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,54 @@
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,
minterAddress: string,
additionalArgs: {
imageBase64File: string
},
env: TheBadgeSDKEnv,
) {
const badgeMetadataIPFSUploaded = await uploadToIPFS<BadgeMetadata<BackendFileUpload>>({
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<BadgeMetadata<BackendFileUpload>>(
{
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}`
}

export async function createAndUploadBadgeEvidence(
columns: MetadataColumn[],
values: Record<string, unknown>,
env: TheBadgeSDKEnv,
): Promise<string> {
const filePaths = getFilePathsFromValues(values)
const evidenceIPFSUploaded = await uploadToIPFS<BadgeEvidenceMetadata>({
attributes: {
columns,
values,
submittedAt: Date.now(),
const evidenceIPFSUploaded = await uploadToIPFS<BadgeEvidenceMetadata>(
{
attributes: {
columns,
values,
submittedAt: Date.now(),
},
filePaths: filePaths,
},
filePaths: filePaths,
})
env,
)

const ipfsHash = evidenceIPFSUploaded.result?.ipfsHash
if (!ipfsHash) {
Expand Down
9 changes: 7 additions & 2 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -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'
22 changes: 13 additions & 9 deletions src/utils/ipfs.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
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.
* @async
* @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<T, X = {}>(hash: string): Promise<BackendResponse<{ content: T } & X>> {
export async function getFromIPFS<T, X = {}>(
hash: string,
env: TheBadgeSDKEnv,
): Promise<BackendResponse<{ content: T } & X>> {
const errorResponse: BackendResponse<{ content: T } & X> = {
error: true,
statusCode: 404,
Expand All @@ -21,9 +26,8 @@ export async function getFromIPFS<T, X = {}>(hash: string): Promise<BackendRespo
const cleanedHash = cleanHash(hash)
if (!cleanedHash) return errorResponse

const response = await axios.get<BackendResponse<{ content: T } & X>>(
`${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<BackendResponse<{ content: T } & X>>(`${apiUrl}/api/ipfs/${cleanedHash}`)
return response.data
}

Expand All @@ -37,14 +41,14 @@ type Args<T = Record<string, unknown>> = {
* Uploads data to IPFS, using The Badge's backend.
* @async
* @param {attributes: Record<string, unknown>, filePaths?: string[]} metadata
* @param env
*/
export async function uploadToIPFS<T>(
metadata: Args<T>,
env: TheBadgeSDKEnv,
): Promise<BackendResponse<{ ipfsHash: string; s3Url: string }>> {
const res = await axios.post<BackendResponse<{ ipfsHash: string; s3Url: string }>>(
`${THE_BADGE_BACKEND_URL}/api/ipfs/pin`,
metadata,
)
const apiUrl = env === TheBadgeSDKEnv.PRODUCTION ? THE_BADGE_IPFS_URL_PROD : THE_BADGE_IPFS_URL_STAGING
const res = await axios.post<BackendResponse<{ ipfsHash: string; s3Url: string }>>(`${apiUrl}/api/ipfs/pin`, metadata)
return res.data
}

Expand Down
Loading