@bitgo/wasm-utxo 1.6.0 → 1.8.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/dist/cjs/js/bip32.d.ts +140 -0
- package/dist/cjs/js/bip32.js +177 -0
- package/dist/cjs/js/ecpair.d.ts +96 -0
- package/dist/cjs/js/ecpair.js +134 -0
- package/dist/cjs/js/fixedScriptWallet/BitGoPsbt.d.ts +242 -0
- package/dist/cjs/js/fixedScriptWallet/BitGoPsbt.js +274 -0
- package/dist/cjs/js/fixedScriptWallet/ReplayProtection.d.ts +58 -0
- package/dist/cjs/js/fixedScriptWallet/ReplayProtection.js +89 -0
- package/dist/cjs/js/fixedScriptWallet/RootWalletKeys.d.ts +66 -0
- package/dist/cjs/js/fixedScriptWallet/RootWalletKeys.js +108 -0
- package/dist/cjs/js/fixedScriptWallet/address.d.ts +20 -0
- package/dist/cjs/js/fixedScriptWallet/address.js +29 -0
- package/dist/cjs/js/fixedScriptWallet/index.d.ts +4 -0
- package/dist/cjs/js/fixedScriptWallet/index.js +12 -0
- package/dist/cjs/js/index.d.ts +5 -1
- package/dist/cjs/js/index.js +11 -2
- package/dist/cjs/js/utxolibCompat.d.ts +0 -18
- package/dist/cjs/js/wasm/wasm_utxo.d.ts +333 -15
- package/dist/cjs/js/wasm/wasm_utxo.js +1311 -201
- package/dist/cjs/js/wasm/wasm_utxo_bg.wasm +0 -0
- package/dist/cjs/js/wasm/wasm_utxo_bg.wasm.d.ts +64 -15
- package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
- package/dist/esm/js/bip32.d.ts +140 -0
- package/dist/esm/js/bip32.js +173 -0
- package/dist/esm/js/ecpair.d.ts +96 -0
- package/dist/esm/js/ecpair.js +130 -0
- package/dist/esm/js/fixedScriptWallet/BitGoPsbt.d.ts +242 -0
- package/dist/esm/js/fixedScriptWallet/BitGoPsbt.js +270 -0
- package/dist/esm/js/fixedScriptWallet/ReplayProtection.d.ts +58 -0
- package/dist/esm/js/fixedScriptWallet/ReplayProtection.js +85 -0
- package/dist/esm/js/fixedScriptWallet/RootWalletKeys.d.ts +66 -0
- package/dist/esm/js/fixedScriptWallet/RootWalletKeys.js +104 -0
- package/dist/esm/js/fixedScriptWallet/address.d.ts +20 -0
- package/dist/esm/js/fixedScriptWallet/address.js +25 -0
- package/dist/esm/js/fixedScriptWallet/index.d.ts +4 -0
- package/dist/esm/js/fixedScriptWallet/index.js +4 -0
- package/dist/esm/js/index.d.ts +5 -1
- package/dist/esm/js/index.js +8 -1
- package/dist/esm/js/utxolibCompat.d.ts +0 -18
- package/dist/esm/js/wasm/wasm_utxo.d.ts +333 -15
- package/dist/esm/js/wasm/wasm_utxo_bg.js +1301 -199
- package/dist/esm/js/wasm/wasm_utxo_bg.wasm +0 -0
- package/dist/esm/js/wasm/wasm_utxo_bg.wasm.d.ts +64 -15
- package/dist/esm/test/bip32.js +242 -0
- package/dist/esm/test/ecpair.d.ts +1 -0
- package/dist/esm/test/ecpair.js +137 -0
- package/dist/esm/test/fixedScript/fixtureUtil.d.ts +12 -2
- package/dist/esm/test/fixedScript/fixtureUtil.js +28 -7
- package/dist/esm/test/fixedScript/musig2Nonces.d.ts +1 -0
- package/dist/esm/test/fixedScript/musig2Nonces.js +77 -0
- package/dist/esm/test/fixedScript/parseTransactionWithWalletKeys.js +7 -7
- package/dist/esm/test/fixedScript/signAndVerifySignature.d.ts +1 -0
- package/dist/esm/test/fixedScript/signAndVerifySignature.js +268 -0
- package/dist/esm/test/fixedScript/walletKeys.util.d.ts +12 -0
- package/dist/esm/test/fixedScript/walletKeys.util.js +17 -0
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/dist/cjs/js/fixedScriptWallet.d.ts +0 -146
- package/dist/cjs/js/fixedScriptWallet.js +0 -138
- package/dist/esm/js/fixedScriptWallet.d.ts +0 -146
- package/dist/esm/js/fixedScriptWallet.js +0 -132
- package/dist/esm/test/fixedScript/verifySignature.js +0 -141
- /package/dist/esm/test/{fixedScript/verifySignature.d.ts → bip32.d.ts} +0 -0
|
@@ -0,0 +1,270 @@
|
|
|
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";
|
|
6
|
+
export class BitGoPsbt {
|
|
7
|
+
wasm;
|
|
8
|
+
constructor(wasm) {
|
|
9
|
+
this.wasm = wasm;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Deserialize a PSBT from bytes
|
|
13
|
+
* @param bytes - The PSBT bytes
|
|
14
|
+
* @param network - The network to use for deserialization (either utxolib name like "bitcoin" or coin name like "btc")
|
|
15
|
+
* @returns A BitGoPsbt instance
|
|
16
|
+
*/
|
|
17
|
+
static fromBytes(bytes, network) {
|
|
18
|
+
const wasm = WasmBitGoPsbt.from_bytes(bytes, network);
|
|
19
|
+
return new BitGoPsbt(wasm);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Get the unsigned transaction ID
|
|
23
|
+
* @returns The unsigned transaction ID
|
|
24
|
+
*/
|
|
25
|
+
unsignedTxid() {
|
|
26
|
+
return this.wasm.unsigned_txid();
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Parse transaction with wallet keys to identify wallet inputs/outputs
|
|
30
|
+
* @param walletKeys - The wallet keys to use for identification
|
|
31
|
+
* @param replayProtection - Scripts that are allowed as inputs without wallet validation
|
|
32
|
+
* @returns Parsed transaction information
|
|
33
|
+
*/
|
|
34
|
+
parseTransactionWithWalletKeys(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);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Parse outputs with wallet keys to identify which outputs belong to a wallet
|
|
41
|
+
* with the given wallet keys.
|
|
42
|
+
*
|
|
43
|
+
* This is useful in cases where we want to identify outputs that belong to a different
|
|
44
|
+
* wallet than the inputs.
|
|
45
|
+
*
|
|
46
|
+
* @param walletKeys - The wallet keys to use for identification
|
|
47
|
+
* @returns Array of parsed outputs
|
|
48
|
+
* @note This method does NOT validate wallet inputs. It only parses outputs.
|
|
49
|
+
*/
|
|
50
|
+
parseOutputsWithWalletKeys(walletKeys) {
|
|
51
|
+
const keys = RootWalletKeys.from(walletKeys);
|
|
52
|
+
return this.wasm.parse_outputs_with_wallet_keys(keys.wasm);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
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
|
|
60
|
+
*
|
|
61
|
+
* When using xpub, it supports:
|
|
62
|
+
* - ECDSA signatures (for legacy/SegWit inputs)
|
|
63
|
+
* - Schnorr signatures (for Taproot script path inputs)
|
|
64
|
+
* - MuSig2 partial signatures (for Taproot keypath MuSig2 inputs)
|
|
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
|
+
*
|
|
71
|
+
* @param inputIndex - The index of the input to check (0-based)
|
|
72
|
+
* @param key - Either an extended public key (base58 string, BIP32 instance, or WasmBIP32) or an ECPair (private key Buffer, ECPair instance, or WasmECPair)
|
|
73
|
+
* @returns true if a valid signature exists, false if no signature exists
|
|
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
|
+
* ```
|
|
88
|
+
*/
|
|
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);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Sign a single input with a private key
|
|
101
|
+
*
|
|
102
|
+
* This method signs a specific input using the provided key. It accepts either:
|
|
103
|
+
* - An xpriv (BIP32Arg: base58 string, BIP32 instance, or WasmBIP32) for wallet inputs - derives the key and signs
|
|
104
|
+
* - A raw privkey (ECPairArg: Buffer, ECPair instance, or WasmECPair) for replay protection inputs - signs directly
|
|
105
|
+
*
|
|
106
|
+
* This method automatically detects and handles different input types:
|
|
107
|
+
* - For regular inputs: uses standard PSBT signing
|
|
108
|
+
* - For MuSig2 inputs: uses the FirstRound state stored by generateMusig2Nonces()
|
|
109
|
+
* - For replay protection inputs: signs with legacy P2SH sighash
|
|
110
|
+
*
|
|
111
|
+
* @param inputIndex - The index of the input to sign (0-based)
|
|
112
|
+
* @param key - Either an xpriv (BIP32Arg) or a raw privkey (ECPairArg)
|
|
113
|
+
* @throws Error if signing fails, or if generateMusig2Nonces() was not called first for MuSig2 inputs
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```typescript
|
|
117
|
+
* // Parse transaction to identify input types
|
|
118
|
+
* const parsed = psbt.parseTransactionWithWalletKeys(walletKeys, replayProtection);
|
|
119
|
+
*
|
|
120
|
+
* // Sign regular wallet inputs with xpriv
|
|
121
|
+
* for (let i = 0; i < parsed.inputs.length; i++) {
|
|
122
|
+
* const input = parsed.inputs[i];
|
|
123
|
+
* if (input.scriptId !== null && input.scriptType !== "p2shP2pk") {
|
|
124
|
+
* psbt.sign(i, userXpriv);
|
|
125
|
+
* }
|
|
126
|
+
* }
|
|
127
|
+
*
|
|
128
|
+
* // Sign replay protection inputs with raw privkey
|
|
129
|
+
* const userPrivkey = bip32.fromBase58(userXpriv).privateKey!;
|
|
130
|
+
* for (let i = 0; i < parsed.inputs.length; i++) {
|
|
131
|
+
* const input = parsed.inputs[i];
|
|
132
|
+
* if (input.scriptType === "p2shP2pk") {
|
|
133
|
+
* psbt.sign(i, userPrivkey);
|
|
134
|
+
* }
|
|
135
|
+
* }
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
sign(inputIndex, key) {
|
|
139
|
+
// Detect key type
|
|
140
|
+
// If string or has 'derive' method → BIP32Arg
|
|
141
|
+
// Otherwise → ECPairArg
|
|
142
|
+
if (typeof key === "string" ||
|
|
143
|
+
(typeof key === "object" &&
|
|
144
|
+
key !== null &&
|
|
145
|
+
"derive" in key &&
|
|
146
|
+
typeof key.derive === "function")) {
|
|
147
|
+
// It's a BIP32Arg
|
|
148
|
+
const wasmKey = BIP32.from(key);
|
|
149
|
+
this.wasm.sign_with_xpriv(inputIndex, wasmKey.wasm);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
// It's an ECPairArg
|
|
153
|
+
const wasmKey = ECPair.from(key);
|
|
154
|
+
this.wasm.sign_with_privkey(inputIndex, wasmKey.wasm);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* @deprecated - use verifySignature with the replay protection key instead
|
|
159
|
+
*
|
|
160
|
+
* Verify if a replay protection input has a valid signature.
|
|
161
|
+
*
|
|
162
|
+
* This method checks if a given input is a replay protection input (like P2shP2pk) and verifies
|
|
163
|
+
* the signature. Replay protection inputs don't use standard derivation paths, so this method
|
|
164
|
+
* verifies signatures without deriving from xpub.
|
|
165
|
+
*
|
|
166
|
+
* For P2PK replay protection inputs, this:
|
|
167
|
+
* - Extracts the signature from final_script_sig
|
|
168
|
+
* - Extracts the public key from redeem_script
|
|
169
|
+
* - Computes the legacy P2SH sighash
|
|
170
|
+
* - Verifies the ECDSA signature cryptographically
|
|
171
|
+
*
|
|
172
|
+
* @param inputIndex - The index of the input to check (0-based)
|
|
173
|
+
* @param replayProtection - Scripts that identify replay protection inputs (same format as parseTransactionWithWalletKeys)
|
|
174
|
+
* @returns true if the input is a replay protection input and has a valid signature, false if no valid signature
|
|
175
|
+
* @throws Error if the input is not a replay protection input, index is out of bounds, or scripts are invalid
|
|
176
|
+
*/
|
|
177
|
+
verifyReplayProtectionSignature(inputIndex, replayProtection) {
|
|
178
|
+
const rp = ReplayProtection.from(replayProtection, this.wasm.network());
|
|
179
|
+
return this.wasm.verify_replay_protection_signature(inputIndex, rp.wasm);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Serialize the PSBT to bytes
|
|
183
|
+
*
|
|
184
|
+
* @returns The serialized PSBT as a byte array
|
|
185
|
+
*/
|
|
186
|
+
serialize() {
|
|
187
|
+
return this.wasm.serialize();
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Generate and store MuSig2 nonces for all MuSig2 inputs
|
|
191
|
+
*
|
|
192
|
+
* This method generates nonces using the State-Machine API and stores them in the PSBT.
|
|
193
|
+
* The nonces are stored as proprietary fields in the PSBT and will be included when serialized.
|
|
194
|
+
* After ALL participants have generated their nonces, you can sign MuSig2 inputs using
|
|
195
|
+
* sign().
|
|
196
|
+
*
|
|
197
|
+
* @param key - The extended private key (xpriv) for signing. Can be a base58 string, BIP32 instance, or WasmBIP32
|
|
198
|
+
* @param sessionId - Optional 32-byte session ID for nonce generation. **Only allowed on testnets**.
|
|
199
|
+
* On mainnets, a secure random session ID is always generated automatically.
|
|
200
|
+
* Must be unique per signing session.
|
|
201
|
+
* @throws Error if nonce generation fails, sessionId length is invalid, or custom sessionId is
|
|
202
|
+
* provided on a mainnet (security restriction)
|
|
203
|
+
*
|
|
204
|
+
* @security The sessionId MUST be cryptographically random and unique for each signing session.
|
|
205
|
+
* Never reuse a sessionId with the same key! On mainnets, sessionId is always randomly
|
|
206
|
+
* generated for security. Custom sessionId is only allowed on testnets for testing purposes.
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```typescript
|
|
210
|
+
* // Phase 1: Both parties generate nonces (with auto-generated session ID)
|
|
211
|
+
* psbt.generateMusig2Nonces(userXpriv);
|
|
212
|
+
* // Nonces are stored in the PSBT
|
|
213
|
+
* // Send PSBT to counterparty
|
|
214
|
+
*
|
|
215
|
+
* // Phase 2: After receiving counterparty PSBT with their nonces
|
|
216
|
+
* const counterpartyPsbt = BitGoPsbt.fromBytes(counterpartyPsbtBytes, network);
|
|
217
|
+
* psbt.combineMusig2Nonces(counterpartyPsbt);
|
|
218
|
+
* // Sign MuSig2 key path inputs
|
|
219
|
+
* const parsed = psbt.parseTransactionWithWalletKeys(walletKeys, replayProtection);
|
|
220
|
+
* for (let i = 0; i < parsed.inputs.length; i++) {
|
|
221
|
+
* if (parsed.inputs[i].scriptType === "p2trMusig2KeyPath") {
|
|
222
|
+
* psbt.sign(i, userXpriv);
|
|
223
|
+
* }
|
|
224
|
+
* }
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
227
|
+
generateMusig2Nonces(key, sessionId) {
|
|
228
|
+
const wasmKey = BIP32.from(key);
|
|
229
|
+
this.wasm.generate_musig2_nonces(wasmKey.wasm, sessionId);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Combine/merge data from another PSBT into this one
|
|
233
|
+
*
|
|
234
|
+
* This method copies MuSig2 nonces and signatures (proprietary key-value pairs) from the
|
|
235
|
+
* source PSBT to this PSBT. This is useful for merging PSBTs during the nonce exchange
|
|
236
|
+
* and signature collection phases.
|
|
237
|
+
*
|
|
238
|
+
* @param sourcePsbt - The source PSBT containing data to merge
|
|
239
|
+
* @throws Error if networks don't match
|
|
240
|
+
*
|
|
241
|
+
* @example
|
|
242
|
+
* ```typescript
|
|
243
|
+
* // After receiving counterparty's PSBT with their nonces
|
|
244
|
+
* const counterpartyPsbt = BitGoPsbt.fromBytes(counterpartyPsbtBytes, network);
|
|
245
|
+
* psbt.combineMusig2Nonces(counterpartyPsbt);
|
|
246
|
+
* // Now can sign with all nonces present
|
|
247
|
+
* psbt.sign(0, userXpriv);
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
combineMusig2Nonces(sourcePsbt) {
|
|
251
|
+
this.wasm.combine_musig2_nonces(sourcePsbt.wasm);
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Finalize all inputs in the PSBT
|
|
255
|
+
*
|
|
256
|
+
* @throws Error if any input failed to finalize
|
|
257
|
+
*/
|
|
258
|
+
finalizeAllInputs() {
|
|
259
|
+
this.wasm.finalize_all_inputs();
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Extract the final transaction from a finalized PSBT
|
|
263
|
+
*
|
|
264
|
+
* @returns The serialized transaction bytes
|
|
265
|
+
* @throws Error if the PSBT is not fully finalized or extraction fails
|
|
266
|
+
*/
|
|
267
|
+
extractTransaction() {
|
|
268
|
+
return this.wasm.extract_transaction();
|
|
269
|
+
}
|
|
270
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { WasmReplayProtection } from "../wasm/wasm_utxo.js";
|
|
2
|
+
import { ECPair } from "../ecpair.js";
|
|
3
|
+
/**
|
|
4
|
+
* ReplayProtection wrapper class for PSBT replay protection inputs
|
|
5
|
+
*/
|
|
6
|
+
export class ReplayProtection {
|
|
7
|
+
_wasm;
|
|
8
|
+
constructor(_wasm) {
|
|
9
|
+
this._wasm = _wasm;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Create a ReplayProtection instance from a WasmReplayProtection instance (internal use)
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
static fromWasm(wasm) {
|
|
16
|
+
return new ReplayProtection(wasm);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Convert ReplayProtectionArg to ReplayProtection instance
|
|
20
|
+
* @param arg - The replay protection in various formats
|
|
21
|
+
* @param network - Optional network string (required for addresses variant)
|
|
22
|
+
* @returns ReplayProtection instance
|
|
23
|
+
*/
|
|
24
|
+
static from(arg, network) {
|
|
25
|
+
// Short-circuit if already a ReplayProtection instance
|
|
26
|
+
if (arg instanceof ReplayProtection) {
|
|
27
|
+
return arg;
|
|
28
|
+
}
|
|
29
|
+
// If it's a WasmReplayProtection instance, wrap it
|
|
30
|
+
if (arg instanceof WasmReplayProtection) {
|
|
31
|
+
return new ReplayProtection(arg);
|
|
32
|
+
}
|
|
33
|
+
// Handle object variants
|
|
34
|
+
if ("publicKeys" in arg) {
|
|
35
|
+
// Convert ECPairArg to public key bytes
|
|
36
|
+
const publicKeyBytes = arg.publicKeys.map((key) => ECPair.from(key).publicKey);
|
|
37
|
+
const wasm = WasmReplayProtection.from_public_keys(publicKeyBytes);
|
|
38
|
+
return new ReplayProtection(wasm);
|
|
39
|
+
}
|
|
40
|
+
if ("outputScripts" in arg) {
|
|
41
|
+
const wasm = WasmReplayProtection.from_output_scripts(arg.outputScripts);
|
|
42
|
+
return new ReplayProtection(wasm);
|
|
43
|
+
}
|
|
44
|
+
if ("addresses" in arg) {
|
|
45
|
+
if (!network) {
|
|
46
|
+
throw new Error("Network is required when using addresses variant");
|
|
47
|
+
}
|
|
48
|
+
const wasm = WasmReplayProtection.from_addresses(arg.addresses, network);
|
|
49
|
+
return new ReplayProtection(wasm);
|
|
50
|
+
}
|
|
51
|
+
throw new Error("Invalid ReplayProtectionArg type");
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Create from public keys (derives P2SH-P2PK output scripts)
|
|
55
|
+
* @param publicKeys - Array of ECPair instances or arguments
|
|
56
|
+
* @returns ReplayProtection instance
|
|
57
|
+
*/
|
|
58
|
+
static fromPublicKeys(publicKeys) {
|
|
59
|
+
return ReplayProtection.from({ publicKeys });
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Create from output scripts
|
|
63
|
+
* @param outputScripts - Array of output script buffers
|
|
64
|
+
* @returns ReplayProtection instance
|
|
65
|
+
*/
|
|
66
|
+
static fromOutputScripts(outputScripts) {
|
|
67
|
+
return ReplayProtection.from({ outputScripts });
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Create from addresses
|
|
71
|
+
* @param addresses - Array of address strings
|
|
72
|
+
* @param network - Network string (e.g., "bitcoin", "testnet", "btc", "tbtc")
|
|
73
|
+
* @returns ReplayProtection instance
|
|
74
|
+
*/
|
|
75
|
+
static fromAddresses(addresses, network) {
|
|
76
|
+
return ReplayProtection.from({ addresses }, network);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Get the underlying WASM instance (internal use only)
|
|
80
|
+
* @internal
|
|
81
|
+
*/
|
|
82
|
+
get wasm() {
|
|
83
|
+
return this._wasm;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { BIP32Interface } from "../bip32.js";
|
|
2
|
+
import { BIP32 } from "../bip32.js";
|
|
3
|
+
import { Triple } from "../triple.js";
|
|
4
|
+
import { WasmRootWalletKeys } from "../wasm/wasm_utxo.js";
|
|
5
|
+
/**
|
|
6
|
+
* IWalletKeys represents the various forms that wallet keys can take
|
|
7
|
+
* before being converted to a RootWalletKeys instance
|
|
8
|
+
*/
|
|
9
|
+
export type IWalletKeys = {
|
|
10
|
+
triple: Triple<BIP32Interface>;
|
|
11
|
+
derivationPrefixes: Triple<string>;
|
|
12
|
+
};
|
|
13
|
+
export type WalletKeysArg =
|
|
14
|
+
/** Just an xpub triple, will assume default derivation prefixes */
|
|
15
|
+
Triple<string>
|
|
16
|
+
/** Compatible with utxolib RootWalletKeys */
|
|
17
|
+
| IWalletKeys
|
|
18
|
+
/** RootWalletKeys instance */
|
|
19
|
+
| RootWalletKeys;
|
|
20
|
+
/**
|
|
21
|
+
* RootWalletKeys represents a set of three extended public keys with their derivation prefixes
|
|
22
|
+
*/
|
|
23
|
+
export declare class RootWalletKeys {
|
|
24
|
+
private _wasm;
|
|
25
|
+
private constructor();
|
|
26
|
+
/**
|
|
27
|
+
* Create a RootWalletKeys from various input formats
|
|
28
|
+
* @param keys - Can be a triple of xpub strings, an IWalletKeys object, or another RootWalletKeys instance
|
|
29
|
+
* @returns A RootWalletKeys instance
|
|
30
|
+
*/
|
|
31
|
+
static from(keys: WalletKeysArg): RootWalletKeys;
|
|
32
|
+
/**
|
|
33
|
+
* Create a RootWalletKeys from three xpub strings
|
|
34
|
+
* Uses default derivation prefix of m/0/0 for all three keys
|
|
35
|
+
* @param xpubs - Triple of xpub strings
|
|
36
|
+
* @returns A RootWalletKeys instance
|
|
37
|
+
*/
|
|
38
|
+
static fromXpubs(xpubs: Triple<string>): RootWalletKeys;
|
|
39
|
+
/**
|
|
40
|
+
* Create a RootWalletKeys from three xpub strings with custom derivation prefixes
|
|
41
|
+
* @param xpubs - Triple of xpub strings
|
|
42
|
+
* @param derivationPrefixes - Triple of derivation path strings (e.g., ["0/0", "0/0", "0/0"])
|
|
43
|
+
* @returns A RootWalletKeys instance
|
|
44
|
+
*/
|
|
45
|
+
static withDerivationPrefixes(xpubs: Triple<string>, derivationPrefixes: Triple<string>): RootWalletKeys;
|
|
46
|
+
/**
|
|
47
|
+
* Get the user key (first xpub)
|
|
48
|
+
* @returns The user key as a BIP32 instance
|
|
49
|
+
*/
|
|
50
|
+
userKey(): BIP32;
|
|
51
|
+
/**
|
|
52
|
+
* Get the backup key (second xpub)
|
|
53
|
+
* @returns The backup key as a BIP32 instance
|
|
54
|
+
*/
|
|
55
|
+
backupKey(): BIP32;
|
|
56
|
+
/**
|
|
57
|
+
* Get the BitGo key (third xpub)
|
|
58
|
+
* @returns The BitGo key as a BIP32 instance
|
|
59
|
+
*/
|
|
60
|
+
bitgoKey(): BIP32;
|
|
61
|
+
/**
|
|
62
|
+
* Get the underlying WASM instance (internal use only)
|
|
63
|
+
* @internal
|
|
64
|
+
*/
|
|
65
|
+
get wasm(): WasmRootWalletKeys;
|
|
66
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { BIP32 } from "../bip32.js";
|
|
2
|
+
import { WasmRootWalletKeys, WasmBIP32 } from "../wasm/wasm_utxo.js";
|
|
3
|
+
/**
|
|
4
|
+
* Convert WalletKeysArg to a triple of BIP32 instances
|
|
5
|
+
*/
|
|
6
|
+
function toBIP32Triple(keys) {
|
|
7
|
+
if (keys instanceof RootWalletKeys) {
|
|
8
|
+
return [keys.userKey(), keys.backupKey(), keys.bitgoKey()];
|
|
9
|
+
}
|
|
10
|
+
// Check if it's an IWalletKeys object
|
|
11
|
+
if (typeof keys === "object" && "triple" in keys) {
|
|
12
|
+
// Extract BIP32 keys from the triple
|
|
13
|
+
return keys.triple.map((key) => BIP32.from(key));
|
|
14
|
+
}
|
|
15
|
+
// Otherwise it's a triple of strings (xpubs)
|
|
16
|
+
return keys.map((xpub) => BIP32.fromWasm(WasmBIP32.from_xpub(xpub)));
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Extract derivation prefixes from WalletKeysArg, if present
|
|
20
|
+
*/
|
|
21
|
+
function extractDerivationPrefixes(keys) {
|
|
22
|
+
if (typeof keys === "object" && "derivationPrefixes" in keys) {
|
|
23
|
+
return keys.derivationPrefixes;
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* RootWalletKeys represents a set of three extended public keys with their derivation prefixes
|
|
29
|
+
*/
|
|
30
|
+
export class RootWalletKeys {
|
|
31
|
+
_wasm;
|
|
32
|
+
constructor(_wasm) {
|
|
33
|
+
this._wasm = _wasm;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Create a RootWalletKeys from various input formats
|
|
37
|
+
* @param keys - Can be a triple of xpub strings, an IWalletKeys object, or another RootWalletKeys instance
|
|
38
|
+
* @returns A RootWalletKeys instance
|
|
39
|
+
*/
|
|
40
|
+
static from(keys) {
|
|
41
|
+
if (keys instanceof RootWalletKeys) {
|
|
42
|
+
return keys;
|
|
43
|
+
}
|
|
44
|
+
const [user, backup, bitgo] = toBIP32Triple(keys);
|
|
45
|
+
const derivationPrefixes = extractDerivationPrefixes(keys);
|
|
46
|
+
const wasm = derivationPrefixes
|
|
47
|
+
? WasmRootWalletKeys.with_derivation_prefixes(user.wasm, backup.wasm, bitgo.wasm, derivationPrefixes[0], derivationPrefixes[1], derivationPrefixes[2])
|
|
48
|
+
: new WasmRootWalletKeys(user.wasm, backup.wasm, bitgo.wasm);
|
|
49
|
+
return new RootWalletKeys(wasm);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Create a RootWalletKeys from three xpub strings
|
|
53
|
+
* Uses default derivation prefix of m/0/0 for all three keys
|
|
54
|
+
* @param xpubs - Triple of xpub strings
|
|
55
|
+
* @returns A RootWalletKeys instance
|
|
56
|
+
*/
|
|
57
|
+
static fromXpubs(xpubs) {
|
|
58
|
+
const [user, backup, bitgo] = xpubs.map((xpub) => WasmBIP32.from_xpub(xpub));
|
|
59
|
+
const wasm = new WasmRootWalletKeys(user, backup, bitgo);
|
|
60
|
+
return new RootWalletKeys(wasm);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Create a RootWalletKeys from three xpub strings with custom derivation prefixes
|
|
64
|
+
* @param xpubs - Triple of xpub strings
|
|
65
|
+
* @param derivationPrefixes - Triple of derivation path strings (e.g., ["0/0", "0/0", "0/0"])
|
|
66
|
+
* @returns A RootWalletKeys instance
|
|
67
|
+
*/
|
|
68
|
+
static withDerivationPrefixes(xpubs, derivationPrefixes) {
|
|
69
|
+
const [user, backup, bitgo] = xpubs.map((xpub) => WasmBIP32.from_xpub(xpub));
|
|
70
|
+
const wasm = WasmRootWalletKeys.with_derivation_prefixes(user, backup, bitgo, derivationPrefixes[0], derivationPrefixes[1], derivationPrefixes[2]);
|
|
71
|
+
return new RootWalletKeys(wasm);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Get the user key (first xpub)
|
|
75
|
+
* @returns The user key as a BIP32 instance
|
|
76
|
+
*/
|
|
77
|
+
userKey() {
|
|
78
|
+
const wasm = this._wasm.user_key();
|
|
79
|
+
return BIP32.fromWasm(wasm);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get the backup key (second xpub)
|
|
83
|
+
* @returns The backup key as a BIP32 instance
|
|
84
|
+
*/
|
|
85
|
+
backupKey() {
|
|
86
|
+
const wasm = this._wasm.backup_key();
|
|
87
|
+
return BIP32.fromWasm(wasm);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Get the BitGo key (third xpub)
|
|
91
|
+
* @returns The BitGo key as a BIP32 instance
|
|
92
|
+
*/
|
|
93
|
+
bitgoKey() {
|
|
94
|
+
const wasm = this._wasm.bitgo_key();
|
|
95
|
+
return BIP32.fromWasm(wasm);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Get the underlying WASM instance (internal use only)
|
|
99
|
+
* @internal
|
|
100
|
+
*/
|
|
101
|
+
get wasm() {
|
|
102
|
+
return this._wasm;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type WalletKeysArg } from "./RootWalletKeys.js";
|
|
2
|
+
import type { UtxolibNetwork } from "../utxolibCompat.js";
|
|
3
|
+
import { AddressFormat } from "../address.js";
|
|
4
|
+
/**
|
|
5
|
+
* Create the output script for a given wallet keys and chain and index
|
|
6
|
+
*/
|
|
7
|
+
export declare function outputScript(keys: WalletKeysArg, chain: number, index: number, network: UtxolibNetwork): Uint8Array;
|
|
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 declare function address(keys: WalletKeysArg, chain: number, index: number, network: UtxolibNetwork, addressFormat?: AddressFormat): string;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { FixedScriptWalletNamespace } from "../wasm/wasm_utxo.js";
|
|
2
|
+
import { RootWalletKeys } from "./RootWalletKeys.js";
|
|
3
|
+
/**
|
|
4
|
+
* Create the output script for a given wallet keys and chain and index
|
|
5
|
+
*/
|
|
6
|
+
export function outputScript(keys, chain, index, network) {
|
|
7
|
+
const walletKeys = RootWalletKeys.from(keys);
|
|
8
|
+
return FixedScriptWalletNamespace.output_script(walletKeys.wasm, chain, index, network);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Create the address for a given wallet keys and chain and index and network.
|
|
12
|
+
* Wrapper for outputScript that also encodes the script to an address.
|
|
13
|
+
* @param keys - The wallet keys to use.
|
|
14
|
+
* @param chain - The chain to use.
|
|
15
|
+
* @param index - The index to use.
|
|
16
|
+
* @param network - The network to use.
|
|
17
|
+
* @param addressFormat - The address format to use.
|
|
18
|
+
* Only relevant for Bitcoin Cash and eCash networks, where:
|
|
19
|
+
* - "default" means base58check,
|
|
20
|
+
* - "cashaddr" means cashaddr.
|
|
21
|
+
*/
|
|
22
|
+
export function address(keys, chain, index, network, addressFormat) {
|
|
23
|
+
const walletKeys = RootWalletKeys.from(keys);
|
|
24
|
+
return FixedScriptWalletNamespace.address(walletKeys.wasm, chain, index, network, addressFormat);
|
|
25
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { RootWalletKeys, type WalletKeysArg, type IWalletKeys } from "./RootWalletKeys.js";
|
|
2
|
+
export { ReplayProtection, type ReplayProtectionArg } from "./ReplayProtection.js";
|
|
3
|
+
export { outputScript, address } from "./address.js";
|
|
4
|
+
export { BitGoPsbt, type NetworkName, type ScriptId, type InputScriptType, type ParsedInput, type ParsedOutput, type ParsedTransaction, } from "./BitGoPsbt.js";
|
package/dist/esm/js/index.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
export * as address from "./address.js";
|
|
2
2
|
export * as ast from "./ast/index.js";
|
|
3
3
|
export * as utxolibCompat from "./utxolibCompat.js";
|
|
4
|
-
export * as fixedScriptWallet from "./fixedScriptWallet.js";
|
|
4
|
+
export * as fixedScriptWallet from "./fixedScriptWallet/index.js";
|
|
5
|
+
export * as bip32 from "./bip32.js";
|
|
6
|
+
export * as ecpair from "./ecpair.js";
|
|
7
|
+
export { ECPair } from "./ecpair.js";
|
|
8
|
+
export { BIP32 } from "./bip32.js";
|
|
5
9
|
export type { CoinName } from "./coinName.js";
|
|
6
10
|
export type { Triple } from "./triple.js";
|
|
7
11
|
export type { AddressFormat } from "./address.js";
|
package/dist/esm/js/index.js
CHANGED
|
@@ -2,10 +2,17 @@ import * as wasm from "./wasm/wasm_utxo.js";
|
|
|
2
2
|
// we need to access the wasm module here, otherwise webpack gets all weird
|
|
3
3
|
// and forgets to include it in the bundle
|
|
4
4
|
void wasm;
|
|
5
|
+
// Most exports are namespaced to avoid polluting the top-level namespace
|
|
6
|
+
// and to make imports more explicit (e.g., `import { address } from '@bitgo/wasm-utxo'`)
|
|
5
7
|
export * as address from "./address.js";
|
|
6
8
|
export * as ast from "./ast/index.js";
|
|
7
9
|
export * as utxolibCompat from "./utxolibCompat.js";
|
|
8
|
-
export * as fixedScriptWallet from "./fixedScriptWallet.js";
|
|
10
|
+
export * as fixedScriptWallet from "./fixedScriptWallet/index.js";
|
|
11
|
+
export * as bip32 from "./bip32.js";
|
|
12
|
+
export * as ecpair from "./ecpair.js";
|
|
13
|
+
// Only the most commonly used classes and types are exported at the top level for convenience
|
|
14
|
+
export { ECPair } from "./ecpair.js";
|
|
15
|
+
export { BIP32 } from "./bip32.js";
|
|
9
16
|
export { WrapDescriptor as Descriptor } from "./wasm/wasm_utxo.js";
|
|
10
17
|
export { WrapMiniscript as Miniscript } from "./wasm/wasm_utxo.js";
|
|
11
18
|
export { WrapPsbt as Psbt } from "./wasm/wasm_utxo.js";
|