Initial commit with 🏗️ Scaffold-ETH 2 @ 1.0.5
This commit is contained in:
35
packages/nextjs/utils/tokenization/ipfs.ts
Normal file
35
packages/nextjs/utils/tokenization/ipfs.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { create } from "kubo-rpc-client";
|
||||
|
||||
const PROJECT_ID = "2GajDLTC6y04qsYsoDRq9nGmWwK";
|
||||
const PROJECT_SECRET = "48c62c6b3f82d2ecfa2cbe4c90f97037";
|
||||
const PROJECT_ID_SECRECT = `${PROJECT_ID}:${PROJECT_SECRET}`;
|
||||
|
||||
export const ipfsClient = create({
|
||||
host: "ipfs.infura.io",
|
||||
port: 5001,
|
||||
protocol: "https",
|
||||
headers: {
|
||||
Authorization: `Basic ${Buffer.from(PROJECT_ID_SECRECT).toString("base64")}`,
|
||||
},
|
||||
});
|
||||
|
||||
export async function getNFTMetadataFromIPFS(ipfsHash: string) {
|
||||
for await (const file of ipfsClient.get(ipfsHash)) {
|
||||
// The file is of type unit8array so we need to convert it to string
|
||||
const content = new TextDecoder().decode(file);
|
||||
// Remove any leading/trailing whitespace
|
||||
const trimmedContent = content.trim();
|
||||
// Find the start and end index of the JSON object
|
||||
const startIndex = trimmedContent.indexOf("{");
|
||||
const endIndex = trimmedContent.lastIndexOf("}") + 1;
|
||||
// Extract the JSON object string
|
||||
const jsonObjectString = trimmedContent.slice(startIndex, endIndex);
|
||||
try {
|
||||
const jsonObject = JSON.parse(jsonObjectString);
|
||||
return jsonObject;
|
||||
} catch (error) {
|
||||
console.log("Error parsing JSON:", error);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user