Throw a user-friendly error when a private key isn't provided

This commit is contained in:
Alex Gleason 2023-05-07 15:43:38 -05:00
parent 8f65939f1c
commit 1271e36f7e
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 10 additions and 2 deletions

View File

@ -1,12 +1,20 @@
import { type AppContext } from '@/app.ts';
import { getEventHash, getPublicKey, getSignature } from '@/deps.ts';
import { getEventHash, getPublicKey, getSignature, HTTPException } from '@/deps.ts';
import type { Event, EventTemplate, SignedEvent } from '@/event.ts';
/** Sign Nostr event using the app context. */
// deno-lint-ignore require-await
async function signEvent<K extends number = number>(event: EventTemplate<K>, c: AppContext): Promise<SignedEvent<K>> {
const seckey = c.get('seckey')!;
const seckey = c.get('seckey');
// Ditto only supports publishing events with a private key (for now).
// TODO: Let the client sign events through a websocket.
if (!seckey) {
throw new HTTPException(400, {
res: c.json({ id: 'ditto.private_key', error: 'No private key' }, 400),
});
}
(event as Event<K>).pubkey = getPublicKey(seckey);
(event as Event<K>).id = getEventHash(event as Event<K>);