@bitgo/wasm-utxo 1.21.0 → 1.23.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.
@@ -0,0 +1,236 @@
1
+ /**
2
+ * BIP-0322 Generic Signed Message Format
3
+ *
4
+ * This module implements BIP-0322 for BitGo fixed-script wallets.
5
+ * It allows proving control of wallet addresses by signing arbitrary messages.
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * import { bip322, fixedScriptWallet } from '@bitgo/wasm-utxo';
10
+ *
11
+ * // Create wallet keys
12
+ * const walletKeys = fixedScriptWallet.RootWalletKeys.from([userXpub, backupXpub, bitgoXpub]);
13
+ *
14
+ * // Create an empty PSBT for BIP-0322 (version 0 required)
15
+ * const psbt = BitGoPsbt.createEmpty("bitcoin", walletKeys, { version: 0 });
16
+ *
17
+ * // Add BIP-0322 inputs
18
+ * const idx0 = bip322.addBip322Input(psbt, {
19
+ * message: "Hello, World!",
20
+ * scriptId: { chain: 10, index: 0 },
21
+ * rootWalletKeys: walletKeys,
22
+ * });
23
+ *
24
+ * // Sign the input
25
+ * psbt.sign(idx0, userXpriv);
26
+ * psbt.sign(idx0, bitgoXpriv);
27
+ *
28
+ * // Verify the input
29
+ * bip322.verifyBip322PsbtInput(psbt, idx0, {
30
+ * message: "Hello, World!",
31
+ * scriptId: { chain: 10, index: 0 },
32
+ * rootWalletKeys: walletKeys,
33
+ * });
34
+ * ```
35
+ */
36
+ import { BitGoPsbt, type NetworkName, type ScriptId, type SignPath } from "../fixedScriptWallet/BitGoPsbt.js";
37
+ import { type WalletKeysArg } from "../fixedScriptWallet/RootWalletKeys.js";
38
+ import { type OutputScriptType } from "../fixedScriptWallet/scriptType.js";
39
+ import { Transaction } from "../transaction.js";
40
+ export type { OutputScriptType };
41
+ /**
42
+ * Parameters for adding a BIP-0322 input to a PSBT
43
+ */
44
+ export type AddBip322InputParams = {
45
+ /** The message to sign (UTF-8 string) */
46
+ message: string;
47
+ /** The wallet script location (chain and index) */
48
+ scriptId: ScriptId;
49
+ /** The wallet's root keys */
50
+ rootWalletKeys: WalletKeysArg;
51
+ /**
52
+ * Sign path for taproot inputs (required for p2tr/p2trMusig2).
53
+ * Specifies which two keys will sign the message.
54
+ */
55
+ signPath?: SignPath;
56
+ /** Custom tag for message hashing (default: "BIP0322-signed-message") */
57
+ tag?: string;
58
+ };
59
+ /**
60
+ * Parameters for verifying a BIP-0322 input
61
+ */
62
+ export type VerifyBip322InputParams = {
63
+ /** The message that was signed */
64
+ message: string;
65
+ /** The wallet script location (chain and index) */
66
+ scriptId: ScriptId;
67
+ /** The wallet's root keys */
68
+ rootWalletKeys: WalletKeysArg;
69
+ /** Custom tag if one was used during signing */
70
+ tag?: string;
71
+ };
72
+ /**
73
+ * Parameters for verifying a BIP-0322 transaction input
74
+ */
75
+ export type VerifyBip322TxInputParams = VerifyBip322InputParams & {
76
+ /** Network name (default: "bitcoin") */
77
+ network?: NetworkName;
78
+ };
79
+ /**
80
+ * Add a BIP-0322 message input to an existing BitGoPsbt
81
+ *
82
+ * The PSBT must have version 0 per BIP-0322 specification. Use
83
+ * `BitGoPsbt.createEmpty(network, walletKeys, { version: 0 })` to create one.
84
+ *
85
+ * On the first input added, this also adds the required OP_RETURN output.
86
+ *
87
+ * @param psbt - The BitGoPsbt to add the input to (must have version 0)
88
+ * @param params - Input parameters including message, scriptId, and wallet keys
89
+ * @returns The index of the added input
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * // Create a BIP-0322 PSBT
94
+ * const psbt = BitGoPsbt.createEmpty("bitcoin", walletKeys, { version: 0 });
95
+ *
96
+ * // Add inputs
97
+ * const idx0 = bip322.addBip322Input(psbt, {
98
+ * message: "I control this address",
99
+ * scriptId: { chain: 10, index: 5 },
100
+ * rootWalletKeys: walletKeys,
101
+ * });
102
+ *
103
+ * // Sign with user and bitgo keys
104
+ * psbt.sign(idx0, userXpriv);
105
+ * psbt.sign(idx0, bitgoXpriv);
106
+ * ```
107
+ */
108
+ export declare function addBip322Input(psbt: BitGoPsbt, params: AddBip322InputParams): number;
109
+ /**
110
+ * Verify a single input of a BIP-0322 transaction proof
111
+ *
112
+ * This verifies that the specified input correctly proves control of the
113
+ * wallet address corresponding to the given message.
114
+ *
115
+ * @param tx - The signed transaction
116
+ * @param inputIndex - The index of the input to verify
117
+ * @param params - Verification parameters including message, scriptId, and wallet keys
118
+ * @throws Error if verification fails
119
+ *
120
+ * @example
121
+ * ```typescript
122
+ * // Extract and verify the transaction
123
+ * psbt.finalizeAllInputs();
124
+ * const txBytes = psbt.extractTransaction();
125
+ * const tx = Transaction.fromBytes(txBytes, "bitcoin");
126
+ *
127
+ * bip322.verifyBip322TxInput(tx, 0, {
128
+ * message: "Hello, World!",
129
+ * scriptId: { chain: 10, index: 0 },
130
+ * rootWalletKeys: walletKeys,
131
+ * network: "bitcoin",
132
+ * });
133
+ * ```
134
+ */
135
+ export declare function verifyBip322TxInput(tx: Transaction, inputIndex: number, params: VerifyBip322TxInputParams): void;
136
+ /** Signer key name */
137
+ export type SignerName = "user" | "backup" | "bitgo";
138
+ /** Triple of hex-encoded pubkeys [user, backup, bitgo] */
139
+ export type PubkeyTriple = [string, string, string];
140
+ /**
141
+ * Parameters for verifying a BIP-0322 input with pubkeys
142
+ */
143
+ export type VerifyBip322WithPubkeysParams = {
144
+ /** The message that was signed */
145
+ message: string;
146
+ /** Hex-encoded pubkeys [user, backup, bitgo] */
147
+ pubkeys: PubkeyTriple;
148
+ /** Script type */
149
+ scriptType: OutputScriptType;
150
+ /** For taproot types, whether script path was used */
151
+ isScriptPath?: boolean;
152
+ /** Custom tag if one was used during signing */
153
+ tag?: string;
154
+ };
155
+ /**
156
+ * Parameters for verifying a BIP-0322 transaction input with pubkeys
157
+ */
158
+ export type VerifyBip322TxWithPubkeysParams = VerifyBip322WithPubkeysParams;
159
+ /**
160
+ * Verify a single input of a BIP-0322 PSBT proof
161
+ *
162
+ * This verifies that the specified input correctly proves control of the
163
+ * wallet address by checking:
164
+ * - The PSBT structure follows BIP-0322 (version 0, OP_RETURN output)
165
+ * - The input references the correct virtual to_spend transaction
166
+ * - At least one valid signature exists from the wallet keys
167
+ *
168
+ * @param psbt - The signed PSBT
169
+ * @param inputIndex - The index of the input to verify
170
+ * @param params - Verification parameters including message, scriptId, and wallet keys
171
+ * @returns An array of signer names ("user", "backup", "bitgo") that have valid signatures
172
+ * @throws Error if verification fails or no valid signatures found
173
+ *
174
+ * @example
175
+ * ```typescript
176
+ * // Verify the signed PSBT input
177
+ * const signers = bip322.verifyBip322PsbtInput(psbt, 0, {
178
+ * message: "Hello, World!",
179
+ * scriptId: { chain: 10, index: 0 },
180
+ * rootWalletKeys: walletKeys,
181
+ * });
182
+ * console.log(signers); // ["user", "bitgo"]
183
+ * ```
184
+ */
185
+ export declare function verifyBip322PsbtInput(psbt: BitGoPsbt, inputIndex: number, params: VerifyBip322InputParams): SignerName[];
186
+ /**
187
+ * Verify a single input of a BIP-0322 PSBT proof using pubkeys directly
188
+ *
189
+ * This verifies that the specified input correctly proves control of the
190
+ * wallet address by checking:
191
+ * - The PSBT structure follows BIP-0322 (version 0, OP_RETURN output)
192
+ * - The input references the correct virtual to_spend transaction
193
+ * - At least one valid signature exists from the provided pubkeys
194
+ *
195
+ * @param psbt - The signed PSBT
196
+ * @param inputIndex - The index of the input to verify
197
+ * @param params - Verification parameters including message, pubkeys, and script type
198
+ * @returns An array of pubkey indices (0, 1, 2) that have valid signatures
199
+ * @throws Error if verification fails or no valid signatures found
200
+ *
201
+ * @example
202
+ * ```typescript
203
+ * // Verify the signed PSBT input with pubkeys
204
+ * const signerIndices = bip322.verifyBip322PsbtInputWithPubkeys(psbt, 0, {
205
+ * message: "Hello, World!",
206
+ * pubkeys: [userPubkey, backupPubkey, bitgoPubkey],
207
+ * scriptType: "p2shP2wsh",
208
+ * });
209
+ * console.log(signerIndices); // [0, 2] for user+bitgo
210
+ * ```
211
+ */
212
+ export declare function verifyBip322PsbtInputWithPubkeys(psbt: BitGoPsbt, inputIndex: number, params: VerifyBip322WithPubkeysParams): number[];
213
+ /**
214
+ * Verify a single input of a BIP-0322 transaction proof using pubkeys directly
215
+ *
216
+ * This verifies that the specified input correctly proves control of the
217
+ * wallet address corresponding to the given message.
218
+ *
219
+ * @param tx - The signed transaction
220
+ * @param inputIndex - The index of the input to verify
221
+ * @param params - Verification parameters including message, pubkeys, and script type
222
+ * @returns An array of pubkey indices (0, 1, 2) that have valid signatures
223
+ * @throws Error if verification fails
224
+ *
225
+ * @example
226
+ * ```typescript
227
+ * // Verify the signed transaction input with pubkeys
228
+ * const signerIndices = bip322.verifyBip322TxInputWithPubkeys(tx, 0, {
229
+ * message: "Hello, World!",
230
+ * pubkeys: [userPubkey, backupPubkey, bitgoPubkey],
231
+ * scriptType: "p2wsh",
232
+ * });
233
+ * console.log(signerIndices); // [0, 2] for user+bitgo
234
+ * ```
235
+ */
236
+ export declare function verifyBip322TxInputWithPubkeys(tx: Transaction, inputIndex: number, params: VerifyBip322TxWithPubkeysParams): number[];
@@ -0,0 +1,193 @@
1
+ "use strict";
2
+ /**
3
+ * BIP-0322 Generic Signed Message Format
4
+ *
5
+ * This module implements BIP-0322 for BitGo fixed-script wallets.
6
+ * It allows proving control of wallet addresses by signing arbitrary messages.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * import { bip322, fixedScriptWallet } from '@bitgo/wasm-utxo';
11
+ *
12
+ * // Create wallet keys
13
+ * const walletKeys = fixedScriptWallet.RootWalletKeys.from([userXpub, backupXpub, bitgoXpub]);
14
+ *
15
+ * // Create an empty PSBT for BIP-0322 (version 0 required)
16
+ * const psbt = BitGoPsbt.createEmpty("bitcoin", walletKeys, { version: 0 });
17
+ *
18
+ * // Add BIP-0322 inputs
19
+ * const idx0 = bip322.addBip322Input(psbt, {
20
+ * message: "Hello, World!",
21
+ * scriptId: { chain: 10, index: 0 },
22
+ * rootWalletKeys: walletKeys,
23
+ * });
24
+ *
25
+ * // Sign the input
26
+ * psbt.sign(idx0, userXpriv);
27
+ * psbt.sign(idx0, bitgoXpriv);
28
+ *
29
+ * // Verify the input
30
+ * bip322.verifyBip322PsbtInput(psbt, idx0, {
31
+ * message: "Hello, World!",
32
+ * scriptId: { chain: 10, index: 0 },
33
+ * rootWalletKeys: walletKeys,
34
+ * });
35
+ * ```
36
+ */
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.addBip322Input = addBip322Input;
39
+ exports.verifyBip322TxInput = verifyBip322TxInput;
40
+ exports.verifyBip322PsbtInput = verifyBip322PsbtInput;
41
+ exports.verifyBip322PsbtInputWithPubkeys = verifyBip322PsbtInputWithPubkeys;
42
+ exports.verifyBip322TxInputWithPubkeys = verifyBip322TxInputWithPubkeys;
43
+ const wasm_utxo_js_1 = require("../wasm/wasm_utxo.js");
44
+ const RootWalletKeys_js_1 = require("../fixedScriptWallet/RootWalletKeys.js");
45
+ /**
46
+ * Add a BIP-0322 message input to an existing BitGoPsbt
47
+ *
48
+ * The PSBT must have version 0 per BIP-0322 specification. Use
49
+ * `BitGoPsbt.createEmpty(network, walletKeys, { version: 0 })` to create one.
50
+ *
51
+ * On the first input added, this also adds the required OP_RETURN output.
52
+ *
53
+ * @param psbt - The BitGoPsbt to add the input to (must have version 0)
54
+ * @param params - Input parameters including message, scriptId, and wallet keys
55
+ * @returns The index of the added input
56
+ *
57
+ * @example
58
+ * ```typescript
59
+ * // Create a BIP-0322 PSBT
60
+ * const psbt = BitGoPsbt.createEmpty("bitcoin", walletKeys, { version: 0 });
61
+ *
62
+ * // Add inputs
63
+ * const idx0 = bip322.addBip322Input(psbt, {
64
+ * message: "I control this address",
65
+ * scriptId: { chain: 10, index: 5 },
66
+ * rootWalletKeys: walletKeys,
67
+ * });
68
+ *
69
+ * // Sign with user and bitgo keys
70
+ * psbt.sign(idx0, userXpriv);
71
+ * psbt.sign(idx0, bitgoXpriv);
72
+ * ```
73
+ */
74
+ function addBip322Input(psbt, params) {
75
+ const keys = RootWalletKeys_js_1.RootWalletKeys.from(params.rootWalletKeys);
76
+ return wasm_utxo_js_1.Bip322Namespace.add_bip322_input(psbt.wasm, params.message, params.scriptId.chain, params.scriptId.index, keys.wasm, params.signPath?.signer, params.signPath?.cosigner, params.tag);
77
+ }
78
+ /**
79
+ * Verify a single input of a BIP-0322 transaction proof
80
+ *
81
+ * This verifies that the specified input correctly proves control of the
82
+ * wallet address corresponding to the given message.
83
+ *
84
+ * @param tx - The signed transaction
85
+ * @param inputIndex - The index of the input to verify
86
+ * @param params - Verification parameters including message, scriptId, and wallet keys
87
+ * @throws Error if verification fails
88
+ *
89
+ * @example
90
+ * ```typescript
91
+ * // Extract and verify the transaction
92
+ * psbt.finalizeAllInputs();
93
+ * const txBytes = psbt.extractTransaction();
94
+ * const tx = Transaction.fromBytes(txBytes, "bitcoin");
95
+ *
96
+ * bip322.verifyBip322TxInput(tx, 0, {
97
+ * message: "Hello, World!",
98
+ * scriptId: { chain: 10, index: 0 },
99
+ * rootWalletKeys: walletKeys,
100
+ * network: "bitcoin",
101
+ * });
102
+ * ```
103
+ */
104
+ function verifyBip322TxInput(tx, inputIndex, params) {
105
+ const keys = RootWalletKeys_js_1.RootWalletKeys.from(params.rootWalletKeys);
106
+ const network = params.network ?? "bitcoin";
107
+ wasm_utxo_js_1.Bip322Namespace.verify_bip322_tx_input(tx.wasm, inputIndex, params.message, params.scriptId.chain, params.scriptId.index, keys.wasm, network, params.tag);
108
+ }
109
+ /**
110
+ * Verify a single input of a BIP-0322 PSBT proof
111
+ *
112
+ * This verifies that the specified input correctly proves control of the
113
+ * wallet address by checking:
114
+ * - The PSBT structure follows BIP-0322 (version 0, OP_RETURN output)
115
+ * - The input references the correct virtual to_spend transaction
116
+ * - At least one valid signature exists from the wallet keys
117
+ *
118
+ * @param psbt - The signed PSBT
119
+ * @param inputIndex - The index of the input to verify
120
+ * @param params - Verification parameters including message, scriptId, and wallet keys
121
+ * @returns An array of signer names ("user", "backup", "bitgo") that have valid signatures
122
+ * @throws Error if verification fails or no valid signatures found
123
+ *
124
+ * @example
125
+ * ```typescript
126
+ * // Verify the signed PSBT input
127
+ * const signers = bip322.verifyBip322PsbtInput(psbt, 0, {
128
+ * message: "Hello, World!",
129
+ * scriptId: { chain: 10, index: 0 },
130
+ * rootWalletKeys: walletKeys,
131
+ * });
132
+ * console.log(signers); // ["user", "bitgo"]
133
+ * ```
134
+ */
135
+ function verifyBip322PsbtInput(psbt, inputIndex, params) {
136
+ const keys = RootWalletKeys_js_1.RootWalletKeys.from(params.rootWalletKeys);
137
+ return wasm_utxo_js_1.Bip322Namespace.verify_bip322_psbt_input(psbt.wasm, inputIndex, params.message, params.scriptId.chain, params.scriptId.index, keys.wasm, params.tag);
138
+ }
139
+ /**
140
+ * Verify a single input of a BIP-0322 PSBT proof using pubkeys directly
141
+ *
142
+ * This verifies that the specified input correctly proves control of the
143
+ * wallet address by checking:
144
+ * - The PSBT structure follows BIP-0322 (version 0, OP_RETURN output)
145
+ * - The input references the correct virtual to_spend transaction
146
+ * - At least one valid signature exists from the provided pubkeys
147
+ *
148
+ * @param psbt - The signed PSBT
149
+ * @param inputIndex - The index of the input to verify
150
+ * @param params - Verification parameters including message, pubkeys, and script type
151
+ * @returns An array of pubkey indices (0, 1, 2) that have valid signatures
152
+ * @throws Error if verification fails or no valid signatures found
153
+ *
154
+ * @example
155
+ * ```typescript
156
+ * // Verify the signed PSBT input with pubkeys
157
+ * const signerIndices = bip322.verifyBip322PsbtInputWithPubkeys(psbt, 0, {
158
+ * message: "Hello, World!",
159
+ * pubkeys: [userPubkey, backupPubkey, bitgoPubkey],
160
+ * scriptType: "p2shP2wsh",
161
+ * });
162
+ * console.log(signerIndices); // [0, 2] for user+bitgo
163
+ * ```
164
+ */
165
+ function verifyBip322PsbtInputWithPubkeys(psbt, inputIndex, params) {
166
+ return Array.from(wasm_utxo_js_1.Bip322Namespace.verify_bip322_psbt_input_with_pubkeys(psbt.wasm, inputIndex, params.message, params.pubkeys, params.scriptType, params.isScriptPath, params.tag));
167
+ }
168
+ /**
169
+ * Verify a single input of a BIP-0322 transaction proof using pubkeys directly
170
+ *
171
+ * This verifies that the specified input correctly proves control of the
172
+ * wallet address corresponding to the given message.
173
+ *
174
+ * @param tx - The signed transaction
175
+ * @param inputIndex - The index of the input to verify
176
+ * @param params - Verification parameters including message, pubkeys, and script type
177
+ * @returns An array of pubkey indices (0, 1, 2) that have valid signatures
178
+ * @throws Error if verification fails
179
+ *
180
+ * @example
181
+ * ```typescript
182
+ * // Verify the signed transaction input with pubkeys
183
+ * const signerIndices = bip322.verifyBip322TxInputWithPubkeys(tx, 0, {
184
+ * message: "Hello, World!",
185
+ * pubkeys: [userPubkey, backupPubkey, bitgoPubkey],
186
+ * scriptType: "p2wsh",
187
+ * });
188
+ * console.log(signerIndices); // [0, 2] for user+bitgo
189
+ * ```
190
+ */
191
+ function verifyBip322TxInputWithPubkeys(tx, inputIndex, params) {
192
+ return Array.from(wasm_utxo_js_1.Bip322Namespace.verify_bip322_tx_input_with_pubkeys(tx.wasm, inputIndex, params.message, params.pubkeys, params.scriptType, params.isScriptPath, params.tag));
193
+ }
@@ -47,6 +47,10 @@ export declare class Dimensions {
47
47
  * Combine with another Dimensions instance
48
48
  */
49
49
  plus(other: Dimensions): Dimensions;
50
+ /**
51
+ * Multiply dimensions by a scalar
52
+ */
53
+ times(n: number): Dimensions;
50
54
  /**
51
55
  * Whether any inputs are segwit (affects overhead calculation)
52
56
  */
@@ -61,5 +65,23 @@ export declare class Dimensions {
61
65
  * @param size - "min" or "max", defaults to "max"
62
66
  */
63
67
  getVSize(size?: "min" | "max"): number;
68
+ /**
69
+ * Get input weight only (min or max)
70
+ * @param size - "min" or "max", defaults to "max"
71
+ */
72
+ getInputWeight(size?: "min" | "max"): number;
73
+ /**
74
+ * Get input virtual size (min or max)
75
+ * @param size - "min" or "max", defaults to "max"
76
+ */
77
+ getInputVSize(size?: "min" | "max"): number;
78
+ /**
79
+ * Get output weight
80
+ */
81
+ getOutputWeight(): number;
82
+ /**
83
+ * Get output virtual size
84
+ */
85
+ getOutputVSize(): number;
64
86
  }
65
87
  export {};
@@ -59,6 +59,12 @@ class Dimensions {
59
59
  plus(other) {
60
60
  return new Dimensions(this._wasm.plus(other._wasm));
61
61
  }
62
+ /**
63
+ * Multiply dimensions by a scalar
64
+ */
65
+ times(n) {
66
+ return new Dimensions(this._wasm.times(n));
67
+ }
62
68
  /**
63
69
  * Whether any inputs are segwit (affects overhead calculation)
64
70
  */
@@ -79,5 +85,31 @@ class Dimensions {
79
85
  getVSize(size = "max") {
80
86
  return this._wasm.get_vsize(size);
81
87
  }
88
+ /**
89
+ * Get input weight only (min or max)
90
+ * @param size - "min" or "max", defaults to "max"
91
+ */
92
+ getInputWeight(size = "max") {
93
+ return this._wasm.get_input_weight(size);
94
+ }
95
+ /**
96
+ * Get input virtual size (min or max)
97
+ * @param size - "min" or "max", defaults to "max"
98
+ */
99
+ getInputVSize(size = "max") {
100
+ return this._wasm.get_input_vsize(size);
101
+ }
102
+ /**
103
+ * Get output weight
104
+ */
105
+ getOutputWeight() {
106
+ return this._wasm.get_output_weight();
107
+ }
108
+ /**
109
+ * Get output virtual size
110
+ */
111
+ getOutputVSize() {
112
+ return this._wasm.get_output_vsize();
113
+ }
82
114
  }
83
115
  exports.Dimensions = Dimensions;
@@ -0,0 +1,56 @@
1
+ import type { OutputScriptType } from "./scriptType.js";
2
+ /** All valid chain codes as a const tuple */
3
+ export declare const chainCodes: readonly [0, 1, 10, 11, 20, 21, 30, 31, 40, 41];
4
+ /** A valid chain code value */
5
+ export type ChainCode = (typeof chainCodes)[number];
6
+ /** Whether a chain is for receiving (external) or change (internal) addresses */
7
+ export type Scope = "internal" | "external";
8
+ /**
9
+ * ChainCode namespace with utility functions for working with chain codes.
10
+ */
11
+ export declare const ChainCode: {
12
+ /**
13
+ * Check if a value is a valid chain code.
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * if (ChainCode.is(maybeChain)) {
18
+ * // maybeChain is now typed as ChainCode
19
+ * const scope = ChainCode.scope(maybeChain);
20
+ * }
21
+ * ```
22
+ */
23
+ is(n: unknown): n is ChainCode;
24
+ /**
25
+ * Get the chain code for a script type and scope.
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * const externalP2wsh = ChainCode.value("p2wsh", "external"); // 20
30
+ * const internalP2tr = ChainCode.value("p2trLegacy", "internal"); // 31
31
+ * ```
32
+ */
33
+ value(scriptType: OutputScriptType | "p2tr", scope: Scope): ChainCode;
34
+ /**
35
+ * Get the scope (external/internal) for a chain code.
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * ChainCode.scope(0); // "external"
40
+ * ChainCode.scope(1); // "internal"
41
+ * ChainCode.scope(20); // "external"
42
+ * ```
43
+ */
44
+ scope(chainCode: ChainCode): Scope;
45
+ /**
46
+ * Get the script type for a chain code.
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * ChainCode.scriptType(0); // "p2sh"
51
+ * ChainCode.scriptType(20); // "p2wsh"
52
+ * ChainCode.scriptType(40); // "p2trMusig2"
53
+ * ```
54
+ */
55
+ scriptType(chainCode: ChainCode): OutputScriptType;
56
+ };
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChainCode = exports.chainCodes = void 0;
4
+ /**
5
+ * Chain code utilities for BitGo fixed-script wallets.
6
+ *
7
+ * Chain codes define the derivation path component for different script types
8
+ * and scopes (external/internal) in the format `m/0/0/{chain}/{index}`.
9
+ */
10
+ const wasm_utxo_js_1 = require("../wasm/wasm_utxo.js");
11
+ /** All valid chain codes as a const tuple */
12
+ exports.chainCodes = [0, 1, 10, 11, 20, 21, 30, 31, 40, 41];
13
+ // Build static lookup tables once at module load time
14
+ const chainCodeSet = new Set(exports.chainCodes);
15
+ const chainToMeta = new Map();
16
+ const scriptTypeToChain = new Map();
17
+ // Initialize from WASM (called once at load time)
18
+ function assertChainCode(n) {
19
+ if (!chainCodeSet.has(n)) {
20
+ throw new Error(`Invalid chain code from WASM: ${n}`);
21
+ }
22
+ return n;
23
+ }
24
+ function assertScope(s) {
25
+ if (s !== "internal" && s !== "external") {
26
+ throw new Error(`Invalid scope from WASM: ${s}`);
27
+ }
28
+ return s;
29
+ }
30
+ for (const tuple of wasm_utxo_js_1.FixedScriptWalletNamespace.chain_code_table()) {
31
+ if (!Array.isArray(tuple) || tuple.length !== 3) {
32
+ throw new Error(`Invalid chain_code_table entry: expected [number, string, string]`);
33
+ }
34
+ const [rawCode, rawScriptType, rawScope] = tuple;
35
+ if (typeof rawCode !== "number") {
36
+ throw new Error(`Invalid chain code type: ${typeof rawCode}`);
37
+ }
38
+ if (typeof rawScriptType !== "string") {
39
+ throw new Error(`Invalid scriptType type: ${typeof rawScriptType}`);
40
+ }
41
+ if (typeof rawScope !== "string") {
42
+ throw new Error(`Invalid scope type: ${typeof rawScope}`);
43
+ }
44
+ const code = assertChainCode(rawCode);
45
+ const scriptType = rawScriptType;
46
+ const scope = assertScope(rawScope);
47
+ chainToMeta.set(code, { scope, scriptType });
48
+ let entry = scriptTypeToChain.get(scriptType);
49
+ if (!entry) {
50
+ entry = {};
51
+ scriptTypeToChain.set(scriptType, entry);
52
+ }
53
+ entry[scope] = code;
54
+ }
55
+ /**
56
+ * ChainCode namespace with utility functions for working with chain codes.
57
+ */
58
+ exports.ChainCode = {
59
+ /**
60
+ * Check if a value is a valid chain code.
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * if (ChainCode.is(maybeChain)) {
65
+ * // maybeChain is now typed as ChainCode
66
+ * const scope = ChainCode.scope(maybeChain);
67
+ * }
68
+ * ```
69
+ */
70
+ is(n) {
71
+ return typeof n === "number" && chainCodeSet.has(n);
72
+ },
73
+ /**
74
+ * Get the chain code for a script type and scope.
75
+ *
76
+ * @example
77
+ * ```typescript
78
+ * const externalP2wsh = ChainCode.value("p2wsh", "external"); // 20
79
+ * const internalP2tr = ChainCode.value("p2trLegacy", "internal"); // 31
80
+ * ```
81
+ */
82
+ value(scriptType, scope) {
83
+ // legacy alias for p2trLegacy
84
+ if (scriptType === "p2tr") {
85
+ scriptType = "p2trLegacy";
86
+ }
87
+ const entry = scriptTypeToChain.get(scriptType);
88
+ if (!entry) {
89
+ throw new Error(`Invalid scriptType: ${scriptType}`);
90
+ }
91
+ return entry[scope];
92
+ },
93
+ /**
94
+ * Get the scope (external/internal) for a chain code.
95
+ *
96
+ * @example
97
+ * ```typescript
98
+ * ChainCode.scope(0); // "external"
99
+ * ChainCode.scope(1); // "internal"
100
+ * ChainCode.scope(20); // "external"
101
+ * ```
102
+ */
103
+ scope(chainCode) {
104
+ const meta = chainToMeta.get(chainCode);
105
+ if (!meta)
106
+ throw new Error(`Invalid chainCode: ${chainCode}`);
107
+ return meta.scope;
108
+ },
109
+ /**
110
+ * Get the script type for a chain code.
111
+ *
112
+ * @example
113
+ * ```typescript
114
+ * ChainCode.scriptType(0); // "p2sh"
115
+ * ChainCode.scriptType(20); // "p2wsh"
116
+ * ChainCode.scriptType(40); // "p2trMusig2"
117
+ * ```
118
+ */
119
+ scriptType(chainCode) {
120
+ const meta = chainToMeta.get(chainCode);
121
+ if (!meta)
122
+ throw new Error(`Invalid chainCode: ${chainCode}`);
123
+ return meta.scriptType;
124
+ },
125
+ };