Skip to content

Commit b357efd

Browse files
committed
feat: cid from multihash buffer
1 parent 81ebff7 commit b357efd

File tree

2 files changed

+60
-37
lines changed

2 files changed

+60
-37
lines changed

cid.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Following code pattern:
2727
2828
if (CID.isCID(value)) {
2929
doSomethingWithCID(value)
30-
}
30+
}
3131
3232
Is replaced with:
3333
@@ -100,6 +100,13 @@ export default multiformats => {
100100
readonly(this, 'buffer', cid)
101101
let code
102102
;[code, cid] = parse(cid)
103+
if (code === 18) {
104+
// CIDv0
105+
readonly(this, 'version', 0)
106+
readonly(this, 'code', 0x70)
107+
this._multihash = this.buffer
108+
return
109+
}
103110
if (code > 1) throw new Error(`Invalid CID version ${code}`)
104111
readonly(this, 'version', code)
105112
;[code, cid] = parse(cid)

test/test-cid.js

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* globals before, describe, it */
1+
/* globals describe, it */
22
import crypto from 'crypto'
33
import OLDCID from 'cids'
44
import assert from 'assert'
@@ -48,11 +48,6 @@ describe('CID', () => {
4848
]
4949
multihash.add(hashes)
5050
const b58 = multibase.get('base58btc')
51-
let hash
52-
53-
before(async () => {
54-
hash = await multihash.hash(Buffer.from('abc'), 'sha2-256')
55-
})
5651

5752
describe('v0', () => {
5853
test('handles B58Str multihash', () => {
@@ -66,7 +61,8 @@ describe('CID', () => {
6661
same(cid.toString(), mhStr)
6762
})
6863

69-
test('create by parts', () => {
64+
test('create by parts', async () => {
65+
const hash = await multihash.hash(Buffer.from('abc'), 'sha2-256')
7066
const cid = new CID(0, 112, hash)
7167

7268
same(cid.code, 112)
@@ -76,17 +72,30 @@ describe('CID', () => {
7672
same(cid.toString(), b58.encode(hash))
7773
})
7874

79-
test('throws on invalid BS58Str multihash ', () => {
75+
test('create from multihash', async () => {
76+
const hash = await multihash.hash(Buffer.from('abc'), 'sha2-256')
77+
const cid = new CID(hash)
78+
79+
same(cid.code, 112)
80+
same(cid.version, 0)
81+
same(cid.multihash, hash)
82+
cid.toString()
83+
same(cid.toString(), b58.encode(hash))
84+
})
85+
86+
test('throws on invalid BS58Str multihash ', async () => {
8087
const msg = 'Non-base58 character'
8188
testThrow(() => new CID('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zIII'), msg)
8289
})
8390

84-
test('throws on trying to create a CIDv0 with a codec other than dag-pb', () => {
91+
test('throws on trying to create a CIDv0 with a codec other than dag-pb', async () => {
92+
const hash = await multihash.hash(Buffer.from('abc'), 'sha2-256')
8593
const msg = 'Version 0 CID must be 112 codec (dag-cbor)'
8694
testThrow(() => new CID(0, 113, hash), msg)
8795
})
8896

89-
test('throws on trying to pass specific base encoding [deprecated]', () => {
97+
test('throws on trying to pass specific base encoding [deprecated]', async () => {
98+
const hash = await multihash.hash(Buffer.from('abc'), 'sha2-256')
9099
const msg = 'No longer supported, cannot specify base encoding in instantiation'
91100
testThrow(() => new CID(0, 112, hash, 'base32'), msg)
92101
})
@@ -98,7 +107,8 @@ describe('CID', () => {
98107
testThrow(() => cid.toString('base32'), msg)
99108
})
100109

101-
test('.buffer', () => {
110+
test('.buffer', async () => {
111+
const hash = await multihash.hash(Buffer.from('abc'), 'sha2-256')
102112
const codec = 112
103113
const cid = new CID(0, codec, hash)
104114
const buffer = cid.buffer
@@ -134,14 +144,16 @@ describe('CID', () => {
134144
same(cid.toString(), cidStr)
135145
})
136146

137-
test('create by parts', () => {
147+
test('create by parts', async () => {
148+
const hash = await multihash.hash(Buffer.from('abc'), 'sha2-256')
138149
const cid = new CID(1, 0x71, hash)
139150
same(cid.code, 0x71)
140151
same(cid.version, 1)
141152
same(cid.multihash, hash)
142153
})
143154

144-
test('can roundtrip through cid.toString()', () => {
155+
test('can roundtrip through cid.toString()', async () => {
156+
const hash = await multihash.hash(Buffer.from('abc'), 'sha2-256')
145157
const cid1 = new CID(1, 0x71, hash)
146158
const cid2 = new CID(cid1.toString())
147159

@@ -167,7 +179,8 @@ describe('CID', () => {
167179
})
168180
*/
169181

170-
test('.buffer', () => {
182+
test('.buffer', async () => {
183+
const hash = await multihash.hash(Buffer.from('abc'), 'sha2-256')
171184
const code = 0x71
172185
const cid = new CID(1, code, hash)
173186
const buffer = cid.buffer
@@ -248,13 +261,6 @@ describe('CID', () => {
248261
test(name, () => testThrowAny(() => new CID(1, 112, i)))
249262
}
250263
invalid.forEach(mapper)
251-
252-
const invalidVersions = [-1, 2]
253-
mapper = i => {
254-
const name = `new CID(${i}, 112, buffer)`
255-
test(name, () => testThrowAny(new CID(i, 112, hash)))
256-
}
257-
invalidVersions.forEach(mapper)
258264
})
259265

260266
describe('idempotence', () => {
@@ -301,7 +307,8 @@ describe('CID', () => {
301307
assert.ok(cid.buffer)
302308
same(cid.buffer, cid.buffer)
303309
})
304-
test('should cache string representation when it matches the multibaseName it was constructed with', () => {
310+
test('should cache string representation when it matches the multibaseName it was constructed with', async () => {
311+
const hash = await multihash.hash(Buffer.from('abc'), 'sha2-256')
305312
const cid = new CID(1, 112, hash)
306313
same(cid._baseCache.size, 0)
307314

@@ -323,17 +330,20 @@ describe('CID', () => {
323330
})
324331
})
325332

326-
test('toJSON()', () => {
333+
test('toJSON()', async () => {
334+
const hash = await multihash.hash(Buffer.from('abc'), 'sha2-256')
327335
const cid = new CID(1, 112, hash)
328336
same(cid.toJSON(), { code: 112, version: 1, hash })
329337
})
330338

331-
test('isCID', () => {
339+
test('isCID', async () => {
340+
const hash = await multihash.hash(Buffer.from('abc'), 'sha2-256')
332341
const cid = new CID(1, 112, hash)
333342
assert.ok(OLDCID.isCID(cid))
334343
})
335344

336-
test('asCID', () => {
345+
test('asCID', async () => {
346+
const hash = await multihash.hash(Buffer.from('abc'), 'sha2-256')
337347
class IncompatibleCID {
338348
constructor (version, code, multihash) {
339349
this.version = version
@@ -349,9 +359,9 @@ describe('CID', () => {
349359

350360
const version = 1
351361
const code = 112
352-
const multihash = hash
362+
const _multihash = hash
353363

354-
const incompatibleCID = new IncompatibleCID(version, code, multihash)
364+
const incompatibleCID = new IncompatibleCID(version, code, _multihash)
355365
assert.ok(CID.isCID(incompatibleCID))
356366
assert.strictEqual(incompatibleCID.toString(), '[object Object]')
357367
assert.strictEqual(typeof incompatibleCID.toV0, 'undefined')
@@ -360,18 +370,18 @@ describe('CID', () => {
360370
assert.ok(cid1 instanceof CID)
361371
assert.strictEqual(cid1.code, code)
362372
assert.strictEqual(cid1.version, version)
363-
assert.strictEqual(cid1.multihash, multihash)
373+
assert.strictEqual(cid1.multihash, _multihash)
364374

365-
const cid2 = CID.asCID({ version, code, multihash })
375+
const cid2 = CID.asCID({ version, code, _multihash })
366376
assert.strictEqual(cid2, null)
367377

368-
const duckCID = { version, code, multihash }
378+
const duckCID = { version, code, multihash: _multihash }
369379
duckCID.asCID = duckCID
370380
const cid3 = CID.asCID(duckCID)
371381
assert.ok(cid3 instanceof CID)
372382
assert.strictEqual(cid3.code, code)
373383
assert.strictEqual(cid3.version, version)
374-
assert.strictEqual(cid3.multihash, multihash)
384+
assert.strictEqual(cid3.multihash, _multihash)
375385

376386
const cid4 = CID.asCID(cid3)
377387
assert.strictEqual(cid3, cid4)
@@ -383,42 +393,48 @@ describe('CID', () => {
383393
assert.strictEqual(cid5.code, 85)
384394
})
385395

386-
test('new CID from old CID', () => {
396+
test('new CID from old CID', async () => {
397+
const hash = await multihash.hash(Buffer.from('abc'), 'sha2-256')
387398
const cid = new CID(new OLDCID(1, 'raw', Buffer.from(hash)))
388399
same(cid.version, 1)
389400
same(cid.multihash, hash)
390401
same(cid.code, 85)
391402
})
392403

393404
if (!process.browser) {
394-
test('util.inspect', () => {
405+
test('util.inspect', async () => {
406+
const hash = await multihash.hash(Buffer.from('abc'), 'sha2-256')
395407
const cid = new CID(1, 112, hash)
396408
same(util.inspect(cid), 'CID(bafybeif2pall7dybz7vecqka3zo24irdwabwdi4wc55jznaq75q7eaavvu)')
397409
})
398410
}
399411

400-
describe('deprecations', () => {
412+
describe('deprecations', async () => {
401413
test('codec', async () => {
414+
const hash = await multihash.hash(Buffer.from('abc'), 'sha2-256')
402415
const cid = new CID(1, 112, hash)
403416
await testThrow(() => cid.codec, '"codec" property is deprecated, use integer "code" property instead')
404417
await testThrow(() => new CID(1, 'dag-pb', hash), 'String codecs are no longer supported')
405418
})
406419
test('multibaseName', async () => {
420+
const hash = await multihash.hash(Buffer.from('abc'), 'sha2-256')
407421
const cid = new CID(1, 112, hash)
408422
await testThrow(() => cid.multibaseName, '"multibaseName" property is deprecated')
409423
})
410424
test('prefix', async () => {
425+
const hash = await multihash.hash(Buffer.from('abc'), 'sha2-256')
411426
const cid = new CID(1, 112, hash)
412427
await testThrow(() => cid.prefix, '"prefix" property is deprecated')
413428
})
414429
test('toBaseEncodedString()', async () => {
430+
const hash = await multihash.hash(Buffer.from('abc'), 'sha2-256')
415431
const cid = new CID(1, 112, hash)
416432
await testThrow(() => cid.toBaseEncodedString(), 'Deprecated, use .toString()')
417433
})
418434
})
419435

420436
test('invalid CID version', async () => {
421-
const encoded = varint.encode(18)
422-
await testThrow(() => new CID(encoded), 'Invalid CID version 18')
437+
const encoded = varint.encode(2)
438+
await testThrow(() => new CID(encoded), 'Invalid CID version 2')
423439
})
424440
})

0 commit comments

Comments
 (0)