{"id":13837636,"url":"https://github.com/multiformats/js-multiformats","last_synced_at":"2025-05-14T14:08:53.958Z","repository":{"id":37184182,"uuid":"257753597","full_name":"multiformats/js-multiformats","owner":"multiformats","description":"Multiformats interface (multihash, multicodec, multibase and CID)","archived":false,"fork":false,"pushed_at":"2025-05-08T17:27:12.000Z","size":2133,"stargazers_count":253,"open_issues_count":42,"forks_count":53,"subscribers_count":17,"default_branch":"master","last_synced_at":"2025-05-08T17:46:09.360Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/multiformats.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-04-22T00:58:33.000Z","updated_at":"2025-05-08T17:26:55.000Z","dependencies_parsed_at":"2023-10-03T05:44:04.120Z","dependency_job_id":"7aa36bba-db9f-4eec-8ef3-860dc00fac6e","html_url":"https://github.com/multiformats/js-multiformats","commit_stats":{"total_commits":363,"total_committers":30,"mean_commits":12.1,"dds":0.7575757575757576,"last_synced_commit":"75712e79cee565e2f24977c19b8d269617e1a24a"},"previous_names":[],"tags_count":155,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multiformats%2Fjs-multiformats","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multiformats%2Fjs-multiformats/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multiformats%2Fjs-multiformats/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/multiformats%2Fjs-multiformats/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/multiformats","download_url":"https://codeload.github.com/multiformats/js-multiformats/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253141534,"owners_count":21860542,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T15:01:18.060Z","updated_at":"2025-05-14T14:08:48.948Z","avatar_url":"https://github.com/multiformats.png","language":"TypeScript","readme":"# multiformats\n\n[![multiformats.io](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://multiformats.io)\n[![codecov](https://img.shields.io/codecov/c/github/multiformats/js-multiformats.svg?style=flat-square)](https://codecov.io/gh/multiformats/js-multiformats)\n[![CI](https://img.shields.io/github/actions/workflow/status/multiformats/js-multiformats/js-test-and-release.yml?branch=master\\\u0026style=flat-square)](https://github.com/multiformats/js-multiformats/actions/workflows/js-test-and-release.yml?query=branch%3Amaster)\n\n\u003e Interface for multihash, multicodec, multibase and CID\n\n# About\n\nThis 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.\n\nThis library provides implementations for most basics and many others can be found in linked repositories.\n\n```TypeScript\nimport { CID } from 'multiformats/cid'\nimport * as json from 'multiformats/codecs/json'\nimport { sha256 } from 'multiformats/hashes/sha2'\n\nconst bytes = json.encode({ hello: 'world' })\n\nconst hash = await sha256.digest(bytes)\nconst cid = CID.create(1, json.code, hash)\n//\u003e CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)\n```\n\n## Creating Blocks\n\n```TypeScript\nimport * as Block from 'multiformats/block'\nimport * as codec from '@ipld/dag-cbor'\nimport { sha256 as hasher } from 'multiformats/hashes/sha2'\n\nconst value = { hello: 'world' }\n\n// encode a block\nlet block = await Block.encode({ value, codec, hasher })\n\nblock.value // { hello: 'world' }\nblock.bytes // Uint8Array\nblock.cid   // CID() w/ sha2-256 hash address and dag-cbor codec\n\n// you can also decode blocks from their binary state\nblock = await Block.decode({ bytes: block.bytes, codec, hasher })\n\n// if you have the cid you can also verify the hash on decode\nblock = await Block.create({ bytes: block.bytes, cid: block.cid, codec, hasher })\n```\n\n## Multibase Encoders / Decoders / Codecs\n\nCIDs can be serialized to string representation using multibase encoders that implement [`MultibaseEncoder`](https://github.com/multiformats/js-multiformats/blob/master/src/bases/interface.ts) interface. This library provides quite a few implementations that can be imported:\n\n```TypeScript\nimport { base64 } from \"multiformats/bases/base64\"\ncid.toString(base64.encoder)\n//\u003e 'mAYAEEiCTojlxqRTl6svwqNJRVM2jCcPBxy+7mRTUfGDzy2gViA'\n```\n\nParsing CID string serialized CIDs requires multibase decoder that implements [`MultibaseDecoder`](https://github.com/multiformats/js-multiformats/blob/master/src/bases/interface.ts) interface. This library provides a decoder for every encoder it provides:\n\n```TypeScript\nCID.parse('mAYAEEiCTojlxqRTl6svwqNJRVM2jCcPBxy+7mRTUfGDzy2gViA', base64.decoder)\n//\u003e CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)\n```\n\nDual of multibase encoder \u0026 decoder is defined as multibase codec and it exposes\nthem as `encoder` and `decoder` properties. For added convenience codecs also\nimplement `MultibaseEncoder` and `MultibaseDecoder` interfaces so they could be\nused as either or both:\n\n```TypeScript\ncid.toString(base64)\nCID.parse(cid.toString(base64), base64)\n```\n\n**Note:** CID implementation comes bundled with `base32`, `base36`, and `base58btc`\nmultibase codecs so that CIDs can be base serialized to (version specific)\ndefault base encoding and parsed without having to supply base encoders/decoders:\n\n```TypeScript\nconst v1 = CID.parse('bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea')\nv1.toString()\n//\u003e 'bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea'\n\nconst v0 = CID.parse('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n')\nv0.toString()\n//\u003e 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'\nv0.toV1().toString()\n//\u003e 'bafybeihdwdcefgh4dqkjv67uzcmw7ojee6xedzdetojuzjevtenxquvyku'\n```\n\n## Multicodec Encoders / Decoders / Codecs\n\nThis library defines [`BlockEncoder`, `BlockDecoder` and `BlockCodec` interfaces](https://github.com/multiformats/js-multiformats/blob/master/src/codecs/interface.ts).\nCodec implementations should conform to the `BlockCodec` interface which implements both `BlockEncoder` and `BlockDecoder`.\nHere is an example implementation of JSON `BlockCodec`.\n\n```TypeScript\nexport const { name, code, encode, decode } = {\n  name: 'json',\n  code: 0x0200,\n  encode: json =\u003e new TextEncoder().encode(JSON.stringify(json)),\n  decode: bytes =\u003e JSON.parse(new TextDecoder().decode(bytes))\n}\n```\n\n## Multihash Hashers\n\nThis library defines [`MultihashHasher` and `MultihashDigest` interfaces](https://github.com/multiformats/js-multiformats/blob/master/src/hashes/interface.ts) and convinient function for implementing them:\n\n```TypeScript\nimport * as hasher from 'multiformats/hashes/hasher'\n\nconst sha256 = hasher.from({\n  // As per multiformats table\n  // https://github.com/multiformats/multicodec/blob/master/table.csv#L9\n  name: 'sha2-256',\n  code: 0x12,\n\n  encode: (input) =\u003e new Uint8Array(crypto.createHash('sha256').update(input).digest())\n})\n\nconst hash = await sha256.digest(json.encode({ hello: 'world' }))\nCID.create(1, json.code, hash)\n\n//\u003e CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)\n```\n\n## Traversal\n\nThis library contains higher-order functions for traversing graphs of data easily.\n\n`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()`.\n\n```TypeScript\nimport { walk } from 'multiformats/traversal'\nimport * as Block from 'multiformats/block'\nimport * as codec from 'multiformats/codecs/json'\nimport { sha256 as hasher } from 'multiformats/hashes/sha2'\n\n// build a DAG (a single block for this simple example)\nconst value = { hello: 'world' }\nconst block = await Block.encode({ value, codec, hasher })\nconst { cid } = block\nconsole.log(cid)\n//\u003e CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)\n\n// create a loader function that also collects CIDs of blocks in\n// their traversal order\nconst load = (cid, blocks) =\u003e async (cid) =\u003e {\n  // fetch a block using its cid\n  // e.g.: const block = await fetchBlockByCID(cid)\n  blocks.push(cid)\n  return block\n}\n\n// collect blocks in this DAG starting from the root `cid`\nconst blocks = []\nawait walk({ cid, load: load(cid, blocks) })\n\nconsole.log(blocks)\n//\u003e [CID(bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea)]\n```\n\n## Legacy interface\n\n[`blockcodec-to-ipld-format`](https://github.com/ipld/js-blockcodec-to-ipld-format) converts a multiformats [`BlockCodec`](https://github.com/multiformats/js-multiformats/blob/master/src/codecs/interface.ts#L21) into an\n[`interface-ipld-format`](https://github.com/ipld/interface-ipld-format) for use with the [`ipld`](https://github.com/ipld/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.\n\n## Implementations\n\nBy default, no base encodings (other than base32 \u0026 base58btc), hash functions,\nor codec implementations are exposed by `multiformats`, you need to\nimport the ones you need yourself.\n\n### Multibase codecs\n\n| bases                                                         | import                      | repo                                                                                              |\n| ------------------------------------------------------------- | --------------------------- | ------------------------------------------------------------------------------------------------- |\n| `base16`                                                      | `multiformats/bases/base16` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/bases) |\n| `base32`, `base32pad`, `base32hex`, `base32hexpad`, `base32z` | `multiformats/bases/base32` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/bases) |\n| `base64`, `base64pad`, `base64url`, `base64urlpad`            | `multiformats/bases/base64` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/bases) |\n| `base58btc`, `base58flick4`                                   | `multiformats/bases/base58` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/bases) |\n\nOther (less useful) bases implemented in [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/bases) include: `base2`, `base8`, `base10`, `base36` and `base256emoji`.\n\n### Multihash hashers\n\n| hashes                                                                                                                          | import                         | repo                                                                                                               |\n| ------------------------------------------------------------------------------------------------------------------------------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------ |\n| `sha2-256`, `sha2-512`                                                                                                          | `multiformats/hashes/sha2`     | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/src/hashes)             |\n| `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](https://github.com/multiformats/js-sha3)                                                    |\n| `identity`                                                                                                                      | `multiformats/hashes/identity` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/src/hashes/identity.js) |\n| `murmur3-128`, `murmur3-32`                                                                                                     | `@multiformats/murmur3`        | [multiformats/js-murmur3](https://github.com/multiformats/js-murmur3)                                              |\n| `blake2b-*`, `blake2s-*`                                                                                                        | `@multiformats/blake2`         | [multiformats/js-blake2](https://github.com/multiformats/js-blake2)                                                |\n\n### IPLD codecs (multicodec)\n\n| codec      | import                     | repo                                                                                                   |\n| ---------- | -------------------------- | ------------------------------------------------------------------------------------------------------ |\n| `raw`      | `multiformats/codecs/raw`  | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/src/codecs) |\n| `json`     | `multiformats/codecs/json` | [multiformats/js-multiformats](https://github.com/multiformats/js-multiformats/tree/master/src/codecs) |\n| `dag-cbor` | `@ipld/dag-cbor`           | [ipld/js-dag-cbor](https://github.com/ipld/js-dag-cbor)                                                |\n| `dag-json` | `@ipld/dag-json`           | [ipld/js-dag-json](https://github.com/ipld/js-dag-json)                                                |\n| `dag-pb`   | `@ipld/dag-pb`             | [ipld/js-dag-pb](https://github.com/ipld/js-dag-pb)                                                    |\n| `dag-jose` | `dag-jose`                 | [ceramicnetwork/js-dag-jose](https://github.com/ceramicnetwork/js-dag-jose)                            |\n\n# Install\n\n```console\n$ npm i multiformats\n```\n\n## Browser `\u003cscript\u003e` tag\n\nLoading this module through a script tag will make its exports available as `Multiformats` in the global namespace.\n\n```html\n\u003cscript src=\"https://unpkg.com/multiformats/dist/index.min.js\"\u003e\u003c/script\u003e\n```\n\n# API Docs\n\n- \u003chttps://multiformats.github.io/js-multiformats\u003e\n\n# License\n\nLicensed under either of\n\n- Apache 2.0, ([LICENSE-APACHE](https://github.com/multiformats/js-multiformats/LICENSE-APACHE) / \u003chttp://www.apache.org/licenses/LICENSE-2.0\u003e)\n- MIT ([LICENSE-MIT](https://github.com/multiformats/js-multiformats/LICENSE-MIT) / \u003chttp://opensource.org/licenses/MIT\u003e)\n\n# Contribution\n\nUnless 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.\n","funding_links":[],"categories":["JavaScript","TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmultiformats%2Fjs-multiformats","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmultiformats%2Fjs-multiformats","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmultiformats%2Fjs-multiformats/lists"}