Work on new net
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import type {WebSocketSubject} from "rxjs/websocket"
|
||||
import {Subject} from "rxjs"
|
||||
import type {SignedEvent} from "@welshman/util"
|
||||
import {createEvent, CLIENT_AUTH} from "@welshman/util"
|
||||
import type {SocketResponse} from "./socket.js"
|
||||
|
||||
export const createAuthEvent = (url: string, challenge: string) =>
|
||||
createEvent(CLIENT_AUTH, {
|
||||
tags: [
|
||||
["relay", url],
|
||||
["challenge", challenge],
|
||||
],
|
||||
})
|
||||
|
||||
export type AuthResult = {
|
||||
ok: boolean
|
||||
reason?: string
|
||||
}
|
||||
|
||||
export const authenticate = (socket: WebSocketSubject<SocketResponse>, event: SignedEvent) => {
|
||||
const subject = new Subject<AuthResult>()
|
||||
|
||||
socket.next(["AUTH", event])
|
||||
|
||||
socket.subscribe(message => {
|
||||
if (message[0] === "OK") {
|
||||
const [id, ok = false, reason = ""] = message.slice(1)
|
||||
|
||||
if (id === event.id) {
|
||||
subject.next({ok, reason})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return subject
|
||||
}
|
||||
|
||||
export const forceAuth = <T>(socket: WebSocketSubject<T>) => {}
|
||||
@@ -0,0 +1,10 @@
|
||||
import {webSocket} from "rxjs/websocket"
|
||||
import type {SignedEvent} from "@welshman/util"
|
||||
|
||||
export type SocketResponse =
|
||||
| ["AUTH", string]
|
||||
| ["EVENT", string, SignedEvent]
|
||||
| ["EOSE", string, SignedEvent]
|
||||
| ["OK", string, boolean, string]
|
||||
|
||||
export const connect = (url: string) => webSocket<SocketResponse>(url)
|
||||
Reference in New Issue
Block a user