frontend client for gemstone. decentralised workplace app
1import { z } from "zod";
2
3export const didPlcSchema = z.templateLiteral(["did:plc:", z.string()]);
4export type DidPlc = z.infer<typeof didPlcSchema>;
5
6export const didWebSchema = z.templateLiteral(["did:web:", z.string()]);
7export type DidWeb = z.infer<typeof didWebSchema>;
8
9export const didSchema = z.templateLiteral([
10 "did:",
11 z.string(),
12 ":",
13 z.string(),
14]);
15export type Did = z.infer<typeof didSchema>;
16
17export const nsidSchema = z.custom<`${string}.${string}.${string}`>(
18 (val): val is `${string}.${string}.${string}` => {
19 return (
20 typeof val === "string" &&
21 /^[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)+(\.[a-zA-Z]([a-zA-Z0-9]{0,62})?)$/.test(
22 val,
23 )
24 );
25 },
26 { message: "Invalid atproto nsid format." },
27);
28export type Nsid = z.infer<typeof nsidSchema>;
29
30export const atprotoHandleSchema = z.custom<`${string}.${string}`>(
31 (val): val is `${string}.${string}` => {
32 return (
33 typeof val === "string" &&
34 /^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/.test(
35 val,
36 )
37 );
38 },
39 { message: "Invalid atproto handle format." },
40);
41export type AtprotoHandle = z.infer<typeof atprotoHandleSchema>;
42
43export const atUriAuthoritySchema = z.union([
44 didPlcSchema,
45 didWebSchema,
46 atprotoHandleSchema,
47]);
48export type AtUriAuthority = z.infer<typeof atUriAuthoritySchema>;
49
50export const atUriSchema = z.object({
51 authority: atUriAuthoritySchema,
52 collection: z.optional(nsidSchema),
53 rKey: z.optional(z.string()),
54});
55export type AtUri = z.infer<typeof atUriSchema>;
56
57export const verificationMethodSchema = z.object({
58 id: z.string(),
59 type: z.string(),
60 controller: z.string(),
61 publicKeyMultibase: z.optional(z.string()),
62});
63export type VerificationMethod = z.infer<typeof verificationMethodSchema>;
64
65export const didDocumentSchema = z.object({
66 "@context": z.array(z.string()),
67 id: didSchema,
68 alsoKnownAs: z.optional(z.array(z.string())),
69 verificationMethod: z.optional(z.array(verificationMethodSchema)),
70 service: z.optional(
71 z.array(
72 z.object({
73 id: z.string(),
74 type: z.union([z.string(), z.array(z.string())]),
75 serviceEndpoint: z.union([
76 z.string(),
77 z.record(z.string(), z.string()),
78 z.array(
79 z.union([z.string(), z.record(z.string(), z.string())]),
80 ),
81 ]),
82 }),
83 ),
84 ),
85});
86export type DidDocument = z.infer<typeof didDocumentSchema>;
87
88export const comAtprotoRepoStrongRefSchema = z.object({
89 $type: z.optional(z.literal("com.atproto.repo.strongRef")),
90 cid: z.string(),
91 uri: z.string(),
92});
93export type ComAtprotoRepoStrongRef = z.infer<
94 typeof comAtprotoRepoStrongRefSchema
95>;