Loading…
Server-side BRC-103 mutual authentication wrapper for Socket.IO. Enforces cryptographic signing and verification on all WebSocket messages, enabling peer-to-peer identity verification and certificate exchange.
npm install @bsv/authsocketimport { AuthSocketServer } from '@bsv/authsocket'
import { PrivateKey, ProtoWallet } from '@bsv/sdk'
import http from 'http'
const server = http.createServer()
const serverWallet = new ProtoWallet(
PrivateKey.fromHex('0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef')
)
const io = new AuthSocketServer(server, { wallet: serverWallet, cors: { origin: '*' } })
io.on('connection', async socket => {
console.log('Authenticated socket:', socket.id)
socket.on('chatMessage', msg => {
console.log('Verified message:', msg)
})
await socket.emit('chatMessage', { from: socket.id, text: 'Hello client' })
})
server.listen(3000)'authMessage' channel for BRC-103 frames; user code sees normal Socket.IO eventsclose() disconnects clients and closes the attached HTTP serverimport { AuthSocketServer } from '@bsv/authsocket'
import { PrivateKey, ProtoWallet } from '@bsv/sdk'
const wallet = new ProtoWallet(PrivateKey.fromHex(privateKeyHex))
const io = new AuthSocketServer(server, {
wallet,
cors: { origin: '*' },
requestedCertificates: {
certifiers: ['<33-byte-pubkey-hex>'],
types: {
'age-verification': ['dateOfBirth', 'country']
}
}
})
io.on('connection', async socket => {
socket.on('message', data => {
// All messages already verified
})
await socket.emit('response', { authenticated: true })
})process.once('SIGTERM', () => {
void io.close()
})close() is idempotent. Socket.IO disconnects active clients before closing
the HTTP server supplied to AuthSocketServer.
const io = new AuthSocketServer(server, {
wallet,
maxPendingAuthMessages: 32,
onError: (error, context) => {
console.error(context.phase, context.socketId, error)
}
})Authentication, connection, and application callback failures are contained
and disconnect only the affected socket. The error context identifies the
phase and socket without including remote payloads or wallet material. An
onError handler that throws or rejects is also contained.
io.on('connection', async socket => {
// Messages are automatically verified before reaching here
socket.on('userAction', action => {
console.log('Verified action from', socket.id, ':', action)
})
// Send authenticated response
await socket.emit('status', { result: 'success' })
})PeerSessionManager'authMessage' channel for BRC-103 frames; user code sees normal Socket.IO events@bsv/auth-express-middlewarePeer and Transport abstractions from SDKsign() and verify() methodscors config for browser clients