@bitgo/wasm-utxo 1.43.0 → 2.0.0

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.
package/README.md CHANGED
@@ -31,6 +31,36 @@ Zcash support includes:
31
31
  - **Height-Based API**: Preferred `createEmpty()` method automatically selects correct consensus rules
32
32
  - **Parity Testing**: Validated against `zebra-chain` for accuracy across all network upgrades
33
33
 
34
+ ## Inspect Feature
35
+
36
+ The `inspect` feature adds PSBT and transaction parsing into hierarchical node trees,
37
+ useful for building tree-view UIs and CLI formatters.
38
+
39
+ It is behind a Cargo feature flag because it pulls in extra dependencies (`serde`, `serde_json`,
40
+ `num-bigint`, `hex`) that are not needed for core wallet operations.
41
+
42
+ ### Rust
43
+
44
+ ```rust
45
+ // Cargo.toml
46
+ wasm-utxo = { path = ".", features = ["inspect"] }
47
+
48
+ // Usage
49
+ use wasm_utxo::inspect::{parse_psbt_bytes_with_network, parse_tx_bytes_with_network, Node};
50
+ ```
51
+
52
+ ### TypeScript
53
+
54
+ Available as a separate import path, not included in the main `@bitgo/wasm-utxo` entry:
55
+
56
+ ```typescript
57
+ import { parsePsbtToNode, parseTxToNode, isInspectEnabled } from "@bitgo/wasm-utxo/inspect";
58
+ ```
59
+
60
+ The published npm package includes stub implementations that return `isInspectEnabled() === false`
61
+ and throw runtime errors from the parse functions. To get a working build, compile the WASM with
62
+ `--features inspect` (see [`packages/webui/scripts/build-wasm.sh`](../webui/scripts/build-wasm.sh)).
63
+
34
64
  ## Building
35
65
 
36
66
  ### Mac
@@ -1,5 +1,5 @@
1
1
  import { BitGoPsbt as WasmBitGoPsbt, type PsbtInputData, type PsbtOutputData, type PsbtOutputDataWithAddress } from "../wasm/wasm_utxo.js";
2
- import type { IPsbtIntrospectionWithAddress } from "../psbt.js";
2
+ import type { IPsbtWithAddress } from "../psbt.js";
3
3
  import { type WalletKeysArg } from "./RootWalletKeys.js";
4
4
  import { type ReplayProtectionArg } from "./ReplayProtection.js";
5
5
  import { type BIP32Arg } from "../bip32.js";
@@ -98,7 +98,7 @@ export type ParseTransactionOptions = {
98
98
  export type ParseOutputsOptions = {
99
99
  payGoPubkeys?: ECPairArg[];
100
100
  };
101
- export declare class BitGoPsbt implements IPsbtIntrospectionWithAddress {
101
+ export declare class BitGoPsbt implements IPsbtWithAddress {
102
102
  protected _wasm: WasmBitGoPsbt;
103
103
  protected constructor(_wasm: WasmBitGoPsbt);
104
104
  /**
@@ -155,6 +155,8 @@ export declare class BitGoPsbt implements IPsbtIntrospectionWithAddress {
155
155
  * }, outputScript);
156
156
  * ```
157
157
  */
158
+ addInputAtIndex(index: number, txid: string, vout: number, value: bigint, script: Uint8Array, sequence?: number): number;
159
+ addInputAtIndex(index: number, options: AddInputOptions, script: Uint8Array): number;
158
160
  addInput(options: AddInputOptions, script: Uint8Array): number;
159
161
  /**
160
162
  * Add an output to the PSBT
@@ -168,41 +170,11 @@ export declare class BitGoPsbt implements IPsbtIntrospectionWithAddress {
168
170
  * const outputIndex = psbt.addOutput(outputScript, 50000n);
169
171
  * ```
170
172
  */
173
+ addOutputAtIndex(index: number, script: Uint8Array, value: bigint): number;
174
+ addOutputAtIndex(index: number, address: string, value: bigint): number;
175
+ addOutputAtIndex(index: number, options: AddOutputOptions): number;
171
176
  addOutput(script: Uint8Array, value: bigint): number;
172
- /**
173
- * Add an output to the PSBT by address
174
- *
175
- * @param address - The destination address
176
- * @param value - Value in satoshis
177
- * @returns The index of the newly added output
178
- *
179
- * @example
180
- * ```typescript
181
- * const outputIndex = psbt.addOutput("bc1q...", 50000n);
182
- * ```
183
- */
184
177
  addOutput(address: string, value: bigint): number;
185
- /**
186
- * Add an output to the PSBT
187
- *
188
- * @param options - Output options (script or address, and value)
189
- * @returns The index of the newly added output
190
- *
191
- * @example
192
- * ```typescript
193
- * // Using script
194
- * const outputIndex = psbt.addOutput({
195
- * script: outputScript,
196
- * value: 50000n,
197
- * });
198
- *
199
- * // Using address
200
- * const outputIndex = psbt.addOutput({
201
- * address: "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4",
202
- * value: 50000n,
203
- * });
204
- * ```
205
- */
206
178
  addOutput(options: AddOutputOptions): number;
207
179
  /**
208
180
  * Add a wallet input with full PSBT metadata
@@ -243,6 +215,7 @@ export declare class BitGoPsbt implements IPsbtIntrospectionWithAddress {
243
215
  * );
244
216
  * ```
245
217
  */
218
+ addWalletInputAtIndex(index: number, inputOptions: AddInputOptions, walletKeys: WalletKeysArg, walletOptions: AddWalletInputOptions): number;
246
219
  addWalletInput(inputOptions: AddInputOptions, walletKeys: WalletKeysArg, walletOptions: AddWalletInputOptions): number;
247
220
  /**
248
221
  * Add a wallet output with full PSBT metadata
@@ -274,6 +247,7 @@ export declare class BitGoPsbt implements IPsbtIntrospectionWithAddress {
274
247
  * });
275
248
  * ```
276
249
  */
250
+ addWalletOutputAtIndex(index: number, walletKeys: WalletKeysArg, options: AddWalletOutputOptions): number;
277
251
  addWalletOutput(walletKeys: WalletKeysArg, options: AddWalletOutputOptions): number;
278
252
  /**
279
253
  * Add a replay protection input to the PSBT
@@ -294,22 +268,21 @@ export declare class BitGoPsbt implements IPsbtIntrospectionWithAddress {
294
268
  * );
295
269
  * ```
296
270
  */
271
+ addReplayProtectionInputAtIndex(index: number, inputOptions: AddInputOptions, key: ECPairArg): number;
297
272
  addReplayProtectionInput(inputOptions: AddInputOptions, key: ECPairArg): number;
273
+ removeInput(index: number): void;
274
+ removeOutput(index: number): void;
298
275
  /**
299
276
  * Get the unsigned transaction ID
300
277
  * @returns The unsigned transaction ID
301
278
  */
302
- unsignedTxid(): string;
279
+ unsignedTxId(): string;
303
280
  /**
304
281
  * Get the transaction version
305
282
  * @returns The transaction version number
306
283
  */
307
- get version(): number;
308
- /**
309
- * Get the transaction lock time
310
- * @returns The transaction lock time
311
- */
312
- get lockTime(): number;
284
+ version(): number;
285
+ lockTime(): number;
313
286
  /**
314
287
  * Parse transaction with wallet keys to identify wallet inputs/outputs
315
288
  * @param walletKeys - The wallet keys to use for identification
@@ -584,12 +557,8 @@ export declare class BitGoPsbt implements IPsbtIntrospectionWithAddress {
584
557
  * Get the number of inputs in the PSBT
585
558
  * @returns The number of inputs
586
559
  */
587
- get inputCount(): number;
588
- /**
589
- * Get the number of outputs in the PSBT
590
- * @returns The number of outputs
591
- */
592
- get outputCount(): number;
560
+ inputCount(): number;
561
+ outputCount(): number;
593
562
  /**
594
563
  * Get all PSBT inputs as an array
595
564
  *
@@ -56,28 +56,35 @@ class BitGoPsbt {
56
56
  const wasm = wasm_utxo_js_1.BitGoPsbt.from_bytes(bytes, network);
57
57
  return new BitGoPsbt(wasm);
58
58
  }
59
- /**
60
- * Add an input to the PSBT
61
- *
62
- * This adds a transaction input and corresponding PSBT input metadata.
63
- * The witness_utxo is automatically populated for modern signing compatibility.
64
- *
65
- * @param options - Input options (txid, vout, value, sequence)
66
- * @param script - Output script of the UTXO being spent
67
- * @returns The index of the newly added input
68
- *
69
- * @example
70
- * ```typescript
71
- * const inputIndex = psbt.addInput({
72
- * txid: "abc123...",
73
- * vout: 0,
74
- * value: 100000n,
75
- * }, outputScript);
76
- * ```
77
- */
59
+ addInputAtIndex(index, txidOrOptions, voutOrScript, value, script, sequence) {
60
+ if (typeof txidOrOptions === "string") {
61
+ return this._wasm.add_input_at_index(index, txidOrOptions, voutOrScript, value, script, sequence);
62
+ }
63
+ const options = txidOrOptions;
64
+ return this._wasm.add_input_at_index(index, options.txid, options.vout, options.value, voutOrScript, options.sequence, options.prevTx);
65
+ }
78
66
  addInput(options, script) {
79
67
  return this._wasm.add_input(options.txid, options.vout, options.value, script, options.sequence, options.prevTx);
80
68
  }
69
+ addOutputAtIndex(index, scriptOrOptions, value) {
70
+ if (scriptOrOptions instanceof Uint8Array || typeof scriptOrOptions === "string") {
71
+ if (value === undefined) {
72
+ throw new Error("Value is required when passing a script or address");
73
+ }
74
+ if (scriptOrOptions instanceof Uint8Array) {
75
+ return this._wasm.add_output_at_index(index, scriptOrOptions, value);
76
+ }
77
+ return this._wasm.add_output_with_address_at_index(index, scriptOrOptions, value);
78
+ }
79
+ const options = scriptOrOptions;
80
+ if ("script" in options) {
81
+ return this._wasm.add_output_at_index(index, options.script, options.value);
82
+ }
83
+ if ("address" in options) {
84
+ return this._wasm.add_output_with_address_at_index(index, options.address, options.value);
85
+ }
86
+ throw new Error("Invalid output options");
87
+ }
81
88
  addOutput(scriptOrOptions, value) {
82
89
  if (scriptOrOptions instanceof Uint8Array || typeof scriptOrOptions === "string") {
83
90
  if (value === undefined) {
@@ -136,6 +143,10 @@ class BitGoPsbt {
136
143
  * );
137
144
  * ```
138
145
  */
146
+ addWalletInputAtIndex(index, inputOptions, walletKeys, walletOptions) {
147
+ const keys = RootWalletKeys_js_1.RootWalletKeys.from(walletKeys);
148
+ return this._wasm.add_wallet_input_at_index(index, inputOptions.txid, inputOptions.vout, inputOptions.value, keys.wasm, walletOptions.scriptId.chain, walletOptions.scriptId.index, walletOptions.signPath?.signer, walletOptions.signPath?.cosigner, inputOptions.sequence, inputOptions.prevTx);
149
+ }
139
150
  addWalletInput(inputOptions, walletKeys, walletOptions) {
140
151
  const keys = RootWalletKeys_js_1.RootWalletKeys.from(walletKeys);
141
152
  return this._wasm.add_wallet_input(inputOptions.txid, inputOptions.vout, inputOptions.value, keys.wasm, walletOptions.scriptId.chain, walletOptions.scriptId.index, walletOptions.signPath?.signer, walletOptions.signPath?.cosigner, inputOptions.sequence, inputOptions.prevTx);
@@ -170,6 +181,10 @@ class BitGoPsbt {
170
181
  * });
171
182
  * ```
172
183
  */
184
+ addWalletOutputAtIndex(index, walletKeys, options) {
185
+ const keys = RootWalletKeys_js_1.RootWalletKeys.from(walletKeys);
186
+ return this._wasm.add_wallet_output_at_index(index, options.chain, options.index, options.value, keys.wasm);
187
+ }
173
188
  addWalletOutput(walletKeys, options) {
174
189
  const keys = RootWalletKeys_js_1.RootWalletKeys.from(walletKeys);
175
190
  return this._wasm.add_wallet_output(options.chain, options.index, options.value, keys.wasm);
@@ -193,29 +208,35 @@ class BitGoPsbt {
193
208
  * );
194
209
  * ```
195
210
  */
211
+ addReplayProtectionInputAtIndex(index, inputOptions, key) {
212
+ const ecpair = ecpair_js_1.ECPair.from(key);
213
+ return this._wasm.add_replay_protection_input_at_index(index, ecpair.wasm, inputOptions.txid, inputOptions.vout, inputOptions.value, inputOptions.sequence, inputOptions.prevTx);
214
+ }
196
215
  addReplayProtectionInput(inputOptions, key) {
197
216
  const ecpair = ecpair_js_1.ECPair.from(key);
198
217
  return this._wasm.add_replay_protection_input(ecpair.wasm, inputOptions.txid, inputOptions.vout, inputOptions.value, inputOptions.sequence, inputOptions.prevTx);
199
218
  }
219
+ removeInput(index) {
220
+ this._wasm.remove_input(index);
221
+ }
222
+ removeOutput(index) {
223
+ this._wasm.remove_output(index);
224
+ }
200
225
  /**
201
226
  * Get the unsigned transaction ID
202
227
  * @returns The unsigned transaction ID
203
228
  */
204
- unsignedTxid() {
229
+ unsignedTxId() {
205
230
  return this._wasm.unsigned_txid();
206
231
  }
207
232
  /**
208
233
  * Get the transaction version
209
234
  * @returns The transaction version number
210
235
  */
211
- get version() {
236
+ version() {
212
237
  return this._wasm.version();
213
238
  }
214
- /**
215
- * Get the transaction lock time
216
- * @returns The transaction lock time
217
- */
218
- get lockTime() {
239
+ lockTime() {
219
240
  return this._wasm.lock_time();
220
241
  }
221
242
  /**
@@ -541,14 +562,10 @@ class BitGoPsbt {
541
562
  * Get the number of inputs in the PSBT
542
563
  * @returns The number of inputs
543
564
  */
544
- get inputCount() {
565
+ inputCount() {
545
566
  return this._wasm.input_count();
546
567
  }
547
- /**
548
- * Get the number of outputs in the PSBT
549
- * @returns The number of outputs
550
- */
551
- get outputCount() {
568
+ outputCount() {
552
569
  return this._wasm.output_count();
553
570
  }
554
571
  /**
@@ -82,6 +82,10 @@ declare module "./wasm/wasm_utxo.js" {
82
82
  validateSignatureAtInput(inputIndex: number, pubkey: Uint8Array): boolean;
83
83
  verifySignatureWithKey(inputIndex: number, key: WasmBIP32): boolean;
84
84
  extractTransaction(): WasmTransaction;
85
+ addInputAtIndex(index: number, txid: string, vout: number, value: bigint, script: Uint8Array, sequence?: number): number;
86
+ addOutputAtIndex(index: number, script: Uint8Array, value: bigint): number;
87
+ removeInput(index: number): void;
88
+ removeOutput(index: number): void;
85
89
  unsignedTxId(): string;
86
90
  lockTime(): number;
87
91
  version(): number;
@@ -91,4 +95,4 @@ export { WrapDescriptor as Descriptor } from "./wasm/wasm_utxo.js";
91
95
  export { WrapMiniscript as Miniscript } from "./wasm/wasm_utxo.js";
92
96
  export { WrapPsbt as Psbt } from "./wasm/wasm_utxo.js";
93
97
  export { DashTransaction, Transaction, ZcashTransaction } from "./transaction.js";
94
- export { hasPsbtMagic, type IPsbtIntrospection, type IPsbtIntrospectionWithAddress, } from "./psbt.js";
98
+ export { hasPsbtMagic, type IPsbt, type IPsbtWithAddress } from "./psbt.js";
@@ -0,0 +1,105 @@
1
+ /**
2
+ * Inspect - TypeScript bindings for PSBT and Transaction parsing
3
+ *
4
+ * Provides typed wrappers around the WASM inspect functions that return
5
+ * hierarchical node structures suitable for display as collapsible trees.
6
+ *
7
+ * Import via: `import { ... } from "@bitgo/wasm-utxo/inspect"`
8
+ */
9
+ import type { CoinName } from "../coinName.js";
10
+ /** Re-export CoinName for convenience */
11
+ export type { CoinName };
12
+ /** All supported networks in order of parsing priority */
13
+ export declare const allNetworks: CoinName[];
14
+ /**
15
+ * Primitive value types that can appear in a Node.
16
+ * Buffer values are hex-encoded strings, Integer is a decimal string for BigInt support.
17
+ */
18
+ export type PrimitiveType = "String" | "Buffer" | "Integer" | "U8" | "U16" | "U32" | "U64" | "I8" | "I16" | "I32" | "I64" | "Boolean" | "None";
19
+ /**
20
+ * A tagged union representing primitive values in the parse tree.
21
+ */
22
+ export interface Primitive {
23
+ type: PrimitiveType;
24
+ value?: string | number | boolean;
25
+ }
26
+ /**
27
+ * A node in the parse tree representing a PSBT or transaction element.
28
+ */
29
+ export interface Node {
30
+ label: string;
31
+ value: Primitive;
32
+ children: Node[];
33
+ }
34
+ /**
35
+ * Parse a PSBT and return a typed node tree.
36
+ *
37
+ * @param psbtBytes - The raw PSBT bytes
38
+ * @param network - The network coin name (e.g., "btc", "ltc", "bch")
39
+ * @returns A Node tree representing the parsed PSBT structure
40
+ * @throws If the PSBT bytes are invalid or network is unknown
41
+ */
42
+ export declare function parsePsbtToNode(psbtBytes: Uint8Array, network: CoinName): Node;
43
+ /**
44
+ * Parse a transaction and return a typed node tree.
45
+ *
46
+ * @param txBytes - The raw transaction bytes
47
+ * @param network - The network coin name (e.g., "btc", "ltc", "bch")
48
+ * @returns A Node tree representing the parsed transaction structure
49
+ * @throws If the transaction bytes are invalid or network is unknown
50
+ */
51
+ export declare function parseTxToNode(txBytes: Uint8Array, network: CoinName): Node;
52
+ /**
53
+ * Try to parse a PSBT with all networks and return the first one that succeeds.
54
+ *
55
+ * @param psbtBytes - The raw PSBT bytes
56
+ * @param networks - Optional list of networks to try (defaults to all networks)
57
+ * @returns An object with the parsed Node and detected network, or null if all fail
58
+ */
59
+ export declare function tryParsePsbt(psbtBytes: Uint8Array, networks?: CoinName[]): {
60
+ node: Node;
61
+ network: CoinName;
62
+ } | null;
63
+ /**
64
+ * Try to parse a transaction with all networks and return the first one that succeeds.
65
+ *
66
+ * @param txBytes - The raw transaction bytes
67
+ * @param networks - Optional list of networks to try (defaults to all networks)
68
+ * @returns An object with the parsed Node and detected network, or null if all fail
69
+ */
70
+ export declare function tryParseTx(txBytes: Uint8Array, networks?: CoinName[]): {
71
+ node: Node;
72
+ network: CoinName;
73
+ } | null;
74
+ /**
75
+ * Parse a PSBT at the raw byte level and return a typed node tree.
76
+ *
77
+ * Unlike `parsePsbtToNode`, this function exposes the raw key-value pair
78
+ * structure as defined in BIP-174, showing:
79
+ * - Raw key type IDs and their human-readable names
80
+ * - Proprietary keys with their structured format
81
+ * - Unknown/unrecognized keys that standard parsers might skip
82
+ *
83
+ * @param psbtBytes - The raw PSBT bytes
84
+ * @param network - The network coin name (e.g., "btc", "ltc", "zec")
85
+ * @returns A Node tree representing the raw PSBT key-value structure
86
+ * @throws If the PSBT bytes are invalid or network is unknown
87
+ */
88
+ export declare function parsePsbtRawToNode(psbtBytes: Uint8Array, network: CoinName): Node;
89
+ /**
90
+ * Try to parse a raw PSBT with all networks and return the first one that succeeds.
91
+ *
92
+ * @param psbtBytes - The raw PSBT bytes
93
+ * @param networks - Optional list of networks to try (defaults to all networks)
94
+ * @returns An object with the parsed Node and detected network, or null if all fail
95
+ */
96
+ export declare function tryParsePsbtRaw(psbtBytes: Uint8Array, networks?: CoinName[]): {
97
+ node: Node;
98
+ network: CoinName;
99
+ } | null;
100
+ /**
101
+ * Check if the inspect feature is enabled in the WASM build.
102
+ *
103
+ * @returns true if the feature is enabled, false otherwise
104
+ */
105
+ export declare function isInspectEnabled(): boolean;
@@ -0,0 +1,150 @@
1
+ "use strict";
2
+ /**
3
+ * Inspect - TypeScript bindings for PSBT and Transaction parsing
4
+ *
5
+ * Provides typed wrappers around the WASM inspect functions that return
6
+ * hierarchical node structures suitable for display as collapsible trees.
7
+ *
8
+ * Import via: `import { ... } from "@bitgo/wasm-utxo/inspect"`
9
+ */
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ exports.allNetworks = void 0;
12
+ exports.parsePsbtToNode = parsePsbtToNode;
13
+ exports.parseTxToNode = parseTxToNode;
14
+ exports.tryParsePsbt = tryParsePsbt;
15
+ exports.tryParseTx = tryParseTx;
16
+ exports.parsePsbtRawToNode = parsePsbtRawToNode;
17
+ exports.tryParsePsbtRaw = tryParsePsbtRaw;
18
+ exports.isInspectEnabled = isInspectEnabled;
19
+ const wasm_utxo_js_1 = require("../wasm/wasm_utxo.js");
20
+ /** All supported networks in order of parsing priority */
21
+ exports.allNetworks = [
22
+ "btc",
23
+ "tbtc",
24
+ "tbtc4",
25
+ "tbtcsig",
26
+ "tbtcbgsig",
27
+ "ltc",
28
+ "tltc",
29
+ "bch",
30
+ "tbch",
31
+ "bcha",
32
+ "tbcha",
33
+ "btg",
34
+ "tbtg",
35
+ "bsv",
36
+ "tbsv",
37
+ "dash",
38
+ "tdash",
39
+ "doge",
40
+ "tdoge",
41
+ "zec",
42
+ "tzec",
43
+ ];
44
+ /**
45
+ * Parse a PSBT and return a typed node tree.
46
+ *
47
+ * @param psbtBytes - The raw PSBT bytes
48
+ * @param network - The network coin name (e.g., "btc", "ltc", "bch")
49
+ * @returns A Node tree representing the parsed PSBT structure
50
+ * @throws If the PSBT bytes are invalid or network is unknown
51
+ */
52
+ function parsePsbtToNode(psbtBytes, network) {
53
+ const json = (0, wasm_utxo_js_1.parsePsbtToJson)(psbtBytes, network);
54
+ return JSON.parse(json);
55
+ }
56
+ /**
57
+ * Parse a transaction and return a typed node tree.
58
+ *
59
+ * @param txBytes - The raw transaction bytes
60
+ * @param network - The network coin name (e.g., "btc", "ltc", "bch")
61
+ * @returns A Node tree representing the parsed transaction structure
62
+ * @throws If the transaction bytes are invalid or network is unknown
63
+ */
64
+ function parseTxToNode(txBytes, network) {
65
+ const json = (0, wasm_utxo_js_1.parseTxToJson)(txBytes, network);
66
+ return JSON.parse(json);
67
+ }
68
+ /**
69
+ * Try to parse a PSBT with all networks and return the first one that succeeds.
70
+ *
71
+ * @param psbtBytes - The raw PSBT bytes
72
+ * @param networks - Optional list of networks to try (defaults to all networks)
73
+ * @returns An object with the parsed Node and detected network, or null if all fail
74
+ */
75
+ function tryParsePsbt(psbtBytes, networks = exports.allNetworks) {
76
+ for (const network of networks) {
77
+ try {
78
+ const node = parsePsbtToNode(psbtBytes, network);
79
+ return { node, network };
80
+ }
81
+ catch {
82
+ // Try next network
83
+ }
84
+ }
85
+ return null;
86
+ }
87
+ /**
88
+ * Try to parse a transaction with all networks and return the first one that succeeds.
89
+ *
90
+ * @param txBytes - The raw transaction bytes
91
+ * @param networks - Optional list of networks to try (defaults to all networks)
92
+ * @returns An object with the parsed Node and detected network, or null if all fail
93
+ */
94
+ function tryParseTx(txBytes, networks = exports.allNetworks) {
95
+ for (const network of networks) {
96
+ try {
97
+ const node = parseTxToNode(txBytes, network);
98
+ return { node, network };
99
+ }
100
+ catch {
101
+ // Try next network
102
+ }
103
+ }
104
+ return null;
105
+ }
106
+ /**
107
+ * Parse a PSBT at the raw byte level and return a typed node tree.
108
+ *
109
+ * Unlike `parsePsbtToNode`, this function exposes the raw key-value pair
110
+ * structure as defined in BIP-174, showing:
111
+ * - Raw key type IDs and their human-readable names
112
+ * - Proprietary keys with their structured format
113
+ * - Unknown/unrecognized keys that standard parsers might skip
114
+ *
115
+ * @param psbtBytes - The raw PSBT bytes
116
+ * @param network - The network coin name (e.g., "btc", "ltc", "zec")
117
+ * @returns A Node tree representing the raw PSBT key-value structure
118
+ * @throws If the PSBT bytes are invalid or network is unknown
119
+ */
120
+ function parsePsbtRawToNode(psbtBytes, network) {
121
+ const json = (0, wasm_utxo_js_1.parsePsbtRawToJson)(psbtBytes, network);
122
+ return JSON.parse(json);
123
+ }
124
+ /**
125
+ * Try to parse a raw PSBT with all networks and return the first one that succeeds.
126
+ *
127
+ * @param psbtBytes - The raw PSBT bytes
128
+ * @param networks - Optional list of networks to try (defaults to all networks)
129
+ * @returns An object with the parsed Node and detected network, or null if all fail
130
+ */
131
+ function tryParsePsbtRaw(psbtBytes, networks = exports.allNetworks) {
132
+ for (const network of networks) {
133
+ try {
134
+ const node = parsePsbtRawToNode(psbtBytes, network);
135
+ return { node, network };
136
+ }
137
+ catch {
138
+ // Try next network
139
+ }
140
+ }
141
+ return null;
142
+ }
143
+ /**
144
+ * Check if the inspect feature is enabled in the WASM build.
145
+ *
146
+ * @returns true if the feature is enabled, false otherwise
147
+ */
148
+ function isInspectEnabled() {
149
+ return (0, wasm_utxo_js_1.isInspectEnabled)();
150
+ }
@@ -1,13 +1,20 @@
1
1
  import type { PsbtInputData, PsbtOutputData, PsbtOutputDataWithAddress } from "./wasm/wasm_utxo.js";
2
- /** Common interface for PSBT introspection methods */
3
- export interface IPsbtIntrospection {
4
- readonly inputCount: number;
5
- readonly outputCount: number;
2
+ /** Common interface for PSBT types */
3
+ export interface IPsbt {
4
+ inputCount(): number;
5
+ outputCount(): number;
6
6
  getInputs(): PsbtInputData[];
7
7
  getOutputs(): PsbtOutputData[];
8
+ version(): number;
9
+ lockTime(): number;
10
+ unsignedTxId(): string;
11
+ addInputAtIndex(index: number, txid: string, vout: number, value: bigint, script: Uint8Array, sequence?: number): number;
12
+ addOutputAtIndex(index: number, script: Uint8Array, value: bigint): number;
13
+ removeInput(index: number): void;
14
+ removeOutput(index: number): void;
8
15
  }
9
- /** Extended introspection with address resolution (no coin parameter needed) */
10
- export interface IPsbtIntrospectionWithAddress extends IPsbtIntrospection {
16
+ /** Extended PSBT with address resolution (no coin parameter needed) */
17
+ export interface IPsbtWithAddress extends IPsbt {
11
18
  getOutputsWithAddress(): PsbtOutputDataWithAddress[];
12
19
  }
13
20
  /**
@@ -30,13 +30,9 @@ export declare class Transaction implements ITransaction {
30
30
  * @param sequence - Optional sequence number (default: 0xFFFFFFFF)
31
31
  * @returns The index of the newly added input
32
32
  */
33
+ addInputAtIndex(index: number, txid: string, vout: number, sequence?: number): number;
33
34
  addInput(txid: string, vout: number, sequence?: number): number;
34
- /**
35
- * Add an output to the transaction
36
- * @param script - Output script (scriptPubKey)
37
- * @param value - Value in satoshis
38
- * @returns The index of the newly added output
39
- */
35
+ addOutputAtIndex(index: number, script: Uint8Array, value: bigint): number;
40
36
  addOutput(script: Uint8Array, value: bigint): number;
41
37
  toBytes(): Uint8Array;
42
38
  /**
@@ -34,15 +34,15 @@ class Transaction {
34
34
  * @param sequence - Optional sequence number (default: 0xFFFFFFFF)
35
35
  * @returns The index of the newly added input
36
36
  */
37
+ addInputAtIndex(index, txid, vout, sequence) {
38
+ return this._wasm.add_input_at_index(index, txid, vout, sequence);
39
+ }
37
40
  addInput(txid, vout, sequence) {
38
41
  return this._wasm.add_input(txid, vout, sequence);
39
42
  }
40
- /**
41
- * Add an output to the transaction
42
- * @param script - Output script (scriptPubKey)
43
- * @param value - Value in satoshis
44
- * @returns The index of the newly added output
45
- */
43
+ addOutputAtIndex(index, script, value) {
44
+ return this._wasm.add_output_at_index(index, script, value);
45
+ }
46
46
  addOutput(script, value) {
47
47
  return this._wasm.add_output(script, value);
48
48
  }