-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpinata.js
63 lines (54 loc) · 2.04 KB
/
pinata.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const axios = require('axios');
const FormData = require('form-data');
const dotenv = require('dotenv');
dotenv.config();
// Placeholder for API key and secret
const PINATA_API_KEY = process.env.PINATA_API_KEY;
const PINATA_API_SECRET = process.env.PINATA_API_SECRET;
/**
* Uploads an NFT image file to IPFS using Piñata's API.
* @param {Buffer} fileBuffer - The buffer of the file to upload.
* @param {string} fileName - The name of the file to upload.
* @returns {Promise<string>} - The IPFS URL of the uploaded file.
*/
async function uploadFileToIPFS(fileBuffer, fileName) {
const url = 'https://api.pinata.cloud/pinning/pinFileToIPFS';
const formData = new FormData();
formData.append('file', fileBuffer, { filename: fileName });
try {
const response = await axios.post(url, formData, {
headers: {
...formData.getHeaders(),
pinata_api_key: PINATA_API_KEY,
pinata_secret_api_key: PINATA_API_SECRET,
},
});
return `https://gateway.pinata.cloud/ipfs/${response.data.IpfsHash}`;
}
catch (error) {
console.error('Error uploading file to IPFS:', error.message);
throw new Error('File upload to IPFS failed.');
}
}
/**
* Uploads JSON metadata to IPFS using Piñata's API.
* @param {Object} jsonObject - The JSON object to upload.
* @returns {Promise<string>} - The IPFS URL of the uploaded JSON.
*/
async function uploadJSONToIPFS(jsonObject) {
const url = 'https://api.pinata.cloud/pinning/pinJSONToIPFS';
try {
const response = await axios.post(url, jsonObject, {
headers: {
pinata_api_key: PINATA_API_KEY,
pinata_secret_api_key: PINATA_API_SECRET,
},
});
return `https://gateway.pinata.cloud/ipfs/${response.data.IpfsHash}`;
}
catch (error) {
console.error('Error uploading JSON to IPFS:', error.message);
throw new Error('JSON upload to IPFS failed.');
}
}
module.exports = { uploadFileToIPFS, uploadJSONToIPFS };