Sign events from app context

This commit is contained in:
Alex Gleason 2023-04-29 21:48:22 -05:00
parent 1ba822abaa
commit 693c7cc7bf
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
4 changed files with 40 additions and 19 deletions

View File

@ -1,8 +1,9 @@
import { type AppContext, AppController } from '@/app.ts';
import { validator, z } from '@/deps.ts';
import { type Event } from '@/event.ts';
import { getAncestors, getDescendants, getEvent } from '../client.ts';
import { signEvent } from '@/sign.ts';
import { getAncestors, getDescendants, getEvent } from '../client.ts';
import publish from '../publisher.ts';
import { toStatus } from '../transmute.ts';
@ -22,22 +23,19 @@ const statusController: AppController = async (c) => {
};
const createStatusController = validator('json', async (value, c: AppContext) => {
const pubkey = c.get('pubkey')!;
const seckey = c.get('seckey');
const result = createStatusSchema.safeParse(value);
if (result.success && seckey) {
if (result.success) {
const { data } = result;
const event: Event<1> = {
const event = await signEvent<1>({
kind: 1,
pubkey: pubkey,
content: data.status,
tags: [],
created_at: Math.floor(new Date().getTime() / 1000),
};
}, c);
publish(event, seckey);
publish(event);
return c.json(await toStatus(event));
} else {

View File

@ -1,13 +1,22 @@
interface Event<K extends number = number> {
id?: string;
sig?: string;
interface EventTemplate<K extends number = number> {
kind: K;
tags: string[][];
pubkey: string;
content: string;
created_at: number;
}
type SignedEvent<K extends number = number> = Event<K> & { id: string; sig: string };
interface UnsignedEvent<K extends number = number> extends EventTemplate<K> {
pubkey: string;
}
export type { Event, SignedEvent };
interface Event<K extends number = number> extends UnsignedEvent<K> {
id?: string;
sig?: string;
}
interface SignedEvent<K extends number = number> extends Event<K> {
id: string;
sig: string;
}
export type { Event, EventTemplate, SignedEvent, UnsignedEvent };

View File

@ -1,13 +1,10 @@
import { getEventHash, getSignature } from '@/deps.ts';
import { type Event } from '@/event.ts';
import { type SignedEvent } from '@/event.ts';
import { pool } from './client.ts';
import { publishRelays } from './config.ts';
/** Publish an event to the Nostr relay. */
function publish(event: Event, privatekey: string, relays = publishRelays): void {
event.id = getEventHash(event);
event.sig = getSignature(event, privatekey);
function publish(event: SignedEvent, relays = publishRelays): void {
console.log('Publishing event', event);
try {
pool.publish(event, relays);

17
src/sign.ts Normal file
View File

@ -0,0 +1,17 @@
import { type AppContext } from '@/app.ts';
import { getEventHash, getPublicKey, getSignature } from '@/deps.ts';
import type { Event, EventTemplate, SignedEvent } from '@/event.ts';
/** Sign Nostr event using the app context. */
async function signEvent<K extends number = number>(event: EventTemplate<K>, c: AppContext): Promise<SignedEvent<K>> {
const seckey = c.get('seckey')!;
(event as Event<K>).pubkey = getPublicKey(seckey);
(event as Event<K>).id = getEventHash(event as Event<K>);
(event as Event<K>).sig = getSignature(event as Event<K>, seckey);
return event as SignedEvent<K>;
}
export { signEvent };