Sign events from app context
This commit is contained in:
parent
1ba822abaa
commit
693c7cc7bf
|
@ -1,8 +1,9 @@
|
||||||
import { type AppContext, AppController } from '@/app.ts';
|
import { type AppContext, AppController } from '@/app.ts';
|
||||||
import { validator, z } from '@/deps.ts';
|
import { validator, z } from '@/deps.ts';
|
||||||
import { type Event } from '@/event.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 publish from '../publisher.ts';
|
||||||
import { toStatus } from '../transmute.ts';
|
import { toStatus } from '../transmute.ts';
|
||||||
|
|
||||||
|
@ -22,22 +23,19 @@ const statusController: AppController = async (c) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const createStatusController = validator('json', async (value, c: AppContext) => {
|
const createStatusController = validator('json', async (value, c: AppContext) => {
|
||||||
const pubkey = c.get('pubkey')!;
|
|
||||||
const seckey = c.get('seckey');
|
|
||||||
const result = createStatusSchema.safeParse(value);
|
const result = createStatusSchema.safeParse(value);
|
||||||
|
|
||||||
if (result.success && seckey) {
|
if (result.success) {
|
||||||
const { data } = result;
|
const { data } = result;
|
||||||
|
|
||||||
const event: Event<1> = {
|
const event = await signEvent<1>({
|
||||||
kind: 1,
|
kind: 1,
|
||||||
pubkey: pubkey,
|
|
||||||
content: data.status,
|
content: data.status,
|
||||||
tags: [],
|
tags: [],
|
||||||
created_at: Math.floor(new Date().getTime() / 1000),
|
created_at: Math.floor(new Date().getTime() / 1000),
|
||||||
};
|
}, c);
|
||||||
|
|
||||||
publish(event, seckey);
|
publish(event);
|
||||||
|
|
||||||
return c.json(await toStatus(event));
|
return c.json(await toStatus(event));
|
||||||
} else {
|
} else {
|
||||||
|
|
21
src/event.ts
21
src/event.ts
|
@ -1,13 +1,22 @@
|
||||||
interface Event<K extends number = number> {
|
interface EventTemplate<K extends number = number> {
|
||||||
id?: string;
|
|
||||||
sig?: string;
|
|
||||||
kind: K;
|
kind: K;
|
||||||
tags: string[][];
|
tags: string[][];
|
||||||
pubkey: string;
|
|
||||||
content: string;
|
content: string;
|
||||||
created_at: number;
|
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 };
|
||||||
|
|
|
@ -1,13 +1,10 @@
|
||||||
import { getEventHash, getSignature } from '@/deps.ts';
|
import { type SignedEvent } from '@/event.ts';
|
||||||
import { type Event } from '@/event.ts';
|
|
||||||
|
|
||||||
import { pool } from './client.ts';
|
import { pool } from './client.ts';
|
||||||
import { publishRelays } from './config.ts';
|
import { publishRelays } from './config.ts';
|
||||||
|
|
||||||
/** Publish an event to the Nostr relay. */
|
/** Publish an event to the Nostr relay. */
|
||||||
function publish(event: Event, privatekey: string, relays = publishRelays): void {
|
function publish(event: SignedEvent, relays = publishRelays): void {
|
||||||
event.id = getEventHash(event);
|
|
||||||
event.sig = getSignature(event, privatekey);
|
|
||||||
console.log('Publishing event', event);
|
console.log('Publishing event', event);
|
||||||
try {
|
try {
|
||||||
pool.publish(event, relays);
|
pool.publish(event, relays);
|
||||||
|
|
|
@ -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 };
|
Loading…
Reference in New Issue