From e94a40eaf61bf7c41c12267572718aa76a44e206 Mon Sep 17 00:00:00 2001 From: Water Blowater <127284497+BlowaterNostr@users.noreply.github.com> Date: Thu, 6 Jun 2024 18:36:57 +0800 Subject: [PATCH] Simple Authorization (#477) --- DevOps/stats.ts | 50 ----------------------------- DevOps/uniqueKeys.ts | 10 ------ app/UI/app.tsx | 26 +++------------ app/UI/app_update.tsx | 11 ++----- app/UI/config-other.ts | 22 +++++-------- app/UI/public-message-container.tsx | 4 +-- libs/nostr.ts | 2 +- 7 files changed, 18 insertions(+), 107 deletions(-) delete mode 100644 DevOps/stats.ts delete mode 100644 DevOps/uniqueKeys.ts diff --git a/DevOps/stats.ts b/DevOps/stats.ts deleted file mode 100644 index 62718a3..0000000 --- a/DevOps/stats.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { DB } from "https://deno.land/x/sqlite@v3.7.2/mod.ts"; -import { NostrKind } from "../libs/nostr.ts/nostr.ts"; -import { ConnectionPool } from "../libs/nostr.ts/relay-pool.ts"; - -// Open a database -const db = new DB("stats.sqlite"); -db.execute(` - CREATE TABLE IF NOT EXISTS stats ( - pubkey TEXT, - eventID TEXT, - PRIMARY KEY (pubkey, eventID) - ) -`); - -const pool = new ConnectionPool(); -const urls = [ - "wss://relay.damus.io", - "wss://nos.lol", - "wss://eden.nostr.land", - "wss://brb.io", - "wss://sg.qemura.xyz", - "wss://nostr-sg.com", - "wss://nostr-pub.wellorder.net", - "wss://relay.snort.social", - "wss://offchain.pub", -]; -for (const url of urls) { - pool.addRelayURL(url); -} - -const r = await pool.newSub("stats", { - kinds: [NostrKind.Custom_App_Data], -}); -if (r instanceof Error) { - throw r; -} - -for await (const { res: e, url } of r.chan) { - console.log(url); - if (e.type != "EVENT") { - continue; - } - - const pub = e.event.pubkey; - try { - db.query("INSERT INTO stats (pubkey, eventID) VALUES (?, ?)", [pub, e.event.id]); - } catch (e) { - console.log(e.message); - } -} diff --git a/DevOps/uniqueKeys.ts b/DevOps/uniqueKeys.ts deleted file mode 100644 index d84a754..0000000 --- a/DevOps/uniqueKeys.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { DB } from "https://deno.land/x/sqlite@v3.7.2/mod.ts"; - -const query = `SELECT DISTINCT(pubkey) From stats`; - -const db = new DB("stats.sqlite"); -const res = db.query(query); -for (const row of res) { - console.log(row); -} -console.log(res.length, "keys"); diff --git a/app/UI/app.tsx b/app/UI/app.tsx index 35d1bd7..7742cc4 100644 --- a/app/UI/app.tsx +++ b/app/UI/app.tsx @@ -51,21 +51,10 @@ export async function Start(database: DexieDatabase) { const lamport = new LamportTime(); const model = initialModel(); const eventBus = new EventBus(); - const pool = new ConnectionPool(); const popOverInputChan: PopOverInputChannel = new Channel(); const rightPanelInputChan: RightPanelChannel = new Channel(); const toastInputChan: ToastChannel = new Channel(); const dbView = await Database_View.New(database, database, database); - const newNostrEventChannel = new Channel(); - - (async () => { - for await (const event of newNostrEventChannel) { - const err = await pool.sendEvent(event); - if (err instanceof Error) { - console.error(err); - } - } - })(); { for (;;) { @@ -77,13 +66,13 @@ export async function Start(database: DexieDatabase) { console.error(ctx); break; } else if (ctx) { - const otherConfig = await OtherConfig.FromLocalStorage(ctx, newNostrEventChannel, lamport); + const otherConfig = await OtherConfig.FromLocalStorage(ctx, lamport); const app = await App.Start({ database: dbView, model, ctx, eventBus, - pool, + pool: new ConnectionPool({ signer: ctx }), popOverInputChan, rightPanelInputChan, otherConfig, @@ -101,7 +90,6 @@ export async function Start(database: DexieDatabase) { ); @@ -503,7 +485,7 @@ export class AppComponent extends Component; lamport: LamportTime; installPrompt: InstallPrompt; toastInputChan: ToastChannel; @@ -128,15 +126,13 @@ const handle_update_event = async (chan: PutChannel, args: { model: Model; eventBus: AppEventBus; dbView: Database_View; - pool: ConnectionPool; popOver: PopOverInputChannel; rightPanel: RightPanelChannel; - newNostrEventChannel: Channel; lamport: LamportTime; installPrompt: InstallPrompt; toastInputChan: ToastChannel; }) => { - const { model, dbView, eventBus, pool, installPrompt } = args; + const { model, dbView, eventBus, installPrompt } = args; for await (const event of eventBus.onChange()) { console.log(event); if (event.type == "SignInEvent") { @@ -144,7 +140,6 @@ const handle_update_event = async (chan: PutChannel, args: { console.log("sign in as", ctx.publicKey.bech32()); const otherConfig = await OtherConfig.FromLocalStorage( ctx, - args.newNostrEventChannel, args.lamport, ); const app = await App.Start({ @@ -152,7 +147,7 @@ const handle_update_event = async (chan: PutChannel, args: { model, ctx, eventBus, - pool, + pool: new ConnectionPool({ signer: ctx }), popOverInputChan: args.popOver, rightPanelInputChan: args.rightPanel, otherConfig, @@ -170,7 +165,7 @@ const handle_update_event = async (chan: PutChannel, args: { console.warn("This could not happen!"); continue; } - + const pool = app.pool; const blowater_relay = pool.getRelay(default_blowater_relay); if (blowater_relay == undefined) { console.error(Array.from(pool.getRelays())); diff --git a/app/UI/config-other.ts b/app/UI/config-other.ts index 350f6dd..5caf9d2 100644 --- a/app/UI/config-other.ts +++ b/app/UI/config-other.ts @@ -11,27 +11,26 @@ export type NostrEventAdder = { }; export class OtherConfig implements PinListGetter, NostrEventAdder { - static Empty(nostrEventPusher: Channel, ctx: NostrAccountContext, lamport: LamportTime) { - return new OtherConfig(nostrEventPusher, ctx, lamport); + static Empty(ctx: NostrAccountContext, lamport: LamportTime) { + return new OtherConfig(ctx, lamport); } static async FromLocalStorage( ctx: NostrAccountContext, - eventPusher: Channel, lamport: LamportTime, ) { const item = localStorage.getItem(`${OtherConfig.name}:${ctx.publicKey.bech32()}`); if (item == null) { - return OtherConfig.Empty(eventPusher, ctx, lamport); + return OtherConfig.Empty(ctx, lamport); } const event = parseJSON(item); if (event instanceof Error) { console.error(event); - return OtherConfig.Empty(eventPusher, ctx, lamport); + return OtherConfig.Empty(ctx, lamport); } const ok = await verifyEvent(event); if (!ok) { - return OtherConfig.Empty(eventPusher, ctx, lamport); + return OtherConfig.Empty(ctx, lamport); } if (event.kind == NostrKind.Encrypted_Custom_App_Data) { const config = await OtherConfig.FromNostrEvent( @@ -40,19 +39,17 @@ export class OtherConfig implements PinListGetter, NostrEventAdder { kind: event.kind, }, ctx, - eventPusher, lamport, ); if (config instanceof Error) { - return OtherConfig.Empty(eventPusher, ctx, lamport); + return OtherConfig.Empty(ctx, lamport); } return config; } - return OtherConfig.Empty(eventPusher, ctx, lamport); + return OtherConfig.Empty(ctx, lamport); } private constructor( - private readonly nostrEventPusher: Channel, private readonly ctx: NostrAccountContext, private readonly lamport: LamportTime, ) {} @@ -94,7 +91,6 @@ export class OtherConfig implements PinListGetter, NostrEventAdder { if (err instanceof Error) { return err; } - /* no await */ this.nostrEventPusher.put(event); } async removePin(pubkey: string) { @@ -121,13 +117,11 @@ export class OtherConfig implements PinListGetter, NostrEventAdder { if (err instanceof Error) { return err; } - /* no await */ this.nostrEventPusher.put(event); } static async FromNostrEvent( event: NostrEvent, ctx: NostrAccountContext, - pusher: Channel, lamport: LamportTime, ) { const decrypted = await ctx.decrypt(ctx.publicKey.hex, event.content, "nip4"); @@ -147,7 +141,7 @@ export class OtherConfig implements PinListGetter, NostrEventAdder { pinList = []; } - const c = new OtherConfig(pusher, ctx, lamport); + const c = new OtherConfig(ctx, lamport); for (const pin of pinList) { const err = await c.addPin(pin); if (err instanceof Error) { diff --git a/app/UI/public-message-container.tsx b/app/UI/public-message-container.tsx index 16f5583..52b1dc9 100644 --- a/app/UI/public-message-container.tsx +++ b/app/UI/public-message-container.tsx @@ -25,7 +25,7 @@ export type func_IsUserBlocked = (pubkey: PublicKey) => boolean; type Props = { ctx: NostrAccountContext; - relay: SingleRelayConnection; + relay_url: string; bus: EventBus; messages: ChatMessage[]; getters: { @@ -87,7 +87,7 @@ export class PublicMessageContainer extends Component {
{