Sign nostr event from websocket
This commit is contained in:
@@ -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;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
@@ -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() {
|
||||
|
||||
8
app/soapbox/types/nostr.ts
Normal file
8
app/soapbox/types/nostr.ts
Normal 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
7
app/soapbox/types/window.d.ts
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
import type Nostr from './nostr';
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
nostr?: Nostr
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user