|
| 1 | +# Purpose |
| 2 | + |
| 3 | +The primary purpose of this directory is to expose better TypeScript signatures than those |
| 4 | +generated by the `wasm-pack` command (which uses `wasm-bindgen`). |
| 5 | + |
| 6 | +While the `wasm-bindgen` crate allows some customization of the emitted type signatures, it |
| 7 | +is a bit painful to use and has certain limitations that cannot be easily worked around. |
| 8 | + |
| 9 | +## Architecture Pattern |
| 10 | + |
| 11 | +This directory implements a **namespace wrapper pattern** that provides a cleaner, more |
| 12 | +type-safe API over the raw WASM bindings. |
| 13 | + |
| 14 | +### Pattern Overview |
| 15 | + |
| 16 | +1. **WASM Generation** (`wasm/wasm_utxo.d.ts`) |
| 17 | + |
| 18 | + - Generated by `wasm-bindgen` from Rust code |
| 19 | + - Exports classes with static methods (e.g., `AddressNamespace`, `UtxolibCompatNamespace`) |
| 20 | + - Uses loose types (`any`, `string | null`) due to WASM-bindgen limitations |
| 21 | + |
| 22 | +2. **Namespace Wrapper Files** (e.g., `address.ts`, `utxolibCompat.ts`, `fixedScriptWallet.ts`) |
| 23 | + |
| 24 | + - Import the generated WASM namespace classes |
| 25 | + - Define precise TypeScript types to replace `any` types |
| 26 | + - Export individual functions that wrap the static WASM methods |
| 27 | + - Re-export related types for convenience |
| 28 | + |
| 29 | +3. **Shared Type Files** (e.g., `coinName.ts`, `triple.ts`) |
| 30 | + |
| 31 | + - Define common types used across multiple modules |
| 32 | + - Single source of truth to avoid duplication |
| 33 | + - Imported by wrapper files as needed |
| 34 | + |
| 35 | +4. **Main Entry Point** (`index.ts`) |
| 36 | + - Uses `export * as` to group related functionality into namespaces |
| 37 | + - Re-exports shared types for top-level access |
| 38 | + - Augments WASM types with additional TypeScript declarations |
| 39 | + |
| 40 | +### Example |
| 41 | + |
| 42 | +Given a WASM-generated class: |
| 43 | + |
| 44 | +```typescript |
| 45 | +// wasm/wasm_utxo.d.ts (generated) |
| 46 | +export class AddressNamespace { |
| 47 | + static to_output_script_with_coin(address: string, coin: string): Uint8Array; |
| 48 | + static from_output_script_with_coin( |
| 49 | + script: Uint8Array, |
| 50 | + coin: string, |
| 51 | + format?: string | null, |
| 52 | + ): string; |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +We create a wrapper module: |
| 57 | + |
| 58 | +```typescript |
| 59 | +// address.ts |
| 60 | +import { AddressNamespace } from "./wasm/wasm_utxo"; |
| 61 | +import type { CoinName } from "./coinName"; |
| 62 | + |
| 63 | +export type AddressFormat = "default" | "cashaddr"; |
| 64 | + |
| 65 | +export function toOutputScriptWithCoin(address: string, coin: CoinName): Uint8Array { |
| 66 | + return AddressNamespace.to_output_script_with_coin(address, coin); |
| 67 | +} |
| 68 | + |
| 69 | +export function fromOutputScriptWithCoin( |
| 70 | + script: Uint8Array, |
| 71 | + coin: CoinName, |
| 72 | + format?: AddressFormat, |
| 73 | +): string { |
| 74 | + return AddressNamespace.from_output_script_with_coin(script, coin, format); |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +And expose it via the main entry point: |
| 79 | + |
| 80 | +```typescript |
| 81 | +// index.ts |
| 82 | +export * as address from "./address"; |
| 83 | +``` |
| 84 | + |
| 85 | +### Benefits |
| 86 | + |
| 87 | +- **Type Safety**: Replace loose `any` and `string` types with precise union types |
| 88 | +- **Better DX**: IDE autocomplete works better with concrete types |
| 89 | +- **Maintainability**: Centralized type definitions prevent duplication |
0 commit comments