Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

multiformats

multiformats4.9mApache-2.0 OR MIT13.3.1TypeScript support: included

Interface for multihash, multicodec, multibase and CID

ipfs, ipld, multiformats

readme

multiformats

multiformats.io codecov CI

Interface for multihash, multicodec, multibase and CID

About

This library defines common interfaces and low level building blocks for various interrelated multiformat technologies (multicodec, multihash, multibase, and CID). They can be used to implement custom base encoders / decoders / codecs, codec encoders /decoders and multihash hashers that comply to the interface that layers above assume.

This library provides implementations for most basics and many others can be found in linked repositories.

import { CID } from 'multiformats/cid'
import * as json from 'multiformats/codecs/json'
import { sha256 } from 'multiformats/hashes/sha2'

const bytes = json.encode({ hello: 'world' })

const hash = await sha256.digest(bytes)
const cid = CID.create(1, json.code, hash)
//> CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)

Creating Blocks

import * as Block from 'multiformats/block'
import * as codec from '@ipld/dag-cbor'
import { sha256 as hasher } from 'multiformats/hashes/sha2'

const value = { hello: 'world' }

// encode a block
let block = await Block.encode({ value, codec, hasher })

block.value // { hello: 'world' }
block.bytes // Uint8Array
block.cid   // CID() w/ sha2-256 hash address and dag-cbor codec

// you can also decode blocks from their binary state
block = await Block.decode({ bytes: block.bytes, codec, hasher })

// if you have the cid you can also verify the hash on decode
block = await Block.create({ bytes: block.bytes, cid: block.cid, codec, hasher })

Multibase Encoders / Decoders / Codecs

CIDs can be serialized to string representation using multibase encoders that implement MultibaseEncoder interface. This library provides quite a few implementations that can be imported:

import { base64 } from "multiformats/bases/base64"
cid.toString(base64.encoder)
//> 'mAYAEEiCTojlxqRTl6svwqNJRVM2jCcPBxy+7mRTUfGDzy2gViA'

Parsing CID string serialized CIDs requires multibase decoder that implements MultibaseDecoder interface. This library provides a decoder for every encoder it provides:

CID.parse('mAYAEEiCTojlxqRTl6svwqNJRVM2jCcPBxy+7mRTUfGDzy2gViA', base64.decoder)
//> CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)

Dual of multibase encoder & decoder is defined as multibase codec and it exposes them as encoder and decoder properties. For added convenience codecs also implement MultibaseEncoder and MultibaseDecoder interfaces so they could be used as either or both:

cid.toString(base64)
CID.parse(cid.toString(base64), base64)

Note: CID implementation comes bundled with base32 and base58btc multibase codecs so that CIDs can be base serialized to (version specific) default base encoding and parsed without having to supply base encoders/decoders:

const v1 = CID.parse('bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea')
v1.toString()
//> 'bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea'

const v0 = CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')
v0.toString()
//> 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'
v0.toV1().toString()
//> 'bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku'

Multicodec Encoders / Decoders / Codecs

This library defines BlockEncoder, BlockDecoder and BlockCodec interfaces. Codec implementations should conform to the BlockCodec interface which implements both BlockEncoder and BlockDecoder. Here is an example implementation of JSON BlockCodec.

export const { name, code, encode, decode } = {
  name: 'json',
  code: 0x0200,
  encode: json => new TextEncoder().encode(JSON.stringify(json)),
  decode: bytes => JSON.parse(new TextDecoder().decode(bytes))
}

Multihash Hashers

This library defines MultihashHasher and MultihashDigest interfaces and convinient function for implementing them:

import * as hasher from 'multiformats/hashes/hasher'

const sha256 = hasher.from({
  // As per multiformats table
  // https://github.com/multiformats/multicodec/blob/master/table.csv#L9
  name: 'sha2-256',
  code: 0x12,

  encode: (input) => new Uint8Array(crypto.createHash('sha256').update(input).digest())
})

const hash = await sha256.digest(json.encode({ hello: 'world' }))
CID.create(1, json.code, hash)

//> CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)

Traversal

This library contains higher-order functions for traversing graphs of data easily.

walk() walks through the links in each block of a DAG calling a user-supplied loader function for each one, in depth-first order with no duplicate block visits. The loader should return a Block object and can be used to inspect and collect block ordering for a full DAG walk. The loader should throw on error, and return null if a block should be skipped by walk().

import { walk } from 'multiformats/traversal'
import * as Block from 'multiformats/block'
import * as codec from 'multiformats/codecs/json'
import { sha256 as hasher } from 'multiformats/hashes/sha2'

// build a DAG (a single block for this simple example)
const value = { hello: 'world' }
const block = await Block.encode({ value, codec, hasher })
const { cid } = block
console.log(cid)
//> CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)

// create a loader function that also collects CIDs of blocks in
// their traversal order
const load = (cid, blocks) => async (cid) => {
  // fetch a block using its cid
  // e.g.: const block = await fetchBlockByCID(cid)
  blocks.push(cid)
  return block
}

// collect blocks in this DAG starting from the root `cid`
const blocks = []
await walk({ cid, load: load(cid, blocks) })

console.log(blocks)
//> [CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)]

Legacy interface

blockcodec-to-ipld-format converts a multiformats BlockCodec into an interface-ipld-format for use with the ipld package. This can help bridge IPLD codecs implemented using the structure and interfaces defined here to existing code that assumes, or requires interface-ipld-format. This bridge also includes the relevant TypeScript definitions.

Implementations

By default, no base encodings (other than base32 & base58btc), hash functions, or codec implementations are exposed by multiformats, you need to import the ones you need yourself.

Multibase codecs

bases import repo
base16 multiformats/bases/base16 multiformats/js-multiformats
base32, base32pad, base32hex, base32hexpad, base32z multiformats/bases/base32 multiformats/js-multiformats
base64, base64pad, base64url, base64urlpad multiformats/bases/base64 multiformats/js-multiformats
base58btc, base58flick4 multiformats/bases/base58 multiformats/js-multiformats

Other (less useful) bases implemented in multiformats/js-multiformats include: base2, base8, base10, base36 and base256emoji.

Multihash hashers

hashes import repo
sha2-256, sha2-512 multiformats/hashes/sha2 multiformats/js-multiformats
sha3-224, sha3-256, sha3-384,sha3-512, shake-128, shake-256, keccak-224, keccak-256, keccak-384, keccak-512 @multiformats/sha3 multiformats/js-sha3
identity multiformats/hashes/identity multiformats/js-multiformats
murmur3-128, murmur3-32 @multiformats/murmur3 multiformats/js-murmur3
blake2b-*, blake2s-* @multiformats/blake2 multiformats/js-blake2

IPLD codecs (multicodec)

codec import repo
raw multiformats/codecs/raw multiformats/js-multiformats
json multiformats/codecs/json multiformats/js-multiformats
dag-cbor @ipld/dag-cbor ipld/js-dag-cbor
dag-json @ipld/dag-json ipld/js-dag-json
dag-pb @ipld/dag-pb ipld/js-dag-pb
dag-jose dag-jose ceramicnetwork/js-dag-jose

Install

$ npm i multiformats

Browser <script> tag

Loading this module through a script tag will make its exports available as Multiformats in the global namespace.

<script src="https://unpkg.com/multiformats/dist/index.min.js"></script>

API Docs

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

changelog

13.3.1 (2024-10-29)

Trivial Changes

  • adjust examples for new linting rules (adc534b)

Dependencies

  • dev: bump aegir from 44.1.4 to 45.0.0 (1cd48bf)

13.3.0 (2024-09-16)

Features

13.2.4 (2024-09-16)

Trivial Changes

Dependencies

  • dev: bump aegir from 43.0.3 to 44.1.1 (a0fdb76)

13.2.3 (2024-09-12)

Bug Fixes

13.2.2 (2024-07-30)

Dependencies

  • dev: bump @types/node from 20.14.13 to 22.0.0 (2b65321)

13.2.1 (2024-07-24)

13.2.0 (2024-07-22)

Features

  • BlockCodec methods are generic (2ee062e)

13.1.3 (2024-07-02)

Dependencies

  • dev: bump @stablelib/sha512 from 1.0.1 to 2.0.0 (3091935)

13.1.2 (2024-07-02)

Dependencies

  • dev: bump @stablelib/sha256 from 1.0.1 to 2.0.0 (bc7f8a5)

13.1.1 (2024-06-01)

Dependencies

  • dev: bump aegir from 42.2.11 to 43.0.1 (bc14c48)

13.1.0 (2024-02-15)

Features

Trivial Changes

  • Update .github/dependabot.yml [skip ci] (aa9c730)

13.0.1 (2024-01-10)

Dependencies

  • dev: bump aegir from 41.3.5 to 42.1.0 (12c8686)

13.0.0 (2023-12-20)

⚠ BREAKING CHANGES

  • this module is now TypeScript - the API has not changed but releasing as a major for saftey

Features

Trivial Changes

  • deps: bump actions/setup-node from 3.8.2 to 4.0.0 (139b3c2)

12.1.3 (2023-10-25)

Trivial Changes

  • deps: bump actions/setup-node from 3.8.1 to 3.8.2 (f190ad7)

Dependencies

  • dev: bump crypto-hash from 2.0.1 to 3.0.0 (f5b9958)

12.1.2 (2023-10-03)

Bug Fixes

  • switch interface method decl style (a33d24f)

Dependencies

  • dev: bump aegir from 40.0.13 to 41.0.0 (41f008b)

12.1.1 (2023-09-05)

Bug Fixes

  • update link interface path in exports map (#270) (d38e4a8)

Trivial Changes

  • deps: bump actions/checkout from 3 to 4 (f94559e)

12.1.0 (2023-08-28)

Features

12.0.2 (2023-08-28)

Bug Fixes

Trivial Changes

  • add or force update .github/workflows/js-test-and-release.yml (21b7591)
  • delete templates [skip ci] (#263) (d2b614d)
  • deps: bump actions/setup-node from 3.5.0 to 3.8.0 (7dcf225)
  • deps: bump actions/setup-node from 3.8.0 to 3.8.1 (d7ec85c)
  • deps: bump gozala/typescript-error-reporter-action (4a36fb7)
  • Update .github/dependabot.yml [skip ci] (58bebda)

Dependencies

  • dev: bump aegir from 37.12.1 to 40.0.11 (d17424d)

12.0.1 (2023-06-15)

Trivial Changes

  • deps: bump codecov/codecov-action from 3.1.1 to 3.1.4 (#256) (910eeeb)

Dependencies

  • dev: bump @types/node from 18.16.18 to 20.3.1 (7a6d036)

12.0.0 (2023-06-14)

⚠ BREAKING CHANGES

  • use aegir for ESM-only build/testing/release

Features

  • use aegir for ESM-only build/testing/release (f82e61b)

11.0.2 (2023-03-09)

Bug Fixes

11.0.1 (2023-01-18)

Bug Fixes

  • throw on CID.parse v0 string with multibase prefix (258a0be)

11.0.0 (2023-01-02)

⚠ BREAKING CHANGES

  • Make link.toJSON return a DAG-JSON link

Features

  • Make link.toJSON return a DAG-JSON link (9e087d6)

Bug Fixes

Documentation

  • fix typos in jsdoc comments (a246054)

10.0.3 (2022-12-16)

Documentation

10.0.2 (2022-10-19)

Bug Fixes

Trivial Changes

  • no-release: rename varint test file so it is run (#209) (e32fe47)
  • remove unnecessary dev deps (#218) (a43ffff)

10.0.1 (2022-10-17)

Bug Fixes

10.0.0 (2022-10-12)

⚠ BREAKING CHANGES

  • remove use of Object.defineProperties in CID class
  • use aegir for ESM-only build/testing/release

Features

  • add complete set of aegir-based scripts (1190bc6)
  • define Link interface for CID (88e29ea)
  • remove deprecated CID properties & methods (ffc4e6f)
  • use aegir for ESM-only build/testing/release (163d463)

Bug Fixes

Tests

  • check for non-enumerability of asCID property (b4ba07d)

Trivial Changes

  • add test for structural copying (#206) (e8def36)
  • no-release: bump @types/mocha from 9.1.1 to 10.0.0 (#205) (a9a9347)
  • no-release: bump actions/setup-node from 3.4.1 to 3.5.0 (#204) (604ca1f)

9.9.0 (2022-09-20)

Features

  • add optional offset param to varint.decode (#201) (1e1b583)

9.7.1 (2022-07-26)

Bug Fixes

Trivial Changes

  • no-release: bump actions/setup-node from 3.3.0 to 3.4.0 (#189) (362b167)
  • no-release: bump actions/setup-node from 3.4.0 to 3.4.1 (#190) (67f22c4)

9.7.0 (2022-06-23)

Features

Trivial Changes

  • no-release: bump @types/node from 17.0.45 to 18.0.0 (#188) (99e94ed)
  • no-release: bump actions/setup-node from 3.1.0 to 3.2.0 (#182) (86ec43d)
  • no-release: bump actions/setup-node from 3.2.0 to 3.3.0 (#186) (712c1c4)
  • no-release: fix typo implemnetation -> implementation (#184) (3d4ae50)

9.6.5 (2022-05-06)

Trivial Changes

  • no-release: bump actions/checkout from 2.4.0 to 3 (#172) (a1b38c2)
  • no-release: bump actions/setup-node from 2.5.1 to 3.0.0 (#169) (8deb4d5)
  • no-release: bump actions/setup-node from 3.0.0 to 3.1.0 (#174) (9bcd7fe)
  • no-release: bump mocha from 9.2.2 to 10.0.0 (#179) (b2951dc)
  • no-release: bump polendina from 2.0.15 to 3.0.0 (#180) (659516b)
  • no-release: bump standard from 16.0.4 to 17.0.0 (#178) (2683344)
  • update tsdoc for CID code param to clarify "what kind of code?" (#181) (adec0c4)

9.6.4 (2022-02-14)

Trivial Changes

  • clean typos and formatting (0d976fd)

9.6.3 (2022-02-04)

Bug Fixes

  • run test:ci in CI, fix package.json keywords (#139) (8ec8eb0)

9.6.2 (2022-01-20)

Bug Fixes

9.6.1 (2022-01-20)

Bug Fixes

  • export only identity hasher const (330082a)

9.6.0 (2022-01-19)

Features

9.5.9 (2022-01-18)

Trivial Changes

  • Reanable tsc action and reconfigure project slightly (#157) (c936a6d)

9.5.8 (2022-01-07)

Trivial Changes

  • test: use chai throws() & chai-as-promised isRejected() (6e4ba86)

9.5.7 (2022-01-07)

Bug Fixes

  • types: combine composite tsconfigs (18c5734)

Trivial Changes

  • types: re-enable typechecks for tests by split tsconfig (4c017dc)
  • types: remove explicit typecheck action (b0467e5)

9.5.6 (2022-01-04)

Bug Fixes

  • types: fix publishing of types (58b5604), closes #150

9.5.5 (2022-01-04)

Bug Fixes

Trivial Changes

  • no-release: bump @types/node from 16.11.14 to 17.0.0 (#145) (66aaf0f)
  • no-release: bump actions/setup-node from 2.5.0 to 2.5.1 (#147) (32cf7bd)

9.5.4 (2021-12-09)

Bug Fixes

9.5.3 (2021-12-09)

Bug Fixes

  • add windows CI support (replace hundreds with c8 direct) (5da242c)
  • ipjs windows fix, add windows back in to CI (196e404)
  • prepare auto-release from dist dir & w/ build (90693dd)

Trivial Changes

  • drop test support for 12.x (a7a2110)
  • remove windows from CI pending ipjs fix (2ab914b)
  • update auto-release work w/ semantic-release (db86f48)
  • update devDeps (55b9856)
  • upgrade polendina, test esm & cjs in browser (852f1a5)