1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//! DID Document - resolved identity information
//!
//! a did document contains:
//! - handle (from alsoKnownAs)
//! - signing key (from verificationMethod)
//! - pds endpoint (from service)
//!
//! see: https://atproto.com/specs/did
const std = @import("std");
const Did = @import("../syntax/did.zig").Did;
pub const DidDocument = struct {
allocator: std.mem.Allocator,
/// the did this document describes
id: []const u8,
/// handles (from alsoKnownAs, stripped of at:// prefix)
handles: [][]const u8,
/// verification methods (signing keys)
verification_methods: []VerificationMethod,
/// services (pds endpoints)
services: []Service,
pub const VerificationMethod = struct {
id: []const u8,
type: []const u8,
controller: []const u8,
public_key_multibase: []const u8,
};
pub const Service = struct {
id: []const u8,
type: []const u8,
service_endpoint: []const u8,
};
/// get the primary handle (first valid one)
pub fn handle(self: DidDocument) ?[]const u8 {
if (self.handles.len == 0) return null;
return self.handles[0];
}
/// get the atproto signing key
pub fn signingKey(self: DidDocument) ?VerificationMethod {
for (self.verification_methods) |vm| {
if (std.mem.endsWith(u8, vm.id, "#atproto")) {
return vm;
}
}
return null;
}
/// The endpoint of the service `id`, written `#fragment` or `did#fragment`.
/// A document may list its services either way, so both forms match an
/// entry for this document's DID.
pub fn serviceEndpoint(self: DidDocument, id: []const u8) ?[]const u8 {
const fragment = serviceFragment(self.id, id) orelse return null;
for (self.services) |svc| {
const svc_fragment = serviceFragment(self.id, svc.id) orelse continue;
if (std.mem.eql(u8, svc_fragment, fragment)) return svc.service_endpoint;
}
return null;
}
fn serviceFragment(did: []const u8, id: []const u8) ?[]const u8 {
const hash = std.mem.indexOfScalar(u8, id, '#') orelse return null;
if (hash != 0 and !std.mem.eql(u8, id[0..hash], did)) return null;
return id[hash..];
}
/// get the pds endpoint
pub fn pdsEndpoint(self: DidDocument) ?[]const u8 {
for (self.services) |svc| {
if (std.mem.endsWith(u8, svc.id, "#atproto_pds")) {
return svc.service_endpoint;
}
}
return null;
}
/// get the key a labeler signs its labels with (`#atproto_label`)
pub fn labelerKey(self: DidDocument) ?VerificationMethod {
for (self.verification_methods) |vm| {
if (std.mem.endsWith(u8, vm.id, "#atproto_label")) {
return vm;
}
}
return null;
}
/// get a labeler's service endpoint (`#atproto_labeler`)
pub fn labelerEndpoint(self: DidDocument) ?[]const u8 {
for (self.services) |svc| {
if (std.mem.endsWith(u8, svc.id, "#atproto_labeler")) {
return svc.service_endpoint;
}
}
return null;
}
/// parse a did document from json
pub fn parse(allocator: std.mem.Allocator, json_str: []const u8) !DidDocument {
const parsed = try std.json.parseFromSlice(std.json.Value, allocator, json_str, .{});
defer parsed.deinit();
return try parseValue(allocator, parsed.value);
}
/// parse from an already-parsed json value
pub fn parseValue(allocator: std.mem.Allocator, root: std.json.Value) !DidDocument {
if (root != .object) return error.InvalidDidDocument;
const obj = root.object;
// id is required
const id = if (obj.get("id")) |v| switch (v) {
.string => |s| try allocator.dupe(u8, s),
else => return error.InvalidDidDocument,
} else return error.InvalidDidDocument;
errdefer allocator.free(id);
// parse alsoKnownAs -> handles
var handles: std.ArrayList([]const u8) = .empty;
errdefer {
for (handles.items) |h| allocator.free(h);
handles.deinit(allocator);
}
if (obj.get("alsoKnownAs")) |aka| {
if (aka == .array) {
for (aka.array.items) |item| {
if (item == .string) {
const s = item.string;
// strip at:// prefix if present
const h = if (std.mem.startsWith(u8, s, "at://"))
s[5..]
else
s;
try handles.append(allocator, try allocator.dupe(u8, h));
}
}
}
}
// parse verificationMethod
var vms: std.ArrayList(VerificationMethod) = .empty;
errdefer {
for (vms.items) |vm| {
allocator.free(vm.id);
allocator.free(vm.type);
allocator.free(vm.controller);
allocator.free(vm.public_key_multibase);
}
vms.deinit(allocator);
}
if (obj.get("verificationMethod")) |vm_arr| {
if (vm_arr == .array) {
for (vm_arr.array.items) |item| {
if (item == .object) {
const vm_obj = item.object;
const vm = VerificationMethod{
.id = try allocator.dupe(u8, getStr(vm_obj, "id") orelse continue),
.type = try allocator.dupe(u8, getStr(vm_obj, "type") orelse ""),
.controller = try allocator.dupe(u8, getStr(vm_obj, "controller") orelse ""),
.public_key_multibase = try allocator.dupe(u8, getStr(vm_obj, "publicKeyMultibase") orelse ""),
};
try vms.append(allocator, vm);
}
}
}
}
// parse service
var svcs: std.ArrayList(Service) = .empty;
errdefer {
for (svcs.items) |svc| {
allocator.free(svc.id);
allocator.free(svc.type);
allocator.free(svc.service_endpoint);
}
svcs.deinit(allocator);
}
if (obj.get("service")) |svc_arr| {
if (svc_arr == .array) {
for (svc_arr.array.items) |item| {
if (item == .object) {
const svc_obj = item.object;
const svc = Service{
.id = try allocator.dupe(u8, getStr(svc_obj, "id") orelse continue),
.type = try allocator.dupe(u8, getStr(svc_obj, "type") orelse ""),
.service_endpoint = try allocator.dupe(u8, getStr(svc_obj, "serviceEndpoint") orelse ""),
};
try svcs.append(allocator, svc);
}
}
}
}
return .{
.allocator = allocator,
.id = id,
.handles = try handles.toOwnedSlice(allocator),
.verification_methods = try vms.toOwnedSlice(allocator),
.services = try svcs.toOwnedSlice(allocator),
};
}
pub fn deinit(self: *DidDocument) void {
for (self.handles) |h| self.allocator.free(h);
self.allocator.free(self.handles);
for (self.verification_methods) |vm| {
self.allocator.free(vm.id);
self.allocator.free(vm.type);
self.allocator.free(vm.controller);
self.allocator.free(vm.public_key_multibase);
}
self.allocator.free(self.verification_methods);
for (self.services) |svc| {
self.allocator.free(svc.id);
self.allocator.free(svc.type);
self.allocator.free(svc.service_endpoint);
}
self.allocator.free(self.services);
self.allocator.free(self.id);
}
fn getStr(obj: std.json.ObjectMap, key: []const u8) ?[]const u8 {
if (obj.get(key)) |v| {
if (v == .string) return v.string;
}
return null;
}
};
// === tests ===
test "parse did document" {
const json =
\\{
\\ "id": "did:plc:z72i7hdynmk6r22z27h6tvur",
\\ "alsoKnownAs": ["at://jay.bsky.social"],
\\ "verificationMethod": [
\\ {
\\ "id": "did:plc:z72i7hdynmk6r22z27h6tvur#atproto",
\\ "type": "Multikey",
\\ "controller": "did:plc:z72i7hdynmk6r22z27h6tvur",
\\ "publicKeyMultibase": "zQ3shXjHeiBuRCKmM36cuYnm7YEMzhGnCmCyW92sRJ9pribSF"
\\ }
\\ ],
\\ "service": [
\\ {
\\ "id": "#atproto_pds",
\\ "type": "AtprotoPersonalDataServer",
\\ "serviceEndpoint": "https://shimeji.us-east.host.bsky.network"
\\ }
\\ ]
\\}
;
var doc = try DidDocument.parse(std.testing.allocator, json);
defer doc.deinit();
try std.testing.expectEqualStrings("did:plc:z72i7hdynmk6r22z27h6tvur", doc.id);
try std.testing.expectEqualStrings("jay.bsky.social", doc.handle().?);
try std.testing.expectEqualStrings("https://shimeji.us-east.host.bsky.network", doc.pdsEndpoint().?);
const key = doc.signingKey().?;
try std.testing.expect(std.mem.endsWith(u8, key.id, "#atproto"));
}
test "a labeler's document names its label key and endpoint" {
// Bluesky's moderation service, as plc.directory served it on 2026-11-01
const json =
\\{
\\ "id": "did:plc:ar7c4by46qjdydhdevvrndac",
\\ "alsoKnownAs": ["at://moderation.bsky.app"],
\\ "verificationMethod": [
\\ {
\\ "id": "did:plc:ar7c4by46qjdydhdevvrndac#atproto_label",
\\ "type": "Multikey",
\\ "controller": "did:plc:ar7c4by46qjdydhdevvrndac",
\\ "publicKeyMultibase": "zQ3shmV1BNcX17coaDbfen6zArEad6SCLT3jVWCbC6Y9iinTa"
\\ }
\\ ],
\\ "service": [
\\ {
\\ "id": "#atproto_labeler",
\\ "type": "AtprotoLabeler",
\\ "serviceEndpoint": "https://mod.bsky.app"
\\ }
\\ ]
\\}
;
var doc = try DidDocument.parse(std.testing.allocator, json);
defer doc.deinit();
try std.testing.expectEqualStrings("https://mod.bsky.app", doc.labelerEndpoint().?);
try std.testing.expectEqualStrings("zQ3shmV1BNcX17coaDbfen6zArEad6SCLT3jVWCbC6Y9iinTa", doc.labelerKey().?.public_key_multibase);
try std.testing.expect(doc.signingKey() == null);
try std.testing.expect(doc.pdsEndpoint() == null);
}
test "parse did document with no handle" {
const json =
\\{
\\ "id": "did:plc:test123",
\\ "alsoKnownAs": [],
\\ "verificationMethod": [],
\\ "service": []
\\}
;
var doc = try DidDocument.parse(std.testing.allocator, json);
defer doc.deinit();
try std.testing.expect(doc.handle() == null);
try std.testing.expect(doc.pdsEndpoint() == null);
try std.testing.expect(doc.signingKey() == null);
}
test "a service is found by its relative or absolute id" {
var doc = try DidDocument.parse(std.testing.allocator,
\\{"id":"did:web:svc.test","service":[{"id":"#bsky_appview","type":"BskyAppView","serviceEndpoint":"https://a.test"},{"id":"did:web:svc.test#labeler","type":"AtprotoLabeler","serviceEndpoint":"https://l.test"},{"id":"did:web:other.test#spoof","type":"X","serviceEndpoint":"https://x.test"}]}
);
defer doc.deinit();
try std.testing.expectEqualStrings("https://a.test", doc.serviceEndpoint("did:web:svc.test#bsky_appview").?);
try std.testing.expectEqualStrings("https://a.test", doc.serviceEndpoint("#bsky_appview").?);
try std.testing.expectEqualStrings("https://l.test", doc.serviceEndpoint("#labeler").?);
try std.testing.expect(doc.serviceEndpoint("did:web:other.test#bsky_appview") == null);
try std.testing.expect(doc.serviceEndpoint("#spoof") == null);
try std.testing.expect(doc.serviceEndpoint("bsky_appview") == null);
}