atproto—crates

Tl.

Content identifiers · 0.15.0 · MIT

atpcid

atpcid computes the CID of a record or a file. It reads JSON from its arguments, from stdin, or from files. A JSON object gets the DAG-CBOR record CID the network computes for it, and anything else gets a raw CID.

§ 01

Install

atpcid is a binary in the atproto-dasl crate, behind its clap feature. Homebrew has prebuilt binaries for macOS and Linux.

Homebrew or crates.ioListing 1
brew tap ngerakines/tap
brew install atpcid

# or
cargo install atproto-dasl --version 0.15.0 --features clap --bin atpcid

Leave out --features clap and Cargo warns that atpcid requires it, then exits 0 without installing anything. Check what you got with atpcid --version, which prints atpcid 0.15.0.

§ 02

How it picks a CID

atpcid looks at the first byte of each input that isn’t whitespace. If it’s {, the input is a JSON record and gets a DAG-CBOR record CID: CIDv1, codec 0x71, SHA-256, starting bafyrei…. Anything else, a JSON array included, gets a raw CID: CIDv1, codec 0x55, SHA-256, starting bafkrei….

A record goes through the AT Protocol data model before it’s hashed, with compute_cid. A {"$link": …} object becomes a tag-42 CID link and a {"$bytes": …} object becomes a byte string, so a record that carries a blob gets the CID its PDS reports. The data model doesn’t have floats, so 1.5 is refused. 1.0 names an integer and hashes as 1.

--raw and --record override the guess, and they can’t be combined. Use --raw for any file you’ll upload as a blob, JSON included. A blob’s CID is the hash of its bytes, and a JSON file starting with { would otherwise get a record CID that a PDS won’t report. --record refuses input that isn’t JSON, where the default would hash it raw.

§ 03

Usage

atpcid [OPTIONS] [INPUTS]... prints one CID per line, one for each input. There aren’t any subcommands.

Arguments and options at 0.15.0
ArgumentDoes
[INPUTS]...The data to hash. Each argument is one input and produces one CID. With --files, each argument is a file path.
--Read one input from stdin instead of the arguments. Can’t be combined with --files.
--rawForce a raw CID (codec 0x55).
--recordForce a DAG-CBOR record CID (codec 0x71). The input has to be JSON.
--filesTreat the arguments as file paths and hash their contents.
-h, --helpPrint help. --help includes the detection rules and examples.
-V, --versionPrint the version.

Stdin needs the --. The help says that an empty argument list reads stdin, but at 0.15.0 it doesn’t. It exits 1 with no input provided; pass data as arguments, use -- for stdin, or use --files.

§ 04

Worked examples

Every output below is what atpcid 0.15.0 printed.

Arguments and stdinListing 2
$ atpcid '{"text":"hello"}'
bafyreidefdycgbfy3oglcb6ism3eqhyp5llsrpzxjsuac2gsy4mtrtx244

$ atpcid '{"text":"hello"}' '{"text":"world"}'
bafyreidefdycgbfy3oglcb6ism3eqhyp5llsrpzxjsuac2gsy4mtrtx244
bafyreidpw4cbv6gr4ukh33z23pvvrpr3wi4gnpmi4doamlsl3sa4rgri2a

$ echo '{"text":"hello"}' | atpcid --
bafyreidefdycgbfy3oglcb6ism3eqhyp5llsrpzxjsuac2gsy4mtrtx244

# The same bytes, hashed as bytes.
$ atpcid --raw '{"text":"hello"}'
bafkreiglxpone5usgrg6lw5lhk6kxjat7mhukmdsm7phbakacv3n6hfroy

$ printf 'hello' | atpcid --
bafkreibm6jg3ux5qumhcn2b3flc3tyu6dmlb4xa7u5bf44yegnrjhc4yeq

The next two check atpcid against the network. The first fetches the profile record of ngerakines.me from its PDS and hashes the record’s value. The CID matches the one the PDS reports, because the avatar and banner blobs inside it hash as links.

A record’s CID, checked against its PDSListing 3
$ curl -s 'https://vesuvius.pyroclastic.cloud/xrpc/com.atproto.repo.getRecord?repo=did:plc:cbkjy5n7bk3ax2wplmtjofq2&collection=app.bsky.actor.profile&rkey=self' \
    > profile.json

$ jq -r .cid profile.json
bafyreibibreg72cwvjy4gtpvcmkl4qyxyabmfigf2fji7iikj7cma3nryu

$ jq .value profile.json | atpcid --
bafyreibibreg72cwvjy4gtpvcmkl4qyxyabmfigf2fji7iikj7cma3nryu

The second downloads the avatar that record points to and hashes the file. A blob gets a raw CID, and the result is the $link in the profile.

A blob’s CIDListing 4
$ curl -s -o avatar.jpg 'https://vesuvius.pyroclastic.cloud/xrpc/com.atproto.sync.getBlob?did=did:plc:cbkjy5n7bk3ax2wplmtjofq2&cid=bafkreias6srdyhszkjjubug6jcy4onpa3byeotsrjnz5g7rlxeoxq36zra'

$ atpcid --raw --files avatar.jpg
bafkreias6srdyhszkjjubug6jcy4onpa3byeotsrjnz5g7rlxeoxq36zra
What it refusesListing 5
$ atpcid '{"a":1.5}'
Error: error-atproto-dasl-encode-9 Value not representable: the data model has no floating-point type: 1.5

$ atpcid '{"x":{"$link":"nope"}}'
Error: error-atproto-dasl-encode-9 Value not representable: $link value is not an AT Protocol CID: error-atproto-dasl-cid-5 Invalid CID: DASL CIDs must use base32lower encoding (prefix 'b'), got 'n'

$ atpcid --raw --record x
error: the argument '--raw' cannot be used with '--record'