NIP-46: request target proof-of-work difficulty when signing events

This commit is contained in:
Alex Gleason 2023-11-20 12:34:19 -06:00
parent 7ed34a0906
commit 6868f39719
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 20 additions and 5 deletions

View File

@ -91,7 +91,7 @@ function withProof(
async function obtainProof(c: AppContext, opts?: ParseAuthRequestOpts) { async function obtainProof(c: AppContext, opts?: ParseAuthRequestOpts) {
const req = localRequest(c); const req = localRequest(c);
const reqEvent = await buildAuthEventTemplate(req, opts); const reqEvent = await buildAuthEventTemplate(req, opts);
const resEvent = await signEvent(reqEvent, c); const resEvent = await signEvent(reqEvent, c, opts);
const result = await validateAuthEvent(req, resEvent, opts); const result = await validateAuthEvent(req, resEvent, opts);
if (result.success) { if (result.success) {

View File

@ -8,13 +8,22 @@ import { Sub } from '@/subs.ts';
import { eventMatchesTemplate, Time } from '@/utils.ts'; import { eventMatchesTemplate, Time } from '@/utils.ts';
import { createAdminEvent } from '@/utils/web.ts'; import { createAdminEvent } from '@/utils/web.ts';
interface SignEventOpts {
/** Target proof-of-work difficulty for the signed event. */
pow?: number;
}
/** /**
* Sign Nostr event using the app context. * Sign Nostr event using the app context.
* *
* - If a secret key is provided, it will be used to sign the event. * - If a secret key is provided, it will be used to sign the event.
* - If `X-Nostr-Sign` is passed, it will use NIP-46 to sign the event. * - If `X-Nostr-Sign` is passed, it will use NIP-46 to sign the event.
*/ */
async function signEvent<K extends number = number>(event: EventTemplate<K>, c: AppContext): Promise<Event<K>> { async function signEvent<K extends number = number>(
event: EventTemplate<K>,
c: AppContext,
opts: SignEventOpts = {},
): Promise<Event<K>> {
const seckey = c.get('seckey'); const seckey = c.get('seckey');
const header = c.req.headers.get('x-nostr-sign'); const header = c.req.headers.get('x-nostr-sign');
@ -23,7 +32,7 @@ async function signEvent<K extends number = number>(event: EventTemplate<K>, c:
} }
if (header) { if (header) {
return await signNostrConnect(event, c); return await signNostrConnect(event, c, opts);
} }
throw new HTTPException(400, { throw new HTTPException(400, {
@ -32,7 +41,11 @@ async function signEvent<K extends number = number>(event: EventTemplate<K>, c:
} }
/** Sign event with NIP-46, waiting in the background for the signed event. */ /** Sign event with NIP-46, waiting in the background for the signed event. */
async function signNostrConnect<K extends number = number>(event: EventTemplate<K>, c: AppContext): Promise<Event<K>> { async function signNostrConnect<K extends number = number>(
event: EventTemplate<K>,
c: AppContext,
opts: SignEventOpts = {},
): Promise<Event<K>> {
const pubkey = c.get('pubkey'); const pubkey = c.get('pubkey');
if (!pubkey) { if (!pubkey) {
@ -48,7 +61,9 @@ async function signNostrConnect<K extends number = number>(event: EventTemplate<
JSON.stringify({ JSON.stringify({
id: messageId, id: messageId,
method: 'sign_event', method: 'sign_event',
params: [event], params: [event, {
pow: opts.pow,
}],
}), }),
), ),
tags: [['p', pubkey]], tags: [['p', pubkey]],