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

Commit

Permalink
feat: preserve base when constructed from a string
Browse files Browse the repository at this point in the history
BREAKING CHANGE - previously base was not preserved and all CIDs would
be normalised to base58btc when asking for their string representation.

The default will change to base32 in https://github.com/multiformats/js-cid/pull/73/files

The idea behind this change is that we shouldnt lose information when
the user passes us a base encoded string, but keep it and use it as
the default base so toString returns the same string they provided.

I'd like this as a fix for ipld explorer, which currently forces all
CIDs into base58btc, seee: ipfs/ipfs-webui#999

This PR is the smallest change I can see to make it work. Do we want
to also add base as a constructor parameter? If so, i guess it should
go at the end as it's optional, but thats aesthetically displeasing
as the base encoding goes at the front in string form.

License: MIT
Signed-off-by: Oli Evans <oli@tableflip.io>
  • Loading branch information
olizilla committed Apr 3, 2019
1 parent 2c06315 commit d59b820
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
15 changes: 13 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,21 @@ class CID {
*
*/
constructor (version, codec, multihash) {
let base = null
if (module.exports.isCID(version)) {
let cid = version
this.version = cid.version
this.codec = cid.codec
this.multihash = Buffer.from(cid.multihash)
if (cid.base) {
this.base = cid.base
}
return
}
if (typeof version === 'string') {
if (multibase.isEncoded(version)) { // CID String (encoded with multibase)
// base is the base name e.g. 'base32' or false
base = multibase.isEncoded(version)
if (base) { // CID String (encoded with multibase)
const cid = multibase.decode(version)
version = parseInt(cid.slice(0, 1).toString('hex'), 16)
codec = multicodec.getCodec(cid.slice(1))
Expand All @@ -95,6 +101,11 @@ class CID {
}
}

/**
* @type {string}
*/
this.base = base || 'base58btc'

/**
* @type {string}
*/
Expand Down Expand Up @@ -197,7 +208,7 @@ class CID {
* @returns {string}
*/
toBaseEncodedString (base) {
base = base || 'base58btc'
base = base || this.base

switch (this.version) {
case 0: {
Expand Down
39 changes: 26 additions & 13 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,32 @@ describe('CID', () => {
})
})

describe('.toString', () => {
it('returns a CID string', () => {
const cid = new CID(hash)
expect(cid.toString()).to.equal('QmatYkNGZnELf8cAGdyJpUca2PyY4szai3RHyyWofNY1pY')
})

it('returns a string in the same base as the string passed to the constructor - base64 flavour', () => {
const base64Str = 'mAXASIOnrbGCADfkPyOI37VMkbzluh1eaukBqqnl2oFaFnuIt'
const cid = new CID(base64Str)
expect(cid.toString()).to.equal(base64Str)
})

it('returns a string in the same base as the string passed to the constructor - base16 flavour', () => {
const base16Str = 'f01701220e9eb6c60800df90fc8e237ed53246f396e87579aba406aaa7976a056859ee22d'
const cid = new CID(base16Str)
expect(cid.toString()).to.equal(base16Str)
})

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

describe('utilities', () => {
const h1 = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'
const h2 = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1o'
Expand Down Expand Up @@ -207,19 +233,6 @@ describe('CID', () => {
CID.isCID(new CID(h1).toV1())
).to.equal(true)
})

it('.toString() outputs default base encoded CID', () => {
const mhStr = 'QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n'
const cid = new CID(mhStr)
expect(`${cid}`).to.equal(mhStr)
})

it('.toString(base) outputs base encoded CID', () => {
const b58v1Str = 'zdj7Wd8AMwqnhJGQCbFxBVodGSBG84TM7Hs1rcJuQMwTyfEDS'
const b32v1Str = 'bafybeidskjjd4zmr7oh6ku6wp72vvbxyibcli2r6if3ocdcy7jjjusvl2u'
const cid = new CID(b58v1Str)
expect(cid.toString('base32')).to.equal(b32v1Str)
})
})

describe('throws on invalid inputs', () => {
Expand Down

0 comments on commit d59b820

Please sign in to comment.