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
const JWT = Deno.env.get("PINATA_JWT");
const uploadFile = async () => {
try {
const url = "https://blobscan.com/tx/0xc239185ec981528648ec17b9d0647f3b0aa259006a1087e73a0a075f056e9a4a";
const hash = new URL(url).pathname.split("/").pop();
let data = new FormData();
if (hash.startsWith("0x0")) {
const hash = new URL(url).pathname.split("/").pop();
const urlStream = await fetch(`https://api.blobscan.com/blobs/${hash}`);
const arrayBuffer = await urlStream.arrayBuffer();
const blob = new Blob([arrayBuffer], { type: "application/json" });
const file = new File([blob], "blob.json", { type: "application/json" });
data.append("file", file);
} else {
const tx = await fetch(`https://api.blobscan.com/transactions/${hash}`);
const txData = await tx.json();
let blobs = []
let folder = "blobs"
await Promise.all(txData.blobs.map(async (blob, index) => {
const blobStream = await fetch(`https://api.blobscan.com/blobs/${blob}`);
const arrayBuffer = await blobStream.arrayBuffer();
const blobFile = new Blob([arrayBuffer], { type: "application/json" });
const file = new File([blobFile], `blob_${index}.json`, { type: "application/json" });
blobs.push(file)
}));
Array.from(blobs).forEach((blob) => {
data.append('file', blob, `${folder}/${blob.name}`)
})
}
console.log(data);
const response = await fetch("https://api.pinata.cloud/pinning/pinFileToIPFS", {
method: "POST",
headers: {
"Authorization": `Bearer ${JWT}`,
},
body: data,
});
const {IpfsHash} = await response.json();
const link = `https://dweb.mypinata.cloud/ipfs/${IpfsHash}`;
console.log(link);
return link;
} catch (error) {
console.log(error);
return error;
}
};
uploadFile();
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
March 15, 2024