~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zat

atproto utils for zig · mirror of https://tangled.org/zat.dev/zat

0
0
68 lines · 3.0 KB Raw History
//! follow a labeler's stream and check every label's signature.
//!
//! a labeler publishes signed moderation labels on its own event stream,
//! `com.atproto.label.subscribeLabels`. its DID document names the endpoint
//! (`#atproto_labeler`) and the key it signs with (`#atproto_label`), so a
//! consumer can verify each label without trusting the connection.
//! https://atproto.com/specs/label
//!
//! run: zig build example-follow-a-labeler   (stops after 50 labels)

const std = @import("std");
const zat = @import("zat");

const LIMIT = 50;

const Tally = struct {
    allocator: std.mem.Allocator,
    /// the labeler's signing key as a did:key
    key: []const u8,
    seen: usize = 0,
    verified: usize = 0,
    on_accounts: usize = 0,
    withdrawn: usize = 0,

    pub fn onEvent(self: *Tally, event: zat.LabelEvent) void {
        switch (event) {
            .labels => |batch| for (batch.labels) |label| {
                self.seen += 1;
                if (label.verify(self.allocator, self.key)) |_| self.verified += 1 else |_| {}
                // a DID subject labels the whole account; an at:// URI labels one record
                if (std.mem.startsWith(u8, label.uri, "did:")) self.on_accounts += 1;
                if (label.neg) self.withdrawn += 1;
                std.debug.print("seq {d}  {s}{s}  {s}\n", .{ batch.seq, if (label.neg) "-" else "+", label.val, label.uri });
            },
            .info => |info| std.debug.print("info: {s}\n", .{info.name orelse "?"}),
            .err => |err| std.debug.print("error: {s}\n", .{err.name orelse "?"}),
        }
    }

    pub fn shouldStop(self: *Tally) bool {
        return self.seen >= LIMIT;
    }
};

pub fn main(init: std.process.Init) !void {
    const allocator = init.gpa;

    var resolver = zat.DidResolver.init(init.io, allocator);
    defer resolver.deinit();
    var doc = try resolver.resolve(zat.Did.parse("did:plc:ar7c4by46qjdydhdevvrndac").?); // moderation.bsky.app
    defer doc.deinit();
    // the document names an https service; the stream is its wss twin
    const service = doc.labelerEndpoint() orelse return error.NotALabeler;
    if (!std.mem.startsWith(u8, service, "https://")) return error.UnexpectedEndpoint;
    const endpoint = try std.fmt.allocPrint(allocator, "wss://{s}", .{service["https://".len..]});
    defer allocator.free(endpoint);
    const key = doc.labelerKey() orelse return error.NoLabelKey;
    const did_key = try std.fmt.allocPrint(allocator, "did:key:{s}", .{key.public_key_multibase});
    defer allocator.free(did_key);

    var tally: Tally = .{ .allocator = allocator, .key = did_key };
    // no cursor: start at the live head. pass `.cursor` to resume after a seq.
    var client = zat.LabelClient.init(init.io, allocator, .{ .hosts = &.{endpoint} });
    defer client.deinit();
    try client.subscribe(&tally);

    std.debug.print("\n{d} labels, {d} signatures verified, {d} on whole accounts, {d} withdrawn\n", .{ tally.seen, tally.verified, tally.on_accounts, tally.withdrawn });
}