@elemental-zcash/zaddr_wasm_parser

中文文档
0.2.0发布于 2025/04/24MIT OR Apache-2.0TypeScriptES Module零依赖近 30 天下载 2230

WASM wrapper around the official Rust zcash_address library for parsing and validating Zcash Unified Addresses.

原始使用文档
文档目录22 节
  1. Installation and Build
  2. 1. Clone the repository:
  3. 2. Install dependencies:
  4. 3. Build the WASM module:
  5. TypeScript Wrapper
  6. Compile and run demo
  7. npm Usage
  8. Usage Example
  9. API Reference
  10. initWasm(): Promise<void>
  11. isZcashAddressValid(address: string): boolean
  12. getZcashAddressType(address: string): "p2pkh" | "p2sh" | "s…
  13. normalizeZcashAddress(address: string): string
  14. getAddressReceivers(address: string): AddressReceivers
  15. Fuzz Testing (Vitest)
  16. Rust Testing
  17. Node.js Integration Tests
  18. Browser Demo
  19. Why Use This?
  20. Project Structure
  21. License
  22. Maintainer

zaddr-wasm-parser

zaddr-wasm-parser is a lightweight, stateless address parser for Zcash, compiled to WebAssembly (WASM) for safe integration into front-end applications.

It provides a minimal and reliable API to validate, classify, and analyze Zcash addresses, including Unified Addresses (UA), without requiring access to private keys, wallet state, or blockchain nodes.

This module is built using the Rust library zcash_address.
Source code is located in the src/ directory.
WASM-integrated tests are located in the tests/ directory.


Installation and Build

To set up the project from a clean system:

1. Clone the repository:

cd ~/zcash-wasm
git clone https://github.com/ruzcash/zaddr-wasm-parser.git
cd zaddr-wasm-parser

2. Install dependencies:

Install the Rust WebAssembly toolchain:

brew install wasm-pack

(Optional) Install wasm-bindgen-cli to run browser tests:

cargo install wasm-bindgen-cli

(Optional) Install a simple local server for browser preview:

npm install -g live-server

3. Build the WASM module:

For Node.js (TypeScript tests and demo)

wasm-pack build --target bundler --out-dir pkg

For browser (HTML demo and tests)

wasm-pack build --target web --out-dir pkg

⚠️ These two targets are not compatible with each other. If you switch between browser and Node.js testing, recompile the WASM module accordingly.


TypeScript Wrapper

Compile and run demo

cd ts-wrapper
npm install
npm run build
npm run demo-dev   # Runs src/demo.ts with a mocked SDK
npm run demo       # Runs compiled dist/demo.js with real WASM

demo.ts dynamically switches between src/ and dist/ depending on the USE_SRC environment variable.


npm Usage

This package is published to npm under a forked repository: https://github.com/elemental-zcash/zaddr-wasm-parser with the package name @elemental-zcash/zaddr_wasm_parser.

import initWasm, {
  normalize_zcash_address,
  is_valid_zcash_address,
  get_zcash_address_type,
  get_address_receivers
} from "@elemental-zcash/zaddr_wasm_parser";

await initWasm();

const addr = "u1...";

console.log("Valid:", isZcashAddressValid(addr));
console.log("Type:", getZcashAddressType(addr));
console.log("Normalized:", normalizeZcashAddress(addr));
console.log("Receivers:", getAddressReceivers(addr));

Returned receiver structure:

  p2pkh: string | null;
  p2sh: string | null;
  sapling: string | null;
  orchard: string | null;
  tex: string | null;
};

Usage Example

import {
  initWasm,
  isZcashAddressValid,
  getZcashAddressType,
  normalizeZcashAddress,
  getAddressReceivers,
} from "zaddr-wasm-parser";


await initWasm();

const addr = "u1...";
console.log("Valid:", isZcashAddressValid(addr));
console.log("Type:", getZcashAddressType(addr));
console.log("Normalized:", normalizeZcashAddress(addr));
console.log("Receivers:", getAddressReceivers(addr));

API Reference

initWasm(): Promise<void>

Initializes the WebAssembly module. Must be called before using other functions.

isZcashAddressValid(address: string): boolean

Returns true if the given string is a valid Zcash address.

getZcashAddressType(address: string): "p2pkh" | "p2sh" | "sapling" | "unified" | "tex"

Returns the address type:

  • "p2pkh" — Transparent Pay-to-PubKey-Hash address
  • "p2sh" — Transparent Pay-to-Script-Hash address
  • "sapling" — Sapling shielded address
  • "unified" — Unified address
  • "tex" — TExt address (experimental)

normalizeZcashAddress(address: string): string

Returns a normalized version of the address string. Throws a JsError if the input is invalid.

getAddressReceivers(address: string): AddressReceivers

Decomposes a Zcash address into its internal receivers.
Throws a JsError if the input is invalid.

type AddressReceivers = {
  p2pkh: string | null;
  p2sh: string | null;
  sapling: string | null;
  orchard: string | null;
  tex: string | null;
};

Fuzz Testing (Vitest)

A comprehensive fuzz test is included to ensure the Zcash address parser handles edge cases correctly and safely.

This uses vitest to validate the parser’s behavior across malformed, truncated, and fully valid addresses. It covers:

  • Address validation
  • Type detection
  • Normalization
  • Receiver extraction

Supported address types include:

  • Transparent (p2pkh, p2sh)
  • Sapling
  • Unified Address (UA)
  • Orchard-only UA
  • TEX

All tests are located in:
ts-wrapper/fuzz.test.ts

Run it from the ts-wrapper directory:

cd ts-wrapper
npm run fuzz

Rust Testing

To run integration tests using Chrome:

wasm-pack test --chrome

All WASM-enabled Rust tests are located in the tests/ directory.
They cover transparent, shielded, Unified, and invalid Zcash address formats.

If you see errors related to WebAssembly.instantiateStreaming, make sure the module was built with --target web:

wasm-pack build --target web --out-dir pkg

Expected output:

test result: ok. 16 passed; 0 failed

Node.js Integration Tests

A plain JavaScript test file is included for manual validation via Node.js:

npx tsx test-integration/test.js

This script verifies multiple Zcash address types, including:

  • t1 — Transparent P2PKH
  • zs — Sapling
  • u1 — Unified Address
  • tex1 — TEX address (wrapped P2PKH with a different prefix)

It also covers malformed inputs (e.g. empty strings, emojis, and garbage).

Each test case prints results from the following methods:

  • is_valid_zcash_address
  • get_zcash_address_type
  • normalize_zcash_address
  • get_address_receivers

This is a quick way to confirm that the WASM module functions correctly in Node.js environments.


Browser Demo

A functional browser-based demo page is included in the repository:
zcash_address_parser_test.html

To view the demo locally:

npx live-server

Then open the following URL:

http://127.0.0.1:8080/zcash_address_parser_test.html

This HTML page allows interactive testing of Zcash address parsing and classification using the compiled WASM module.

Ensure the WASM module was compiled using --target web before using the browser demo.


Why Use This?

zaddr-wasm-parser is designed for use cases where full-node access is not practical — especially:

  • Browser-based applications
  • Exchanges migrating from transparent to Unified Addresses
  • Zcash-compatible tooling that needs quick validation

This library never touches private keys or network state. It's ideal for front-end environments, static analysis, or integration in Web SDKs.


Project Structure

zaddr-wasm-parser/
├── src/                        # Rust source code (lib.rs)
├── tests/                      # Rust-based tests for WASM
├── ts-wrapper/                # TypeScript interface and bindings
│   ├── src/
│   │   ├── index.ts
│   │   └── types.ts
│   ├── dist/
│   ├── demo.ts
│   ├── fuzz.test.ts
│   ├── package.json
│   └── __mocks__/
├── test-integration/          # Optional JS-only local test
│   └── test.js
├── zcash_address_parser_test.html  # Browser test page
├── Cargo.toml
├── README.md
├── .gitignore
├── LICENSE-MIT
├── LICENSE-APACHE

License

Dual-licensed under MIT or Apache-2.0.


Maintainer

版本与发布记录

@elemental-zcash/zaddr_wasm_parser 共发布 3 个版本,首次发布于 2025/04/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.2.0latest2025/04/241.6 年前
0.1.12025/04/171.6 年前
0.1.02025/04/171.6 年前

关于 @elemental-zcash/zaddr_wasm_parser 的常见问题

@elemental-zcash/zaddr_wasm_parser 是什么?

WASM wrapper around the official Rust zcash_address library for parsing and validating Zcash Unified Addresses.

如何安装 @elemental-zcash/zaddr_wasm_parser?

在项目目录运行 npm install @elemental-zcash/zaddr_wasm_parser,或使用 pnpm add / yarn add / bun add。

@elemental-zcash/zaddr_wasm_parser 支持 TypeScript 吗?

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

@elemental-zcash/zaddr_wasm_parser 支持 ESM 还是 CommonJS?

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

@elemental-zcash/zaddr_wasm_parser 可以商用吗?

@elemental-zcash/zaddr_wasm_parser 使用 MIT OR Apache-2.0 许可。可在 MIT、Apache-2.0 中任选其一遵守。选择 MIT 时,宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@elemental-zcash/zaddr_wasm_parser 的下载量有多少?

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

@elemental-zcash/zaddr_wasm_parser 还在维护吗?

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