IPFS
What is it?
IPFS is a decentralized protocol and peer-to-peer network that stores and shares hypermedia using content-addressing. It ensures global, permanent access to files by assigning unique cryptographic hashes as addresses, employs a distributed network for resilience, tracks file versions, organizes data with a Merkle DAG, supports offline sharing, and utilizes caching for efficient content retrieval. IPFS is widely used for decentralized file storage, web hosting, DApps, and ensuring data integrity in various domains, including blockchain and cryptocurrency.
Integrating IPFS with a Substrate-based Blockchain
Integrating IPFS with a Substrate-based blockchain allows for decentralized storage solutions in blockchain applications. This guide outlines the process to connect to IPFS, store data, generate a unique storage key, and interact with the blockchain to store and retrieve data CIDs.
Connect to IPFS
We will be using Helia to connect to a node. Helia gives developers the ability to run IPFS-compatible functionality without requiring a full IPFS daemon. It is a newer, modular, implementation for building IPFS-like networks. A main benefit of Helia is that it can operate in browser environments, enabling IPFS-based applications to run entirely in the browser without requiring an HTTP API connection to a remote IPFS node.
First we will need to install the libraries that Helia depends on. You can do so using npm with the following commands:
npm install helia
- Used to import the Helia package which is used in creating a new instance of a Helia node.npm install @helia/unixfs
- Installs the unixfs package from Helia that is used to create a filesystem.
Import the two packages previously downloaded, connect to the node, and create a filesystem for that particular node:
Next we provide a code snippet to show how to add data to the newly created Helia node:
The code snippet above encodes the string data into bytes and adds it to the filesystem (fs) initialized in the previous example. Take special note of the CID that is returned back after adding your data. That value will be needed when reading back from the node.
Retrieve data from Node
The next lines of code provides an example on how to read data back from the node using the cid that was returned previously:
Initialize Blockchain API Connection
Next we will need to install the libraries that the Substrate-based blockchain depends on. You can do so using npm with the following commands:
npm install @polkadot/api
- Used to import the websocket provider and api promise to communicate to the Substrate-based blockchain. Imports Keyring object to sign the write transaction to peaq storage.npm install @peaq-network/types
- Used to see the options that peaq offers in the types package.
In this example we will use the testnet url, agung, to connect to the Substrate blockchain using Polkadot.js API:
Add CID to peaqStorage
Store the previously created CID with an item_type name for the data to be added in peaq Storage:
Create a Unique Storage Key and Retrieve Data
Import the following libraries that the Substrate-based blockchain uses to decode and generate a key for storage.
npm install @polkadot/util-crypto
- Used to import the decodeAddress and blake2AsHex packages to successfully generate a storage key used in retrieving data from peaq Storage.npm install @polkadot/util
- Used to import polkadot-provided conversion mechanisms to read data from peaq Storage.
The following code utilizes the user’s address and data type to generate a unique key for identifying stored data on the blockchain. Then it fetches the corresponding data from IPFS:
Putting it all together
The code snippet provides an overview of how to use this functionality with everything put together:
Summary
This guide provides a foundational approach for leveraging IPFS with Substrate-based blockchains, emphasizing decentralized storage solutions and data integrity within blockchain applications. For any additional questions please see the Helia Documentation.