Reorganize some nostr schema code
This commit is contained in:
parent
1f470ffe2d
commit
893542cf58
|
@ -2,7 +2,8 @@ import { type AppMiddleware } from '@/app.ts';
|
||||||
import { Conf } from '@/config.ts';
|
import { Conf } from '@/config.ts';
|
||||||
import { HTTPException } from '@/deps.ts';
|
import { HTTPException } from '@/deps.ts';
|
||||||
import { type Event } from '@/event.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';
|
import { eventAge, findTag, sha256, Time } from '@/utils.ts';
|
||||||
|
|
||||||
const decodeEventSchema = decode64Schema.pipe(jsonSchema).pipe(signedEventSchema);
|
const decodeEventSchema = decode64Schema.pipe(jsonSchema).pipe(signedEventSchema);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { verifySignature, z } from '@/deps.ts';
|
import { z } from '@/deps.ts';
|
||||||
|
|
||||||
import type { Event } from './event.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()]);
|
const emojiTagSchema = z.tuple([z.literal('emoji'), z.string(), z.string().url()]);
|
||||||
|
|
||||||
/** https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem */
|
/** https://developer.mozilla.org/en-US/docs/Glossary/Base64#the_unicode_problem */
|
||||||
|
@ -102,12 +88,10 @@ export {
|
||||||
emojiTagSchema,
|
emojiTagSchema,
|
||||||
filteredArray,
|
filteredArray,
|
||||||
hashtagSchema,
|
hashtagSchema,
|
||||||
hexIdSchema,
|
|
||||||
jsonSchema,
|
jsonSchema,
|
||||||
type MetaContent,
|
type MetaContent,
|
||||||
metaContentSchema,
|
metaContentSchema,
|
||||||
parseMetaContent,
|
parseMetaContent,
|
||||||
parseRelay,
|
parseRelay,
|
||||||
relaySchema,
|
relaySchema,
|
||||||
signedEventSchema,
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -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({
|
const filterSchema = z.object({
|
||||||
kinds: z.number().int().positive().array().optional(),
|
kinds: z.number().int().positive().array().optional(),
|
||||||
ids: hexIdSchema.array().optional(),
|
ids: hexIdSchema.array().optional(),
|
||||||
|
@ -14,12 +30,11 @@ const filterSchema = z.object({
|
||||||
z.string().array(),
|
z.string().array(),
|
||||||
));
|
));
|
||||||
|
|
||||||
|
/** Client message to a Nostr relay. */
|
||||||
const clientMsgSchema = z.union([
|
const clientMsgSchema = z.union([
|
||||||
z.tuple([z.literal('REQ'), z.string().min(1)]).rest(filterSchema),
|
z.tuple([z.literal('REQ'), z.string().min(1)]).rest(filterSchema),
|
||||||
z.tuple([z.literal('EVENT'), signedEventSchema]),
|
z.tuple([z.literal('EVENT'), signedEventSchema]),
|
||||||
z.tuple([z.literal('CLOSE'), z.string().min(1)]),
|
z.tuple([z.literal('CLOSE'), z.string().min(1)]),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
type Filter = z.infer<typeof filterSchema>;
|
export { clientMsgSchema, filterSchema, hexIdSchema, signedEventSchema };
|
||||||
|
|
||||||
export { clientMsgSchema, filterSchema };
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import { type AppContext } from '@/app.ts';
|
import { type AppContext } from '@/app.ts';
|
||||||
import { getEventHash, getPublicKey, getSignature, HTTPException, z } from '@/deps.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 { ws } from '@/stream.ts';
|
||||||
|
|
||||||
import type { Event, EventTemplate, SignedEvent } from '@/event.ts';
|
import type { Event, EventTemplate, SignedEvent } from '@/event.ts';
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { Sqlite } from '@/deps.ts';
|
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 { Time } from '@/utils.ts';
|
||||||
import { generateDateRange } from '@/utils/time.ts';
|
import { generateDateRange } from '@/utils/time.ts';
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue