@anon-rpc/browser-harness

中文文档
0.4.0发布于 2026/10/28MITTypeScriptES Module近 30 天下载 3254

anon-rpc browser harness: loads a hash-pinned anon-client worker under null-origin iframe isolation and exposes an anonymized fetch (SPEC §3.1 conformance target).

原始使用文档
文档目录7 节
  1. Install
  2. Use
  3. Per-call options
  4. Collecting the worker's logs
  5. Strict Content Security Policies
  6. Notes
  7. License

@anon-rpc/browser-harness

A browser harness for anon-rpc — a standard that lets a wallet or application make anonymized RPC requests by running hash-pinned anon-client code inside a sandboxed worker.

Implements the anon-rpc specification version 0.4.0. (The package version is kept >= the implemented spec version; a package release without a spec change bumps past it.)

The harness:

  • resolves the anon-client bundle from an on-chain specifier contract — over https: or over KPS itself via kps: resolver entries (SPEC §4.1–4.2) — and verifies keccak256(bytes) == workerHash() before executing a single byte (trust the hash, not the URL);
  • runs it in a Web Worker inside a null-origin sandboxed iframe, with no ambient access to your DOM, storage, cookies, or keys;
  • grants it a small, explicit capability API — inbound fetch calls, a KPS key-pinned transport (bridged so the worker never touches WebRTC), persistent storage (IndexedDB on the host origin, namespaced per specifier), and logging;
  • hands you back one thing: an anonymized fetch.

Install

npm install @anon-rpc/browser-harness

Use

import { AnonRpcWorker } from "@anon-rpc/browser-harness";

const worker = new AnonRpcWorker({
  // The IWorkerSpecifier contract identifying the anon-client by hash.
  address: "0x…",
  // Optional: a plain value (JSON, plus undefined, Uint8Array, ArrayBuffer)
  // delivered to the worker exactly as `anonRpcWorker.config`. Opaque to the
  // harness; schema is the worker's. Anything else throws a TypeError here.
  config: { network: "mainnet" },
  // Bootstrap provider used only to read the specifier (breaks the circular
  // "need the chain to reach the chain" dependency).
  preExisting: { rpcProvider },
});

// Optional: Wait for the worker to report that it is ready. You can start
// making fetch calls before this - they'll just get buffered.
// await worker.ready;

// A standard fetch, routed through the sandboxed anon-client.
// It is this-bound, so passing it around as a free function is fine.
const res = await worker.fetch("https://rpc.example/", {
  method: "POST",
  body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "eth_blockNumber" }),
});

worker.close(); // tears down the iframe and worker

Per-call options

anonOptions in a fetch init is passed to the worker, not the destination (SPEC §9.3):

await worker.fetch(url, { method: "POST", body, anonOptions: { isolationKey: "account-1" } });

isolationKey asks that calls under different keys not be linkable to one another. Support varies — check what your network does with this key if it's important to you.

Collecting the worker's logs

A worker's §13 log calls go to the console unless you collect them. Pull them with acceptLog() (SPEC §5, §13.1) and they stop being echoed there:

for (;;) {
  // Resolves with the next entry; waits when there is none. Rejects once the
  // worker has failed or closed AND its retained entries are drained — so the
  // lines explaining a failure arrive before the rejection does.
  const { level, args } = await worker.acceptLog();
  render(level, args);
}

Entries are retained from the moment the worker starts, so a line logged during boot is still there when you first ask. The buffer is bounded (1000 entries) and drops the oldest beyond that; §13.1 makes this the one place a harness may lose a log call it has already accepted.

Strict Content Security Policies

By default the harness builds its null-origin iframe with srcdoc and an inline bootstrap script, which needs nothing from you. If your page's CSP omits 'unsafe-inline' for script-src, that bootstrap will not run — a srcdoc document inherits the embedder's policy, and a policy can only be tightened from within a document, never relaxed. The symptom is a ready that never settles, plus a CSP violation in the console.

Serve the harness's bootstrap page yourself and point iframeUrl (SPEC §5) at it:

new AnonRpcWorker({ address, preExisting, iframeUrl: "/anon-rpc-iframe.html" });

The page needs only the harness's own bootstrap script, which ships in this package as dist/iframe-boot.js:

<!doctype html><meta charset="utf-8"><script src="/anon-rpc-iframe.js"></script>

The harness still applies sandbox="allow-scripts" itself, so the document is placed at an opaque origin whichever route it came from, and §6 requires the URL to be same-origin with your page — a cross-origin bootstrap would hand the isolation boundary to a third party, so the harness rejects one.

On Firefox, allow data: in the bootstrap page's script-src wherever a policy applies there: Firefox refuses importScripts(blob:) in an opaque-origin worker, so the harness loads the verified bundle from a data: URL instead (it detects this; other browsers keep using blob:).

For MV3 browser extensions this is not optional and the page must additionally be declared in the manifest's sandbox.pages; @anon-rpc/browser-extension-harness packages all of that.

Notes

  • Browser-only: the isolation model is a null-origin iframe and the KPS transport runs over WebRTC. A native/Node harness would be a separate package.
  • The worker-facing capability API (anonRpcWorker) and all conformance requirements are defined in the specification. A template anon-client to copy lives in impl/passthrough-worker.
  • Status: prototype-grade reference implementation of a draft spec; interfaces track the spec and may change.

License

MIT © Ethereum Foundation

版本与发布记录

@anon-rpc/browser-harness 共发布 6 个版本,首次发布于 2026/07/06;过去 12 个月发布了 6 个版本。

版本发布日期距今
0.4.0latest2026/10/2810 天前
0.3.32026/10/1325 天前
0.3.22026/09/281 个月前
0.3.02026/07/273 个月前
0.2.12026/07/273 个月前
0.1.02026/07/064 个月前

关于 @anon-rpc/browser-harness 的常见问题

@anon-rpc/browser-harness 是什么?

anon-rpc browser harness: loads a hash-pinned anon-client worker under null-origin iframe isolation and exposes an anonymized fetch (SPEC §3.1 conformance target).

如何安装 @anon-rpc/browser-harness?

在项目目录运行 npm install @anon-rpc/browser-harness,或使用 pnpm add / yarn add / bun add。

@anon-rpc/browser-harness 支持 TypeScript 吗?

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

@anon-rpc/browser-harness 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@anon-rpc/browser-harness 可以商用吗?

@anon-rpc/browser-harness 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@anon-rpc/browser-harness 的下载量有多少?

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

@anon-rpc/browser-harness 还在维护吗?

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