Skip to content

Commit 3bff103

Browse files
committed
docs: add BitGoWasm API design conventions
document 7 architectural patterns enforced in code reviews: - prefer Uint8Array, avoid unnecessary base conversions - bigint for monetary amounts (conversions are caller's responsibility) - as const arrays for union types, not magic strings - builders return Transaction objects, not bytes - parsing separate from Transaction (standalone parseTransaction) - use wrapper classes over raw WASM bindings - consistent wrapper API (fromBytes, toBytes, getId, get wasm()) distilled from recurring review comments across wasm-utxo, wasm-solana, and wasm-dot. ref: PR #145 discussion.
1 parent f0a75ac commit 3bff103

1 file changed

Lines changed: 264 additions & 0 deletions

File tree

CONVENTIONS.md

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
# BitGoWasm Code Conventions
2+
3+
This file documents API design and architecture patterns from code reviews. Following these conventions prevents review churn and keeps the codebase consistent across wasm-utxo, wasm-solana, wasm-mps, and future packages.
4+
5+
These are hard rules, not suggestions. If you're unsure about a pattern, check the existing implementations in wasm-utxo.
6+
---
7+
8+
## 1. Prefer Uint8Array, avoid unnecessary base conversions
9+
10+
**What:** Generally, binary formats like transactions should use `Uint8Array` . Avoid base conversions in the wasm interface.
11+
12+
**Why:** Type safety at the boundary. If a method accepts or returns transaction bytes, it's always `Uint8Array`. String encodings (hex, base64) belong in serialization/API layers, not in the core transaction model. This prevents API bloat where we have to add encoding and decoding variants for various base conversion formats, as well as inefficiencies due to round-tripping binary data through two base conversions.
13+
14+
Exceptions are allowed for instances where a hex encoded representations are conventional (Ethereum addresses for instance)
15+
16+
**Good:**
17+
```typescript
18+
class Transaction {
19+
static fromBytes(bytes: Uint8Array): Transaction { ... }
20+
toBytes(): Uint8Array { ... }
21+
signablePayload(): Uint8Array { ... }
22+
}
23+
24+
// Encoding happens at the boundary
25+
const txBytes = Buffer.from(txHex, 'hex');
26+
const tx = Transaction.fromBytes(txBytes);
27+
```
28+
29+
**Bad:**
30+
```typescript
31+
// ❌ Don't accept/return hex strings on Transaction
32+
class Transaction {
33+
static fromHex(hex: string): Transaction { ... }
34+
toHex(): string { ... }
35+
}
36+
37+
// ❌ Don't mix encodings
38+
static fromBytes(bytes: Uint8Array | string): Transaction { ... }
39+
```
40+
41+
**See:** `packages/wasm-solana/js/transaction.ts`, `packages/wasm-utxo/js/transaction.ts`
42+
43+
---
44+
45+
## 2. bigint for amounts, never string
46+
47+
**What:** All monetary amounts, lamports, satoshis, token quantities, fees — use `bigint`. Never `number` or `string`.
48+
49+
**Why:**
50+
- `number` loses precision above 2^53 (unsafe for large amounts)
51+
- `string` delays type errors to runtime (no compile-time safety)
52+
- `bigint` is exact, type-safe, and enforces correctness at compile time
53+
54+
Conversions between external representations (API strings, JSON numbers) and `bigint` are the caller's responsibility, outside the `wasm-*` package boundary. The wasm package API accepts and returns `bigint` only — no `string` or `number` overloads for amounts.
55+
56+
**Good:**
57+
```typescript
58+
export interface ExplainedOutput {
59+
address: string;
60+
amount: bigint; //
61+
}
62+
63+
const fee = 5000n;
64+
const total = amount + fee; // Type-safe bigint arithmetic
65+
```
66+
67+
**Bad:**
68+
```typescript
69+
export interface ExplainedOutput {
70+
address: string;
71+
amount: string; // ❌ Runtime errors, no type safety
72+
}
73+
74+
const fee = "5000"; // ❌ Can't do arithmetic
75+
const total = parseInt(amount) + parseInt(fee); // ❌ Loses precision
76+
```
77+
78+
**See:** `packages/wasm-solana/js/explain.ts` (lines 40-43), `CLAUDE.md`
79+
80+
---
81+
82+
## 3. Const arrays for union types, not magic strings
83+
84+
**What:** Use `as const` arrays to define finite sets of known values. Never use bare string literals for types, opcodes, instruction names, etc.
85+
86+
**Why:**
87+
- Compile-time checking (typos caught at build time)
88+
- IDE autocomplete
89+
- Exhaustiveness checking in switch statements
90+
- Less repetitive than `enum` (no `Key = "Key"` duplication)
91+
92+
**Good:**
93+
```typescript
94+
export const TransactionType = ["Send", "StakingActivate", "StakingDeactivate"] as const;
95+
export type TransactionType = typeof TransactionType[number];
96+
97+
function handleTx(type: TransactionType) {
98+
switch (type) {
99+
case "Send":
100+
// ...
101+
case "StakingActivate":
102+
// ...
103+
// TypeScript warns if you miss a case
104+
}
105+
}
106+
```
107+
108+
**Bad:**
109+
```typescript
110+
// ❌ No type safety, typos not caught
111+
function handleTx(type: string) {
112+
if (type === "send") { // Oops, wrong case
113+
// ...
114+
}
115+
}
116+
117+
// ❌ Magic strings scattered everywhere
118+
const txType = "Send";
119+
```
120+
121+
**See:** `packages/wasm-solana/js/explain.ts` (lines 19-28)
122+
123+
---
124+
125+
## 4. Return Transaction objects, not bytes (builders)
126+
127+
**What:** Builder functions and transaction constructors return `Transaction` objects, not raw `Uint8Array`. The caller serializes when they need bytes.
128+
129+
**Why:** Transaction objects can be inspected and further modified (`.addSignature()`, `.signWithKeypair()`). Returning bytes forces the caller to re-parse if they need to inspect or modify.
130+
131+
**Good:**
132+
```typescript
133+
export function buildFromIntent(params: BuildParams): Transaction {
134+
const wasm = BuilderNamespace.build_from_intent(...);
135+
return Transaction.fromWasm(wasm);
136+
}
137+
138+
// Caller has full control
139+
const tx = buildFromIntent(intent);
140+
console.log(tx.feePayer); // Inspect
141+
tx.addSignature(pubkey, sig); // Modify
142+
const bytes = tx.toBytes(); // Serialize when ready
143+
```
144+
145+
**Bad:**
146+
```typescript
147+
// ❌ Forces caller to re-parse for inspection
148+
export function buildFromIntent(params: BuildParams): Uint8Array {
149+
const wasm = BuilderNamespace.build_from_intent(...);
150+
return wasm.to_bytes();
151+
}
152+
153+
const bytes = buildFromIntent(intent);
154+
const tx = Transaction.fromBytes(bytes); // Unnecessary round-trip
155+
```
156+
157+
**See:** `packages/wasm-solana/js/intentBuilder.ts`, `packages/wasm-solana/js/builder.ts`
158+
159+
---
160+
161+
## 5. Parsing separate from Transaction
162+
163+
**What:** Transaction deserialization (for signing) and transaction parsing (decoding instructions) are separate operations with separate entry points. `Transaction.fromBytes()` deserializes for signing. `parseTransaction()` is a standalone function that decodes instructions into structured data.
164+
165+
**Why:**
166+
- Separation of concerns: decoding is a protocol-level concept, parsing is a BitGo-level concept
167+
168+
**Good:**
169+
```typescript
170+
// For signing — deserialize only
171+
const tx = Transaction.fromBytes(txBytes);
172+
tx.addSignature(pubkey, signature);
173+
const signedBytes = tx.toBytes();
174+
175+
// For parsing — standalone function, takes bytes as argument
176+
const parsed = parseTransaction(txBytes, context);
177+
for (const instr of parsed.instructionsData) {
178+
if (instr.type === 'Transfer') {
179+
console.log(`${instr.amount} to ${instr.toAddress}`);
180+
}
181+
}
182+
```
183+
184+
**Bad:**
185+
```typescript
186+
// ❌ Transaction no longer has .parse() method
187+
const tx = Transaction.fromBytes(txBytes);
188+
const parsed = tx.parse(); // Doesn't exist
189+
190+
// ❌ Don't use parseTransaction result for signing
191+
const parsed = parseTransaction(txBytes);
192+
parsed.addSignature(pubkey, sig); // Wrong object type
193+
```
194+
195+
**See:** `packages/wasm-solana/js/parser.ts` (parseTransaction function), `packages/wasm-solana/js/transaction.ts` (Transaction.fromBytes), `packages/wasm-dot/js/parser.ts`
196+
197+
---
198+
199+
## 6. Use wrapper classes
200+
201+
**What:** Wrap WASM-generated types in TypeScript classes that provide better type signatures, `camelCase` naming, and encapsulation. Don't expose raw WASM bindings to consumers.
202+
203+
**Why:** `wasm-bindgen` emits loose types (`any`, `string | null`) and `snake_case` naming. Wrapper classes provide precise TypeScript types, idiomatic JS naming, and hide WASM implementation details. Two patterns exist: namespace wrappers for stateless utilities, class wrappers for stateful objects.
204+
205+
**See:** [`packages/wasm-utxo/js/README.md`](https://github.com/BitGo/BitGoWASM/blob/master/packages/wasm-utxo/js/README.md#purpose) for the full rationale and examples of both patterns.
206+
207+
---
208+
209+
## 7. Follow wasm-utxo conventions (get wasm(), fromBytes, toBytes, getId)
210+
211+
**What:** All wrapper classes follow the same API pattern:
212+
- `static fromBytes(bytes: Uint8Array)` — deserialize
213+
- `toBytes(): Uint8Array` — serialize
214+
- `getId(): string` — transaction ID / hash
215+
- `get wasm(): WasmType` (internal) — access underlying WASM instance
216+
217+
**Why:**
218+
- Consistency across packages (wasm-utxo, wasm-solana, wasm-mps all work the same way)
219+
- Predictable API for consumers
220+
- `get wasm()` allows package-internal code to access WASM without exposing it publicly
221+
222+
**Good:**
223+
```typescript
224+
export class Transaction {
225+
private constructor(private _wasm: WasmTransaction) {}
226+
227+
static fromBytes(bytes: Uint8Array): Transaction {
228+
return new Transaction(WasmTransaction.from_bytes(bytes));
229+
}
230+
231+
toBytes(): Uint8Array {
232+
return this._wasm.to_bytes();
233+
}
234+
235+
getId(): string {
236+
return this._wasm.id;
237+
}
238+
239+
/** @internal */
240+
get wasm(): WasmTransaction {
241+
return this._wasm;
242+
}
243+
}
244+
```
245+
246+
**Bad:**
247+
```typescript
248+
// ❌ Inconsistent naming
249+
export class Transaction {
250+
static parse(bytes: Uint8Array): Transaction { ... } // Should be fromBytes
251+
serialize(): Uint8Array { ... } // Should be toBytes
252+
getTransactionId(): string { ... } // Should be getId
253+
}
254+
```
255+
256+
**See:** `packages/wasm-utxo/js/transaction.ts`, `packages/wasm-solana/js/transaction.ts`
257+
258+
---
259+
260+
## Summary
261+
262+
These 7 conventions define how BitGoWasm packages structure their APIs. They're architectural patterns enforced in code reviews — not general software practices or build requirements.
263+
264+
When in doubt, look at wasm-solana and wasm-utxo — they're the reference implementations. Following these patterns from the start prevents review churn and keeps all packages consistent.

0 commit comments

Comments
 (0)