useSignerStream: simplify with nostrify

environments/review-nostrify-xvdhxw/deployments/4584
Alex Gleason 2024-04-30 14:30:34 -05:00
rodzic df407381b2
commit 49a25f2eb0
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
2 zmienionych plików z 7 dodań i 44 usunięć

Wyświetl plik

@ -1,19 +1,8 @@
import { type NostrEvent } from '@nostrify/nostrify';
import { NostrEvent, NostrConnectResponse, NSchema as n } from '@nostrify/nostrify';
import { useEffect } from 'react';
import { useNostr } from 'soapbox/contexts/nostr-context';
import { connectRequestSchema, nwcRequestSchema } from 'soapbox/schemas/nostr';
import { jsonSchema } from 'soapbox/schemas/utils';
/** NIP-46 [response](https://github.com/nostr-protocol/nips/blob/master/46.md#response-events-kind24133) content. */
interface NostrConnectResponse {
/** Request ID that this response is for. */
id: string;
/** Result of the call (this can be either a string or a JSON stringified object) */
result: string;
/** Error in string form, if any. Its presence indicates an error with the request. */
error?: string;
}
import { nwcRequestSchema } from 'soapbox/schemas/nostr';
function useSignerStream() {
const { relay, pubkey, signer } = useNostr();
@ -35,7 +24,7 @@ function useSignerStream() {
if (!relay || !pubkey || !signer) return;
const decrypted = await signer.nip04!.decrypt(pubkey, event.content);
const reqMsg = jsonSchema.pipe(connectRequestSchema).safeParse(decrypted);
const reqMsg = n.json().pipe(n.connectRequest()).safeParse(decrypted);
if (!reqMsg.success) {
console.warn(decrypted);
console.warn(reqMsg.error);
@ -104,7 +93,7 @@ function useSignerStream() {
const decrypted = await signer.nip04!.decrypt(pubkey, event.content);
const reqMsg = jsonSchema.pipe(nwcRequestSchema).safeParse(decrypted);
const reqMsg = n.json().pipe(nwcRequestSchema).safeParse(decrypted);
if (!reqMsg.success) {
console.warn(decrypted);
console.warn(reqMsg.error);

Wyświetl plik

@ -1,35 +1,9 @@
import { NSchema as n } from '@nostrify/nostrify';
import { verifyEvent } from 'nostr-tools';
import { z } from 'zod';
/** Schema to validate Nostr hex IDs such as event IDs and pubkeys. */
const nostrIdSchema = z.string().regex(/^[0-9a-f]{64}$/);
/** Nostr kinds are positive integers. */
const kindSchema = z.number().int().nonnegative();
/** Nostr event template schema. */
const eventTemplateSchema = z.object({
kind: kindSchema,
tags: z.array(z.array(z.string())),
content: z.string(),
created_at: z.number(),
});
/** Nostr event schema. */
const eventSchema = eventTemplateSchema.extend({
id: nostrIdSchema,
pubkey: nostrIdSchema,
sig: z.string(),
});
/** Nostr event schema that also verifies the event's signature. */
const signedEventSchema = eventSchema.refine(verifyEvent);
/** NIP-46 request content schema. */
const connectRequestSchema = z.object({
id: z.string(),
method: z.string(),
params: z.string().array(),
});
const signedEventSchema = n.event().refine(verifyEvent);
/** NIP-47 signer response. */
const nwcRequestSchema = z.object({
@ -39,4 +13,4 @@ const nwcRequestSchema = z.object({
}),
});
export { nostrIdSchema, kindSchema, eventSchema, signedEventSchema, connectRequestSchema, nwcRequestSchema };
export { signedEventSchema, nwcRequestSchema };