Finish rewriting the bunker...? (now everything else is broken)

environments/review-update-vid-g70vyz/deployments/5013
Alex Gleason 2024-10-28 17:00:46 -05:00
rodzic 78bf71bdf9
commit 333d3e76d6
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
4 zmienionych plików z 16 dodań i 26 usunięć

Wyświetl plik

@ -1,28 +1,23 @@
import { NSecSigner } from '@nostrify/nostrify'; import { NSecSigner } from '@nostrify/nostrify';
import { nip19 } from 'nostr-tools';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { useNostr } from 'soapbox/contexts/nostr-context'; import { useNostr } from 'soapbox/contexts/nostr-context';
import { NBunker, NBunkerOpts } from 'soapbox/features/nostr/NBunker'; import { NBunker, NBunkerOpts } from 'soapbox/features/nostr/NBunker';
import { NKeys } from 'soapbox/features/nostr/keys'; import { NKeys } from 'soapbox/features/nostr/keys';
import { useAppSelector } from 'soapbox/hooks'; import { useAppSelector } from 'soapbox/hooks';
import { useBunkerStore } from 'soapbox/hooks/useBunkerStore';
const secretStorageKey = 'soapbox:nip46:secret';
sessionStorage.setItem(secretStorageKey, crypto.randomUUID());
function useBunker() { function useBunker() {
const { relay } = useNostr(); const { relay } = useNostr();
const { authorizations, connections } = useBunkerStore();
const [isSubscribed, setIsSubscribed] = useState(false); const [isSubscribed, setIsSubscribed] = useState(false);
const [isSubscribing, setIsSubscribing] = useState(true); const [isSubscribing, setIsSubscribing] = useState(true);
const authorizations = useAppSelector((state) => state.bunker.authorizations);
const connection = useAppSelector((state) => { const connection = useAppSelector((state) => {
const accessToken = state.auth.tokens[state.auth.me!]?.access_token; const accessToken = state.auth.tokens[state.auth.me!]?.access_token;
if (accessToken) { if (accessToken) {
return state.bunker.connections.find((conn) => conn.accessToken === accessToken); return connections.find((conn) => conn.accessToken === accessToken);
} }
}); });
@ -38,14 +33,11 @@ function useBunker() {
const user = NKeys.get(pubkey) ?? window.nostr; const user = NKeys.get(pubkey) ?? window.nostr;
if (!user) return; if (!user) return;
const decoded = nip19.decode(bunkerSeckey);
if (decoded.type !== 'nsec') return;
return { return {
authorizedPubkey, authorizedPubkey,
signers: { signers: {
user, user,
bunker: new NSecSigner(decoded.data), bunker: new NSecSigner(bunkerSeckey),
}, },
}; };
})(), })(),
@ -55,22 +47,16 @@ function useBunker() {
const user = NKeys.get(pubkey) ?? window.nostr; const user = NKeys.get(pubkey) ?? window.nostr;
if (!user) return result; if (!user) return result;
const decoded = nip19.decode(bunkerSeckey);
if (decoded.type !== 'nsec') return result;
result.push({ result.push({
secret, secret,
signers: { signers: {
user, user,
bunker: new NSecSigner(decoded.data), bunker: new NSecSigner(bunkerSeckey),
}, },
}); });
return result; return result;
}, [] as NBunkerOpts['authorizations']), }, [] as NBunkerOpts['authorizations']),
onAuthorize(authorizedPubkey) {
sessionStorage.setItem(secretStorageKey, crypto.randomUUID());
},
onSubscribed() { onSubscribed() {
setIsSubscribed(true); setIsSubscribed(true);
setIsSubscribing(false); setIsSubscribing(false);

Wyświetl plik

@ -27,7 +27,6 @@ export interface NBunkerOpts {
relay: NRelay; relay: NRelay;
connection?: NBunkerConnection; connection?: NBunkerConnection;
authorizations: NBunkerAuthorization[]; authorizations: NBunkerAuthorization[];
onAuthorize(pubkey: string): void;
onSubscribed(): void; onSubscribed(): void;
} }
@ -36,7 +35,6 @@ export class NBunker {
private relay: NRelay; private relay: NRelay;
private connection?: NBunkerConnection; private connection?: NBunkerConnection;
private authorizations: NBunkerAuthorization[]; private authorizations: NBunkerAuthorization[];
private onAuthorize: (pubkey: string) => void;
private onSubscribed: () => void; private onSubscribed: () => void;
private controller = new AbortController(); private controller = new AbortController();
@ -45,7 +43,6 @@ export class NBunker {
this.relay = opts.relay; this.relay = opts.relay;
this.connection = opts.connection; this.connection = opts.connection;
this.authorizations = opts.authorizations; this.authorizations = opts.authorizations;
this.onAuthorize = opts.onAuthorize;
this.onSubscribed = opts.onSubscribed; this.onSubscribed = opts.onSubscribed;
this.open(); this.open();
@ -171,8 +168,6 @@ export class NBunker {
const [, secret] = request.params; const [, secret] = request.params;
if (secret === authorization.secret) { if (secret === authorization.secret) {
this.onAuthorize(event.pubkey);
await this.sendResponse(event.pubkey, { await this.sendResponse(event.pubkey, {
id: request.id, id: request.id,
result: 'ack', result: 'ack',

Wyświetl plik

@ -77,6 +77,8 @@ const stateSchema = z.object({
interface BunkerState { interface BunkerState {
connections: BunkerConnection[]; connections: BunkerConnection[];
authorizations: BunkerAuthorization[]; authorizations: BunkerAuthorization[];
authorize(pubkey: string): BunkerURI;
connect(request: BunkerConnectRequest): void;
} }
export const useBunkerStore = create<BunkerState>()( export const useBunkerStore = create<BunkerState>()(
@ -132,6 +134,15 @@ export const useBunkerStore = create<BunkerState>()(
}); });
}); });
}, },
/** Revoke any connections associated with the access token. */
revoke(accessToken: string) {
setState((state) => {
return produce(state, (draft) => {
draft.connections = draft.connections.filter((conn) => conn.accessToken !== accessToken);
});
});
},
}), }),
{ {
name: 'soapbox:bunker', name: 'soapbox:bunker',

Wyświetl plik

@ -7,7 +7,6 @@ import admin from './admin';
import aliases from './aliases'; import aliases from './aliases';
import auth from './auth'; import auth from './auth';
import backups from './backups'; import backups from './backups';
import bunker from './bunker';
import chat_message_lists from './chat-message-lists'; import chat_message_lists from './chat-message-lists';
import chat_messages from './chat-messages'; import chat_messages from './chat-messages';
import chats from './chats'; import chats from './chats';
@ -112,5 +111,4 @@ export default combineReducers({
trending_statuses, trending_statuses,
trends, trends,
user_lists, user_lists,
bunker: bunker.reducer,
}); });