import { Server } from "node:http"; import { ServerOptions, Socket } from "socket.io"; import { AsyncSessionManager, Peer, SessionManager, WalletInterface } from "@bsv/sdk"; //#region src/AuthSocketServer.d.ts type AuthSocketErrorPhase = 'authentication' | 'application' | 'connection' | 'send'; interface AuthSocketErrorContext { phase: AuthSocketErrorPhase; socketId?: string; eventName?: string; } type AuthSocketErrorHandler = (error: unknown, context: AuthSocketErrorContext) => void | Promise; declare function decodeAuthSocketEventPayload(payload: number[]): { eventName: string; data: any; }; interface AuthSocketServerOptions extends Partial { wallet: WalletInterface; requestedCertificates?: any; /** * Optional shared BRC-103 session store. Use an AsyncSessionManager backed by * a shared database when more than one server replica handles connections. */ sessionManager?: SessionManager | AsyncSessionManager; /** Maximum authentication messages processed concurrently by each socket. Defaults to 32. */ maxPendingAuthMessages?: number; /** Receives contained transport and application errors without exposing remote payloads. */ onError?: AuthSocketErrorHandler; } /** * A server-side wrapper for Socket.IO that integrates BRC-103 mutual authentication * to ensure secure, identity-aware communication between clients and the server. * * This class functions as a drop-in replacement for the `Server` class from Socket.IO, * with added support for: * - Automatic BRC-103 handshake for secure client authentication. * - Management of authenticated client sessions, avoiding redundant handshakes. * - Event-based communication through signed and verified BRC-103 messages. * * Features: * - Tracks client connections and their associated `Peer` and `AuthSocket` instances. * - Allows broadcasting messages to all authenticated clients. * - Provides a seamless API for developers by wrapping Socket.IO functionality. **/ declare class AuthSocketServer { private readonly options; private readonly realIo; /** * Map from socket.id -> peer info * * Once we discover the identity key, we store `identityKey` * for that connection to skip re-handshaking. */ private readonly peers; private readonly connectionCallbacks; private closePromise?; /** * @param httpServer - The underlying HTTP server * @param options - Contains both standard Socket.IO server config and BRC-103 config. */ constructor(httpServer: Server, options: AuthSocketServerOptions); /** * A direct pass-through to `io.on('connection', cb)`, * but the callback is invoked with an AuthSocket instead. */ on(eventName: 'connection', callback: (socket: AuthSocket) => void | Promise): void; on(eventName: string, callback: (data: any) => void | Promise): void; /** * Provide a classic pass-through to `io.emit(...)`. * * Under the hood, we sign a separate BRC-103 AuthMessage for each * authenticated peer. We'll embed eventName + data in the payload. */ emit(eventName: string, data: any): void; /** * Emit only to connections whose cryptographically authenticated peer * identity matches the requested identity key. * * This is safer than application-level "room" names for private delivery: * a client cannot subscribe itself to another identity because the routing * decision uses the key discovered by the BRC-103 handshake. * * @returns the number of authenticated connections selected for delivery */ emitToIdentity(identityKey: string, eventName: string, data: any): number; /** * Stops accepting connections, disconnects active sockets, and closes the * attached HTTP server. Repeated calls share the same shutdown operation. */ close(): Promise; /** * If the developer needs direct access to the underlying raw Socket.IO server, * we can provide a getter. */ private handleNewConnection; private encodeEventPayload; private reportError; private disconnectSafely; } /** * A wrapper around a real `IoSocket` used by a server that performs BRC-103 * signing and verification via the Peer class. */ declare class AuthSocket { readonly ioSocket: Socket; private readonly peer; /** * A function the server passes in so we can * notify it once we discover the peer's identity key. */ private readonly onIdentityKeyDiscovered; private readonly onError; private readonly eventCallbacks; /** * Current known identity key of the server, if discovered * (i.e. after the handshake yields a general message or * or we've forced a getAuthenticatedSession). */ private peerIdentityKey?; constructor(ioSocket: Socket, peer: Peer, /** * A function the server passes in so we can * notify it once we discover the peer's identity key. */ onIdentityKeyDiscovered: (socketId: string, identityKey: string) => void, onError?: AuthSocketErrorHandler); /** * Register a callback for an event name, just like `socket.on(...)`. */ on(eventName: string, callback: (data: any) => void | Promise): void; /** * Emulate `socket.emit(eventName, data)`. * We'll sign a BRC-103 `general` message via Peer, * embedding the event name & data in the payload. * * If we do not yet have the peer's identity key (handshake not done?), * the Peer will attempt the handshake. Once known, subsequent calls * will pass identityKey to skip the initial handshake. */ emit(eventName: string, data: any): Promise; /** * The Socket.IO 'id' */ get id(): string; /** * The client's identity key, if discovered */ get identityKey(): string | undefined; private encodeEventPayload; private decodeEventPayload; private reportError; private disconnectSafely; } //#endregion export { AuthSocket, AuthSocketErrorContext, AuthSocketErrorHandler, AuthSocketErrorPhase, AuthSocketServer, AuthSocketServerOptions, decodeAuthSocketEventPayload }; //# sourceMappingURL=AuthSocketServer.d.cts.map