js-cid
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>
Gotchas
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.
Usage
You can create an instance from a CID string or CID Buffer
const CID = require('cids')
const cid = new CID('zdj7WkRPAX9o9nb9zPbXzwG7JEs78uyhwbUs8JSUayB98DWWY')
cid.version
cid.codec
cid.multibaseName
cid.toString()
or by specifying the cid version, multicodec name and multihash:
const CID = require('cids')
const multihashing = require('multihashing-async')
multihashing(Buffer.from('OMG!'), 'sha2-256', (err, hash) => {
const cid = new CID(1, 'dag-pb', hash)
console.log(cid.toString())
})
The string form of CIDs currently defaults to base58btc
flavour. (This is soon to change to base32
. 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, 'base32')
console.log(cid.toString())
If you construct an instance from a valid CID string, the base you provided will be preserved as the default.
const cid = new CID('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy')
cid.toString()
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. Returns true if valid, false 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 Buffer
instance of a valid multihash.
multibaseName
optional string. Must be a valid multibase name. Default is base58btc
.
new CID(baseEncodedString)
Additionally, you can instantiate an instance from a base encoded
string.
new CID(Buffer)
Additionally, you can instantiate an instance from a buffer.
cid.codec
Property containing the codec string.
cid.version
Property containing the CID version integer.
cid.multihash
Property containing the multihash buffer.
cid.multibaseName
Property containing the default base to use when calling .toString
cid.buffer
Property containing the full CID encoded as a Buffer
.
cid.prefix
Proprety containing a buffer 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 encodec string of the CID. Defaults to the base encoding in this.multibaseName
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