2023-04-30 02:48:22 +00:00
|
|
|
import { type AppContext } from '@/app.ts';
|
2023-08-26 17:48:08 +00:00
|
|
|
import { Conf } from '@/config.ts';
|
2023-08-26 23:03:59 +00:00
|
|
|
import { decryptAdmin, encryptAdmin } from '@/crypto.ts';
|
|
|
|
import { type Event, type EventTemplate, finishEvent, HTTPException } from '@/deps.ts';
|
|
|
|
import { connectResponseSchema } from '@/schemas/nostr.ts';
|
2023-08-27 03:49:32 +00:00
|
|
|
import { jsonSchema } from '@/schema.ts';
|
2023-08-26 23:03:59 +00:00
|
|
|
import { Sub } from '@/subs.ts';
|
2023-09-03 01:09:28 +00:00
|
|
|
import { eventMatchesTemplate, Time } from '@/utils.ts';
|
2023-08-26 23:03:59 +00:00
|
|
|
import { createAdminEvent } from '@/utils/web.ts';
|
2023-05-21 02:16:14 +00:00
|
|
|
|
2023-11-20 18:34:19 +00:00
|
|
|
interface SignEventOpts {
|
|
|
|
/** Target proof-of-work difficulty for the signed event. */
|
|
|
|
pow?: number;
|
|
|
|
}
|
|
|
|
|
2023-05-14 01:16:44 +00:00
|
|
|
/**
|
|
|
|
* Sign Nostr event using the app context.
|
|
|
|
*
|
|
|
|
* - If a secret key is provided, it will be used to sign the event.
|
2023-09-03 01:09:28 +00:00
|
|
|
* - If `X-Nostr-Sign` is passed, it will use NIP-46 to sign the event.
|
2023-05-14 01:16:44 +00:00
|
|
|
*/
|
2023-11-20 18:34:19 +00:00
|
|
|
async function signEvent<K extends number = number>(
|
|
|
|
event: EventTemplate<K>,
|
|
|
|
c: AppContext,
|
|
|
|
opts: SignEventOpts = {},
|
|
|
|
): Promise<Event<K>> {
|
2023-05-07 20:43:38 +00:00
|
|
|
const seckey = c.get('seckey');
|
2023-11-20 18:39:20 +00:00
|
|
|
const header = c.req.header('x-nostr-sign');
|
2023-08-26 23:03:59 +00:00
|
|
|
|
|
|
|
if (seckey) {
|
|
|
|
return finishEvent(event, seckey);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header) {
|
2023-11-20 18:34:19 +00:00
|
|
|
return await signNostrConnect(event, c, opts);
|
2023-08-26 23:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new HTTPException(400, {
|
|
|
|
res: c.json({ id: 'ditto.sign', error: 'Unable to sign event' }, 400),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Sign event with NIP-46, waiting in the background for the signed event. */
|
2023-11-20 18:34:19 +00:00
|
|
|
async function signNostrConnect<K extends number = number>(
|
|
|
|
event: EventTemplate<K>,
|
|
|
|
c: AppContext,
|
|
|
|
opts: SignEventOpts = {},
|
|
|
|
): Promise<Event<K>> {
|
2023-08-26 23:03:59 +00:00
|
|
|
const pubkey = c.get('pubkey');
|
|
|
|
|
|
|
|
if (!pubkey) {
|
2023-09-11 00:43:07 +00:00
|
|
|
throw new HTTPException(401, { message: 'Missing pubkey' });
|
2023-05-14 00:46:47 +00:00
|
|
|
}
|
2023-05-07 20:43:38 +00:00
|
|
|
|
2023-08-26 23:03:59 +00:00
|
|
|
const messageId = crypto.randomUUID();
|
|
|
|
|
|
|
|
createAdminEvent({
|
|
|
|
kind: 24133,
|
|
|
|
content: await encryptAdmin(
|
|
|
|
pubkey,
|
|
|
|
JSON.stringify({
|
|
|
|
id: messageId,
|
|
|
|
method: 'sign_event',
|
2023-11-20 18:34:19 +00:00
|
|
|
params: [event, {
|
|
|
|
pow: opts.pow,
|
|
|
|
}],
|
2023-08-26 23:03:59 +00:00
|
|
|
}),
|
|
|
|
),
|
|
|
|
tags: [['p', pubkey]],
|
|
|
|
}, c);
|
|
|
|
|
2023-09-03 01:09:28 +00:00
|
|
|
return awaitSignedEvent<K>(pubkey, messageId, event, c);
|
2023-08-26 23:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Wait for signed event to be sent through Nostr relay. */
|
2023-09-02 23:56:20 +00:00
|
|
|
async function awaitSignedEvent<K extends number = number>(
|
2023-08-26 23:03:59 +00:00
|
|
|
pubkey: string,
|
|
|
|
messageId: string,
|
2023-09-03 01:09:28 +00:00
|
|
|
template: EventTemplate<K>,
|
2023-08-26 23:03:59 +00:00
|
|
|
c: AppContext,
|
|
|
|
): Promise<Event<K>> {
|
|
|
|
const sub = Sub.sub(messageId, '1', [{ kinds: [24133], authors: [pubkey], '#p': [Conf.pubkey] }]);
|
|
|
|
|
|
|
|
function close(): void {
|
|
|
|
Sub.close(messageId);
|
2023-05-07 20:43:38 +00:00
|
|
|
}
|
2023-04-30 02:48:22 +00:00
|
|
|
|
2023-09-03 03:38:55 +00:00
|
|
|
const timeout = setTimeout(close, Time.minutes(1));
|
2023-09-02 23:56:20 +00:00
|
|
|
|
|
|
|
for await (const event of sub) {
|
2023-09-03 03:38:55 +00:00
|
|
|
const decrypted = await decryptAdmin(event.pubkey, event.content);
|
|
|
|
|
|
|
|
const result = jsonSchema
|
|
|
|
.pipe(connectResponseSchema)
|
|
|
|
.refine((msg) => msg.id === messageId, 'Message ID mismatch')
|
|
|
|
.refine((msg) => eventMatchesTemplate(msg.result, template), 'Event template mismatch')
|
|
|
|
.safeParse(decrypted);
|
|
|
|
|
|
|
|
if (result.success) {
|
|
|
|
close();
|
|
|
|
clearTimeout(timeout);
|
|
|
|
return result.data.result as Event<K>;
|
2023-09-02 23:56:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-03 03:38:55 +00:00
|
|
|
throw new HTTPException(408, {
|
|
|
|
res: c.json({ id: 'ditto.timeout', error: 'Signing timeout' }),
|
2023-08-26 23:03:59 +00:00
|
|
|
});
|
2023-08-26 17:48:08 +00:00
|
|
|
}
|
2023-04-30 02:48:22 +00:00
|
|
|
|
2023-08-26 17:48:08 +00:00
|
|
|
/** Sign event as the Ditto server. */
|
|
|
|
// deno-lint-ignore require-await
|
|
|
|
async function signAdminEvent<K extends number = number>(event: EventTemplate<K>): Promise<Event<K>> {
|
|
|
|
return finishEvent(event, Conf.seckey);
|
2023-04-30 02:48:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-08 23:29:26 +00:00
|
|
|
export { signAdminEvent, signEvent };
|