TL;DR
Use decentralised blockchain technology to store and access information that is resistant to censorship. This guide covers using IPFS with a blockchain for content addressing, creating a simple smart contract for registration, and accessing the data through a web interface.
1. Understanding the Problem & Solution
Traditional internet systems rely on central authorities (servers, ISPs) who can block or filter information. Blockchain offers a way to store data across many computers, making it very difficult for anyone to control or censor it. IPFS (InterPlanetary File System) is a peer-to-peer protocol for storing files in a distributed manner. Combining IPFS with a blockchain allows you to create permanent, censorship-resistant content addresses.
2. Setting up Your Environment
- Install Node.js and npm: You’ll need these to run JavaScript code. Download from nodejs.org
- Install Truffle: A development framework for Ethereum smart contracts. Open your terminal and run:
npm install -g truffle - Install Ganache: A personal blockchain for testing. Download from trufflesuite.com/ganache
- Install IPFS: Download and install the IPFS desktop application from ipfs.io/#downloads. Make sure the ipfs daemon is running in your terminal (
ipfs daemon).
3. Creating a Simple Smart Contract
This contract will allow users to register their IPFS content hashes on the blockchain.
- Create a new Truffle project: In your terminal, run:
truffle init - Write the smart contract (contracts/ContentRegistry.sol):
pragma solidity ^0.8.0; contract ContentRegistry { address public owner; mapping(address => string) public contentHashes; constructor() { owner = msg.sender; } function registerContent(string memory _hash) public { contentHashes[msg.sender] = _hash; } function getContentHash(address _addr) public view returns (string memory) { return contentHashes[_addr]; } } - Compile the contract: In your terminal, run:
truffle compile
4. Deploying the Smart Contract
- Start Ganache: Open the Ganache application. It will provide you with development blockchain accounts and a network address (usually http://127.0.0.1:7545).
- Migrate the contract: Edit
truffle-config.jsto point to your Ganache network. Then, run:truffle migrate
5. Storing Content on IPFS
Upload the content you want to store to IPFS.
- Add your file: Open your terminal and run:
ipfs add /path/to/your/fileThis will return a long hash (e.g.,
Qm...). This is the content identifier.
6. Registering Content Hash on Blockchain
Register the IPFS hash with your smart contract.
- Connect to the blockchain: Use a JavaScript library like Web3.js or Ethers.js in your frontend application.
- Call the
registerContentfunction: Send a transaction to the deployed smart contract, passing the IPFS hash as an argument.// Example using Web3.js (simplified) const contentRegistry = new web3.eth.Contract(abi, address); contentRegistry.methods.registerContent(ipfsHash).send({ from: account });
7. Accessing Content
Retrieve the IPFS hash from the blockchain and access the content.
- Call the
getContentHashfunction: Query the smart contract to get the IPFS hash associated with a specific address.// Example using Web3.js (simplified) const ipfsHash = await contentRegistry.methods.getContentHash(account).call(); - Fetch content from IPFS: Use the retrieved IPFS hash to download the content directly from the IPFS network.
ipfs cat ${ipfsHash} // using ipfs command line tool
8. Building a Web Interface
Create a simple web page with forms to upload files, register hashes and display content.
- Use HTML for the user interface.
- Use JavaScript (Web3.js or Ethers.js) to interact with the smart contract.
- Implement error handling and loading states.
Important Considerations
- Gas Costs: Registering content on the blockchain requires gas fees.
- IPFS Pinning: Ensure your content is pinned to prevent it from being garbage collected by IPFS nodes. Consider using a pinning service like Pinata or Infura.
- cyber security: Always audit smart contracts before deploying them to production. Be careful about the data you store on the blockchain, as it’s publicly visible.

