@bsv/authsocket-client

中文文档
2.1.10发布于 2026/10/28SEE LICENSE IN LICENSE.txtTypeScriptESM + CJS构建来源已验证近 30 天下载 6888

Mutually Authenticated Web Sockets Client

原始使用文档
文档目录11 节
  1. Overview
  2. Installation
  3. Usage
  4. Failure isolation and resource limits
  5. How It Works (Briefly)
  6. Detailed Explanations
  7. SocketClientTransport
  8. AuthSocketClient
  9. License
  10. Development and distribution
  11. SDK4 hotfix peer requirement

AuthSocket (client-side)

Patch candidate 2.1.10 updates packed first-party runtime or development dependency ranges. This metadata-only patch preserves public APIs, peer ranges, wire formats and defaults. No additional consumer or data migration is required; earlier migration guidance still applies.

Overview

This package provides a drop-in client-side solution for Socket.IO that signs outbound messages and verifies inbound messages using BRC-103.

  • Works with @bsv/authsocket or any BRC-103-compatible server.
  • Minimal changes compared to normal socket.io-client usage.

Installation

Install the client and its required SDK peer:

npm install @bsv/authsocket-client @bsv/sdk

Provide a BRC-103-compatible Wallet, such as one from @bsv/sdk.

Usage

Below is a minimal client code that wraps socket.io-client:

import { AuthSocketClient } from '@bsv/authsocket-client'
import { ProtoWallet } from '@bsv/sdk' // your BRC-103-compatible wallet

// Create or load your local BRC-103 wallet
const clientWallet = new ProtoWallet('client-private-key-hex')

// Wrap the normal Socket.IO client with AuthSocketClient
const socket = AuthSocketClient('http://localhost:3000', {
  wallet: clientWallet,
  onError: (error, context) => {
    // Context identifies the phase and event without copying the remote payload.
    console.error(context.phase, context.eventName, error)
  }
})

// Standard Socket.IO usage
socket.on('connect', () => {
  console.log('Socket.IO transport connected. Socket ID:', socket.id)

  // Emit a sample message
  socket.emit('chatMessage', {
    text: 'Hello from client!'
  })
})

socket.on('chatMessage', msg => {
  console.log('Server says:', msg)
})

socket.on('disconnect', () => {
  console.log('Disconnected from server')
})
  1. Use AuthSocketClient(serverUrl, options) to create a BRC-103-secured socket client.
  2. Interact with .on(...), .emit(...) as normal.
  3. Behind the scenes, each message is signed with your client wallet key and verified by the server. Inbound messages are also verified.

The standard connect event and connected property report only the underlying Socket.IO transport. They do not assert that the server has completed BRC-103 authentication. An outbound application emit performs the handshake before its signed general message is sent; inbound application callbacks run only for verified general messages. For local side effects that require a known server, configure expectedServerIdentityKey and wait for a verified application event, not merely connect.

Remote endpoints must use https:// or wss://; cleartext is accepted only for exact loopback hosts. BRC-103 authenticates messages but does not encrypt payloads or Socket.IO metadata, so TLS remains mandatory in production. The optional expectedServerIdentityKey binds the connection to a known canonical BRC-103 server identity. When omitted, the client binds to the identity on the first verified application message for that connection. URL-authority and TLS verification overrides in managerOptions are rejected; use a trusted custom CA while retaining certificate verification for private PKI.

Authenticated event data preserves supported JSON exactly, including plain numeric-key objects under names such as data, payload, transaction, and tx. Real Uint8Array values are serialized as portable number arrays. Code that owns a typed payment or wallet protocol may recover a historical numeric-key byte object at that protocol's explicit byte field after receipt. Non-JSON values, negative zero, nested undefined, accessors, hidden/extra properties, sparse arrays, and serialization hooks are rejected before signing.

Failure isolation and resource limits

Authentication frames and application callbacks are contained inside the client connection. If a server sends a frame that fails BRC-103 processing, or an event callback throws or rejects, the client disconnects without creating an unhandled promise rejection. The optional onError(error, context) hook is also isolated if it throws or rejects, and its context does not include remote payloads or wallet material.

The client processes at most 32 authentication messages concurrently by default. Set maxPendingAuthMessages to a positive safe integer to choose a different bound; a server that exceeds it is disconnected.

Authenticated application frames default to a 1 MiB encoded limit. Set maxEventPayloadBytes to another positive safe integer if needed. Event names are bounded and cannot use Socket.IO lifecycle names or the internal _unknown sentinel. Malformed, oversized, or reserved-name frames are not dispatched.

How It Works (Briefly)

  • AuthSocketClient creates an internal BRC-103 Peer that handles:
    • Generating ephemeral nonces and signatures for each outbound message.
    • Verifying inbound messages from the server using the server’s public key.
  • A special 'authMessage' channel is used for the underlying BRC-103 handshake. You only interact with standard Socket.IO event names (like 'chatMessage'), as AuthSocketClient automatically re-dispatches them.

Detailed Explanations

SocketClientTransport

  • Implements the BRC-103 Transport interface on the client side.
  • Relies on the underlying socket.io-client for raw message passing via the 'authMessage' channel.
  • The BRC-103 Peer calls this transport to send and receive raw BRC-103 frames.
  • Rejected or synchronous authentication failures are contained before they can become unhandled rejections.

AuthSocketClient

  • A function that returns a proxy-like client socket.
  • Inside, it:
    1. Creates a real io(url, managerOptions) from socket.io-client.
    2. Attaches a SocketClientTransport.
    3. Creates a Peer with your wallet.
    4. Provides the final object with .on(eventName, callback) and .emit(eventName, data) methods.

Note: If you want to see a full end-to-end example, combine the server code from the authsocket README with the client code from the authsocket-client README, then run both. You should see messages securely exchanged and logs showing mutual authentication in action.

License

TS Stack first-party material is under the Open BSV License Version 6. The UMD bundle incorporates separately licensed SDK material; keep THIRD_PARTY_NOTICES.md and LICENSES/ with the bundle.

Development and distribution

The npm tarball contains browser-targeted ESM and CommonJS entry points, source maps, declarations for both module systems, and a UMD build. Pull requests and releases should run:

pnpm format:check
pnpm lint
pnpm typecheck
pnpm test:coverage
pnpm build
pnpm pack:check
pnpm test:browser

pack:check validates the exact npm tarball with publint, strict ESM and CommonJS type resolution, and clean consumer installations. test:browser verifies the packed package with Vite, esbuild, and the UMD artifact and enforces the repository's compressed-size budgets. The package uses the Open BSV License Version 6; the repository license controls ensure the manifest, included license, and packed artifact remain in sync.

SDK4 hotfix peer requirement

The SDK4 peer alternative starts at @bsv/sdk ^4.0.1, which restores numeric wallet results and legacy wire compatibility. Existing SDK2/3 alternatives and lower bounds remain unchanged. SDK4 users should install the 4.0.1 hotfix; no application-wallet wire or data migration is required.

版本与发布记录

@bsv/authsocket-client 共发布 27 个版本,首次发布于 2025/03/13;过去 12 个月发布了 14 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.1.10latest2026/10/2811 天前
2.1.92026/10/2415 天前
2.1.82026/10/031 个月前
2.1.72026/09/231 个月前
2.1.62026/08/282 个月前
2.1.52026/08/152 个月前

关于 @bsv/authsocket-client 的常见问题

@bsv/authsocket-client 是什么?

Mutually Authenticated Web Sockets Client

如何安装 @bsv/authsocket-client?

在项目目录运行 npm install @bsv/authsocket-client,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@bsv/sdk(^2.4.1 || ^3.0.0 || ^4.0.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bsv/authsocket-client 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bsv/authsocket-client 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@bsv/authsocket-client 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@bsv/authsocket-client 可以商用吗?

@bsv/authsocket-client 使用 SEE LICENSE IN LICENSE.txt 许可。许可条款写在包内的独立文件中,请阅读该文件确认可否商用。以上为概括,不构成法律意见,请以许可原文为准。

@bsv/authsocket-client 的下载量有多少?

根据 npm 官方下载统计,@bsv/authsocket-client 在 2026/10/09 – 2026/11/07 这 30 天内被下载 6,888 次,日均约 230 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bsv/authsocket-client 还在维护吗?

最新版本 2.1.10 发布于 2026/10/28(11 天前);至今共发布 27 个版本,其中过去 12 个月发布了 14 个;首次发布于 2025/03/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。