Skip to content

Commit d34cee0

Browse files
committed
refactor: follow wasm-utxo parse convention (PR #145 feedback)
remove hex methods, move parse() to standalone function, use get wasm() changes per otto's review: - remove Transaction.fromHex(), .toHex(), .toBroadcastFormat(), .callDataHex, .signablePayloadHex() - remove _context field and .parse() method from Transaction - rename parseTransactionData → parseTransaction (the actual parser) - rename getInner() → get wasm() (@internal) - remove top-level parseTransaction function (callers use DotTransaction.fromBytes directly) - move createParseContext to parser.ts (single source) - update tests: DotTransaction.fromBytes() for signing, parseTransaction() for decoded data - callers do their own base conversions (Uint8Array only on public API) separation of concerns: - Transaction.fromBytes(bytes) - deserialize for signing - parseTransaction(bytes, context?) - decode into structured data - explainTransaction(bytes, options) - high-level explanation BTC-0
1 parent 19a7ab6 commit d34cee0

7 files changed

Lines changed: 90 additions & 227 deletions

File tree

packages/wasm-dot/js/builder.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ import type { TransactionIntent, BuildContext } from "./types";
7474
*/
7575
export function buildTransaction(intent: TransactionIntent, context: BuildContext): DotTransaction {
7676
const inner = BuilderNamespace.buildTransaction(intent, context);
77-
const parseContext = { material: context.material, sender: context.sender };
78-
return DotTransaction.fromInner(inner as any, parseContext);
77+
return DotTransaction.fromInner(inner as any);
7978
}
8079

8180
// Re-export types for convenience

packages/wasm-dot/js/explain.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* the structured ExplainedTransaction that consumers expect.
77
*/
88

9-
import { parseTransactionData, type TransactionInput } from "./parser";
9+
import { parseTransaction, type TransactionInput } from "./parser";
1010
import type { Era, ParseContext, ParsedMethod } from "./types";
1111

1212
const MAX_NESTING_DEPTH = 10;
@@ -98,7 +98,7 @@ export function explainTransaction(
9898
input: TransactionInput,
9999
options?: { context?: ParseContext },
100100
): ExplainedTransaction {
101-
const parsed = parseTransactionData(input, options?.context);
101+
const parsed = parseTransaction(input, options?.context);
102102
const type_ = deriveTransactionType(parsed.method, 0);
103103
const outputs = extractOutputs(parsed.method, 0);
104104
const sender = parsed.sender ?? undefined;

packages/wasm-dot/js/index.ts

Lines changed: 3 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22
* wasm-dot: WASM bindings for Polkadot/DOT transaction operations
33
*
44
* This module provides:
5-
* - Transaction parsing: parseTransaction(bytes) → DotTransaction (inspect + sign)
5+
* - Transaction parsing: parseTransaction(bytes, context) → ParsedTransaction
66
* - Transaction explanation: explainTransaction(bytes, options) → ExplainedTransaction
77
* - Transaction building: buildTransaction(intent, context) → DotTransaction
8-
*
9-
* Pattern matches wasm-solana:
10-
* - parseTransaction(bytes) returns a Transaction object (not plain data)
11-
* - Transaction.parse() returns decoded instruction/method data
12-
* - explainTransaction() is a standalone function for high-level explanation
8+
* - Transaction signing: DotTransaction.fromBytes(bytes) → inspect + sign
139
*/
1410

1511
import {
@@ -25,65 +21,15 @@ import {
2521
export {
2622
WasmTransaction,
2723
ParserNamespace,
28-
ParserNamespace as DotParser,
2924
BuilderNamespace,
3025
MaterialJs,
3126
ValidityJs,
3227
ParseContextJs,
3328
};
3429

35-
// Re-export types
30+
// Re-export all public API
3631
export * from "./types";
3732
export * from "./transaction";
3833
export * from "./parser";
3934
export * from "./builder";
4035
export * from "./explain";
41-
42-
// =============================================================================
43-
// parseTransaction — Top-level entry point (returns DotTransaction)
44-
// =============================================================================
45-
46-
import { DotTransaction } from "./transaction";
47-
import type { ParseContext } from "./types";
48-
49-
/**
50-
* Parse a DOT transaction from bytes or hex, returning a DotTransaction
51-
* that can be both inspected and signed.
52-
*
53-
* This is the standard entry point for working with DOT transactions,
54-
* matching wasm-solana's `parseTransaction(bytes) → Transaction` pattern.
55-
*
56-
* Use `.parse()` for decoded method data, `.addSignature()` for signing,
57-
* `.toBytes()` for serialization.
58-
*
59-
* For low-level parsed data without a Transaction object, use
60-
* `parseTransactionData()` instead.
61-
*
62-
* @param input - Raw bytes or hex string (with or without 0x)
63-
* @param context - Parsing context with chain material
64-
* @returns DotTransaction instance
65-
*
66-
* @example
67-
* ```typescript
68-
* import { parseTransaction } from '@bitgo/wasm-dot';
69-
*
70-
* const tx = parseTransaction(txHex, { material });
71-
*
72-
* // Inspect decoded method data
73-
* const parsed = tx.parse();
74-
* console.log(parsed.method.pallet); // "balances"
75-
*
76-
* // Sign and serialize
77-
* tx.addSignature(signature, pubkey);
78-
* const signedBytes = tx.toBytes();
79-
* ```
80-
*/
81-
export function parseTransaction(
82-
input: Uint8Array | string,
83-
context?: ParseContext,
84-
): DotTransaction {
85-
if (typeof input === "string") {
86-
return DotTransaction.fromHex(input, context);
87-
}
88-
return DotTransaction.fromBytes(input, context);
89-
}

packages/wasm-dot/js/parser.ts

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
/**
2-
* Low-level transaction parsing.
2+
* Transaction parsing — standalone function that decodes extrinsic bytes
3+
* into structured data (pallet, method, args, nonce, etc.).
34
*
4-
* Provides `parseTransactionData()` — the low-level parser that returns
5-
* a plain `ParsedTransaction` object (pallet, method, args, nonce, etc.).
6-
*
7-
* For the high-level entry point that returns a `DotTransaction` (with
8-
* signing + `.parse()` methods), use `parseTransaction()` from index.ts.
5+
* This is separate from the DotTransaction class, which handles signing.
6+
* Use DotTransaction.fromBytes() when you need to sign.
7+
* Use parseTransaction() when you need decoded data.
98
*/
109

1110
import { ParserNamespace, MaterialJs, ParseContextJs } from "./wasm/wasm_dot";
@@ -18,44 +17,42 @@ import type { ParseContext, ParsedTransaction } from "./types";
1817
export type TransactionInput = Uint8Array | string | DotTransaction;
1918

2019
/**
21-
* Parse a DOT transaction into structured data (low-level).
20+
* Parse a DOT transaction into structured data.
2221
*
2322
* Returns a plain `ParsedTransaction` object with decoded pallet, method,
24-
* args, nonce, tip, era, etc. This is the raw decoded output — no type
25-
* derivation or output extraction.
23+
* args, nonce, tip, era, etc.
2624
*
27-
* For a `DotTransaction` object (with signing methods + `.parse()`),
28-
* use `parseTransaction()` instead.
25+
* For a signable `DotTransaction` object, use `DotTransaction.fromBytes()` instead.
2926
*
3027
* @param input - Raw bytes, hex string (with or without 0x), or DotTransaction
3128
* @param context - Parsing context with chain material (required for decoding)
3229
* @returns Parsed transaction data
3330
*
3431
* @example
3532
* ```typescript
36-
* import { parseTransactionData } from '@bitgo/wasm-dot';
33+
* import { parseTransaction } from '@bitgo/wasm-dot';
3734
*
38-
* const parsed = parseTransactionData(txHex, { material });
35+
* const parsed = parseTransaction(txBytes, { material });
3936
* console.log(parsed.method.pallet); // "balances"
4037
* console.log(parsed.method.name); // "transferKeepAlive"
4138
* ```
4239
*/
43-
export function parseTransactionData(
40+
export function parseTransaction(
4441
input: TransactionInput,
4542
context?: ParseContext,
4643
): ParsedTransaction {
4744
const ctx = context ? createParseContext(context) : undefined;
4845

49-
if (typeof input === "string") {
50-
return ParserNamespace.parseTransactionHex(input, ctx) as ParsedTransaction;
46+
if (input instanceof DotTransaction) {
47+
return ParserNamespace.parseTransaction(input.toBytes(), ctx) as ParsedTransaction;
5148
}
5249

53-
if (input instanceof DotTransaction) {
54-
const hex = input.toHex();
55-
return ParserNamespace.parseTransactionHex(hex, ctx) as ParsedTransaction;
50+
if (input instanceof Uint8Array) {
51+
return ParserNamespace.parseTransaction(input, ctx) as ParsedTransaction;
5652
}
5753

58-
return ParserNamespace.parseTransaction(input, ctx) as ParsedTransaction;
54+
// String input — let WASM handle hex parsing
55+
return ParserNamespace.parseTransactionHex(input, ctx) as ParsedTransaction;
5956
}
6057

6158
/**

0 commit comments

Comments
 (0)