From 1accae2222a5a7c7571e4e72c8d0fce93f576b97 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 14 May 2024 12:04:31 -0500 Subject: [PATCH] Add a ConnectSigner to wrap our default opts to NConnectSigner, add c.set('signer') calls to nip98 middleware --- src/app.ts | 2 +- src/middleware/auth98.ts | 10 ++++++---- src/middleware/signerMiddleware.ts | 15 +++------------ src/signers/ConnectSigner.ts | 20 ++++++++++++++++++++ 4 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 src/signers/ConnectSigner.ts diff --git a/src/app.ts b/src/app.ts index 5ef5056..62da64c 100644 --- a/src/app.ts +++ b/src/app.ts @@ -126,9 +126,9 @@ app.use( '*', csp(), cors({ origin: '*', exposeHeaders: ['link'] }), + signerMiddleware, auth98(), storeMiddleware, - signerMiddleware, ); app.get('/.well-known/webfinger', webfingerController); diff --git a/src/middleware/auth98.ts b/src/middleware/auth98.ts index d761b95..fbadb2c 100644 --- a/src/middleware/auth98.ts +++ b/src/middleware/auth98.ts @@ -1,14 +1,16 @@ import { NostrEvent } from '@nostrify/nostrify'; import { HTTPException } from 'hono'; + import { type AppContext, type AppMiddleware } from '@/app.ts'; +import { findUser, User } from '@/db/users.ts'; +import { ConnectSigner } from '@/signers/ConnectSigner.ts'; +import { localRequest } from '@/utils/api.ts'; import { buildAuthEventTemplate, parseAuthRequest, type ParseAuthRequestOpts, validateAuthEvent, } from '@/utils/nip98.ts'; -import { localRequest } from '@/utils/api.ts'; -import { findUser, User } from '@/db/users.ts'; /** * NIP-98 auth. @@ -20,7 +22,7 @@ function auth98(opts: ParseAuthRequestOpts = {}): AppMiddleware { const result = await parseAuthRequest(req, opts); if (result.success) { - c.set('pubkey', result.data.pubkey); + c.set('signer', new ConnectSigner(result.data.pubkey)); c.set('proof', result.data); } @@ -78,7 +80,7 @@ function withProof( } if (proof) { - c.set('pubkey', proof.pubkey); + c.set('signer', new ConnectSigner(proof.pubkey)); c.set('proof', proof); await handler(c, proof, next); } else { diff --git a/src/middleware/signerMiddleware.ts b/src/middleware/signerMiddleware.ts index d056391..85a2ff1 100644 --- a/src/middleware/signerMiddleware.ts +++ b/src/middleware/signerMiddleware.ts @@ -1,9 +1,8 @@ -import { NConnectSigner, NSecSigner } from '@nostrify/nostrify'; +import { NSecSigner } from '@nostrify/nostrify'; import { nip19 } from 'nostr-tools'; import { AppMiddleware } from '@/app.ts'; -import { AdminSigner } from '@/signers/AdminSigner.ts'; -import { Storages } from '@/storages.ts'; +import { ConnectSigner } from '@/signers/ConnectSigner.ts'; /** We only accept "Bearer" type. */ const BEARER_REGEX = new RegExp(`^Bearer (${nip19.BECH32_REGEX.source})$`); @@ -21,15 +20,7 @@ export const signerMiddleware: AppMiddleware = async (c, next) => { switch (decoded.type) { case 'npub': - c.set( - 'signer', - new NConnectSigner({ - pubkey: decoded.data, - relay: Storages.pubsub, - signer: new AdminSigner(), - timeout: 60000, - }), - ); + c.set('signer', new ConnectSigner(decoded.data)); break; case 'nsec': c.set('signer', new NSecSigner(decoded.data)); diff --git a/src/signers/ConnectSigner.ts b/src/signers/ConnectSigner.ts new file mode 100644 index 0000000..b98a59f --- /dev/null +++ b/src/signers/ConnectSigner.ts @@ -0,0 +1,20 @@ +import { NConnectSigner } from '@nostrify/nostrify'; + +import { AdminSigner } from '@/signers/AdminSigner.ts'; +import { Storages } from '@/storages.ts'; + +/** + * NIP-46 signer. + * + * Simple extension of nostrify's `NConnectSigner`, with our options to keep it DRY. + */ +export class ConnectSigner extends NConnectSigner { + constructor(pubkey: string) { + super({ + pubkey, + relay: Storages.pubsub, + signer: new AdminSigner(), + timeout: 60000, + }); + } +}