i need help. i cant figure out the issue
i am stuck in this for 2 weeks. im getting error:”The provided route does not match a valid Pinata endpoint” but cant figureout the problem. isk what else to do
import React, { useState } from "react";
import { ethers } from "ethers";
import { create } from "ipfs-http-client";
import BookStorageABI from "./BookStorageABI.json";
import { Buffer } from "buffer";
const pinataApiKey = "1a1f5873d314cb07889f";
const pinataSecretApiKey =
"9861376b4e2082a7cfc349c9a9e8e42406b1da09c27f1c48732fe06dbb654e55";
const ipfs = create({
host: "api.pinata.cloud",
port: 443,
protocol: "https",
apiPath: "/pinning/v1/add",
headers: {
"Content-Type": "application/json",
pinata_api_key: pinataApiKey,
pinata_secret_api_key: pinataSecretApiKey,
},
});
const contractAddress = "0x5C9aD105930c084088c744fB3D462E2171098A8a";
function App() {
const [pdfFile, setPdfFile] = useState(null);
const [ipfsHash, setIpfsHash] = useState("");
const [storedIpfsHash, setStoredIpfsHash] = useState("");
const handleFileChange = (e) => {
setPdfFile(e.target.files[0]);
};
const handleUpload = async () => {
if (!pdfFile) return;
try {
const pdfBuffer = await new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(Buffer.from(reader.result));
reader.onerror = reject;
reader.readAsArrayBuffer(pdfFile);
});
const formData = new FormData();
formData.append(
"file",
new Blob([pdfBuffer], { type: "application/pdf" })
);
const response = await fetch(
"https://api.pinata.cloud/pinning/v1/pinFile",
{
method: "POST",
headers: {
pinata_api_key: pinataApiKey,
pinata_secret_api_key: pinataSecretApiKey,
Authorization: `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySW5mb3JtYXRpb24iOnsiaWQiOiIyMmNhYWY4Zi00ZmEyLTRkN2MtYmZmMy0wMzk5NTljZThlNzciLCJlbWFpbCI6Im11bXRhem11aGFtbWFkNDkwQGdtYWlsLmNvbSIsImVtYWlsX3ZlcmlmaWVkIjp0cnVlLCJwaW5fcG9saWN5Ijp7InJlZ2lvbnMiOlt7ImlkIjoiRlJBMSIsImRlc2lyZWRSZXBsaWNhdGlvbkNvdW50IjoxfSx7ImlkIjoiTllDMSIsImRlc2lyZWRSZXBsaWNhdGlvbkNvdW50IjoxfV0sInZlcnNpb24iOjF9LCJtZmFfZW5hYmxlZCI6ZmFsc2UsInN0YXR1cyI6IkFDVElWRSJ9LCJhdXRoZW50aWNhdGlvblR5cGUiOiJzY29wZWRLZXkiLCJzY29wZWRLZXlLZXkiOiIxYTFmNTg3M2QzMTRjYjA3ODg5ZiIsInNjb3BlZEtleVNlY3JldCI6Ijk4NjEzNzZiNGUyMDgyYTdjZmMzNDljOWE5ZThlNDI0MDZiMWRhMDljMjdmMWM0ODczMmZlMDZkYmI2NTRlNTUiLCJpYXQiOjE3MTY4Mzg0MjJ9.lejJQi5mjhLVCO_pKq3SKteVgWz_4RjnKn-G4ZDQevg`,
},
body: formData,
}
);
if (!response.ok) {
const errorData = await response.json();
console.error("Error uploading to IPFS:", errorData);
throw new Error(`HTTP error status: ${response.status}`);
}
const { IpfsHash } = await response.json();
setIpfsHash(IpfsHash);
const provider = new ethers.providers.AlchemyProvider(
"sepolia",
"kKv4hFRw45Rax8LFzQs3xMtkZUjiNQZX"
);
const signer = provider.getSigner();
const contract = new ethers.Contract(
contractAddress,
BookStorageABI,
signer
);
const tx = await contract.storeBookCID(IpfsHash);
await tx.wait();
alert("PDF uploaded and CID stored in smart contract");
} catch (error) {
console.error("Error uploading to IPFS:", error);
alert(
"Failed to upload PDF to IPFS. Please check the console for more details."
);
}
};
const handleRetrieve = async () => {
try {
const provider = new ethers.providers.AlchemyProvider(
"sepolia",
"kKv4hFRw45Rax8LFzQs3xMtkZUjiNQZX"
);
const signer = provider.getSigner();
const contract = new ethers.Contract(
contractAddress,
BookStorageABI,
signer
);
const hash = await contract.getBookCID(await signer.getAddress());
setStoredIpfsHash(hash);
} catch (error) {
console.error("Error retrieving CID:", error);
alert(
"Failed to retrieve CID. Please check the console for more details."
);
}
};
const handleDownload = async () => {
if (!storedIpfsHash) return;
try {
const response = await fetch(
`https://gateway.pinata.cloud/ipfs/${storedIpfsHash}`
);
if (!response.ok) {
throw new Error(`HTTP error status: ${response.status}`);
}
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "downloaded_book.pdf";
document.body.appendChild(a);
a.click();
a.remove();
} catch (error) {
console.error("Error downloading PDF:", error);
alert(
"Failed to download PDF. Please check the console for more details."
);
}
};
return (
<div className="App">
<h1>Upload and Download PDF via IPFS and Smart Contract</h1>
<input type="file" onChange={handleFileChange} />
<button onClick={handleUpload}>Upload PDF</button>
<button onClick={handleRetrieve}>Retrieve PDF CID</button>
<button onClick={handleDownload}>Download PDF</button>
<p>IPFS Hash: {ipfsHash}</p>
<p>Stored IPFS Hash: {storedIpfsHash}</p>
</div>
);
}
export default App;
i used solidity contract for it and ist address is also given…………………………………
New contributor
Nada Mumtaz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.