Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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