Auth
Handles NIP-42 relay authentication flow.
Core Classes
AuthState
Manages authentication state for a socket connection.
Status Values:
AuthStatus.None
- No authentication required/attemptedAuthStatus.Requested
- Relay requested authenticationAuthStatus.PendingSignature
- Waiting for user to sign auth eventAuthStatus.DeniedSignature
- User denied signingAuthStatus.PendingResponse
- Waiting for relay responseAuthStatus.Forbidden
- Authentication failedAuthStatus.Ok
- Authentication successful
Methods:
doAuth(sign)
- Authenticate with the relay using provided signing functionattemptAuth(sign)
- Attempt authentication with timeout handlingcleanup()
- Clean up event listeners
Events:
AuthStateEvent.Status
- Emitted when authentication status changes
Example
typescript
const authState = new AuthState(socket)
// Listen for auth status changes
authState.on(AuthStateEvent.Status, (status) => {
console.log('Auth status:', status)
})
// Attempt authentication when relay requests it
await authState.attemptAuth(async (template) => {
return await signer.signEvent(template)
})