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

Package detail

cids

multiformats1.3mMITdeprecated1.1.9TypeScript support: included

This module has been superseded by the multiformats module

CID Implementation in JavaScript

multihash, cid, ipld

readme

⛔️ DEPRECATED: This module has been superseded by the multiformats module

js-cid

Travis CI Coverage Status Dependency Status js-standard-style Greenkeeper badge

CID implementation in JavaScript.

Lead Maintainer

Volker Mische

Table of Contents

Install

In Node.js through npm

$ npm install --save cids

Browser: Browserify, Webpack, other bundlers

The code published to npm that gets loaded on require is in fact an ES5 transpiled version with the right shims added. This means that you can require it and use with your favourite bundler without having to adjust asset management process.

const CID = require('cids')

In the Browser through <script> tag

Loading this module through a script tag will make the Cids obj available in the global namespace.

<script src="https://unpkg.com/cids/dist/index.min.js"></script>
<!-- OR -->
<script src="https://unpkg.com/cids/dist/index.js"></script>

Usage

You can create an instance from a CID string or CID Uint8Array

const CID = require('cids')

const cid = new CID('bafybeig6xv5nwphfmvcnektpnojts33jqcuam7bmye2pb54adnrtccjlsu')

cid.version       // 1
cid.codec         // 'dag-pb'
cid.code          // 112
cid.multibaseName // 'base32'
cid.toString()
// 'bafybeig6xv5nwphfmvcnektpnojts33jqcuam7bmye2pb54adnrtccjlsu'

or by specifying the cid version, multicodec name and multihash:

const CID = require('cids')
const multihashing = require('multihashing-async')
const bytes = new TextEncoder('utf8').encode('OMG!')

const hash = await multihashing(bytes, 'sha2-256')
const cid = new CID(1, 'dag-pb', hash)
console.log(cid.toString())
// bafybeig6xv5nwphfmvcnektpnojts33jqcuam7bmye2pb54adnrtccjlsu

The multicodec integer code can also be used to create a new CID:

const cid = new CID(1, 112, hash)
console.log(cid.toString())
// bafybeig6xv5nwphfmvcnektpnojts33jqcuam7bmye2pb54adnrtccjlsu

The string form of v1 CIDs defaults to base32 encoding (v0 CIDs are always base58btc encoded). When creating a new instance you can optionally specify the default multibase to use when calling toBaseEncodedString() or toString()

const cid = new CID(1, 'raw', hash, 'base64')
console.log(cid.toString())
// mAXASIN69ets85WVE0ipva5M5b2mAqAZ8LME08PeAG2MxCSuV

If you construct an instance from a valid CID string, the base you provided will be preserved as the default.

// e.g. a base64url encoded CID
const cid = new CID('uAXASIHJSUj5lkfuP5VPWf_VahvhARLRqPkF24QxY-lKaSqvV')
cid.toString()
// uAXASIHJSUj5lkfuP5VPWf_VahvhARLRqPkF24QxY-lKaSqvV

API

CID.isCID(cid)

Returns true if object is a valid CID instance, false if not valid.

It's important to use this method rather than instanceof checks in order to handle CID objects from different versions of this module.

CID.validateCID(cid)

Validates the different components (version, codec, multihash, multibaseName) of the CID instance. Throws an Error if not valid.

new CID(version, codec, multihash, [multibaseName])

version must be either 0 or 1.

codec must be a string of a valid registered codec.

multihash must be a Uint8Array instance of a valid multihash.

multibaseName optional string. Must be a valid multibase name. Default is base58btc for v0 CIDs or base32 for v1 CIDs.

new CID(baseEncodedString)

Additionally, you can instantiate an instance from a base encoded string.

new CID(Uint8Array)

Additionally, you can instantiate an instance from a Uint8Array.

cid.codec

Property containing the string identifier of the codec.

cid.code

Property containing the integer identifier of the codec.

cid.version

Property containing the CID version integer.

cid.multihash

Property containing the multihash Uint8Array.

cid.multibaseName

Property containing the default base to use when calling .toString

cid.bytes

Property containing the full CID encoded as a Uint8Array.

cid.prefix

Proprety containing a Uint8Array of the CID version, codec, and the prefix section of the multihash.

cid.toV0()

Returns the CID encoded in version 0. Only works for dag-pb codecs.

Throws if codec is not dag-pb.

cid.toV1()

Returns the CID encoded in version 1.

cid.toBaseEncodedString(base=this.multibaseName)

Returns a base encoded string of the CID. Defaults to the base encoding in this.multibaseName.

The value of this.multibaseName depends on how the instance was constructed:

  1. If the CID was constructed from an object that already had a multibase (a string or an existing CID) then it retains that base.
  2. If the CID was constructed from an object that did not have a multibase (a Uint8Array, or by passing only version + codec + multihash to the constructor), then multibaseName will be base58btc for a v0 CID or base32 for a v1 CID.

cid.toString(base=this.multibaseName)

Shorthand for cid.toBaseEncodedString described above.

cid.equals(cid)

Compare cid instance. Returns true if CID's are identical, false if otherwise.

Contribute

Contributions welcome. Please check out the issues.

Check out our contributing document for more information on how we work, and about contributing in general. Please be aware that all interactions related to multiformats are subject to the IPFS Code of Conduct.

Small note: If editing the Readme, please conform to the standard-readme specification.

License

MIT

changelog

1.1.9 (2021-09-02)

1.1.8 (2021-08-24)

1.1.7 (2021-06-15)

Bug Fixes

  • add test for v1->v1 conversion (de28c1f)

1.1.6 (2021-03-02)

Bug Fixes

1.1.5 (2021-01-13)

Bug Fixes

1.1.4 (2020-12-12)

Bug Fixes

1.1.3 (2020-12-11)

Bug Fixes

  • use latest multicodec release (6cc387f)

1.1.2 (2020-12-11)

Bug Fixes

  • pin multicodec to version 2.0.4 (14e55a1)
  • turn isCID to a type guard (7413819)

1.1.1 (2020-12-11)

Bug Fixes

  • allow CIDs to be compared through deep equality (127745e), closes #131

1.1.0 (2020-12-11)

Features

  • add ts types with aegir (0e11f03)

1.0.2 (2020-10-14)

Bug Fixes

1.0.1 (2020-09-25)

Bug Fixes

1.0.0 (2020-08-04)

Bug Fixes

  • replace node buffers with uint8arrays (#117) (a7ae250)

Features

BREAKING CHANGES

    • node Buffers have been replaced with Uint8Arrays
  • the .buffer property has been renamed to .bytes and is now a Uint8Array
  • the .prefix property is now a Uint8Array

0.8.3 (2020-06-19)

Bug Fixes

  • fix support for identity multihash (#93) (51105b6)

0.8.2 (2020-06-15)

Features

0.8.1 (2020-05-22)

Features

  • support type predicates (f7ede61)

0.8.0 (2020-03-24)

Chores

BREAKING CHANGES

  • base1 support was removed

0.7.5 (2020-03-24)

Features

  • add nodejs.util.inspect.custom (fe953c1)

0.7.4 (2020-03-16)

Bug Fixes

0.7.3 (2020-01-24)

Bug Fixes

  • address review requests + ordering (29e2def)
  • update toString to include optional base (5aff196)

Features

  • more correct type defs + docs (4eb0c60)

0.7.2 (2020-01-14)

Bug Fixes

  • codecs -> record of codec: buffer (4cf17bb)
  • explicitly require .json ext of base-table (a9898ff), closes #96

Features

0.7.1 (2019-05-14)

Bug Fixes

  • create new CID from old CID (c888183)

0.7.0 (2019-05-09)

Bug Fixes

  • broken link to contributing document (c29d12e)
  • update typedefs to reflect API changes (63cd5f3), closes #77

Code Refactoring

  • default to base32 encoding for v1 CIDs (2f854c7)

BREAKING CHANGES

  • The default string encoding for v1 CIDs has changed from base58btc to base32.

License: MIT Signed-off-by: Alan Shaw alan.shaw@protocol.ai

0.6.0 (2019-04-08)

Features

  • add flow typedefs (1cf9740)
  • cache string represntation (537f604)
  • preserve base when constructed from a string (2e597b9)

BREAKING CHANGES

  • previously base was not preserved and all CIDs would be normalised to base58btc when asking for their string representation.

The default will change to base32 in https://github.com/multiformats/js-cid/pull/73/files

The idea behind this change is that we shouldnt lose information when the user passes us a base encoded string, but keep it and use it as the default base so toString returns the same string they provided.

I'd like this as a fix for ipld explorer, which currently forces all CIDs into base58btc, seee: https://github.com/ipfs-shipyard/ipfs-webui/issues/999

License: MIT Signed-off-by: Oli Evans oli@tableflip.io

0.5.8 (2019-03-14)

Performance Improvements

  • cache buffer form of CID when created (c7fc646)

0.5.7 (2018-12-06)

Bug Fixes

  • stricter validation for CID v1 to v0 conversion (0bd7318)

0.5.6 (2018-11-22)

Bug Fixes

  • add class name (b9fc845)
  • generated docs, re-add isCID (5b826fc)
  • package: update multibase to version 0.6.0 (e4e6508)

0.5.5 (2018-09-25)

Bug Fixes

  • toV0 and toV1 create instances that cause isCID be false (14ab8e4)

0.5.4 (2018-09-24)

Bug Fixes

  • linter errors (9f9359d)
  • migrate to class-is for instance comparise, fixes #53 (6b6873b)
  • remove direct access to codec lookup table (4027108)
  • use org/repo convention (5805660)

Features

0.5.3 (2018-03-12)

Bug Fixes

0.5.2 (2017-10-06)

0.5.1 (2017-07-10)

0.5.0 (2017-03-30)

Bug Fixes

0.4.2 (2017-03-16)

Bug Fixes

  • package: update multihashes to version 0.4.0 (1d0c3c8)

0.4.1 (2017-02-09)

0.4.0 (2017-01-25)

Bug Fixes

  • make toV0 and toV1 return CID objects (32902e3)
  • throw an error if another base is picked for cidv0 (24f2c0b)

0.3.6 (2017-01-21)

0.3.5 (2016-12-16)

Features

0.3.4 (2016-12-08)

0.3.3 (2016-12-07)

0.3.2 (2016-12-07)

0.3.1 (2016-12-07)

Bug Fixes

0.3.0 (2016-12-05)

Features

  • deps: update to multihashes@0.3.0 (a0e331d)

0.2.0 (2016-10-24)

Features

  • cidV0 and cidV1 support (211970b)

0.1.1 (2016-09-09)