remove listeners, set auth function onto system

This commit is contained in:
Liran Cohen 2023-01-25 08:37:35 -05:00 committed by Kieran
parent 97a467260d
commit 2d75139065
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
5 changed files with 39 additions and 74 deletions

View File

@ -7,8 +7,6 @@ import { RootState } from "State/Store";
import { HexKey, RawEvent, u256, UserMetadata } from "Nostr";
import { bech32ToHex } from "Util"
import { DefaultRelays, HashtagRegex } from "Const";
import { useEffect } from "react";
import { NIP42AuthChallenge, NIP42AuthResponse } from "Nostr/Auth";
declare global {
interface Window {
@ -79,7 +77,7 @@ export default function useEventPublisher() {
}
return {
nip42Auth: async (challenge: string, relay:string): Promise<NEvent> => {
nip42Auth: async (challenge: string, relay:string) => {
if(pubKey) {
const ev = NEvent.ForPubKey(pubKey);
ev.Kind = EventKind.Auth;
@ -88,7 +86,6 @@ export default function useEventPublisher() {
ev.Tags.push(new Tag(["challenge", challenge], 1));
return await signEvent(ev);
}
return Promise.reject();
},
broadcast: (ev: NEvent | undefined) => {
if (ev) {

View File

@ -1,19 +0,0 @@
import {default as NEvent} from "./Event"
export class NIP42AuthChallenge extends Event {
challenge?:string
relay?:string
constructor(challenge:string, relay:string) {
super("nip42auth");
this.challenge = challenge;
this.relay = relay;
}
}
export class NIP42AuthResponse extends Event {
event?: NEvent
constructor(challenge: string, event: NEvent) {
super(`nip42response:${challenge}`);
this.event = event;
}
}

View File

@ -8,6 +8,7 @@ import { ConnectionStats } from "Nostr/ConnectionStats";
import { RawEvent, TaggedRawEvent, u256 } from "Nostr";
import { RelayInfo } from "./RelayInfo";
import Nips from "./Nips";
import { System } from "./System";
export type CustomHook = (state: Readonly<StateSnapshot>) => void;
@ -367,46 +368,38 @@ export default class Connection {
}
}
async _OnAuthAsync(challenge: string) {
const challengeEvent = new NIP42AuthChallenge(challenge, this.Address)
async _OnAuthAsync(challenge: string): Promise<void> {
const authCleanup = () => {
this.AwaitingAuth.delete(challenge)
}
const authCallback = (e:NIP42AuthResponse):Promise<void> => {
window.removeEventListener(`nip42response:${challenge}`, authCallback)
return new Promise((resolve,_) => {
if(!e.event) {
authCleanup();
return Promise.reject('no event');
}
let t = setTimeout(() => {
authCleanup();
resolve();
}, 10_000);
this.EventsCallback.set(e.event.Id, (msg:any[]) => {
clearTimeout(t);
authCleanup();
if(msg.length > 3 && msg[2] === true) {
this.Authed = true;
this._InitSubscriptions();
}
resolve();
});
let req = ["AUTH", e.event.ToObject()];
this._SendJson(req);
this.Stats.EventsSent++;
this._UpdateState();
})
}
this.AwaitingAuth.set(challenge, true)
window.addEventListener(`nip42response:${challenge}`, authCallback)
window.dispatchEvent(challengeEvent)
const authEvent = await System.nip42Auth(challenge, this.Address)
return new Promise((resolve,_) => {
if(!authEvent) {
authCleanup();
return Promise.reject('no event');
}
let t = setTimeout(() => {
authCleanup();
resolve();
}, 10_000);
this.EventsCallback.set(authEvent.Id, (msg:any[]) => {
clearTimeout(t);
authCleanup();
if(msg.length > 3 && msg[2] === true) {
this.Authed = true;
this._InitSubscriptions();
}
resolve();
});
let req = ["AUTH", authEvent.ToObject()];
this._SendJson(req);
this.Stats.EventsSent++;
this._UpdateState();
})
}
_OnEnd(subId: string) {

View File

@ -212,6 +212,10 @@ export class NostrSystem {
setTimeout(() => this._FetchMetadata(), 500);
}
async nip42Auth(challenge: string, relay:string): Promise<Event|undefined> {
return
}
}
export const System = new NostrSystem();

View File

@ -1,5 +1,5 @@
import "./Layout.css";
import { useEffect } from "react"
import { useEffect, useMemo } from "react"
import { useDispatch, useSelector } from "react-redux";
import { Outlet, useNavigate } from "react-router-dom";
import { faBell, faMessage, faSearch } from "@fortawesome/free-solid-svg-icons";
@ -15,7 +15,6 @@ import useLoginFeed from "Feed/LoginFeed";
import { totalUnread } from "Pages/MessagesPage";
import { SearchRelays } from 'Const';
import useEventPublisher from "Feed/EventPublisher";
import { NIP42AuthChallenge, NIP42AuthResponse } from "Nostr/Auth";
export default function Layout() {
const dispatch = useDispatch();
@ -30,17 +29,12 @@ export default function Layout() {
const pub = useEventPublisher();
useLoginFeed();
useMemo(() => {
System.nip42Auth = pub.nip42Auth
},[pub])
useEffect(() => {
if (relays) {
const nip42AuthEvent = async (event: NIP42AuthChallenge) => {
if(event.challenge && event.relay) {
const signedEvent = await pub.nip42Auth(event.challenge, event.relay);
const response = new NIP42AuthResponse(event.challenge, signedEvent);
window.dispatchEvent(response);
}
}
window.addEventListener("nip42auth", nip42AuthEvent)
for (let [k, v] of Object.entries(relays)) {
System.ConnectToRelay(k, v);
}
@ -49,10 +43,6 @@ export default function Layout() {
System.DisconnectRelay(k);
}
}
return () => {
window.removeEventListener("nip42auth", nip42AuthEvent)
}
}
}, [relays]);