@knirv/sdk

中文文档
0.2.0发布于 2026/11/01MITTypeScriptES Module零依赖

Browser and edge WebAssembly SDK core for KNIRV Network

原始使用文档
文档目录5 节
  1. Initialising the core
  2. Wallet protocol
  3. Signing and URIs (Rust-backed since 0.2.0)
  4. Breaking change from 0.1.x: initialise the core once
  5. Core WASM modules

@knirv/sdk

Browser and edge distribution of the portable KNIRV Rust core. The package ships the generated wasm-bindgen glue, declarations, and *.wasm binary.

import { SdkClient } from "@knirv/sdk";
const sdk = await SdkClient.init(); // uses the bundled inline fallback
const response = await sdk.sha256("knirv");
console.log(response.payload.digest);

For a CDN, bundler asset, or Cloudflare Worker module binding, avoid the inline copy and provide the asset explicitly:

import { SdkClient } from "@knirv/sdk/slim";
const sdk = await SdkClient.init(new URL("./knirv_sdk_core_bg.wasm", import.meta.url));

Run npm run build, then npm run pack:check before publishing. The latter must list wasm/knirv_sdk_core_bg.wasm.

Network operations (transaction.chain, gateway.health, transmission.*, wallet.account) run in the Rust core, which owns request construction, retries, failover and error mapping. The host only moves bytes: pass SdkClient.init(source, { http, config }) to supply your own { http_request(json): Promise<string>, sleep(ms): Promise<void> } (default: global fetch) and default client settings. SdkClient.create and BindingTransport were removed in favour of this single seam.

Initialising the core

The signing, URI, relay, model and frame functions all run in the Rust core, a WebAssembly module that must be instantiated once.

  • Browser main thread: await initLocal() once at start-up (browsers refuse to compile a module of this size synchronously). KNIRVARENA and the controller do it before rendering; the wallet module does it before it signs.
  • Node, web workers, most edge runtimes: nothing to do; the first call compiles the bundled module. Calling await initLocal() anyway is harmless and idempotent (KNIRV.COM's registration route does).
  • Cloudflare Workers: await initLocal(wasmModule) with a pre-compiled module.
  • SdkClient.init() and ModelsClient.advise() initialise it for you.

Wallet protocol

@knirv/sdk/wallet carries the message protocol of the KNIRV wallet extension: the request kinds, the success/failure/reject outcomes with their codes and messages (WalletMessageInfo), WalletResponse, and the KnirvWalletProvider interface the extension injects as window.knirv. The protocol is written once, in core/data/wallet_protocol.json; the build generates the TypeScript constants and the core serves the same data as the wallet.protocol operation.

Signing and URIs (Rust-backed since 0.2.0)

@knirv/sdk/signing and @knirv/sdk/uri are typed wrappers over the Rust core, which is the single implementation of signing, addresses, message/relay/WASM envelopes, knirv:// URIs and authority records. No cryptography is written in TypeScript, and the package has no JS crypto dependencies. The function names and types are unchanged from 0.1.x:

import { signMessageEnvelope, verifyMessage } from "@knirv/sdk/signing";

const signed = await signMessageEnvelope(privateKey, envelope);
await verifyMessage(signed, envelope.domain, envelope.purpose, envelope.chainId, envelope.nonce, new Date());

Breaking change from 0.1.x: initialise the core once

The core is a WebAssembly module, so it must be instantiated before the first call. What that takes depends on the platform:

PlatformWhat to do
Node, web workers, most edge runtimesNothing. The first call compiles the bundled module synchronously.
Browser main threadawait initLocal() once at start-up. Browsers refuse to compile a module this size synchronously on the main thread.
Cloudflare Workersimport wasm from "@knirv/sdk/wasm/knirv_sdk_core_bg.wasm"; await initLocal(wasm); Workers forbid compiling WASM from bytes, so pass the pre-compiled module.

initLocal is exported from @knirv/sdk/signing and @knirv/sdk/uri, is idempotent, and is shared with SdkClient.init(). A call made before the core can be instantiated throws LocalCoreNotInitializedError with these instructions.

Other differences: failures are BindingOperationErrors carrying the core's message (they are still Errors), verifyMessage accepts an envelope whose address field is empty (the address is derived from the public key), and the WASM binary is about 0.7 MB instead of 28 KB.

Core WASM modules

The three core modules are published as separate modules/*.wasm assets with a digest-pinned modules/manifest.json; they are not nested inside the SDK WASM runtime. After initialization, load a default asset or provide a CDN/Worker asset explicitly:

const { bytes, metadata } = await sdk.moduleBytes("crypto-core");
// or: await sdk.moduleBytes("crypto-core", myWorkerModuleBytes)

moduleBytes copies the bytes and verifies their SHA-256 against the published manifest before returning them. wasmModuleURL and wasmModulesManifest are also exported from @knirv/sdk/modules for hosts that manage loading directly.

版本与发布记录

@knirv/sdk 共发布 5 个版本,首次发布于 2026/08/22;过去 12 个月发布了 5 个版本。

版本发布日期距今
0.2.0latest2026/11/011 天前
0.1.32026/09/161 个月前
0.1.22026/08/252 个月前
0.1.12026/08/222 个月前
0.1.02026/08/222 个月前

关于 @knirv/sdk 的常见问题

@knirv/sdk 是什么?

浏览器和边缘WebAssembly SDK核心的KNIRV网络

如何安装 @knirv/sdk?

在项目目录运行 npm install @knirv/sdk,或使用 pnpm add / yarn add / bun add。

@knirv/sdk 支持 TypeScript 吗?

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

@knirv/sdk 支持 ESM 还是 CommonJS?

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

@knirv/sdk 需要什么版本的 Node.js?

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

@knirv/sdk 可以商用吗?

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

@knirv/sdk 还在维护吗?

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