You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(wasm-utxo): standardize on Rust snake_case and JavaScript camelCase
Update documentation and implementation to follow a consistent naming
convention where:
- Rust WASM bindings use snake_case (Rust convention)
- TypeScript wrappers convert to camelCase (JS convention)
Update BitGoPsbt class implementation to follow this pattern and align
method call sites with the new naming.
Issue: BTC-2652
Co-authored-by: llm-git <[email protected]>
Copy file name to clipboardExpand all lines: packages/wasm-utxo/src/wasm-bindgen.md
+22-15Lines changed: 22 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -66,20 +66,23 @@ pub use fixed_script_wallet::FixedScriptWalletNamespace;
66
66
67
67
4.**Methods**: All functions are static methods on the namespace struct
68
68
69
-
5.**Case**: Use `snake_case` for method names (Rust convention) - they'll be available as both `snake_case` and `camelCase`in JavaScript
69
+
5.**Case**: Use `snake_case` for method names (Rust convention) - TypeScript wrappers will provide `camelCase`versions
70
70
71
-
6.**Error Handling**: Return `Result<T, JsValue>` types - `wasm-bindgen` automatically converts these to JavaScript exceptions
71
+
6.**No `js_name` Attributes**: Do NOT use `#[wasm_bindgen(js_name = "...")]` to rename methods - keep Rust names pure and let TypeScript wrappers handle JS naming conventions
72
72
73
-
7.**Separation**: WASM binding layer delegates to core implementation in domain modules (e.g., `src/address/`, `src/fixed_script_wallet/`)
73
+
7.**Error Handling**: Return `Result<T, JsValue>` types - `wasm-bindgen` automatically converts these to JavaScript exceptions
74
+
75
+
8.**Separation**: WASM binding layer delegates to core implementation in domain modules (e.g., `src/address/`, `src/fixed_script_wallet/`)
74
76
75
77
### Generated TypeScript
76
78
77
-
The Rust namespace struct becomes a TypeScript class with static methods:
79
+
The Rust namespace struct becomes a TypeScript class with static methods (using `snake_case` from Rust):
78
80
79
81
```typescript
80
82
// wasm/wasm_utxo.d.ts (generated by wasm-bindgen)
@@ -91,31 +94,33 @@ export class AddressNamespace {
91
94
92
95
### TypeScript Wrapper Layer
93
96
94
-
The generated types have limitations (loose types like `any`, `string | null`). We wrap them with better TypeScript types in the `js/` directory.
97
+
The generated types have limitations (loose types like `any`, `string | null`, `snake_case` naming). We wrap them with better TypeScript types in the `js/` directory.
95
98
96
99
**See `../js/README.md` for the complete TypeScript wrapper pattern.**
97
100
98
101
The wrapper layer:
99
102
100
103
- Imports the generated namespace classes
101
104
- Defines precise TypeScript types (e.g., union types instead of `string`)
105
+
- Converts `snake_case` method names to `camelCase` (JavaScript convention)
102
106
- Exports wrapper functions with strong type signatures
103
107
- Provides better IDE support and compile-time type checking
104
108
105
109
### Example Flow
106
110
107
111
1.**Core Implementation**: Business logic in domain modules (e.g., `src/address/networks.rs`)
108
-
2.**WASM Bindings**: Define `AddressNamespace` struct with static methods in `src/wasm/address.rs` that calls into core implementation
112
+
2.**WASM Bindings**: Define `AddressNamespace` struct with `snake_case` methods in `src/wasm/address.rs` that calls into core implementation
109
113
3.**Module Export**: Export namespace from `src/wasm/mod.rs`
110
-
4.**wasm-bindgen Generation**: Generates `AddressNamespace` class in `wasm/wasm_utxo.d.ts`
111
-
5.**TypeScript Wrapper**: `js/address.ts` wraps it with precise types
114
+
4.**wasm-bindgen Generation**: Generates `AddressNamespace` class with `snake_case` methods in `wasm/wasm_utxo.d.ts`
115
+
5.**TypeScript Wrapper**: `js/address.ts` wraps it with precise types and `camelCase` naming
112
116
6.**Main Export**: `js/index.ts` exports it as `export * as address from "./address"`
113
117
114
118
This layered approach gives us:
115
119
116
120
- Clear separation between core implementation and WASM bindings
117
-
- Automatic WASM bindings generation
118
-
- Type-safe, well-documented TypeScript API
121
+
- Automatic WASM bindings generation with Rust conventions
122
+
- Type-safe, well-documented TypeScript API with JavaScript conventions
123
+
- Each layer remains idiomatic to its language
119
124
120
125
## Type Mapping
121
126
@@ -139,12 +144,14 @@ Common Rust ↔ JavaScript type mappings:
139
144
140
145
3.**Use descriptive namespace names** - Clear what domain they cover (e.g., `AddressNamespace`, `PsbtNamespace`)
141
146
142
-
4.**Thin binding layer** - WASM methods should delegate to core implementation, only handling type conversions
147
+
4.**Use `snake_case` naming only** - Never use `#[wasm_bindgen(js_name = "...")]` to convert to camelCase - let TypeScript wrappers handle naming conversion
148
+
149
+
5.**Thin binding layer** - WASM methods should delegate to core implementation, only handling type conversions
143
150
144
-
5.**Return `Result<T, JsValue>` types** - Let `wasm-bindgen` handle error conversion to JavaScript exceptions
151
+
6.**Return `Result<T, JsValue>` types** - Let `wasm-bindgen` handle error conversion to JavaScript exceptions
145
152
146
-
6.**Avoid complex types in signatures** - Stick to primitives and byte arrays when possible; use `JsValue` for complex types
153
+
7.**Avoid complex types in signatures** - Stick to primitives and byte arrays when possible; use `JsValue` for complex types
147
154
148
-
7.**Document with Rust doc comments** - They'll appear in the generated TypeScript
155
+
8.**Document with Rust doc comments** - They'll appear in the generated TypeScript
149
156
150
-
8.**Coordinate with TypeScript wrappers** - Keep the wrapper layer in mind when designing the Rust API
157
+
9.**Coordinate with TypeScript wrappers** - Keep the wrapper layer in mind when designing the Rust API
0 commit comments