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

feat: add upload to pinnata function #117

Merged
merged 7 commits into from
Dec 2, 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: 2 additions & 0 deletions .github/workflows/sdk-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jobs:
uses: ./.github/workflows/formatting-linting.yml
tests:
runs-on: 'ubuntu-latest'
secrets:
PUB_PINATA_JWT: ${{ secrets.PUB_PINATA_JWT }}
defaults:
run:
working-directory: sdk
Expand Down
3 changes: 2 additions & 1 deletion sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@ethersproject/hash": "5.7.0",
"@ethersproject/logger": "5.7.0",
"@ethersproject/providers": "5.7.2",
"ipfs-http-client": "^51.0.0"
"dotenv": "^16.4.5",
"undici": "^6.21.0"
}
}
47 changes: 38 additions & 9 deletions sdk/src/ipfs.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,54 @@
import IPFS from 'ipfs-http-client';
import dotenv from 'dotenv';
import {fetch} from 'undici';

dotenv.config();

const IPFS_CID_REGEX =
/^((Qm[1-9A-HJ-NP-Za-km-z]{44,})|(b[A-Za-z2-7]{58,}|B[A-Z2-7]{58,})|(z[1-9A-HJ-NP-Za-km-z]{48,})|(F[0-9A-F]{50,}))$/;

const IPFS_URI_REGEX =
/^ipfs:\/\/((Qm[1-9A-HJ-NP-Za-km-z]{44,})|(b[A-Za-z2-7]{58,}|B[A-Z2-7]{58,})|(z[1-9A-HJ-NP-Za-km-z]{48,})|(F[0-9A-F]{50,}))$/;

// TODO Revisit
export async function uploadToIPFS(content: string): Promise<string> {
const client = IPFS.create({
url: 'https://prod.ipfs.aragon.network/api/v0',
function buildFileName(fileName: string): string {
return `osx-${fileName}.json`;
}

export async function uploadToPinata(
content: string,
fileName: string
): Promise<string> {
const body = {
pinataOptions: {
cidVersion: 1,
},
pinataMetadata: {
name: buildFileName(fileName),
},
pinataContent: content,
};

const res = await fetch('https://api.pinata.cloud/pinning/pinJsonToIPFS', {
method: 'POST',
headers: {
'X-API-KEY': 'b477RhECf8s8sdM7XrkLBs2wHc4kCMwpbcFC55Kt',
Authorization: `Bearer ${process.env.PUB_PINATA_JWT}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
});

const res = await client.add(content);
await client.pin.add(res.cid);
return res.cid.toString();
const resData: {error?: string; IpfsHash?: string} | undefined =
(await res.json()) as any;

if (resData?.error)
throw new Error(`Request failed: ${errorToString(resData.error)}`);
else if (!resData?.IpfsHash) throw new Error('Could not pin the metadata');

return `ipfs://${resData.IpfsHash}`;
}

function errorToString(error: any): string {
return `${error.reason}: ${error.details}`;
}
/**
* Checks if the given string is a valid IPFS CID
*
Expand Down
11 changes: 10 additions & 1 deletion sdk/test/unit/ipfs.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {isIpfsCid, isIpfsUri} from '../../src';
import {isIpfsCid, isIpfsUri, uploadToPinata} from '../../src';
import {
TEST_INVALID_IPFS_URI,
TEST_IPFS_CID_V0,
Expand Down Expand Up @@ -36,4 +36,13 @@ describe('ipfs', () => {
}
});
});
describe('uploadToPinata', () => {
it('Should upload the data to pinnata', async () => {
const ipfsUri = await uploadToPinata(
JSON.stringify('METADATA.release', null, 2),
'random-file-name'
);
expect(ipfsUri).toBeDefined();
});
});
});
Loading
Loading