Fork of github.com/did-method-plc/did-method-plc
1import * as z from 'zod'
2import * as mf from 'multiformats/cid'
3
4const cid = z
5 .any()
6 .refine((obj: unknown) => mf.CID.asCID(obj) !== null, {
7 message: 'Not a CID',
8 })
9 .transform((obj: unknown) => mf.CID.asCID(obj) as mf.CID)
10
11const service = z.object({
12 type: z.string(),
13 endpoint: z.string(),
14})
15
16const documentData = z.object({
17 did: z.string(),
18 rotationKeys: z.array(z.string()),
19 verificationMethods: z.record(z.string()),
20 alsoKnownAs: z.array(z.string()),
21 services: z.record(service),
22})
23export type DocumentData = z.infer<typeof documentData>
24
25const unsignedCreateOpV1 = z.object({
26 type: z.literal('create'),
27 signingKey: z.string(),
28 recoveryKey: z.string(),
29 handle: z.string(),
30 service: z.string(),
31 prev: z.null(),
32})
33export type UnsignedCreateOpV1 = z.infer<typeof unsignedCreateOpV1>
34const createOpV1 = unsignedCreateOpV1.extend({ sig: z.string() })
35export type CreateOpV1 = z.infer<typeof createOpV1>
36
37const unsignedOperation = z
38 .object({
39 type: z.literal('plc_operation'),
40 rotationKeys: z.array(z.string()),
41 verificationMethods: z.record(z.string()),
42 alsoKnownAs: z.array(z.string()),
43 services: z.record(service),
44 prev: z.string().nullable(),
45 })
46 .strict()
47export type UnsignedOperation = z.infer<typeof unsignedOperation>
48const operation = unsignedOperation.extend({ sig: z.string() }).strict()
49export type Operation = z.infer<typeof operation>
50
51const unsignedTombstone = z
52 .object({
53 type: z.literal('plc_tombstone'),
54 prev: z.string(),
55 })
56 .strict()
57export type UnsignedTombstone = z.infer<typeof unsignedTombstone>
58const tombstone = unsignedTombstone.extend({ sig: z.string() }).strict()
59export type Tombstone = z.infer<typeof tombstone>
60
61const opOrTombstone = z.union([operation, tombstone])
62export type OpOrTombstone = z.infer<typeof opOrTombstone>
63const compatibleOp = z.union([createOpV1, operation])
64export type CompatibleOp = z.infer<typeof compatibleOp>
65const compatibleOpOrTombstone = z.union([createOpV1, operation, tombstone])
66export type CompatibleOpOrTombstone = z.infer<typeof compatibleOpOrTombstone>
67
68export const indexedOperation = z.object({
69 did: z.string(),
70 operation: compatibleOpOrTombstone,
71 cid: cid,
72 nullified: z.boolean(),
73 createdAt: z.date(),
74})
75export type IndexedOperation = z.infer<typeof indexedOperation>
76
77export const exportedOp = z.object({
78 did: z.string(),
79 operation: compatibleOpOrTombstone,
80 cid: z.string(),
81 nullified: z.boolean(),
82 createdAt: z.string(),
83})
84export type ExportedOp = z.infer<typeof exportedOp>
85
86export const didDocVerificationMethod = z.object({
87 id: z.string(),
88 type: z.string(),
89 controller: z.string(),
90 publicKeyMultibase: z.string(),
91})
92
93export const didDocService = z.object({
94 id: z.string(),
95 type: z.string(),
96 serviceEndpoint: z.string(),
97})
98
99export const didDocument = z.object({
100 '@context': z.array(z.string()),
101 id: z.string(),
102 alsoKnownAs: z.array(z.string()),
103 verificationMethod: z.array(didDocVerificationMethod),
104 service: z.array(didDocService),
105})
106export type DidDocument = z.infer<typeof didDocument>
107
108export const def = {
109 documentData,
110 createOpV1,
111 unsignedOperation,
112 operation,
113 tombstone,
114 opOrTombstone,
115 compatibleOp,
116 compatibleOpOrTombstone,
117 indexedOperation,
118 exportedOp,
119 didDocument,
120}