Throw a user-friendly error when a private key isn't provided
This commit is contained in:
parent
8f65939f1c
commit
1271e36f7e
12
src/sign.ts
12
src/sign.ts
|
@ -1,12 +1,20 @@
|
||||||
import { type AppContext } from '@/app.ts';
|
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';
|
import type { Event, EventTemplate, SignedEvent } from '@/event.ts';
|
||||||
|
|
||||||
/** Sign Nostr event using the app context. */
|
/** Sign Nostr event using the app context. */
|
||||||
// deno-lint-ignore require-await
|
// deno-lint-ignore require-await
|
||||||
async function signEvent<K extends number = number>(event: EventTemplate<K>, c: AppContext): Promise<SignedEvent<K>> {
|
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>).pubkey = getPublicKey(seckey);
|
||||||
(event as Event<K>).id = getEventHash(event as Event<K>);
|
(event as Event<K>).id = getEventHash(event as Event<K>);
|
||||||
|
|
Loading…
Reference in New Issue