@bitgo/wasm-utxo 1.6.0 → 1.7.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.
Files changed (53) hide show
  1. package/dist/cjs/js/bip32.d.ts +140 -0
  2. package/dist/cjs/js/bip32.js +177 -0
  3. package/dist/cjs/js/ecpair.d.ts +96 -0
  4. package/dist/cjs/js/ecpair.js +134 -0
  5. package/dist/cjs/js/{fixedScriptWallet.d.ts → fixedScriptWallet/BitGoPsbt.d.ts} +38 -41
  6. package/dist/cjs/js/{fixedScriptWallet.js → fixedScriptWallet/BitGoPsbt.js} +50 -36
  7. package/dist/cjs/js/fixedScriptWallet/ReplayProtection.d.ts +58 -0
  8. package/dist/cjs/js/fixedScriptWallet/ReplayProtection.js +89 -0
  9. package/dist/cjs/js/fixedScriptWallet/RootWalletKeys.d.ts +66 -0
  10. package/dist/cjs/js/fixedScriptWallet/RootWalletKeys.js +108 -0
  11. package/dist/cjs/js/fixedScriptWallet/address.d.ts +20 -0
  12. package/dist/cjs/js/fixedScriptWallet/address.js +29 -0
  13. package/dist/cjs/js/fixedScriptWallet/index.d.ts +4 -0
  14. package/dist/cjs/js/fixedScriptWallet/index.js +12 -0
  15. package/dist/cjs/js/index.d.ts +5 -1
  16. package/dist/cjs/js/index.js +11 -2
  17. package/dist/cjs/js/utxolibCompat.d.ts +0 -18
  18. package/dist/cjs/js/wasm/wasm_utxo.d.ts +254 -22
  19. package/dist/cjs/js/wasm/wasm_utxo.js +1081 -223
  20. package/dist/cjs/js/wasm/wasm_utxo_bg.wasm +0 -0
  21. package/dist/cjs/js/wasm/wasm_utxo_bg.wasm.d.ts +53 -8
  22. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  23. package/dist/esm/js/bip32.d.ts +140 -0
  24. package/dist/esm/js/bip32.js +173 -0
  25. package/dist/esm/js/ecpair.d.ts +96 -0
  26. package/dist/esm/js/ecpair.js +130 -0
  27. package/dist/esm/js/{fixedScriptWallet.d.ts → fixedScriptWallet/BitGoPsbt.d.ts} +38 -41
  28. package/dist/esm/js/{fixedScriptWallet.js → fixedScriptWallet/BitGoPsbt.js} +49 -33
  29. package/dist/esm/js/fixedScriptWallet/ReplayProtection.d.ts +58 -0
  30. package/dist/esm/js/fixedScriptWallet/ReplayProtection.js +85 -0
  31. package/dist/esm/js/fixedScriptWallet/RootWalletKeys.d.ts +66 -0
  32. package/dist/esm/js/fixedScriptWallet/RootWalletKeys.js +104 -0
  33. package/dist/esm/js/fixedScriptWallet/address.d.ts +20 -0
  34. package/dist/esm/js/fixedScriptWallet/address.js +25 -0
  35. package/dist/esm/js/fixedScriptWallet/index.d.ts +4 -0
  36. package/dist/esm/js/fixedScriptWallet/index.js +4 -0
  37. package/dist/esm/js/index.d.ts +5 -1
  38. package/dist/esm/js/index.js +8 -1
  39. package/dist/esm/js/utxolibCompat.d.ts +0 -18
  40. package/dist/esm/js/wasm/wasm_utxo.d.ts +254 -22
  41. package/dist/esm/js/wasm/wasm_utxo_bg.js +1070 -220
  42. package/dist/esm/js/wasm/wasm_utxo_bg.wasm +0 -0
  43. package/dist/esm/js/wasm/wasm_utxo_bg.wasm.d.ts +53 -8
  44. package/dist/esm/test/bip32.d.ts +1 -0
  45. package/dist/esm/test/bip32.js +242 -0
  46. package/dist/esm/test/ecpair.d.ts +1 -0
  47. package/dist/esm/test/ecpair.js +137 -0
  48. package/dist/esm/test/fixedScript/fixtureUtil.d.ts +4 -2
  49. package/dist/esm/test/fixedScript/fixtureUtil.js +18 -7
  50. package/dist/esm/test/fixedScript/parseTransactionWithWalletKeys.js +7 -7
  51. package/dist/esm/test/fixedScript/verifySignature.js +72 -26
  52. package/dist/esm/tsconfig.tsbuildinfo +1 -1
  53. package/package.json +1 -1
@@ -0,0 +1,173 @@
1
+ import { WasmBIP32 } from "./wasm/wasm_utxo.js";
2
+ /**
3
+ * BIP32 wrapper class for extended key operations
4
+ */
5
+ export class BIP32 {
6
+ _wasm;
7
+ constructor(_wasm) {
8
+ this._wasm = _wasm;
9
+ }
10
+ /**
11
+ * Create a BIP32 instance from a WasmBIP32 instance (internal use)
12
+ * @internal
13
+ */
14
+ static fromWasm(wasm) {
15
+ return new BIP32(wasm);
16
+ }
17
+ /**
18
+ * Convert BIP32Arg to BIP32 instance
19
+ * @param key - The BIP32 key in various formats
20
+ * @returns BIP32 instance
21
+ */
22
+ static from(key) {
23
+ // Short-circuit if already a BIP32 instance
24
+ if (key instanceof BIP32) {
25
+ return key;
26
+ }
27
+ // If it's a WasmBIP32 instance, wrap it
28
+ if (key instanceof WasmBIP32) {
29
+ return new BIP32(key);
30
+ }
31
+ // If it's a string, parse from base58
32
+ if (typeof key === "string") {
33
+ const wasm = WasmBIP32.from_base58(key);
34
+ return new BIP32(wasm);
35
+ }
36
+ // If it's an object (BIP32Interface), use from_bip32_interface
37
+ if (typeof key === "object" && key !== null) {
38
+ const wasm = WasmBIP32.from_bip32_interface(key);
39
+ return new BIP32(wasm);
40
+ }
41
+ throw new Error("Invalid BIP32Arg type");
42
+ }
43
+ /**
44
+ * Create a BIP32 key from a base58 string (xpub/xprv/tpub/tprv)
45
+ * @param base58Str - The base58-encoded extended key string
46
+ * @returns A BIP32 instance
47
+ */
48
+ static fromBase58(base58Str) {
49
+ const wasm = WasmBIP32.from_base58(base58Str);
50
+ return new BIP32(wasm);
51
+ }
52
+ /**
53
+ * Create a BIP32 master key from a seed
54
+ * @param seed - The seed bytes
55
+ * @param network - Optional network string
56
+ * @returns A BIP32 instance
57
+ */
58
+ static fromSeed(seed, network) {
59
+ const wasm = WasmBIP32.from_seed(seed, network);
60
+ return new BIP32(wasm);
61
+ }
62
+ /**
63
+ * Get the chain code as a Uint8Array
64
+ */
65
+ get chainCode() {
66
+ return this._wasm.chain_code;
67
+ }
68
+ /**
69
+ * Get the depth
70
+ */
71
+ get depth() {
72
+ return this._wasm.depth;
73
+ }
74
+ /**
75
+ * Get the child index
76
+ */
77
+ get index() {
78
+ return this._wasm.index;
79
+ }
80
+ /**
81
+ * Get the parent fingerprint
82
+ */
83
+ get parentFingerprint() {
84
+ return this._wasm.parent_fingerprint;
85
+ }
86
+ /**
87
+ * Get the private key as a Uint8Array (if available)
88
+ */
89
+ get privateKey() {
90
+ return this._wasm.private_key;
91
+ }
92
+ /**
93
+ * Get the public key as a Uint8Array
94
+ */
95
+ get publicKey() {
96
+ return this._wasm.public_key;
97
+ }
98
+ /**
99
+ * Get the identifier as a Uint8Array
100
+ */
101
+ get identifier() {
102
+ return this._wasm.identifier;
103
+ }
104
+ /**
105
+ * Get the fingerprint as a Uint8Array
106
+ */
107
+ get fingerprint() {
108
+ return this._wasm.fingerprint;
109
+ }
110
+ /**
111
+ * Check if this is a neutered (public) key
112
+ * @returns True if the key is public-only (neutered)
113
+ */
114
+ isNeutered() {
115
+ return this._wasm.is_neutered();
116
+ }
117
+ /**
118
+ * Get the neutered (public) version of this key
119
+ * @returns A new BIP32 instance containing only the public key
120
+ */
121
+ neutered() {
122
+ const wasm = this._wasm.neutered();
123
+ return new BIP32(wasm);
124
+ }
125
+ /**
126
+ * Serialize to base58 string
127
+ * @returns The base58-encoded extended key string
128
+ */
129
+ toBase58() {
130
+ return this._wasm.to_base58();
131
+ }
132
+ /**
133
+ * Get the WIF encoding of the private key
134
+ * @returns The WIF-encoded private key
135
+ */
136
+ toWIF() {
137
+ return this._wasm.to_wif();
138
+ }
139
+ /**
140
+ * Derive a normal (non-hardened) child key
141
+ * @param index - The child index
142
+ * @returns A new BIP32 instance for the derived key
143
+ */
144
+ derive(index) {
145
+ const wasm = this._wasm.derive(index);
146
+ return new BIP32(wasm);
147
+ }
148
+ /**
149
+ * Derive a hardened child key (only works for private keys)
150
+ * @param index - The child index
151
+ * @returns A new BIP32 instance for the derived key
152
+ */
153
+ deriveHardened(index) {
154
+ const wasm = this._wasm.derive_hardened(index);
155
+ return new BIP32(wasm);
156
+ }
157
+ /**
158
+ * Derive a key using a derivation path (e.g., "0/1/2" or "m/0/1/2")
159
+ * @param path - The derivation path string
160
+ * @returns A new BIP32 instance for the derived key
161
+ */
162
+ derivePath(path) {
163
+ const wasm = this._wasm.derive_path(path);
164
+ return new BIP32(wasm);
165
+ }
166
+ /**
167
+ * Get the underlying WASM instance (internal use only)
168
+ * @internal
169
+ */
170
+ get wasm() {
171
+ return this._wasm;
172
+ }
173
+ }
@@ -0,0 +1,96 @@
1
+ import { WasmECPair } from "./wasm/wasm_utxo.js";
2
+ /**
3
+ * ECPairArg represents the various forms that ECPair keys can take
4
+ * before being converted to a WasmECPair instance
5
+ */
6
+ export type ECPairArg =
7
+ /** Private key (32 bytes) or compressed public key (33 bytes) as Buffer/Uint8Array */
8
+ Uint8Array
9
+ /** ECPair instance */
10
+ | ECPair
11
+ /** WasmECPair instance */
12
+ | WasmECPair;
13
+ /**
14
+ * ECPair interface for elliptic curve key pair operations
15
+ */
16
+ export interface ECPairInterface {
17
+ publicKey: Uint8Array;
18
+ privateKey?: Uint8Array;
19
+ toWIF(): string;
20
+ }
21
+ /**
22
+ * ECPair wrapper class for elliptic curve key pair operations
23
+ */
24
+ export declare class ECPair implements ECPairInterface {
25
+ private _wasm;
26
+ private constructor();
27
+ /**
28
+ * Create an ECPair instance from a WasmECPair instance (internal use)
29
+ * @internal
30
+ */
31
+ static fromWasm(wasm: WasmECPair): ECPair;
32
+ /**
33
+ * Convert ECPairArg to ECPair instance
34
+ * @param key - The ECPair key in various formats
35
+ * @returns ECPair instance
36
+ */
37
+ static from(key: ECPairArg): ECPair;
38
+ /**
39
+ * Create an ECPair from a private key (always uses compressed keys)
40
+ * @param buffer - The 32-byte private key
41
+ * @returns An ECPair instance
42
+ */
43
+ static fromPrivateKey(buffer: Uint8Array): ECPair;
44
+ /**
45
+ * Create an ECPair from a compressed public key
46
+ * @param buffer - The compressed public key bytes (33 bytes)
47
+ * @returns An ECPair instance
48
+ */
49
+ static fromPublicKey(buffer: Uint8Array): ECPair;
50
+ /**
51
+ * Create an ECPair from a WIF string (auto-detects network from WIF)
52
+ * @param wifString - The WIF-encoded private key string
53
+ * @returns An ECPair instance
54
+ */
55
+ static fromWIF(wifString: string): ECPair;
56
+ /**
57
+ * Create an ECPair from a mainnet WIF string
58
+ * @param wifString - The WIF-encoded private key string
59
+ * @returns An ECPair instance
60
+ */
61
+ static fromWIFMainnet(wifString: string): ECPair;
62
+ /**
63
+ * Create an ECPair from a testnet WIF string
64
+ * @param wifString - The WIF-encoded private key string
65
+ * @returns An ECPair instance
66
+ */
67
+ static fromWIFTestnet(wifString: string): ECPair;
68
+ /**
69
+ * Get the private key as a Uint8Array (if available)
70
+ */
71
+ get privateKey(): Uint8Array | undefined;
72
+ /**
73
+ * Get the public key as a Uint8Array
74
+ */
75
+ get publicKey(): Uint8Array;
76
+ /**
77
+ * Convert to WIF string (mainnet)
78
+ * @returns The WIF-encoded private key
79
+ */
80
+ toWIF(): string;
81
+ /**
82
+ * Convert to mainnet WIF string
83
+ * @returns The WIF-encoded private key
84
+ */
85
+ toWIFMainnet(): string;
86
+ /**
87
+ * Convert to testnet WIF string
88
+ * @returns The WIF-encoded private key
89
+ */
90
+ toWIFTestnet(): string;
91
+ /**
92
+ * Get the underlying WASM instance (internal use only)
93
+ * @internal
94
+ */
95
+ get wasm(): WasmECPair;
96
+ }
@@ -0,0 +1,130 @@
1
+ import { WasmECPair } from "./wasm/wasm_utxo.js";
2
+ /**
3
+ * ECPair wrapper class for elliptic curve key pair operations
4
+ */
5
+ export class ECPair {
6
+ _wasm;
7
+ constructor(_wasm) {
8
+ this._wasm = _wasm;
9
+ }
10
+ /**
11
+ * Create an ECPair instance from a WasmECPair instance (internal use)
12
+ * @internal
13
+ */
14
+ static fromWasm(wasm) {
15
+ return new ECPair(wasm);
16
+ }
17
+ /**
18
+ * Convert ECPairArg to ECPair instance
19
+ * @param key - The ECPair key in various formats
20
+ * @returns ECPair instance
21
+ */
22
+ static from(key) {
23
+ // Short-circuit if already an ECPair instance
24
+ if (key instanceof ECPair) {
25
+ return key;
26
+ }
27
+ // If it's a WasmECPair instance, wrap it
28
+ if (key instanceof WasmECPair) {
29
+ return new ECPair(key);
30
+ }
31
+ // Parse from Buffer/Uint8Array
32
+ // Check length to determine if it's a private key (32 bytes) or public key (33 bytes)
33
+ if (key.length === 32) {
34
+ const wasm = WasmECPair.from_private_key(key);
35
+ return new ECPair(wasm);
36
+ }
37
+ else if (key.length === 33) {
38
+ const wasm = WasmECPair.from_public_key(key);
39
+ return new ECPair(wasm);
40
+ }
41
+ else {
42
+ throw new Error(`Invalid key length: ${key.length}. Expected 32 bytes (private key) or 33 bytes (compressed public key)`);
43
+ }
44
+ }
45
+ /**
46
+ * Create an ECPair from a private key (always uses compressed keys)
47
+ * @param buffer - The 32-byte private key
48
+ * @returns An ECPair instance
49
+ */
50
+ static fromPrivateKey(buffer) {
51
+ const wasm = WasmECPair.from_private_key(buffer);
52
+ return new ECPair(wasm);
53
+ }
54
+ /**
55
+ * Create an ECPair from a compressed public key
56
+ * @param buffer - The compressed public key bytes (33 bytes)
57
+ * @returns An ECPair instance
58
+ */
59
+ static fromPublicKey(buffer) {
60
+ const wasm = WasmECPair.from_public_key(buffer);
61
+ return new ECPair(wasm);
62
+ }
63
+ /**
64
+ * Create an ECPair from a WIF string (auto-detects network from WIF)
65
+ * @param wifString - The WIF-encoded private key string
66
+ * @returns An ECPair instance
67
+ */
68
+ static fromWIF(wifString) {
69
+ const wasm = WasmECPair.from_wif(wifString);
70
+ return new ECPair(wasm);
71
+ }
72
+ /**
73
+ * Create an ECPair from a mainnet WIF string
74
+ * @param wifString - The WIF-encoded private key string
75
+ * @returns An ECPair instance
76
+ */
77
+ static fromWIFMainnet(wifString) {
78
+ const wasm = WasmECPair.from_wif_mainnet(wifString);
79
+ return new ECPair(wasm);
80
+ }
81
+ /**
82
+ * Create an ECPair from a testnet WIF string
83
+ * @param wifString - The WIF-encoded private key string
84
+ * @returns An ECPair instance
85
+ */
86
+ static fromWIFTestnet(wifString) {
87
+ const wasm = WasmECPair.from_wif_testnet(wifString);
88
+ return new ECPair(wasm);
89
+ }
90
+ /**
91
+ * Get the private key as a Uint8Array (if available)
92
+ */
93
+ get privateKey() {
94
+ return this._wasm.private_key;
95
+ }
96
+ /**
97
+ * Get the public key as a Uint8Array
98
+ */
99
+ get publicKey() {
100
+ return this._wasm.public_key;
101
+ }
102
+ /**
103
+ * Convert to WIF string (mainnet)
104
+ * @returns The WIF-encoded private key
105
+ */
106
+ toWIF() {
107
+ return this._wasm.to_wif();
108
+ }
109
+ /**
110
+ * Convert to mainnet WIF string
111
+ * @returns The WIF-encoded private key
112
+ */
113
+ toWIFMainnet() {
114
+ return this._wasm.to_wif_mainnet();
115
+ }
116
+ /**
117
+ * Convert to testnet WIF string
118
+ * @returns The WIF-encoded private key
119
+ */
120
+ toWIFTestnet() {
121
+ return this._wasm.to_wif_testnet();
122
+ }
123
+ /**
124
+ * Get the underlying WASM instance (internal use only)
125
+ * @internal
126
+ */
127
+ get wasm() {
128
+ return this._wasm;
129
+ }
130
+ }
@@ -1,35 +1,10 @@
1
- import type { UtxolibName, UtxolibNetwork, UtxolibRootWalletKeys } from "./utxolibCompat.js";
2
- import type { CoinName } from "./coinName.js";
3
- import { Triple } from "./triple.js";
4
- import { AddressFormat } from "./address.js";
1
+ import { type WalletKeysArg } from "./RootWalletKeys.js";
2
+ import { type ReplayProtectionArg } from "./ReplayProtection.js";
3
+ import { type BIP32Arg } from "../bip32.js";
4
+ import { type ECPairArg } from "../ecpair.js";
5
+ import type { UtxolibName } from "../utxolibCompat.js";
6
+ import type { CoinName } from "../coinName.js";
5
7
  export type NetworkName = UtxolibName | CoinName;
6
- export type WalletKeys =
7
- /** Just an xpub triple, will assume default derivation prefixes */
8
- Triple<string>
9
- /** Compatible with utxolib RootWalletKeys */
10
- | UtxolibRootWalletKeys;
11
- /**
12
- * Create the output script for a given wallet keys and chain and index
13
- */
14
- export declare function outputScript(keys: WalletKeys, chain: number, index: number, network: UtxolibNetwork): Uint8Array;
15
- /**
16
- * Create the address for a given wallet keys and chain and index and network.
17
- * Wrapper for outputScript that also encodes the script to an address.
18
- * @param keys - The wallet keys to use.
19
- * @param chain - The chain to use.
20
- * @param index - The index to use.
21
- * @param network - The network to use.
22
- * @param addressFormat - The address format to use.
23
- * Only relevant for Bitcoin Cash and eCash networks, where:
24
- * - "default" means base58check,
25
- * - "cashaddr" means cashaddr.
26
- */
27
- export declare function address(keys: WalletKeys, chain: number, index: number, network: UtxolibNetwork, addressFormat?: AddressFormat): string;
28
- type ReplayProtection = {
29
- outputScripts: Uint8Array[];
30
- } | {
31
- addresses: string[];
32
- };
33
8
  export type ScriptId = {
34
9
  chain: number;
35
10
  index: number;
@@ -76,7 +51,7 @@ export declare class BitGoPsbt {
76
51
  * @param replayProtection - Scripts that are allowed as inputs without wallet validation
77
52
  * @returns Parsed transaction information
78
53
  */
79
- parseTransactionWithWalletKeys(walletKeys: WalletKeys, replayProtection: ReplayProtection): ParsedTransaction;
54
+ parseTransactionWithWalletKeys(walletKeys: WalletKeysArg, replayProtection: ReplayProtectionArg): ParsedTransaction;
80
55
  /**
81
56
  * Parse outputs with wallet keys to identify which outputs belong to a wallet
82
57
  * with the given wallet keys.
@@ -88,23 +63,46 @@ export declare class BitGoPsbt {
88
63
  * @returns Array of parsed outputs
89
64
  * @note This method does NOT validate wallet inputs. It only parses outputs.
90
65
  */
91
- parseOutputsWithWalletKeys(walletKeys: WalletKeys): ParsedOutput[];
66
+ parseOutputsWithWalletKeys(walletKeys: WalletKeysArg): ParsedOutput[];
92
67
  /**
93
- * Verify if a valid signature exists for a given extended public key at the specified input index.
68
+ * Verify if a valid signature exists for a given key at the specified input index.
69
+ *
70
+ * This method can verify signatures using either:
71
+ * - Extended public key (xpub): Derives the public key using the derivation path from PSBT
72
+ * - ECPair (private key): Extracts the public key and verifies directly
94
73
  *
95
- * This method derives the public key from the xpub using the derivation path found in the
96
- * PSBT input, then verifies the signature. It supports:
74
+ * When using xpub, it supports:
97
75
  * - ECDSA signatures (for legacy/SegWit inputs)
98
76
  * - Schnorr signatures (for Taproot script path inputs)
99
77
  * - MuSig2 partial signatures (for Taproot keypath MuSig2 inputs)
100
78
  *
79
+ * When using ECPair, it supports:
80
+ * - ECDSA signatures (for legacy/SegWit inputs)
81
+ * - Schnorr signatures (for Taproot script path inputs)
82
+ * Note: MuSig2 inputs require xpubs for derivation
83
+ *
101
84
  * @param inputIndex - The index of the input to check (0-based)
102
- * @param xpub - The extended public key as a base58-encoded string
85
+ * @param key - Either an extended public key (base58 string, BIP32 instance, or WasmBIP32) or an ECPair (private key Buffer, ECPair instance, or WasmECPair)
103
86
  * @returns true if a valid signature exists, false if no signature exists
104
- * @throws Error if input index is out of bounds, xpub is invalid, or verification fails
87
+ * @throws Error if input index is out of bounds, key is invalid, or verification fails
88
+ *
89
+ * @example
90
+ * ```typescript
91
+ * // Verify wallet input signature with xpub
92
+ * const hasUserSig = psbt.verifySignature(0, userXpub);
93
+ *
94
+ * // Verify signature with ECPair (private key)
95
+ * const ecpair = ECPair.fromPrivateKey(privateKeyBuffer);
96
+ * const hasReplaySig = psbt.verifySignature(1, ecpair);
97
+ *
98
+ * // Or pass private key directly
99
+ * const hasReplaySig2 = psbt.verifySignature(1, privateKeyBuffer);
100
+ * ```
105
101
  */
106
- verifySignature(inputIndex: number, xpub: string): boolean;
102
+ verifySignature(inputIndex: number, key: BIP32Arg | ECPairArg): boolean;
107
103
  /**
104
+ * @deprecated - use verifySignature with the replay protection key instead
105
+ *
108
106
  * Verify if a replay protection input has a valid signature.
109
107
  *
110
108
  * This method checks if a given input is a replay protection input (like P2shP2pk) and verifies
@@ -122,7 +120,7 @@ export declare class BitGoPsbt {
122
120
  * @returns true if the input is a replay protection input and has a valid signature, false if no valid signature
123
121
  * @throws Error if the input is not a replay protection input, index is out of bounds, or scripts are invalid
124
122
  */
125
- verifyReplayProtectionSignature(inputIndex: number, replayProtection: ReplayProtection): boolean;
123
+ verifyReplayProtectionSignature(inputIndex: number, replayProtection: ReplayProtectionArg): boolean;
126
124
  /**
127
125
  * Serialize the PSBT to bytes
128
126
  *
@@ -143,4 +141,3 @@ export declare class BitGoPsbt {
143
141
  */
144
142
  extractTransaction(): Uint8Array;
145
143
  }
146
- export {};
@@ -1,26 +1,8 @@
1
- import { FixedScriptWalletNamespace } from "./wasm/wasm_utxo.js";
2
- /**
3
- * Create the output script for a given wallet keys and chain and index
4
- */
5
- export function outputScript(keys, chain, index, network) {
6
- return FixedScriptWalletNamespace.output_script(keys, chain, index, network);
7
- }
8
- /**
9
- * Create the address for a given wallet keys and chain and index and network.
10
- * Wrapper for outputScript that also encodes the script to an address.
11
- * @param keys - The wallet keys to use.
12
- * @param chain - The chain to use.
13
- * @param index - The index to use.
14
- * @param network - The network to use.
15
- * @param addressFormat - The address format to use.
16
- * Only relevant for Bitcoin Cash and eCash networks, where:
17
- * - "default" means base58check,
18
- * - "cashaddr" means cashaddr.
19
- */
20
- export function address(keys, chain, index, network, addressFormat) {
21
- return FixedScriptWalletNamespace.address(keys, chain, index, network, addressFormat);
22
- }
23
- import { BitGoPsbt as WasmBitGoPsbt } from "./wasm/wasm_utxo.js";
1
+ import { BitGoPsbt as WasmBitGoPsbt } from "../wasm/wasm_utxo.js";
2
+ import { RootWalletKeys } from "./RootWalletKeys.js";
3
+ import { ReplayProtection } from "./ReplayProtection.js";
4
+ import { BIP32 } from "../bip32.js";
5
+ import { ECPair } from "../ecpair.js";
24
6
  export class BitGoPsbt {
25
7
  wasm;
26
8
  constructor(wasm) {
@@ -50,7 +32,9 @@ export class BitGoPsbt {
50
32
  * @returns Parsed transaction information
51
33
  */
52
34
  parseTransactionWithWalletKeys(walletKeys, replayProtection) {
53
- return this.wasm.parse_transaction_with_wallet_keys(walletKeys, replayProtection);
35
+ const keys = RootWalletKeys.from(walletKeys);
36
+ const rp = ReplayProtection.from(replayProtection, this.wasm.network());
37
+ return this.wasm.parse_transaction_with_wallet_keys(keys.wasm, rp.wasm);
54
38
  }
55
39
  /**
56
40
  * Parse outputs with wallet keys to identify which outputs belong to a wallet
@@ -64,26 +48,57 @@ export class BitGoPsbt {
64
48
  * @note This method does NOT validate wallet inputs. It only parses outputs.
65
49
  */
66
50
  parseOutputsWithWalletKeys(walletKeys) {
67
- return this.wasm.parse_outputs_with_wallet_keys(walletKeys);
51
+ const keys = RootWalletKeys.from(walletKeys);
52
+ return this.wasm.parse_outputs_with_wallet_keys(keys.wasm);
68
53
  }
69
54
  /**
70
- * Verify if a valid signature exists for a given extended public key at the specified input index.
55
+ * Verify if a valid signature exists for a given key at the specified input index.
56
+ *
57
+ * This method can verify signatures using either:
58
+ * - Extended public key (xpub): Derives the public key using the derivation path from PSBT
59
+ * - ECPair (private key): Extracts the public key and verifies directly
71
60
  *
72
- * This method derives the public key from the xpub using the derivation path found in the
73
- * PSBT input, then verifies the signature. It supports:
61
+ * When using xpub, it supports:
74
62
  * - ECDSA signatures (for legacy/SegWit inputs)
75
63
  * - Schnorr signatures (for Taproot script path inputs)
76
64
  * - MuSig2 partial signatures (for Taproot keypath MuSig2 inputs)
77
65
  *
66
+ * When using ECPair, it supports:
67
+ * - ECDSA signatures (for legacy/SegWit inputs)
68
+ * - Schnorr signatures (for Taproot script path inputs)
69
+ * Note: MuSig2 inputs require xpubs for derivation
70
+ *
78
71
  * @param inputIndex - The index of the input to check (0-based)
79
- * @param xpub - The extended public key as a base58-encoded string
72
+ * @param key - Either an extended public key (base58 string, BIP32 instance, or WasmBIP32) or an ECPair (private key Buffer, ECPair instance, or WasmECPair)
80
73
  * @returns true if a valid signature exists, false if no signature exists
81
- * @throws Error if input index is out of bounds, xpub is invalid, or verification fails
74
+ * @throws Error if input index is out of bounds, key is invalid, or verification fails
75
+ *
76
+ * @example
77
+ * ```typescript
78
+ * // Verify wallet input signature with xpub
79
+ * const hasUserSig = psbt.verifySignature(0, userXpub);
80
+ *
81
+ * // Verify signature with ECPair (private key)
82
+ * const ecpair = ECPair.fromPrivateKey(privateKeyBuffer);
83
+ * const hasReplaySig = psbt.verifySignature(1, ecpair);
84
+ *
85
+ * // Or pass private key directly
86
+ * const hasReplaySig2 = psbt.verifySignature(1, privateKeyBuffer);
87
+ * ```
82
88
  */
83
- verifySignature(inputIndex, xpub) {
84
- return this.wasm.verify_signature(inputIndex, xpub);
89
+ verifySignature(inputIndex, key) {
90
+ // Try to parse as BIP32Arg first (string or BIP32 instance)
91
+ if (typeof key === "string" || ("derive" in key && typeof key.derive === "function")) {
92
+ const wasmKey = BIP32.from(key).wasm;
93
+ return this.wasm.verify_signature_with_xpub(inputIndex, wasmKey);
94
+ }
95
+ // Otherwise it's an ECPairArg (Uint8Array, ECPair, or WasmECPair)
96
+ const wasmECPair = ECPair.from(key).wasm;
97
+ return this.wasm.verify_signature_with_pub(inputIndex, wasmECPair);
85
98
  }
86
99
  /**
100
+ * @deprecated - use verifySignature with the replay protection key instead
101
+ *
87
102
  * Verify if a replay protection input has a valid signature.
88
103
  *
89
104
  * This method checks if a given input is a replay protection input (like P2shP2pk) and verifies
@@ -102,7 +117,8 @@ export class BitGoPsbt {
102
117
  * @throws Error if the input is not a replay protection input, index is out of bounds, or scripts are invalid
103
118
  */
104
119
  verifyReplayProtectionSignature(inputIndex, replayProtection) {
105
- return this.wasm.verify_replay_protection_signature(inputIndex, replayProtection);
120
+ const rp = ReplayProtection.from(replayProtection, this.wasm.network());
121
+ return this.wasm.verify_replay_protection_signature(inputIndex, rp.wasm);
106
122
  }
107
123
  /**
108
124
  * Serialize the PSBT to bytes
@@ -0,0 +1,58 @@
1
+ import { WasmReplayProtection } from "../wasm/wasm_utxo.js";
2
+ import { type ECPairArg } from "../ecpair.js";
3
+ /**
4
+ * ReplayProtectionArg represents the various forms that replay protection can take
5
+ * before being converted to a WasmReplayProtection instance
6
+ */
7
+ export type ReplayProtectionArg = ReplayProtection | WasmReplayProtection | {
8
+ publicKeys: ECPairArg[];
9
+ } | {
10
+ /** @deprecated - use publicKeys instead */
11
+ outputScripts: Uint8Array[];
12
+ } | {
13
+ /** @deprecated - use publicKeys instead */
14
+ addresses: string[];
15
+ };
16
+ /**
17
+ * ReplayProtection wrapper class for PSBT replay protection inputs
18
+ */
19
+ export declare class ReplayProtection {
20
+ private _wasm;
21
+ private constructor();
22
+ /**
23
+ * Create a ReplayProtection instance from a WasmReplayProtection instance (internal use)
24
+ * @internal
25
+ */
26
+ static fromWasm(wasm: WasmReplayProtection): ReplayProtection;
27
+ /**
28
+ * Convert ReplayProtectionArg to ReplayProtection instance
29
+ * @param arg - The replay protection in various formats
30
+ * @param network - Optional network string (required for addresses variant)
31
+ * @returns ReplayProtection instance
32
+ */
33
+ static from(arg: ReplayProtectionArg, network?: string): ReplayProtection;
34
+ /**
35
+ * Create from public keys (derives P2SH-P2PK output scripts)
36
+ * @param publicKeys - Array of ECPair instances or arguments
37
+ * @returns ReplayProtection instance
38
+ */
39
+ static fromPublicKeys(publicKeys: ECPairArg[]): ReplayProtection;
40
+ /**
41
+ * Create from output scripts
42
+ * @param outputScripts - Array of output script buffers
43
+ * @returns ReplayProtection instance
44
+ */
45
+ static fromOutputScripts(outputScripts: Uint8Array[]): ReplayProtection;
46
+ /**
47
+ * Create from addresses
48
+ * @param addresses - Array of address strings
49
+ * @param network - Network string (e.g., "bitcoin", "testnet", "btc", "tbtc")
50
+ * @returns ReplayProtection instance
51
+ */
52
+ static fromAddresses(addresses: string[], network: string): ReplayProtection;
53
+ /**
54
+ * Get the underlying WASM instance (internal use only)
55
+ * @internal
56
+ */
57
+ get wasm(): WasmReplayProtection;
58
+ }