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

Commit 211970b

Browse files
committed
feat: cidV0 and cidV1 support
update multibase and multicodec add tests add documentation
1 parent 733cb96 commit 211970b

File tree

7 files changed

+240
-106
lines changed

7 files changed

+240
-106
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ const cid = new CID(base58Multihash)
7575

7676
## API
7777

78-
### `new CID(codec[, version, hash])`
78+
### Constructor
79+
80+
- `new CID(<version>, <codec>, <multihash>)`
81+
- `new CID(<cidStr>)`
82+
- `new CID(<cid.buffer>)`
83+
- `new CID(<multihash>)`
84+
- `new CID(<bs58 encoded multihash>)`
7985

8086
### `.codec`
8187

@@ -85,7 +91,13 @@ const cid = new CID(base58Multihash)
8591

8692
### `.buffer`
8793

88-
### `.toString()`
94+
### `.toV0()`
95+
96+
### `.toV1()`
97+
98+
### `.toBaseEncodedString(base)`
99+
100+
Defaults to 'base58btc'
89101

90102
### `.toJSON()`
91103

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
},
3737
"dependencies": {
3838
"multibase": "^0.2.0",
39+
"multicodec": "0.1.0",
3940
"multihashes": "^0.2.2"
4041
},
4142
"devDependencies": {
@@ -50,4 +51,4 @@
5051
"contributors": [
5152
"Friedel Ziegelmayer <dignifiedquire@gmail.com>"
5253
]
53-
}
54+
}

src/cid.js

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/codecs.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
'use strict'
22

3+
/*
4+
* Consult table at: https://github.com/multiformats/multicodec
5+
*/
6+
37
module.exports = {
4-
protobuf: 0,
5-
raw: 1,
6-
json: 2,
7-
cbor: 3
8+
raw: new Buffer('00', 'hex'),
9+
'dag-pb': new Buffer('70', 'hex'),
10+
'dag-cbor': new Buffer('71', 'hex'),
11+
'eth-block': new Buffer('90', 'hex'),
12+
'eth-tx': new Buffer('91', 'hex')
813
}

src/index.js

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,120 @@
11
'use strict'
22

3-
const CID = require('./cid')
3+
const mh = require('multihashes')
4+
const multibase = require('multibase')
5+
const multicodec = require('multicodec')
6+
7+
const codecs = require('./codecs')
8+
9+
// CID: <mbase><version><mcodec><mhash>
10+
11+
class CID {
12+
/*
13+
* if (str)
14+
* if (1st char is on multibase table) -> CID String
15+
* else -> bs58 encoded multihash
16+
* else if (Buffer)
17+
* if (0 or 1) -> CID
18+
* else -> multihash
19+
* else if (Number)
20+
* -> construct CID by parts
21+
*
22+
* ..if only JS had traits..
23+
*/
24+
constructor (version, codec, multihash) {
25+
if (typeof version === 'string') {
26+
if (multibase.isEncoded(version)) { // CID String (encoded with multibase)
27+
const cid = multibase.decode(version)
28+
this.version = parseInt(cid.slice(0, 1).toString('hex'), 16)
29+
this.codec = multicodec.getCodec(cid.slice(1))
30+
this.multihash = multicodec.rmPrefix(cid.slice(1))
31+
} else { // bs58 string encoded multihash
32+
this.codec = 'dag-pb'
33+
this.multihash = mh.fromB58String(version)
34+
this.version = 0
35+
}
36+
} else if (Buffer.isBuffer(version)) {
37+
const firstByte = version.slice(0, 1)
38+
const v = parseInt(firstByte.toString('hex'), 16)
39+
if (v === 0 || v === 1) { // CID
40+
const cid = version
41+
this.version = v
42+
this.codec = multicodec.getCodec(cid.slice(1))
43+
this.multihash = multicodec.rmPrefix(cid.slice(1))
44+
} else { // multihash
45+
this.codec = 'dag-pb'
46+
this.multihash = version
47+
this.version = 0
48+
}
49+
} else if (typeof version === 'number') {
50+
if (typeof codec !== 'string') {
51+
throw new Error('codec must be string')
52+
}
53+
if (!(version === 0 || version === 1)) {
54+
throw new Error('version must be a number equal to 0 or 1')
55+
}
56+
mh.validate(multihash)
57+
this.codec = codec
58+
this.version = version
59+
this.multihash = multihash
60+
}
61+
}
62+
63+
get buffer () {
64+
switch (this.version) {
65+
case 0:
66+
return this.multihash
67+
case 1:
68+
return Buffer.concat([
69+
Buffer('01', 'hex'),
70+
Buffer(codecs[this.codec]),
71+
this.multihash
72+
])
73+
default:
74+
throw new Error('unsupported version')
75+
}
76+
}
77+
78+
toV0 () {
79+
return this.multihash
80+
}
81+
82+
toV1 () {
83+
return this.buffer
84+
}
85+
86+
/* defaults to base58btc */
87+
toBaseEncodedString (base) {
88+
base = base || 'base58btc'
89+
90+
switch (this.version) {
91+
case 0:
92+
return mh.toB58String(this.multihash)
93+
case 1:
94+
return multibase.encode(base, this.buffer).toString()
95+
default:
96+
throw new Error('Unsupported version')
97+
}
98+
}
99+
100+
toJSON () {
101+
return {
102+
codec: this.codec,
103+
version: this.version,
104+
hash: this.multihash
105+
}
106+
}
107+
108+
equals (other) {
109+
return this.codec === other.codec &&
110+
this.version === other.version &&
111+
this.multihash.equals(other.multihash)
112+
}
113+
}
114+
115+
CID.codecs = codecs
116+
CID.isCID = (other) => {
117+
return other.constructor.name === 'CID'
118+
}
4119

5120
module.exports = CID

test/helpers/gen-cid.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
3+
// CID String: <mbase><version><mcodec><mhash>
4+
5+
const multibase = require('multibase')
6+
const codecs = require('../../src').codecs
7+
const multihashing = require('multihashing')
8+
9+
const mh = multihashing(Buffer('oh, hey!'), 'sha2-256')
10+
const cid = Buffer.concat([
11+
new Buffer('01', 'hex'),
12+
codecs.dagPB,
13+
mh
14+
])
15+
16+
const cidStr = multibase.encode('base58btc', cid).toString()
17+
18+
console.log('CID String (multibase included)')
19+
console.log(cidStr)
20+
console.log('CID in hex (multibase not included)')
21+
console.log(cid.toString('hex'))

0 commit comments

Comments
 (0)