atproto 0.0.1-preview.1
AT Protocol data model, repositories, identity, XRPC, OAuth and sessions
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 inzena:encoding.atproto/cid: CIDs, their binary and string forms, and the CIDs atproto gives records and blobs.atproto/data: the data model (Valueand its cases), its JSON form, and blob references (BlobRef).atproto/cbor: DAG-CBOR encoding and strict decoding,cidForValue, andsortedKeys, the order DAG-CBOR writes map keys in.atproto/syntax: DIDs, handles, NSIDs, record keys, TIDs, datetimes, and CID strings.atproto/car: CAR files andBlockMap, 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 verifyingcom.atproto.sync.getRecordproofs.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 aRequestSignercan authorize one by one.atproto/methods: the NSIDs of standardcom.atprotomethods, andxrpcPath, a method's path on a server.atproto/plc:did:plcoperations: 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
/**
* Timestamp identifiers (https://atproto.com/specs/tid): the record keys
* of most records, and every commit's `rev`.
*
* A TID is a 64-bit number written as 13 characters of sortable base32
* (`234567abcdefghijklmnopqrstuvwxyz`), most significant bits first, so
* TIDs sort as strings in the order of their numbers. The top bit is zero,
* the next 53 are microseconds since the Unix epoch, and the last 10 are a
* clock identifier, which keeps apart TIDs two clocks make in the same
* microsecond.
*
* `TidClock` hands out TIDs that always increase, as a commit's `rev` must,
* even when the clock it is given stands still or steps back. This library
* reads no clock itself.
*/
import {StringBuilder} from 'zena:core';
let ALPHABET = '234567abcdefghijklmnopqrstuvwxyz';
/** The TID for `micros` since the Unix epoch and the clock `clockId`, 0 to 1023. */
export function formatTid(micros: i64, clockId: i32): String {
let value = (micros << (10 as i64)) | ((clockId & 1023) as i64);
let sb = new StringBuilder(13);
for (var i = 0; i < 13; i += 1) {
let shift = (60 - 5 * i) as i64;
let digit = ((value >> shift) & (31 as i64)) as i32;
sb.appendByte(ALPHABET.getByteAt(digit));
}
return sb.toString();
}
/** The microseconds since the Unix epoch that `tid` records. */
export function tidMicros(tid: String): i64 {
var value = 0 as i64;
for (var i = 0; i < tid.length; i += 1) {
let b = tid.getByteAt(i);
let digit = if (b >= 97) b - 97 + 6 else b - 50;
value = (value << (5 as i64)) | (digit as i64);
}
return value >> (10 as i64);
}
/** Hands out increasing TIDs for one clock identifier. */
export final class TidClock {
#clockId: i32;
var #last = 0 as i64;
new(clockId: i32) : #clockId = clockId;
/**
* A TID for `nowMicros`, or for the microsecond after the last one this
* clock gave out, whichever is later.
*/
next(nowMicros: i64): String {
let micros = if (nowMicros > this.#last) nowMicros else this.#last + 1;
this.#last = micros;
return formatTid(micros, this.#clockId);
}
}
| Version | Published | Dependencies | Approved by |
|---|---|---|---|
| 0.0.1-preview.1 | 2026-11-19 | none |
|