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

Commit 31f00f0

Browse files
author
Alan Shaw
committed
refactor: default to base32 encoding for v1 CIDs
This switches the default encoding for v1 CIDs from base58btc to base32. See ipfs/js-ipfs#1995 for details. BREAKING CHANGE: The default string encoding for v1 CIDs has changed from base58btc to base32. License: MIT Signed-off-by: Alan Shaw <alan.shaw@protocol.ai>
1 parent c29d12e commit 31f00f0

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

Diff for: src/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class CID {
5959
* new CID(<bs58 encoded multihash>)
6060
* new CID(<cid>)
6161
*/
62-
constructor (version, codec, multihash, multibaseName = 'base58btc') {
62+
constructor (version, codec, multihash, multibaseName) {
6363
if (module.exports.isCID(version)) {
6464
// version is an exising CID instance
6565
const cid = version
@@ -95,13 +95,13 @@ class CID {
9595
if (Buffer.isBuffer(version)) {
9696
const firstByte = version.slice(0, 1)
9797
const v = parseInt(firstByte.toString('hex'), 16)
98-
if (v === 0 || v === 1) {
98+
if (v === 1) {
9999
// version is a CID buffer
100100
const cid = version
101101
this.version = v
102102
this.codec = multicodec.getCodec(cid.slice(1))
103103
this.multihash = multicodec.rmPrefix(cid.slice(1))
104-
this.multibaseName = (v === 0) ? 'base58btc' : multibaseName
104+
this.multibaseName = 'base32'
105105
} else {
106106
// version is a raw multihash buffer, so v0
107107
this.version = 0
@@ -133,7 +133,7 @@ class CID {
133133
/**
134134
* @type {string}
135135
*/
136-
this.multibaseName = multibaseName
136+
this.multibaseName = multibaseName || (version === 0 ? 'base58btc' : 'base32')
137137

138138
CID.validateCID(this)
139139
}

Diff for: test/index.spec.js

+12-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ describe('CID', () => {
3232
expect(cid).to.have.property('codec', 'dag-pb')
3333
expect(cid).to.have.property('version', 0)
3434
expect(cid).to.have.property('multihash').that.eql(multihash.fromB58String(mhStr))
35+
expect(cid).to.have.property('multibaseName', 'base58btc')
3536

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

5052
expect(cid.toBaseEncodedString()).to.eql(mhStr)
5153
done()
@@ -58,6 +60,7 @@ describe('CID', () => {
5860
expect(cid).to.have.property('codec', 'dag-pb')
5961
expect(cid).to.have.property('version', 0)
6062
expect(cid).to.have.property('multihash')
63+
expect(cid).to.have.property('multibaseName', 'base58btc')
6164
})
6265

6366
it('throws on invalid BS58Str multihash ', () => {
@@ -108,19 +111,21 @@ describe('CID', () => {
108111
expect(cid).to.have.property('codec', 'dag-pb')
109112
expect(cid).to.have.property('version', 1)
110113
expect(cid).to.have.property('multihash')
114+
expect(cid).to.have.property('multibaseName', 'base58btc')
111115

112116
expect(cid.toBaseEncodedString()).to.be.eql(cidStr)
113117
})
114118

115119
it('handles CID (no multibase)', () => {
116-
const cidStr = 'zdj7Wd8AMwqnhJGQCbFxBVodGSBG84TM7Hs1rcJuQMwTyfEDS'
120+
const cidStr = 'bafybeidskjjd4zmr7oh6ku6wp72vvbxyibcli2r6if3ocdcy7jjjusvl2u'
117121
const cidBuf = Buffer.from('017012207252523e6591fb8fe553d67ff55a86f84044b46a3e4176e10c58fa529a4aabd5', 'hex')
118122

119123
const cid = new CID(cidBuf)
120124

121125
expect(cid).to.have.property('codec', 'dag-pb')
122126
expect(cid).to.have.property('version', 1)
123127
expect(cid).to.have.property('multihash')
128+
expect(cid).to.have.property('multibaseName', 'base32')
124129

125130
expect(cid.toBaseEncodedString()).to.be.eql(cidStr)
126131
})
@@ -131,6 +136,7 @@ describe('CID', () => {
131136
expect(cid).to.have.property('codec', 'dag-cbor')
132137
expect(cid).to.have.property('version', 1)
133138
expect(cid).to.have.property('multihash')
139+
expect(cid).to.have.property('multibaseName', 'base32')
134140
})
135141

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

145152
it('handles multibyte varint encoded codec codes', () => {
@@ -151,9 +158,11 @@ describe('CID', () => {
151158
expect(cid1).to.have.property('codec', 'eth-block')
152159
expect(cid1).to.have.property('version', 1)
153160
expect(cid1).to.have.property('multihash').that.eql(mh)
161+
expect(cid1).to.have.property('multibaseName', 'base32')
154162
expect(cid2).to.have.property('codec', 'eth-block')
155163
expect(cid2).to.have.property('version', 1)
156164
expect(cid2).to.have.property('multihash').that.eql(mh)
165+
expect(cid2).to.have.property('multibaseName', 'base32')
157166
})
158167

159168
it('.prefix', () => {
@@ -199,9 +208,9 @@ describe('CID', () => {
199208

200209
it('returns a string in the base provided', () => {
201210
const b58v1Str = 'zdj7Wd8AMwqnhJGQCbFxBVodGSBG84TM7Hs1rcJuQMwTyfEDS'
202-
const b32v1Str = 'bafybeidskjjd4zmr7oh6ku6wp72vvbxyibcli2r6if3ocdcy7jjjusvl2u'
211+
const b64urlv1Str = 'uAXASIHJSUj5lkfuP5VPWf_VahvhARLRqPkF24QxY-lKaSqvV'
203212
const cid = new CID(b58v1Str)
204-
expect(cid.toString('base32')).to.equal(b32v1Str)
213+
expect(cid.toString('base64url')).to.equal(b64urlv1Str)
205214
})
206215
})
207216

0 commit comments

Comments
 (0)