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

Feature/pinata ipfs #47

Merged
merged 2 commits into from
Feb 9, 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
7 changes: 3 additions & 4 deletions apps/web/lib/ipfs/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { InfuraIpfsConnector } from '@dae/ipfs'
import { PinataIpfsConnector } from '@dae/ipfs'

export const IpfsConnector = new InfuraIpfsConnector(
process.env.INFURA_IPFS_API_KEY ?? '',
process.env.INFURA_IPFS_API_SECRET ?? '',
export const IpfsConnector = new PinataIpfsConnector(
process.env.NEXT_PUBLIC_IPFS_GATEWAY_URL ?? '',
process.env.PINATA_JWT_TOKEN ?? '',
)
18 changes: 6 additions & 12 deletions apps/web/pages/api/v0/course/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,8 @@ const handlePostRequest = async (
data: {
name: 'Admin',
description: 'The course Admin credential',
image_url:
'https://dae-demo.infura-ipfs.io/ipfs/QmXibYJSXskaqS7WyXLGwy16vGASqhN75yNUp2UfMRiLkF',
ipfs_url:
'https://dae-demo.infura-ipfs.io/ipfs/QmWNqAC88Sbti885sSbax9RK1Sfbo3akVeue5SMEhXWbjN',
image_url: `${process.env.NEXT_PUBLIC_IPFS_GATEWAY_URL}/QmXibYJSXskaqS7WyXLGwy16vGASqhN75yNUp2UfMRiLkF`,
ipfs_url: `${process.env.NEXT_PUBLIC_IPFS_GATEWAY_URL}/QmWNqAC88Sbti885sSbax9RK1Sfbo3akVeue5SMEhXWbjN`,
ipfs_cid: 'QmWNqAC88Sbti885sSbax9RK1Sfbo3akVeue5SMEhXWbjN',
type: 'ADMIN',
course: {
Expand All @@ -187,10 +185,8 @@ const handlePostRequest = async (
data: {
name: 'Magister',
description: 'The course Magister credential',
image_url:
'https://dae-demo.infura-ipfs.io/ipfs/QmTFVE4FoPJm2vazgVtKajbw2XSNtM2wTDrkUinxMcLbBg',
ipfs_url:
'https://dae-demo.infura-ipfs.io/ipfs/QmXRAu1zZ7igsNWo8egMDH3g77vFQgZHfcE2k6hoJp4JwT',
image_url: `${process.env.NEXT_PUBLIC_IPFS_GATEWAY_URL}/QmTFVE4FoPJm2vazgVtKajbw2XSNtM2wTDrkUinxMcLbBg`,
ipfs_url: `${process.env.NEXT_PUBLIC_IPFS_GATEWAY_URL}/QmXRAu1zZ7igsNWo8egMDH3g77vFQgZHfcE2k6hoJp4JwT`,
ipfs_cid: 'QmXRAu1zZ7igsNWo8egMDH3g77vFQgZHfcE2k6hoJp4JwT',
type: 'MAGISTER',
course: {
Expand All @@ -207,10 +203,8 @@ const handlePostRequest = async (
data: {
name: 'Discipulus',
description: 'The course Discipulus credential',
image_url:
'https://dae-demo.infura-ipfs.io/ipfs/QmUEC1WiGo9Vr3WER68u3T6mSwLexyDXj5G6WUgpVECmBY',
ipfs_url:
'https://dae-demo.infura-ipfs.io/ipfs/QmPfKCv7ZAz8294ShRTcHft5LSM9YaDJ4NTjZisCkhFxW8',
image_url: `${process.env.NEXT_PUBLIC_IPFS_GATEWAY_URL}/QmUEC1WiGo9Vr3WER68u3T6mSwLexyDXj5G6WUgpVECmBY`,
ipfs_url: `${process.env.NEXT_PUBLIC_IPFS_GATEWAY_URL}/QmPfKCv7ZAz8294ShRTcHft5LSM9YaDJ4NTjZisCkhFxW8`,
ipfs_cid: 'QmPfKCv7ZAz8294ShRTcHft5LSM9YaDJ4NTjZisCkhFxW8',
type: 'DISCIPULUS',
course: {
Expand Down
3 changes: 1 addition & 2 deletions apps/web/pages/api/v0/course/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ const handlePostRequest = async (
}

const { mimetype, filepath, originalFilename } = imageFile
console.log(originalFilename)
const courseImageBuffer = fs.readFileSync(filepath)

const ipfsCourseImageData = await IpfsConnector.upload({
Expand All @@ -51,7 +50,7 @@ const handlePostRequest = async (
'media-channel': mediaChannel,
},
fileName: '',
mimeType: 'data/json',
mimeType: 'application/json',
})

res.status(200).json({
Expand Down
4 changes: 3 additions & 1 deletion packages/ipfs/src/connectors/infura.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import axios from 'axios'
import FormData from 'form-data'
import { IpfsConnector, IpfsUploadResult } from '../'

export class InfuraIpfsConnector implements IpfsConnector {
export class InfuraIpfsConnector
implements IpfsConnector<Buffer | Record<string, any>>
{
private apiKey: string
private apiSecret: string
private authToken: string
Expand Down
80 changes: 80 additions & 0 deletions packages/ipfs/src/connectors/pinata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import axios from 'axios'
import FormData from 'form-data'
import { IpfsConnector, IpfsUploadResult } from '../'
import { Readable } from 'stream'

export class PinataIpfsConnector
implements IpfsConnector<Buffer | Record<string, any>>
{
private ipfsGateway: string
private JWT: string

constructor(ipfsGateway: string, jwtToken: string) {
this.ipfsGateway = ipfsGateway
this.JWT = jwtToken
}

public async upload({
fileContent,
fileName,
mimeType,
}: {
fileContent: Buffer | Record<string, any>
fileName: string
mimeType: string
}): Promise<IpfsUploadResult> {
const formData = new FormData()
let response = null

if (fileContent instanceof Buffer) {
console.log(fileName)
const stream = Readable.from(fileContent)
formData.append('file', stream, {
filepath: fileName,
contentType: mimeType,
})
response = await axios.post(
'https://api.pinata.cloud/pinning/pinFileToIPFS',
formData,
{
maxBodyLength: Infinity,
headers: {
'Content-Type': `multipart/form-data; boundary=${formData.getBoundary()}`,
Authorization: `Bearer ${this.JWT}`,
},
},
)
} else if (typeof fileContent === 'object') {
response = await axios.post(
'https://api.pinata.cloud/pinning/pinJSONToIPFS',
fileContent,
{
headers: {
accept: 'application/json',
'content-type': 'application/json',
Authorization: `Bearer ${this.JWT}`,
},
},
)
} else {
throw new Error('File type not supported.')
}

if (response.status !== 200) {
throw Error('Error uploading files to IPFS.')
}

const data = response.data as {
IpfsHash: string
PinSize: string
Timestamp: string
}

return {
hash: data.IpfsHash,
size: data.PinSize,
name: fileName,
url: `${this.ipfsGateway}/${data.IpfsHash}`,
}
}
}
5 changes: 3 additions & 2 deletions packages/ipfs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './connectors/infura'
export * from './connectors/pinata'

export type IpfsUploadResult = {
hash: string
Expand All @@ -8,13 +9,13 @@ export type IpfsUploadResult = {
timestamp?: number
}

export interface IpfsConnector {
export interface IpfsConnector<T> {
upload({
fileContent,
mimeType,
fileName,
}: {
fileContent: Buffer | Record<string, any>
fileContent: T
mimeType?: string
fileName?: string
}): Promise<IpfsUploadResult>
Expand Down
2 changes: 1 addition & 1 deletion packages/snapshot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
},
"dependencies": {
"@snapshot-labs/snapshot.js": "0.9.6",
"@snapshot-labs/snapshot.js": "0.10.1",
"ethers": "5.7.2",
"graphql": "16.8.1",
"graphql-request": "6.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@dae/wagmi": "workspace:*",
"@emotion/react": "11.11.1",
"@emotion/styled": "11.11.0",
"@snapshot-labs/snapshot.js": "0.9.6",
"@snapshot-labs/snapshot.js": "0.10.1",
"@types/papaparse": "5.3.11",
"@types/react": "18.2.37",
"@types/react-dom": "18.2.15",
Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading