Skip to content

Commit 892f198

Browse files
authored
fix: CID.parse(base36) (#307)
This aims to support base36 out of the box, without having to specify the 'base' parameter explicitly. Rationale is that base36 is used across ecosystem for PeerIDs that have to be represented in base36 to fit in a single DNS label (due to limit of 63 characters).
1 parent 2189443 commit 892f198

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/cid.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { base32 } from './bases/base32.js'
2+
import { base36 } from './bases/base36.js'
23
import { base58btc } from './bases/base58.js'
34
import { coerce } from './bytes.js'
45
import * as Digest from './hashes/digest.js'
@@ -402,10 +403,14 @@ function parseCIDtoBytes <Prefix extends string, Data, Code extends number, Alg
402403
const decoder = base ?? base32
403404
return [base32.prefix as Prefix, decoder.decode(source)]
404405
}
406+
case base36.prefix: {
407+
const decoder = base ?? base36
408+
return [base36.prefix as Prefix, decoder.decode(source)]
409+
}
405410
default: {
406411
if (base == null) {
407412
throw Error(
408-
'To parse non base32 or base58btc encoded CID multibase decoder must be provided'
413+
'To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided'
409414
)
410415
}
411416
return [source[0] as Prefix, base.decode(source)]

test/test-cid.spec.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { assert } from 'aegir/chai'
44
import OLDCID from 'cids'
55
import { base32 } from '../src/bases/base32.js'
6+
import { base36 } from '../src/bases/base36.js'
67
import { base58btc } from '../src/bases/base58.js'
78
import { base64 } from '../src/bases/base64.js'
89
import { fromHex, toHex, equals } from '../src/bytes.js'
@@ -568,7 +569,15 @@ describe('CID', () => {
568569
const hash = await sha256.digest(textEncoder.encode('abc'))
569570
const cid = CID.create(1, 112, hash)
570571

571-
const parsed = CID.parse(cid.toString())
572+
const parsed = CID.parse(cid.toString(base32))
573+
digestsame(cid, parsed)
574+
})
575+
576+
it('parse 36 encoded CIDv1', async () => {
577+
const hash = await sha256.digest(textEncoder.encode('abc'))
578+
const cid = CID.create(1, 112, hash)
579+
580+
const parsed = CID.parse(cid.toString(base36))
572581
digestsame(cid, parsed)
573582
})
574583

@@ -592,7 +601,7 @@ describe('CID', () => {
592601
const hash = await sha256.digest(textEncoder.encode('abc'))
593602
const cid = CID.create(1, 112, hash)
594603
const msg =
595-
'To parse non base32 or base58btc encoded CID multibase decoder must be provided'
604+
'To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided'
596605

597606
assert.throws(() => CID.parse(cid.toString(base64)), msg)
598607
})

0 commit comments

Comments
 (0)