atproto 0.0.1-preview.1

AT Protocol data model, repositories, identity, XRPC, OAuth and sessions

Readme Files Dependencies Versions

atproto#

Libraries for the AT Protocol in Zena: its data model, encodings, cryptography, identity resolution, and repositories.

Usage#

import {cidForValue, decodeCbor, encodeCbor} from 'atproto/cbor';
import {StringValue, newMap, stringField} from 'atproto/data';

let record = newMap([
  ('$type', new StringValue('app.bsky.feed.post')),
  ('text', new StringValue('Hello from Zena!')),
]);

let bytes = encodeCbor(record);
let cid = cidForValue(record);
let decoded = decodeCbor(bytes);

Libraries#

  • atproto/varint: multiformats unsigned varints.
  • atproto/multibase: multibase strings, and base58btc. The other base encodings are in zena:encoding.
  • atproto/cid: CIDs, their binary and string forms, and the CIDs atproto gives records and blobs.
  • atproto/data: the data model (Value and its cases), its JSON form, and blob references (BlobRef).
  • atproto/cbor: DAG-CBOR encoding and strict decoding, cidForValue, and sortedKeys, the order DAG-CBOR writes map keys in.
  • atproto/syntax: DIDs, handles, NSIDs, record keys, TIDs, datetimes, and CID strings.
  • atproto/car: CAR files and BlockMap, checking every block against its CID.
  • atproto/mst: the Merkle search tree: key heights, nodes, building a tree, and looking a key up through the nodes of a proof.
  • atproto/repo: commits, signing them, and verifying com.atproto.sync.getRecord proofs.
  • atproto/tid: timestamp identifiers, and a clock that hands out increasing ones.
  • atproto/didkey: public keys as multikeys, did:keys, and DID document verification methods, and checking atproto signatures with them.
  • atproto/did: what atproto reads from a DID document: the signing key, the PDS, and the claimed handle.
  • atproto/service-auth: service-auth JWTs, which an account's PDS signs so the account can call another service: creating them, checking them against the issuer's key, the audience, the method, and the time, and refusing a token presented twice (ReplayGuard).
  • atproto/session: signing in to an account's PDS with a password, refreshing and ending the session, and asking the PDS for service-auth tokens, with a password session or an OAuth one (AccountSession).
  • atproto/oauth: signing in with atproto OAuth as a public client, such as a command-line tool: finding the PDS's authorization server, the authorization request with PKCE and DPoP, trading the code for tokens, and a session that signs each request, follows the servers' nonces, refreshes and revokes its tokens.
  • atproto/dpop: DPoP keys and proofs: ES256 proofs for each request, checking them, and JWK thumbprints.
  • atproto/xrpc: an XRPC client, whose requests a RequestSigner can authorize one by one.
  • atproto/methods: the NSIDs of standard com.atproto methods, and xrpcPath, a method's path on a server.
  • atproto/plc: did:plc operations: building and signing them, the DID a genesis operation makes, and sending an operation to a PLC directory.
  • atproto/identity: resolving DIDs (did:plc, did:web) and handles (DNS over HTTPS, then HTTPS), checking handles in both directions.
  • atproto/sync: fetching a record and checking its proof against the DID document's key, fetching a blob and checking it against its CID, and listing a collection's records, which comes with no proof.

atproto/xrpc, atproto/identity, atproto/sync, atproto/session, atproto/oauth, and atproto/plc send requests with zena:fetch; atproto/oauth also reads the clock and draws random bytes. The other libraries have no imports from the host.

2.0 KiB Raw
/**
 * Unsigned varints as multiformats defines them
 * (https://github.com/multiformats/unsigned-varint): seven bits per byte,
 * least significant group first, with the high bit set on every byte but
 * the last. CIDs and multicodec prefixes are made of them.
 *
 * The specification allows at most nine bytes, so values below 2^63, and
 * only the shortest encoding of a value. The decoder rejects anything
 * else.
 */

import {ByteBuffer} from 'zena:core';

/** A varint that is truncated, too long, or not the shortest encoding. */
export class VarintError extends Error {
  new(message: String) : super(message);
}

let MAX_BYTES = 9;

/** Appends the encoding of `value`, which must not be negative. */
export function writeVarint(out: ByteBuffer, value: i64): void {
  if (value < 0) {
    throw new VarintError(`a varint cannot encode ${value}`);
  }
  var rest = value;
  while (rest >= 0x80) {
    out.writeByte(((rest & 0x7f) | 0x80) as i32);
    rest = rest >>> 7;
  }
  out.writeByte(rest as i32);
}

/** The number of bytes `writeVarint` writes for `value`. */
export function varintLength(value: i64): i32 {
  var rest = value;
  var length = 1;
  while (rest >= 0x80) {
    rest = rest >>> 7;
    length += 1;
  }
  return length;
}

/**
 * Decodes the varint at `bytes[offset]`, reading no further than `end`.
 * Returns the value and the offset just past it.
 */
export function readVarint(bytes: FixedArray<u8>, offset: i32, end: i32): inline (i64, i32) {
  var value: i64 = 0;
  var shift: i64 = 0;
  var i = offset;
  while (i < end) {
    if (i - offset == MAX_BYTES) {
      throw new VarintError(`varint at offset ${offset} is longer than ${MAX_BYTES} bytes`);
    }
    let b = bytes[i] as i64;
    i += 1;
    value = value | ((b & 0x7f) << shift);
    if ((b & 0x80) == 0) {
      if (b == 0 && i - offset > 1) {
        throw new VarintError(`varint at offset ${offset} is not minimally encoded`);
      }
      return (value, i);
    }
    shift += 7;
  }
  throw new VarintError(`varint at offset ${offset} is truncated`);
}
none
VersionPublishedDependenciesApproved by
0.0.1-preview.1 2026-11-19 none