@bitgo/wasm-solana 1.4.1 → 1.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/cjs/js/builder.d.ts +499 -0
  2. package/dist/cjs/js/builder.js +113 -0
  3. package/dist/cjs/js/index.d.ts +16 -0
  4. package/dist/cjs/js/index.js +79 -0
  5. package/dist/cjs/js/keypair.d.ts +47 -0
  6. package/dist/cjs/js/keypair.js +69 -0
  7. package/dist/cjs/js/parser.d.ts +253 -0
  8. package/dist/cjs/js/parser.js +58 -0
  9. package/dist/cjs/js/pubkey.d.ts +54 -0
  10. package/dist/cjs/js/pubkey.js +76 -0
  11. package/dist/cjs/js/transaction.d.ts +156 -0
  12. package/dist/cjs/js/transaction.js +176 -0
  13. package/dist/cjs/js/versioned.d.ts +177 -0
  14. package/dist/cjs/js/versioned.js +197 -0
  15. package/dist/cjs/js/wasm/wasm_solana.d.ts +1030 -0
  16. package/dist/cjs/js/wasm/wasm_solana.js +6216 -0
  17. package/dist/cjs/js/wasm/wasm_solana_bg.wasm +0 -0
  18. package/dist/cjs/js/wasm/wasm_solana_bg.wasm.d.ts +341 -0
  19. package/dist/cjs/package.json +1 -0
  20. package/dist/esm/js/builder.d.ts +499 -0
  21. package/dist/esm/js/builder.js +109 -0
  22. package/dist/esm/js/index.d.ts +16 -0
  23. package/dist/esm/js/index.js +24 -0
  24. package/dist/esm/js/keypair.d.ts +47 -0
  25. package/dist/esm/js/keypair.js +65 -0
  26. package/dist/esm/js/parser.d.ts +253 -0
  27. package/dist/esm/js/parser.js +55 -0
  28. package/dist/esm/js/pubkey.d.ts +54 -0
  29. package/dist/esm/js/pubkey.js +72 -0
  30. package/dist/esm/js/transaction.d.ts +156 -0
  31. package/dist/esm/js/transaction.js +172 -0
  32. package/dist/esm/js/versioned.d.ts +177 -0
  33. package/dist/esm/js/versioned.js +192 -0
  34. package/dist/esm/js/wasm/wasm_solana.d.ts +1030 -0
  35. package/dist/esm/js/wasm/wasm_solana.js +4 -0
  36. package/dist/esm/js/wasm/wasm_solana_bg.js +6138 -0
  37. package/dist/esm/js/wasm/wasm_solana_bg.wasm +0 -0
  38. package/dist/esm/js/wasm/wasm_solana_bg.wasm.d.ts +341 -0
  39. package/package.json +1 -1
@@ -0,0 +1,172 @@
1
+ import { WasmTransaction } from "./wasm/wasm_solana.js";
2
+ import { Pubkey } from "./pubkey.js";
3
+ /**
4
+ * Solana Transaction wrapper for low-level deserialization and inspection.
5
+ *
6
+ * This class provides low-level access to transaction structure.
7
+ * For high-level semantic parsing with decoded instructions, use `parseTransaction()` instead.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { Transaction, parseTransaction } from '@bitgo/wasm-solana';
12
+ *
13
+ * // Low-level access:
14
+ * const tx = Transaction.fromBytes(txBytes);
15
+ * console.log(tx.feePayer);
16
+ *
17
+ * // High-level parsing (preferred):
18
+ * const parsed = parseTransaction(txBytes);
19
+ * console.log(parsed.instructionsData); // Decoded instruction types
20
+ * ```
21
+ */
22
+ export class Transaction {
23
+ _wasm;
24
+ constructor(_wasm) {
25
+ this._wasm = _wasm;
26
+ }
27
+ /**
28
+ * Deserialize a transaction from raw bytes
29
+ * @param bytes - The raw transaction bytes
30
+ * @returns A Transaction instance
31
+ */
32
+ static fromBytes(bytes) {
33
+ const wasm = WasmTransaction.from_bytes(bytes);
34
+ return new Transaction(wasm);
35
+ }
36
+ /**
37
+ * Create a Transaction from a WasmTransaction instance.
38
+ * @internal Used by builder functions
39
+ */
40
+ static fromWasm(wasm) {
41
+ return new Transaction(wasm);
42
+ }
43
+ /**
44
+ * Get the fee payer address as a base58 string
45
+ * Returns null if there are no account keys (shouldn't happen for valid transactions)
46
+ */
47
+ get feePayer() {
48
+ return this._wasm.fee_payer ?? null;
49
+ }
50
+ /**
51
+ * Get the recent blockhash as a base58 string
52
+ */
53
+ get recentBlockhash() {
54
+ return this._wasm.recent_blockhash;
55
+ }
56
+ /**
57
+ * Get the number of signatures in the transaction
58
+ */
59
+ get numSignatures() {
60
+ return this._wasm.num_signatures;
61
+ }
62
+ /**
63
+ * Get the signable message payload (what gets signed)
64
+ * This is the serialized message that signers sign
65
+ * @returns The message bytes
66
+ */
67
+ signablePayload() {
68
+ return this._wasm.signable_payload();
69
+ }
70
+ /**
71
+ * Serialize the message portion of the transaction.
72
+ * Alias for signablePayload() - provides compatibility with @solana/web3.js API.
73
+ * Returns a Buffer for compatibility with code expecting .toString('base64').
74
+ * @returns The serialized message bytes as a Buffer
75
+ */
76
+ serializeMessage() {
77
+ return Buffer.from(this.signablePayload());
78
+ }
79
+ /**
80
+ * Serialize the transaction to bytes
81
+ * @returns The serialized transaction bytes
82
+ */
83
+ toBytes() {
84
+ return this._wasm.to_bytes();
85
+ }
86
+ /**
87
+ * Get all account keys as Pubkey instances
88
+ * @returns Array of account public keys
89
+ */
90
+ accountKeys() {
91
+ const keys = Array.from(this._wasm.account_keys());
92
+ return keys.map((k) => Pubkey.fromBase58(k));
93
+ }
94
+ /**
95
+ * Get all signatures as byte arrays.
96
+ * Provides compatibility with @solana/web3.js Transaction.signatures API.
97
+ * @returns Array of signature byte arrays
98
+ */
99
+ get signatures() {
100
+ return Array.from(this._wasm.signatures());
101
+ }
102
+ /**
103
+ * Get all signatures as byte arrays (method form).
104
+ * Alias for the `signatures` property getter.
105
+ * @returns Array of signature byte arrays
106
+ */
107
+ getSignatures() {
108
+ return this.signatures;
109
+ }
110
+ /**
111
+ * Get all instructions in the transaction.
112
+ * Returns an array with programId, accounts, and data for each instruction.
113
+ *
114
+ * Note: This is a getter property to provide compatibility with code
115
+ * expecting @solana/web3.js Transaction.instructions API. If you need
116
+ * to call this as a method, use `getInstructions()` instead.
117
+ */
118
+ get instructions() {
119
+ const rawInstructions = this._wasm.instructions();
120
+ return Array.from(rawInstructions);
121
+ }
122
+ /**
123
+ * Get all instructions in the transaction (method form).
124
+ * Alias for the `instructions` property getter.
125
+ * @returns Array of instructions with programId, accounts, and data
126
+ */
127
+ getInstructions() {
128
+ return this.instructions;
129
+ }
130
+ /**
131
+ * Add a signature for a given public key.
132
+ *
133
+ * The pubkey must be one of the required signers in the transaction.
134
+ * The signature must be exactly 64 bytes (Ed25519 signature).
135
+ *
136
+ * @param pubkey - The public key as a base58 string
137
+ * @param signature - The 64-byte signature as Uint8Array
138
+ * @throws Error if pubkey is not a signer or signature is invalid
139
+ *
140
+ * @example
141
+ * ```typescript
142
+ * // Add a pre-computed signature (e.g., from TSS)
143
+ * tx.addSignature(signerPubkey, signatureBytes);
144
+ *
145
+ * // Serialize and broadcast
146
+ * const signedTxBytes = tx.toBytes();
147
+ * ```
148
+ */
149
+ addSignature(pubkey, signature) {
150
+ this._wasm.add_signature(pubkey, signature);
151
+ }
152
+ /**
153
+ * Get the signer index for a public key.
154
+ *
155
+ * Returns the index in the signatures array where this pubkey's
156
+ * signature should be placed, or null if the pubkey is not a signer.
157
+ *
158
+ * @param pubkey - The public key as a base58 string
159
+ * @returns The signer index, or null if not a signer
160
+ */
161
+ signerIndex(pubkey) {
162
+ const idx = this._wasm.signer_index(pubkey);
163
+ return idx ?? null;
164
+ }
165
+ /**
166
+ * Get the underlying WASM instance (internal use only)
167
+ * @internal
168
+ */
169
+ get wasm() {
170
+ return this._wasm;
171
+ }
172
+ }
@@ -0,0 +1,177 @@
1
+ /**
2
+ * Versioned Transaction Support
3
+ *
4
+ * Handles both legacy and versioned (MessageV0) Solana transactions.
5
+ * Versioned transactions use Address Lookup Tables (ALTs) to compress
6
+ * transaction size by referencing accounts via lookup table indices.
7
+ */
8
+ import { WasmVersionedTransaction } from "./wasm/wasm_solana.js";
9
+ import type { RawVersionedTransactionData } from "./builder.js";
10
+ /**
11
+ * Address Lookup Table data extracted from versioned transactions.
12
+ */
13
+ export interface AddressLookupTableData {
14
+ /** The lookup table account address (base58) */
15
+ accountKey: string;
16
+ /** Indices of writable accounts in the lookup table */
17
+ writableIndexes: Uint8Array;
18
+ /** Indices of readonly accounts in the lookup table */
19
+ readonlyIndexes: Uint8Array;
20
+ }
21
+ /**
22
+ * Account metadata for instructions.
23
+ */
24
+ export interface VersionedAccountMeta {
25
+ /** Account index within the transaction */
26
+ index: number;
27
+ /** Account pubkey (only present for static accounts) */
28
+ pubkey?: string;
29
+ /** Whether this account is from an Address Lookup Table */
30
+ isLookupTable: boolean;
31
+ /** Whether this account is a signer */
32
+ isSigner: boolean;
33
+ }
34
+ /**
35
+ * Instruction from a versioned transaction.
36
+ */
37
+ export interface VersionedInstruction {
38
+ /** Program ID */
39
+ programId: string;
40
+ /** Accounts used by the instruction */
41
+ accounts: VersionedAccountMeta[];
42
+ /** Instruction data */
43
+ data: Uint8Array;
44
+ }
45
+ /**
46
+ * Detect if transaction bytes represent a versioned transaction (MessageV0).
47
+ *
48
+ * @param bytes - Raw transaction bytes
49
+ * @returns true if versioned, false if legacy
50
+ */
51
+ export declare function isVersionedTransaction(bytes: Uint8Array): boolean;
52
+ /**
53
+ * Versioned Transaction class.
54
+ *
55
+ * Handles both legacy and versioned (MessageV0) transactions.
56
+ * Provides access to Address Lookup Table data for versioned transactions.
57
+ */
58
+ export declare class VersionedTransaction {
59
+ private inner;
60
+ private constructor();
61
+ /**
62
+ * Deserialize a transaction from raw bytes.
63
+ * Automatically handles both legacy and versioned formats.
64
+ */
65
+ static fromBytes(bytes: Uint8Array): VersionedTransaction;
66
+ /**
67
+ * Deserialize a transaction from base64 string.
68
+ */
69
+ static fromBase64(base64: string): VersionedTransaction;
70
+ /**
71
+ * Create a VersionedTransaction from a WasmVersionedTransaction instance.
72
+ * @internal Used by builder functions
73
+ */
74
+ static fromWasm(wasm: WasmVersionedTransaction): VersionedTransaction;
75
+ /**
76
+ * Create a versioned transaction from raw MessageV0 data.
77
+ *
78
+ * This is used for the `fromVersionedTransactionData()` path where we have
79
+ * pre-compiled versioned data (indexes + ALT refs). No instruction compilation
80
+ * is needed - this just constructs the transaction from the raw structure.
81
+ *
82
+ * @param data - Raw versioned transaction data
83
+ * @returns A VersionedTransaction instance
84
+ *
85
+ * @example
86
+ * ```typescript
87
+ * const tx = VersionedTransaction.fromVersionedData({
88
+ * staticAccountKeys: ['pubkey1', 'pubkey2', ...],
89
+ * addressLookupTables: [
90
+ * { accountKey: 'altPubkey', writableIndexes: [0, 1], readonlyIndexes: [2] }
91
+ * ],
92
+ * versionedInstructions: [
93
+ * { programIdIndex: 0, accountKeyIndexes: [1, 2], data: 'base58EncodedData' }
94
+ * ],
95
+ * messageHeader: {
96
+ * numRequiredSignatures: 1,
97
+ * numReadonlySignedAccounts: 0,
98
+ * numReadonlyUnsignedAccounts: 3
99
+ * },
100
+ * recentBlockhash: 'blockhash'
101
+ * });
102
+ * ```
103
+ */
104
+ static fromVersionedData(data: RawVersionedTransactionData): VersionedTransaction;
105
+ /**
106
+ * Check if this is a versioned transaction (MessageV0).
107
+ */
108
+ get isVersioned(): boolean;
109
+ /**
110
+ * Get the fee payer address.
111
+ */
112
+ get feePayer(): string | undefined;
113
+ /**
114
+ * Get the recent blockhash.
115
+ */
116
+ get recentBlockhash(): string;
117
+ /**
118
+ * Get the number of instructions.
119
+ */
120
+ get numInstructions(): number;
121
+ /**
122
+ * Get the number of signatures.
123
+ */
124
+ get numSignatures(): number;
125
+ /**
126
+ * Get the signable message payload.
127
+ */
128
+ signablePayload(): Uint8Array;
129
+ /**
130
+ * Serialize the message portion of the transaction.
131
+ * Alias for signablePayload() - provides compatibility with @solana/web3.js API.
132
+ * Returns a Buffer for compatibility with code expecting .toString('base64').
133
+ * @returns The serialized message bytes as a Buffer
134
+ */
135
+ serializeMessage(): Buffer;
136
+ /**
137
+ * Serialize the transaction to bytes.
138
+ */
139
+ toBytes(): Uint8Array;
140
+ /**
141
+ * Serialize the transaction to base64.
142
+ */
143
+ toBase64(): string;
144
+ /**
145
+ * Get static account keys (accounts stored directly in the message).
146
+ * For versioned transactions, additional accounts may be referenced via ALTs.
147
+ */
148
+ staticAccountKeys(): string[];
149
+ /**
150
+ * Get Address Lookup Table data.
151
+ * Returns empty array for legacy transactions.
152
+ */
153
+ addressLookupTables(): AddressLookupTableData[];
154
+ /**
155
+ * Get all signatures as byte arrays.
156
+ * Provides compatibility with @solana/web3.js Transaction.signatures API.
157
+ */
158
+ get signatures(): Uint8Array[];
159
+ /**
160
+ * Add a signature for a given public key.
161
+ *
162
+ * @param pubkey - The public key as base58 string
163
+ * @param signature - The 64-byte Ed25519 signature
164
+ */
165
+ addSignature(pubkey: string, signature: Uint8Array): void;
166
+ /**
167
+ * Get the signer index for a public key.
168
+ * Returns undefined if the pubkey is not a required signer.
169
+ */
170
+ signerIndex(pubkey: string): number | undefined;
171
+ /**
172
+ * Get all instructions.
173
+ * Note: For versioned transactions, account indices may reference
174
+ * accounts from Address Lookup Tables.
175
+ */
176
+ instructions(): VersionedInstruction[];
177
+ }
@@ -0,0 +1,192 @@
1
+ /**
2
+ * Versioned Transaction Support
3
+ *
4
+ * Handles both legacy and versioned (MessageV0) Solana transactions.
5
+ * Versioned transactions use Address Lookup Tables (ALTs) to compress
6
+ * transaction size by referencing accounts via lookup table indices.
7
+ */
8
+ import { WasmVersionedTransaction, is_versioned_transaction, BuilderNamespace, } from "./wasm/wasm_solana.js";
9
+ /**
10
+ * Detect if transaction bytes represent a versioned transaction (MessageV0).
11
+ *
12
+ * @param bytes - Raw transaction bytes
13
+ * @returns true if versioned, false if legacy
14
+ */
15
+ export function isVersionedTransaction(bytes) {
16
+ return is_versioned_transaction(bytes);
17
+ }
18
+ /**
19
+ * Versioned Transaction class.
20
+ *
21
+ * Handles both legacy and versioned (MessageV0) transactions.
22
+ * Provides access to Address Lookup Table data for versioned transactions.
23
+ */
24
+ export class VersionedTransaction {
25
+ inner;
26
+ constructor(inner) {
27
+ this.inner = inner;
28
+ }
29
+ /**
30
+ * Deserialize a transaction from raw bytes.
31
+ * Automatically handles both legacy and versioned formats.
32
+ */
33
+ static fromBytes(bytes) {
34
+ return new VersionedTransaction(WasmVersionedTransaction.from_bytes(bytes));
35
+ }
36
+ /**
37
+ * Deserialize a transaction from base64 string.
38
+ */
39
+ static fromBase64(base64) {
40
+ const bytes = Uint8Array.from(Buffer.from(base64, "base64"));
41
+ return VersionedTransaction.fromBytes(bytes);
42
+ }
43
+ /**
44
+ * Create a VersionedTransaction from a WasmVersionedTransaction instance.
45
+ * @internal Used by builder functions
46
+ */
47
+ static fromWasm(wasm) {
48
+ return new VersionedTransaction(wasm);
49
+ }
50
+ /**
51
+ * Create a versioned transaction from raw MessageV0 data.
52
+ *
53
+ * This is used for the `fromVersionedTransactionData()` path where we have
54
+ * pre-compiled versioned data (indexes + ALT refs). No instruction compilation
55
+ * is needed - this just constructs the transaction from the raw structure.
56
+ *
57
+ * @param data - Raw versioned transaction data
58
+ * @returns A VersionedTransaction instance
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * const tx = VersionedTransaction.fromVersionedData({
63
+ * staticAccountKeys: ['pubkey1', 'pubkey2', ...],
64
+ * addressLookupTables: [
65
+ * { accountKey: 'altPubkey', writableIndexes: [0, 1], readonlyIndexes: [2] }
66
+ * ],
67
+ * versionedInstructions: [
68
+ * { programIdIndex: 0, accountKeyIndexes: [1, 2], data: 'base58EncodedData' }
69
+ * ],
70
+ * messageHeader: {
71
+ * numRequiredSignatures: 1,
72
+ * numReadonlySignedAccounts: 0,
73
+ * numReadonlyUnsignedAccounts: 3
74
+ * },
75
+ * recentBlockhash: 'blockhash'
76
+ * });
77
+ * ```
78
+ */
79
+ static fromVersionedData(data) {
80
+ // Build the transaction using WASM and wrap in TypeScript class
81
+ const wasm = BuilderNamespace.build_from_versioned_data(data);
82
+ return VersionedTransaction.fromWasm(wasm);
83
+ }
84
+ /**
85
+ * Check if this is a versioned transaction (MessageV0).
86
+ */
87
+ get isVersioned() {
88
+ return this.inner.is_versioned;
89
+ }
90
+ /**
91
+ * Get the fee payer address.
92
+ */
93
+ get feePayer() {
94
+ return this.inner.fee_payer ?? undefined;
95
+ }
96
+ /**
97
+ * Get the recent blockhash.
98
+ */
99
+ get recentBlockhash() {
100
+ return this.inner.recent_blockhash;
101
+ }
102
+ /**
103
+ * Get the number of instructions.
104
+ */
105
+ get numInstructions() {
106
+ return this.inner.num_instructions;
107
+ }
108
+ /**
109
+ * Get the number of signatures.
110
+ */
111
+ get numSignatures() {
112
+ return this.inner.num_signatures;
113
+ }
114
+ /**
115
+ * Get the signable message payload.
116
+ */
117
+ signablePayload() {
118
+ return this.inner.signable_payload();
119
+ }
120
+ /**
121
+ * Serialize the message portion of the transaction.
122
+ * Alias for signablePayload() - provides compatibility with @solana/web3.js API.
123
+ * Returns a Buffer for compatibility with code expecting .toString('base64').
124
+ * @returns The serialized message bytes as a Buffer
125
+ */
126
+ serializeMessage() {
127
+ return Buffer.from(this.signablePayload());
128
+ }
129
+ /**
130
+ * Serialize the transaction to bytes.
131
+ */
132
+ toBytes() {
133
+ return this.inner.to_bytes();
134
+ }
135
+ /**
136
+ * Serialize the transaction to base64.
137
+ */
138
+ toBase64() {
139
+ return Buffer.from(this.toBytes()).toString("base64");
140
+ }
141
+ /**
142
+ * Get static account keys (accounts stored directly in the message).
143
+ * For versioned transactions, additional accounts may be referenced via ALTs.
144
+ */
145
+ staticAccountKeys() {
146
+ return Array.from(this.inner.static_account_keys());
147
+ }
148
+ /**
149
+ * Get Address Lookup Table data.
150
+ * Returns empty array for legacy transactions.
151
+ */
152
+ addressLookupTables() {
153
+ const alts = this.inner.address_lookup_tables();
154
+ return Array.from(alts).map((alt) => ({
155
+ accountKey: alt.accountKey,
156
+ writableIndexes: alt.writableIndexes,
157
+ readonlyIndexes: alt.readonlyIndexes,
158
+ }));
159
+ }
160
+ /**
161
+ * Get all signatures as byte arrays.
162
+ * Provides compatibility with @solana/web3.js Transaction.signatures API.
163
+ */
164
+ get signatures() {
165
+ return Array.from(this.inner.signatures());
166
+ }
167
+ /**
168
+ * Add a signature for a given public key.
169
+ *
170
+ * @param pubkey - The public key as base58 string
171
+ * @param signature - The 64-byte Ed25519 signature
172
+ */
173
+ addSignature(pubkey, signature) {
174
+ this.inner.add_signature(pubkey, signature);
175
+ }
176
+ /**
177
+ * Get the signer index for a public key.
178
+ * Returns undefined if the pubkey is not a required signer.
179
+ */
180
+ signerIndex(pubkey) {
181
+ return this.inner.signer_index(pubkey) ?? undefined;
182
+ }
183
+ /**
184
+ * Get all instructions.
185
+ * Note: For versioned transactions, account indices may reference
186
+ * accounts from Address Lookup Tables.
187
+ */
188
+ instructions() {
189
+ const instructions = this.inner.instructions();
190
+ return Array.from(instructions);
191
+ }
192
+ }