This source candidate declares SDK peer ^2.4.1 || ^3.0.0. SDK3 remains
a coordinated proposal; see the qualification and migration limits
before adopting it.
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)The connection callback runs only after identity proof and completes before any first or concurrently received application event is dispatched. Until every connection callback finishes, the peer is excluded from broadcasts and identity-targeted delivery. A failed callback therefore cannot leave queued messages authorized.
requestedCertificates is only the legacy v0.1 SDK allowlist. It cannot express
complete application fulfillment semantics, and this wrapper does not expose
the evidence to the connection callback. Do not infer certificate-based
authorization from configuring the option; authorize the verified identity
independently or use an integration with an explicit evidence callback.
'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