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

refactor: default to base32 encoding for v1 CIDs #89

Merged
merged 3 commits into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class CID {
* new CID(<bs58 encoded multihash>)
* new CID(<cid>)
*/
constructor (version, codec, multihash, multibaseName = 'base58btc') {
constructor (version, codec, multihash, multibaseName) {
if (module.exports.isCID(version)) {
// version is an exising CID instance
const cid = version
Expand Down Expand Up @@ -95,13 +95,13 @@ class CID {
if (Buffer.isBuffer(version)) {
const firstByte = version.slice(0, 1)
const v = parseInt(firstByte.toString('hex'), 16)
if (v === 0 || v === 1) {
if (v === 1) {
// version is a CID buffer
const cid = version
this.version = v
this.codec = multicodec.getCodec(cid.slice(1))
this.multihash = multicodec.rmPrefix(cid.slice(1))
this.multibaseName = (v === 0) ? 'base58btc' : multibaseName
this.multibaseName = 'base32'
} else {
// version is a raw multihash buffer, so v0
this.version = 0
Expand Down Expand Up @@ -133,7 +133,7 @@ class CID {
/**
* @type {string}
*/
this.multibaseName = multibaseName
this.multibaseName = multibaseName || (version === 0 ? 'base58btc' : 'base32')

CID.validateCID(this)
}
Expand Down
15 changes: 12 additions & 3 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe('CID', () => {
expect(cid).to.have.property('codec', 'dag-pb')
expect(cid).to.have.property('version', 0)
expect(cid).to.have.property('multihash').that.eql(multihash.fromB58String(mhStr))
expect(cid).to.have.property('multibaseName', 'base58btc')

expect(cid.toBaseEncodedString()).to.be.eql(mhStr)
})
Expand All @@ -46,6 +47,7 @@ describe('CID', () => {
expect(cid).to.have.property('codec', 'dag-pb')
expect(cid).to.have.property('version', 0)
expect(cid).to.have.property('multihash').that.eql(mh)
expect(cid).to.have.property('multibaseName', 'base58btc')

expect(cid.toBaseEncodedString()).to.eql(mhStr)
done()
Expand All @@ -58,6 +60,7 @@ describe('CID', () => {
expect(cid).to.have.property('codec', 'dag-pb')
expect(cid).to.have.property('version', 0)
expect(cid).to.have.property('multihash')
expect(cid).to.have.property('multibaseName', 'base58btc')
})

it('throws on invalid BS58Str multihash ', () => {
Expand Down Expand Up @@ -108,19 +111,21 @@ describe('CID', () => {
expect(cid).to.have.property('codec', 'dag-pb')
expect(cid).to.have.property('version', 1)
expect(cid).to.have.property('multihash')
expect(cid).to.have.property('multibaseName', 'base58btc')

expect(cid.toBaseEncodedString()).to.be.eql(cidStr)
})

it('handles CID (no multibase)', () => {
const cidStr = 'zdj7Wd8AMwqnhJGQCbFxBVodGSBG84TM7Hs1rcJuQMwTyfEDS'
const cidStr = 'bafybeidskjjd4zmr7oh6ku6wp72vvbxyibcli2r6if3ocdcy7jjjusvl2u'
const cidBuf = Buffer.from('017012207252523e6591fb8fe553d67ff55a86f84044b46a3e4176e10c58fa529a4aabd5', 'hex')

const cid = new CID(cidBuf)

expect(cid).to.have.property('codec', 'dag-pb')
expect(cid).to.have.property('version', 1)
expect(cid).to.have.property('multihash')
expect(cid).to.have.property('multibaseName', 'base32')

expect(cid.toBaseEncodedString()).to.be.eql(cidStr)
})
Expand All @@ -131,6 +136,7 @@ describe('CID', () => {
expect(cid).to.have.property('codec', 'dag-cbor')
expect(cid).to.have.property('version', 1)
expect(cid).to.have.property('multihash')
expect(cid).to.have.property('multibaseName', 'base32')
})

it('can roundtrip through cid.toBaseEncodedString()', () => {
Expand All @@ -140,6 +146,7 @@ describe('CID', () => {
expect(cid1).to.have.property('codec').that.eql(cid2.codec)
expect(cid1).to.have.property('version').that.eql(cid2.version)
expect(cid1).to.have.property('multihash').that.eql(cid2.multihash)
expect(cid1).to.have.property('multibaseName').that.eql(cid2.multibaseName)
})

it('handles multibyte varint encoded codec codes', () => {
Expand All @@ -151,9 +158,11 @@ describe('CID', () => {
expect(cid1).to.have.property('codec', 'eth-block')
expect(cid1).to.have.property('version', 1)
expect(cid1).to.have.property('multihash').that.eql(mh)
expect(cid1).to.have.property('multibaseName', 'base32')
expect(cid2).to.have.property('codec', 'eth-block')
expect(cid2).to.have.property('version', 1)
expect(cid2).to.have.property('multihash').that.eql(mh)
expect(cid2).to.have.property('multibaseName', 'base32')
})

it('.prefix', () => {
Expand Down Expand Up @@ -199,9 +208,9 @@ describe('CID', () => {

it('returns a string in the base provided', () => {
const b58v1Str = 'zdj7Wd8AMwqnhJGQCbFxBVodGSBG84TM7Hs1rcJuQMwTyfEDS'
const b32v1Str = 'bafybeidskjjd4zmr7oh6ku6wp72vvbxyibcli2r6if3ocdcy7jjjusvl2u'
const b64urlv1Str = 'uAXASIHJSUj5lkfuP5VPWf_VahvhARLRqPkF24QxY-lKaSqvV'
const cid = new CID(b58v1Str)
expect(cid.toString('base32')).to.equal(b32v1Str)
expect(cid.toString('base64url')).to.equal(b64urlv1Str)
})
})

Expand Down