Nostr: sign events with NIP-46 See merge request soapbox-pub/soapbox!2649environments/review-develop-3zknud/deployments/3737
commit
62d56634e0
@ -0,0 +1,57 @@
|
||||
import { relayInit, type Relay } from 'nostr-tools';
|
||||
import { useEffect } from 'react';
|
||||
|
||||
import { useInstance } from 'soapbox/hooks';
|
||||
import { connectRequestSchema } from 'soapbox/schemas/nostr';
|
||||
import { jsonSchema } from 'soapbox/schemas/utils';
|
||||
|
||||
function useSignerStream() {
|
||||
const { nostr } = useInstance();
|
||||
|
||||
const relayUrl = nostr.get('relay') as string | undefined;
|
||||
const pubkey = nostr.get('pubkey') as string | undefined;
|
||||
|
||||
useEffect(() => {
|
||||
let relay: Relay | undefined;
|
||||
|
||||
if (relayUrl && pubkey && window.nostr?.nip04) {
|
||||
relay = relayInit(relayUrl);
|
||||
relay.connect();
|
||||
|
||||
relay
|
||||
.sub([{ kinds: [24133], authors: [pubkey], limit: 0 }])
|
||||
.on('event', async (event) => {
|
||||
if (!relay || !window.nostr?.nip04) return;
|
||||
|
||||
const decrypted = await window.nostr.nip04.decrypt(pubkey, event.content);
|
||||
const reqMsg = jsonSchema.pipe(connectRequestSchema).safeParse(decrypted);
|
||||
|
||||
if (!reqMsg.success) {
|
||||
console.warn(decrypted);
|
||||
console.warn(reqMsg.error);
|
||||
return;
|
||||
}
|
||||
|
||||
const signed = await window.nostr.signEvent(reqMsg.data.params[0]);
|
||||
const respMsg = {
|
||||
id: reqMsg.data.id,
|
||||
result: signed,
|
||||
};
|
||||
|
||||
const respEvent = await window.nostr.signEvent({
|
||||
kind: 24133,
|
||||
content: await window.nostr.nip04.encrypt(pubkey, JSON.stringify(respMsg)),
|
||||
tags: [['p', pubkey]],
|
||||
created_at: Math.floor(Date.now() / 1000),
|
||||
});
|
||||
|
||||
relay.publish(respEvent);
|
||||
});
|
||||
}
|
||||
return () => {
|
||||
relay?.close();
|
||||
};
|
||||
}, [relayUrl, pubkey]);
|
||||
}
|
||||
|
||||
export { useSignerStream };
|
@ -1,20 +0,0 @@
|
||||
import { useFeatures, useLoggedIn } from 'soapbox/hooks';
|
||||
|
||||
import { useTimelineStream } from './useTimelineStream';
|
||||
|
||||
function useNostrStream() {
|
||||
const features = useFeatures();
|
||||
const { isLoggedIn } = useLoggedIn();
|
||||
|
||||
return useTimelineStream(
|
||||
'nostr',
|
||||
'nostr',
|
||||
null,
|
||||
null,
|
||||
{
|
||||
enabled: isLoggedIn && features.nostrSign && Boolean(window.nostr),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
export { useNostrStream };
|
@ -0,0 +1,34 @@
|
||||
import { verifySignature } from 'nostr-tools';
|
||||
import { z } from 'zod';
|
||||
|
||||
/** Schema to validate Nostr hex IDs such as event IDs and pubkeys. */
|
||||
const nostrIdSchema = z.string().regex(/^[0-9a-f]{64}$/);
|
||||
/** Nostr kinds are positive integers. */
|
||||
const kindSchema = z.number().int().positive();
|
||||
|
||||
/** Nostr event template schema. */
|
||||
const eventTemplateSchema = z.object({
|
||||
kind: kindSchema,
|
||||
tags: z.array(z.array(z.string())),
|
||||
content: z.string(),
|
||||
created_at: z.number(),
|
||||
});
|
||||
|
||||
/** Nostr event schema. */
|
||||
const eventSchema = eventTemplateSchema.extend({
|
||||
id: nostrIdSchema,
|
||||
pubkey: nostrIdSchema,
|
||||
sig: z.string(),
|
||||
});
|
||||
|
||||
/** Nostr event schema that also verifies the event's signature. */
|
||||
const signedEventSchema = eventSchema.refine(verifySignature);
|
||||
|
||||
/** NIP-46 signer request. */
|
||||
const connectRequestSchema = z.object({
|
||||
id: z.string(),
|
||||
method: z.literal('sign_event'),
|
||||
params: z.tuple([eventTemplateSchema]),
|
||||
});
|
||||
|
||||
export { nostrIdSchema, kindSchema, eventSchema, signedEventSchema, connectRequestSchema };
|
Loading…
Reference in new issue