Skip to main content
IPFS is a peer-to-peer protocol for storing and sharing files by content address. Every file gets a content identifier (CID) derived from its bytes, and anyone holding that CID can fetch the file from any node that has it.

Store IPFS data on peaq

This guide adds data to IPFS, writes the returned CID to peaq storage, and reads the data back through that CID.

Connect to IPFS

This guide uses Helia, an IPFS implementation that runs without a full IPFS daemon and works in the browser as well as in Node. Install the two packages Helia needs:
  • npm install helia - creates a Helia node.
  • npm install @helia/unixfs - adds the unixfs filesystem on top of that node.
Import both packages, start a node, and create a filesystem for it:
Add data to the node:
The snippet encodes the string into bytes and adds it to the filesystem created above. Keep the returned CID: you need it to read the data back.

Retrieve data from the node

Read the data back with that CID:

Connect to peaq

Install the two packages the chain connection needs:
  • npm install @polkadot/api - websocket provider, API client, and the keyring used to sign the storage transaction.
  • npm install @peaq-network/types - peaq’s type definitions.
This example connects to the agung testnet:

Add the CID to peaq storage

Store the CID under an item type name:

Build the storage key and retrieve the data

Install the two packages used to build the storage key:
  • npm install @polkadot/util-crypto - decodeAddress and blake2AsHex.
  • npm install @polkadot/util - byte and hex conversions.
The storage key is the blake2 hash of your decoded address concatenated with the item type. Read the CID back with that key, then fetch the data from IPFS:

Putting it all together

The full script:
signAndSend without a status callback returns a transaction hash before inclusion. The fixed delay above is only a pause before reading. To wait for inclusion, use a signAndSend status callback, check dispatchError, and wait for status.isInBlock (or status.isFinalized for finality) before reading on-chain storage. For more Helia usage, see the Helia examples.