You may have heard about IPFS before, the Interplanetary File System. The concept has existed for quite some time now, but with IPFS you'll get a more reliable data storage, through content addressing and peer-to-peer protocols. IPFS itself is not a blockchain. Filecoin is a separate network that provides storage incentives.
If you are participating in the HackFS, a 2020 hackathon sponsored by ETHGlobal and Protocol Labs (the makers of IPFS), or not, knowing IPFS will be a useful skill for your Dapp developments.
Can't you just store all data in the Ethereum blockchain directly?
Excellent question and in theory yes. But remember that data in the Ethereum blockchain is shared between every single node. This is extremely inefficient for large sets of data. One alternative is to keep file contents off-chain and put a content identifier on-chain. IPFS nodes retain content they add, pin or cache; the network does not randomly assign files to storage nodes.
How does it work?
The core pieces include content identifiers, peer-to-peer transfers and content routing. The Kademlia-based DHT helps find peers advertising content; it does not itself distribute or store the files. Nodes then request blocks from providers.
IPNS provides mutable names that can point to changing content identifiers. Filecoin provides a separate storage market. IPFS is an open protocol, but running nodes or paying a pinning service still has costs. File access is not protected by built-in private file permissions; encrypt confidential content before sharing it.
Is it secure and reliable?
In short no, but it depends what we mean with 'reliable'. You cannot store a file in IPFS, then try to read it two years later and still expect it to be there. Nodes will garbage collect unused files. Popularity may increase caching, but it is not a retention guarantee. Pin important content on maintained nodes or services, keep backups and test retrieval after the original uploader is offline. And is it secure? Data is all public, so if you are concerned about privacy, you need to encrypt the files.
Adding IPFS to Solidity contracts
The Solidity part will be very simple. We will only store the IPFS hash of users inside our contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.11;
contract IpfsStorage {
mapping (address => string) public userFiles;
function setFile(string memory file) external {
userFiles[msg.sender] = file;
}
}The linked Kovan deployment belongs to the original 2020 example. Kovan is retired; use a currently supported test network for a fresh deployment, and check the compiler and frontend dependency versions together.
yarn workspace @project/react-app add ipfsThe full Javascript IPFS documentation is available here. Be aware that it's still very much work in progress.
Okay, now let's initialize IPFS. It creates and connects an IPFS node that we can use to read and upload files.
JavaScript compatibility: The following snippets are the old js-IPFS API. That project is deprecated. For a new frontend, use Helia and its current content APIs; simply changing the package name does not migrate IPFS.create, node.add or the CID result shape.
import IPFS from "ipfs";
let node; // shared with the historical upload example
async function initIpfs() {
node = await IPFS.create();
const version = await node.version();
console.log("IPFS Node Version:", version.version);
}Now that we have the IPFS node, let's add a function to read the current file from the contract.
We use the ethers.js library with our current address.
async function readCurrentUserFile() {
const result = await ipfsContract.userFiles(
defaultProvider.getSigner().getAddress()
);
return result;
}Let's upload a file to IPFS and store it in our contract.
- uploadFile: This will be the function to take a file and upload it to IPFS using our IPFS node.
setFile: After a successful upload, we can store the IPFS hash inside our contract using this function.
async function setFile(hash) {
const ipfsWithSigner = ipfsContract.connect(defaultProvider.getSigner());
await ipfsWithSigner.setFile(hash);
setIpfsHash(hash);
}
async function uploadFile(file) {
const files = [{ path: file.name + file.path, content: file }];
for await (const result of node.add(files)) {
await setFile(result.cid.string);
}
}Now let's add a nice drag zone for the user to upload a file in the browser. And put it all together.
Please check out the live demo and repository for the full example.
- Repository: https://github.com/gorgos/IPFS-Dapp
- Live Demo: https://ipfs-dapp.netlify.app/

Some alternatives
- Swarm: Very similar to IPFS, you can use it the same way. It has a bigger focus on Ethereum relying on smart contract incentives. However, it's not quite as matured yet.
- SKALE Filestorage: Side chains are effectively using a subset of Ethereum nodes allowing for more data to be stored inside them. Skale made use of that and introduced their own file-storage sytem. SKALE was launched to mainet only a few weeks ago, so you can go ahead and try it.
The launch timing and maturity comparisons above describe 2020. They are not current service recommendations or guarantees of data retention.




Join the conversation
Comments are hosted by Disqus and load only when you choose to enable them.