Integrating IPFS with Hyperledger Fabric Using IPFS Kubo
By Niraja.J.Shenoy, R & D Engineer (Training) at Kerala Blockchain Academy
In the blockchain world, storing data is a challenge. This becomes true in enterprise-level blockchains like Hyperledger Fabric, a private and permissioned blockchain. Fabric helps record transactions and maintain trust, but when it comes to managing large files, it faces limitations.
Imagine an enterprise using Hyperledger Fabric to manage supply chain transactions. Different types of transactions, like contracts, invoices, and quality certificates, will be included. Storing large files directly on the blockchain increases block size, reduces cost efficiency, and creates scalability challenges. Fabric needs a solution to handle extensive data efficiently. IPFS is the perfect partner for Fabric, solving the storage problem with its decentralised and content-addressed architecture.
Installing and Configuring Kubo for IPFS on Ubuntu
IPFS stands for InterPlanetary File System, which uses a communication protocol to store and share data securely using a distributed file system. IPFS employs content-addressed storage (CAS) to store and retrieve data based on its content rather than its location.
When data is uploaded to an existing node, the data is chopped into smaller chunks, a hash value of data is generated, and it is given a unique content identifier (CID) that serves as a fingerprint. This makes it faster and easier to store small data on the network.
Once the data is uploaded to a node, say Node A, other nodes in the network update their nodes to contain a cached copy of the data in Node A. All the nodes in the network can retain or discard the data based on their priority as a way to save memory.
For IPFS to be used in Go, we use Kubo. Go is a popular programming language used in Hyperledger Fabric primarily because it offers a range of features well-suited for developing blockchain platforms like the Concurrency Model with Goroutines, Performance and Efficiency, Ease of development, and Readability. Kubo is an implementation of the InterPlanetary File System (IPFS). Kubo is written in Go (hence its earlier name, go-ipfs) and is one of the most widely used IPFS implementations. In this article, we will be using Kubo in Ubuntu.
Download the binary required from the link based on the type of OS
Setting Up IPFS Kubo for Ubuntu
For Ubuntu
- Download the Linux binary file :
wget https://dist.ipfs.tech/kubo/v0.27.0/kubo_v0.27.0_linux-amd64.tar.gz
2. Unzip the file:
3. Move into the kubo folder:
4. Run the install script
5. Check Kubo has been installed correctly:
If Kubo installation is not initiated,
Initialise the repository
1. Open a terminal window.
2.Initialize the repository with the IPFS init command
Make your node online.
- Open a new terminal window.
- Start the IPFS daemon in the new terminal window:
- Switch back to your original terminal window.
- Run IPFS swarm peers to see the IPFS addresses of your peers:
Output similar to the following displays:
Next, create a file that contains the string ‘meow’ to add to your node using the echo command:
Add meow.txt using IPFS add:
Interact with the node using the web console
View the web console of a local node by navigating to localhost:5001/webui.
The web console shows files that are in the Mutable File System (MFS), a tool that helps to navigate IPFS files. A file added using the IPFS add command will not be available in MFS. When you add a file to IPFS using the ‘ipfs add’ command, it does not automatically become part of the Mutable File System (MFS) in IPFS because the ‘ipfs add’ command performs the low-level operation. It directly adds content to the IPFS network and generates a CID but does not link that content to the MFS.MFS operates separately, requiring explicit commands to add content to it.
To make it accessible, the following steps should be undertaken:
- Enter localhost: 5001/webui into your browser to view the web console.
- In the left sidebar menu, click Files. An empty directory displays, along with the following message:
“No files here yet! Add files to your local IPFS node by clicking the Import button above.”
- Navigate back to the original terminal window.
- Use the CID <CID> obtained when adding meow.txt to the node in the previous step to copy the files to the MFS.
For example, if the <CID> of meow.txt is QmabZ1pL9npKXJg8JGdMwQMJo2NCVy9yDVYjhiHK4LTJQH, it would be copied to the MFS with:
Viewing a file in the IPFS web console provides a more accessible, visual way to manage and verify the data. It’s helpful for debugging, sharing, and understanding how files interact with the IPFS network.
Integrating IPFS with Hyperledger Fabric
When the IPFS node is up and running, we can integrate it with Hyperledger Fabric. To add a file using the Hyperledger fabric, first bring up the fabric network and deploy the chaincode. The Steps to add a file using IPFS are listed below :
Step 1: Store Data on IPFS
Add Data to IPFS: Add file or data to IPFS using the command:
ipfs add <file_name>
Retrieve the File: Retrieve the file from IPFS using the CID to check if the file is available :
ipfs cat <CID>
Step 2: Store the IPFS CID in Hyperledger Fabric
Define a Smart Contract: Create a smart contract for the storage of CIDs
func(c *Contract)UploadToIPFS(ctx contractapi.TransactionContextInterface, filePath string) (string, error) {
// Connect to the IPFS daemon using the default address
fullPath := filepath.Join("/home/kba/go/Chaincode")
fmt.Printf("path is: %v", fullPath)
// Check if the file exists
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
return "", fmt.Errorf("file not found: %s", fullPath)
}
sh := shell.NewShell("localhost:5001")
// Open the file
file, err := os.Open(filePath)
if err != nil {
return "", fmt.Errorf("error opening file: %v", err)
}
defer file.Close()
// Add the file to IPFS
cid, err := sh.Add(file)
if err != nil {
return "", fmt.Errorf("error uploading file to IPFS: %v", err)
}
return cid, nil
}
The above function does the following
First, the user invokes this smart contract method, passing a file path, and then the file is validated and uploaded to IPFS. Finally, the CID is returned and potentially recorded in the blockchain ledger.
Deploy the Smart Contract: Deploy this smart contract to the Hyperledger Fabric network.
* If you are interested, enroll in our course to get the detailed steps. https://learn.kba.ai/course/hyperledger-fabric-fundamentals-golang/
Store the CID: Use a Fabric client application to store the CID in the blockchain.
Step 3: Retrieve the Data from IPFS
func(c *Contract)RetrieveFromIPFS(ctx contractapi.TransactionContextInterface,cidstring, destinationPath string) error {
if err := os.MkdirAll(filepath.Dir(destinationPath), 0755); err != nil {
return fmt.Errorf("error creating directory: %v", err)
}
// Create a new IPFS shell
sh := shell.NewShell("localhost:5001")
// Retrieve the file from IPFS using CID
err := sh.Get(cid, destinationPath)
if err != nil {
return fmt.Errorf("error retrieving file from IPFS: %v", err)
}
return nil
}
This function does the following. First, the user provides the CID of a file stored in IPFS to the smart contract. Then, the function connects to the IPFS node and fetches the file content using the CID. Finally, the file content is returned as a byte slice to the caller for further processing.
To stop IPFS, Use the command
ipfs shutdown
Conclusion
Integrating IPFS with Hyperledger Fabric using IPFS Kubo gives a flexible and scalable solution for managing data in a decentralized application. Kubo is most popular for implementing IPFS due to its performance, reliability, and ecosystem support combination. It’s a good choice for users and developers who need High performance and efficiency. Kubo provides the best balance of stability, features, and support, making it preferable over generic or alternative IPFS implementations.
Integrating the strengths of IPFS and Fabric simplifies the development of efficient and secure blockchain-based applications. This makes it well-suited for the Data-Intense use case.