ditto/src/sign.ts

67 lines
2.1 KiB
TypeScript
Raw Normal View History

2023-04-30 02:48:22 +00:00
import { type AppContext } from '@/app.ts';
import { getEventHash, getPublicKey, getSignature, HTTPException } from '@/deps.ts';
2023-05-21 00:39:05 +00:00
import ws from '@/stream.ts';
2023-04-30 02:48:22 +00:00
import type { Event, EventTemplate, SignedEvent } from '@/event.ts';
2023-05-14 00:46:47 +00:00
/** Get signing WebSocket from app context. */
function getSignStream(c: AppContext): WebSocket | undefined {
2023-05-21 00:39:05 +00:00
const pubkey = c.get('pubkey');
const session = c.get('session');
if (pubkey && session) {
const [socket] = ws.getSockets(`nostr:${pubkey}:${session}`);
return socket;
}
2023-05-14 00:46:47 +00:00
}
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.
* - If a signing WebSocket is provided, it will be used to sign the event.
*/
2023-04-30 02:48:22 +00:00
async function signEvent<K extends number = number>(event: EventTemplate<K>, c: AppContext): Promise<SignedEvent<K>> {
const seckey = c.get('seckey');
2023-05-14 00:46:47 +00:00
const stream = getSignStream(c);
2023-05-14 01:16:44 +00:00
if (!seckey && stream) {
2023-05-14 00:46:47 +00:00
try {
return await new Promise<SignedEvent<K>>((resolve, reject) => {
2023-05-21 00:39:05 +00:00
const handleMessage = (e: MessageEvent) => {
2023-05-14 02:25:43 +00:00
// TODO: parse and validate with zod
2023-05-14 00:46:47 +00:00
const data = JSON.parse(e.data);
if (data.event === 'nostr.sign') {
2023-05-21 00:39:05 +00:00
stream.removeEventListener('message', handleMessage);
2023-05-14 00:46:47 +00:00
resolve(JSON.parse(data.payload));
}
2023-05-21 00:39:05 +00:00
};
stream.addEventListener('message', handleMessage);
2023-05-14 00:46:47 +00:00
stream.send(JSON.stringify({ event: 'nostr.sign', payload: JSON.stringify(event) }));
2023-05-21 00:39:05 +00:00
setTimeout(() => {
stream.removeEventListener('message', handleMessage);
reject();
}, 60000);
2023-05-14 00:46:47 +00:00
});
} catch (_e) {
2023-05-14 02:25:43 +00:00
throw new HTTPException(408, {
res: c.json({ id: 'ditto.timeout', error: 'Signing timeout' }, 408),
2023-05-14 00:46:47 +00:00
});
}
}
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 };