Skip to content
This repository has been archived by the owner on Sep 3, 2021. It is now read-only.

feat: expose codec code and allow construction by code #118

Merged
merged 1 commit into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
- [new CID(baseEncodedString)](#new-cidbaseencodedstring)
- [new CID(Uint8Array)](#new-ciduint8array)
- [cid.codec](#cidcodec)
- [cid.code](#cidcode)
- [cid.version](#cidversion)
- [cid.multihash](#cidmultihash)
- [cid.multibaseName](#cidmultibasename)
Expand Down Expand Up @@ -78,6 +79,7 @@ const cid = new CID('bafybeig6xv5nwphfmvcnektpnojts33jqcuam7bmye2pb54adnrtccjlsu

cid.version // 1
cid.codec // 'dag-pb'
cid.code // 112
cid.multibaseName // 'base32'
cid.toString()
// 'bafybeig6xv5nwphfmvcnektpnojts33jqcuam7bmye2pb54adnrtccjlsu'
Expand All @@ -96,6 +98,14 @@ console.log(cid.toString())
// bafybeig6xv5nwphfmvcnektpnojts33jqcuam7bmye2pb54adnrtccjlsu
```

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

```js
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()`


Expand Down Expand Up @@ -150,7 +160,11 @@ Additionally, you can instantiate an instance from a `Uint8Array`.

#### cid.codec

Property containing the codec string.
Property containing the string identifier of the codec.

#### cid.code

Property containing the integer identifier of the codec.

#### cid.version

Expand Down
7 changes: 6 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ declare class CID {
*/
constructor(
version: 0 | 1,
codec: string,
codec: string | number,
multhash: Uint8Array,
multibaseName?: string
);
Expand All @@ -48,6 +48,11 @@ declare class CID {
*/
codec: string;

/**
* The codec of the CID in its number form.
*/
code: number;

/**
* The multihash of the CID.
*/
Expand Down
15 changes: 14 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ const uint8ArrayConcat = require('uint8arrays/concat')
const uint8ArrayToString = require('uint8arrays/to-string')
const uint8ArrayEquals = require('uint8arrays/equals')

const codecInts = Object.keys(codecs).reduce((p, name) => {
p[codecs[name]] = name
return p
}, {})

/**
* @typedef {Object} SerializedCID
* @param {string} codec
Expand Down Expand Up @@ -50,7 +55,7 @@ class CID {
* ```
*
* @param {string|Uint8Array|CID} version
* @param {string} [codec]
* @param {string|number} [codec]
* @param {Uint8Array} [multihash]
* @param {string} [multibaseName]
*
Expand Down Expand Up @@ -124,6 +129,10 @@ class CID {
*/
this.version = version

if (typeof codec === 'number') {
codec = codecInts[codec]
}

/**
* @type {string}
*/
Expand Down Expand Up @@ -188,6 +197,10 @@ class CID {
return prefix
}

get code () {
return codecs[this.codec]
}

/**
* Convert to a CID of version `0`.
*
Expand Down
3 changes: 3 additions & 0 deletions src/index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

export type Version = 0 | 1
export type Codec = string
export type CodecCode = number
export type Multihash = Uint8Array
export type BaseEncodedString = string
export type MultibaseName = string

declare class CID<a> {
constructor(Version, Codec, Multihash, multibaseName?:MultibaseName): void;
constructor(Version, CodecCode, Multihash, multibaseName?:MultibaseName): void;
constructor(BaseEncodedString): void;
constructor(Uint8Array): void;

+codec: Codec;
+code: CodecCode;
+multihash: Multihash;
+bytes: Uint8Array;
+prefix: Uint8Array;
Expand Down
21 changes: 21 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('CID', () => {
const cid = new CID(mhStr)

expect(cid).to.have.property('codec', 'dag-pb')
expect(cid).to.have.property('code', 112)
expect(cid).to.have.property('version', 0)
expect(cid).to.have.property('multihash').that.eql(multihash.fromB58String(mhStr))
expect(cid).to.have.property('multibaseName', 'base58btc')
Expand All @@ -36,6 +37,7 @@ describe('CID', () => {
const cid = new CID(mh)

expect(cid).to.have.property('codec', 'dag-pb')
expect(cid).to.have.property('code', 112)
expect(cid).to.have.property('version', 0)
expect(cid).to.have.property('multihash').that.eql(mh)
expect(cid).to.have.property('multibaseName', 'base58btc')
Expand All @@ -47,6 +49,17 @@ describe('CID', () => {
const cid = new CID(0, 'dag-pb', hash)

expect(cid).to.have.property('codec', 'dag-pb')
expect(cid).to.have.property('code', 112)
expect(cid).to.have.property('version', 0)
expect(cid).to.have.property('multihash')
expect(cid).to.have.property('multibaseName', 'base58btc')
})

it('create by parts (int codec)', () => {
const cid = new CID(0, 112, hash)

expect(cid).to.have.property('codec', 'dag-pb')
expect(cid).to.have.property('code', 112)
expect(cid).to.have.property('version', 0)
expect(cid).to.have.property('multihash')
expect(cid).to.have.property('multibaseName', 'base58btc')
Expand Down Expand Up @@ -110,6 +123,7 @@ describe('CID', () => {
const cid = new CID(cidStr)

expect(cid).to.have.property('codec', 'dag-pb')
expect(cid).to.have.property('code', 112)
expect(cid).to.have.property('version', 1)
expect(cid).to.have.property('multihash')
expect(cid).to.have.property('multibaseName', 'base58btc')
Expand All @@ -124,6 +138,7 @@ describe('CID', () => {
const cid = new CID(cidBuf)

expect(cid).to.have.property('codec', 'dag-pb')
expect(cid).to.have.property('code', 112)
expect(cid).to.have.property('version', 1)
expect(cid).to.have.property('multihash')
expect(cid).to.have.property('multibaseName', 'base32')
Expand All @@ -137,6 +152,7 @@ describe('CID', () => {
const cid = new CID(peerIdStr)

expect(cid).to.have.property('codec', 'libp2p-key')
expect(cid).to.have.property('code', 114)
expect(cid).to.have.property('version', 1)
expect(cid).to.have.property('multihash')
expect(cid).to.have.property('multibaseName', 'base36')
Expand All @@ -148,6 +164,7 @@ describe('CID', () => {
const cid = new CID(1, 'dag-cbor', hash)

expect(cid).to.have.property('codec', 'dag-cbor')
expect(cid).to.have.property('code', 113)
expect(cid).to.have.property('version', 1)
expect(cid).to.have.property('multihash')
expect(cid).to.have.property('multibaseName', 'base32')
Expand All @@ -158,6 +175,7 @@ describe('CID', () => {
const cid2 = new CID(cid1.toBaseEncodedString())

expect(cid1).to.have.property('codec').that.eql(cid2.codec)
expect(cid1).to.have.property('code').that.eql(cid2.code)
expect(cid1).to.have.property('version').that.eql(cid2.version)
expect(cid1).to.have.property('multihash').that.eql(cid2.multihash)
expect(cid1).to.have.property('multibaseName').that.eql(cid2.multibaseName)
Expand All @@ -170,6 +188,7 @@ describe('CID', () => {
const cid2 = new CID(cid1.toBaseEncodedString())

expect(cid1).to.have.property('codec', 'eth-block')
expect(cid1).to.have.property('code', 144)
expect(cid1).to.have.property('version', 1)
expect(cid1).to.have.property('multihash').that.eql(mh)
expect(cid1).to.have.property('multibaseName', 'base32')
Expand All @@ -189,13 +208,15 @@ describe('CID', () => {
const cid0 = new CID(0, 'dag-pb', mh)

expect(cid0).to.have.property('codec', 'dag-pb')
expect(cid0).to.have.property('code', 112)
expect(cid0).to.have.property('version', 0)
expect(cid0).to.have.property('multihash').that.eql(mh)
expect(cid0.toBaseEncodedString()).to.eql('161g3c')

const cid1 = new CID(1, 'dag-cbor', mh)

expect(cid1).to.have.property('codec', 'dag-cbor')
expect(cid1).to.have.property('code', 113)
expect(cid1).to.have.property('version', 1)
expect(cid1).to.have.property('multihash').that.eql(mh)
expect(cid1.toBaseEncodedString()).to.eql('bafyqaa3bmjrq')
Expand Down