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

Commit 59469b6

Browse files
rvaggvmx
authored andcommitted
feat: expose codec code and allow construction by code
Ref: #117 (comment)
1 parent a7ae250 commit 59469b6

File tree

5 files changed

+59
-3
lines changed

5 files changed

+59
-3
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
- [new CID(baseEncodedString)](#new-cidbaseencodedstring)
2929
- [new CID(Uint8Array)](#new-ciduint8array)
3030
- [cid.codec](#cidcodec)
31+
- [cid.code](#cidcode)
3132
- [cid.version](#cidversion)
3233
- [cid.multihash](#cidmultihash)
3334
- [cid.multibaseName](#cidmultibasename)
@@ -78,6 +79,7 @@ const cid = new CID('bafybeig6xv5nwphfmvcnektpnojts33jqcuam7bmye2pb54adnrtccjlsu
7879

7980
cid.version // 1
8081
cid.codec // 'dag-pb'
82+
cid.code // 112
8183
cid.multibaseName // 'base32'
8284
cid.toString()
8385
// 'bafybeig6xv5nwphfmvcnektpnojts33jqcuam7bmye2pb54adnrtccjlsu'
@@ -96,6 +98,14 @@ console.log(cid.toString())
9698
// bafybeig6xv5nwphfmvcnektpnojts33jqcuam7bmye2pb54adnrtccjlsu
9799
```
98100

101+
The multicodec integer code can also be used to create a new CID:
102+
103+
```js
104+
const cid = new CID(1, 112, hash)
105+
console.log(cid.toString())
106+
// bafybeig6xv5nwphfmvcnektpnojts33jqcuam7bmye2pb54adnrtccjlsu
107+
```
108+
99109
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()`
100110

101111

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

151161
#### cid.codec
152162

153-
Property containing the codec string.
163+
Property containing the string identifier of the codec.
164+
165+
#### cid.code
166+
167+
Property containing the integer identifier of the codec.
154168

155169
#### cid.version
156170

src/index.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ declare class CID {
3030
*/
3131
constructor(
3232
version: 0 | 1,
33-
codec: string,
33+
codec: string | number,
3434
multhash: Uint8Array,
3535
multibaseName?: string
3636
);
@@ -48,6 +48,11 @@ declare class CID {
4848
*/
4949
codec: string;
5050

51+
/**
52+
* The codec of the CID in its number form.
53+
*/
54+
code: number;
55+
5156
/**
5257
* The multihash of the CID.
5358
*/

src/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ const uint8ArrayConcat = require('uint8arrays/concat')
1010
const uint8ArrayToString = require('uint8arrays/to-string')
1111
const uint8ArrayEquals = require('uint8arrays/equals')
1212

13+
const codecInts = Object.keys(codecs).reduce((p, name) => {
14+
p[codecs[name]] = name
15+
return p
16+
}, {})
17+
1318
/**
1419
* @typedef {Object} SerializedCID
1520
* @param {string} codec
@@ -50,7 +55,7 @@ class CID {
5055
* ```
5156
*
5257
* @param {string|Uint8Array|CID} version
53-
* @param {string} [codec]
58+
* @param {string|number} [codec]
5459
* @param {Uint8Array} [multihash]
5560
* @param {string} [multibaseName]
5661
*
@@ -124,6 +129,10 @@ class CID {
124129
*/
125130
this.version = version
126131

132+
if (typeof codec === 'number') {
133+
codec = codecInts[codec]
134+
}
135+
127136
/**
128137
* @type {string}
129138
*/
@@ -188,6 +197,10 @@ class CID {
188197
return prefix
189198
}
190199

200+
get code () {
201+
return codecs[this.codec]
202+
}
203+
191204
/**
192205
* Convert to a CID of version `0`.
193206
*

src/index.js.flow

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22

33
export type Version = 0 | 1
44
export type Codec = string
5+
export type CodecCode = number
56
export type Multihash = Uint8Array
67
export type BaseEncodedString = string
78
export type MultibaseName = string
89

910
declare class CID<a> {
1011
constructor(Version, Codec, Multihash, multibaseName?:MultibaseName): void;
12+
constructor(Version, CodecCode, Multihash, multibaseName?:MultibaseName): void;
1113
constructor(BaseEncodedString): void;
1214
constructor(Uint8Array): void;
1315

1416
+codec: Codec;
17+
+code: CodecCode;
1518
+multihash: Multihash;
1619
+bytes: Uint8Array;
1720
+prefix: Uint8Array;

test/index.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('CID', () => {
2222
const cid = new CID(mhStr)
2323

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

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

4951
expect(cid).to.have.property('codec', 'dag-pb')
52+
expect(cid).to.have.property('code', 112)
53+
expect(cid).to.have.property('version', 0)
54+
expect(cid).to.have.property('multihash')
55+
expect(cid).to.have.property('multibaseName', 'base58btc')
56+
})
57+
58+
it('create by parts (int codec)', () => {
59+
const cid = new CID(0, 112, hash)
60+
61+
expect(cid).to.have.property('codec', 'dag-pb')
62+
expect(cid).to.have.property('code', 112)
5063
expect(cid).to.have.property('version', 0)
5164
expect(cid).to.have.property('multihash')
5265
expect(cid).to.have.property('multibaseName', 'base58btc')
@@ -110,6 +123,7 @@ describe('CID', () => {
110123
const cid = new CID(cidStr)
111124

112125
expect(cid).to.have.property('codec', 'dag-pb')
126+
expect(cid).to.have.property('code', 112)
113127
expect(cid).to.have.property('version', 1)
114128
expect(cid).to.have.property('multihash')
115129
expect(cid).to.have.property('multibaseName', 'base58btc')
@@ -124,6 +138,7 @@ describe('CID', () => {
124138
const cid = new CID(cidBuf)
125139

126140
expect(cid).to.have.property('codec', 'dag-pb')
141+
expect(cid).to.have.property('code', 112)
127142
expect(cid).to.have.property('version', 1)
128143
expect(cid).to.have.property('multihash')
129144
expect(cid).to.have.property('multibaseName', 'base32')
@@ -137,6 +152,7 @@ describe('CID', () => {
137152
const cid = new CID(peerIdStr)
138153

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

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

160177
expect(cid1).to.have.property('codec').that.eql(cid2.codec)
178+
expect(cid1).to.have.property('code').that.eql(cid2.code)
161179
expect(cid1).to.have.property('version').that.eql(cid2.version)
162180
expect(cid1).to.have.property('multihash').that.eql(cid2.multihash)
163181
expect(cid1).to.have.property('multibaseName').that.eql(cid2.multibaseName)
@@ -170,6 +188,7 @@ describe('CID', () => {
170188
const cid2 = new CID(cid1.toBaseEncodedString())
171189

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

191210
expect(cid0).to.have.property('codec', 'dag-pb')
211+
expect(cid0).to.have.property('code', 112)
192212
expect(cid0).to.have.property('version', 0)
193213
expect(cid0).to.have.property('multihash').that.eql(mh)
194214
expect(cid0.toBaseEncodedString()).to.eql('161g3c')
195215

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

198218
expect(cid1).to.have.property('codec', 'dag-cbor')
219+
expect(cid1).to.have.property('code', 113)
199220
expect(cid1).to.have.property('version', 1)
200221
expect(cid1).to.have.property('multihash').that.eql(mh)
201222
expect(cid1.toBaseEncodedString()).to.eql('bafyqaa3bmjrq')

0 commit comments

Comments
 (0)