@kodex-data/hardhat-ipfs
v0.0.1-rc.3
Published
Hardhat TypeScript plugin for using IPFS
Downloads
30
Readme
Hardhat IPFS Documentation
Overview
Hardhat IPFS is a plugin for Hardhat, a popular development environment for Ethereum smart contracts. This plugin allows you to easily integrate IPFS (InterPlanetary File System) into your development workflow.
With Hardhat IPFS, you can upload and retrieve data to and from IPFS directly from your smart contracts or Hardhat tasks. This can be useful for various purposes such as storing large files, publishing metadata, or creating decentralized applications.
Installation
To install Hardhat IPFS, use the following command:
yarn add @kodex-data/hardhat-ipfs
# or
npm install @kodex-data/hardhat-ipfs
Configuration
Once you have installed Hardhat IPFS, you need to configure it by adding the following line to the top of your hardhat.config.ts
file:
import '@kodex-data/hardhat-ipfs';
This will register the IPFS plugin with Hardhat.
IPFS Instance Configuration
By default, the IPFS client will be configured to use Infura. If you want to use a different IPFS node, you can configure it in your hardhat.config.ts
file like so:
const config: HardhatUserConfig = {
ipfs: {
host: 'ipfs.example.com',
port: 5001,
protocol: 'https'
},
// ...
}
Replace ipfs.example.com
with the hostname of your IPFS node, and adjust the port
and protocol
settings as necessary.
Usage
Hardhat IPFS provides several utility functions that can be accessed through the hre.ipfs
object.
Adding Data to IPFS
To add data to IPFS, use the addFolder
function. This function takes a local file or directory path, and uploads it to IPFS. It returns an array of AddFolderResult
objects, each of which contains the CID of the added data.
const results = await hre.ipfs.addFolder('/path/to/data', '**/*')
console.log(results.map(r => r.cid.toString()))
The second argument to addFolder
is an optional glob pattern, which can be used to select specific files within the directory.
Retrieving Data from IPFS
To retrieve data from IPFS, use the getData
function. This function takes a CID or string, and returns the data stored in IPFS as a string.
const cid = 'QmWzHm1ZhymhMNUZTzT8WJ7RnRgTR9XhNEKv62Dahx2CYT'
const data = await hre.ipfs.getData(cid)
console.log(data)
Storing Data in IPFS as a DAG
To store data in IPFS as a DAG (directed acyclic graph), use the putDag
function. This function takes an object containing the data you want to store, and returns the CID of the stored DAG.
const data = { foo: 'bar' }
const cid = await hre.ipfs.putDag(data)
console.log(cid.toString())
Retrieving a DAG from IPFS
To retrieve a DAG from IPFS, use the getDag
function. This function takes a CID or string, and returns the DAG stored in IPFS as a GetResult
object.
const cid = 'QmWzHm1ZhymhMNUZTzT8WJ7RnRgTR9XhNEKv62