sign: refactor the unnecessarily complex awaitSignedEvent function
This commit is contained in:
parent
61f5acc937
commit
c8d6389132
17
src/sign.ts
17
src/sign.ts
|
@ -58,7 +58,7 @@ async function signNostrConnect<K extends number = number>(event: EventTemplate<
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Wait for signed event to be sent through Nostr relay. */
|
/** Wait for signed event to be sent through Nostr relay. */
|
||||||
function awaitSignedEvent<K extends number = number>(
|
async function awaitSignedEvent<K extends number = number>(
|
||||||
pubkey: string,
|
pubkey: string,
|
||||||
messageId: string,
|
messageId: string,
|
||||||
c: AppContext,
|
c: AppContext,
|
||||||
|
@ -69,17 +69,13 @@ function awaitSignedEvent<K extends number = number>(
|
||||||
Sub.close(messageId);
|
Sub.close(messageId);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const timeout = setTimeout(() => {
|
const timeout = setTimeout(() => {
|
||||||
close();
|
close();
|
||||||
reject(
|
throw new HTTPException(408, {
|
||||||
new HTTPException(408, {
|
|
||||||
res: c.json({ id: 'ditto.timeout', error: 'Signing timeout' }),
|
res: c.json({ id: 'ditto.timeout', error: 'Signing timeout' }),
|
||||||
}),
|
});
|
||||||
);
|
|
||||||
}, Time.minutes(1));
|
}, Time.minutes(1));
|
||||||
|
|
||||||
(async () => {
|
|
||||||
for await (const event of sub) {
|
for await (const event of sub) {
|
||||||
if (event.kind === 24133) {
|
if (event.kind === 24133) {
|
||||||
const decrypted = await decryptAdmin(event.pubkey, event.content);
|
const decrypted = await decryptAdmin(event.pubkey, event.content);
|
||||||
|
@ -88,11 +84,14 @@ function awaitSignedEvent<K extends number = number>(
|
||||||
if (msg.id === messageId) {
|
if (msg.id === messageId) {
|
||||||
close();
|
close();
|
||||||
clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
resolve(msg.result as Event<K>);
|
return msg.result as Event<K>;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})();
|
|
||||||
|
// This should never happen.
|
||||||
|
throw new HTTPException(500, {
|
||||||
|
res: c.json({ id: 'ditto.sign', error: 'Unable to sign event' }, 500),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue