From dbff3fee9a31d80e833fe0d3bf6a681c82fd7a3d Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 12 Feb 2024 11:40:17 -0600 Subject: [PATCH] Upgrade nostr-tools to v2.1.5 --- src/app.ts | 2 +- src/config.ts | 4 ++-- src/deps.ts | 9 +++++---- src/schemas/nostr.ts | 7 +++---- src/sign.ts | 6 +++--- src/utils/nip98.ts | 2 +- src/workers/verify.worker.ts | 4 ++-- 7 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/app.ts b/src/app.ts index 159bff84..92eaed4f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -88,7 +88,7 @@ interface AppEnv extends HonoEnv { /** Hex pubkey for the current user. If provided, the user is considered "logged in." */ pubkey?: string; /** Hex secret key for the current user. Optional, but easiest way to use legacy Mastodon apps. */ - seckey?: string; + seckey?: Uint8Array; /** NIP-98 signed event proving the pubkey is owned by the user. */ proof?: NostrEvent; /** User associated with the pubkey, if any. */ diff --git a/src/config.ts b/src/config.ts index 2f98c1e5..cdd07ba2 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,4 +1,4 @@ -import { dotenv, getPublicKey, nip19, secp, z } from '@/deps.ts'; +import { dotenv, getPublicKey, nip19, z } from '@/deps.ts'; /** Load environment config from `.env` */ await dotenv.load({ @@ -32,7 +32,7 @@ const Conf = { get cryptoKey() { return crypto.subtle.importKey( 'raw', - secp.etc.hexToBytes(Conf.seckey), + Conf.seckey, { name: 'HMAC', hash: 'SHA-256' }, false, ['sign', 'verify'], diff --git a/src/deps.ts b/src/deps.ts index e2090181..2019b351 100644 --- a/src/deps.ts +++ b/src/deps.ts @@ -12,10 +12,9 @@ export { z } from 'https://deno.land/x/zod@v3.21.4/mod.ts'; export { RelayPoolWorker } from 'npm:nostr-relaypool2@0.6.34'; export { type EventTemplate, - finishEvent, + finalizeEvent, getEventHash, getPublicKey, - getSignature, matchFilter, matchFilters, nip04, @@ -25,8 +24,8 @@ export { nip21, type UnsignedEvent, type VerifiedEvent, - verifySignature, -} from 'npm:nostr-tools@^1.17.0'; + verifyEvent, +} from 'npm:nostr-tools@^2.1.5'; export { parseFormData } from 'npm:formdata-helper@^0.3.0'; // @deno-types="npm:@types/lodash@4.14.194" export { default as lodash } from 'https://esm.sh/lodash@4.17.21'; @@ -89,6 +88,8 @@ export { NIP05, type NostrEvent, type NostrFilter, + type NostrSigner, + NSecSigner, NSet, type NStore, type NStoreOpts, diff --git a/src/schemas/nostr.ts b/src/schemas/nostr.ts index c6decbaf..708ba96d 100644 --- a/src/schemas/nostr.ts +++ b/src/schemas/nostr.ts @@ -1,6 +1,5 @@ -import { getEventHash, verifySignature, z } from '@/deps.ts'; - -import { jsonSchema, safeUrlSchema } from '../schema.ts'; +import { getEventHash, verifyEvent, z } from '@/deps.ts'; +import { jsonSchema, safeUrlSchema } from '@/schema.ts'; /** Schema to validate Nostr hex IDs such as event IDs and pubkeys. */ const nostrIdSchema = z.string().regex(/^[0-9a-f]{64}$/); @@ -21,7 +20,7 @@ const eventSchema = z.object({ /** Nostr event schema that also verifies the event's signature. */ const signedEventSchema = eventSchema .refine((event) => event.id === getEventHash(event), 'Event ID does not match hash') - .refine(verifySignature, 'Event signature is invalid'); + .refine(verifyEvent, 'Event signature is invalid'); /** Nostr relay filter schema. */ const filterSchema = z.object({ diff --git a/src/sign.ts b/src/sign.ts index efe88c1b..247dd824 100644 --- a/src/sign.ts +++ b/src/sign.ts @@ -1,7 +1,7 @@ import { type AppContext } from '@/app.ts'; import { Conf } from '@/config.ts'; import { decryptAdmin, encryptAdmin } from '@/crypto.ts'; -import { Debug, type EventTemplate, finishEvent, HTTPException, type NostrEvent } from '@/deps.ts'; +import { Debug, type EventTemplate, finalizeEvent, HTTPException, type NostrEvent } from '@/deps.ts'; import { connectResponseSchema } from '@/schemas/nostr.ts'; import { jsonSchema } from '@/schema.ts'; import { Sub } from '@/subs.ts'; @@ -31,7 +31,7 @@ async function signEvent( if (seckey) { debug(`Signing Event<${event.kind}> with secret key`); - return finishEvent(event, seckey); + return finalizeEvent(event, seckey); } if (header) { @@ -115,7 +115,7 @@ async function awaitSignedEvent( /** Sign event as the Ditto server. */ // deno-lint-ignore require-await async function signAdminEvent(event: EventTemplate): Promise { - return finishEvent(event, Conf.seckey); + return finalizeEvent(event, Conf.seckey); } export { signAdminEvent, signEvent }; diff --git a/src/utils/nip98.ts b/src/utils/nip98.ts index 0b832836..f41fd680 100644 --- a/src/utils/nip98.ts +++ b/src/utils/nip98.ts @@ -50,7 +50,7 @@ function validateAuthEvent(req: Request, event: NostrEvent, opts: ParseAuthReque } /** Create an auth EventTemplate from a Request. */ -async function buildAuthEventTemplate(req: Request, opts: ParseAuthRequestOpts = {}): Promise> { +async function buildAuthEventTemplate(req: Request, opts: ParseAuthRequestOpts = {}): Promise { const { validatePayload = true } = opts; const { method, url } = req; diff --git a/src/workers/verify.worker.ts b/src/workers/verify.worker.ts index 32ef1263..01377fe6 100644 --- a/src/workers/verify.worker.ts +++ b/src/workers/verify.worker.ts @@ -1,8 +1,8 @@ -import { Comlink, type NostrEvent, type VerifiedEvent, verifySignature } from '@/deps.ts'; +import { Comlink, type NostrEvent, type VerifiedEvent, verifyEvent } from '@/deps.ts'; export const VerifyWorker = { verifySignature(event: NostrEvent): event is VerifiedEvent { - return verifySignature(event); + return verifyEvent(event); }, };