This project demonstrates decentralized storage using IPFS and the Ethereum blockchain with Hardhat. It uploads a file to IPFS, retrieves its CID, stores the CID in a smart contract, and interacts with the contract to read/write the CID.
- Mohamed Lakssir
- Node.js (>=20.0.0) and npm
- pnpm (install with
npm install -g pnpm
) - Hardhat (install with
pnpm install --save-dev hardhat
) - IPFS CLI or Docker for the IPFS node
- Docker Compose (for local IPFS node)
-
Clone Repository:
git clone <repository-url> cd ipfs-blockchain-demo
-
Install Dependencies:
pnpm install
-
Run Local IPFS Node: Create a
docker-compose.yml
file:services: ipfs: image: ipfs/kubo:latest container_name: ipfs-node ports: - "4001:4001" # Peer-to-peer - "5001:5001" # API (used for interaction with ipfs-http-client) - "8080:8080" # Public gateway volumes: - ipfs_staging:/export - ipfs_data:/data/ipfs restart: unless-stopped command: daemon --migrate volumes: ipfs_staging: ipfs_data:
-
Start IPFS:
docker-compose up -d
-
Upload File to IPFS: Create a file (
fichier.txt
):echo "Bonjour IPFS + Blockchain" > fichier.txt
-
Run upload script to upload and get CID:
node scripts/uploadToIPFS.js
-
Start Hardhat Node:
npx hardhat node
-
Deploy Smart Contract:
npx hardhat run scripts/deploy.js --network localhost
Note the contract address (e.g.,
0x5FbDB2315678afecb367f032d93F642f64180aa3
). -
Interact with Contract: Update
scripts/interact.js
with the deployed contract address, then run:npx hardhat run scripts/interact.js --network localhost
Expected output:
Stored CID: QmT5NvUtoM5nZHUW2pZXEjrScpRr4V6kgwYZin13uA5fXZ
contracts/IPFSStorage.sol
: Smart contract for storing/retrieving IPFS CID.scripts/deploy.js
: Deploys the smart contract.scripts/interact.js
: Interacts with the deployed contract to set/get CID.scripts/uploadToIPFS.js
: Uploads a file to IPFS and retrieves its CID.hardhat.config.js
: Hardhat configuration.package.json
: Project dependencies and scripts.LICENSE
: The license under which the project is distributed.SECURITY.md
: Security policy and reporting guidelines for vulnerabilities.
Here are some screenshots demonstrating the functionality of the project:
-
Ensure the Hardhat node is running (
http://127.0.0.1:8545
). -
Verify the contract address in
interact.js
. -
Check IPFS node connectivity (
http://localhost:5001
). -
Reinstall dependencies if errors occur:
rm -rf node_modules pnpm-lock.yaml pnpm install
This project is licensed under the MIT License. See the LICENSE file for details.
For information on security vulnerabilities, please refer to the SECURITY.md file.