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

Add Pinata SDK and file upload script #9

Merged
merged 3 commits into from
Dec 17, 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
6 changes: 6 additions & 0 deletions .github/workflows/continuousIntgration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ jobs:
PUBLIC_DEPLOYMENT_CONTRACT_ID: ${{ vars.PUBLIC_DEPLOYMENT_CONTRACT_ID }}
PUBLIC_DEPLOYMENT_NFT_TOKEN_ID: ${{ vars.PUBLIC_DEPLOYMENT_NFT_TOKEN_ID }}
PUBLIC_IPFS_GATEWAY_BASE_URL: ${{ vars.PUBLIC_IPFS_GATEWAY_BASE_URL }}
PUBLIC_GATEWAY_URL: ${{ vars.PUBLIC_GATEWAY_URL }}
PINATA_GROUP_ID: ${{ secrets,PINATA_GROUP_ID }}
PINATA_JWT: ${{ secrets.PINATA_JWT }}
steps:
# setup
- uses: actions/checkout@v4
Expand All @@ -40,6 +43,9 @@ jobs:
PUBLIC_DEPLOYMENT_CONTRACT_ID: ${{ vars.PUBLIC_DEPLOYMENT_CONTRACT_ID }}
PUBLIC_DEPLOYMENT_NFT_TOKEN_ID: ${{ vars.PUBLIC_DEPLOYMENT_NFT_TOKEN_ID }}
PUBLIC_IPFS_GATEWAY_BASE_URL: ${{ vars.PUBLIC_IPFS_GATEWAY_BASE_URL }}
PUBLIC_GATEWAY_URL: ${{ vars.PUBLIC_GATEWAY_URL }}
PINATA_GROUP_ID: ${{ secrets,PINATA_GROUP_ID }}
PINATA_JWT: ${{ secrets.PINATA_JWT }}
steps:
# setup
- uses: actions/checkout@v4
Expand Down
5 changes: 4 additions & 1 deletion frontend/.env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
HEDERA_ACCOUNT_ID=
HEDERA_PRIVATE_KEY=

# testnet / mainnet
HEDERA_ENVIRONMENT=
# the reown (formerly walletconnect) project id used for hashpack
Expand All @@ -9,3 +8,7 @@ PUBLIC_HASHCONNECT_PROJECT_ID=3918c43fa2467261721ed00df472e8be
PUBLIC_DEPLOYMENT_CONTRACT_ID=0.0.5272938
PUBLIC_DEPLOYMENT_NFT_TOKEN_ID=0.0.5272939
PUBLIC_IPFS_GATEWAY_BASE_URL=https://flk-ipfs.xyz/ipfs
# pinata
PINATA_JWT=
PINATA_GROUP_ID=
PUBLIC_GATEWAY_URL=
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"eslint-plugin-svelte": "^2.46.1",
"globals": "^15.13.0",
"hashconnect": "^3.0.13",
"pinata-web3": "^0.5.3",
"prettier": "^3.4.2",
"prettier-plugin-css-order": "^2.1.2",
"prettier-plugin-organize-imports": "^4.1.0",
Expand Down
1 change: 0 additions & 1 deletion frontend/src/lib/index.ts

This file was deleted.

8 changes: 8 additions & 0 deletions frontend/src/lib/pinata/pinata.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { PINATA_JWT } from '$env/static/private'
import { PUBLIC_GATEWAY_URL } from '$env/static/public'
import { PinataSDK } from 'pinata-web3'

export const pinata = new PinataSDK({
pinataJwt: PINATA_JWT,
pinataGateway: PUBLIC_GATEWAY_URL,
})
49 changes: 49 additions & 0 deletions frontend/src/routes/api/pinata/upload/+server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// https://docs.pinata.cloud/web3/sdk/getting-started

import { pinata } from '$lib/pinata/pinata.server'
import { json } from '@sveltejs/kit'
import type { RequestHandler } from './$types'

import { PINATA_GROUP_ID } from '$env/static/private'

export const POST: RequestHandler = async ({ request }) => {
try {
const formData = await request.formData()

// todo we wont need file uplaod in the future, as the image will be generated randomly!
const uploadedFile = formData.get('file') as File
const metadata = JSON.parse(formData.get('metadata') as string)

if (!uploadedFile?.name || uploadedFile.size === 0) {
return json({ error: 'You must provide a file to upload' }, { status: 400 })
}

// Upload image todo for some reason the group id gets ignored?
const imageUpload = await pinata.upload.file(uploadedFile).group(PINATA_GROUP_ID)

const imageIpfsUrl = `ipfs://${imageUpload.IpfsHash}`

// Create and upload metadata (containing IPFS image url)
const fullMetadata = {
...metadata,
type: 'image/jpg',
format: 'none',
image: imageIpfsUrl,
}

// Upload metadata to IPFS
const metadataUpload = await pinata.upload.json(fullMetadata)
const metadataUrl = `ipfs://${metadataUpload.IpfsHash}`

return json({
imageUrl: await pinata.gateways.convert(imageUpload.IpfsHash),
metadataUrl, // this is the only thing that matters id say, and we will pass this as metadata on mint process
imageIpfsHash: imageUpload.IpfsHash,
metadataIpfsHash: metadataUpload.IpfsHash,
})
} catch (throwable) {
console.error('Upload failed:', throwable)
const error = throwable instanceof Error ? throwable : new Error(String(throwable))
return json({ error: error.message }, { status: 500 })
}
}
Loading