Using IPFS with Caver

Tech at Klaytn
Klaytn
Published in
3 min readMay 21, 2021

--

See the list of articles here.
🇰🇷: Caver를 이용한 IPFS 사용법

From Caver v1.5.5 onwards, you can store and retrieve data with IPFS (InterPlanetary File System). In this post, we will explain how to use IPFS with Caver.

You can find the complete code used in the tutorial in the links below:

Initializing IPFS

In order to use IPFS with Caver, you need to be connected with the IPFS node.

You can use the caver.ipfs.setIPFSNode function to set up the IPFS node information. And then you can upload and download files to and from IPFS.

We will first explain how to initialize using caver-js.

// Initialize connection with an IPFS Node — caver-js
caver.ipfs.setIPFSNode(‘IPFS Node URL’, 5001, true)

The first parameter to be passed is the url of an IPFS node, and the second is its port. For the last parameter, define the protocol that you will be using. If the last parameter passed is true, https protocol will be used, and http if false.

caver-java also uses the caver.ipfs.setIPFSNode function with the same parameters.

// Initialize connection with an IPFS Node — caver-java
caver.ipfs.setIPFSNode(“IPFS Node URL”, 5001, true);

Uploading Files to IPFS

If you are connected to the IPFS node, you can start uploading files to IPFS using the caver.ipfs.add function. With Caver, there are two ways to upload files.

  • Pass the path of the file to be uploaded as parameter
  • Pass the file contents as parameter

For the former, you pass the path of the file for its contents to be uploaded on IPFS. For the latter, the byte buffer will directly be uploaded on IPFS. When you successfully upload the file on IPFS, CID (Content Identifier) will be returned as a result.

Here is how you upload the file path on IPFS using caver-js:

// Upload a file to IPFS with a file path — caver-js
const cid = caver.ipfs.add(‘./test.txt’)

Also for caver-java, you pass the file path as a parameter to the caver.ipfs.add function.

// Upload a file to IPFS with a file path — caver-java
String cid = caver.ipfs.add(“./test.txt”);

Below, we will demonstrate how to directly upload file contents to IPFS.

First, on how to use caver-js. When directly uploading file contents, caver-js uses buffer type.

// Upload a file to IPFS with a file content — caver-js
const cid = caver.ipfs.add(Buffer.from(‘test data’))

On the other hand, caver-java uses byte array.

// Upload a file to IPFS with a file content — caver-java
String text = “test data”;
byte[] data = text.getBytes();
String cid = caver.ipfs.add(data);

Downloading Files from IPFS

You can download a file uploaded on IPFS using the caver.ipfs.get function. Pass the CID to download as a parameter.

Below example shows how to download data stored on IPFS using caver-js. Caver-js returns the data in buffer type.

// Download a file from IPFS — caver-js
const cid = ‘Qmd9thymMS6mejhEDZfwXPowSDunzgma9ex4ezpCSRZGwC’
const content = caver.ipfs.get(cid)

And the below sample code shows how to download data stored on IPFS using caver-java. Caver-java returns data in byte array type.

// Download a file from IPFS — caver-java
String cid = “QmYzW1fXbapdxkZXMQeCYoDCjVc18H8tLfMfrxXRySmQiq”;
byte[] content = caver.ipfs.get(cid);

Converting between CID and multihash

Caver provides a function for converting the CID to a hexadecimal multihash, or a multihash to CID.

Below is when you are using caver-js.

// Convert a CID to multihash — caver-js
const cid = ‘Qmd9thymMS6mejhEDZfwXPowSDunzgma9ex4ezpCSRZGwC’
const multihash = caver.ipfs.toHex(cid)

For caver-java, use caver.ipfs.toHex as shown below.

// Convert a CID to multihash — caver-java
String cid = “QmYtUc4iTCbbfVSDNKvtQqrfyezPPnFvE33wFmutw9PBBk”;
String multihash = caver.ipfs.toHex(cid);

You can record the hexadecimal multihash that was converted with toHex on the Klaytn Blockchain via a transaction.

Now let’s look at how to convert hexadecimal multihash into CID.

The below example shows how to convert multihash to CID using caver.ipfs.fromHex of caver-js.

// Convert a multihash to CID — caver-js
const multihash = ‘0x1220dc1dbe0bcf1e5f6cce80bd3d7e7d873801c5a1732add889c0f25391d53470dc3’
const cid = caver.ipfs.fromHex(multihash)

And caver-java uses the caver.ipfs.fromHex function to convert multihash to CID.

// Convert a multihash to CID — caver-java
String multihash = “0x1220dc1dbe0bcf1e5f6cce80bd3d7e7d873801c5a1732add889c0f25391d53470dc3”;
String cid = caver.ipfs.fromHex(multihash);

The complete code used in the tutorial

--

--