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

Commit

Permalink
chore: validate and test CIDv0 invariants
Browse files Browse the repository at this point in the history
Adds tests for
- base other than 'base32btc' for CIDv0
- codec other than 'dag-pb' for CIDv0

License: MIT
Signed-off-by: Oli Evans <oli@tableflip.io>
  • Loading branch information
olizilla authored and vmx committed Apr 4, 2019
1 parent 2e597b9 commit 0e94b55
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/cid-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ var CIDUtil = {
return 'codec must be string'
}

if (other.version === 0) {
if (other.codec !== 'dag-pb') {
return `codec must be 'dag-pb' for CIDv0`
}
if (other.multibaseName !== 'base58btc') {
return `multibaseName must be 'base58btc' for CIDv0`
}
}

if (!Buffer.isBuffer(other.multihash)) {
return 'multihash must be a Buffer'
}
Expand Down
14 changes: 13 additions & 1 deletion test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,19 @@ describe('CID', () => {
).to.throw()
})

it('throws on trying to base encode CIDv0 in other base than base58 ', () => {
it('throws on trying to create a CIDv0 with a codec other than dag-pb', () => {
expect(
() => new CID(0, 'dag-cbor', hash)
).to.throw(`codec must be 'dag-pb' for CIDv0`)
})

it('throws on trying to create a CIDv0 with a base other than base58btc', () => {
expect(
() => new CID(0, 'dag-pb', hash, 'base32')
).to.throw(`multibaseName must be 'base58btc' for CIDv0`)
})

it('throws on trying to base encode CIDv0 in other base than base58btc', () => {
const mhStr = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'
const cid = new CID(mhStr)
expect(() => cid.toBaseEncodedString('base16')).to.throw()
Expand Down

0 comments on commit 0e94b55

Please sign in to comment.