diff --git a/src/sign.ts b/src/sign.ts index 2a91a06..cdce150 100644 --- a/src/sign.ts +++ b/src/sign.ts @@ -58,7 +58,7 @@ async function signNostrConnect(event: EventTemplate< } /** Wait for signed event to be sent through Nostr relay. */ -function awaitSignedEvent( +async function awaitSignedEvent( pubkey: string, messageId: string, c: AppContext, @@ -69,30 +69,29 @@ function awaitSignedEvent( Sub.close(messageId); } - return new Promise((resolve, reject) => { - const timeout = setTimeout(() => { - close(); - reject( - new HTTPException(408, { - res: c.json({ id: 'ditto.timeout', error: 'Signing timeout' }), - }), - ); - }, Time.minutes(1)); + const timeout = setTimeout(() => { + close(); + throw new HTTPException(408, { + res: c.json({ id: 'ditto.timeout', error: 'Signing timeout' }), + }); + }, Time.minutes(1)); - (async () => { - for await (const event of sub) { - if (event.kind === 24133) { - const decrypted = await decryptAdmin(event.pubkey, event.content); - const msg = jsonSchema.pipe(connectResponseSchema).parse(decrypted); + for await (const event of sub) { + if (event.kind === 24133) { + const decrypted = await decryptAdmin(event.pubkey, event.content); + const msg = jsonSchema.pipe(connectResponseSchema).parse(decrypted); - if (msg.id === messageId) { - close(); - clearTimeout(timeout); - resolve(msg.result as Event); - } - } + if (msg.id === messageId) { + close(); + clearTimeout(timeout); + return msg.result as Event; } - })(); + } + } + + // This should never happen. + throw new HTTPException(500, { + res: c.json({ id: 'ditto.sign', error: 'Unable to sign event' }, 500), }); }