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

fix: pinata #142

Merged
merged 6 commits into from
Jun 13, 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
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@itheum/sdk-mx-data-nft",
"version": "3.5.0",
"version": "3.5.1",
"description": "SDK for Itheum's Data NFT Technology on MultiversX Blockchain",
"main": "out/index.js",
"types": "out/index.d.js",
Expand Down
71 changes: 55 additions & 16 deletions src/common/mint-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,64 @@ export async function dataNFTDataStreamAdvertise(

export async function storeToIpfs(
storageToken: string,
traits: File,
image: File
traits: object,
image: Blob
): Promise<{ imageOnIpfsUrl: string; metadataOnIpfsUrl: string }> {
let res;
try {
const nftstorage = new NFTStorage({
token: storageToken
});
const dir = [image, traits];
res = await nftstorage.storeDirectory(dir);
const imageHash = await storeImageToIpfs(image, storageToken);
const traitsHash = await storeTraitsToIpfs(traits, storageToken);
return {
imageOnIpfsUrl: `https://ipfs.io/ipfs/${imageHash}`,
metadataOnIpfsUrl: `https://ipfs.io/ipfs/${traitsHash}`
};
} catch (error) {
throw error;
}
return {
imageOnIpfsUrl: `https://ipfs.io/ipfs/${res}/image.png`,
metadataOnIpfsUrl: `https://ipfs.io/ipfs/${res}/metadata.json`
}

async function storeImageToIpfs(image: Blob, storageToken: string) {
const form = new FormData();
form.append('file', image);
form.append('pinataMetadata', '{\n "name": "image"\n}');
form.append('pinataOptions', '{\n "cidVersion": 0\n}');

const options = {
method: 'POST',
headers: {
Authorization: `Bearer ${storageToken}`
},
body: form
};
const response = await fetch(
'https://api.pinata.cloud/pinning/pinFileToIPFS',
options
);
const res = await response.json();
const imageHash = res.IpfsHash;
return imageHash;
}

async function storeTraitsToIpfs(traits: object, storageToken: string) {
const options = {
method: 'POST',
headers: {
Authorization: `Bearer ${storageToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
pinataOptions: { cidVersion: 0 },
pinataMetadata: { name: 'metadata' },
pinataContent: traits
})
};

const response = await fetch(
'https://api.pinata.cloud/pinning/pinJSONToIPFS',
options
);
const res = await response.json();
const traitsHash = res.IpfsHash;
return traitsHash;
}

export function createIpfsMetadata(
Expand Down Expand Up @@ -96,11 +137,11 @@ export async function createFileFromUrl(
) {
let res: any = '';
let data: any = '';
let _imageFile: File = new File([], '');
let _imageFile: Blob = new Blob();
if (url) {
res = await fetch(url);
data = await res.blob();
_imageFile = new File([data], 'image.png', { type: 'image/png' });
_imageFile = data;
}
const traits = createIpfsMetadata(
res.headers.get('x-nft-traits') || '',
Expand All @@ -110,8 +151,6 @@ export async function createFileFromUrl(
address,
extraAssets
);
const _traitsFile = new File([JSON.stringify(traits)], 'metadata.json', {
type: 'application/json'
});
const _traitsFile = traits;
return { image: _imageFile, traits: _traitsFile };
}
Loading