first pass at nip42 authentication #93
@ -7,6 +7,8 @@ import { RootState } from "State/Store";
|
|||||||
import { HexKey, RawEvent, u256, UserMetadata } from "Nostr";
|
import { HexKey, RawEvent, u256, UserMetadata } from "Nostr";
|
||||||
import { bech32ToHex } from "Util"
|
import { bech32ToHex } from "Util"
|
||||||
import { DefaultRelays, HashtagRegex } from "Const";
|
import { DefaultRelays, HashtagRegex } from "Const";
|
||||||
|
import { useEffect } from "react";
|
||||||
|
import { NIP42AuthChallenge, NIP42AuthResponse } from "Nostr/Auth";
|
||||||
|
|
||||||
declare global {
|
declare global {
|
||||||
interface Window {
|
interface Window {
|
||||||
@ -76,6 +78,33 @@ export default function useEventPublisher() {
|
|||||||
ev.Content = content;
|
ev.Content = content;
|
||||||
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
const nip42Auth = async (challenge: string, relay:string): Promise<NEvent> => {
|
||||||
|
if(pubKey) {
|
||||||
|
const ev = NEvent.ForPubKey(pubKey);
|
||||||
|
ev.Kind = EventKind.Auth;
|
||||||
|
ev.Content = "";
|
||||||
|
ev.Tags.push(new Tag(["relay", relay], 0));
|
||||||
|
ev.Tags.push(new Tag(["challenge", challenge], 1));
|
||||||
|
return await signEvent(ev);
|
||||||
|
}
|
||||||
|
return Promise.reject();
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() =>{
|
||||||
|
const nip42AuthEvent = async (event: NIP42AuthChallenge) => {
|
||||||
|
if(event.challenge && event.relay) {
|
||||||
|
const signedEvent = await nip42Auth(event.challenge, event.relay);
|
||||||
|
const response = new NIP42AuthResponse(event.challenge, signedEvent);
|
||||||
|
window.dispatchEvent(response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
window.addEventListener("nip42auth", nip42AuthEvent)
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener("nip42auth", nip42AuthEvent)
|
||||||
|
}
|
||||||
|
}, [])
|
||||||
|
|
||||||
return {
|
return {
|
||||||
broadcast: (ev: NEvent | undefined) => {
|
broadcast: (ev: NEvent | undefined) => {
|
||||||
if (ev) {
|
if (ev) {
|
||||||
|
19
src/Nostr/Auth.ts
Normal file
19
src/Nostr/Auth.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
@ -48,6 +48,8 @@ export default class Connection {
|
|||||||
IsClosed: boolean;
|
IsClosed: boolean;
|
||||||
ReconnectTimer: ReturnType<typeof setTimeout> | null;
|
ReconnectTimer: ReturnType<typeof setTimeout> | null;
|
||||||
EventsCallback: Map<u256, () => void>;
|
EventsCallback: Map<u256, () => void>;
|
||||||
|
Authed: boolean;
|
||||||
|
AwaitingAuth: boolean;
|
||||||
|
|
||||||
constructor(addr: string, options: RelaySettings) {
|
constructor(addr: string, options: RelaySettings) {
|
||||||
this.Address = addr;
|
this.Address = addr;
|
||||||
@ -72,6 +74,8 @@ export default class Connection {
|
|||||||
this.IsClosed = false;
|
this.IsClosed = false;
|
||||||
this.ReconnectTimer = null;
|
this.ReconnectTimer = null;
|
||||||
this.EventsCallback = new Map();
|
this.EventsCallback = new Map();
|
||||||
|
this.Authed = false;
|
||||||
|
this.AwaitingAuth = false;
|
||||||
this.Connect();
|
this.Connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,17 +123,12 @@ export default class Connection {
|
|||||||
OnOpen(e: Event) {
|
OnOpen(e: Event) {
|
||||||
this.ConnectTimeout = DefaultConnectTimeout;
|
this.ConnectTimeout = DefaultConnectTimeout;
|
||||||
console.log(`[${this.Address}] Open!`);
|
console.log(`[${this.Address}] Open!`);
|
||||||
|
setTimeout(() => {
|
||||||
// send pending
|
if(this.AwaitingAuth) {
|
||||||
for (let p of this.Pending) {
|
return
|
||||||
this._SendJson(p);
|
}
|
||||||
}
|
this._InitSubscriptions();
|
||||||
this.Pending = [];
|
}, 500)
|
||||||
|
|
||||||
for (let [_, s] of this.Subscriptions) {
|
|
||||||
this._SendSubscription(s);
|
|
||||||
}
|
|
||||||
this._UpdateState();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
OnClose(e: CloseEvent) {
|
OnClose(e: CloseEvent) {
|
||||||
@ -152,6 +151,12 @@ export default class Connection {
|
|||||||
let msg = JSON.parse(e.data);
|
let msg = JSON.parse(e.data);
|
||||||
let tag = msg[0];
|
let tag = msg[0];
|
||||||
switch (tag) {
|
switch (tag) {
|
||||||
|
case "AUTH": {
|
||||||
|
this._OnAuth(msg[1])
|
||||||
|
this.Stats.EventsReceived++;
|
||||||
|
this._UpdateState();
|
||||||
|
break;
|
||||||
|
}
|
||||||
case "EVENT": {
|
case "EVENT": {
|
||||||
this._OnEvent(msg[1], msg[2]);
|
this._OnEvent(msg[1], msg[2]);
|
||||||
this.Stats.EventsReceived++;
|
this.Stats.EventsReceived++;
|
||||||
@ -297,6 +302,19 @@ export default class Connection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_InitSubscriptions() {
|
||||||
|
// send pending
|
||||||
|
for (let p of this.Pending) {
|
||||||
|
this._SendJson(p);
|
||||||
|
}
|
||||||
|
this.Pending = [];
|
||||||
|
|
||||||
|
for (let [_, s] of this.Subscriptions) {
|
||||||
|
this._SendSubscription(s);
|
||||||
|
}
|
||||||
|
this._UpdateState();
|
||||||
|
}
|
||||||
|
|
||||||
_SendSubscription(sub: Subscriptions) {
|
_SendSubscription(sub: Subscriptions) {
|
||||||
let req = ["REQ", sub.Id, sub.ToObject()];
|
let req = ["REQ", sub.Id, sub.ToObject()];
|
||||||
if (sub.OrSubs.length > 0) {
|
if (sub.OrSubs.length > 0) {
|
||||||
@ -332,6 +350,40 @@ export default class Connection {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async _OnAuth(challenge: string, timeout: number = 5000):Promise<void> {
|
||||||
|
const challengeEvent = new NIP42AuthChallenge(challenge, this.Address)
|
||||||
|
|
||||||
|
const authCallback = (e:NIP42AuthResponse):Promise<void> => {
|
||||||
|
return new Promise((resolve,reject) => {
|
||||||
|
if(!e.event) {
|
||||||
|
return Promise.reject('no event');
|
||||||
|
}
|
||||||
|
|
||||||
|
let t = setTimeout(() => {
|
||||||
|
window.removeEventListener(`nip42response:${challenge}`, authCallback);
|
||||||
|
this.AwaitingAuth = false;
|
||||||
|
reject('timeout');
|
||||||
|
}, timeout);
|
||||||
|
|
||||||
|
this.EventsCallback.set(e.event.Id, () => {
|
||||||
|
clearTimeout(t);
|
||||||
|
this.AwaitingAuth = false;
|
||||||
|
window.removeEventListener(`nip42response:${challenge}`, authCallback)
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
|
|
||||||
|
let req = ["AUTH", e.event.ToObject()];
|
||||||
|
this._SendJson(req);
|
||||||
|
this.Stats.EventsSent++;
|
||||||
|
this._UpdateState();
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
this.AwaitingAuth = true;
|
||||||
|
window.addEventListener(`nip42response:${challenge}`, authCallback)
|
||||||
|
window.dispatchEvent(challengeEvent)
|
||||||
|
}
|
||||||
|
|
||||||
_OnEnd(subId: string) {
|
_OnEnd(subId: string) {
|
||||||
let sub = this.Subscriptions.get(subId);
|
let sub = this.Subscriptions.get(subId);
|
||||||
if (sub) {
|
if (sub) {
|
||||||
|
@ -7,7 +7,8 @@ const enum EventKind {
|
|||||||
DirectMessage = 4, // NIP-04
|
DirectMessage = 4, // NIP-04
|
||||||
Deletion = 5, // NIP-09
|
Deletion = 5, // NIP-09
|
||||||
Repost = 6, // NIP-18
|
Repost = 6, // NIP-18
|
||||||
Reaction = 7 // NIP-25
|
Reaction = 7, // NIP-25
|
||||||
|
Auth = 22242 // NIP-42
|
||||||
};
|
};
|
||||||
|
|
||||||
export default EventKind;
|
export default EventKind;
|
Loading…
Reference in New Issue
Block a user
You can simply just not return anything here instead, undefined is fine