Command-line to transform base58 PeerID to subdomain-friendly base36

How to transform a base58 PeerID or key to base36 CIDv1 from command-line, while offline?

Constraints: Can’t use or the redirect (Location header) from a subdomain gateway.

More specifically, fill in the blank below

ipfs cid format ___________ 12D3KooWDGbVHU5r58JEiVJXVhM9fcCYaSacm8GxWe5C68yAJpNn

Output>> k50rm9yjlt0jcavdtmewsd7gi3r4zmizb824gwt1ddwtws7imugv7339z4dhqb

Using local subdomain gateway, it’s a rather easy hack.

If it is a libp2p-key under your control, you can get them directly:

  • ipfs id --peerid-base base36 (self key used for your PeerID)
  • ipfs key list -l --ipns-base base36 (all IPNS keys)

Third-party PeerIDs that start with Qm.. (RSA keys):

  • ipfs cid format -v 1 -b base36 --codec libp2p-key QmLegacyPeerIDInBase58 (upgrade legacy PeerID encoded as a Multihash in Base58 to self-describing CIDv1 with libp2p codec)

Third-party PeerIDs that start with 12... (inlined ED25519) were added recently and working with them is bit awkward at the moment. Such PeerID notation is not a CIDv0, so can’t update them to CIDv1 with ipfs cid, but with go-ipfs 0.10.0+ this should work (thx @adin):

  • echo -n 12D3PeerIDInBase58 | awk '{printf "z" $0}' | ipfs multibase transcode -b=base16 | cut -c2- | awk '{printf "f0170" $0}' | ipfs multibase transcode -b=base36

Finally, below solutions will work for all PeerID types:

  • ipfs id --peerid-base base36 12D3PeerIDInBase58 | jq --raw-output .ID (downside: requires connection to the peer due to a bug)
  • subdomain gateway, as described by @SomajitDey above

We most likely will switch PeerID output to CIDv1 everywhere in near future, so regular ipfs cid will be enough.

1 Like