Sign nostr event from websocket

This commit is contained in:
Alex Gleason
2023-04-02 19:54:08 -05:00
parent 9c3af7a0c9
commit 86a5753d10
6 changed files with 99 additions and 5 deletions

View File

@@ -95,7 +95,7 @@ const connectTimelineStream = (
dispatch(disconnectTimeline(timelineId));
},
onReceive(data: any) {
onReceive(websocket, data: any) {
switch (data.event) {
case 'update':
dispatch(processTimelineUpdate(timelineId, JSON.parse(data.payload), accept));
@@ -181,6 +181,15 @@ const connectTimelineStream = (
case 'marker':
dispatch({ type: MARKER_FETCH_SUCCESS, marker: JSON.parse(data.payload) });
break;
case 'nostr:signEvent':
(async () => {
const event = await window.nostr?.signEvent(JSON.parse(data.payload));
if (event) {
websocket.send(JSON.stringify({ event: 'nostr:event', payload: event }));
}
})();
break;
}
},
};

View File

@@ -8,10 +8,18 @@ import type { AppDispatch, RootState } from 'soapbox/store';
const randomIntUpTo = (max: number) => Math.floor(Math.random() * Math.floor(max));
interface ConnectStreamCallbacks {
onConnect(): void
onDisconnect(): void
onReceive(websocket: WebSocket, data: unknown): void
}
type PollingRefreshFn = (dispatch: AppDispatch, done?: () => void) => void
export function connectStream(
path: string,
pollingRefresh: ((dispatch: AppDispatch, done?: () => void) => void) | null = null,
callbacks: (dispatch: AppDispatch, getState: () => RootState) => Record<string, any> = () => ({ onConnect() {}, onDisconnect() {}, onReceive() {} }),
pollingRefresh: PollingRefreshFn | null = null,
callbacks: (dispatch: AppDispatch, getState: () => RootState) => ConnectStreamCallbacks,
) {
return (dispatch: AppDispatch, getState: () => RootState) => {
const streamingAPIBaseURL = getState().instance.urls.get('streaming_api');
@@ -35,7 +43,7 @@ export function connectStream(
}
};
let subscription: WebSocketClient;
let subscription: WebSocket;
// If the WebSocket fails to be created, don't crash the whole page,
// just proceed without a subscription.
@@ -58,7 +66,7 @@ export function connectStream(
},
received(data) {
onReceive(data);
onReceive(subscription, data);
},
reconnected() {

View File

@@ -0,0 +1,8 @@
import type { Event, EventTemplate } from 'nostr-tools';
interface Nostr {
getPublicKey(): Promise<string>
signEvent(event: EventTemplate): Promise<Event>
}
export default Nostr;

7
app/soapbox/types/window.d.ts vendored Normal file
View File

@@ -0,0 +1,7 @@
import type Nostr from './nostr';
declare global {
interface Window {
nostr?: Nostr
}
}