新来的?首次访问区块链节点即可享受 30% 折扣!

banner image

What is the IPFS (InterPlanetary File System)? How it works and how to use IPFS? 

Vance Wood

Vance Wood

September 4, 2023

36 分鐘閱讀

article cover

Guide to IPFS: How to use Decentralized Data Storage with Ethereum

As we’re headed towards decentralized everything with the help of blockchain, it is only within the principles of Web3 to have a solid decentralized file storage solution where an Interplanetary File System is a fundamental part.

This IPFS tutorial will introduce you to the IPFS basics and provide tips on how to use IPFS to enhance the Ethereum dApp development experience.

Key takeaways

  • Similar to how torrents work, IPFS protocol serves for hosting data and transporting it peer-to-peer without relying on centralized servers;
  • The protocol offers a new way of addressing content consumed over the Internet by making files findable based on the actual contents rather than their location;
  • Anyone can download the dedicated software and start leveraging the technology for personal use;
  • Decentralized storage tools are becoming more significant over time seeing the rising interest in non-fungible tokens and a way to store them securely;
  • Ethereum developers benefit from IPFS by hosting dApp data in decentralized repositories instead of a rather costly alternative of keeping it on the blockchain.

What is IPFS?

IPFS, Interplanetary File System, is a protocol enabling various servers across the globe to talk to each other, store, and exchange files with no middlemen involved. Everyone familiar with BitTorrent may have grasped the concept by now.

Traditional web built on HTTP continuously gets criticized for being subject to censorship by governments and hosting providers, let alone DDoS attacks and server outages. Protocol Labs, the company behind the IPFS core technology, is developing a cryptographically secured decentralized web browsing method as an alternative to HTTP protocol.

Client-server_vs_P2P_models

Image by IPFS

Accordingly, any type of file that we normally store on HTTP can be accommodated within IPFS. In contrast to the traditional web, however, there’s no need to contact remote centralized servers to store or obtain that content.

How does IPFS work?

Similarly to the blockchain, the Interplanetary File System network consists of nodes or local computers. However, unlike the blockchain, these nodes refer to computers that run the IPFS software and only host content they are willing to keep.

RELATED: https://getblock.io/blog/what-is-a-blockchain-node-and-how-does-it-work/

Let’s now address the key features that make decentralized content sharing the strongest rival of HTTP:

Content detecting
Opposite to how URLs work, data on IPFS is findable by the actual content regardless of its location. As long as that content is living on the node somewhere on IPFS, it will be served by its CID.

IPFS & crypto algorithms
Every piece of content registered to IPFS is put through hashing and given a Content ID (CID) — a unique hash. To share the file with the world, simply spread the hash. One CID always refers to the same content because of the cryptographic relationship with the original data.

Verifiable data
Every minor edit to the content completely changes its hash or CID. This feature of IPFS comes with several benefits. First, it removes the need for duplication since identical content will return the same identifier. On top of this, we can be certain that the data behind a specific CID will never be replaced.

File pinning
Simply uploading the file to IPFS and retrieving its hash will not by default start serving it to other peers on the network. The “pin” option applied to a file communicates to other nodes that you have this CID and can share it upon request.

While the underlying concept of IPFS is nothing new, as we can see, the way the protocol was implemented makes it a technology to pay attention to.

How to explore IPFS data?

The unique hash string is the key to finding any file on the IPFS network. When initiating the request by CID, nodes communicate in order to locate the required data. Imagine it as if you’re asking your friends who has that meme or video you want to share with somebody else.

Picking the method of scanning IPFS depends on whether you choose to set up the node on your computer.

Public gateways allow navigating the decentralized storage without running the node. Multiple IPFS Gateways such as ipfs.io and cloudflare-ipfs.com route HTTP requests to multiple IPFS servers to provide you viewer mode access.

Such requests can be performed via any web browser in the following formats depending on the gateway: https://ipfs.io/ipfs/[CID], https://cloudflare-ipfs.com/ipfs/[CID].

While gateways help effectively bridge Web2 and Web3, be aware that using this method is associated with trusting third parties to fetch the content for you, hence it doesn’t stay true to the peer-to-peer principle.

Direct access to IPFS via your own node

Turning your computer into a node is as easy as downloading the desktop application – we’ll guide you through the process further in this article. The desktop application will return the desirable result if you just browse the CID inside the app:

IPFS_Desktop_Search_Bar

Image from IPFS Desktop

Other than that, popular browsers support the official IPFS Companion extension to view files in Chrome or Firefox and redirect them to your node provided you have the application set up.

An alternative way of hosting your own IPFS server is using Brave and Opera browsers. While by default they interact with public gateways to fetch the content, there’s an option to set up a local node in any of these browsers.

Brave_IPFS_support

Image by Brave

In this case, the node will run behind the scenes so that users can directly access files from a search bar by submitting the “ipfs://” prefix followed by a CID. An example would be ipfs://QmaoWVmfgfpayKvaSEaAuu6YDwkUz8Fr9easDchoPsKuxT.

Why use IPFS?

Having reviewed all the details of the technology behind IPFS, let’s now summarize it in three benefits that make it an ahead-of-the-curve technology compared to HTTP.

1) No single point of failure
In the traditional web, we access data from websites via URLs, which point to a server where content is actually stored. Corporations managing these servers have centralized control over every scrap of data delegated to them and can singlehandedly cut off access to websites. More to this, one server may have a hard time handling heavy traffic.

Instead, the Interplanetary File System consists of many servers independent of governments or intermediaries. While several nodes may go down, information is still retrievable if there’s at least one node serving the file.

2) Connection speeds
Since no middlemen are involved in data exchange with IPFS, sending files directly from one node to another significantly speeds up the process. Additionally, the protocol is capable of scanning the network to find multiple nearest nodes with desirable content, which prevents the bottleneck problem.

3) Security
The main idea of IPFS is to let people host their own data securely, which is guaranteed by cryptographic hashing. There’s no way that one CID will refer to different content, so you can always check the CID provided to you and the unique identifier of the file received to prove that data is not tempered. Besides, having multiple nodes instead of one hub makes IPFS immune to data breaches.

Undoubtedly, the client-server model of HTTP revolutionized the way we use the Web. Yet, the Internet we know today is increasingly associated with several problems, which only emphasizes the need for proper decentralized solutions.

IPFS Tutorial: How to Deploy a Local Node

Spinning up an IPFS node on your local computer is freely available and straightforward and yet will allow you to benefit from all of the advantages the technology has to offer. This tutorial will show how to start using IPFS via a desktop client or command line.

Desktop App: most user-friendly way

IPFS Desktop is the most convenient and beginner-friendly tool that will deploy a local IPFS node on your computer upon installation and enable sharing and reading files. Follow our lead and set up your server in a few clicks:

  1. Download the package for Windows, macOS, or Linux from the IPFS GitHub repository and install the IPFS desktop application. Your IPFS node is immediately connected and ready to use!
IPFS_Desktop

Image from IPFS Desktop

  1. Tap “Files” on the sidebar, press “Import”, and pick a desired doc from a window that pops up.
How_to_import_files_to_IPFS

Image from IPFS Desktop

  1. When the upload completes, you will see the unique CID generated for your file. Press the push pin icon on the right to make your imports discoverable by other participants.
How_to_pin_files_to_IPFS

Image from IPFS Desktop

  1. To send the file, navigate the three-dot icon to copy the CID or find a shareable link. To retrieve files shared with you, navigate to the search bar on the top and enter the Content ID.
How_to_share_IPFS_files

Image from IPFS Desktop

Now you are all equipped to start the decentralized web browsing journey.

IPFS for power users

Kubo, the Golang implementation of IPFS, and Helia written in JavaScript are geared towards command-line interface usage. To illustrate, let’s use a Go client for deploying the IPFS node on macOS and interact with it.

  1. Head to the Kubo download page. Install and untar the package.
  2. In the terminal, navigate to the Kubo directory: cd Kubo
  3. To boot up the node, use the following command: ipfs init
  4. Make a test request and check if the node is launched:

ipfs cat /ipfs/QmS4ustL54uo8FzR9455qaxZwuMiUhyvMcX9Ba8nUH4uVv/readme

If successful, the welcome message will show up, meaning we can proceed. Try opening other files in the same folder that will guide you through the key functions.

Kubo_directory_IPFS_cat

Image from Kubo CLI

For example, we already used the ipfs cat command to read the file above, some other functions will be:

  • Create the file: echo “your text here” >[name the file]
  • Move it to your local node: ipfs add [file name]
  • Let your node share the locally hosted content with others: ipfs daemon

Utilizing the CLI version of IPFS opens access to an extensive API library that can be customized and configured to suit various use cases and therefore come in handy for developers to integrate IPFS with their applications.

IPFS Use Cases for Ethereum Blockchain

Ethereum, a smart-contract powerhouse of crypto, is the cozy home for NFTs and multitudes of decentralized applications. One thing to know about Ethereum-based apps and non-fungible tokens is that application files and NFT raw data are not normally stored on-chain for several reasons.

The ETH chain is not designed to handle large amounts of data, and even if in theory you could do that, gas fees will be enormous. One popular way of safekeeping that data is via centralized cloud hosting services like AWS or Google Cloud, which do not look reliable. Should the centralized server storing that data disappear the NFT becomes pointless.

Here is where blockchain and IPFS can complement each other nicely.

Powering Ethereum NFTs

NFTs minted on the blockchain have a reference to a metadata file and an actual image that is kept off-chain. Storing this data on IPFS instead of centralized servers might be a smarter solution. How it works:

  • URL link that leads to an asset included in NFT’s metadata references CID;
  • IPFS helps ensure that the token was never tempered since CID will completely change if even one pixel of that jpeg is altered;
How_to_find_NFT_metadata

Image from OpenSea

Similarly, take the responsibility of storing your purchased NFTs by pinning CID on your local node to make sure it’s not going anywhere. You can also use free tools in the Protocol Labs suite such as Nft.storage and Web3.storage to simplify the process.

RELATED: https://getblock.io/blog/how-to-create-an-nft-collection-five-easy-steps/

Decentralized storage for dApps

Many Ethereum applications leverage IPFS tech to store application files in a decentralized way by sending hashes to smart contracts instead of putting whole chunks of data into the chain.

See the image below to get a glimpse of how IPFS and Ethereum smart contracts work combined:

Integrating_IPFS_into_Smart_Contracts

Image Source

Here's an example code snippet for importing IPFS files into a Solidity smart contract:

pragma solidity ^0.8.0;

contract IPFSExample {
    function importFileFromIPFS(string memory ipfsHash) public {
        // Connect to IPFS HTTP API and fetch the file
        // Convert the file to a byte array

        // Assuming you have fetched the file and converted it to a byte array called 'fileData'
        // Pass the byte array to a smart contract function
        yourSmartContractFunction(fileData);
    }

    function yourSmartContractFunction(bytes memory fileData) private {
        // Process the file data here
        // Store it or perform any other operations you require
    }
}

To use this code, you need to replace yourSmartContractFunction with the actual function in your smart contract that handles the file import. Note that you might need to use a library like ipfs-http-client in your backend code to be able to perform the function.

IPFS Costs and Expenses

Provided you already have the hardware with common storage capacities you use daily and a reliable internet connection set up, utilizing IPFS-based storage will bear no extra costs. To guarantee the availability and longevity of your IPFS files, try so-called “pinning” services that spread your content to multiple nodes for a fee.

One prominent example of this type of service is the native Protocol Labs tool Filecoin. The platform will help find servers ready to pin your data for a specified period of time in exchange for rewards in Filecoin native tokens, which will approximately cost as low as 20 cents per month.

Additionally, here’s the table comparing prices of storing data directly on the Ethereum chain vs storing hashes on-chain:

On-chain data storage Storing 1 IPFS hash on-chain
Ethereum Gas per 32 Byte (256 Bit) Slot 21,000 gas
Average Gas Price ≈ 36 gwei
Stored Size > 32 Bytes 32 Bytes
Final Price in ETH 1 Kb = (32 x 21,000 gas) = 672,000 gas

672,000 gas = 24,192,000 gwei = 0.024 ETH
21,000 gas

756,000 gwei = 0.0008 ETH
Final Price in USD ≈ 40 USD per Kb ≈ 1.35 USD

IPFS, Ethereum, and GetBlock to streamline Web3 development

While you let IPFS safely store the Ethereum application data off-chain, it’s time to take care of the dApp infrastructure.

GetBlock, a time-tested RPC endpoints provider, offers services to connect Web3 applications to the Ethereum blockchain in a few short clicks.

Explore how to start using blockchain data with GetBlock’s ETH RPCs in this video:

VIDEO TO EMBED: https://www.youtube.com/watch?v=NYGpLFLFzFI

And better yet, grab the free package and test it yourself: https://account.getblock.io/

Context Banner

Closing thoughts

While the traditional HTTP-based web structure is well-defined and widely adopted, using IPFS hasn’t yet been standardized on the World Wide Web, which sets certain barriers to entry for general public use. Nevertheless, the transparency and resilience of data are the major reasons why internet users and Web3 developers are leaning towards IPFS today.

Building on Ethereum? Let GetBlock handle the infrastructure and secure the high performance of your dApps: https://account.getblock.io/.

Try it out and invite friends who may also find it useful for a nice bonus: https://getblock.io/affiliate-program/

常问问题

  • What is IPFS?

    plus
  • Is IPFS a blockchain?

    plus
  • Can I use IPFS with Ethereum?

    plus
  • How can I add files to IPFS and retrieve them later?

    plus
Vance Wood

Vance Wood

September 4, 2023

36 分鐘閱讀

twittertwittertelegramtelegramLinkedinLinkedin