-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.js
More file actions
118 lines (104 loc) · 3.19 KB
/
Copy pathsource.js
File metadata and controls
118 lines (104 loc) · 3.19 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const cid = args[0];
const senderAddress = args[1];
const receiverAddress = args[2];
if (!cid) throw new Error("Missing CID");
if (!senderAddress) throw new Error("Missing senderAddress");
if (!receiverAddress) throw new Error("Missing receiverAddress");
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function validateSecrets(requiredKeys) {
for (const key of requiredKeys) {
if (!secrets[key]) {
throw new Error(`Missing required secret: ${key}`);
}
}
}
validateSecrets([
"PINATA_API_JWT",
"PINATA_GATEWAY",
"PINATA_PRIVATE_GROUP_ID",
"PINATA_PUBLIC_GROUP_ID",
]);
const authHeaders = () => ({
Authorization: `Bearer ${secrets.PINATA_API_JWT}`,
"Content-Type": "application/json",
});
async function getDownloadLink(cid) {
const url = `${secrets.PINATA_GATEWAY}/files/${cid}`;
const response = await Functions.makeHttpRequest({
method: "POST",
url: "https://api.pinata.cloud/v3/files/private/download_link",
headers: authHeaders(),
data: {
url,
expires: 180,
date: Math.floor(Date.now() / 1000),
method: "GET",
},
});
return response.data.data;
}
async function fetchPrivateContent(downloadUrl) {
const response = await Functions.makeHttpRequest({
method: "GET",
url: downloadUrl,
});
return response.data;
}
async function getPrivateFileInfo(cid) {
const response = await Functions.makeHttpRequest({
method: "GET",
url: `https://api.pinata.cloud/v3/files/private?group=${secrets.PINATA_PRIVATE_GROUP_ID}&cid=${cid}`,
headers: authHeaders(),
});
return response.data.data.files[0];
}
async function pinPublicVersion(content, privateInfo) {
const metadata = {
name: `${privateInfo.name.replace(/\.json$/i, "")}-public.json`,
keyvalues: { privateCid: privateInfo.cid, senderAddress, receiverAddress },
};
const response = await Functions.makeHttpRequest({
method: "POST",
url: "https://api.pinata.cloud/pinning/pinJSONToIPFS",
headers: authHeaders(),
data: {
pinataContent: content,
pinataMetadata: metadata,
},
});
return response.data;
}
async function assignToPublicGroup(newId) {
await Functions.makeHttpRequest({
method: "PUT",
url: `https://api.pinata.cloud/v3/groups/public/${secrets.PINATA_PUBLIC_GROUP_ID}/ids/${newId}`,
headers: authHeaders(),
});
}
async function deletePrivateFile(privateId) {
const response = await Functions.makeHttpRequest({
method: "DELETE",
url: `https://api.pinata.cloud/v3/files/private/${privateId}`,
headers: authHeaders(),
});
}
// --- Execution Flow ---
let newCid;
try {
const downloadUrl = await getDownloadLink(cid);
const privateContent = await fetchPrivateContent(downloadUrl);
const privateInfo = await getPrivateFileInfo(cid);
const publicData = await pinPublicVersion(privateContent, privateInfo);
newCid = publicData.IpfsHash;
await assignToPublicGroup(publicData.ID);
// await sleep(2500);
// await deletePrivateFile(privateInfo.id);
} catch (error) {
throw new Error("Error ocurred: " + error.message);
}
if (!newCid || typeof newCid !== "string") {
throw new Error("Failed to process the CID or get a new CID");
}
return Functions.encodeString(newCid);