Skip to content

Commit fbc7c4a

Browse files
Merge pull request #117 from BitGo/BTC-2980.add-sign-input-func.testutils
feat(wasm-utxo): add helper functions for PSBT testing and OP_RETURN scripts
2 parents 2b9bc51 + 4e5a152 commit fbc7c4a

File tree

8 files changed

+1028
-23
lines changed

8 files changed

+1028
-23
lines changed

packages/wasm-utxo/js/fixedScriptWallet/chains.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ const chainCodeSet = new Set<number>(chainCodes);
2121
const chainToMeta = new Map<ChainCode, { scope: Scope; scriptType: OutputScriptType }>();
2222
const scriptTypeToChain = new Map<OutputScriptType, { internal: ChainCode; external: ChainCode }>();
2323

24-
// Initialize from WASM (called once at load time)
25-
function assertChainCode(n: number): ChainCode {
24+
/**
25+
* Assert that a number is a valid chain code.
26+
* @throws Error if the number is not a valid chain code
27+
*/
28+
export function assertChainCode(n: number): ChainCode {
2629
if (!chainCodeSet.has(n)) {
27-
throw new Error(`Invalid chain code from WASM: ${n}`);
30+
throw new Error(`Invalid chain code: ${n}`);
2831
}
2932
return n as ChainCode;
3033
}

packages/wasm-utxo/js/fixedScriptWallet/index.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ export { RootWalletKeys, type WalletKeysArg, type IWalletKeys } from "./RootWall
55
export { ReplayProtection, type ReplayProtectionArg } from "./ReplayProtection.js";
66
export { outputScript, address } from "./address.js";
77
export { Dimensions } from "./Dimensions.js";
8-
export { type OutputScriptType, type InputScriptType, type ScriptType } from "./scriptType.js";
9-
export { ChainCode, chainCodes, type Scope } from "./chains.js";
8+
export {
9+
outputScriptTypes,
10+
inputScriptTypes,
11+
type OutputScriptType,
12+
type InputScriptType,
13+
type ScriptType,
14+
} from "./scriptType.js";
15+
export { ChainCode, chainCodes, assertChainCode, type Scope } from "./chains.js";
1016

1117
// Bitcoin-like PSBT (for all non-Zcash networks)
1218
export {
@@ -61,3 +67,23 @@ import type { ScriptType } from "./scriptType.js";
6167
export function supportsScriptType(coin: CoinName, scriptType: ScriptType): boolean {
6268
return FixedScriptWalletNamespace.supports_script_type(coin, scriptType);
6369
}
70+
71+
/**
72+
* Create an OP_RETURN output script with optional data
73+
*
74+
* @param data - Optional data bytes to include in the OP_RETURN script
75+
* @returns The OP_RETURN script as a Uint8Array
76+
*
77+
* @example
78+
* ```typescript
79+
* // Empty OP_RETURN
80+
* const script = createOpReturnScript();
81+
*
82+
* // OP_RETURN with data
83+
* const data = new Uint8Array([1, 2, 3, 4]);
84+
* const script = createOpReturnScript(data);
85+
* ```
86+
*/
87+
export function createOpReturnScript(data?: Uint8Array): Uint8Array {
88+
return FixedScriptWalletNamespace.create_op_return_script(data);
89+
}

packages/wasm-utxo/js/fixedScriptWallet/scriptType.ts

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,43 @@
11
/**
2-
* Fixed-script wallet output script types (2-of-3 multisig)
2+
* All output script types for fixed-script wallets (2-of-3 multisig)
33
*
4-
* This type represents the abstract script type, independent of chain (external/internal).
4+
* This represents the abstract script type, independent of chain (external/internal).
55
* Use this for checking network support or when you need the script type without derivation info.
66
*/
7-
export type OutputScriptType =
8-
| "p2sh"
9-
| "p2shP2wsh"
10-
| "p2wsh"
11-
| "p2tr" // alias for p2trLegacy
12-
| "p2trLegacy"
13-
| "p2trMusig2";
7+
export const outputScriptTypes = [
8+
"p2sh",
9+
"p2shP2wsh",
10+
"p2wsh",
11+
"p2trLegacy",
12+
"p2trMusig2",
13+
] as const;
1414

1515
/**
16-
* Input script types for fixed-script wallets
16+
* Output script type for fixed-script wallets
17+
*
18+
* Note: "p2tr" is an alias for "p2trLegacy" for backward compatibility.
19+
*/
20+
export type OutputScriptType = (typeof outputScriptTypes)[number] | "p2tr";
21+
22+
/**
23+
* All input script types for fixed-script wallets
1724
*
1825
* These are more specific than output types and include single-sig and taproot variants.
1926
*/
20-
export type InputScriptType =
21-
| "p2shP2pk"
22-
| "p2sh"
23-
| "p2shP2wsh"
24-
| "p2wsh"
25-
| "p2trLegacy"
26-
| "p2trMusig2ScriptPath"
27-
| "p2trMusig2KeyPath";
27+
export const inputScriptTypes = [
28+
"p2shP2pk",
29+
"p2sh",
30+
"p2shP2wsh",
31+
"p2wsh",
32+
"p2trLegacy",
33+
"p2trMusig2ScriptPath",
34+
"p2trMusig2KeyPath",
35+
] as const;
36+
37+
/**
38+
* Input script type for fixed-script wallets
39+
*/
40+
export type InputScriptType = (typeof inputScriptTypes)[number];
2841

2942
/**
3043
* Union of all script types that can be checked for network support

0 commit comments

Comments
 (0)