feat: new types for identity files

This commit is contained in:
Shibo Lyu 2024-09-24 03:02:57 +08:00
parent c5f6659d8f
commit 50553085e7
6 changed files with 78 additions and 13 deletions

View file

@ -1,12 +1,26 @@
export type BlahPayloadSignee<P> = {
nonce: number;
payload: P;
timestamp: number;
id_key: string;
act_key?: string;
};
import z from "zod";
export type BlahSignedPayload<P> = {
sig: string;
signee: BlahPayloadSignee<P>;
};
export function blahPayloadSigneeSchemaOf<P extends z.ZodTypeAny>(schema: P) {
return z.object({
nonce: z.number().int(),
payload: schema,
timestamp: z.number().int(),
id_key: z.string(),
act_key: z.string().optional(),
});
}
export function blahSignedPayloadSchemaOf<P extends z.ZodTypeAny>(schema: P) {
return z.object({
sig: z.string(),
signee: blahPayloadSigneeSchemaOf(schema),
});
}
export type BlahPayloadSignee<P extends z.ZodTypeAny> = z.infer<
ReturnType<typeof blahPayloadSigneeSchemaOf<P>>
>;
export type BlahSignedPayload<P extends z.ZodTypeAny> = z.infer<
ReturnType<typeof blahSignedPayloadSchemaOf<P>>
>;

View file

@ -1,6 +1,7 @@
{
"imports": {
"@deno/dnt": "jsr:@deno/dnt@^0.41.3",
"@std/expect": "jsr:@std/expect@^1.0.3"
"@std/expect": "jsr:@std/expect@^1.0.3",
"zod": "https://deno.land/x/zod@v3.23.8/mod.ts"
}
}

16
deno.lock generated
View file

@ -140,7 +140,21 @@
}
}
},
"remote": {},
"remote": {
"https://deno.land/x/zod@v3.23.8/ZodError.ts": "528da200fbe995157b9ae91498b103c4ef482217a5c086249507ac850bd78f52",
"https://deno.land/x/zod@v3.23.8/errors.ts": "5285922d2be9700cc0c70c95e4858952b07ae193aa0224be3cbd5cd5567eabef",
"https://deno.land/x/zod@v3.23.8/external.ts": "a6cfbd61e9e097d5f42f8a7ed6f92f93f51ff927d29c9fbaec04f03cbce130fe",
"https://deno.land/x/zod@v3.23.8/helpers/enumUtil.ts": "54efc393cc9860e687d8b81ff52e980def00fa67377ad0bf8b3104f8a5bf698c",
"https://deno.land/x/zod@v3.23.8/helpers/errorUtil.ts": "7a77328240be7b847af6de9189963bd9f79cab32bbc61502a9db4fe6683e2ea7",
"https://deno.land/x/zod@v3.23.8/helpers/parseUtil.ts": "c14814d167cc286972b6e094df88d7d982572a08424b7cd50f862036b6fcaa77",
"https://deno.land/x/zod@v3.23.8/helpers/partialUtil.ts": "998c2fe79795257d4d1cf10361e74492f3b7d852f61057c7c08ac0a46488b7e7",
"https://deno.land/x/zod@v3.23.8/helpers/typeAliases.ts": "0fda31a063c6736fc3cf9090dd94865c811dfff4f3cb8707b932bf937c6f2c3e",
"https://deno.land/x/zod@v3.23.8/helpers/util.ts": "30c273131661ca5dc973f2cfb196fa23caf3a43e224cdde7a683b72e101a31fc",
"https://deno.land/x/zod@v3.23.8/index.ts": "d27aabd973613985574bc31f39e45cb5d856aa122ef094a9f38a463b8ef1a268",
"https://deno.land/x/zod@v3.23.8/locales/en.ts": "a7a25cd23563ccb5e0eed214d9b31846305ddbcdb9c5c8f508b108943366ab4c",
"https://deno.land/x/zod@v3.23.8/mod.ts": "ec6e2b1255c1a350b80188f97bd0a6bac45801bb46fc48f50b9763aa66046039",
"https://deno.land/x/zod@v3.23.8/types.ts": "1b172c90782b1eaa837100ebb6abd726d79d6c1ec336350c8e851e0fd706bf5c"
},
"workspace": {
"dependencies": [
"jsr:@deno/dnt@^0.41.3",

11
identity/actKey.ts Normal file
View file

@ -0,0 +1,11 @@
import z from "zod";
export const blahActKeyRecordSchema = z.object({
typ: z.literal("use_act_key"),
act_key: z.string(),
expire_time: z.number().int(),
comment: z.string(),
});
export type BlahActKeyRecord = z.input<typeof blahActKeyRecordSchema>;
export type BlahParsedActKeyRecord = z.infer<typeof blahActKeyRecordSchema>;

13
identity/identityFile.ts Normal file
View file

@ -0,0 +1,13 @@
import { z } from "zod";
import { blahSignedPayloadSchemaOf } from "../crypto/mod.ts";
import { blahActKeyRecordSchema } from "./actKey.ts";
import { blahProfileSchema } from "./profile.ts";
export const blahIdentityFileSchema = z.object({
id_key: z.string(),
act_keys: z.array(blahSignedPayloadSchemaOf(blahActKeyRecordSchema)),
profile: blahSignedPayloadSchemaOf(blahProfileSchema),
});
export type BlahIdentityFile = z.input<typeof blahIdentityFileSchema>;
export type BlahParsedIdentityFile = z.infer<typeof blahIdentityFileSchema>;

12
identity/profile.ts Normal file
View file

@ -0,0 +1,12 @@
import z from "zod";
export const blahProfileSchema = z.object({
typ: z.literal("profile"),
preferred_chat_server_urls: z.array(z.string().url()),
id_urls: z.array(z.string().url()),
name: z.string(),
bio: z.string().optional(),
});
export type BlahProfile = z.input<typeof blahProfileSchema>;
export type BlahParsedProfile = z.infer<typeof blahProfileSchema>;