2023-04-30 02:48:22 +00:00
|
|
|
import { type AppContext } from '@/app.ts';
|
2023-05-07 20:43:38 +00:00
|
|
|
import { getEventHash, getPublicKey, getSignature, HTTPException } from '@/deps.ts';
|
2023-04-30 02:48:22 +00:00
|
|
|
|
|
|
|
import type { Event, EventTemplate, SignedEvent } from '@/event.ts';
|
|
|
|
|
|
|
|
/** Sign Nostr event using the app context. */
|
2023-04-30 02:57:30 +00:00
|
|
|
// deno-lint-ignore require-await
|
2023-04-30 02:48:22 +00:00
|
|
|
async function signEvent<K extends number = number>(event: EventTemplate<K>, c: AppContext): Promise<SignedEvent<K>> {
|
2023-05-07 20:43:38 +00:00
|
|
|
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),
|
|
|
|
});
|
|
|
|
}
|
2023-04-30 02:48:22 +00:00
|
|
|
|
|
|
|
(event as Event<K>).pubkey = getPublicKey(seckey);
|
|
|
|
(event as Event<K>).id = getEventHash(event as Event<K>);
|
|
|
|
(event as Event<K>).sig = getSignature(event as Event<K>, seckey);
|
|
|
|
|
|
|
|
return event as SignedEvent<K>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export { signEvent };
|