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

Commit 350f58c

Browse files
olizillavmx
authored andcommitted
chore: validate and test CIDv0 invariants
Adds tests for - base other than 'base32btc' for CIDv0 - codec other than 'dag-pb' for CIDv0 License: MIT Signed-off-by: Oli Evans <[email protected]>
1 parent 56f3a69 commit 350f58c

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/cid-util.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ var CIDUtil = {
2424
return 'codec must be string'
2525
}
2626

27+
if (other.version === 0) {
28+
if (other.codec !== 'dag-pb') {
29+
return `codec must be 'dag-pb' for CIDv0`
30+
}
31+
if (other.multibaseName !== 'base58btc') {
32+
return `multibaseName must be 'base58btc' for CIDv0`
33+
}
34+
}
35+
2736
if (!Buffer.isBuffer(other.multihash)) {
2837
return 'multihash must be a Buffer'
2938
}

test/index.spec.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,19 @@ describe('CID', () => {
6666
).to.throw()
6767
})
6868

69-
it('throws on trying to base encode CIDv0 in other base than base58 ', () => {
69+
it('throws on trying to create a CIDv0 with a codec other than dag-pb', () => {
70+
expect(
71+
() => new CID(0, 'dag-cbor', hash)
72+
).to.throw(`codec must be 'dag-pb' for CIDv0`)
73+
})
74+
75+
it('throws on trying to create a CIDv0 with a base other than base58btc', () => {
76+
expect(
77+
() => new CID(0, 'dag-pb', hash, 'base32')
78+
).to.throw(`multibaseName must be 'base58btc' for CIDv0`)
79+
})
80+
81+
it('throws on trying to base encode CIDv0 in other base than base58btc', () => {
7082
const mhStr = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'
7183
const cid = new CID(mhStr)
7284
expect(() => cid.toBaseEncodedString('base16')).to.throw()

0 commit comments

Comments
 (0)