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

refactor: base32 encode CIDs by default #73

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 5 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,21 +185,21 @@ class CID {
/**
* Encode the CID into a string.
*
* @param {string} [base='base58btc'] - Base encoding to use.
* @param {string} [base='base32'] - Base encoding to use. For v0 CIDs this
* defaults to 'base58btc' and if passed MUST have that value. For v1 CIDs
* it defaults to base32.
* @returns {string}
*/
toBaseEncodedString (base) {
base = base || 'base58btc'

switch (this.version) {
case 0: {
if (base !== 'base58btc') {
if (base && 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()
return multibase.encode(base || 'base32', this.buffer).toString()
default:
throw new Error('Unsupported version')
}
Expand Down
4 changes: 2 additions & 2 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('CID', () => {
expect(cid).to.have.property('version', 1)
expect(cid).to.have.property('multihash')

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

it('handles CID (no multibase)', () => {
Expand All @@ -110,7 +110,7 @@ describe('CID', () => {
expect(cid).to.have.property('version', 1)
expect(cid).to.have.property('multihash')

expect(cid.toBaseEncodedString()).to.be.eql(cidStr)
expect(cid.toBaseEncodedString('base58btc')).to.be.eql(cidStr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please change the expect to use no base parameter, else the test doesn't match its description and is the same test as the one above.

})

it('create by parts', () => {
Expand Down