Reorganize some nostr schema code

This commit is contained in:
Alex Gleason 2023-08-12 11:28:16 -05:00
parent 1f470ffe2d
commit 893542cf58
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
5 changed files with 26 additions and 25 deletions

View File

@ -2,7 +2,8 @@ import { type AppMiddleware } from '@/app.ts';
import { Conf } from '@/config.ts';
import { HTTPException } from '@/deps.ts';
import { type Event } from '@/event.ts';
import { decode64Schema, jsonSchema, signedEventSchema } from '@/schema.ts';
import { decode64Schema, jsonSchema } from '@/schema.ts';
import { signedEventSchema } from '@/schemas/nostr.ts';
import { eventAge, findTag, sha256, Time } from '@/utils.ts';
const decodeEventSchema = decode64Schema.pipe(jsonSchema).pipe(signedEventSchema);

View File

@ -1,4 +1,4 @@
import { verifySignature, z } from '@/deps.ts';
import { z } from '@/deps.ts';
import type { Event } from './event.ts';
@ -67,20 +67,6 @@ const relaySchema = z.custom<URL>((relay) => {
}
});
const hexIdSchema = z.string().regex(/^[0-9a-f]{64}$/);
const eventSchema = z.object({
id: hexIdSchema,
kind: z.number(),
tags: z.array(z.array(z.string())),
content: z.string(),
created_at: z.number(),
pubkey: hexIdSchema,
sig: z.string(),
});
const signedEventSchema = eventSchema.refine(verifySignature);
const emojiTagSchema = z.tuple([z.literal('emoji'), z.string(), z.string().url()]);
/** https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem */
@ -102,12 +88,10 @@ export {
emojiTagSchema,
filteredArray,
hashtagSchema,
hexIdSchema,
jsonSchema,
type MetaContent,
metaContentSchema,
parseMetaContent,
parseRelay,
relaySchema,
signedEventSchema,
};

View File

@ -1,7 +1,23 @@
import { z } from '@/deps.ts';
import { verifySignature, z } from '@/deps.ts';
import { hexIdSchema, signedEventSchema } from '../schema.ts';
/** Schema to validate Nostr hex IDs such as event IDs and pubkeys. */
const hexIdSchema = z.string().regex(/^[0-9a-f]{64}$/);
/** Nostr event schema. */
const eventSchema = z.object({
id: hexIdSchema,
kind: z.number(),
tags: z.array(z.array(z.string())),
content: z.string(),
created_at: z.number(),
pubkey: hexIdSchema,
sig: z.string(),
});
/** Nostr event schema that also verifies the event's signature. */
const signedEventSchema = eventSchema.refine(verifySignature);
/** Nostr relay filter schema. */
const filterSchema = z.object({
kinds: z.number().int().positive().array().optional(),
ids: hexIdSchema.array().optional(),
@ -14,12 +30,11 @@ const filterSchema = z.object({
z.string().array(),
));
/** Client message to a Nostr relay. */
const clientMsgSchema = z.union([
z.tuple([z.literal('REQ'), z.string().min(1)]).rest(filterSchema),
z.tuple([z.literal('EVENT'), signedEventSchema]),
z.tuple([z.literal('CLOSE'), z.string().min(1)]),
]);
type Filter = z.infer<typeof filterSchema>;
export { clientMsgSchema, filterSchema };
export { clientMsgSchema, filterSchema, hexIdSchema, signedEventSchema };

View File

@ -1,6 +1,6 @@
import { type AppContext } from '@/app.ts';
import { getEventHash, getPublicKey, getSignature, HTTPException, z } from '@/deps.ts';
import { signedEventSchema } from '@/schema.ts';
import { signedEventSchema } from '@/schemas/nostr.ts';
import { ws } from '@/stream.ts';
import type { Event, EventTemplate, SignedEvent } from '@/event.ts';

View File

@ -1,5 +1,6 @@
import { Sqlite } from '@/deps.ts';
import { hashtagSchema, hexIdSchema } from '@/schema.ts';
import { hashtagSchema } from '@/schema.ts';
import { hexIdSchema } from '@/schemas/nostr.ts';
import { Time } from '@/utils.ts';
import { generateDateRange } from '@/utils/time.ts';