@bitgo-beta/babylonlabs-io-btc-staking-ts 0.4.0-bitgo.1

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/index.cjs ADDED
@@ -0,0 +1,2521 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key2 of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key2) && key2 !== except)
16
+ __defProp(to, key2, { get: () => from[key2], enumerable: !(desc = __getOwnPropDesc(from, key2)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ BabylonBtcStakingManager: () => BabylonBtcStakingManager,
34
+ BitcoinScriptType: () => BitcoinScriptType,
35
+ ObservableStaking: () => ObservableStaking,
36
+ ObservableStakingScriptData: () => ObservableStakingScriptData,
37
+ SigningStep: () => SigningStep,
38
+ Staking: () => Staking,
39
+ StakingScriptData: () => StakingScriptData,
40
+ buildStakingTransactionOutputs: () => buildStakingTransactionOutputs,
41
+ createCovenantWitness: () => createCovenantWitness,
42
+ deriveSlashingOutput: () => deriveSlashingOutput,
43
+ deriveStakingOutputInfo: () => deriveStakingOutputInfo,
44
+ deriveUnbondingOutputInfo: () => deriveUnbondingOutputInfo,
45
+ findInputUTXO: () => findInputUTXO,
46
+ findMatchingTxOutputIndex: () => findMatchingTxOutputIndex,
47
+ getBabylonParamByBtcHeight: () => getBabylonParamByBtcHeight,
48
+ getBabylonParamByVersion: () => getBabylonParamByVersion,
49
+ getPsbtInputFields: () => getPsbtInputFields,
50
+ getPublicKeyNoCoord: () => getPublicKeyNoCoord,
51
+ getScriptType: () => getScriptType,
52
+ getUnbondingTxStakerSignature: () => getUnbondingTxStakerSignature,
53
+ initBTCCurve: () => initBTCCurve,
54
+ isTaproot: () => isTaproot,
55
+ isValidBitcoinAddress: () => isValidBitcoinAddress,
56
+ isValidNoCoordPublicKey: () => isValidNoCoordPublicKey,
57
+ slashEarlyUnbondedTransaction: () => slashEarlyUnbondedTransaction,
58
+ slashTimelockUnbondedTransaction: () => slashTimelockUnbondedTransaction,
59
+ stakingTransaction: () => stakingTransaction,
60
+ toBuffers: () => toBuffers,
61
+ transactionIdToHash: () => transactionIdToHash,
62
+ unbondingTransaction: () => unbondingTransaction,
63
+ validateParams: () => validateParams,
64
+ validateStakingTimelock: () => validateStakingTimelock,
65
+ validateStakingTxInputData: () => validateStakingTxInputData,
66
+ withdrawEarlyUnbondedTransaction: () => withdrawEarlyUnbondedTransaction,
67
+ withdrawSlashingTransaction: () => withdrawSlashingTransaction,
68
+ withdrawTimelockUnbondedTransaction: () => withdrawTimelockUnbondedTransaction
69
+ });
70
+ module.exports = __toCommonJS(src_exports);
71
+
72
+ // src/staking/stakingScript.ts
73
+ var import_bitcoinjs_lib = require("bitcoinjs-lib");
74
+
75
+ // src/constants/keys.ts
76
+ var NO_COORD_PK_BYTE_LENGTH = 32;
77
+
78
+ // src/staking/stakingScript.ts
79
+ var MAGIC_BYTES_LEN = 4;
80
+ var StakingScriptData = class {
81
+ constructor(stakerKey, finalityProviderKeys, covenantKeys, covenantThreshold, stakingTimelock, unbondingTimelock) {
82
+ if (!stakerKey || !finalityProviderKeys || !covenantKeys || !covenantThreshold || !stakingTimelock || !unbondingTimelock) {
83
+ throw new Error("Missing required input values");
84
+ }
85
+ this.stakerKey = stakerKey;
86
+ this.finalityProviderKeys = finalityProviderKeys;
87
+ this.covenantKeys = covenantKeys;
88
+ this.covenantThreshold = covenantThreshold;
89
+ this.stakingTimeLock = stakingTimelock;
90
+ this.unbondingTimeLock = unbondingTimelock;
91
+ if (!this.validate()) {
92
+ throw new Error("Invalid script data provided");
93
+ }
94
+ }
95
+ /**
96
+ * Validates the staking script.
97
+ * @returns {boolean} Returns true if the staking script is valid, otherwise false.
98
+ */
99
+ validate() {
100
+ if (this.stakerKey.length != NO_COORD_PK_BYTE_LENGTH) {
101
+ return false;
102
+ }
103
+ if (this.finalityProviderKeys.some(
104
+ (finalityProviderKey) => finalityProviderKey.length != NO_COORD_PK_BYTE_LENGTH
105
+ )) {
106
+ return false;
107
+ }
108
+ if (this.covenantKeys.some((covenantKey) => covenantKey.length != NO_COORD_PK_BYTE_LENGTH)) {
109
+ return false;
110
+ }
111
+ const allPks = [
112
+ this.stakerKey,
113
+ ...this.finalityProviderKeys,
114
+ ...this.covenantKeys
115
+ ];
116
+ const allPksSet = new Set(allPks);
117
+ if (allPks.length !== allPksSet.size) {
118
+ return false;
119
+ }
120
+ if (this.covenantThreshold == 0 || this.covenantThreshold > this.covenantKeys.length) {
121
+ return false;
122
+ }
123
+ if (this.stakingTimeLock == 0 || this.stakingTimeLock > 65535) {
124
+ return false;
125
+ }
126
+ if (this.unbondingTimeLock == 0 || this.unbondingTimeLock > 65535) {
127
+ return false;
128
+ }
129
+ return true;
130
+ }
131
+ // The staking script allows for multiple finality provider public keys
132
+ // to support (re)stake to multiple finality providers
133
+ // Covenant members are going to have multiple keys
134
+ /**
135
+ * Builds a timelock script.
136
+ * @param timelock - The timelock value to encode in the script.
137
+ * @returns {Buffer} containing the compiled timelock script.
138
+ */
139
+ buildTimelockScript(timelock) {
140
+ return import_bitcoinjs_lib.script.compile([
141
+ this.stakerKey,
142
+ import_bitcoinjs_lib.opcodes.OP_CHECKSIGVERIFY,
143
+ import_bitcoinjs_lib.script.number.encode(timelock),
144
+ import_bitcoinjs_lib.opcodes.OP_CHECKSEQUENCEVERIFY
145
+ ]);
146
+ }
147
+ /**
148
+ * Builds the staking timelock script.
149
+ * Only holder of private key for given pubKey can spend after relative lock time
150
+ * Creates the timelock script in the form:
151
+ * <stakerPubKey>
152
+ * OP_CHECKSIGVERIFY
153
+ * <stakingTimeBlocks>
154
+ * OP_CHECKSEQUENCEVERIFY
155
+ * @returns {Buffer} The staking timelock script.
156
+ */
157
+ buildStakingTimelockScript() {
158
+ return this.buildTimelockScript(this.stakingTimeLock);
159
+ }
160
+ /**
161
+ * Builds the unbonding timelock script.
162
+ * Creates the unbonding timelock script in the form:
163
+ * <stakerPubKey>
164
+ * OP_CHECKSIGVERIFY
165
+ * <unbondingTimeBlocks>
166
+ * OP_CHECKSEQUENCEVERIFY
167
+ * @returns {Buffer} The unbonding timelock script.
168
+ */
169
+ buildUnbondingTimelockScript() {
170
+ return this.buildTimelockScript(this.unbondingTimeLock);
171
+ }
172
+ /**
173
+ * Builds the unbonding script in the form:
174
+ * buildSingleKeyScript(stakerPk, true) ||
175
+ * buildMultiKeyScript(covenantPks, covenantThreshold, false)
176
+ * || means combining the scripts
177
+ * @returns {Buffer} The unbonding script.
178
+ */
179
+ buildUnbondingScript() {
180
+ return Buffer.concat([
181
+ this.buildSingleKeyScript(this.stakerKey, true),
182
+ this.buildMultiKeyScript(
183
+ this.covenantKeys,
184
+ this.covenantThreshold,
185
+ false
186
+ )
187
+ ]);
188
+ }
189
+ /**
190
+ * Builds the slashing script for staking in the form:
191
+ * buildSingleKeyScript(stakerPk, true) ||
192
+ * buildMultiKeyScript(finalityProviderPKs, 1, true) ||
193
+ * buildMultiKeyScript(covenantPks, covenantThreshold, false)
194
+ * || means combining the scripts
195
+ * The slashing script is a combination of single-key and multi-key scripts.
196
+ * The single-key script is used for staker key verification.
197
+ * The multi-key script is used for finality provider key verification and covenant key verification.
198
+ * @returns {Buffer} The slashing script as a Buffer.
199
+ */
200
+ buildSlashingScript() {
201
+ return Buffer.concat([
202
+ this.buildSingleKeyScript(this.stakerKey, true),
203
+ this.buildMultiKeyScript(
204
+ this.finalityProviderKeys,
205
+ // The threshold is always 1 as we only need one
206
+ // finalityProvider signature to perform slashing
207
+ // (only one finalityProvider performs an offence)
208
+ 1,
209
+ // OP_VERIFY/OP_CHECKSIGVERIFY is added at the end
210
+ true
211
+ ),
212
+ this.buildMultiKeyScript(
213
+ this.covenantKeys,
214
+ this.covenantThreshold,
215
+ // No need to add verify since covenants are at the end of the script
216
+ false
217
+ )
218
+ ]);
219
+ }
220
+ /**
221
+ * Builds the staking scripts.
222
+ * @returns {StakingScripts} The staking scripts.
223
+ */
224
+ buildScripts() {
225
+ return {
226
+ timelockScript: this.buildStakingTimelockScript(),
227
+ unbondingScript: this.buildUnbondingScript(),
228
+ slashingScript: this.buildSlashingScript(),
229
+ unbondingTimelockScript: this.buildUnbondingTimelockScript()
230
+ };
231
+ }
232
+ // buildSingleKeyScript and buildMultiKeyScript allow us to reuse functionality
233
+ // for creating Bitcoin scripts for the unbonding script and the slashing script
234
+ /**
235
+ * Builds a single key script in the form:
236
+ * buildSingleKeyScript creates a single key script
237
+ * <pk> OP_CHECKSIGVERIFY (if withVerify is true)
238
+ * <pk> OP_CHECKSIG (if withVerify is false)
239
+ * @param pk - The public key buffer.
240
+ * @param withVerify - A boolean indicating whether to include the OP_CHECKSIGVERIFY opcode.
241
+ * @returns The compiled script buffer.
242
+ */
243
+ buildSingleKeyScript(pk, withVerify) {
244
+ if (pk.length != NO_COORD_PK_BYTE_LENGTH) {
245
+ throw new Error("Invalid key length");
246
+ }
247
+ return import_bitcoinjs_lib.script.compile([
248
+ pk,
249
+ withVerify ? import_bitcoinjs_lib.opcodes.OP_CHECKSIGVERIFY : import_bitcoinjs_lib.opcodes.OP_CHECKSIG
250
+ ]);
251
+ }
252
+ /**
253
+ * Builds a multi-key script in the form:
254
+ * <pk1> OP_CHEKCSIG <pk2> OP_CHECKSIGADD <pk3> OP_CHECKSIGADD ... <pkN> OP_CHECKSIGADD <threshold> OP_NUMEQUAL
255
+ * <withVerify -> OP_NUMEQUALVERIFY>
256
+ * It validates whether provided keys are unique and the threshold is not greater than number of keys
257
+ * If there is only one key provided it will return single key sig script
258
+ * @param pks - An array of public keys.
259
+ * @param threshold - The required number of valid signers.
260
+ * @param withVerify - A boolean indicating whether to include the OP_VERIFY opcode.
261
+ * @returns The compiled multi-key script as a Buffer.
262
+ * @throws {Error} If no keys are provided, if the required number of valid signers is greater than the number of provided keys, or if duplicate keys are provided.
263
+ */
264
+ buildMultiKeyScript(pks, threshold, withVerify) {
265
+ if (!pks || pks.length === 0) {
266
+ throw new Error("No keys provided");
267
+ }
268
+ if (pks.some((pk) => pk.length != NO_COORD_PK_BYTE_LENGTH)) {
269
+ throw new Error("Invalid key length");
270
+ }
271
+ if (threshold > pks.length) {
272
+ throw new Error(
273
+ "Required number of valid signers is greater than number of provided keys"
274
+ );
275
+ }
276
+ if (pks.length === 1) {
277
+ return this.buildSingleKeyScript(pks[0], withVerify);
278
+ }
279
+ const sortedPks = [...pks].sort(Buffer.compare);
280
+ for (let i = 0; i < sortedPks.length - 1; ++i) {
281
+ if (sortedPks[i].equals(sortedPks[i + 1])) {
282
+ throw new Error("Duplicate keys provided");
283
+ }
284
+ }
285
+ const scriptElements = [sortedPks[0], import_bitcoinjs_lib.opcodes.OP_CHECKSIG];
286
+ for (let i = 1; i < sortedPks.length; i++) {
287
+ scriptElements.push(sortedPks[i]);
288
+ scriptElements.push(import_bitcoinjs_lib.opcodes.OP_CHECKSIGADD);
289
+ }
290
+ scriptElements.push(import_bitcoinjs_lib.script.number.encode(threshold));
291
+ if (withVerify) {
292
+ scriptElements.push(import_bitcoinjs_lib.opcodes.OP_NUMEQUALVERIFY);
293
+ } else {
294
+ scriptElements.push(import_bitcoinjs_lib.opcodes.OP_NUMEQUAL);
295
+ }
296
+ return import_bitcoinjs_lib.script.compile(scriptElements);
297
+ }
298
+ };
299
+
300
+ // src/error/index.ts
301
+ var StakingError = class _StakingError extends Error {
302
+ constructor(code, message) {
303
+ super(message);
304
+ this.code = code;
305
+ }
306
+ // Static method to safely handle unknown errors
307
+ static fromUnknown(error, code, fallbackMsg) {
308
+ if (error instanceof _StakingError) {
309
+ return error;
310
+ }
311
+ if (error instanceof Error) {
312
+ return new _StakingError(code, error.message);
313
+ }
314
+ return new _StakingError(code, fallbackMsg);
315
+ }
316
+ };
317
+
318
+ // src/staking/transactions.ts
319
+ var import_bitcoinjs_lib6 = require("bitcoinjs-lib");
320
+
321
+ // src/constants/dustSat.ts
322
+ var BTC_DUST_SAT = 546;
323
+
324
+ // src/constants/internalPubkey.ts
325
+ var key = "0250929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0";
326
+ var internalPubkey = Buffer.from(key, "hex").subarray(1, 33);
327
+
328
+ // src/utils/btc.ts
329
+ var ecc = __toESM(require("@bitcoin-js/tiny-secp256k1-asmjs"), 1);
330
+ var import_bitcoinjs_lib2 = require("bitcoinjs-lib");
331
+ var initBTCCurve = () => {
332
+ (0, import_bitcoinjs_lib2.initEccLib)(ecc);
333
+ };
334
+ var isValidBitcoinAddress = (btcAddress, network) => {
335
+ try {
336
+ return !!import_bitcoinjs_lib2.address.toOutputScript(btcAddress, network);
337
+ } catch (error) {
338
+ return false;
339
+ }
340
+ };
341
+ var isTaproot = (taprootAddress, network) => {
342
+ try {
343
+ const decoded = import_bitcoinjs_lib2.address.fromBech32(taprootAddress);
344
+ if (decoded.version !== 1) {
345
+ return false;
346
+ }
347
+ switch (network) {
348
+ case import_bitcoinjs_lib2.networks.bitcoin:
349
+ return taprootAddress.startsWith("bc1p");
350
+ case import_bitcoinjs_lib2.networks.testnet:
351
+ return taprootAddress.startsWith("tb1p") || taprootAddress.startsWith("sb1p");
352
+ default:
353
+ return false;
354
+ }
355
+ } catch (error) {
356
+ return false;
357
+ }
358
+ };
359
+ var isValidNoCoordPublicKey = (pkWithNoCoord) => {
360
+ try {
361
+ const keyBuffer = Buffer.from(pkWithNoCoord, "hex");
362
+ return validateNoCoordPublicKeyBuffer(keyBuffer);
363
+ } catch (error) {
364
+ return false;
365
+ }
366
+ };
367
+ var getPublicKeyNoCoord = (pkHex) => {
368
+ const publicKey = Buffer.from(pkHex, "hex");
369
+ const publicKeyNoCoordBuffer = publicKey.length === NO_COORD_PK_BYTE_LENGTH ? publicKey : publicKey.subarray(1, 33);
370
+ if (!validateNoCoordPublicKeyBuffer(publicKeyNoCoordBuffer)) {
371
+ throw new Error("Invalid public key without coordinate");
372
+ }
373
+ return publicKeyNoCoordBuffer.toString("hex");
374
+ };
375
+ var validateNoCoordPublicKeyBuffer = (pkBuffer) => {
376
+ if (pkBuffer.length !== NO_COORD_PK_BYTE_LENGTH) {
377
+ return false;
378
+ }
379
+ const compressedKeyEven = Buffer.concat([Buffer.from([2]), pkBuffer]);
380
+ const compressedKeyOdd = Buffer.concat([Buffer.from([3]), pkBuffer]);
381
+ return ecc.isPoint(compressedKeyEven) || ecc.isPoint(compressedKeyOdd);
382
+ };
383
+ var transactionIdToHash = (txId) => {
384
+ if (txId === "") {
385
+ throw new Error("Transaction id cannot be empty");
386
+ }
387
+ return Buffer.from(txId, "hex").reverse();
388
+ };
389
+
390
+ // src/utils/fee/index.ts
391
+ var import_bitcoinjs_lib4 = require("bitcoinjs-lib");
392
+
393
+ // src/constants/fee.ts
394
+ var DEFAULT_INPUT_SIZE = 180;
395
+ var P2WPKH_INPUT_SIZE = 68;
396
+ var P2TR_INPUT_SIZE = 58;
397
+ var TX_BUFFER_SIZE_OVERHEAD = 11;
398
+ var LOW_RATE_ESTIMATION_ACCURACY_BUFFER = 30;
399
+ var MAX_NON_LEGACY_OUTPUT_SIZE = 43;
400
+ var WITHDRAW_TX_BUFFER_SIZE = 17;
401
+ var WALLET_RELAY_FEE_RATE_THRESHOLD = 2;
402
+ var OP_RETURN_OUTPUT_VALUE_SIZE = 8;
403
+ var OP_RETURN_VALUE_SERIALIZE_SIZE = 1;
404
+
405
+ // src/utils/fee/utils.ts
406
+ var import_bitcoinjs_lib3 = require("bitcoinjs-lib");
407
+ var isOP_RETURN = (script4) => {
408
+ const decompiled = import_bitcoinjs_lib3.script.decompile(script4);
409
+ return !!decompiled && decompiled[0] === import_bitcoinjs_lib3.opcodes.OP_RETURN;
410
+ };
411
+ var getInputSizeByScript = (script4) => {
412
+ try {
413
+ const { address: p2wpkhAddress } = import_bitcoinjs_lib3.payments.p2wpkh({
414
+ output: script4
415
+ });
416
+ if (p2wpkhAddress) {
417
+ return P2WPKH_INPUT_SIZE;
418
+ }
419
+ } catch (error) {
420
+ }
421
+ try {
422
+ const { address: p2trAddress } = import_bitcoinjs_lib3.payments.p2tr({
423
+ output: script4
424
+ });
425
+ if (p2trAddress) {
426
+ return P2TR_INPUT_SIZE;
427
+ }
428
+ } catch (error) {
429
+ }
430
+ return DEFAULT_INPUT_SIZE;
431
+ };
432
+ var getEstimatedChangeOutputSize = () => {
433
+ return MAX_NON_LEGACY_OUTPUT_SIZE;
434
+ };
435
+ var inputValueSum = (inputUTXOs) => {
436
+ return inputUTXOs.reduce((acc, utxo) => acc + utxo.value, 0);
437
+ };
438
+
439
+ // src/utils/fee/index.ts
440
+ var getStakingTxInputUTXOsAndFees = (availableUTXOs, stakingAmount, feeRate, outputs) => {
441
+ if (availableUTXOs.length === 0) {
442
+ throw new Error("Insufficient funds");
443
+ }
444
+ const validUTXOs = availableUTXOs.filter((utxo) => {
445
+ const script4 = Buffer.from(utxo.scriptPubKey, "hex");
446
+ return !!import_bitcoinjs_lib4.script.decompile(script4);
447
+ });
448
+ if (validUTXOs.length === 0) {
449
+ throw new Error("Insufficient funds: no valid UTXOs available for staking");
450
+ }
451
+ const sortedUTXOs = validUTXOs.sort((a, b) => b.value - a.value);
452
+ const selectedUTXOs = [];
453
+ let accumulatedValue = 0;
454
+ let estimatedFee = 0;
455
+ for (const utxo of sortedUTXOs) {
456
+ selectedUTXOs.push(utxo);
457
+ accumulatedValue += utxo.value;
458
+ const estimatedSize = getEstimatedSize(selectedUTXOs, outputs);
459
+ estimatedFee = estimatedSize * feeRate + rateBasedTxBufferFee(feeRate);
460
+ if (accumulatedValue - (stakingAmount + estimatedFee) > BTC_DUST_SAT) {
461
+ estimatedFee += getEstimatedChangeOutputSize() * feeRate;
462
+ }
463
+ if (accumulatedValue >= stakingAmount + estimatedFee) {
464
+ break;
465
+ }
466
+ }
467
+ if (accumulatedValue < stakingAmount + estimatedFee) {
468
+ throw new Error(
469
+ "Insufficient funds: unable to gather enough UTXOs to cover the staking amount and fees"
470
+ );
471
+ }
472
+ return {
473
+ selectedUTXOs,
474
+ fee: estimatedFee
475
+ };
476
+ };
477
+ var getWithdrawTxFee = (feeRate) => {
478
+ const inputSize = P2TR_INPUT_SIZE;
479
+ const outputSize = getEstimatedChangeOutputSize();
480
+ return feeRate * (inputSize + outputSize + TX_BUFFER_SIZE_OVERHEAD + WITHDRAW_TX_BUFFER_SIZE) + rateBasedTxBufferFee(feeRate);
481
+ };
482
+ var getEstimatedSize = (inputUtxos, outputs) => {
483
+ const inputSize = inputUtxos.reduce((acc, u) => {
484
+ const script4 = Buffer.from(u.scriptPubKey, "hex");
485
+ const decompiledScript = import_bitcoinjs_lib4.script.decompile(script4);
486
+ if (!decompiledScript) {
487
+ return acc;
488
+ }
489
+ return acc + getInputSizeByScript(script4);
490
+ }, 0);
491
+ const outputSize = outputs.reduce((acc, output) => {
492
+ if (isOP_RETURN(output.scriptPubKey)) {
493
+ return acc + output.scriptPubKey.length + OP_RETURN_OUTPUT_VALUE_SIZE + OP_RETURN_VALUE_SERIALIZE_SIZE;
494
+ }
495
+ return acc + MAX_NON_LEGACY_OUTPUT_SIZE;
496
+ }, 0);
497
+ return inputSize + outputSize + TX_BUFFER_SIZE_OVERHEAD;
498
+ };
499
+ var rateBasedTxBufferFee = (feeRate) => {
500
+ return feeRate <= WALLET_RELAY_FEE_RATE_THRESHOLD ? LOW_RATE_ESTIMATION_ACCURACY_BUFFER : 0;
501
+ };
502
+
503
+ // src/utils/staking/index.ts
504
+ var import_bitcoinjs_lib5 = require("bitcoinjs-lib");
505
+
506
+ // src/constants/unbonding.ts
507
+ var MIN_UNBONDING_OUTPUT_VALUE = 1e3;
508
+
509
+ // src/utils/staking/index.ts
510
+ var buildStakingTransactionOutputs = (scripts, network, amount) => {
511
+ const stakingOutputInfo = deriveStakingOutputInfo(scripts, network);
512
+ const transactionOutputs = [
513
+ {
514
+ scriptPubKey: stakingOutputInfo.scriptPubKey,
515
+ value: amount
516
+ }
517
+ ];
518
+ if (scripts.dataEmbedScript) {
519
+ transactionOutputs.push({
520
+ scriptPubKey: scripts.dataEmbedScript,
521
+ value: 0
522
+ });
523
+ }
524
+ return transactionOutputs;
525
+ };
526
+ var deriveStakingOutputInfo = (scripts, network) => {
527
+ const scriptTree = [
528
+ {
529
+ output: scripts.slashingScript
530
+ },
531
+ [{ output: scripts.unbondingScript }, { output: scripts.timelockScript }]
532
+ ];
533
+ const stakingOutput = import_bitcoinjs_lib5.payments.p2tr({
534
+ internalPubkey,
535
+ scriptTree,
536
+ network
537
+ });
538
+ if (!stakingOutput.address) {
539
+ throw new StakingError(
540
+ "INVALID_OUTPUT" /* INVALID_OUTPUT */,
541
+ "Failed to build staking output"
542
+ );
543
+ }
544
+ return {
545
+ outputAddress: stakingOutput.address,
546
+ scriptPubKey: import_bitcoinjs_lib5.address.toOutputScript(stakingOutput.address, network)
547
+ };
548
+ };
549
+ var deriveUnbondingOutputInfo = (scripts, network) => {
550
+ const outputScriptTree = [
551
+ {
552
+ output: scripts.slashingScript
553
+ },
554
+ { output: scripts.unbondingTimelockScript }
555
+ ];
556
+ const unbondingOutput = import_bitcoinjs_lib5.payments.p2tr({
557
+ internalPubkey,
558
+ scriptTree: outputScriptTree,
559
+ network
560
+ });
561
+ if (!unbondingOutput.address) {
562
+ throw new StakingError(
563
+ "INVALID_OUTPUT" /* INVALID_OUTPUT */,
564
+ "Failed to build unbonding output"
565
+ );
566
+ }
567
+ return {
568
+ outputAddress: unbondingOutput.address,
569
+ scriptPubKey: import_bitcoinjs_lib5.address.toOutputScript(unbondingOutput.address, network)
570
+ };
571
+ };
572
+ var deriveSlashingOutput = (scripts, network) => {
573
+ const slashingOutput = import_bitcoinjs_lib5.payments.p2tr({
574
+ internalPubkey,
575
+ scriptTree: { output: scripts.unbondingTimelockScript },
576
+ network
577
+ });
578
+ const slashingOutputAddress = slashingOutput.address;
579
+ if (!slashingOutputAddress) {
580
+ throw new StakingError(
581
+ "INVALID_OUTPUT" /* INVALID_OUTPUT */,
582
+ "Failed to build slashing output address"
583
+ );
584
+ }
585
+ return {
586
+ outputAddress: slashingOutputAddress,
587
+ scriptPubKey: import_bitcoinjs_lib5.address.toOutputScript(slashingOutputAddress, network)
588
+ };
589
+ };
590
+ var findMatchingTxOutputIndex = (tx, outputAddress, network) => {
591
+ const index = tx.outs.findIndex((output) => {
592
+ return import_bitcoinjs_lib5.address.fromOutputScript(output.script, network) === outputAddress;
593
+ });
594
+ if (index === -1) {
595
+ throw new StakingError(
596
+ "INVALID_OUTPUT" /* INVALID_OUTPUT */,
597
+ `Matching output not found for address: ${outputAddress}`
598
+ );
599
+ }
600
+ return index;
601
+ };
602
+ var validateStakingTxInputData = (stakingAmountSat, timelock, params, inputUTXOs, feeRate) => {
603
+ if (stakingAmountSat < params.minStakingAmountSat || stakingAmountSat > params.maxStakingAmountSat) {
604
+ throw new StakingError(
605
+ "INVALID_INPUT" /* INVALID_INPUT */,
606
+ "Invalid staking amount"
607
+ );
608
+ }
609
+ if (timelock < params.minStakingTimeBlocks || timelock > params.maxStakingTimeBlocks) {
610
+ throw new StakingError(
611
+ "INVALID_INPUT" /* INVALID_INPUT */,
612
+ "Invalid timelock"
613
+ );
614
+ }
615
+ if (inputUTXOs.length == 0) {
616
+ throw new StakingError(
617
+ "INVALID_INPUT" /* INVALID_INPUT */,
618
+ "No input UTXOs provided"
619
+ );
620
+ }
621
+ if (feeRate <= 0) {
622
+ throw new StakingError(
623
+ "INVALID_INPUT" /* INVALID_INPUT */,
624
+ "Invalid fee rate"
625
+ );
626
+ }
627
+ };
628
+ var validateParams = (params) => {
629
+ if (params.covenantNoCoordPks.length == 0) {
630
+ throw new StakingError(
631
+ "INVALID_PARAMS" /* INVALID_PARAMS */,
632
+ "Could not find any covenant public keys"
633
+ );
634
+ }
635
+ if (params.covenantNoCoordPks.length < params.covenantQuorum) {
636
+ throw new StakingError(
637
+ "INVALID_PARAMS" /* INVALID_PARAMS */,
638
+ "Covenant public keys must be greater than or equal to the quorum"
639
+ );
640
+ }
641
+ params.covenantNoCoordPks.forEach((pk) => {
642
+ if (!isValidNoCoordPublicKey(pk)) {
643
+ throw new StakingError(
644
+ "INVALID_PARAMS" /* INVALID_PARAMS */,
645
+ "Covenant public key should contains no coordinate"
646
+ );
647
+ }
648
+ });
649
+ if (params.unbondingTime <= 0) {
650
+ throw new StakingError(
651
+ "INVALID_PARAMS" /* INVALID_PARAMS */,
652
+ "Unbonding time must be greater than 0"
653
+ );
654
+ }
655
+ if (params.unbondingFeeSat <= 0) {
656
+ throw new StakingError(
657
+ "INVALID_PARAMS" /* INVALID_PARAMS */,
658
+ "Unbonding fee must be greater than 0"
659
+ );
660
+ }
661
+ if (params.maxStakingAmountSat < params.minStakingAmountSat) {
662
+ throw new StakingError(
663
+ "INVALID_PARAMS" /* INVALID_PARAMS */,
664
+ "Max staking amount must be greater or equal to min staking amount"
665
+ );
666
+ }
667
+ if (params.minStakingAmountSat < params.unbondingFeeSat + MIN_UNBONDING_OUTPUT_VALUE) {
668
+ throw new StakingError(
669
+ "INVALID_PARAMS" /* INVALID_PARAMS */,
670
+ `Min staking amount must be greater than unbonding fee plus ${MIN_UNBONDING_OUTPUT_VALUE}`
671
+ );
672
+ }
673
+ if (params.maxStakingTimeBlocks < params.minStakingTimeBlocks) {
674
+ throw new StakingError(
675
+ "INVALID_PARAMS" /* INVALID_PARAMS */,
676
+ "Max staking time must be greater or equal to min staking time"
677
+ );
678
+ }
679
+ if (params.minStakingTimeBlocks <= 0) {
680
+ throw new StakingError(
681
+ "INVALID_PARAMS" /* INVALID_PARAMS */,
682
+ "Min staking time must be greater than 0"
683
+ );
684
+ }
685
+ if (params.covenantQuorum <= 0) {
686
+ throw new StakingError(
687
+ "INVALID_PARAMS" /* INVALID_PARAMS */,
688
+ "Covenant quorum must be greater than 0"
689
+ );
690
+ }
691
+ if (params.slashing) {
692
+ if (params.slashing.slashingRate <= 0) {
693
+ throw new StakingError(
694
+ "INVALID_PARAMS" /* INVALID_PARAMS */,
695
+ "Slashing rate must be greater than 0"
696
+ );
697
+ }
698
+ if (params.slashing.slashingRate > 1) {
699
+ throw new StakingError(
700
+ "INVALID_PARAMS" /* INVALID_PARAMS */,
701
+ "Slashing rate must be less or equal to 1"
702
+ );
703
+ }
704
+ if (params.slashing.slashingPkScriptHex.length == 0) {
705
+ throw new StakingError(
706
+ "INVALID_PARAMS" /* INVALID_PARAMS */,
707
+ "Slashing public key script is missing"
708
+ );
709
+ }
710
+ if (params.slashing.minSlashingTxFeeSat <= 0) {
711
+ throw new StakingError(
712
+ "INVALID_PARAMS" /* INVALID_PARAMS */,
713
+ "Minimum slashing transaction fee must be greater than 0"
714
+ );
715
+ }
716
+ }
717
+ };
718
+ var validateStakingTimelock = (stakingTimelock, params) => {
719
+ if (stakingTimelock < params.minStakingTimeBlocks || stakingTimelock > params.maxStakingTimeBlocks) {
720
+ throw new StakingError(
721
+ "INVALID_INPUT" /* INVALID_INPUT */,
722
+ "Staking transaction timelock is out of range"
723
+ );
724
+ }
725
+ };
726
+ var toBuffers = (inputs) => {
727
+ try {
728
+ return inputs.map(
729
+ (i) => Buffer.from(i, "hex")
730
+ );
731
+ } catch (error) {
732
+ throw StakingError.fromUnknown(
733
+ error,
734
+ "INVALID_INPUT" /* INVALID_INPUT */,
735
+ "Cannot convert values to buffers"
736
+ );
737
+ }
738
+ };
739
+
740
+ // src/constants/psbt.ts
741
+ var NON_RBF_SEQUENCE = 4294967295;
742
+ var TRANSACTION_VERSION = 2;
743
+
744
+ // src/constants/transaction.ts
745
+ var REDEEM_VERSION = 192;
746
+
747
+ // src/staking/transactions.ts
748
+ var BTC_LOCKTIME_HEIGHT_TIME_CUTOFF = 5e8;
749
+ function stakingTransaction(scripts, amount, changeAddress, inputUTXOs, network, feeRate, lockHeight) {
750
+ if (amount <= 0 || feeRate <= 0) {
751
+ throw new Error("Amount and fee rate must be bigger than 0");
752
+ }
753
+ if (!isValidBitcoinAddress(changeAddress, network)) {
754
+ throw new Error("Invalid change address");
755
+ }
756
+ const stakingOutputs = buildStakingTransactionOutputs(scripts, network, amount);
757
+ const { selectedUTXOs, fee } = getStakingTxInputUTXOsAndFees(
758
+ inputUTXOs,
759
+ amount,
760
+ feeRate,
761
+ stakingOutputs
762
+ );
763
+ const tx = new import_bitcoinjs_lib6.Transaction();
764
+ tx.version = TRANSACTION_VERSION;
765
+ for (let i = 0; i < selectedUTXOs.length; ++i) {
766
+ const input = selectedUTXOs[i];
767
+ tx.addInput(
768
+ transactionIdToHash(input.txid),
769
+ input.vout,
770
+ NON_RBF_SEQUENCE
771
+ );
772
+ }
773
+ stakingOutputs.forEach((o) => {
774
+ tx.addOutput(o.scriptPubKey, o.value);
775
+ });
776
+ const inputsSum = inputValueSum(selectedUTXOs);
777
+ if (inputsSum - (amount + fee) > BTC_DUST_SAT) {
778
+ tx.addOutput(
779
+ import_bitcoinjs_lib6.address.toOutputScript(changeAddress, network),
780
+ inputsSum - (amount + fee)
781
+ );
782
+ }
783
+ if (lockHeight) {
784
+ if (lockHeight >= BTC_LOCKTIME_HEIGHT_TIME_CUTOFF) {
785
+ throw new Error("Invalid lock height");
786
+ }
787
+ tx.locktime = lockHeight;
788
+ }
789
+ return {
790
+ transaction: tx,
791
+ fee
792
+ };
793
+ }
794
+ function withdrawEarlyUnbondedTransaction(scripts, unbondingTx, withdrawalAddress, network, feeRate) {
795
+ const scriptTree = [
796
+ {
797
+ output: scripts.slashingScript
798
+ },
799
+ { output: scripts.unbondingTimelockScript }
800
+ ];
801
+ return withdrawalTransaction(
802
+ {
803
+ timelockScript: scripts.unbondingTimelockScript
804
+ },
805
+ scriptTree,
806
+ unbondingTx,
807
+ withdrawalAddress,
808
+ network,
809
+ feeRate,
810
+ 0
811
+ // unbonding always has a single output
812
+ );
813
+ }
814
+ function withdrawTimelockUnbondedTransaction(scripts, tx, withdrawalAddress, network, feeRate, outputIndex = 0) {
815
+ const scriptTree = [
816
+ {
817
+ output: scripts.slashingScript
818
+ },
819
+ [{ output: scripts.unbondingScript }, { output: scripts.timelockScript }]
820
+ ];
821
+ return withdrawalTransaction(
822
+ scripts,
823
+ scriptTree,
824
+ tx,
825
+ withdrawalAddress,
826
+ network,
827
+ feeRate,
828
+ outputIndex
829
+ );
830
+ }
831
+ function withdrawSlashingTransaction(scripts, slashingTx, withdrawalAddress, network, feeRate, outputIndex) {
832
+ const scriptTree = { output: scripts.unbondingTimelockScript };
833
+ return withdrawalTransaction(
834
+ {
835
+ timelockScript: scripts.unbondingTimelockScript
836
+ },
837
+ scriptTree,
838
+ slashingTx,
839
+ withdrawalAddress,
840
+ network,
841
+ feeRate,
842
+ outputIndex
843
+ );
844
+ }
845
+ function withdrawalTransaction(scripts, scriptTree, tx, withdrawalAddress, network, feeRate, outputIndex = 0) {
846
+ if (feeRate <= 0) {
847
+ throw new Error("Withdrawal feeRate must be bigger than 0");
848
+ }
849
+ if (outputIndex < 0) {
850
+ throw new Error("Output index must be bigger or equal to 0");
851
+ }
852
+ const timePosition = 2;
853
+ const decompiled = import_bitcoinjs_lib6.script.decompile(scripts.timelockScript);
854
+ if (!decompiled) {
855
+ throw new Error("Timelock script is not valid");
856
+ }
857
+ let timelock = 0;
858
+ if (typeof decompiled[timePosition] !== "number") {
859
+ const timeBuffer = decompiled[timePosition];
860
+ timelock = import_bitcoinjs_lib6.script.number.decode(timeBuffer);
861
+ } else {
862
+ const wrap = decompiled[timePosition] % 16;
863
+ timelock = wrap === 0 ? 16 : wrap;
864
+ }
865
+ const redeem = {
866
+ output: scripts.timelockScript,
867
+ redeemVersion: REDEEM_VERSION
868
+ };
869
+ const p2tr = import_bitcoinjs_lib6.payments.p2tr({
870
+ internalPubkey,
871
+ scriptTree,
872
+ redeem,
873
+ network
874
+ });
875
+ const tapLeafScript = {
876
+ leafVersion: redeem.redeemVersion,
877
+ script: redeem.output,
878
+ controlBlock: p2tr.witness[p2tr.witness.length - 1]
879
+ };
880
+ const psbt = new import_bitcoinjs_lib6.Psbt({ network });
881
+ psbt.setVersion(TRANSACTION_VERSION);
882
+ psbt.addInput({
883
+ hash: tx.getHash(),
884
+ index: outputIndex,
885
+ tapInternalKey: internalPubkey,
886
+ witnessUtxo: {
887
+ value: tx.outs[outputIndex].value,
888
+ script: tx.outs[outputIndex].script
889
+ },
890
+ tapLeafScript: [tapLeafScript],
891
+ sequence: timelock
892
+ });
893
+ const estimatedFee = getWithdrawTxFee(feeRate);
894
+ const outputValue = tx.outs[outputIndex].value - estimatedFee;
895
+ if (outputValue < 0) {
896
+ throw new Error(
897
+ "Not enough funds to cover the fee for withdrawal transaction"
898
+ );
899
+ }
900
+ if (outputValue < BTC_DUST_SAT) {
901
+ throw new Error("Output value is less than dust limit");
902
+ }
903
+ psbt.addOutput({
904
+ address: withdrawalAddress,
905
+ value: outputValue
906
+ });
907
+ psbt.setLocktime(0);
908
+ return {
909
+ psbt,
910
+ fee: estimatedFee
911
+ };
912
+ }
913
+ function slashTimelockUnbondedTransaction(scripts, stakingTransaction2, slashingPkScriptHex, slashingRate, minimumFee, network, outputIndex = 0) {
914
+ const slashingScriptTree = [
915
+ {
916
+ output: scripts.slashingScript
917
+ },
918
+ [{ output: scripts.unbondingScript }, { output: scripts.timelockScript }]
919
+ ];
920
+ return slashingTransaction(
921
+ {
922
+ unbondingTimelockScript: scripts.unbondingTimelockScript,
923
+ slashingScript: scripts.slashingScript
924
+ },
925
+ slashingScriptTree,
926
+ stakingTransaction2,
927
+ slashingPkScriptHex,
928
+ slashingRate,
929
+ minimumFee,
930
+ network,
931
+ outputIndex
932
+ );
933
+ }
934
+ function slashEarlyUnbondedTransaction(scripts, unbondingTx, slashingPkScriptHex, slashingRate, minimumSlashingFee, network) {
935
+ const unbondingScriptTree = [
936
+ {
937
+ output: scripts.slashingScript
938
+ },
939
+ {
940
+ output: scripts.unbondingTimelockScript
941
+ }
942
+ ];
943
+ return slashingTransaction(
944
+ {
945
+ unbondingTimelockScript: scripts.unbondingTimelockScript,
946
+ slashingScript: scripts.slashingScript
947
+ },
948
+ unbondingScriptTree,
949
+ unbondingTx,
950
+ slashingPkScriptHex,
951
+ slashingRate,
952
+ minimumSlashingFee,
953
+ network,
954
+ 0
955
+ // unbonding always has a single output
956
+ );
957
+ }
958
+ function slashingTransaction(scripts, scriptTree, transaction, slashingPkScriptHex, slashingRate, minimumFee, network, outputIndex = 0) {
959
+ if (slashingRate <= 0 || slashingRate >= 1) {
960
+ throw new Error("Slashing rate must be between 0 and 1");
961
+ }
962
+ slashingRate = parseFloat(slashingRate.toFixed(2));
963
+ if (minimumFee <= 0 || !Number.isInteger(minimumFee)) {
964
+ throw new Error("Minimum fee must be a positve integer");
965
+ }
966
+ if (outputIndex < 0 || !Number.isInteger(outputIndex)) {
967
+ throw new Error("Output index must be an integer bigger or equal to 0");
968
+ }
969
+ if (!transaction.outs[outputIndex]) {
970
+ throw new Error("Output index is out of range");
971
+ }
972
+ const redeem = {
973
+ output: scripts.slashingScript,
974
+ redeemVersion: REDEEM_VERSION
975
+ };
976
+ const p2tr = import_bitcoinjs_lib6.payments.p2tr({
977
+ internalPubkey,
978
+ scriptTree,
979
+ redeem,
980
+ network
981
+ });
982
+ const tapLeafScript = {
983
+ leafVersion: redeem.redeemVersion,
984
+ script: redeem.output,
985
+ controlBlock: p2tr.witness[p2tr.witness.length - 1]
986
+ };
987
+ const stakingAmount = transaction.outs[outputIndex].value;
988
+ const slashingAmount = Math.floor(stakingAmount * slashingRate);
989
+ if (slashingAmount <= BTC_DUST_SAT) {
990
+ throw new Error("Slashing amount is less than dust limit");
991
+ }
992
+ const userFunds = stakingAmount - slashingAmount - minimumFee;
993
+ if (userFunds <= BTC_DUST_SAT) {
994
+ throw new Error("User funds are less than dust limit");
995
+ }
996
+ const psbt = new import_bitcoinjs_lib6.Psbt({ network });
997
+ psbt.setVersion(TRANSACTION_VERSION);
998
+ psbt.addInput({
999
+ hash: transaction.getHash(),
1000
+ index: outputIndex,
1001
+ tapInternalKey: internalPubkey,
1002
+ witnessUtxo: {
1003
+ value: stakingAmount,
1004
+ script: transaction.outs[outputIndex].script
1005
+ },
1006
+ tapLeafScript: [tapLeafScript],
1007
+ // not RBF-able
1008
+ sequence: NON_RBF_SEQUENCE
1009
+ });
1010
+ psbt.addOutput({
1011
+ script: Buffer.from(slashingPkScriptHex, "hex"),
1012
+ value: slashingAmount
1013
+ });
1014
+ const changeOutput = import_bitcoinjs_lib6.payments.p2tr({
1015
+ internalPubkey,
1016
+ scriptTree: { output: scripts.unbondingTimelockScript },
1017
+ network
1018
+ });
1019
+ psbt.addOutput({
1020
+ address: changeOutput.address,
1021
+ value: userFunds
1022
+ });
1023
+ psbt.setLocktime(0);
1024
+ return { psbt };
1025
+ }
1026
+ function unbondingTransaction(scripts, stakingTx, unbondingFee, network, outputIndex = 0) {
1027
+ if (unbondingFee <= 0) {
1028
+ throw new Error("Unbonding fee must be bigger than 0");
1029
+ }
1030
+ if (outputIndex < 0) {
1031
+ throw new Error("Output index must be bigger or equal to 0");
1032
+ }
1033
+ const tx = new import_bitcoinjs_lib6.Transaction();
1034
+ tx.version = TRANSACTION_VERSION;
1035
+ tx.addInput(
1036
+ stakingTx.getHash(),
1037
+ outputIndex,
1038
+ NON_RBF_SEQUENCE
1039
+ // not RBF-able
1040
+ );
1041
+ const unbondingOutputInfo = deriveUnbondingOutputInfo(scripts, network);
1042
+ const outputValue = stakingTx.outs[outputIndex].value - unbondingFee;
1043
+ if (outputValue < BTC_DUST_SAT) {
1044
+ throw new Error("Output value is less than dust limit for unbonding transaction");
1045
+ }
1046
+ if (!unbondingOutputInfo.outputAddress) {
1047
+ throw new Error("Unbonding output address is not defined");
1048
+ }
1049
+ tx.addOutput(
1050
+ unbondingOutputInfo.scriptPubKey,
1051
+ outputValue
1052
+ );
1053
+ tx.locktime = 0;
1054
+ return {
1055
+ transaction: tx,
1056
+ fee: unbondingFee
1057
+ };
1058
+ }
1059
+ var createCovenantWitness = (originalWitness, paramsCovenants, covenantSigs, covenantQuorum) => {
1060
+ if (covenantSigs.length < covenantQuorum) {
1061
+ throw new Error(
1062
+ `Not enough covenant signatures. Required: ${covenantQuorum}, got: ${covenantSigs.length}`
1063
+ );
1064
+ }
1065
+ for (const sig of covenantSigs) {
1066
+ const btcPkHexBuf = Buffer.from(sig.btcPkHex, "hex");
1067
+ if (!paramsCovenants.some((covenant) => covenant.equals(btcPkHexBuf))) {
1068
+ throw new Error(
1069
+ `Covenant signature public key ${sig.btcPkHex} not found in params covenants`
1070
+ );
1071
+ }
1072
+ }
1073
+ const covenantSigsBuffers = covenantSigs.slice(0, covenantQuorum).map((sig) => ({
1074
+ btcPkHex: Buffer.from(sig.btcPkHex, "hex"),
1075
+ sigHex: Buffer.from(sig.sigHex, "hex")
1076
+ }));
1077
+ const paramsCovenantsSorted = [...paramsCovenants].sort(Buffer.compare).reverse();
1078
+ const composedCovenantSigs = paramsCovenantsSorted.map((covenant) => {
1079
+ const covenantSig = covenantSigsBuffers.find(
1080
+ (sig) => sig.btcPkHex.compare(covenant) === 0
1081
+ );
1082
+ return covenantSig?.sigHex || Buffer.alloc(0);
1083
+ });
1084
+ return [...composedCovenantSigs, ...originalWitness];
1085
+ };
1086
+
1087
+ // src/staking/psbt.ts
1088
+ var import_bitcoinjs_lib8 = require("bitcoinjs-lib");
1089
+
1090
+ // src/utils/utxo/findInputUTXO.ts
1091
+ var findInputUTXO = (inputUTXOs, input) => {
1092
+ const inputUTXO = inputUTXOs.find(
1093
+ (u) => transactionIdToHash(u.txid).toString("hex") === input.hash.toString("hex") && u.vout === input.index
1094
+ );
1095
+ if (!inputUTXO) {
1096
+ throw new Error(
1097
+ `Input UTXO not found for txid: ${Buffer.from(input.hash).reverse().toString("hex")} and vout: ${input.index}`
1098
+ );
1099
+ }
1100
+ return inputUTXO;
1101
+ };
1102
+
1103
+ // src/utils/utxo/getScriptType.ts
1104
+ var import_bitcoinjs_lib7 = require("bitcoinjs-lib");
1105
+ var BitcoinScriptType = /* @__PURE__ */ ((BitcoinScriptType2) => {
1106
+ BitcoinScriptType2["P2PKH"] = "pubkeyhash";
1107
+ BitcoinScriptType2["P2SH"] = "scripthash";
1108
+ BitcoinScriptType2["P2WPKH"] = "witnesspubkeyhash";
1109
+ BitcoinScriptType2["P2WSH"] = "witnessscripthash";
1110
+ BitcoinScriptType2["P2TR"] = "taproot";
1111
+ return BitcoinScriptType2;
1112
+ })(BitcoinScriptType || {});
1113
+ var getScriptType = (script4) => {
1114
+ try {
1115
+ import_bitcoinjs_lib7.payments.p2pkh({ output: script4 });
1116
+ return "pubkeyhash" /* P2PKH */;
1117
+ } catch {
1118
+ }
1119
+ try {
1120
+ import_bitcoinjs_lib7.payments.p2sh({ output: script4 });
1121
+ return "scripthash" /* P2SH */;
1122
+ } catch {
1123
+ }
1124
+ try {
1125
+ import_bitcoinjs_lib7.payments.p2wpkh({ output: script4 });
1126
+ return "witnesspubkeyhash" /* P2WPKH */;
1127
+ } catch {
1128
+ }
1129
+ try {
1130
+ import_bitcoinjs_lib7.payments.p2wsh({ output: script4 });
1131
+ return "witnessscripthash" /* P2WSH */;
1132
+ } catch {
1133
+ }
1134
+ try {
1135
+ import_bitcoinjs_lib7.payments.p2tr({ output: script4 });
1136
+ return "taproot" /* P2TR */;
1137
+ } catch {
1138
+ }
1139
+ throw new Error("Unknown script type");
1140
+ };
1141
+
1142
+ // src/utils/utxo/getPsbtInputFields.ts
1143
+ var getPsbtInputFields = (utxo, publicKeyNoCoord) => {
1144
+ const scriptPubKey = Buffer.from(utxo.scriptPubKey, "hex");
1145
+ const type = getScriptType(scriptPubKey);
1146
+ switch (type) {
1147
+ case "pubkeyhash" /* P2PKH */: {
1148
+ if (!utxo.rawTxHex) {
1149
+ throw new Error("Missing rawTxHex for legacy P2PKH input");
1150
+ }
1151
+ return { nonWitnessUtxo: Buffer.from(utxo.rawTxHex, "hex") };
1152
+ }
1153
+ case "scripthash" /* P2SH */: {
1154
+ if (!utxo.rawTxHex) {
1155
+ throw new Error("Missing rawTxHex for P2SH input");
1156
+ }
1157
+ if (!utxo.redeemScript) {
1158
+ throw new Error("Missing redeemScript for P2SH input");
1159
+ }
1160
+ return {
1161
+ nonWitnessUtxo: Buffer.from(utxo.rawTxHex, "hex"),
1162
+ redeemScript: Buffer.from(utxo.redeemScript, "hex")
1163
+ };
1164
+ }
1165
+ case "witnesspubkeyhash" /* P2WPKH */: {
1166
+ return {
1167
+ witnessUtxo: {
1168
+ script: scriptPubKey,
1169
+ value: utxo.value
1170
+ }
1171
+ };
1172
+ }
1173
+ case "witnessscripthash" /* P2WSH */: {
1174
+ if (!utxo.witnessScript) {
1175
+ throw new Error("Missing witnessScript for P2WSH input");
1176
+ }
1177
+ return {
1178
+ witnessUtxo: {
1179
+ script: scriptPubKey,
1180
+ value: utxo.value
1181
+ },
1182
+ witnessScript: Buffer.from(utxo.witnessScript, "hex")
1183
+ };
1184
+ }
1185
+ case "taproot" /* P2TR */: {
1186
+ return {
1187
+ witnessUtxo: {
1188
+ script: scriptPubKey,
1189
+ value: utxo.value
1190
+ },
1191
+ // this is needed only if the wallet is in taproot mode
1192
+ ...publicKeyNoCoord && { tapInternalKey: publicKeyNoCoord }
1193
+ };
1194
+ }
1195
+ default:
1196
+ throw new Error(`Unsupported script type: ${type}`);
1197
+ }
1198
+ };
1199
+
1200
+ // src/staking/psbt.ts
1201
+ var stakingPsbt = (stakingTx, network, inputUTXOs, publicKeyNoCoord) => {
1202
+ if (publicKeyNoCoord && publicKeyNoCoord.length !== NO_COORD_PK_BYTE_LENGTH) {
1203
+ throw new Error("Invalid public key");
1204
+ }
1205
+ const psbt = new import_bitcoinjs_lib8.Psbt({ network });
1206
+ if (stakingTx.version !== void 0)
1207
+ psbt.setVersion(stakingTx.version);
1208
+ if (stakingTx.locktime !== void 0)
1209
+ psbt.setLocktime(stakingTx.locktime);
1210
+ stakingTx.ins.forEach((input) => {
1211
+ const inputUTXO = findInputUTXO(inputUTXOs, input);
1212
+ const psbtInputData = getPsbtInputFields(inputUTXO, publicKeyNoCoord);
1213
+ psbt.addInput({
1214
+ hash: input.hash,
1215
+ index: input.index,
1216
+ sequence: input.sequence,
1217
+ ...psbtInputData
1218
+ });
1219
+ });
1220
+ stakingTx.outs.forEach((o) => {
1221
+ psbt.addOutput({ script: o.script, value: o.value });
1222
+ });
1223
+ return psbt;
1224
+ };
1225
+ var unbondingPsbt = (scripts, unbondingTx, stakingTx, network) => {
1226
+ if (unbondingTx.outs.length !== 1) {
1227
+ throw new Error("Unbonding transaction must have exactly one output");
1228
+ }
1229
+ if (unbondingTx.ins.length !== 1) {
1230
+ throw new Error("Unbonding transaction must have exactly one input");
1231
+ }
1232
+ validateUnbondingOutput(scripts, unbondingTx, network);
1233
+ const psbt = new import_bitcoinjs_lib8.Psbt({ network });
1234
+ if (unbondingTx.version !== void 0) {
1235
+ psbt.setVersion(unbondingTx.version);
1236
+ }
1237
+ if (unbondingTx.locktime !== void 0) {
1238
+ psbt.setLocktime(unbondingTx.locktime);
1239
+ }
1240
+ const input = unbondingTx.ins[0];
1241
+ const outputIndex = input.index;
1242
+ const inputScriptTree = [
1243
+ { output: scripts.slashingScript },
1244
+ [{ output: scripts.unbondingScript }, { output: scripts.timelockScript }]
1245
+ ];
1246
+ const inputRedeem = {
1247
+ output: scripts.unbondingScript,
1248
+ redeemVersion: REDEEM_VERSION
1249
+ };
1250
+ const p2tr = import_bitcoinjs_lib8.payments.p2tr({
1251
+ internalPubkey,
1252
+ scriptTree: inputScriptTree,
1253
+ redeem: inputRedeem,
1254
+ network
1255
+ });
1256
+ const inputTapLeafScript = {
1257
+ leafVersion: inputRedeem.redeemVersion,
1258
+ script: inputRedeem.output,
1259
+ controlBlock: p2tr.witness[p2tr.witness.length - 1]
1260
+ };
1261
+ psbt.addInput({
1262
+ hash: input.hash,
1263
+ index: input.index,
1264
+ sequence: input.sequence,
1265
+ tapInternalKey: internalPubkey,
1266
+ witnessUtxo: {
1267
+ value: stakingTx.outs[outputIndex].value,
1268
+ script: stakingTx.outs[outputIndex].script
1269
+ },
1270
+ tapLeafScript: [inputTapLeafScript]
1271
+ });
1272
+ psbt.addOutput({
1273
+ script: unbondingTx.outs[0].script,
1274
+ value: unbondingTx.outs[0].value
1275
+ });
1276
+ return psbt;
1277
+ };
1278
+ var validateUnbondingOutput = (scripts, unbondingTx, network) => {
1279
+ const unbondingOutputInfo = deriveUnbondingOutputInfo(scripts, network);
1280
+ if (unbondingOutputInfo.scriptPubKey.toString("hex") !== unbondingTx.outs[0].script.toString("hex")) {
1281
+ throw new Error(
1282
+ "Unbonding output script does not match the expected script while building psbt"
1283
+ );
1284
+ }
1285
+ };
1286
+
1287
+ // src/staking/index.ts
1288
+ var Staking = class {
1289
+ constructor(network, stakerInfo, params, finalityProviderPkNoCoordHex, stakingTimelock) {
1290
+ if (!isValidBitcoinAddress(stakerInfo.address, network)) {
1291
+ throw new StakingError(
1292
+ "INVALID_INPUT" /* INVALID_INPUT */,
1293
+ "Invalid staker bitcoin address"
1294
+ );
1295
+ }
1296
+ if (!isValidNoCoordPublicKey(stakerInfo.publicKeyNoCoordHex)) {
1297
+ throw new StakingError(
1298
+ "INVALID_INPUT" /* INVALID_INPUT */,
1299
+ "Invalid staker public key"
1300
+ );
1301
+ }
1302
+ if (!isValidNoCoordPublicKey(finalityProviderPkNoCoordHex)) {
1303
+ throw new StakingError(
1304
+ "INVALID_INPUT" /* INVALID_INPUT */,
1305
+ "Invalid finality provider public key"
1306
+ );
1307
+ }
1308
+ validateParams(params);
1309
+ validateStakingTimelock(stakingTimelock, params);
1310
+ this.network = network;
1311
+ this.stakerInfo = stakerInfo;
1312
+ this.params = params;
1313
+ this.finalityProviderPkNoCoordHex = finalityProviderPkNoCoordHex;
1314
+ this.stakingTimelock = stakingTimelock;
1315
+ }
1316
+ /**
1317
+ * buildScripts builds the staking scripts for the staking transaction.
1318
+ * Note: different staking types may have different scripts.
1319
+ * e.g the observable staking script has a data embed script.
1320
+ *
1321
+ * @returns {StakingScripts} - The staking scripts.
1322
+ */
1323
+ buildScripts() {
1324
+ const { covenantQuorum, covenantNoCoordPks, unbondingTime } = this.params;
1325
+ let stakingScriptData;
1326
+ try {
1327
+ stakingScriptData = new StakingScriptData(
1328
+ Buffer.from(this.stakerInfo.publicKeyNoCoordHex, "hex"),
1329
+ [Buffer.from(this.finalityProviderPkNoCoordHex, "hex")],
1330
+ toBuffers(covenantNoCoordPks),
1331
+ covenantQuorum,
1332
+ this.stakingTimelock,
1333
+ unbondingTime
1334
+ );
1335
+ } catch (error) {
1336
+ throw StakingError.fromUnknown(
1337
+ error,
1338
+ "SCRIPT_FAILURE" /* SCRIPT_FAILURE */,
1339
+ "Cannot build staking script data"
1340
+ );
1341
+ }
1342
+ let scripts;
1343
+ try {
1344
+ scripts = stakingScriptData.buildScripts();
1345
+ } catch (error) {
1346
+ throw StakingError.fromUnknown(
1347
+ error,
1348
+ "SCRIPT_FAILURE" /* SCRIPT_FAILURE */,
1349
+ "Cannot build staking scripts"
1350
+ );
1351
+ }
1352
+ return scripts;
1353
+ }
1354
+ /**
1355
+ * Create a staking transaction for staking.
1356
+ *
1357
+ * @param {number} stakingAmountSat - The amount to stake in satoshis.
1358
+ * @param {UTXO[]} inputUTXOs - The UTXOs to use as inputs for the staking
1359
+ * transaction.
1360
+ * @param {number} feeRate - The fee rate for the transaction in satoshis per byte.
1361
+ * @returns {TransactionResult} - An object containing the unsigned
1362
+ * transaction, and fee
1363
+ * @throws {StakingError} - If the transaction cannot be built
1364
+ */
1365
+ createStakingTransaction(stakingAmountSat, inputUTXOs, feeRate) {
1366
+ validateStakingTxInputData(
1367
+ stakingAmountSat,
1368
+ this.stakingTimelock,
1369
+ this.params,
1370
+ inputUTXOs,
1371
+ feeRate
1372
+ );
1373
+ const scripts = this.buildScripts();
1374
+ try {
1375
+ const { transaction, fee } = stakingTransaction(
1376
+ scripts,
1377
+ stakingAmountSat,
1378
+ this.stakerInfo.address,
1379
+ inputUTXOs,
1380
+ this.network,
1381
+ feeRate
1382
+ );
1383
+ return {
1384
+ transaction,
1385
+ fee
1386
+ };
1387
+ } catch (error) {
1388
+ throw StakingError.fromUnknown(
1389
+ error,
1390
+ "BUILD_TRANSACTION_FAILURE" /* BUILD_TRANSACTION_FAILURE */,
1391
+ "Cannot build unsigned staking transaction"
1392
+ );
1393
+ }
1394
+ }
1395
+ /**
1396
+ * Create a staking psbt based on the existing staking transaction.
1397
+ *
1398
+ * @param {Transaction} stakingTx - The staking transaction.
1399
+ * @param {UTXO[]} inputUTXOs - The UTXOs to use as inputs for the staking
1400
+ * transaction. The UTXOs that were used to create the staking transaction should
1401
+ * be included in this array.
1402
+ * @returns {Psbt} - The psbt.
1403
+ */
1404
+ toStakingPsbt(stakingTx, inputUTXOs) {
1405
+ const scripts = this.buildScripts();
1406
+ const stakingOutputInfo = deriveStakingOutputInfo(scripts, this.network);
1407
+ findMatchingTxOutputIndex(
1408
+ stakingTx,
1409
+ stakingOutputInfo.outputAddress,
1410
+ this.network
1411
+ );
1412
+ return stakingPsbt(
1413
+ stakingTx,
1414
+ this.network,
1415
+ inputUTXOs,
1416
+ isTaproot(
1417
+ this.stakerInfo.address,
1418
+ this.network
1419
+ ) ? Buffer.from(this.stakerInfo.publicKeyNoCoordHex, "hex") : void 0
1420
+ );
1421
+ }
1422
+ /**
1423
+ * Create an unbonding transaction for staking.
1424
+ *
1425
+ * @param {Transaction} stakingTx - The staking transaction to unbond.
1426
+ * @returns {TransactionResult} - An object containing the unsigned
1427
+ * transaction, and fee
1428
+ * @throws {StakingError} - If the transaction cannot be built
1429
+ */
1430
+ createUnbondingTransaction(stakingTx) {
1431
+ const scripts = this.buildScripts();
1432
+ const { outputAddress } = deriveStakingOutputInfo(scripts, this.network);
1433
+ const stakingOutputIndex = findMatchingTxOutputIndex(
1434
+ stakingTx,
1435
+ outputAddress,
1436
+ this.network
1437
+ );
1438
+ try {
1439
+ const { transaction } = unbondingTransaction(
1440
+ scripts,
1441
+ stakingTx,
1442
+ this.params.unbondingFeeSat,
1443
+ this.network,
1444
+ stakingOutputIndex
1445
+ );
1446
+ return {
1447
+ transaction,
1448
+ fee: this.params.unbondingFeeSat
1449
+ };
1450
+ } catch (error) {
1451
+ throw StakingError.fromUnknown(
1452
+ error,
1453
+ "BUILD_TRANSACTION_FAILURE" /* BUILD_TRANSACTION_FAILURE */,
1454
+ "Cannot build the unbonding transaction"
1455
+ );
1456
+ }
1457
+ }
1458
+ /**
1459
+ * Create an unbonding psbt based on the existing unbonding transaction and
1460
+ * staking transaction.
1461
+ *
1462
+ * @param {Transaction} unbondingTx - The unbonding transaction.
1463
+ * @param {Transaction} stakingTx - The staking transaction.
1464
+ *
1465
+ * @returns {Psbt} - The psbt.
1466
+ */
1467
+ toUnbondingPsbt(unbondingTx, stakingTx) {
1468
+ return unbondingPsbt(
1469
+ this.buildScripts(),
1470
+ unbondingTx,
1471
+ stakingTx,
1472
+ this.network
1473
+ );
1474
+ }
1475
+ /**
1476
+ * Creates a withdrawal transaction that spends from an unbonding or slashing
1477
+ * transaction. The timelock on the input transaction must have expired before
1478
+ * this withdrawal can be valid.
1479
+ *
1480
+ * @param {Transaction} earlyUnbondedTx - The unbonding or slashing
1481
+ * transaction to withdraw from
1482
+ * @param {number} feeRate - Fee rate in satoshis per byte for the withdrawal
1483
+ * transaction
1484
+ * @returns {PsbtResult} - Contains the unsigned PSBT and fee amount
1485
+ * @throws {StakingError} - If the input transaction is invalid or withdrawal
1486
+ * transaction cannot be built
1487
+ */
1488
+ createWithdrawEarlyUnbondedTransaction(earlyUnbondedTx, feeRate) {
1489
+ const scripts = this.buildScripts();
1490
+ try {
1491
+ return withdrawEarlyUnbondedTransaction(
1492
+ scripts,
1493
+ earlyUnbondedTx,
1494
+ this.stakerInfo.address,
1495
+ this.network,
1496
+ feeRate
1497
+ );
1498
+ } catch (error) {
1499
+ throw StakingError.fromUnknown(
1500
+ error,
1501
+ "BUILD_TRANSACTION_FAILURE" /* BUILD_TRANSACTION_FAILURE */,
1502
+ "Cannot build unsigned withdraw early unbonded transaction"
1503
+ );
1504
+ }
1505
+ }
1506
+ /**
1507
+ * Create a withdrawal psbt that spends a naturally expired staking
1508
+ * transaction.
1509
+ *
1510
+ * @param {Transaction} stakingTx - The staking transaction to withdraw from.
1511
+ * @param {number} feeRate - The fee rate for the transaction in satoshis per byte.
1512
+ * @returns {PsbtResult} - An object containing the unsigned psbt and fee
1513
+ * @throws {StakingError} - If the delegation is invalid or the transaction cannot be built
1514
+ */
1515
+ createWithdrawStakingExpiredPsbt(stakingTx, feeRate) {
1516
+ const scripts = this.buildScripts();
1517
+ const { outputAddress } = deriveStakingOutputInfo(scripts, this.network);
1518
+ const stakingOutputIndex = findMatchingTxOutputIndex(
1519
+ stakingTx,
1520
+ outputAddress,
1521
+ this.network
1522
+ );
1523
+ try {
1524
+ return withdrawTimelockUnbondedTransaction(
1525
+ scripts,
1526
+ stakingTx,
1527
+ this.stakerInfo.address,
1528
+ this.network,
1529
+ feeRate,
1530
+ stakingOutputIndex
1531
+ );
1532
+ } catch (error) {
1533
+ throw StakingError.fromUnknown(
1534
+ error,
1535
+ "BUILD_TRANSACTION_FAILURE" /* BUILD_TRANSACTION_FAILURE */,
1536
+ "Cannot build unsigned timelock unbonded transaction"
1537
+ );
1538
+ }
1539
+ }
1540
+ /**
1541
+ * Create a slashing psbt spending from the staking output.
1542
+ *
1543
+ * @param {Transaction} stakingTx - The staking transaction to slash.
1544
+ * @returns {PsbtResult} - An object containing the unsigned psbt and fee
1545
+ * @throws {StakingError} - If the delegation is invalid or the transaction cannot be built
1546
+ */
1547
+ createStakingOutputSlashingPsbt(stakingTx) {
1548
+ if (!this.params.slashing) {
1549
+ throw new StakingError(
1550
+ "INVALID_PARAMS" /* INVALID_PARAMS */,
1551
+ "Slashing parameters are missing"
1552
+ );
1553
+ }
1554
+ const scripts = this.buildScripts();
1555
+ try {
1556
+ const { psbt } = slashTimelockUnbondedTransaction(
1557
+ scripts,
1558
+ stakingTx,
1559
+ this.params.slashing.slashingPkScriptHex,
1560
+ this.params.slashing.slashingRate,
1561
+ this.params.slashing.minSlashingTxFeeSat,
1562
+ this.network
1563
+ );
1564
+ return {
1565
+ psbt,
1566
+ fee: this.params.slashing.minSlashingTxFeeSat
1567
+ };
1568
+ } catch (error) {
1569
+ throw StakingError.fromUnknown(
1570
+ error,
1571
+ "BUILD_TRANSACTION_FAILURE" /* BUILD_TRANSACTION_FAILURE */,
1572
+ "Cannot build the slash timelock unbonded transaction"
1573
+ );
1574
+ }
1575
+ }
1576
+ /**
1577
+ * Create a slashing psbt for an unbonding output.
1578
+ *
1579
+ * @param {Transaction} unbondingTx - The unbonding transaction to slash.
1580
+ * @returns {PsbtResult} - An object containing the unsigned psbt and fee
1581
+ * @throws {StakingError} - If the delegation is invalid or the transaction cannot be built
1582
+ */
1583
+ createUnbondingOutputSlashingPsbt(unbondingTx) {
1584
+ if (!this.params.slashing) {
1585
+ throw new StakingError(
1586
+ "INVALID_PARAMS" /* INVALID_PARAMS */,
1587
+ "Slashing parameters are missing"
1588
+ );
1589
+ }
1590
+ const scripts = this.buildScripts();
1591
+ try {
1592
+ const { psbt } = slashEarlyUnbondedTransaction(
1593
+ scripts,
1594
+ unbondingTx,
1595
+ this.params.slashing.slashingPkScriptHex,
1596
+ this.params.slashing.slashingRate,
1597
+ this.params.slashing.minSlashingTxFeeSat,
1598
+ this.network
1599
+ );
1600
+ return {
1601
+ psbt,
1602
+ fee: this.params.slashing.minSlashingTxFeeSat
1603
+ };
1604
+ } catch (error) {
1605
+ throw StakingError.fromUnknown(
1606
+ error,
1607
+ "BUILD_TRANSACTION_FAILURE" /* BUILD_TRANSACTION_FAILURE */,
1608
+ "Cannot build the slash early unbonded transaction"
1609
+ );
1610
+ }
1611
+ }
1612
+ /**
1613
+ * Create a withdraw slashing psbt that spends a slashing transaction from the
1614
+ * staking output.
1615
+ *
1616
+ * @param {Transaction} slashingTx - The slashing transaction.
1617
+ * @param {number} feeRate - The fee rate for the transaction in satoshis per byte.
1618
+ * @returns {PsbtResult} - An object containing the unsigned psbt and fee
1619
+ * @throws {StakingError} - If the delegation is invalid or the transaction cannot be built
1620
+ */
1621
+ createWithdrawSlashingPsbt(slashingTx, feeRate) {
1622
+ const scripts = this.buildScripts();
1623
+ const slashingOutputInfo = deriveSlashingOutput(scripts, this.network);
1624
+ const slashingOutputIndex = findMatchingTxOutputIndex(
1625
+ slashingTx,
1626
+ slashingOutputInfo.outputAddress,
1627
+ this.network
1628
+ );
1629
+ try {
1630
+ return withdrawSlashingTransaction(
1631
+ scripts,
1632
+ slashingTx,
1633
+ this.stakerInfo.address,
1634
+ this.network,
1635
+ feeRate,
1636
+ slashingOutputIndex
1637
+ );
1638
+ } catch (error) {
1639
+ throw StakingError.fromUnknown(
1640
+ error,
1641
+ "BUILD_TRANSACTION_FAILURE" /* BUILD_TRANSACTION_FAILURE */,
1642
+ "Cannot build withdraw slashing transaction"
1643
+ );
1644
+ }
1645
+ }
1646
+ };
1647
+
1648
+ // src/staking/observable/observableStakingScript.ts
1649
+ var import_bitcoinjs_lib9 = require("bitcoinjs-lib");
1650
+ var ObservableStakingScriptData = class extends StakingScriptData {
1651
+ constructor(stakerKey, finalityProviderKeys, covenantKeys, covenantThreshold, stakingTimelock, unbondingTimelock, magicBytes) {
1652
+ super(
1653
+ stakerKey,
1654
+ finalityProviderKeys,
1655
+ covenantKeys,
1656
+ covenantThreshold,
1657
+ stakingTimelock,
1658
+ unbondingTimelock
1659
+ );
1660
+ if (!magicBytes) {
1661
+ throw new Error("Missing required input values");
1662
+ }
1663
+ if (magicBytes.length != MAGIC_BYTES_LEN) {
1664
+ throw new Error("Invalid script data provided");
1665
+ }
1666
+ this.magicBytes = magicBytes;
1667
+ }
1668
+ /**
1669
+ * Builds a data embed script for staking in the form:
1670
+ * OP_RETURN || <serializedStakingData>
1671
+ * where serializedStakingData is the concatenation of:
1672
+ * MagicBytes || Version || StakerPublicKey || FinalityProviderPublicKey || StakingTimeLock
1673
+ * Note: Only a single finality provider key is supported for now in phase 1
1674
+ * @throws {Error} If the number of finality provider keys is not equal to 1.
1675
+ * @returns {Buffer} The compiled data embed script.
1676
+ */
1677
+ buildDataEmbedScript() {
1678
+ if (this.finalityProviderKeys.length != 1) {
1679
+ throw new Error("Only a single finality provider key is supported");
1680
+ }
1681
+ const version = Buffer.alloc(1);
1682
+ version.writeUInt8(0);
1683
+ const stakingTimeLock = Buffer.alloc(2);
1684
+ stakingTimeLock.writeUInt16BE(this.stakingTimeLock);
1685
+ const serializedStakingData = Buffer.concat([
1686
+ this.magicBytes,
1687
+ version,
1688
+ this.stakerKey,
1689
+ this.finalityProviderKeys[0],
1690
+ stakingTimeLock
1691
+ ]);
1692
+ return import_bitcoinjs_lib9.script.compile([import_bitcoinjs_lib9.opcodes.OP_RETURN, serializedStakingData]);
1693
+ }
1694
+ /**
1695
+ * Builds the staking scripts.
1696
+ * @returns {ObservableStakingScripts} The staking scripts that can be used to stake.
1697
+ * contains the timelockScript, unbondingScript, slashingScript,
1698
+ * unbondingTimelockScript, and dataEmbedScript.
1699
+ * @throws {Error} If script data is invalid.
1700
+ */
1701
+ buildScripts() {
1702
+ const scripts = super.buildScripts();
1703
+ return {
1704
+ ...scripts,
1705
+ dataEmbedScript: this.buildDataEmbedScript()
1706
+ };
1707
+ }
1708
+ };
1709
+
1710
+ // src/staking/observable/index.ts
1711
+ var ObservableStaking = class extends Staking {
1712
+ constructor(network, stakerInfo, params, finalityProviderPkNoCoordHex, stakingTimelock) {
1713
+ super(
1714
+ network,
1715
+ stakerInfo,
1716
+ params,
1717
+ finalityProviderPkNoCoordHex,
1718
+ stakingTimelock
1719
+ );
1720
+ if (!params.tag) {
1721
+ throw new StakingError(
1722
+ "INVALID_INPUT" /* INVALID_INPUT */,
1723
+ "Observable staking parameters must include tag"
1724
+ );
1725
+ }
1726
+ if (!params.btcActivationHeight) {
1727
+ throw new StakingError(
1728
+ "INVALID_INPUT" /* INVALID_INPUT */,
1729
+ "Observable staking parameters must include a positive activation height"
1730
+ );
1731
+ }
1732
+ this.params = params;
1733
+ }
1734
+ /**
1735
+ * Build the staking scripts for observable staking.
1736
+ * This method overwrites the base method to include the OP_RETURN tag based
1737
+ * on the tag provided in the parameters.
1738
+ *
1739
+ * @returns {ObservableStakingScripts} - The staking scripts for observable staking.
1740
+ * @throws {StakingError} - If the scripts cannot be built.
1741
+ */
1742
+ buildScripts() {
1743
+ const { covenantQuorum, covenantNoCoordPks, unbondingTime, tag } = this.params;
1744
+ let stakingScriptData;
1745
+ try {
1746
+ stakingScriptData = new ObservableStakingScriptData(
1747
+ Buffer.from(this.stakerInfo.publicKeyNoCoordHex, "hex"),
1748
+ [Buffer.from(this.finalityProviderPkNoCoordHex, "hex")],
1749
+ toBuffers(covenantNoCoordPks),
1750
+ covenantQuorum,
1751
+ this.stakingTimelock,
1752
+ unbondingTime,
1753
+ Buffer.from(tag, "hex")
1754
+ );
1755
+ } catch (error) {
1756
+ throw StakingError.fromUnknown(
1757
+ error,
1758
+ "SCRIPT_FAILURE" /* SCRIPT_FAILURE */,
1759
+ "Cannot build staking script data"
1760
+ );
1761
+ }
1762
+ let scripts;
1763
+ try {
1764
+ scripts = stakingScriptData.buildScripts();
1765
+ } catch (error) {
1766
+ throw StakingError.fromUnknown(
1767
+ error,
1768
+ "SCRIPT_FAILURE" /* SCRIPT_FAILURE */,
1769
+ "Cannot build staking scripts"
1770
+ );
1771
+ }
1772
+ return scripts;
1773
+ }
1774
+ /**
1775
+ * Create a staking transaction for observable staking.
1776
+ * This overwrites the method from the Staking class with the addtion
1777
+ * of the
1778
+ * 1. OP_RETURN tag in the staking scripts
1779
+ * 2. lockHeight parameter
1780
+ *
1781
+ * @param {number} stakingAmountSat - The amount to stake in satoshis.
1782
+ * @param {UTXO[]} inputUTXOs - The UTXOs to use as inputs for the staking
1783
+ * transaction.
1784
+ * @param {number} feeRate - The fee rate for the transaction in satoshis per byte.
1785
+ * @returns {TransactionResult} - An object containing the unsigned transaction,
1786
+ * and fee
1787
+ */
1788
+ createStakingTransaction(stakingAmountSat, inputUTXOs, feeRate) {
1789
+ validateStakingTxInputData(
1790
+ stakingAmountSat,
1791
+ this.stakingTimelock,
1792
+ this.params,
1793
+ inputUTXOs,
1794
+ feeRate
1795
+ );
1796
+ const scripts = this.buildScripts();
1797
+ try {
1798
+ const { transaction, fee } = stakingTransaction(
1799
+ scripts,
1800
+ stakingAmountSat,
1801
+ this.stakerInfo.address,
1802
+ inputUTXOs,
1803
+ this.network,
1804
+ feeRate,
1805
+ // `lockHeight` is exclusive of the provided value.
1806
+ // For example, if a Bitcoin height of X is provided,
1807
+ // the transaction will be included starting from height X+1.
1808
+ // https://learnmeabitcoin.com/technical/transaction/locktime/
1809
+ this.params.btcActivationHeight - 1
1810
+ );
1811
+ return {
1812
+ transaction,
1813
+ fee
1814
+ };
1815
+ } catch (error) {
1816
+ throw StakingError.fromUnknown(
1817
+ error,
1818
+ "BUILD_TRANSACTION_FAILURE" /* BUILD_TRANSACTION_FAILURE */,
1819
+ "Cannot build unsigned staking transaction"
1820
+ );
1821
+ }
1822
+ }
1823
+ /**
1824
+ * Create a staking psbt for observable staking.
1825
+ *
1826
+ * @param {Transaction} stakingTx - The staking transaction.
1827
+ * @param {UTXO[]} inputUTXOs - The UTXOs to use as inputs for the staking
1828
+ * transaction.
1829
+ * @returns {Psbt} - The psbt.
1830
+ */
1831
+ toStakingPsbt(stakingTx, inputUTXOs) {
1832
+ return stakingPsbt(
1833
+ stakingTx,
1834
+ this.network,
1835
+ inputUTXOs,
1836
+ isTaproot(
1837
+ this.stakerInfo.address,
1838
+ this.network
1839
+ ) ? Buffer.from(this.stakerInfo.publicKeyNoCoordHex, "hex") : void 0
1840
+ );
1841
+ }
1842
+ };
1843
+
1844
+ // src/utils/staking/param.ts
1845
+ var getBabylonParamByBtcHeight = (height, babylonParamsVersions) => {
1846
+ const sortedParams = [...babylonParamsVersions].sort(
1847
+ (a, b) => b.btcActivationHeight - a.btcActivationHeight
1848
+ );
1849
+ const params = sortedParams.find(
1850
+ (p) => height >= p.btcActivationHeight
1851
+ );
1852
+ if (!params)
1853
+ throw new Error(`Babylon params not found for height ${height}`);
1854
+ return params;
1855
+ };
1856
+ var getBabylonParamByVersion = (version, babylonParams) => {
1857
+ const params = babylonParams.find((p) => p.version === version);
1858
+ if (!params)
1859
+ throw new Error(`Babylon params not found for version ${version}`);
1860
+ return params;
1861
+ };
1862
+
1863
+ // src/staking/manager.ts
1864
+ var import_bitcoinjs_lib10 = require("bitcoinjs-lib");
1865
+ var import_encoding2 = require("@cosmjs/encoding");
1866
+ var import_babylon_proto_ts = require("@babylonlabs-io/babylon-proto-ts");
1867
+ var import_pop = require("@babylonlabs-io/babylon-proto-ts/dist/generated/babylon/btcstaking/v1/pop");
1868
+
1869
+ // src/constants/registry.ts
1870
+ var BABYLON_REGISTRY_TYPE_URLS = {
1871
+ MsgCreateBTCDelegation: "/babylon.btcstaking.v1.MsgCreateBTCDelegation"
1872
+ };
1873
+
1874
+ // src/utils/index.ts
1875
+ var reverseBuffer = (buffer) => {
1876
+ const clonedBuffer = new Uint8Array(buffer);
1877
+ if (clonedBuffer.length < 1)
1878
+ return clonedBuffer;
1879
+ for (let i = 0, j = clonedBuffer.length - 1; i < clonedBuffer.length / 2; i++, j--) {
1880
+ let tmp = clonedBuffer[i];
1881
+ clonedBuffer[i] = clonedBuffer[j];
1882
+ clonedBuffer[j] = tmp;
1883
+ }
1884
+ return clonedBuffer;
1885
+ };
1886
+ var uint8ArrayToHex = (uint8Array) => {
1887
+ return Array.from(uint8Array).map((byte) => byte.toString(16).padStart(2, "0")).join("");
1888
+ };
1889
+
1890
+ // src/utils/babylon.ts
1891
+ var import_encoding = require("@cosmjs/encoding");
1892
+ var isValidBabylonAddress = (address4) => {
1893
+ try {
1894
+ const { prefix } = (0, import_encoding.fromBech32)(address4);
1895
+ return prefix === "bbn";
1896
+ } catch (error) {
1897
+ return false;
1898
+ }
1899
+ };
1900
+
1901
+ // src/staking/manager.ts
1902
+ var SigningStep = /* @__PURE__ */ ((SigningStep2) => {
1903
+ SigningStep2["STAKING_SLASHING"] = "staking-slashing";
1904
+ SigningStep2["UNBONDING_SLASHING"] = "unbonding-slashing";
1905
+ SigningStep2["PROOF_OF_POSSESSION"] = "proof-of-possession";
1906
+ SigningStep2["CREATE_BTC_DELEGATION_MSG"] = "create-btc-delegation-msg";
1907
+ SigningStep2["STAKING"] = "staking";
1908
+ SigningStep2["UNBONDING"] = "unbonding";
1909
+ SigningStep2["WITHDRAW_STAKING_EXPIRED"] = "withdraw-staking-expired";
1910
+ SigningStep2["WITHDRAW_EARLY_UNBONDED"] = "withdraw-early-unbonded";
1911
+ SigningStep2["WITHDRAW_SLASHING"] = "withdraw-slashing";
1912
+ return SigningStep2;
1913
+ })(SigningStep || {});
1914
+ var BabylonBtcStakingManager = class {
1915
+ constructor(network, stakingParams, btcProvider, babylonProvider) {
1916
+ this.network = network;
1917
+ this.btcProvider = btcProvider;
1918
+ this.babylonProvider = babylonProvider;
1919
+ if (stakingParams.length === 0) {
1920
+ throw new Error("No staking parameters provided");
1921
+ }
1922
+ this.stakingParams = stakingParams;
1923
+ }
1924
+ /**
1925
+ * Creates a signed Pre-Staking Registration transaction that is ready to be
1926
+ * sent to the Babylon chain.
1927
+ * @param stakerBtcInfo - The staker BTC info which includes the BTC address
1928
+ * and the no-coord public key in hex format.
1929
+ * @param stakingInput - The staking inputs.
1930
+ * @param babylonBtcTipHeight - The Babylon BTC tip height.
1931
+ * @param inputUTXOs - The UTXOs that will be used to pay for the staking
1932
+ * transaction.
1933
+ * @param feeRate - The fee rate in satoshis per byte.
1934
+ * @param babylonAddress - The Babylon bech32 encoded address of the staker.
1935
+ * @returns The signed babylon pre-staking registration transaction in base64
1936
+ * format.
1937
+ */
1938
+ async preStakeRegistrationBabylonTransaction(stakerBtcInfo, stakingInput, babylonBtcTipHeight, inputUTXOs, feeRate, babylonAddress) {
1939
+ if (babylonBtcTipHeight === 0) {
1940
+ throw new Error("Babylon BTC tip height cannot be 0");
1941
+ }
1942
+ if (inputUTXOs.length === 0) {
1943
+ throw new Error("No input UTXOs provided");
1944
+ }
1945
+ if (!isValidBabylonAddress(babylonAddress)) {
1946
+ throw new Error("Invalid Babylon address");
1947
+ }
1948
+ const params = getBabylonParamByBtcHeight(
1949
+ babylonBtcTipHeight,
1950
+ this.stakingParams
1951
+ );
1952
+ const staking = new Staking(
1953
+ this.network,
1954
+ stakerBtcInfo,
1955
+ params,
1956
+ stakingInput.finalityProviderPkNoCoordHex,
1957
+ stakingInput.stakingTimelock
1958
+ );
1959
+ const { transaction } = staking.createStakingTransaction(
1960
+ stakingInput.stakingAmountSat,
1961
+ inputUTXOs,
1962
+ feeRate
1963
+ );
1964
+ const msg = await this.createBtcDelegationMsg(
1965
+ staking,
1966
+ stakingInput,
1967
+ transaction,
1968
+ babylonAddress,
1969
+ stakerBtcInfo,
1970
+ params
1971
+ );
1972
+ return {
1973
+ signedBabylonTx: await this.babylonProvider.signTransaction(
1974
+ "create-btc-delegation-msg" /* CREATE_BTC_DELEGATION_MSG */,
1975
+ msg
1976
+ ),
1977
+ stakingTx: transaction
1978
+ };
1979
+ }
1980
+ /**
1981
+ * Creates a signed post-staking registration transaction that is ready to be
1982
+ * sent to the Babylon chain. This is used when a staking transaction is
1983
+ * already created and included in a BTC block and we want to register it on
1984
+ * the Babylon chain.
1985
+ * @param stakerBtcInfo - The staker BTC info which includes the BTC address
1986
+ * and the no-coord public key in hex format.
1987
+ * @param stakingTx - The staking transaction.
1988
+ * @param stakingTxHeight - The BTC height in which the staking transaction
1989
+ * is included.
1990
+ * @param stakingInput - The staking inputs.
1991
+ * @param inclusionProof - The inclusion proof of the staking transaction.
1992
+ * @param babylonAddress - The Babylon bech32 encoded address of the staker.
1993
+ * @returns The signed babylon transaction in base64 format.
1994
+ */
1995
+ async postStakeRegistrationBabylonTransaction(stakerBtcInfo, stakingTx, stakingTxHeight, stakingInput, inclusionProof, babylonAddress) {
1996
+ const params = getBabylonParamByBtcHeight(stakingTxHeight, this.stakingParams);
1997
+ if (!isValidBabylonAddress(babylonAddress)) {
1998
+ throw new Error("Invalid Babylon address");
1999
+ }
2000
+ const stakingInstance = new Staking(
2001
+ this.network,
2002
+ stakerBtcInfo,
2003
+ params,
2004
+ stakingInput.finalityProviderPkNoCoordHex,
2005
+ stakingInput.stakingTimelock
2006
+ );
2007
+ const scripts = stakingInstance.buildScripts();
2008
+ const stakingOutputInfo = deriveStakingOutputInfo(scripts, this.network);
2009
+ findMatchingTxOutputIndex(
2010
+ stakingTx,
2011
+ stakingOutputInfo.outputAddress,
2012
+ this.network
2013
+ );
2014
+ const delegationMsg = await this.createBtcDelegationMsg(
2015
+ stakingInstance,
2016
+ stakingInput,
2017
+ stakingTx,
2018
+ babylonAddress,
2019
+ stakerBtcInfo,
2020
+ params,
2021
+ this.getInclusionProof(inclusionProof)
2022
+ );
2023
+ return {
2024
+ signedBabylonTx: await this.babylonProvider.signTransaction(
2025
+ "create-btc-delegation-msg" /* CREATE_BTC_DELEGATION_MSG */,
2026
+ delegationMsg
2027
+ )
2028
+ };
2029
+ }
2030
+ /**
2031
+ * Estimates the BTC fee required for staking.
2032
+ * @param stakerBtcInfo - The staker BTC info which includes the BTC address
2033
+ * and the no-coord public key in hex format.
2034
+ * @param babylonBtcTipHeight - The BTC tip height recorded on the Babylon
2035
+ * chain.
2036
+ * @param stakingInput - The staking inputs.
2037
+ * @param inputUTXOs - The UTXOs that will be used to pay for the staking
2038
+ * transaction.
2039
+ * @param feeRate - The fee rate in satoshis per byte.
2040
+ * @returns The estimated BTC fee in satoshis.
2041
+ */
2042
+ estimateBtcStakingFee(stakerBtcInfo, babylonBtcTipHeight, stakingInput, inputUTXOs, feeRate) {
2043
+ if (babylonBtcTipHeight === 0) {
2044
+ throw new Error("Babylon BTC tip height cannot be 0");
2045
+ }
2046
+ const params = getBabylonParamByBtcHeight(
2047
+ babylonBtcTipHeight,
2048
+ this.stakingParams
2049
+ );
2050
+ const staking = new Staking(
2051
+ this.network,
2052
+ stakerBtcInfo,
2053
+ params,
2054
+ stakingInput.finalityProviderPkNoCoordHex,
2055
+ stakingInput.stakingTimelock
2056
+ );
2057
+ const { fee: stakingFee } = staking.createStakingTransaction(
2058
+ stakingInput.stakingAmountSat,
2059
+ inputUTXOs,
2060
+ feeRate
2061
+ );
2062
+ return stakingFee;
2063
+ }
2064
+ /**
2065
+ * Creates a signed staking transaction that is ready to be sent to the BTC
2066
+ * network.
2067
+ * @param stakerBtcInfo - The staker BTC info which includes the BTC address
2068
+ * and the no-coord public key in hex format.
2069
+ * @param stakingInput - The staking inputs.
2070
+ * @param unsignedStakingTx - The unsigned staking transaction.
2071
+ * @param inputUTXOs - The UTXOs that will be used to pay for the staking
2072
+ * transaction.
2073
+ * @param stakingParamsVersion - The params version that was used to create the
2074
+ * delegation in Babylon chain
2075
+ * @returns The signed staking transaction.
2076
+ */
2077
+ async createSignedBtcStakingTransaction(stakerBtcInfo, stakingInput, unsignedStakingTx, inputUTXOs, stakingParamsVersion) {
2078
+ const params = getBabylonParamByVersion(stakingParamsVersion, this.stakingParams);
2079
+ if (inputUTXOs.length === 0) {
2080
+ throw new Error("No input UTXOs provided");
2081
+ }
2082
+ const staking = new Staking(
2083
+ this.network,
2084
+ stakerBtcInfo,
2085
+ params,
2086
+ stakingInput.finalityProviderPkNoCoordHex,
2087
+ stakingInput.stakingTimelock
2088
+ );
2089
+ const stakingPsbt2 = staking.toStakingPsbt(
2090
+ unsignedStakingTx,
2091
+ inputUTXOs
2092
+ );
2093
+ const signedStakingPsbtHex = await this.btcProvider.signPsbt(
2094
+ "staking" /* STAKING */,
2095
+ stakingPsbt2.toHex()
2096
+ );
2097
+ return import_bitcoinjs_lib10.Psbt.fromHex(signedStakingPsbtHex).extractTransaction();
2098
+ }
2099
+ /**
2100
+ * Creates a partial signed unbonding transaction that is only signed by the
2101
+ * staker. In order to complete the unbonding transaction, the covenant
2102
+ * unbonding signatures need to be added to the transaction before sending it
2103
+ * to the BTC network.
2104
+ * NOTE: This method should only be used for Babylon phase-1 unbonding.
2105
+ * @param stakerBtcInfo - The staker BTC info which includes the BTC address
2106
+ * and the no-coord public key in hex format.
2107
+ * @param stakingInput - The staking inputs.
2108
+ * @param stakingParamsVersion - The params version that was used to create the
2109
+ * delegation in Babylon chain
2110
+ * @param stakingTx - The staking transaction.
2111
+ * @returns The partial signed unbonding transaction and its fee.
2112
+ */
2113
+ async createPartialSignedBtcUnbondingTransaction(stakerBtcInfo, stakingInput, stakingParamsVersion, stakingTx) {
2114
+ const params = getBabylonParamByVersion(
2115
+ stakingParamsVersion,
2116
+ this.stakingParams
2117
+ );
2118
+ const staking = new Staking(
2119
+ this.network,
2120
+ stakerBtcInfo,
2121
+ params,
2122
+ stakingInput.finalityProviderPkNoCoordHex,
2123
+ stakingInput.stakingTimelock
2124
+ );
2125
+ const {
2126
+ transaction: unbondingTx,
2127
+ fee
2128
+ } = staking.createUnbondingTransaction(stakingTx);
2129
+ const psbt = staking.toUnbondingPsbt(unbondingTx, stakingTx);
2130
+ const signedUnbondingPsbtHex = await this.btcProvider.signPsbt(
2131
+ "unbonding" /* UNBONDING */,
2132
+ psbt.toHex()
2133
+ );
2134
+ const signedUnbondingTx = import_bitcoinjs_lib10.Psbt.fromHex(
2135
+ signedUnbondingPsbtHex
2136
+ ).extractTransaction();
2137
+ return {
2138
+ transaction: signedUnbondingTx,
2139
+ fee
2140
+ };
2141
+ }
2142
+ /**
2143
+ * Creates a signed unbonding transaction that is ready to be sent to the BTC
2144
+ * network.
2145
+ * @param stakerBtcInfo - The staker BTC info which includes the BTC address
2146
+ * and the no-coord public key in hex format.
2147
+ * @param stakingInput - The staking inputs.
2148
+ * @param stakingParamsVersion - The params version that was used to create the
2149
+ * delegation in Babylon chain
2150
+ * @param stakingTx - The staking transaction.
2151
+ * @param unsignedUnbondingTx - The unsigned unbonding transaction.
2152
+ * @param covenantUnbondingSignatures - The covenant unbonding signatures.
2153
+ * It can be retrieved from the Babylon chain or API.
2154
+ * @returns The signed unbonding transaction and its fee.
2155
+ */
2156
+ async createSignedBtcUnbondingTransaction(stakerBtcInfo, stakingInput, stakingParamsVersion, stakingTx, unsignedUnbondingTx, covenantUnbondingSignatures) {
2157
+ const params = getBabylonParamByVersion(
2158
+ stakingParamsVersion,
2159
+ this.stakingParams
2160
+ );
2161
+ const {
2162
+ transaction: signedUnbondingTx,
2163
+ fee
2164
+ } = await this.createPartialSignedBtcUnbondingTransaction(
2165
+ stakerBtcInfo,
2166
+ stakingInput,
2167
+ stakingParamsVersion,
2168
+ stakingTx
2169
+ );
2170
+ if (signedUnbondingTx.getId() !== unsignedUnbondingTx.getId()) {
2171
+ throw new Error(
2172
+ "Unbonding transaction hash does not match the computed hash"
2173
+ );
2174
+ }
2175
+ const covenantBuffers = params.covenantNoCoordPks.map(
2176
+ (covenant) => Buffer.from(covenant, "hex")
2177
+ );
2178
+ const witness = createCovenantWitness(
2179
+ // Since unbonding transactions always have a single input and output,
2180
+ // we expect exactly one signature in TaprootScriptSpendSig when the
2181
+ // signing is successful
2182
+ signedUnbondingTx.ins[0].witness,
2183
+ covenantBuffers,
2184
+ covenantUnbondingSignatures,
2185
+ params.covenantQuorum
2186
+ );
2187
+ signedUnbondingTx.ins[0].witness = witness;
2188
+ return {
2189
+ transaction: signedUnbondingTx,
2190
+ fee
2191
+ };
2192
+ }
2193
+ /**
2194
+ * Creates a signed withdrawal transaction on the unbodning output expiry path
2195
+ * that is ready to be sent to the BTC network.
2196
+ * @param stakingInput - The staking inputs.
2197
+ * @param stakingParamsVersion - The params version that was used to create the
2198
+ * delegation in Babylon chain
2199
+ * @param earlyUnbondingTx - The early unbonding transaction.
2200
+ * @param feeRate - The fee rate in satoshis per byte.
2201
+ * @returns The signed withdrawal transaction and its fee.
2202
+ */
2203
+ async createSignedBtcWithdrawEarlyUnbondedTransaction(stakerBtcInfo, stakingInput, stakingParamsVersion, earlyUnbondingTx, feeRate) {
2204
+ const params = getBabylonParamByVersion(
2205
+ stakingParamsVersion,
2206
+ this.stakingParams
2207
+ );
2208
+ const staking = new Staking(
2209
+ this.network,
2210
+ stakerBtcInfo,
2211
+ params,
2212
+ stakingInput.finalityProviderPkNoCoordHex,
2213
+ stakingInput.stakingTimelock
2214
+ );
2215
+ const { psbt: unbondingPsbt2, fee } = staking.createWithdrawEarlyUnbondedTransaction(
2216
+ earlyUnbondingTx,
2217
+ feeRate
2218
+ );
2219
+ const signedWithdrawalPsbtHex = await this.btcProvider.signPsbt(
2220
+ "withdraw-early-unbonded" /* WITHDRAW_EARLY_UNBONDED */,
2221
+ unbondingPsbt2.toHex()
2222
+ );
2223
+ return {
2224
+ transaction: import_bitcoinjs_lib10.Psbt.fromHex(signedWithdrawalPsbtHex).extractTransaction(),
2225
+ fee
2226
+ };
2227
+ }
2228
+ /**
2229
+ * Creates a signed withdrawal transaction on the staking output expiry path
2230
+ * that is ready to be sent to the BTC network.
2231
+ * @param stakerBtcInfo - The staker BTC info which includes the BTC address
2232
+ * and the no-coord public key in hex format.
2233
+ * @param stakingInput - The staking inputs.
2234
+ * @param stakingParamsVersion - The params version that was used to create the
2235
+ * delegation in Babylon chain
2236
+ * @param stakingTx - The staking transaction.
2237
+ * @param feeRate - The fee rate in satoshis per byte.
2238
+ * @returns The signed withdrawal transaction and its fee.
2239
+ */
2240
+ async createSignedBtcWithdrawStakingExpiredTransaction(stakerBtcInfo, stakingInput, stakingParamsVersion, stakingTx, feeRate) {
2241
+ const params = getBabylonParamByVersion(
2242
+ stakingParamsVersion,
2243
+ this.stakingParams
2244
+ );
2245
+ const staking = new Staking(
2246
+ this.network,
2247
+ stakerBtcInfo,
2248
+ params,
2249
+ stakingInput.finalityProviderPkNoCoordHex,
2250
+ stakingInput.stakingTimelock
2251
+ );
2252
+ const { psbt, fee } = staking.createWithdrawStakingExpiredPsbt(
2253
+ stakingTx,
2254
+ feeRate
2255
+ );
2256
+ const signedWithdrawalPsbtHex = await this.btcProvider.signPsbt(
2257
+ "withdraw-staking-expired" /* WITHDRAW_STAKING_EXPIRED */,
2258
+ psbt.toHex()
2259
+ );
2260
+ return {
2261
+ transaction: import_bitcoinjs_lib10.Psbt.fromHex(signedWithdrawalPsbtHex).extractTransaction(),
2262
+ fee
2263
+ };
2264
+ }
2265
+ /**
2266
+ * Creates a signed withdrawal transaction for the expired slashing output that
2267
+ * is ready to be sent to the BTC network.
2268
+ * @param stakerBtcInfo - The staker BTC info which includes the BTC address
2269
+ * and the no-coord public key in hex format.
2270
+ * @param stakingInput - The staking inputs.
2271
+ * @param stakingParamsVersion - The params version that was used to create the
2272
+ * delegation in Babylon chain
2273
+ * @param slashingTx - The slashing transaction.
2274
+ * @param feeRate - The fee rate in satoshis per byte.
2275
+ * @returns The signed withdrawal transaction and its fee.
2276
+ */
2277
+ async createSignedBtcWithdrawSlashingTransaction(stakerBtcInfo, stakingInput, stakingParamsVersion, slashingTx, feeRate) {
2278
+ const params = getBabylonParamByVersion(
2279
+ stakingParamsVersion,
2280
+ this.stakingParams
2281
+ );
2282
+ const staking = new Staking(
2283
+ this.network,
2284
+ stakerBtcInfo,
2285
+ params,
2286
+ stakingInput.finalityProviderPkNoCoordHex,
2287
+ stakingInput.stakingTimelock
2288
+ );
2289
+ const { psbt, fee } = staking.createWithdrawSlashingPsbt(
2290
+ slashingTx,
2291
+ feeRate
2292
+ );
2293
+ const signedSlashingPsbtHex = await this.btcProvider.signPsbt(
2294
+ "withdraw-slashing" /* WITHDRAW_SLASHING */,
2295
+ psbt.toHex()
2296
+ );
2297
+ return {
2298
+ transaction: import_bitcoinjs_lib10.Psbt.fromHex(signedSlashingPsbtHex).extractTransaction(),
2299
+ fee
2300
+ };
2301
+ }
2302
+ /**
2303
+ * Creates a proof of possession for the staker based on ECDSA signature.
2304
+ * @param bech32Address - The staker's bech32 address.
2305
+ * @returns The proof of possession.
2306
+ */
2307
+ async createProofOfPossession(bech32Address) {
2308
+ if (!this.btcProvider.signMessage) {
2309
+ throw new Error("Sign message function not found");
2310
+ }
2311
+ const bech32AddressHex = uint8ArrayToHex((0, import_encoding2.fromBech32)(bech32Address).data);
2312
+ const signedBabylonAddress = await this.btcProvider.signMessage(
2313
+ "proof-of-possession" /* PROOF_OF_POSSESSION */,
2314
+ bech32AddressHex,
2315
+ "ecdsa"
2316
+ );
2317
+ const ecdsaSig = Uint8Array.from(Buffer.from(signedBabylonAddress, "base64"));
2318
+ return {
2319
+ btcSigType: import_pop.BTCSigType.ECDSA,
2320
+ btcSig: ecdsaSig
2321
+ };
2322
+ }
2323
+ /**
2324
+ * Creates the unbonding, slashing, and unbonding slashing transactions and
2325
+ * PSBTs.
2326
+ * @param stakingInstance - The staking instance.
2327
+ * @param stakingTx - The staking transaction.
2328
+ * @returns The unbonding, slashing, and unbonding slashing transactions and
2329
+ * PSBTs.
2330
+ */
2331
+ async createDelegationTransactionsAndPsbts(stakingInstance, stakingTx) {
2332
+ const { transaction: unbondingTx } = stakingInstance.createUnbondingTransaction(stakingTx);
2333
+ const { psbt: slashingPsbt } = stakingInstance.createStakingOutputSlashingPsbt(stakingTx);
2334
+ const { psbt: unbondingSlashingPsbt } = stakingInstance.createUnbondingOutputSlashingPsbt(unbondingTx);
2335
+ return {
2336
+ unbondingTx,
2337
+ slashingPsbt,
2338
+ unbondingSlashingPsbt
2339
+ };
2340
+ }
2341
+ /**
2342
+ * Creates a protobuf message for the BTC delegation.
2343
+ * @param stakingInstance - The staking instance.
2344
+ * @param stakingInput - The staking inputs.
2345
+ * @param stakingTx - The staking transaction.
2346
+ * @param bech32Address - The staker's babylon chain bech32 address
2347
+ * @param stakerBtcInfo - The staker's BTC information such as address and
2348
+ * public key
2349
+ * @param params - The staking parameters.
2350
+ * @param inclusionProof - The inclusion proof of the staking transaction.
2351
+ * @returns The protobuf message.
2352
+ */
2353
+ async createBtcDelegationMsg(stakingInstance, stakingInput, stakingTx, bech32Address, stakerBtcInfo, params, inclusionProof) {
2354
+ const {
2355
+ unbondingTx,
2356
+ slashingPsbt,
2357
+ unbondingSlashingPsbt
2358
+ } = await this.createDelegationTransactionsAndPsbts(
2359
+ stakingInstance,
2360
+ stakingTx
2361
+ );
2362
+ const signedSlashingPsbtHex = await this.btcProvider.signPsbt(
2363
+ "staking-slashing" /* STAKING_SLASHING */,
2364
+ slashingPsbt.toHex()
2365
+ );
2366
+ const signedSlashingTx = import_bitcoinjs_lib10.Psbt.fromHex(
2367
+ signedSlashingPsbtHex
2368
+ ).extractTransaction();
2369
+ const slashingSig = extractFirstSchnorrSignatureFromTransaction(
2370
+ signedSlashingTx
2371
+ );
2372
+ if (!slashingSig) {
2373
+ throw new Error("No signature found in the staking output slashing PSBT");
2374
+ }
2375
+ const signedUnbondingSlashingPsbtHex = await this.btcProvider.signPsbt(
2376
+ "unbonding-slashing" /* UNBONDING_SLASHING */,
2377
+ unbondingSlashingPsbt.toHex()
2378
+ );
2379
+ const signedUnbondingSlashingTx = import_bitcoinjs_lib10.Psbt.fromHex(
2380
+ signedUnbondingSlashingPsbtHex
2381
+ ).extractTransaction();
2382
+ const unbondingSignatures = extractFirstSchnorrSignatureFromTransaction(
2383
+ signedUnbondingSlashingTx
2384
+ );
2385
+ if (!unbondingSignatures) {
2386
+ throw new Error("No signature found in the unbonding output slashing PSBT");
2387
+ }
2388
+ const proofOfPossession = await this.createProofOfPossession(bech32Address);
2389
+ const msg = import_babylon_proto_ts.btcstakingtx.MsgCreateBTCDelegation.fromPartial({
2390
+ stakerAddr: bech32Address,
2391
+ pop: proofOfPossession,
2392
+ btcPk: Uint8Array.from(
2393
+ Buffer.from(stakerBtcInfo.publicKeyNoCoordHex, "hex")
2394
+ ),
2395
+ fpBtcPkList: [
2396
+ Uint8Array.from(
2397
+ Buffer.from(stakingInput.finalityProviderPkNoCoordHex, "hex")
2398
+ )
2399
+ ],
2400
+ stakingTime: stakingInput.stakingTimelock,
2401
+ stakingValue: stakingInput.stakingAmountSat,
2402
+ stakingTx: Uint8Array.from(stakingTx.toBuffer()),
2403
+ slashingTx: Uint8Array.from(
2404
+ Buffer.from(clearTxSignatures(signedSlashingTx).toHex(), "hex")
2405
+ ),
2406
+ delegatorSlashingSig: Uint8Array.from(slashingSig),
2407
+ unbondingTime: params.unbondingTime,
2408
+ unbondingTx: Uint8Array.from(unbondingTx.toBuffer()),
2409
+ unbondingValue: stakingInput.stakingAmountSat - params.unbondingFeeSat,
2410
+ unbondingSlashingTx: Uint8Array.from(
2411
+ Buffer.from(
2412
+ clearTxSignatures(signedUnbondingSlashingTx).toHex(),
2413
+ "hex"
2414
+ )
2415
+ ),
2416
+ delegatorUnbondingSlashingSig: Uint8Array.from(unbondingSignatures),
2417
+ stakingTxInclusionProof: inclusionProof
2418
+ });
2419
+ return {
2420
+ typeUrl: BABYLON_REGISTRY_TYPE_URLS.MsgCreateBTCDelegation,
2421
+ value: msg
2422
+ };
2423
+ }
2424
+ /**
2425
+ * Gets the inclusion proof for the staking transaction.
2426
+ * See the type `InclusionProof` for more information
2427
+ * @param inclusionProof - The inclusion proof.
2428
+ * @returns The inclusion proof.
2429
+ */
2430
+ getInclusionProof(inclusionProof) {
2431
+ const {
2432
+ pos,
2433
+ merkle,
2434
+ blockHashHex
2435
+ } = inclusionProof;
2436
+ const proofHex = deriveMerkleProof(merkle);
2437
+ const hash = reverseBuffer(Uint8Array.from(Buffer.from(blockHashHex, "hex")));
2438
+ const inclusionProofKey = import_babylon_proto_ts.btccheckpoint.TransactionKey.fromPartial({
2439
+ index: pos,
2440
+ hash
2441
+ });
2442
+ return import_babylon_proto_ts.btcstaking.InclusionProof.fromPartial({
2443
+ key: inclusionProofKey,
2444
+ proof: Uint8Array.from(Buffer.from(proofHex, "hex"))
2445
+ });
2446
+ }
2447
+ };
2448
+ var extractFirstSchnorrSignatureFromTransaction = (singedTransaction) => {
2449
+ for (const input of singedTransaction.ins) {
2450
+ if (input.witness && input.witness.length > 0) {
2451
+ const schnorrSignature = input.witness[0];
2452
+ if (schnorrSignature.length === 64) {
2453
+ return schnorrSignature;
2454
+ }
2455
+ }
2456
+ }
2457
+ return void 0;
2458
+ };
2459
+ var clearTxSignatures = (tx) => {
2460
+ tx.ins.forEach((input) => {
2461
+ input.script = Buffer.alloc(0);
2462
+ input.witness = [];
2463
+ });
2464
+ return tx;
2465
+ };
2466
+ var deriveMerkleProof = (merkle) => {
2467
+ const proofHex = merkle.reduce((acc, m) => {
2468
+ return acc + Buffer.from(m, "hex").reverse().toString("hex");
2469
+ }, "");
2470
+ return proofHex;
2471
+ };
2472
+ var getUnbondingTxStakerSignature = (unbondingTx) => {
2473
+ try {
2474
+ return unbondingTx.ins[0].witness[0].toString("hex");
2475
+ } catch (error) {
2476
+ throw StakingError.fromUnknown(
2477
+ error,
2478
+ "INVALID_INPUT" /* INVALID_INPUT */,
2479
+ "Failed to get staker signature"
2480
+ );
2481
+ }
2482
+ };
2483
+ // Annotate the CommonJS export names for ESM import in node:
2484
+ 0 && (module.exports = {
2485
+ BabylonBtcStakingManager,
2486
+ BitcoinScriptType,
2487
+ ObservableStaking,
2488
+ ObservableStakingScriptData,
2489
+ SigningStep,
2490
+ Staking,
2491
+ StakingScriptData,
2492
+ buildStakingTransactionOutputs,
2493
+ createCovenantWitness,
2494
+ deriveSlashingOutput,
2495
+ deriveStakingOutputInfo,
2496
+ deriveUnbondingOutputInfo,
2497
+ findInputUTXO,
2498
+ findMatchingTxOutputIndex,
2499
+ getBabylonParamByBtcHeight,
2500
+ getBabylonParamByVersion,
2501
+ getPsbtInputFields,
2502
+ getPublicKeyNoCoord,
2503
+ getScriptType,
2504
+ getUnbondingTxStakerSignature,
2505
+ initBTCCurve,
2506
+ isTaproot,
2507
+ isValidBitcoinAddress,
2508
+ isValidNoCoordPublicKey,
2509
+ slashEarlyUnbondedTransaction,
2510
+ slashTimelockUnbondedTransaction,
2511
+ stakingTransaction,
2512
+ toBuffers,
2513
+ transactionIdToHash,
2514
+ unbondingTransaction,
2515
+ validateParams,
2516
+ validateStakingTimelock,
2517
+ validateStakingTxInputData,
2518
+ withdrawEarlyUnbondedTransaction,
2519
+ withdrawSlashingTransaction,
2520
+ withdrawTimelockUnbondedTransaction
2521
+ });