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

Commit

Permalink
fix: throw an error if another base is picked for cidv0
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Jan 21, 2017
1 parent 451e1ea commit 24f2c0b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,12 @@ class CID {
base = base || 'base58btc'

switch (this.version) {
case 0:
case 0: {
if (base !== 'base58btc') {
throw new Error('not supported with CIDv0, to support different bases, please migrate the instance do CIDv1, you can do that through cid.toV1()')
}
return mh.toB58String(this.multihash)
}
case 1:
return multibase.encode(base, this.buffer).toString()
default:
Expand Down
6 changes: 6 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ describe('CID', () => {
).to.throw()
})

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

it('.prefix', () => {
const cid = new CID(0, 'dag-pb', multihash.encode(new Buffer('abc'), 'sha2-256'))
expect(cid.prefix.toString('hex')).to.equal('00701203')
Expand Down

0 comments on commit 24f2c0b

Please sign in to comment.