Skip to content

Commit

Permalink
feat: provide cidv1 in base36 when needed
Browse files Browse the repository at this point in the history
This adds Base36 version  of CIDv1 if it fits inside of a single DNS
label. Follows convention introduced in:
ipfs/kubo#7441
  • Loading branch information
lidel committed Aug 26, 2020
1 parent 4d2d990 commit 07c4a49
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
12 changes: 10 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,20 @@ <h1 class='f3 fw2 montserrat aqua ttu'>CID Inspector</h1>
<div class='f5 sans-serif ma0 fw4 pt2'id="multihash"></div>
</div>
<div class='pt4'>
<label class='dib'>
<a class='db pb1 w-100 tracked ttu f6 teal-muted hover-aqua link' href="https://docs.ipfs.io/concepts/content-addressing/#version-1-v1">
CIDv1 (Base32)
</a>
</label>
<div class='f5 monospace ma0 fw4 pt2' id="base32cidv1"></div>
</div>
<div class='pt4' id='dns'>
<label class='dib'>
<a class='db pb1 w-100 tracked ttu f6 teal-muted hover-aqua link' href="https://docs.ipfs.io/how-to/address-ipfs-on-web/#subdomain-gateway">
Base32 CIDv1
CIDv1 (Base36, optimized for Subdomains)
</a>
</label>
<div class='f5 sans-serif ma0 fw4 pt2'id="base32cidv1"></div>
<div class='f5 monospace ma0 fw4 pt2' id="dnscidv1"></div>
</div>
</section>
<footer class='mt2 pv2 ph2 ph3-l bt b--light-gray'>
Expand Down
22 changes: 21 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const multihash = require('multihashes')
const multibaseConstants = require('multibase/src/constants')
const mutlicodecVarintTable = require('multicodec/src/varint-table')

// Label's max length in DNS (https://tools.ietf.org/html/rfc1034#page-7)
const dnsLabelMaxLength = 63

// cidv0 ::= <multihash-content-address>
// QmRds34t1KFiatDY6yJFj8U9VPTLvSMsR63y7qdUV3RMmT
// <cidv1> ::= <multibase-prefix><cid-version><multicodec-content-type><multihash-content-address>
Expand Down Expand Up @@ -39,6 +42,15 @@ function toBase32(value) {
return cid.toV1().toBaseEncodedString('base32')
}

function toDNSPrefix(value) {
const cid = new CID(value)
const cidb32 = cid.toV1().toBaseEncodedString('base32')
if (cidb32.length <= dnsLabelMaxLength) return cidb32
const cidb36 = cid.toV1().toBaseEncodedString('base36')
if (cidb36.length <= dnsLabelMaxLength) return cidb36
return 'CID incompatible with DNS label length limit of 63'
}

function decodeCidV1 (value, cid) {
return {
cid,
Expand All @@ -58,6 +70,8 @@ document.addEventListener('DOMContentLoaded', () => {
const multicodecOutput = document.querySelector('#multicodec')
const multibaseOutput = document.querySelector('#multibase')
const base32CidV1Output = document.querySelector('#base32cidv1')
const dns = document.querySelector('#dns')
const dnsCidV1Output = document.querySelector('#dnscidv1')
const humanReadableCidOutput = document.querySelector('#hr-cid')
const errorOutput = document.querySelector('#input-error')

Expand All @@ -76,7 +90,13 @@ document.addEventListener('DOMContentLoaded', () => {
multibaseOutput.innerHTML = toDefinitionList({code: data.multibase.code, name: data.multibase.name})
multicodecOutput.innerHTML = toDefinitionList({code: data.multicodec.code, name: data.multicodec.name})
multihashOutput.innerHTML = toDefinitionList({code: data.multihash.code, name: data.multihash.name, bits: data.multihash.length * 8})
base32CidV1Output.innerHTML = toBase32(value.trim())

const cidb32 = toBase32(value.trim())
base32CidV1Output.innerHTML = cidb32
const dnsPrefix = toDNSPrefix(value.trim())
dns.style.visibility = cidb32 !== dnsPrefix ? 'visible' : 'hidden'
dnsCidV1Output.innerHTML = dnsPrefix

clearErrorOutput()
} catch (err) {
if (!value) {
Expand Down

0 comments on commit 07c4a49

Please sign in to comment.