sign: simplify awaitSignedEvent (remove unnecessary conditional, rearrange error, fix crash)
This commit is contained in:
parent
93f06fd342
commit
3c45a4a3aa
34
src/sign.ts
34
src/sign.ts
|
@ -70,34 +70,26 @@ async function awaitSignedEvent<K extends number = number>(
|
||||||
Sub.close(messageId);
|
Sub.close(messageId);
|
||||||
}
|
}
|
||||||
|
|
||||||
const timeout = setTimeout(() => {
|
const timeout = setTimeout(close, Time.minutes(1));
|
||||||
close();
|
|
||||||
throw new HTTPException(408, {
|
|
||||||
res: c.json({ id: 'ditto.timeout', error: 'Signing timeout' }),
|
|
||||||
});
|
|
||||||
}, Time.minutes(1));
|
|
||||||
|
|
||||||
for await (const event of sub) {
|
for await (const event of sub) {
|
||||||
if (event.kind === 24133) {
|
const decrypted = await decryptAdmin(event.pubkey, event.content);
|
||||||
const decrypted = await decryptAdmin(event.pubkey, event.content);
|
|
||||||
|
|
||||||
const result = jsonSchema
|
const result = jsonSchema
|
||||||
.pipe(connectResponseSchema)
|
.pipe(connectResponseSchema)
|
||||||
.refine((msg) => msg.id === messageId)
|
.refine((msg) => msg.id === messageId, 'Message ID mismatch')
|
||||||
.refine((msg) => eventMatchesTemplate(msg.result, template))
|
.refine((msg) => eventMatchesTemplate(msg.result, template), 'Event template mismatch')
|
||||||
.safeParse(decrypted);
|
.safeParse(decrypted);
|
||||||
|
|
||||||
if (result.success) {
|
if (result.success) {
|
||||||
close();
|
close();
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
return result.data.result as Event<K>;
|
return result.data.result as Event<K>;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This should never happen.
|
throw new HTTPException(408, {
|
||||||
throw new HTTPException(500, {
|
res: c.json({ id: 'ditto.timeout', error: 'Signing timeout' }),
|
||||||
res: c.json({ id: 'ditto.sign', error: 'Unable to sign event' }, 500),
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue