1.1.9 • Published 3 years ago

cids v1.1.9

Weekly downloads
138,270
License
MIT
Repository
github
Last release
3 years ago

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

js-cid

npm.io npm.io 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

npm.io

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

@pangolindex/components@arthswap/widgets@openos.com/uniswap@bojinovic/test-package@apato/tupelo-wasm-sdk@232c/orbit-dbgit-remote-dgit@reflame/agent@xyo-network/api-diviner-nodejs@antiyro/componentspangea-widgets@infinitebrahmanuniverse/nolb-cidlabswap-frontend@bojinovic/filecoin-solidity@everything-registry/sub-chunk-1326fluidtokensv2-boosted-staking-widgetlinked-thoughtslibp2p-rpckeyconverterkepler-clientleofcoinleofcoin-coremoonshard_corengernger-clinger-compilerminimanonnft-arcsnft-readermerklingloopring-webnot-a-thing-test-exporternovel-opds-nowmaskdagnode@totality-fi/widgets@timtester-tw/test-tw-os-lib@thetechtrap/git-remote-dgit@thinkincoin/swap@vessel-kit/vessel@vessel-kit/anchoring@vessel-kit/codec@vessel-kit/identity@uprtcl/common@uprtcl/evees-polkadot@uprtcl/ipfs-provider@uprtcl/multiplatform@uprtcl/evees@uprtcl/evees-ethereum@uniswap/widgets@paper-db/orbit-db@stratos-network/stratos-sdk.js@steven7/api-sdk@sudophunk/fulido@sudophunk/interface@sussyswap/widgets@textile/textile@textile/threads-encoding@textile/threads-network@textile/threads-network-client@textile/threads-service@textile/threads-service-client@textile/buckets@testcnsdomains/content-hash@xmader/ipfs-core@xmader/ipfs-http-client@xswap/v2-core@zuludmmswap/interface@yak-spirit/yak-swap-uialt-ipld@wirelineio/cli-ipfs@wirelineio/registry-client@zulustation/sdk@zulustation/web3.storage@zulustation/avatara-nft-sdk@truffle/preserve-to-buckets@truffle/preserve-to-filecoin@web3api/ipfs-plugin-js@web3-react-widgets/core@web3widgets/react@zippie/ipfs-postmsg-proxy@zippie/zippie-utils@zippie/ipfs-bitswap@zippie/ipfs-bridge@zeitgeistpm/avatara-nft-sdk@zeitgeistpm/sdk@zeitgeistpm/web3.storage@znetstar/attic-server@wtchtwr/uniswap-widgets@wunsz-4soft/uniswap-widgetsaegir-test-repo@yumyumswap/widgetsaxelra-uniswap-widgetsaviondb@arve.knudsen/libp2p-daemon@arve.knudsen/libp2p-daemon-client@arve.knudsen/libp2p-kad-dht@_arcanumdex/components@3id/did-provider@atestalth/widgets@akashaproject/ipfs-connector-utils
1.1.9

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.1

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.8.3

4 years ago

0.8.2

4 years ago

0.8.1

4 years ago

0.7.5

4 years ago

0.8.0

4 years ago

0.7.4

4 years ago

0.7.3

4 years ago

0.7.2

4 years ago

0.7.1

5 years ago

0.7.0

5 years ago

0.6.0

5 years ago

0.5.8

5 years ago

0.5.7

5 years ago

0.5.6

5 years ago

0.5.5

6 years ago

0.5.4

6 years ago

0.5.3

6 years ago

0.5.2

6 years ago

0.5.1

7 years ago

0.5.0

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.3.6

7 years ago

0.3.5

7 years ago

0.3.4

7 years ago

0.3.3

7 years ago

0.3.2

7 years ago

0.3.0

7 years ago

0.2.0

7 years ago

0.1.1

8 years ago