Skip to content

Commit 1d4e530

Browse files
authored
Merge pull request #175 from BitGo/BTC-3063.parse-convention
refactor: align wasm-solana with wasm-utxo parse conventions
2 parents ac49807 + 8b8ff2f commit 1d4e530

8 files changed

Lines changed: 22 additions & 88 deletions

File tree

packages/wasm-solana/js/explain.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* overall transaction type.
1010
*/
1111

12-
import { parseTransactionData } from "./parser.js";
12+
import { parseTransaction } from "./parser.js";
1313
import type { InstructionParams, ParsedTransaction } from "./parser.js";
1414

1515
// =============================================================================
@@ -238,7 +238,7 @@ export function explainTransaction(
238238
): ExplainedTransaction {
239239
const { lamportsPerSignature, tokenAccountRentExemptAmount } = options;
240240

241-
const parsed: ParsedTransaction = parseTransactionData(input);
241+
const parsed: ParsedTransaction = parseTransaction(input);
242242

243243
// --- Transaction ID ---
244244
const id = extractTransactionId(parsed.signatures);

packages/wasm-solana/js/index.ts

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,50 +21,11 @@ export { VersionedTransaction, isVersionedTransaction } from "./versioned.js";
2121
export type { AddressLookupTableData } from "./versioned.js";
2222

2323
// Top-level function exports
24-
export { parseTransactionData } from "./parser.js";
24+
export { parseTransaction } from "./parser.js";
2525
export { buildFromVersionedData } from "./builder.js";
2626
export { buildFromIntent, buildFromIntent as buildTransactionFromIntent } from "./intentBuilder.js";
2727
export { explainTransaction, TransactionType } from "./explain.js";
2828

29-
// Re-export Transaction import for parseTransaction
30-
import { Transaction as _Transaction } from "./transaction.js";
31-
32-
/**
33-
* Parse a Solana transaction from raw bytes.
34-
*
35-
* Returns a `Transaction` instance that can be both inspected and signed.
36-
* Use `.parse()` on the returned Transaction to get decoded instruction data.
37-
*
38-
* This is the single entry point for working with transactions — like
39-
* `BitGoPsbt.fromBytes()` in wasm-utxo.
40-
*
41-
* @param bytes - Raw transaction bytes
42-
* @returns A Transaction that can be inspected (`.parse()`) and signed (`.addSignature()`)
43-
*
44-
* @example
45-
* ```typescript
46-
* import { parseTransaction } from '@bitgo/wasm-solana';
47-
*
48-
* const tx = parseTransaction(txBytes);
49-
*
50-
* // Inspect
51-
* const parsed = tx.parse();
52-
* console.log(parsed.feePayer);
53-
* for (const instr of parsed.instructionsData) {
54-
* if (instr.type === 'Transfer') {
55-
* console.log(`${instr.amount} lamports to ${instr.toAddress}`);
56-
* }
57-
* }
58-
*
59-
* // Sign
60-
* tx.addSignature(pubkey, signature);
61-
* const signedBytes = tx.toBytes();
62-
* ```
63-
*/
64-
export function parseTransaction(bytes: Uint8Array): _Transaction {
65-
return _Transaction.fromBytes(bytes);
66-
}
67-
6829
// Intent builder type exports
6930
export type {
7031
BaseIntent,

packages/wasm-solana/js/parser.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,19 +269,21 @@ export interface ParsedTransaction {
269269
}
270270

271271
// =============================================================================
272-
// parseTransactionData function
272+
// parseTransaction function
273273
// =============================================================================
274274

275275
/**
276276
* Parse raw transaction bytes into a plain data object with decoded instructions.
277277
*
278-
* This is the low-level parsing function. Most callers should use the top-level
279-
* `parseTransaction(bytes)` which returns a `Transaction` instance with both
280-
* inspection (`.parse()`) and signing (`.addSignature()`) capabilities.
278+
* This is the main parsing function that returns structured data with all
279+
* instructions decoded into semantic types (Transfer, StakingActivate, etc.)
280+
* with amounts as bigint.
281+
*
282+
* For signing/serialization, use `Transaction.fromBytes()` instead.
281283
*
282284
* @param bytes - Raw transaction bytes
283285
* @returns A ParsedTransaction with all instructions decoded
284286
*/
285-
export function parseTransactionData(bytes: Uint8Array): ParsedTransaction {
287+
export function parseTransaction(bytes: Uint8Array): ParsedTransaction {
286288
return ParserNamespace.parse_transaction(bytes) as ParsedTransaction;
287289
}

packages/wasm-solana/js/transaction.ts

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { WasmTransaction } from "./wasm/wasm_solana.js";
22
import { Keypair } from "./keypair.js";
33
import { Pubkey } from "./pubkey.js";
4-
import { parseTransactionData } from "./parser.js";
5-
import type { ParsedTransaction } from "./parser.js";
64

75
/**
86
* Account metadata for an instruction
@@ -29,27 +27,25 @@ export interface Instruction {
2927
}
3028

3129
/**
32-
* Solana Transaction — the single object for inspecting and signing transactions.
30+
* Solana Transaction — deserialization wrapper for signing and serialization.
3331
*
34-
* Use `parseTransaction(bytes)` to create an instance. The returned Transaction
35-
* can be both inspected (`.parse()` for decoded instructions) and signed
36-
* (`.addSignature()`, `.signablePayload()`, `.toBytes()`).
32+
* Use `Transaction.fromBytes(bytes)` to create an instance for signing.
33+
* Use `parseTransaction(bytes)` from parser.ts to get decoded instruction data.
3734
*
3835
* @example
3936
* ```typescript
40-
* import { parseTransaction } from '@bitgo/wasm-solana';
37+
* import { Transaction, parseTransaction } from '@bitgo/wasm-solana';
4138
*
42-
* const tx = parseTransaction(txBytes);
43-
*
44-
* // Inspect decoded instructions
45-
* const parsed = tx.parse();
39+
* // Parse for decoded instructions
40+
* const parsed = parseTransaction(txBytes);
4641
* for (const instr of parsed.instructionsData) {
4742
* if (instr.type === 'Transfer') {
4843
* console.log(`${instr.amount} lamports to ${instr.toAddress}`);
4944
* }
5045
* }
5146
*
52-
* // Sign and serialize
47+
* // Deserialize for signing
48+
* const tx = Transaction.fromBytes(txBytes);
5349
* tx.addSignature(pubkey, signature);
5450
* const signedBytes = tx.toBytes();
5551
* ```
@@ -246,26 +242,6 @@ export class Transaction {
246242
this._wasm.sign_with_keypair(keypair.wasm);
247243
}
248244

249-
/**
250-
* Parse the transaction into decoded instruction data.
251-
*
252-
* Returns structured data with all instructions decoded into semantic types
253-
* (Transfer, StakeActivate, TokenTransfer, etc.) with amounts as bigint.
254-
*
255-
* @returns A ParsedTransaction with decoded instructions, feePayer, nonce, etc.
256-
*
257-
* @example
258-
* ```typescript
259-
* const tx = parseTransaction(txBytes);
260-
* const parsed = tx.parse();
261-
* console.log(parsed.feePayer);
262-
* console.log(parsed.instructionsData); // Decoded instruction types
263-
* ```
264-
*/
265-
parse(): ParsedTransaction {
266-
return parseTransactionData(this._wasm.to_bytes());
267-
}
268-
269245
/**
270246
* Get the underlying WASM instance (internal use only)
271247
* @internal

packages/wasm-solana/test/bitgojs-compat.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* what BitGoJS's Transaction.toJson() produces.
66
*/
77
import * as assert from "assert";
8-
import { parseTransactionData as parseTransaction } from "../js/parser.js";
8+
import { parseTransaction } from "../js/parser.js";
99

1010
// Helper to decode base64 in tests
1111
function base64ToBytes(base64: string): Uint8Array {

packages/wasm-solana/test/intentBuilder.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@
88
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument */
99

1010
import assert from "assert";
11-
import {
12-
buildFromIntent,
13-
Transaction,
14-
parseTransactionData as parseTransaction,
15-
} from "../dist/cjs/js/index.js";
11+
import { buildFromIntent, Transaction, parseTransaction } from "../dist/cjs/js/index.js";
1612

1713
describe("buildFromIntent", function () {
1814
// Common test params

packages/wasm-solana/test/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as assert from "assert";
2-
import { parseTransactionData as parseTransaction } from "../js/parser.js";
2+
import { parseTransaction } from "../js/parser.js";
33

44
// Helper to decode base64 in tests
55
function base64ToBytes(base64: string): Uint8Array {

packages/webui/src/wasm-solana/transaction/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,7 @@ class SolanaTransactionParser extends BaseComponent {
516516
try {
517517
// Parse the transaction
518518
const bytes = base64ToBytes(txData);
519-
const tx = parseTransaction(bytes);
520-
const parsed = tx.parse();
519+
const parsed = parseTransaction(bytes);
521520

522521
// Render transaction info
523522
txInfoEl.replaceChildren(this.renderTxInfo(parsed));

0 commit comments

Comments
 (0)