From 50553085e77da2fdfe21e875ab08fe3ce28667d7 Mon Sep 17 00:00:00 2001 From: Shibo Lyu Date: Tue, 24 Sep 2024 03:02:57 +0800 Subject: [PATCH] feat: new types for identity files --- crypto/signedPayload.ts | 36 +++++++++++++++++++++++++----------- deno.json | 3 ++- deno.lock | 16 +++++++++++++++- identity/actKey.ts | 11 +++++++++++ identity/identityFile.ts | 13 +++++++++++++ identity/profile.ts | 12 ++++++++++++ 6 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 identity/actKey.ts create mode 100644 identity/identityFile.ts create mode 100644 identity/profile.ts diff --git a/crypto/signedPayload.ts b/crypto/signedPayload.ts index 52aee39..5eed005 100644 --- a/crypto/signedPayload.ts +++ b/crypto/signedPayload.ts @@ -1,12 +1,26 @@ -export type BlahPayloadSignee

= { - nonce: number; - payload: P; - timestamp: number; - id_key: string; - act_key?: string; -}; +import z from "zod"; -export type BlahSignedPayload

= { - sig: string; - signee: BlahPayloadSignee

; -}; +export function blahPayloadSigneeSchemaOf

(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

(schema: P) { + return z.object({ + sig: z.string(), + signee: blahPayloadSigneeSchemaOf(schema), + }); +} + +export type BlahPayloadSignee

= z.infer< + ReturnType> +>; + +export type BlahSignedPayload

= z.infer< + ReturnType> +>; diff --git a/deno.json b/deno.json index 733352f..cd0ecad 100644 --- a/deno.json +++ b/deno.json @@ -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" } } diff --git a/deno.lock b/deno.lock index a185b98..b4009a9 100644 --- a/deno.lock +++ b/deno.lock @@ -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", diff --git a/identity/actKey.ts b/identity/actKey.ts new file mode 100644 index 0000000..18a4753 --- /dev/null +++ b/identity/actKey.ts @@ -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; +export type BlahParsedActKeyRecord = z.infer; diff --git a/identity/identityFile.ts b/identity/identityFile.ts new file mode 100644 index 0000000..cec2f2c --- /dev/null +++ b/identity/identityFile.ts @@ -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; +export type BlahParsedIdentityFile = z.infer; diff --git a/identity/profile.ts b/identity/profile.ts new file mode 100644 index 0000000..b814378 --- /dev/null +++ b/identity/profile.ts @@ -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; +export type BlahParsedProfile = z.infer;