CID implementation in JavaScript.
$ npm install --save cids
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')
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>
You will need to use Node.js Buffer
API compatible, if you are running inside the browser, you can access it by multihash.Buffer
or you can install Feross's Buffer.
You can create an instance from a CID string or CID Buffer
const CID = require('cids')
const cid = new CID('bafybeig6xv5nwphfmvcnektpnojts33jqcuam7bmye2pb54adnrtccjlsu')
cid.version // 1
cid.codec // 'dag-pb'
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 hash = await multihashing(Buffer.from('OMG!'), 'sha2-256')
const cid = new CID(1, 'dag-pb', 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
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.
Validates the different components (version, codec, multihash, multibaseName) of the CID instance. Returns true if valid, false if not valid.
version
must be either 0 or 1.
codec
must be a string of a valid registered codec.
multihash
must be a Buffer
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.
Additionally, you can instantiate an instance from a base encoded string.
Additionally, you can instantiate an instance from a buffer.
Property containing the codec string.
Property containing the CID version integer.
Property containing the multihash buffer.
Property containing the default base to use when calling .toString
Property containing the full CID encoded as a Buffer
.
Proprety containing a buffer of the CID version, codec, and the prefix section of the multihash.
Returns the CID encoded in version 0. Only works for dag-pb
codecs.
Throws if codec is not dag-pb
.
Returns the CID encoded in version 1.
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:
- If the CID was constructed from an object that already had a multibase (a string or an existing CID) then it retains that base.
- If the CID was constructed from an object that did not have a multibase (a buffer, or by passing only version + codec + multihash to the constructor), then
multibaseName
will bebase58btc
for a v0 CID orbase32
for a v1 CID.
Shorthand for cid.toBaseEncodedString
described above.
Compare cid instance. Returns true if CID's are identical, false if otherwise.
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.
MIT