Skip to content

Instantly share code, notes, and snippets.

@SomajitDey
Created October 1, 2021 12:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SomajitDey/980c6bed49fc90c1a39a7bc922df46dc to your computer and use it in GitHub Desktop.
Save SomajitDey/980c6bed49fc90c1a39a7bc922df46dc to your computer and use it in GitHub Desktop.
Convert any IPFS / Libp2p CID to Base36
base36 linguist-language=bash
#!/usr/bin/env bash
#
# Make sure /p2p and subdomain are enabled for both localhost and 127.0.0.1 public gateways
## ipfs config --json Gateway.PublicGateways '{"localhost": {"Paths":["/p2p"],"UseSubdomains": true}}'
## ipfs config --json Gateway.PublicGateways '{"127.0.0.1": {"Paths":["/p2p"],"UseSubdomains": true}}'
## No idea why both localhost and 127.0.0.1 need to be set this way, but otherwise doesn't work
#
# Run `ipfs daemon [--routing=none]` # Use the option to save bandwidth, if needed
#
# Usage: base36 <cid> , or, echo <cid> | base36
## Gateway port to be passed through environment variable PORT, def: 8080
## Stdout: Base36 CIDv1 with codec=protobuf
## Stderr: Base36 CIDv1 with codec=libp2p-key
set -o pipefail
cid="${1:-"$(head -n1)"}"
port="${PORT:-8080}"
curl -sSI localhost:"${port}"/p2p/"${cid}"/http/ | \
sed -n '/^Location:/s/.*\/\([a-z0-9]*\).p2p..*/\1/p' | \
tee /dev/stderr | \
ipfs cid format -v1 --codec=protobuf -b base36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment