@bitgo-beta/babylonlabs-io-btc-staking-ts 0.4.0-beta.142 → 0.4.0-beta.1421
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/README.md +1 -1
- package/dist/index.cjs +2160 -1458
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.cts +752 -496
- package/dist/index.d.ts +1235 -0
- package/dist/index.js +2135 -1431
- package/dist/index.js.map +7 -0
- package/package.json +14 -6
package/dist/index.cjs
CHANGED
|
@@ -28,20 +28,22 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
29
|
|
|
30
30
|
// src/index.ts
|
|
31
|
-
var
|
|
32
|
-
__export(
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
33
|
BabylonBtcStakingManager: () => BabylonBtcStakingManager,
|
|
34
34
|
BitcoinScriptType: () => BitcoinScriptType,
|
|
35
35
|
ObservableStaking: () => ObservableStaking,
|
|
36
36
|
ObservableStakingScriptData: () => ObservableStakingScriptData,
|
|
37
|
-
SigningStep: () => SigningStep,
|
|
38
37
|
Staking: () => Staking,
|
|
39
38
|
StakingScriptData: () => StakingScriptData,
|
|
40
39
|
buildStakingTransactionOutputs: () => buildStakingTransactionOutputs,
|
|
40
|
+
clearTxSignatures: () => clearTxSignatures,
|
|
41
41
|
createCovenantWitness: () => createCovenantWitness,
|
|
42
|
+
deriveMerkleProof: () => deriveMerkleProof,
|
|
42
43
|
deriveSlashingOutput: () => deriveSlashingOutput,
|
|
43
44
|
deriveStakingOutputInfo: () => deriveStakingOutputInfo,
|
|
44
45
|
deriveUnbondingOutputInfo: () => deriveUnbondingOutputInfo,
|
|
46
|
+
extractFirstSchnorrSignatureFromTransaction: () => extractFirstSchnorrSignatureFromTransaction,
|
|
45
47
|
findInputUTXO: () => findInputUTXO,
|
|
46
48
|
findMatchingTxOutputIndex: () => findMatchingTxOutputIndex,
|
|
47
49
|
getBabylonParamByBtcHeight: () => getBabylonParamByBtcHeight,
|
|
@@ -50,6 +52,7 @@ __export(src_exports, {
|
|
|
50
52
|
getPublicKeyNoCoord: () => getPublicKeyNoCoord,
|
|
51
53
|
getScriptType: () => getScriptType,
|
|
52
54
|
getUnbondingTxStakerSignature: () => getUnbondingTxStakerSignature,
|
|
55
|
+
hasSlashing: () => hasSlashing,
|
|
53
56
|
initBTCCurve: () => initBTCCurve,
|
|
54
57
|
isNativeSegwit: () => isNativeSegwit,
|
|
55
58
|
isTaproot: () => isTaproot,
|
|
@@ -58,246 +61,16 @@ __export(src_exports, {
|
|
|
58
61
|
isValidNoCoordPublicKey: () => isValidNoCoordPublicKey,
|
|
59
62
|
slashEarlyUnbondedTransaction: () => slashEarlyUnbondedTransaction,
|
|
60
63
|
slashTimelockUnbondedTransaction: () => slashTimelockUnbondedTransaction,
|
|
64
|
+
stakingExpansionTransaction: () => stakingExpansionTransaction,
|
|
61
65
|
stakingTransaction: () => stakingTransaction,
|
|
62
66
|
toBuffers: () => toBuffers,
|
|
63
67
|
transactionIdToHash: () => transactionIdToHash,
|
|
64
68
|
unbondingTransaction: () => unbondingTransaction,
|
|
65
|
-
validateParams: () => validateParams,
|
|
66
|
-
validateStakingTimelock: () => validateStakingTimelock,
|
|
67
|
-
validateStakingTxInputData: () => validateStakingTxInputData,
|
|
68
69
|
withdrawEarlyUnbondedTransaction: () => withdrawEarlyUnbondedTransaction,
|
|
69
70
|
withdrawSlashingTransaction: () => withdrawSlashingTransaction,
|
|
70
71
|
withdrawTimelockUnbondedTransaction: () => withdrawTimelockUnbondedTransaction
|
|
71
72
|
});
|
|
72
|
-
module.exports = __toCommonJS(
|
|
73
|
-
|
|
74
|
-
// src/staking/stakingScript.ts
|
|
75
|
-
var import_bitcoinjs_lib = require("bitcoinjs-lib");
|
|
76
|
-
|
|
77
|
-
// src/constants/keys.ts
|
|
78
|
-
var NO_COORD_PK_BYTE_LENGTH = 32;
|
|
79
|
-
|
|
80
|
-
// src/staking/stakingScript.ts
|
|
81
|
-
var MAGIC_BYTES_LEN = 4;
|
|
82
|
-
var StakingScriptData = class {
|
|
83
|
-
constructor(stakerKey, finalityProviderKeys, covenantKeys, covenantThreshold, stakingTimelock, unbondingTimelock) {
|
|
84
|
-
if (!stakerKey || !finalityProviderKeys || !covenantKeys || !covenantThreshold || !stakingTimelock || !unbondingTimelock) {
|
|
85
|
-
throw new Error("Missing required input values");
|
|
86
|
-
}
|
|
87
|
-
this.stakerKey = stakerKey;
|
|
88
|
-
this.finalityProviderKeys = finalityProviderKeys;
|
|
89
|
-
this.covenantKeys = covenantKeys;
|
|
90
|
-
this.covenantThreshold = covenantThreshold;
|
|
91
|
-
this.stakingTimeLock = stakingTimelock;
|
|
92
|
-
this.unbondingTimeLock = unbondingTimelock;
|
|
93
|
-
if (!this.validate()) {
|
|
94
|
-
throw new Error("Invalid script data provided");
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Validates the staking script.
|
|
99
|
-
* @returns {boolean} Returns true if the staking script is valid, otherwise false.
|
|
100
|
-
*/
|
|
101
|
-
validate() {
|
|
102
|
-
if (this.stakerKey.length != NO_COORD_PK_BYTE_LENGTH) {
|
|
103
|
-
return false;
|
|
104
|
-
}
|
|
105
|
-
if (this.finalityProviderKeys.some(
|
|
106
|
-
(finalityProviderKey) => finalityProviderKey.length != NO_COORD_PK_BYTE_LENGTH
|
|
107
|
-
)) {
|
|
108
|
-
return false;
|
|
109
|
-
}
|
|
110
|
-
if (this.covenantKeys.some((covenantKey) => covenantKey.length != NO_COORD_PK_BYTE_LENGTH)) {
|
|
111
|
-
return false;
|
|
112
|
-
}
|
|
113
|
-
const allPks = [
|
|
114
|
-
this.stakerKey,
|
|
115
|
-
...this.finalityProviderKeys,
|
|
116
|
-
...this.covenantKeys
|
|
117
|
-
];
|
|
118
|
-
const allPksSet = new Set(allPks);
|
|
119
|
-
if (allPks.length !== allPksSet.size) {
|
|
120
|
-
return false;
|
|
121
|
-
}
|
|
122
|
-
if (this.covenantThreshold <= 0 || this.covenantThreshold > this.covenantKeys.length) {
|
|
123
|
-
return false;
|
|
124
|
-
}
|
|
125
|
-
if (this.stakingTimeLock <= 0 || this.stakingTimeLock > 65535) {
|
|
126
|
-
return false;
|
|
127
|
-
}
|
|
128
|
-
if (this.unbondingTimeLock <= 0 || this.unbondingTimeLock > 65535) {
|
|
129
|
-
return false;
|
|
130
|
-
}
|
|
131
|
-
return true;
|
|
132
|
-
}
|
|
133
|
-
// The staking script allows for multiple finality provider public keys
|
|
134
|
-
// to support (re)stake to multiple finality providers
|
|
135
|
-
// Covenant members are going to have multiple keys
|
|
136
|
-
/**
|
|
137
|
-
* Builds a timelock script.
|
|
138
|
-
* @param timelock - The timelock value to encode in the script.
|
|
139
|
-
* @returns {Buffer} containing the compiled timelock script.
|
|
140
|
-
*/
|
|
141
|
-
buildTimelockScript(timelock) {
|
|
142
|
-
return import_bitcoinjs_lib.script.compile([
|
|
143
|
-
this.stakerKey,
|
|
144
|
-
import_bitcoinjs_lib.opcodes.OP_CHECKSIGVERIFY,
|
|
145
|
-
import_bitcoinjs_lib.script.number.encode(timelock),
|
|
146
|
-
import_bitcoinjs_lib.opcodes.OP_CHECKSEQUENCEVERIFY
|
|
147
|
-
]);
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Builds the staking timelock script.
|
|
151
|
-
* Only holder of private key for given pubKey can spend after relative lock time
|
|
152
|
-
* Creates the timelock script in the form:
|
|
153
|
-
* <stakerPubKey>
|
|
154
|
-
* OP_CHECKSIGVERIFY
|
|
155
|
-
* <stakingTimeBlocks>
|
|
156
|
-
* OP_CHECKSEQUENCEVERIFY
|
|
157
|
-
* @returns {Buffer} The staking timelock script.
|
|
158
|
-
*/
|
|
159
|
-
buildStakingTimelockScript() {
|
|
160
|
-
return this.buildTimelockScript(this.stakingTimeLock);
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Builds the unbonding timelock script.
|
|
164
|
-
* Creates the unbonding timelock script in the form:
|
|
165
|
-
* <stakerPubKey>
|
|
166
|
-
* OP_CHECKSIGVERIFY
|
|
167
|
-
* <unbondingTimeBlocks>
|
|
168
|
-
* OP_CHECKSEQUENCEVERIFY
|
|
169
|
-
* @returns {Buffer} The unbonding timelock script.
|
|
170
|
-
*/
|
|
171
|
-
buildUnbondingTimelockScript() {
|
|
172
|
-
return this.buildTimelockScript(this.unbondingTimeLock);
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Builds the unbonding script in the form:
|
|
176
|
-
* buildSingleKeyScript(stakerPk, true) ||
|
|
177
|
-
* buildMultiKeyScript(covenantPks, covenantThreshold, false)
|
|
178
|
-
* || means combining the scripts
|
|
179
|
-
* @returns {Buffer} The unbonding script.
|
|
180
|
-
*/
|
|
181
|
-
buildUnbondingScript() {
|
|
182
|
-
return Buffer.concat([
|
|
183
|
-
this.buildSingleKeyScript(this.stakerKey, true),
|
|
184
|
-
this.buildMultiKeyScript(
|
|
185
|
-
this.covenantKeys,
|
|
186
|
-
this.covenantThreshold,
|
|
187
|
-
false
|
|
188
|
-
)
|
|
189
|
-
]);
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Builds the slashing script for staking in the form:
|
|
193
|
-
* buildSingleKeyScript(stakerPk, true) ||
|
|
194
|
-
* buildMultiKeyScript(finalityProviderPKs, 1, true) ||
|
|
195
|
-
* buildMultiKeyScript(covenantPks, covenantThreshold, false)
|
|
196
|
-
* || means combining the scripts
|
|
197
|
-
* The slashing script is a combination of single-key and multi-key scripts.
|
|
198
|
-
* The single-key script is used for staker key verification.
|
|
199
|
-
* The multi-key script is used for finality provider key verification and covenant key verification.
|
|
200
|
-
* @returns {Buffer} The slashing script as a Buffer.
|
|
201
|
-
*/
|
|
202
|
-
buildSlashingScript() {
|
|
203
|
-
return Buffer.concat([
|
|
204
|
-
this.buildSingleKeyScript(this.stakerKey, true),
|
|
205
|
-
this.buildMultiKeyScript(
|
|
206
|
-
this.finalityProviderKeys,
|
|
207
|
-
// The threshold is always 1 as we only need one
|
|
208
|
-
// finalityProvider signature to perform slashing
|
|
209
|
-
// (only one finalityProvider performs an offence)
|
|
210
|
-
1,
|
|
211
|
-
// OP_VERIFY/OP_CHECKSIGVERIFY is added at the end
|
|
212
|
-
true
|
|
213
|
-
),
|
|
214
|
-
this.buildMultiKeyScript(
|
|
215
|
-
this.covenantKeys,
|
|
216
|
-
this.covenantThreshold,
|
|
217
|
-
// No need to add verify since covenants are at the end of the script
|
|
218
|
-
false
|
|
219
|
-
)
|
|
220
|
-
]);
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Builds the staking scripts.
|
|
224
|
-
* @returns {StakingScripts} The staking scripts.
|
|
225
|
-
*/
|
|
226
|
-
buildScripts() {
|
|
227
|
-
return {
|
|
228
|
-
timelockScript: this.buildStakingTimelockScript(),
|
|
229
|
-
unbondingScript: this.buildUnbondingScript(),
|
|
230
|
-
slashingScript: this.buildSlashingScript(),
|
|
231
|
-
unbondingTimelockScript: this.buildUnbondingTimelockScript()
|
|
232
|
-
};
|
|
233
|
-
}
|
|
234
|
-
// buildSingleKeyScript and buildMultiKeyScript allow us to reuse functionality
|
|
235
|
-
// for creating Bitcoin scripts for the unbonding script and the slashing script
|
|
236
|
-
/**
|
|
237
|
-
* Builds a single key script in the form:
|
|
238
|
-
* buildSingleKeyScript creates a single key script
|
|
239
|
-
* <pk> OP_CHECKSIGVERIFY (if withVerify is true)
|
|
240
|
-
* <pk> OP_CHECKSIG (if withVerify is false)
|
|
241
|
-
* @param pk - The public key buffer.
|
|
242
|
-
* @param withVerify - A boolean indicating whether to include the OP_CHECKSIGVERIFY opcode.
|
|
243
|
-
* @returns The compiled script buffer.
|
|
244
|
-
*/
|
|
245
|
-
buildSingleKeyScript(pk, withVerify) {
|
|
246
|
-
if (pk.length != NO_COORD_PK_BYTE_LENGTH) {
|
|
247
|
-
throw new Error("Invalid key length");
|
|
248
|
-
}
|
|
249
|
-
return import_bitcoinjs_lib.script.compile([
|
|
250
|
-
pk,
|
|
251
|
-
withVerify ? import_bitcoinjs_lib.opcodes.OP_CHECKSIGVERIFY : import_bitcoinjs_lib.opcodes.OP_CHECKSIG
|
|
252
|
-
]);
|
|
253
|
-
}
|
|
254
|
-
/**
|
|
255
|
-
* Builds a multi-key script in the form:
|
|
256
|
-
* <pk1> OP_CHEKCSIG <pk2> OP_CHECKSIGADD <pk3> OP_CHECKSIGADD ... <pkN> OP_CHECKSIGADD <threshold> OP_NUMEQUAL
|
|
257
|
-
* <withVerify -> OP_NUMEQUALVERIFY>
|
|
258
|
-
* It validates whether provided keys are unique and the threshold is not greater than number of keys
|
|
259
|
-
* If there is only one key provided it will return single key sig script
|
|
260
|
-
* @param pks - An array of public keys.
|
|
261
|
-
* @param threshold - The required number of valid signers.
|
|
262
|
-
* @param withVerify - A boolean indicating whether to include the OP_VERIFY opcode.
|
|
263
|
-
* @returns The compiled multi-key script as a Buffer.
|
|
264
|
-
* @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.
|
|
265
|
-
*/
|
|
266
|
-
buildMultiKeyScript(pks, threshold, withVerify) {
|
|
267
|
-
if (!pks || pks.length === 0) {
|
|
268
|
-
throw new Error("No keys provided");
|
|
269
|
-
}
|
|
270
|
-
if (pks.some((pk) => pk.length != NO_COORD_PK_BYTE_LENGTH)) {
|
|
271
|
-
throw new Error("Invalid key length");
|
|
272
|
-
}
|
|
273
|
-
if (threshold > pks.length) {
|
|
274
|
-
throw new Error(
|
|
275
|
-
"Required number of valid signers is greater than number of provided keys"
|
|
276
|
-
);
|
|
277
|
-
}
|
|
278
|
-
if (pks.length === 1) {
|
|
279
|
-
return this.buildSingleKeyScript(pks[0], withVerify);
|
|
280
|
-
}
|
|
281
|
-
const sortedPks = [...pks].sort(Buffer.compare);
|
|
282
|
-
for (let i = 0; i < sortedPks.length - 1; ++i) {
|
|
283
|
-
if (sortedPks[i].equals(sortedPks[i + 1])) {
|
|
284
|
-
throw new Error("Duplicate keys provided");
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
const scriptElements = [sortedPks[0], import_bitcoinjs_lib.opcodes.OP_CHECKSIG];
|
|
288
|
-
for (let i = 1; i < sortedPks.length; i++) {
|
|
289
|
-
scriptElements.push(sortedPks[i]);
|
|
290
|
-
scriptElements.push(import_bitcoinjs_lib.opcodes.OP_CHECKSIGADD);
|
|
291
|
-
}
|
|
292
|
-
scriptElements.push(import_bitcoinjs_lib.script.number.encode(threshold));
|
|
293
|
-
if (withVerify) {
|
|
294
|
-
scriptElements.push(import_bitcoinjs_lib.opcodes.OP_NUMEQUALVERIFY);
|
|
295
|
-
} else {
|
|
296
|
-
scriptElements.push(import_bitcoinjs_lib.opcodes.OP_NUMEQUAL);
|
|
297
|
-
}
|
|
298
|
-
return import_bitcoinjs_lib.script.compile(scriptElements);
|
|
299
|
-
}
|
|
300
|
-
};
|
|
73
|
+
module.exports = __toCommonJS(index_exports);
|
|
301
74
|
|
|
302
75
|
// src/error/index.ts
|
|
303
76
|
var StakingError = class _StakingError extends Error {
|
|
@@ -317,38 +90,33 @@ var StakingError = class _StakingError extends Error {
|
|
|
317
90
|
}
|
|
318
91
|
};
|
|
319
92
|
|
|
320
|
-
// src/
|
|
321
|
-
var
|
|
322
|
-
|
|
323
|
-
// src/constants/dustSat.ts
|
|
324
|
-
var BTC_DUST_SAT = 546;
|
|
93
|
+
// src/utils/btc.ts
|
|
94
|
+
var ecc = __toESM(require("@bitcoin-js/tiny-secp256k1-asmjs"), 1);
|
|
95
|
+
var import_bitcoinjs_lib = require("bitcoinjs-lib");
|
|
325
96
|
|
|
326
|
-
// src/constants/
|
|
327
|
-
var
|
|
328
|
-
var internalPubkey = Buffer.from(key, "hex").subarray(1, 33);
|
|
97
|
+
// src/constants/keys.ts
|
|
98
|
+
var NO_COORD_PK_BYTE_LENGTH = 32;
|
|
329
99
|
|
|
330
100
|
// src/utils/btc.ts
|
|
331
|
-
var ecc = __toESM(require("@bitcoin-js/tiny-secp256k1-asmjs"), 1);
|
|
332
|
-
var import_bitcoinjs_lib2 = require("bitcoinjs-lib");
|
|
333
101
|
var initBTCCurve = () => {
|
|
334
|
-
(0,
|
|
102
|
+
(0, import_bitcoinjs_lib.initEccLib)(ecc);
|
|
335
103
|
};
|
|
336
104
|
var isValidBitcoinAddress = (btcAddress, network) => {
|
|
337
105
|
try {
|
|
338
|
-
return !!
|
|
106
|
+
return !!import_bitcoinjs_lib.address.toOutputScript(btcAddress, network);
|
|
339
107
|
} catch (error) {
|
|
340
108
|
return false;
|
|
341
109
|
}
|
|
342
110
|
};
|
|
343
111
|
var isTaproot = (taprootAddress, network) => {
|
|
344
112
|
try {
|
|
345
|
-
const decoded =
|
|
113
|
+
const decoded = import_bitcoinjs_lib.address.fromBech32(taprootAddress);
|
|
346
114
|
if (decoded.version !== 1) {
|
|
347
115
|
return false;
|
|
348
116
|
}
|
|
349
|
-
if (network.bech32 ===
|
|
117
|
+
if (network.bech32 === import_bitcoinjs_lib.networks.bitcoin.bech32) {
|
|
350
118
|
return taprootAddress.startsWith("bc1p");
|
|
351
|
-
} else if (network.bech32 ===
|
|
119
|
+
} else if (network.bech32 === import_bitcoinjs_lib.networks.testnet.bech32) {
|
|
352
120
|
return taprootAddress.startsWith("tb1p") || taprootAddress.startsWith("sb1p");
|
|
353
121
|
}
|
|
354
122
|
return false;
|
|
@@ -358,13 +126,13 @@ var isTaproot = (taprootAddress, network) => {
|
|
|
358
126
|
};
|
|
359
127
|
var isNativeSegwit = (segwitAddress, network) => {
|
|
360
128
|
try {
|
|
361
|
-
const decoded =
|
|
129
|
+
const decoded = import_bitcoinjs_lib.address.fromBech32(segwitAddress);
|
|
362
130
|
if (decoded.version !== 0) {
|
|
363
131
|
return false;
|
|
364
132
|
}
|
|
365
|
-
if (network.bech32 ===
|
|
133
|
+
if (network.bech32 === import_bitcoinjs_lib.networks.bitcoin.bech32) {
|
|
366
134
|
return segwitAddress.startsWith("bc1q");
|
|
367
|
-
} else if (network.bech32 ===
|
|
135
|
+
} else if (network.bech32 === import_bitcoinjs_lib.networks.testnet.bech32) {
|
|
368
136
|
return segwitAddress.startsWith("tb1q");
|
|
369
137
|
}
|
|
370
138
|
return false;
|
|
@@ -403,124 +171,12 @@ var transactionIdToHash = (txId) => {
|
|
|
403
171
|
return Buffer.from(txId, "hex").reverse();
|
|
404
172
|
};
|
|
405
173
|
|
|
406
|
-
// src/utils/
|
|
407
|
-
var
|
|
174
|
+
// src/utils/staking/index.ts
|
|
175
|
+
var import_bitcoinjs_lib2 = require("bitcoinjs-lib");
|
|
408
176
|
|
|
409
|
-
// src/constants/
|
|
410
|
-
var
|
|
411
|
-
var
|
|
412
|
-
var P2TR_INPUT_SIZE = 58;
|
|
413
|
-
var TX_BUFFER_SIZE_OVERHEAD = 11;
|
|
414
|
-
var LOW_RATE_ESTIMATION_ACCURACY_BUFFER = 30;
|
|
415
|
-
var MAX_NON_LEGACY_OUTPUT_SIZE = 43;
|
|
416
|
-
var WITHDRAW_TX_BUFFER_SIZE = 17;
|
|
417
|
-
var WALLET_RELAY_FEE_RATE_THRESHOLD = 2;
|
|
418
|
-
var OP_RETURN_OUTPUT_VALUE_SIZE = 8;
|
|
419
|
-
var OP_RETURN_VALUE_SERIALIZE_SIZE = 1;
|
|
420
|
-
|
|
421
|
-
// src/utils/fee/utils.ts
|
|
422
|
-
var import_bitcoinjs_lib3 = require("bitcoinjs-lib");
|
|
423
|
-
var isOP_RETURN = (script4) => {
|
|
424
|
-
const decompiled = import_bitcoinjs_lib3.script.decompile(script4);
|
|
425
|
-
return !!decompiled && decompiled[0] === import_bitcoinjs_lib3.opcodes.OP_RETURN;
|
|
426
|
-
};
|
|
427
|
-
var getInputSizeByScript = (script4) => {
|
|
428
|
-
try {
|
|
429
|
-
const { address: p2wpkhAddress } = import_bitcoinjs_lib3.payments.p2wpkh({
|
|
430
|
-
output: script4
|
|
431
|
-
});
|
|
432
|
-
if (p2wpkhAddress) {
|
|
433
|
-
return P2WPKH_INPUT_SIZE;
|
|
434
|
-
}
|
|
435
|
-
} catch (error) {
|
|
436
|
-
}
|
|
437
|
-
try {
|
|
438
|
-
const { address: p2trAddress } = import_bitcoinjs_lib3.payments.p2tr({
|
|
439
|
-
output: script4
|
|
440
|
-
});
|
|
441
|
-
if (p2trAddress) {
|
|
442
|
-
return P2TR_INPUT_SIZE;
|
|
443
|
-
}
|
|
444
|
-
} catch (error) {
|
|
445
|
-
}
|
|
446
|
-
return DEFAULT_INPUT_SIZE;
|
|
447
|
-
};
|
|
448
|
-
var getEstimatedChangeOutputSize = () => {
|
|
449
|
-
return MAX_NON_LEGACY_OUTPUT_SIZE;
|
|
450
|
-
};
|
|
451
|
-
var inputValueSum = (inputUTXOs) => {
|
|
452
|
-
return inputUTXOs.reduce((acc, utxo) => acc + utxo.value, 0);
|
|
453
|
-
};
|
|
454
|
-
|
|
455
|
-
// src/utils/fee/index.ts
|
|
456
|
-
var getStakingTxInputUTXOsAndFees = (availableUTXOs, stakingAmount, feeRate, outputs) => {
|
|
457
|
-
if (availableUTXOs.length === 0) {
|
|
458
|
-
throw new Error("Insufficient funds");
|
|
459
|
-
}
|
|
460
|
-
const validUTXOs = availableUTXOs.filter((utxo) => {
|
|
461
|
-
const script4 = Buffer.from(utxo.scriptPubKey, "hex");
|
|
462
|
-
return !!import_bitcoinjs_lib4.script.decompile(script4);
|
|
463
|
-
});
|
|
464
|
-
if (validUTXOs.length === 0) {
|
|
465
|
-
throw new Error("Insufficient funds: no valid UTXOs available for staking");
|
|
466
|
-
}
|
|
467
|
-
const sortedUTXOs = validUTXOs.sort((a, b) => b.value - a.value);
|
|
468
|
-
const selectedUTXOs = [];
|
|
469
|
-
let accumulatedValue = 0;
|
|
470
|
-
let estimatedFee = 0;
|
|
471
|
-
for (const utxo of sortedUTXOs) {
|
|
472
|
-
selectedUTXOs.push(utxo);
|
|
473
|
-
accumulatedValue += utxo.value;
|
|
474
|
-
const estimatedSize = getEstimatedSize(selectedUTXOs, outputs);
|
|
475
|
-
estimatedFee = estimatedSize * feeRate + rateBasedTxBufferFee(feeRate);
|
|
476
|
-
if (accumulatedValue - (stakingAmount + estimatedFee) > BTC_DUST_SAT) {
|
|
477
|
-
estimatedFee += getEstimatedChangeOutputSize() * feeRate;
|
|
478
|
-
}
|
|
479
|
-
if (accumulatedValue >= stakingAmount + estimatedFee) {
|
|
480
|
-
break;
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
if (accumulatedValue < stakingAmount + estimatedFee) {
|
|
484
|
-
throw new Error(
|
|
485
|
-
"Insufficient funds: unable to gather enough UTXOs to cover the staking amount and fees"
|
|
486
|
-
);
|
|
487
|
-
}
|
|
488
|
-
return {
|
|
489
|
-
selectedUTXOs,
|
|
490
|
-
fee: estimatedFee
|
|
491
|
-
};
|
|
492
|
-
};
|
|
493
|
-
var getWithdrawTxFee = (feeRate) => {
|
|
494
|
-
const inputSize = P2TR_INPUT_SIZE;
|
|
495
|
-
const outputSize = getEstimatedChangeOutputSize();
|
|
496
|
-
return feeRate * (inputSize + outputSize + TX_BUFFER_SIZE_OVERHEAD + WITHDRAW_TX_BUFFER_SIZE) + rateBasedTxBufferFee(feeRate);
|
|
497
|
-
};
|
|
498
|
-
var getEstimatedSize = (inputUtxos, outputs) => {
|
|
499
|
-
const inputSize = inputUtxos.reduce((acc, u) => {
|
|
500
|
-
const script4 = Buffer.from(u.scriptPubKey, "hex");
|
|
501
|
-
const decompiledScript = import_bitcoinjs_lib4.script.decompile(script4);
|
|
502
|
-
if (!decompiledScript) {
|
|
503
|
-
return acc;
|
|
504
|
-
}
|
|
505
|
-
return acc + getInputSizeByScript(script4);
|
|
506
|
-
}, 0);
|
|
507
|
-
const outputSize = outputs.reduce((acc, output) => {
|
|
508
|
-
if (isOP_RETURN(output.scriptPubKey)) {
|
|
509
|
-
return acc + output.scriptPubKey.length + OP_RETURN_OUTPUT_VALUE_SIZE + OP_RETURN_VALUE_SERIALIZE_SIZE;
|
|
510
|
-
}
|
|
511
|
-
return acc + MAX_NON_LEGACY_OUTPUT_SIZE;
|
|
512
|
-
}, 0);
|
|
513
|
-
return inputSize + outputSize + TX_BUFFER_SIZE_OVERHEAD;
|
|
514
|
-
};
|
|
515
|
-
var rateBasedTxBufferFee = (feeRate) => {
|
|
516
|
-
return feeRate <= WALLET_RELAY_FEE_RATE_THRESHOLD ? LOW_RATE_ESTIMATION_ACCURACY_BUFFER : 0;
|
|
517
|
-
};
|
|
518
|
-
|
|
519
|
-
// src/utils/staking/index.ts
|
|
520
|
-
var import_bitcoinjs_lib5 = require("bitcoinjs-lib");
|
|
521
|
-
|
|
522
|
-
// src/constants/unbonding.ts
|
|
523
|
-
var MIN_UNBONDING_OUTPUT_VALUE = 1e3;
|
|
177
|
+
// src/constants/internalPubkey.ts
|
|
178
|
+
var key = "0250929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0";
|
|
179
|
+
var internalPubkey = Buffer.from(key, "hex").subarray(1, 33);
|
|
524
180
|
|
|
525
181
|
// src/utils/staking/index.ts
|
|
526
182
|
var buildStakingTransactionOutputs = (scripts, network, amount) => {
|
|
@@ -546,7 +202,7 @@ var deriveStakingOutputInfo = (scripts, network) => {
|
|
|
546
202
|
},
|
|
547
203
|
[{ output: scripts.unbondingScript }, { output: scripts.timelockScript }]
|
|
548
204
|
];
|
|
549
|
-
const stakingOutput =
|
|
205
|
+
const stakingOutput = import_bitcoinjs_lib2.payments.p2tr({
|
|
550
206
|
internalPubkey,
|
|
551
207
|
scriptTree,
|
|
552
208
|
network
|
|
@@ -559,7 +215,7 @@ var deriveStakingOutputInfo = (scripts, network) => {
|
|
|
559
215
|
}
|
|
560
216
|
return {
|
|
561
217
|
outputAddress: stakingOutput.address,
|
|
562
|
-
scriptPubKey:
|
|
218
|
+
scriptPubKey: import_bitcoinjs_lib2.address.toOutputScript(stakingOutput.address, network)
|
|
563
219
|
};
|
|
564
220
|
};
|
|
565
221
|
var deriveUnbondingOutputInfo = (scripts, network) => {
|
|
@@ -569,7 +225,7 @@ var deriveUnbondingOutputInfo = (scripts, network) => {
|
|
|
569
225
|
},
|
|
570
226
|
{ output: scripts.unbondingTimelockScript }
|
|
571
227
|
];
|
|
572
|
-
const unbondingOutput =
|
|
228
|
+
const unbondingOutput = import_bitcoinjs_lib2.payments.p2tr({
|
|
573
229
|
internalPubkey,
|
|
574
230
|
scriptTree: outputScriptTree,
|
|
575
231
|
network
|
|
@@ -582,11 +238,11 @@ var deriveUnbondingOutputInfo = (scripts, network) => {
|
|
|
582
238
|
}
|
|
583
239
|
return {
|
|
584
240
|
outputAddress: unbondingOutput.address,
|
|
585
|
-
scriptPubKey:
|
|
241
|
+
scriptPubKey: import_bitcoinjs_lib2.address.toOutputScript(unbondingOutput.address, network)
|
|
586
242
|
};
|
|
587
243
|
};
|
|
588
244
|
var deriveSlashingOutput = (scripts, network) => {
|
|
589
|
-
const slashingOutput =
|
|
245
|
+
const slashingOutput = import_bitcoinjs_lib2.payments.p2tr({
|
|
590
246
|
internalPubkey,
|
|
591
247
|
scriptTree: { output: scripts.unbondingTimelockScript },
|
|
592
248
|
network
|
|
@@ -600,12 +256,16 @@ var deriveSlashingOutput = (scripts, network) => {
|
|
|
600
256
|
}
|
|
601
257
|
return {
|
|
602
258
|
outputAddress: slashingOutputAddress,
|
|
603
|
-
scriptPubKey:
|
|
259
|
+
scriptPubKey: import_bitcoinjs_lib2.address.toOutputScript(slashingOutputAddress, network)
|
|
604
260
|
};
|
|
605
261
|
};
|
|
606
262
|
var findMatchingTxOutputIndex = (tx, outputAddress, network) => {
|
|
607
263
|
const index = tx.outs.findIndex((output) => {
|
|
608
|
-
|
|
264
|
+
try {
|
|
265
|
+
return import_bitcoinjs_lib2.address.fromOutputScript(output.script, network) === outputAddress;
|
|
266
|
+
} catch (error) {
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
609
269
|
});
|
|
610
270
|
if (index === -1) {
|
|
611
271
|
throw new StakingError(
|
|
@@ -615,135 +275,9 @@ var findMatchingTxOutputIndex = (tx, outputAddress, network) => {
|
|
|
615
275
|
}
|
|
616
276
|
return index;
|
|
617
277
|
};
|
|
618
|
-
var validateStakingTxInputData = (stakingAmountSat, timelock, params, inputUTXOs, feeRate) => {
|
|
619
|
-
if (stakingAmountSat < params.minStakingAmountSat || stakingAmountSat > params.maxStakingAmountSat) {
|
|
620
|
-
throw new StakingError(
|
|
621
|
-
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
622
|
-
"Invalid staking amount"
|
|
623
|
-
);
|
|
624
|
-
}
|
|
625
|
-
if (timelock < params.minStakingTimeBlocks || timelock > params.maxStakingTimeBlocks) {
|
|
626
|
-
throw new StakingError(
|
|
627
|
-
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
628
|
-
"Invalid timelock"
|
|
629
|
-
);
|
|
630
|
-
}
|
|
631
|
-
if (inputUTXOs.length == 0) {
|
|
632
|
-
throw new StakingError(
|
|
633
|
-
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
634
|
-
"No input UTXOs provided"
|
|
635
|
-
);
|
|
636
|
-
}
|
|
637
|
-
if (feeRate <= 0) {
|
|
638
|
-
throw new StakingError(
|
|
639
|
-
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
640
|
-
"Invalid fee rate"
|
|
641
|
-
);
|
|
642
|
-
}
|
|
643
|
-
};
|
|
644
|
-
var validateParams = (params) => {
|
|
645
|
-
if (params.covenantNoCoordPks.length == 0) {
|
|
646
|
-
throw new StakingError(
|
|
647
|
-
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
648
|
-
"Could not find any covenant public keys"
|
|
649
|
-
);
|
|
650
|
-
}
|
|
651
|
-
if (params.covenantNoCoordPks.length < params.covenantQuorum) {
|
|
652
|
-
throw new StakingError(
|
|
653
|
-
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
654
|
-
"Covenant public keys must be greater than or equal to the quorum"
|
|
655
|
-
);
|
|
656
|
-
}
|
|
657
|
-
params.covenantNoCoordPks.forEach((pk) => {
|
|
658
|
-
if (!isValidNoCoordPublicKey(pk)) {
|
|
659
|
-
throw new StakingError(
|
|
660
|
-
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
661
|
-
"Covenant public key should contains no coordinate"
|
|
662
|
-
);
|
|
663
|
-
}
|
|
664
|
-
});
|
|
665
|
-
if (params.unbondingTime <= 0) {
|
|
666
|
-
throw new StakingError(
|
|
667
|
-
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
668
|
-
"Unbonding time must be greater than 0"
|
|
669
|
-
);
|
|
670
|
-
}
|
|
671
|
-
if (params.unbondingFeeSat <= 0) {
|
|
672
|
-
throw new StakingError(
|
|
673
|
-
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
674
|
-
"Unbonding fee must be greater than 0"
|
|
675
|
-
);
|
|
676
|
-
}
|
|
677
|
-
if (params.maxStakingAmountSat < params.minStakingAmountSat) {
|
|
678
|
-
throw new StakingError(
|
|
679
|
-
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
680
|
-
"Max staking amount must be greater or equal to min staking amount"
|
|
681
|
-
);
|
|
682
|
-
}
|
|
683
|
-
if (params.minStakingAmountSat < params.unbondingFeeSat + MIN_UNBONDING_OUTPUT_VALUE) {
|
|
684
|
-
throw new StakingError(
|
|
685
|
-
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
686
|
-
`Min staking amount must be greater than unbonding fee plus ${MIN_UNBONDING_OUTPUT_VALUE}`
|
|
687
|
-
);
|
|
688
|
-
}
|
|
689
|
-
if (params.maxStakingTimeBlocks < params.minStakingTimeBlocks) {
|
|
690
|
-
throw new StakingError(
|
|
691
|
-
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
692
|
-
"Max staking time must be greater or equal to min staking time"
|
|
693
|
-
);
|
|
694
|
-
}
|
|
695
|
-
if (params.minStakingTimeBlocks <= 0) {
|
|
696
|
-
throw new StakingError(
|
|
697
|
-
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
698
|
-
"Min staking time must be greater than 0"
|
|
699
|
-
);
|
|
700
|
-
}
|
|
701
|
-
if (params.covenantQuorum <= 0) {
|
|
702
|
-
throw new StakingError(
|
|
703
|
-
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
704
|
-
"Covenant quorum must be greater than 0"
|
|
705
|
-
);
|
|
706
|
-
}
|
|
707
|
-
if (params.slashing) {
|
|
708
|
-
if (params.slashing.slashingRate <= 0) {
|
|
709
|
-
throw new StakingError(
|
|
710
|
-
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
711
|
-
"Slashing rate must be greater than 0"
|
|
712
|
-
);
|
|
713
|
-
}
|
|
714
|
-
if (params.slashing.slashingRate > 1) {
|
|
715
|
-
throw new StakingError(
|
|
716
|
-
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
717
|
-
"Slashing rate must be less or equal to 1"
|
|
718
|
-
);
|
|
719
|
-
}
|
|
720
|
-
if (params.slashing.slashingPkScriptHex.length == 0) {
|
|
721
|
-
throw new StakingError(
|
|
722
|
-
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
723
|
-
"Slashing public key script is missing"
|
|
724
|
-
);
|
|
725
|
-
}
|
|
726
|
-
if (params.slashing.minSlashingTxFeeSat <= 0) {
|
|
727
|
-
throw new StakingError(
|
|
728
|
-
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
729
|
-
"Minimum slashing transaction fee must be greater than 0"
|
|
730
|
-
);
|
|
731
|
-
}
|
|
732
|
-
}
|
|
733
|
-
};
|
|
734
|
-
var validateStakingTimelock = (stakingTimelock, params) => {
|
|
735
|
-
if (stakingTimelock < params.minStakingTimeBlocks || stakingTimelock > params.maxStakingTimeBlocks) {
|
|
736
|
-
throw new StakingError(
|
|
737
|
-
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
738
|
-
"Staking transaction timelock is out of range"
|
|
739
|
-
);
|
|
740
|
-
}
|
|
741
|
-
};
|
|
742
278
|
var toBuffers = (inputs) => {
|
|
743
279
|
try {
|
|
744
|
-
return inputs.map(
|
|
745
|
-
(i) => Buffer.from(i, "hex")
|
|
746
|
-
);
|
|
280
|
+
return inputs.map((i) => Buffer.from(i, "hex"));
|
|
747
281
|
} catch (error) {
|
|
748
282
|
throw StakingError.fromUnknown(
|
|
749
283
|
error,
|
|
@@ -752,561 +286,1284 @@ var toBuffers = (inputs) => {
|
|
|
752
286
|
);
|
|
753
287
|
}
|
|
754
288
|
};
|
|
289
|
+
var clearTxSignatures = (tx) => {
|
|
290
|
+
tx.ins.forEach((input) => {
|
|
291
|
+
input.script = Buffer.alloc(0);
|
|
292
|
+
input.witness = [];
|
|
293
|
+
});
|
|
294
|
+
return tx;
|
|
295
|
+
};
|
|
296
|
+
var deriveMerkleProof = (merkle) => {
|
|
297
|
+
const proofHex = merkle.reduce((acc, m) => {
|
|
298
|
+
return acc + Buffer.from(m, "hex").reverse().toString("hex");
|
|
299
|
+
}, "");
|
|
300
|
+
return proofHex;
|
|
301
|
+
};
|
|
302
|
+
var extractFirstSchnorrSignatureFromTransaction = (singedTransaction) => {
|
|
303
|
+
for (const input of singedTransaction.ins) {
|
|
304
|
+
if (input.witness && input.witness.length > 0) {
|
|
305
|
+
const schnorrSignature = input.witness[0];
|
|
306
|
+
if (schnorrSignature.length === 64) {
|
|
307
|
+
return schnorrSignature;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return void 0;
|
|
312
|
+
};
|
|
755
313
|
|
|
756
|
-
// src/
|
|
757
|
-
var
|
|
758
|
-
var TRANSACTION_VERSION = 2;
|
|
314
|
+
// src/staking/psbt.ts
|
|
315
|
+
var import_bitcoinjs_lib4 = require("bitcoinjs-lib");
|
|
759
316
|
|
|
760
317
|
// src/constants/transaction.ts
|
|
761
318
|
var REDEEM_VERSION = 192;
|
|
762
319
|
|
|
763
|
-
// src/
|
|
764
|
-
var
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
if (amount <= 0 || feeRate <= 0) {
|
|
768
|
-
throw new Error("Amount and fee rate must be bigger than 0");
|
|
769
|
-
}
|
|
770
|
-
if (!isValidBitcoinAddress(changeAddress, network)) {
|
|
771
|
-
throw new Error("Invalid change address");
|
|
772
|
-
}
|
|
773
|
-
const stakingOutputs = buildStakingTransactionOutputs(scripts, network, amount);
|
|
774
|
-
const { selectedUTXOs, fee } = getStakingTxInputUTXOsAndFees(
|
|
775
|
-
inputUTXOs,
|
|
776
|
-
amount,
|
|
777
|
-
feeRate,
|
|
778
|
-
stakingOutputs
|
|
320
|
+
// src/utils/utxo/findInputUTXO.ts
|
|
321
|
+
var findInputUTXO = (inputUTXOs, input) => {
|
|
322
|
+
const inputUTXO = inputUTXOs.find(
|
|
323
|
+
(u) => transactionIdToHash(u.txid).toString("hex") === input.hash.toString("hex") && u.vout === input.index
|
|
779
324
|
);
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
const input = selectedUTXOs[i];
|
|
784
|
-
tx.addInput(
|
|
785
|
-
transactionIdToHash(input.txid),
|
|
786
|
-
input.vout,
|
|
787
|
-
NON_RBF_SEQUENCE
|
|
325
|
+
if (!inputUTXO) {
|
|
326
|
+
throw new Error(
|
|
327
|
+
`Input UTXO not found for txid: ${Buffer.from(input.hash).reverse().toString("hex")} and vout: ${input.index}`
|
|
788
328
|
);
|
|
789
329
|
}
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
330
|
+
return inputUTXO;
|
|
331
|
+
};
|
|
332
|
+
|
|
333
|
+
// src/utils/utxo/getScriptType.ts
|
|
334
|
+
var import_bitcoinjs_lib3 = require("bitcoinjs-lib");
|
|
335
|
+
var BitcoinScriptType = /* @__PURE__ */ ((BitcoinScriptType2) => {
|
|
336
|
+
BitcoinScriptType2["P2PKH"] = "pubkeyhash";
|
|
337
|
+
BitcoinScriptType2["P2SH"] = "scripthash";
|
|
338
|
+
BitcoinScriptType2["P2WPKH"] = "witnesspubkeyhash";
|
|
339
|
+
BitcoinScriptType2["P2WSH"] = "witnessscripthash";
|
|
340
|
+
BitcoinScriptType2["P2TR"] = "taproot";
|
|
341
|
+
return BitcoinScriptType2;
|
|
342
|
+
})(BitcoinScriptType || {});
|
|
343
|
+
var getScriptType = (script4) => {
|
|
344
|
+
try {
|
|
345
|
+
import_bitcoinjs_lib3.payments.p2pkh({ output: script4 });
|
|
346
|
+
return "pubkeyhash" /* P2PKH */;
|
|
347
|
+
} catch {
|
|
799
348
|
}
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
349
|
+
try {
|
|
350
|
+
import_bitcoinjs_lib3.payments.p2sh({ output: script4 });
|
|
351
|
+
return "scripthash" /* P2SH */;
|
|
352
|
+
} catch {
|
|
353
|
+
}
|
|
354
|
+
try {
|
|
355
|
+
import_bitcoinjs_lib3.payments.p2wpkh({ output: script4 });
|
|
356
|
+
return "witnesspubkeyhash" /* P2WPKH */;
|
|
357
|
+
} catch {
|
|
358
|
+
}
|
|
359
|
+
try {
|
|
360
|
+
import_bitcoinjs_lib3.payments.p2wsh({ output: script4 });
|
|
361
|
+
return "witnessscripthash" /* P2WSH */;
|
|
362
|
+
} catch {
|
|
363
|
+
}
|
|
364
|
+
try {
|
|
365
|
+
import_bitcoinjs_lib3.payments.p2tr({ output: script4 });
|
|
366
|
+
return "taproot" /* P2TR */;
|
|
367
|
+
} catch {
|
|
368
|
+
}
|
|
369
|
+
throw new Error("Unknown script type");
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
// src/utils/utxo/getPsbtInputFields.ts
|
|
373
|
+
var getPsbtInputFields = (utxo, publicKeyNoCoord) => {
|
|
374
|
+
const scriptPubKey = Buffer.from(utxo.scriptPubKey, "hex");
|
|
375
|
+
const type = getScriptType(scriptPubKey);
|
|
376
|
+
switch (type) {
|
|
377
|
+
case "pubkeyhash" /* P2PKH */: {
|
|
378
|
+
if (!utxo.rawTxHex) {
|
|
379
|
+
throw new Error("Missing rawTxHex for legacy P2PKH input");
|
|
380
|
+
}
|
|
381
|
+
return { nonWitnessUtxo: Buffer.from(utxo.rawTxHex, "hex") };
|
|
803
382
|
}
|
|
804
|
-
|
|
383
|
+
case "scripthash" /* P2SH */: {
|
|
384
|
+
if (!utxo.rawTxHex) {
|
|
385
|
+
throw new Error("Missing rawTxHex for P2SH input");
|
|
386
|
+
}
|
|
387
|
+
if (!utxo.redeemScript) {
|
|
388
|
+
throw new Error("Missing redeemScript for P2SH input");
|
|
389
|
+
}
|
|
390
|
+
return {
|
|
391
|
+
nonWitnessUtxo: Buffer.from(utxo.rawTxHex, "hex"),
|
|
392
|
+
redeemScript: Buffer.from(utxo.redeemScript, "hex")
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
case "witnesspubkeyhash" /* P2WPKH */: {
|
|
396
|
+
return {
|
|
397
|
+
witnessUtxo: {
|
|
398
|
+
script: scriptPubKey,
|
|
399
|
+
value: utxo.value
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
case "witnessscripthash" /* P2WSH */: {
|
|
404
|
+
if (!utxo.witnessScript) {
|
|
405
|
+
throw new Error("Missing witnessScript for P2WSH input");
|
|
406
|
+
}
|
|
407
|
+
return {
|
|
408
|
+
witnessUtxo: {
|
|
409
|
+
script: scriptPubKey,
|
|
410
|
+
value: utxo.value
|
|
411
|
+
},
|
|
412
|
+
witnessScript: Buffer.from(utxo.witnessScript, "hex")
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
case "taproot" /* P2TR */: {
|
|
416
|
+
return {
|
|
417
|
+
witnessUtxo: {
|
|
418
|
+
script: scriptPubKey,
|
|
419
|
+
value: utxo.value
|
|
420
|
+
},
|
|
421
|
+
// this is needed only if the wallet is in taproot mode
|
|
422
|
+
...publicKeyNoCoord && { tapInternalKey: publicKeyNoCoord }
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
default:
|
|
426
|
+
throw new Error(`Unsupported script type: ${type}`);
|
|
805
427
|
}
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
const scriptTree = [
|
|
813
|
-
{
|
|
814
|
-
output: scripts.slashingScript
|
|
815
|
-
},
|
|
816
|
-
{ output: scripts.unbondingTimelockScript }
|
|
817
|
-
];
|
|
818
|
-
return withdrawalTransaction(
|
|
819
|
-
{
|
|
820
|
-
timelockScript: scripts.unbondingTimelockScript
|
|
821
|
-
},
|
|
822
|
-
scriptTree,
|
|
823
|
-
unbondingTx,
|
|
824
|
-
withdrawalAddress,
|
|
825
|
-
network,
|
|
826
|
-
feeRate,
|
|
827
|
-
0
|
|
828
|
-
// unbonding always has a single output
|
|
829
|
-
);
|
|
830
|
-
}
|
|
831
|
-
function withdrawTimelockUnbondedTransaction(scripts, tx, withdrawalAddress, network, feeRate, outputIndex = 0) {
|
|
832
|
-
const scriptTree = [
|
|
833
|
-
{
|
|
834
|
-
output: scripts.slashingScript
|
|
835
|
-
},
|
|
836
|
-
[{ output: scripts.unbondingScript }, { output: scripts.timelockScript }]
|
|
837
|
-
];
|
|
838
|
-
return withdrawalTransaction(
|
|
839
|
-
scripts,
|
|
840
|
-
scriptTree,
|
|
841
|
-
tx,
|
|
842
|
-
withdrawalAddress,
|
|
843
|
-
network,
|
|
844
|
-
feeRate,
|
|
845
|
-
outputIndex
|
|
846
|
-
);
|
|
847
|
-
}
|
|
848
|
-
function withdrawSlashingTransaction(scripts, slashingTx, withdrawalAddress, network, feeRate, outputIndex) {
|
|
849
|
-
const scriptTree = { output: scripts.unbondingTimelockScript };
|
|
850
|
-
return withdrawalTransaction(
|
|
851
|
-
{
|
|
852
|
-
timelockScript: scripts.unbondingTimelockScript
|
|
853
|
-
},
|
|
854
|
-
scriptTree,
|
|
855
|
-
slashingTx,
|
|
856
|
-
withdrawalAddress,
|
|
857
|
-
network,
|
|
858
|
-
feeRate,
|
|
859
|
-
outputIndex
|
|
860
|
-
);
|
|
861
|
-
}
|
|
862
|
-
function withdrawalTransaction(scripts, scriptTree, tx, withdrawalAddress, network, feeRate, outputIndex = 0) {
|
|
863
|
-
if (feeRate <= 0) {
|
|
864
|
-
throw new Error("Withdrawal feeRate must be bigger than 0");
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
// src/staking/psbt.ts
|
|
431
|
+
var stakingPsbt = (stakingTx, network, inputUTXOs, publicKeyNoCoord) => {
|
|
432
|
+
if (publicKeyNoCoord && publicKeyNoCoord.length !== NO_COORD_PK_BYTE_LENGTH) {
|
|
433
|
+
throw new Error("Invalid public key");
|
|
865
434
|
}
|
|
866
|
-
|
|
867
|
-
|
|
435
|
+
const psbt = new import_bitcoinjs_lib4.Psbt({ network });
|
|
436
|
+
if (stakingTx.version !== void 0) psbt.setVersion(stakingTx.version);
|
|
437
|
+
if (stakingTx.locktime !== void 0) psbt.setLocktime(stakingTx.locktime);
|
|
438
|
+
stakingTx.ins.forEach((input) => {
|
|
439
|
+
const inputUTXO = findInputUTXO(inputUTXOs, input);
|
|
440
|
+
const psbtInputData = getPsbtInputFields(inputUTXO, publicKeyNoCoord);
|
|
441
|
+
psbt.addInput({
|
|
442
|
+
hash: input.hash,
|
|
443
|
+
index: input.index,
|
|
444
|
+
sequence: input.sequence,
|
|
445
|
+
...psbtInputData
|
|
446
|
+
});
|
|
447
|
+
});
|
|
448
|
+
stakingTx.outs.forEach((o) => {
|
|
449
|
+
psbt.addOutput({ script: o.script, value: o.value });
|
|
450
|
+
});
|
|
451
|
+
return psbt;
|
|
452
|
+
};
|
|
453
|
+
var stakingExpansionPsbt = (network, stakingTx, previousStakingTxInfo, inputUTXOs, previousScripts, publicKeyNoCoord) => {
|
|
454
|
+
const psbt = new import_bitcoinjs_lib4.Psbt({ network });
|
|
455
|
+
if (stakingTx.version !== void 0) psbt.setVersion(stakingTx.version);
|
|
456
|
+
if (stakingTx.locktime !== void 0) psbt.setLocktime(stakingTx.locktime);
|
|
457
|
+
if (publicKeyNoCoord && publicKeyNoCoord.length !== NO_COORD_PK_BYTE_LENGTH) {
|
|
458
|
+
throw new Error("Invalid public key");
|
|
868
459
|
}
|
|
869
|
-
const
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
throw new Error("Timelock script is not valid");
|
|
460
|
+
const previousStakingOutput = previousStakingTxInfo.stakingTx.outs[previousStakingTxInfo.outputIndex];
|
|
461
|
+
if (!previousStakingOutput) {
|
|
462
|
+
throw new Error("Previous staking output not found");
|
|
873
463
|
}
|
|
874
|
-
|
|
875
|
-
if (
|
|
876
|
-
|
|
877
|
-
timelock = import_bitcoinjs_lib6.script.number.decode(timeBuffer);
|
|
878
|
-
} else {
|
|
879
|
-
const wrap = decompiled[timePosition] % 16;
|
|
880
|
-
timelock = wrap === 0 ? 16 : wrap;
|
|
464
|
+
;
|
|
465
|
+
if (getScriptType(previousStakingOutput.script) !== "taproot" /* P2TR */) {
|
|
466
|
+
throw new Error("Previous staking output script type is not P2TR");
|
|
881
467
|
}
|
|
882
|
-
|
|
883
|
-
|
|
468
|
+
if (stakingTx.ins.length !== 2) {
|
|
469
|
+
throw new Error(
|
|
470
|
+
"Staking expansion transaction must have exactly 2 inputs"
|
|
471
|
+
);
|
|
472
|
+
}
|
|
473
|
+
const txInputs = stakingTx.ins;
|
|
474
|
+
if (Buffer.from(txInputs[0].hash).reverse().toString("hex") !== previousStakingTxInfo.stakingTx.getId()) {
|
|
475
|
+
throw new Error("Previous staking input hash does not match");
|
|
476
|
+
} else if (txInputs[0].index !== previousStakingTxInfo.outputIndex) {
|
|
477
|
+
throw new Error("Previous staking input index does not match");
|
|
478
|
+
}
|
|
479
|
+
const inputScriptTree = [
|
|
480
|
+
{ output: previousScripts.slashingScript },
|
|
481
|
+
[{ output: previousScripts.unbondingScript }, { output: previousScripts.timelockScript }]
|
|
482
|
+
];
|
|
483
|
+
const inputRedeem = {
|
|
484
|
+
output: previousScripts.unbondingScript,
|
|
884
485
|
redeemVersion: REDEEM_VERSION
|
|
885
486
|
};
|
|
886
|
-
const p2tr =
|
|
487
|
+
const p2tr = import_bitcoinjs_lib4.payments.p2tr({
|
|
887
488
|
internalPubkey,
|
|
888
|
-
scriptTree,
|
|
889
|
-
redeem,
|
|
489
|
+
scriptTree: inputScriptTree,
|
|
490
|
+
redeem: inputRedeem,
|
|
890
491
|
network
|
|
891
492
|
});
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
493
|
+
if (!p2tr.witness || p2tr.witness.length === 0) {
|
|
494
|
+
throw new Error(
|
|
495
|
+
"Failed to create P2TR witness for expansion transaction input"
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
const inputTapLeafScript = {
|
|
499
|
+
leafVersion: inputRedeem.redeemVersion,
|
|
500
|
+
script: inputRedeem.output,
|
|
895
501
|
controlBlock: p2tr.witness[p2tr.witness.length - 1]
|
|
896
502
|
};
|
|
897
|
-
const psbt = new import_bitcoinjs_lib6.Psbt({ network });
|
|
898
|
-
psbt.setVersion(TRANSACTION_VERSION);
|
|
899
503
|
psbt.addInput({
|
|
900
|
-
hash:
|
|
901
|
-
index:
|
|
902
|
-
|
|
504
|
+
hash: txInputs[0].hash,
|
|
505
|
+
index: txInputs[0].index,
|
|
506
|
+
sequence: txInputs[0].sequence,
|
|
903
507
|
witnessUtxo: {
|
|
904
|
-
|
|
905
|
-
|
|
508
|
+
script: previousStakingOutput.script,
|
|
509
|
+
value: previousStakingOutput.value
|
|
906
510
|
},
|
|
907
|
-
|
|
908
|
-
|
|
511
|
+
tapInternalKey: internalPubkey,
|
|
512
|
+
tapLeafScript: [inputTapLeafScript]
|
|
909
513
|
});
|
|
910
|
-
const
|
|
911
|
-
const
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
if (outputValue < BTC_DUST_SAT) {
|
|
918
|
-
throw new Error("Output value is less than dust limit");
|
|
919
|
-
}
|
|
920
|
-
psbt.addOutput({
|
|
921
|
-
address: withdrawalAddress,
|
|
922
|
-
value: outputValue
|
|
514
|
+
const inputUTXO = findInputUTXO(inputUTXOs, txInputs[1]);
|
|
515
|
+
const psbtInputData = getPsbtInputFields(inputUTXO, publicKeyNoCoord);
|
|
516
|
+
psbt.addInput({
|
|
517
|
+
hash: txInputs[1].hash,
|
|
518
|
+
index: txInputs[1].index,
|
|
519
|
+
sequence: txInputs[1].sequence,
|
|
520
|
+
...psbtInputData
|
|
923
521
|
});
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
{
|
|
933
|
-
output: scripts.slashingScript
|
|
934
|
-
},
|
|
935
|
-
[{ output: scripts.unbondingScript }, { output: scripts.timelockScript }]
|
|
936
|
-
];
|
|
937
|
-
return slashingTransaction(
|
|
938
|
-
{
|
|
939
|
-
unbondingTimelockScript: scripts.unbondingTimelockScript,
|
|
940
|
-
slashingScript: scripts.slashingScript
|
|
941
|
-
},
|
|
942
|
-
slashingScriptTree,
|
|
943
|
-
stakingTransaction2,
|
|
944
|
-
slashingPkScriptHex,
|
|
945
|
-
slashingRate,
|
|
946
|
-
minimumFee,
|
|
947
|
-
network,
|
|
948
|
-
outputIndex
|
|
949
|
-
);
|
|
950
|
-
}
|
|
951
|
-
function slashEarlyUnbondedTransaction(scripts, unbondingTx, slashingPkScriptHex, slashingRate, minimumSlashingFee, network) {
|
|
952
|
-
const unbondingScriptTree = [
|
|
953
|
-
{
|
|
954
|
-
output: scripts.slashingScript
|
|
955
|
-
},
|
|
956
|
-
{
|
|
957
|
-
output: scripts.unbondingTimelockScript
|
|
958
|
-
}
|
|
959
|
-
];
|
|
960
|
-
return slashingTransaction(
|
|
961
|
-
{
|
|
962
|
-
unbondingTimelockScript: scripts.unbondingTimelockScript,
|
|
963
|
-
slashingScript: scripts.slashingScript
|
|
964
|
-
},
|
|
965
|
-
unbondingScriptTree,
|
|
966
|
-
unbondingTx,
|
|
967
|
-
slashingPkScriptHex,
|
|
968
|
-
slashingRate,
|
|
969
|
-
minimumSlashingFee,
|
|
970
|
-
network,
|
|
971
|
-
0
|
|
972
|
-
// unbonding always has a single output
|
|
973
|
-
);
|
|
974
|
-
}
|
|
975
|
-
function slashingTransaction(scripts, scriptTree, transaction, slashingPkScriptHex, slashingRate, minimumFee, network, outputIndex = 0) {
|
|
976
|
-
if (slashingRate <= 0 || slashingRate >= 1) {
|
|
977
|
-
throw new Error("Slashing rate must be between 0 and 1");
|
|
522
|
+
stakingTx.outs.forEach((o) => {
|
|
523
|
+
psbt.addOutput({ script: o.script, value: o.value });
|
|
524
|
+
});
|
|
525
|
+
return psbt;
|
|
526
|
+
};
|
|
527
|
+
var unbondingPsbt = (scripts, unbondingTx, stakingTx, network) => {
|
|
528
|
+
if (unbondingTx.outs.length !== 1) {
|
|
529
|
+
throw new Error("Unbonding transaction must have exactly one output");
|
|
978
530
|
}
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
throw new Error("Minimum fee must be a positve integer");
|
|
531
|
+
if (unbondingTx.ins.length !== 1) {
|
|
532
|
+
throw new Error("Unbonding transaction must have exactly one input");
|
|
982
533
|
}
|
|
983
|
-
|
|
984
|
-
|
|
534
|
+
validateUnbondingOutput(scripts, unbondingTx, network);
|
|
535
|
+
const psbt = new import_bitcoinjs_lib4.Psbt({ network });
|
|
536
|
+
if (unbondingTx.version !== void 0) {
|
|
537
|
+
psbt.setVersion(unbondingTx.version);
|
|
985
538
|
}
|
|
986
|
-
if (
|
|
987
|
-
|
|
539
|
+
if (unbondingTx.locktime !== void 0) {
|
|
540
|
+
psbt.setLocktime(unbondingTx.locktime);
|
|
988
541
|
}
|
|
989
|
-
const
|
|
990
|
-
|
|
542
|
+
const input = unbondingTx.ins[0];
|
|
543
|
+
const outputIndex = input.index;
|
|
544
|
+
const inputScriptTree = [
|
|
545
|
+
{ output: scripts.slashingScript },
|
|
546
|
+
[{ output: scripts.unbondingScript }, { output: scripts.timelockScript }]
|
|
547
|
+
];
|
|
548
|
+
const inputRedeem = {
|
|
549
|
+
output: scripts.unbondingScript,
|
|
991
550
|
redeemVersion: REDEEM_VERSION
|
|
992
551
|
};
|
|
993
|
-
const p2tr =
|
|
552
|
+
const p2tr = import_bitcoinjs_lib4.payments.p2tr({
|
|
994
553
|
internalPubkey,
|
|
995
|
-
scriptTree,
|
|
996
|
-
redeem,
|
|
554
|
+
scriptTree: inputScriptTree,
|
|
555
|
+
redeem: inputRedeem,
|
|
997
556
|
network
|
|
998
557
|
});
|
|
999
|
-
const
|
|
1000
|
-
leafVersion:
|
|
1001
|
-
script:
|
|
558
|
+
const inputTapLeafScript = {
|
|
559
|
+
leafVersion: inputRedeem.redeemVersion,
|
|
560
|
+
script: inputRedeem.output,
|
|
1002
561
|
controlBlock: p2tr.witness[p2tr.witness.length - 1]
|
|
1003
562
|
};
|
|
1004
|
-
const stakingAmount = transaction.outs[outputIndex].value;
|
|
1005
|
-
const slashingAmount = Math.round(stakingAmount * slashingRate);
|
|
1006
|
-
const slashingOutput = Buffer.from(slashingPkScriptHex, "hex");
|
|
1007
|
-
if (import_bitcoinjs_lib6.opcodes.OP_RETURN != slashingOutput[0]) {
|
|
1008
|
-
if (slashingAmount <= BTC_DUST_SAT) {
|
|
1009
|
-
throw new Error("Slashing amount is less than dust limit");
|
|
1010
|
-
}
|
|
1011
|
-
}
|
|
1012
|
-
const userFunds = stakingAmount - slashingAmount - minimumFee;
|
|
1013
|
-
if (userFunds <= BTC_DUST_SAT) {
|
|
1014
|
-
throw new Error("User funds are less than dust limit");
|
|
1015
|
-
}
|
|
1016
|
-
const psbt = new import_bitcoinjs_lib6.Psbt({ network });
|
|
1017
|
-
psbt.setVersion(TRANSACTION_VERSION);
|
|
1018
563
|
psbt.addInput({
|
|
1019
|
-
hash:
|
|
1020
|
-
index:
|
|
564
|
+
hash: input.hash,
|
|
565
|
+
index: input.index,
|
|
566
|
+
sequence: input.sequence,
|
|
1021
567
|
tapInternalKey: internalPubkey,
|
|
1022
568
|
witnessUtxo: {
|
|
1023
|
-
value:
|
|
1024
|
-
script:
|
|
569
|
+
value: stakingTx.outs[outputIndex].value,
|
|
570
|
+
script: stakingTx.outs[outputIndex].script
|
|
1025
571
|
},
|
|
1026
|
-
tapLeafScript: [
|
|
1027
|
-
// not RBF-able
|
|
1028
|
-
sequence: NON_RBF_SEQUENCE
|
|
1029
|
-
});
|
|
1030
|
-
psbt.addOutput({
|
|
1031
|
-
script: slashingOutput,
|
|
1032
|
-
value: slashingAmount
|
|
1033
|
-
});
|
|
1034
|
-
const changeOutput = import_bitcoinjs_lib6.payments.p2tr({
|
|
1035
|
-
internalPubkey,
|
|
1036
|
-
scriptTree: { output: scripts.unbondingTimelockScript },
|
|
1037
|
-
network
|
|
572
|
+
tapLeafScript: [inputTapLeafScript]
|
|
1038
573
|
});
|
|
1039
574
|
psbt.addOutput({
|
|
1040
|
-
|
|
1041
|
-
value:
|
|
1042
|
-
});
|
|
1043
|
-
psbt.setLocktime(0);
|
|
1044
|
-
return { psbt };
|
|
1045
|
-
}
|
|
1046
|
-
function unbondingTransaction(scripts, stakingTx, unbondingFee, network, outputIndex = 0) {
|
|
1047
|
-
if (unbondingFee <= 0) {
|
|
1048
|
-
throw new Error("Unbonding fee must be bigger than 0");
|
|
1049
|
-
}
|
|
1050
|
-
if (outputIndex < 0) {
|
|
1051
|
-
throw new Error("Output index must be bigger or equal to 0");
|
|
1052
|
-
}
|
|
1053
|
-
const tx = new import_bitcoinjs_lib6.Transaction();
|
|
1054
|
-
tx.version = TRANSACTION_VERSION;
|
|
1055
|
-
tx.addInput(
|
|
1056
|
-
stakingTx.getHash(),
|
|
1057
|
-
outputIndex,
|
|
1058
|
-
NON_RBF_SEQUENCE
|
|
1059
|
-
// not RBF-able
|
|
1060
|
-
);
|
|
1061
|
-
const unbondingOutputInfo = deriveUnbondingOutputInfo(scripts, network);
|
|
1062
|
-
const outputValue = stakingTx.outs[outputIndex].value - unbondingFee;
|
|
1063
|
-
if (outputValue < BTC_DUST_SAT) {
|
|
1064
|
-
throw new Error("Output value is less than dust limit for unbonding transaction");
|
|
1065
|
-
}
|
|
1066
|
-
if (!unbondingOutputInfo.outputAddress) {
|
|
1067
|
-
throw new Error("Unbonding output address is not defined");
|
|
1068
|
-
}
|
|
1069
|
-
tx.addOutput(
|
|
1070
|
-
unbondingOutputInfo.scriptPubKey,
|
|
1071
|
-
outputValue
|
|
1072
|
-
);
|
|
1073
|
-
tx.locktime = 0;
|
|
1074
|
-
return {
|
|
1075
|
-
transaction: tx,
|
|
1076
|
-
fee: unbondingFee
|
|
1077
|
-
};
|
|
1078
|
-
}
|
|
1079
|
-
var createCovenantWitness = (originalWitness, paramsCovenants, covenantSigs, covenantQuorum) => {
|
|
1080
|
-
if (covenantSigs.length < covenantQuorum) {
|
|
1081
|
-
throw new Error(
|
|
1082
|
-
`Not enough covenant signatures. Required: ${covenantQuorum}, got: ${covenantSigs.length}`
|
|
1083
|
-
);
|
|
1084
|
-
}
|
|
1085
|
-
for (const sig of covenantSigs) {
|
|
1086
|
-
const btcPkHexBuf = Buffer.from(sig.btcPkHex, "hex");
|
|
1087
|
-
if (!paramsCovenants.some((covenant) => covenant.equals(btcPkHexBuf))) {
|
|
1088
|
-
throw new Error(
|
|
1089
|
-
`Covenant signature public key ${sig.btcPkHex} not found in params covenants`
|
|
1090
|
-
);
|
|
1091
|
-
}
|
|
1092
|
-
}
|
|
1093
|
-
const covenantSigsBuffers = covenantSigs.slice(0, covenantQuorum).map((sig) => ({
|
|
1094
|
-
btcPkHex: Buffer.from(sig.btcPkHex, "hex"),
|
|
1095
|
-
sigHex: Buffer.from(sig.sigHex, "hex")
|
|
1096
|
-
}));
|
|
1097
|
-
const paramsCovenantsSorted = [...paramsCovenants].sort(Buffer.compare).reverse();
|
|
1098
|
-
const composedCovenantSigs = paramsCovenantsSorted.map((covenant) => {
|
|
1099
|
-
const covenantSig = covenantSigsBuffers.find(
|
|
1100
|
-
(sig) => sig.btcPkHex.compare(covenant) === 0
|
|
1101
|
-
);
|
|
1102
|
-
return covenantSig?.sigHex || Buffer.alloc(0);
|
|
575
|
+
script: unbondingTx.outs[0].script,
|
|
576
|
+
value: unbondingTx.outs[0].value
|
|
1103
577
|
});
|
|
1104
|
-
return
|
|
578
|
+
return psbt;
|
|
1105
579
|
};
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
// src/utils/utxo/findInputUTXO.ts
|
|
1111
|
-
var findInputUTXO = (inputUTXOs, input) => {
|
|
1112
|
-
const inputUTXO = inputUTXOs.find(
|
|
1113
|
-
(u) => transactionIdToHash(u.txid).toString("hex") === input.hash.toString("hex") && u.vout === input.index
|
|
1114
|
-
);
|
|
1115
|
-
if (!inputUTXO) {
|
|
580
|
+
var validateUnbondingOutput = (scripts, unbondingTx, network) => {
|
|
581
|
+
const unbondingOutputInfo = deriveUnbondingOutputInfo(scripts, network);
|
|
582
|
+
if (unbondingOutputInfo.scriptPubKey.toString("hex") !== unbondingTx.outs[0].script.toString("hex")) {
|
|
1116
583
|
throw new Error(
|
|
1117
|
-
|
|
584
|
+
"Unbonding output script does not match the expected script while building psbt"
|
|
1118
585
|
);
|
|
1119
586
|
}
|
|
1120
|
-
return inputUTXO;
|
|
1121
587
|
};
|
|
1122
588
|
|
|
1123
|
-
// src/
|
|
1124
|
-
var
|
|
1125
|
-
var
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
import_bitcoinjs_lib7.payments.p2sh({ output: script4 });
|
|
1141
|
-
return "scripthash" /* P2SH */;
|
|
1142
|
-
} catch {
|
|
1143
|
-
}
|
|
1144
|
-
try {
|
|
1145
|
-
import_bitcoinjs_lib7.payments.p2wpkh({ output: script4 });
|
|
1146
|
-
return "witnesspubkeyhash" /* P2WPKH */;
|
|
1147
|
-
} catch {
|
|
589
|
+
// src/staking/stakingScript.ts
|
|
590
|
+
var import_bitcoinjs_lib5 = require("bitcoinjs-lib");
|
|
591
|
+
var MAGIC_BYTES_LEN = 4;
|
|
592
|
+
var StakingScriptData = class {
|
|
593
|
+
constructor(stakerKey, finalityProviderKeys, covenantKeys, covenantThreshold, stakingTimelock, unbondingTimelock) {
|
|
594
|
+
if (!stakerKey || !finalityProviderKeys || !covenantKeys || !covenantThreshold || !stakingTimelock || !unbondingTimelock) {
|
|
595
|
+
throw new Error("Missing required input values");
|
|
596
|
+
}
|
|
597
|
+
this.stakerKey = stakerKey;
|
|
598
|
+
this.finalityProviderKeys = finalityProviderKeys;
|
|
599
|
+
this.covenantKeys = covenantKeys;
|
|
600
|
+
this.covenantThreshold = covenantThreshold;
|
|
601
|
+
this.stakingTimeLock = stakingTimelock;
|
|
602
|
+
this.unbondingTimeLock = unbondingTimelock;
|
|
603
|
+
if (!this.validate()) {
|
|
604
|
+
throw new Error("Invalid script data provided");
|
|
605
|
+
}
|
|
1148
606
|
}
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
607
|
+
/**
|
|
608
|
+
* Validates the staking script.
|
|
609
|
+
* @returns {boolean} Returns true if the staking script is valid, otherwise false.
|
|
610
|
+
*/
|
|
611
|
+
validate() {
|
|
612
|
+
if (this.stakerKey.length != NO_COORD_PK_BYTE_LENGTH) {
|
|
613
|
+
return false;
|
|
614
|
+
}
|
|
615
|
+
if (this.finalityProviderKeys.some(
|
|
616
|
+
(finalityProviderKey) => finalityProviderKey.length != NO_COORD_PK_BYTE_LENGTH
|
|
617
|
+
)) {
|
|
618
|
+
return false;
|
|
619
|
+
}
|
|
620
|
+
if (this.covenantKeys.some((covenantKey) => covenantKey.length != NO_COORD_PK_BYTE_LENGTH)) {
|
|
621
|
+
return false;
|
|
622
|
+
}
|
|
623
|
+
const allPks = [
|
|
624
|
+
this.stakerKey,
|
|
625
|
+
...this.finalityProviderKeys,
|
|
626
|
+
...this.covenantKeys
|
|
627
|
+
];
|
|
628
|
+
const allPksSet = new Set(allPks);
|
|
629
|
+
if (allPks.length !== allPksSet.size) {
|
|
630
|
+
return false;
|
|
631
|
+
}
|
|
632
|
+
if (this.covenantThreshold <= 0 || this.covenantThreshold > this.covenantKeys.length) {
|
|
633
|
+
return false;
|
|
634
|
+
}
|
|
635
|
+
if (this.stakingTimeLock <= 0 || this.stakingTimeLock > 65535) {
|
|
636
|
+
return false;
|
|
637
|
+
}
|
|
638
|
+
if (this.unbondingTimeLock <= 0 || this.unbondingTimeLock > 65535) {
|
|
639
|
+
return false;
|
|
640
|
+
}
|
|
641
|
+
return true;
|
|
1153
642
|
}
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
643
|
+
// The staking script allows for multiple finality provider public keys
|
|
644
|
+
// to support (re)stake to multiple finality providers
|
|
645
|
+
// Covenant members are going to have multiple keys
|
|
646
|
+
/**
|
|
647
|
+
* Builds a timelock script.
|
|
648
|
+
* @param timelock - The timelock value to encode in the script.
|
|
649
|
+
* @returns {Buffer} containing the compiled timelock script.
|
|
650
|
+
*/
|
|
651
|
+
buildTimelockScript(timelock) {
|
|
652
|
+
return import_bitcoinjs_lib5.script.compile([
|
|
653
|
+
this.stakerKey,
|
|
654
|
+
import_bitcoinjs_lib5.opcodes.OP_CHECKSIGVERIFY,
|
|
655
|
+
import_bitcoinjs_lib5.script.number.encode(timelock),
|
|
656
|
+
import_bitcoinjs_lib5.opcodes.OP_CHECKSEQUENCEVERIFY
|
|
657
|
+
]);
|
|
1158
658
|
}
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
659
|
+
/**
|
|
660
|
+
* Builds the staking timelock script.
|
|
661
|
+
* Only holder of private key for given pubKey can spend after relative lock time
|
|
662
|
+
* Creates the timelock script in the form:
|
|
663
|
+
* <stakerPubKey>
|
|
664
|
+
* OP_CHECKSIGVERIFY
|
|
665
|
+
* <stakingTimeBlocks>
|
|
666
|
+
* OP_CHECKSEQUENCEVERIFY
|
|
667
|
+
* @returns {Buffer} The staking timelock script.
|
|
668
|
+
*/
|
|
669
|
+
buildStakingTimelockScript() {
|
|
670
|
+
return this.buildTimelockScript(this.stakingTimeLock);
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Builds the unbonding timelock script.
|
|
674
|
+
* Creates the unbonding timelock script in the form:
|
|
675
|
+
* <stakerPubKey>
|
|
676
|
+
* OP_CHECKSIGVERIFY
|
|
677
|
+
* <unbondingTimeBlocks>
|
|
678
|
+
* OP_CHECKSEQUENCEVERIFY
|
|
679
|
+
* @returns {Buffer} The unbonding timelock script.
|
|
680
|
+
*/
|
|
681
|
+
buildUnbondingTimelockScript() {
|
|
682
|
+
return this.buildTimelockScript(this.unbondingTimeLock);
|
|
683
|
+
}
|
|
684
|
+
/**
|
|
685
|
+
* Builds the unbonding script in the form:
|
|
686
|
+
* buildSingleKeyScript(stakerPk, true) ||
|
|
687
|
+
* buildMultiKeyScript(covenantPks, covenantThreshold, false)
|
|
688
|
+
* || means combining the scripts
|
|
689
|
+
* @returns {Buffer} The unbonding script.
|
|
690
|
+
*/
|
|
691
|
+
buildUnbondingScript() {
|
|
692
|
+
return Buffer.concat([
|
|
693
|
+
this.buildSingleKeyScript(this.stakerKey, true),
|
|
694
|
+
this.buildMultiKeyScript(
|
|
695
|
+
this.covenantKeys,
|
|
696
|
+
this.covenantThreshold,
|
|
697
|
+
false
|
|
698
|
+
)
|
|
699
|
+
]);
|
|
700
|
+
}
|
|
701
|
+
/**
|
|
702
|
+
* Builds the slashing script for staking in the form:
|
|
703
|
+
* buildSingleKeyScript(stakerPk, true) ||
|
|
704
|
+
* buildMultiKeyScript(finalityProviderPKs, 1, true) ||
|
|
705
|
+
* buildMultiKeyScript(covenantPks, covenantThreshold, false)
|
|
706
|
+
* || means combining the scripts
|
|
707
|
+
* The slashing script is a combination of single-key and multi-key scripts.
|
|
708
|
+
* The single-key script is used for staker key verification.
|
|
709
|
+
* The multi-key script is used for finality provider key verification and covenant key verification.
|
|
710
|
+
* @returns {Buffer} The slashing script as a Buffer.
|
|
711
|
+
*/
|
|
712
|
+
buildSlashingScript() {
|
|
713
|
+
return Buffer.concat([
|
|
714
|
+
this.buildSingleKeyScript(this.stakerKey, true),
|
|
715
|
+
this.buildMultiKeyScript(
|
|
716
|
+
this.finalityProviderKeys,
|
|
717
|
+
// The threshold is always 1 as we only need one
|
|
718
|
+
// finalityProvider signature to perform slashing
|
|
719
|
+
// (only one finalityProvider performs an offence)
|
|
720
|
+
1,
|
|
721
|
+
// OP_VERIFY/OP_CHECKSIGVERIFY is added at the end
|
|
722
|
+
true
|
|
723
|
+
),
|
|
724
|
+
this.buildMultiKeyScript(
|
|
725
|
+
this.covenantKeys,
|
|
726
|
+
this.covenantThreshold,
|
|
727
|
+
// No need to add verify since covenants are at the end of the script
|
|
728
|
+
false
|
|
729
|
+
)
|
|
730
|
+
]);
|
|
731
|
+
}
|
|
732
|
+
/**
|
|
733
|
+
* Builds the staking scripts.
|
|
734
|
+
* @returns {StakingScripts} The staking scripts.
|
|
735
|
+
*/
|
|
736
|
+
buildScripts() {
|
|
737
|
+
return {
|
|
738
|
+
timelockScript: this.buildStakingTimelockScript(),
|
|
739
|
+
unbondingScript: this.buildUnbondingScript(),
|
|
740
|
+
slashingScript: this.buildSlashingScript(),
|
|
741
|
+
unbondingTimelockScript: this.buildUnbondingTimelockScript()
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
// buildSingleKeyScript and buildMultiKeyScript allow us to reuse functionality
|
|
745
|
+
// for creating Bitcoin scripts for the unbonding script and the slashing script
|
|
746
|
+
/**
|
|
747
|
+
* Builds a single key script in the form:
|
|
748
|
+
* buildSingleKeyScript creates a single key script
|
|
749
|
+
* <pk> OP_CHECKSIGVERIFY (if withVerify is true)
|
|
750
|
+
* <pk> OP_CHECKSIG (if withVerify is false)
|
|
751
|
+
* @param pk - The public key buffer.
|
|
752
|
+
* @param withVerify - A boolean indicating whether to include the OP_CHECKSIGVERIFY opcode.
|
|
753
|
+
* @returns The compiled script buffer.
|
|
754
|
+
*/
|
|
755
|
+
buildSingleKeyScript(pk, withVerify) {
|
|
756
|
+
if (pk.length != NO_COORD_PK_BYTE_LENGTH) {
|
|
757
|
+
throw new Error("Invalid key length");
|
|
1172
758
|
}
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
759
|
+
return import_bitcoinjs_lib5.script.compile([
|
|
760
|
+
pk,
|
|
761
|
+
withVerify ? import_bitcoinjs_lib5.opcodes.OP_CHECKSIGVERIFY : import_bitcoinjs_lib5.opcodes.OP_CHECKSIG
|
|
762
|
+
]);
|
|
763
|
+
}
|
|
764
|
+
/**
|
|
765
|
+
* Builds a multi-key script in the form:
|
|
766
|
+
* <pk1> OP_CHEKCSIG <pk2> OP_CHECKSIGADD <pk3> OP_CHECKSIGADD ... <pkN> OP_CHECKSIGADD <threshold> OP_NUMEQUAL
|
|
767
|
+
* <withVerify -> OP_NUMEQUALVERIFY>
|
|
768
|
+
* It validates whether provided keys are unique and the threshold is not greater than number of keys
|
|
769
|
+
* If there is only one key provided it will return single key sig script
|
|
770
|
+
* @param pks - An array of public keys.
|
|
771
|
+
* @param threshold - The required number of valid signers.
|
|
772
|
+
* @param withVerify - A boolean indicating whether to include the OP_VERIFY opcode.
|
|
773
|
+
* @returns The compiled multi-key script as a Buffer.
|
|
774
|
+
* @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.
|
|
775
|
+
*/
|
|
776
|
+
buildMultiKeyScript(pks, threshold, withVerify) {
|
|
777
|
+
if (!pks || pks.length === 0) {
|
|
778
|
+
throw new Error("No keys provided");
|
|
1184
779
|
}
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
witnessUtxo: {
|
|
1188
|
-
script: scriptPubKey,
|
|
1189
|
-
value: utxo.value
|
|
1190
|
-
}
|
|
1191
|
-
};
|
|
780
|
+
if (pks.some((pk) => pk.length != NO_COORD_PK_BYTE_LENGTH)) {
|
|
781
|
+
throw new Error("Invalid key length");
|
|
1192
782
|
}
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
783
|
+
if (threshold > pks.length) {
|
|
784
|
+
throw new Error(
|
|
785
|
+
"Required number of valid signers is greater than number of provided keys"
|
|
786
|
+
);
|
|
787
|
+
}
|
|
788
|
+
if (pks.length === 1) {
|
|
789
|
+
return this.buildSingleKeyScript(pks[0], withVerify);
|
|
790
|
+
}
|
|
791
|
+
const sortedPks = [...pks].sort(Buffer.compare);
|
|
792
|
+
for (let i = 0; i < sortedPks.length - 1; ++i) {
|
|
793
|
+
if (sortedPks[i].equals(sortedPks[i + 1])) {
|
|
794
|
+
throw new Error("Duplicate keys provided");
|
|
1196
795
|
}
|
|
1197
|
-
return {
|
|
1198
|
-
witnessUtxo: {
|
|
1199
|
-
script: scriptPubKey,
|
|
1200
|
-
value: utxo.value
|
|
1201
|
-
},
|
|
1202
|
-
witnessScript: Buffer.from(utxo.witnessScript, "hex")
|
|
1203
|
-
};
|
|
1204
796
|
}
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
value: utxo.value
|
|
1210
|
-
},
|
|
1211
|
-
// this is needed only if the wallet is in taproot mode
|
|
1212
|
-
...publicKeyNoCoord && { tapInternalKey: publicKeyNoCoord }
|
|
1213
|
-
};
|
|
797
|
+
const scriptElements = [sortedPks[0], import_bitcoinjs_lib5.opcodes.OP_CHECKSIG];
|
|
798
|
+
for (let i = 1; i < sortedPks.length; i++) {
|
|
799
|
+
scriptElements.push(sortedPks[i]);
|
|
800
|
+
scriptElements.push(import_bitcoinjs_lib5.opcodes.OP_CHECKSIGADD);
|
|
1214
801
|
}
|
|
1215
|
-
|
|
1216
|
-
|
|
802
|
+
scriptElements.push(import_bitcoinjs_lib5.script.number.encode(threshold));
|
|
803
|
+
if (withVerify) {
|
|
804
|
+
scriptElements.push(import_bitcoinjs_lib5.opcodes.OP_NUMEQUALVERIFY);
|
|
805
|
+
} else {
|
|
806
|
+
scriptElements.push(import_bitcoinjs_lib5.opcodes.OP_NUMEQUAL);
|
|
807
|
+
}
|
|
808
|
+
return import_bitcoinjs_lib5.script.compile(scriptElements);
|
|
1217
809
|
}
|
|
1218
810
|
};
|
|
1219
811
|
|
|
1220
|
-
// src/staking/
|
|
1221
|
-
var
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
812
|
+
// src/staking/transactions.ts
|
|
813
|
+
var import_bitcoinjs_lib8 = require("bitcoinjs-lib");
|
|
814
|
+
|
|
815
|
+
// src/constants/dustSat.ts
|
|
816
|
+
var BTC_DUST_SAT = 546;
|
|
817
|
+
|
|
818
|
+
// src/utils/fee/index.ts
|
|
819
|
+
var import_bitcoinjs_lib7 = require("bitcoinjs-lib");
|
|
820
|
+
|
|
821
|
+
// src/constants/fee.ts
|
|
822
|
+
var DEFAULT_INPUT_SIZE = 180;
|
|
823
|
+
var P2WPKH_INPUT_SIZE = 68;
|
|
824
|
+
var P2TR_INPUT_SIZE = 58;
|
|
825
|
+
var P2TR_STAKING_EXPANSION_INPUT_SIZE = 268;
|
|
826
|
+
var TX_BUFFER_SIZE_OVERHEAD = 11;
|
|
827
|
+
var LOW_RATE_ESTIMATION_ACCURACY_BUFFER = 30;
|
|
828
|
+
var MAX_NON_LEGACY_OUTPUT_SIZE = 43;
|
|
829
|
+
var WITHDRAW_TX_BUFFER_SIZE = 17;
|
|
830
|
+
var WALLET_RELAY_FEE_RATE_THRESHOLD = 2;
|
|
831
|
+
var OP_RETURN_OUTPUT_VALUE_SIZE = 8;
|
|
832
|
+
var OP_RETURN_VALUE_SERIALIZE_SIZE = 1;
|
|
833
|
+
|
|
834
|
+
// src/utils/fee/utils.ts
|
|
835
|
+
var import_bitcoinjs_lib6 = require("bitcoinjs-lib");
|
|
836
|
+
var isOP_RETURN = (script4) => {
|
|
837
|
+
const decompiled = import_bitcoinjs_lib6.script.decompile(script4);
|
|
838
|
+
return !!decompiled && decompiled[0] === import_bitcoinjs_lib6.opcodes.OP_RETURN;
|
|
1244
839
|
};
|
|
1245
|
-
var
|
|
1246
|
-
|
|
1247
|
-
|
|
840
|
+
var getInputSizeByScript = (script4) => {
|
|
841
|
+
try {
|
|
842
|
+
const { address: p2wpkhAddress } = import_bitcoinjs_lib6.payments.p2wpkh({
|
|
843
|
+
output: script4
|
|
844
|
+
});
|
|
845
|
+
if (p2wpkhAddress) {
|
|
846
|
+
return P2WPKH_INPUT_SIZE;
|
|
847
|
+
}
|
|
848
|
+
} catch (error) {
|
|
1248
849
|
}
|
|
1249
|
-
|
|
1250
|
-
|
|
850
|
+
try {
|
|
851
|
+
const { address: p2trAddress } = import_bitcoinjs_lib6.payments.p2tr({
|
|
852
|
+
output: script4
|
|
853
|
+
});
|
|
854
|
+
if (p2trAddress) {
|
|
855
|
+
return P2TR_INPUT_SIZE;
|
|
856
|
+
}
|
|
857
|
+
} catch (error) {
|
|
1251
858
|
}
|
|
1252
|
-
|
|
859
|
+
return DEFAULT_INPUT_SIZE;
|
|
860
|
+
};
|
|
861
|
+
var getEstimatedChangeOutputSize = () => {
|
|
862
|
+
return MAX_NON_LEGACY_OUTPUT_SIZE;
|
|
863
|
+
};
|
|
864
|
+
var inputValueSum = (inputUTXOs) => {
|
|
865
|
+
return inputUTXOs.reduce((acc, utxo) => acc + utxo.value, 0);
|
|
866
|
+
};
|
|
867
|
+
|
|
868
|
+
// src/utils/fee/index.ts
|
|
869
|
+
var getStakingTxInputUTXOsAndFees = (availableUTXOs, stakingAmount, feeRate, outputs) => {
|
|
870
|
+
if (availableUTXOs.length === 0) {
|
|
871
|
+
throw new Error("Insufficient funds");
|
|
872
|
+
}
|
|
873
|
+
const validUTXOs = availableUTXOs.filter((utxo) => {
|
|
874
|
+
const script4 = Buffer.from(utxo.scriptPubKey, "hex");
|
|
875
|
+
return !!import_bitcoinjs_lib7.script.decompile(script4);
|
|
876
|
+
});
|
|
877
|
+
if (validUTXOs.length === 0) {
|
|
878
|
+
throw new Error("Insufficient funds: no valid UTXOs available for staking");
|
|
879
|
+
}
|
|
880
|
+
const sortedUTXOs = validUTXOs.sort((a, b) => b.value - a.value);
|
|
881
|
+
const selectedUTXOs = [];
|
|
882
|
+
let accumulatedValue = 0;
|
|
883
|
+
let estimatedFee = 0;
|
|
884
|
+
for (const utxo of sortedUTXOs) {
|
|
885
|
+
selectedUTXOs.push(utxo);
|
|
886
|
+
accumulatedValue += utxo.value;
|
|
887
|
+
const estimatedSize = getEstimatedSize(selectedUTXOs, outputs);
|
|
888
|
+
estimatedFee = estimatedSize * feeRate + rateBasedTxBufferFee(feeRate);
|
|
889
|
+
if (accumulatedValue - (stakingAmount + estimatedFee) > BTC_DUST_SAT) {
|
|
890
|
+
estimatedFee += getEstimatedChangeOutputSize() * feeRate;
|
|
891
|
+
}
|
|
892
|
+
if (accumulatedValue >= stakingAmount + estimatedFee) {
|
|
893
|
+
break;
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
if (accumulatedValue < stakingAmount + estimatedFee) {
|
|
897
|
+
throw new Error(
|
|
898
|
+
"Insufficient funds: unable to gather enough UTXOs to cover the staking amount and fees"
|
|
899
|
+
);
|
|
900
|
+
}
|
|
901
|
+
return {
|
|
902
|
+
selectedUTXOs,
|
|
903
|
+
fee: estimatedFee
|
|
904
|
+
};
|
|
905
|
+
};
|
|
906
|
+
var getStakingExpansionTxFundingUTXOAndFees = (availableUTXOs, feeRate, outputs) => {
|
|
907
|
+
if (availableUTXOs.length === 0) {
|
|
908
|
+
throw new Error("Insufficient funds");
|
|
909
|
+
}
|
|
910
|
+
const validUTXOs = availableUTXOs.filter((utxo) => {
|
|
911
|
+
const script4 = Buffer.from(utxo.scriptPubKey, "hex");
|
|
912
|
+
const decompiledScript = import_bitcoinjs_lib7.script.decompile(script4);
|
|
913
|
+
return decompiledScript && decompiledScript.length > 0;
|
|
914
|
+
});
|
|
915
|
+
if (validUTXOs.length === 0) {
|
|
916
|
+
throw new Error("Insufficient funds: no valid UTXOs available for staking");
|
|
917
|
+
}
|
|
918
|
+
const sortedUTXOs = validUTXOs.sort((a, b) => a.value - b.value);
|
|
919
|
+
for (const utxo of sortedUTXOs) {
|
|
920
|
+
const estimatedSize = getEstimatedSize(
|
|
921
|
+
[utxo],
|
|
922
|
+
outputs
|
|
923
|
+
) + P2TR_STAKING_EXPANSION_INPUT_SIZE;
|
|
924
|
+
let estimatedFee = estimatedSize * feeRate + rateBasedTxBufferFee(feeRate);
|
|
925
|
+
if (utxo.value >= estimatedFee) {
|
|
926
|
+
if (utxo.value - estimatedFee > BTC_DUST_SAT) {
|
|
927
|
+
estimatedFee += getEstimatedChangeOutputSize() * feeRate;
|
|
928
|
+
}
|
|
929
|
+
if (utxo.value >= estimatedFee) {
|
|
930
|
+
return {
|
|
931
|
+
selectedUTXO: utxo,
|
|
932
|
+
fee: estimatedFee
|
|
933
|
+
};
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
throw new Error(
|
|
938
|
+
"Insufficient funds: unable to find a UTXO to cover the fees for the staking expansion transaction."
|
|
939
|
+
);
|
|
940
|
+
};
|
|
941
|
+
var getWithdrawTxFee = (feeRate) => {
|
|
942
|
+
const inputSize = P2TR_INPUT_SIZE;
|
|
943
|
+
const outputSize = getEstimatedChangeOutputSize();
|
|
944
|
+
return feeRate * (inputSize + outputSize + TX_BUFFER_SIZE_OVERHEAD + WITHDRAW_TX_BUFFER_SIZE) + rateBasedTxBufferFee(feeRate);
|
|
945
|
+
};
|
|
946
|
+
var getEstimatedSize = (inputUtxos, outputs) => {
|
|
947
|
+
const inputSize = inputUtxos.reduce((acc, u) => {
|
|
948
|
+
const script4 = Buffer.from(u.scriptPubKey, "hex");
|
|
949
|
+
const decompiledScript = import_bitcoinjs_lib7.script.decompile(script4);
|
|
950
|
+
if (!decompiledScript) {
|
|
951
|
+
return acc;
|
|
952
|
+
}
|
|
953
|
+
return acc + getInputSizeByScript(script4);
|
|
954
|
+
}, 0);
|
|
955
|
+
const outputSize = outputs.reduce((acc, output) => {
|
|
956
|
+
if (isOP_RETURN(output.scriptPubKey)) {
|
|
957
|
+
return acc + output.scriptPubKey.length + OP_RETURN_OUTPUT_VALUE_SIZE + OP_RETURN_VALUE_SERIALIZE_SIZE;
|
|
958
|
+
}
|
|
959
|
+
return acc + MAX_NON_LEGACY_OUTPUT_SIZE;
|
|
960
|
+
}, 0);
|
|
961
|
+
return inputSize + outputSize + TX_BUFFER_SIZE_OVERHEAD;
|
|
962
|
+
};
|
|
963
|
+
var rateBasedTxBufferFee = (feeRate) => {
|
|
964
|
+
return feeRate <= WALLET_RELAY_FEE_RATE_THRESHOLD ? LOW_RATE_ESTIMATION_ACCURACY_BUFFER : 0;
|
|
965
|
+
};
|
|
966
|
+
|
|
967
|
+
// src/constants/psbt.ts
|
|
968
|
+
var NON_RBF_SEQUENCE = 4294967295;
|
|
969
|
+
var TRANSACTION_VERSION = 2;
|
|
970
|
+
|
|
971
|
+
// src/staking/transactions.ts
|
|
972
|
+
var BTC_LOCKTIME_HEIGHT_TIME_CUTOFF = 5e8;
|
|
973
|
+
var BTC_SLASHING_FRACTION_DIGITS = 4;
|
|
974
|
+
function stakingTransaction(scripts, amount, changeAddress, inputUTXOs, network, feeRate, lockHeight) {
|
|
975
|
+
if (amount <= 0 || feeRate <= 0) {
|
|
976
|
+
throw new Error("Amount and fee rate must be bigger than 0");
|
|
977
|
+
}
|
|
978
|
+
if (!isValidBitcoinAddress(changeAddress, network)) {
|
|
979
|
+
throw new Error("Invalid change address");
|
|
980
|
+
}
|
|
981
|
+
const stakingOutputs = buildStakingTransactionOutputs(scripts, network, amount);
|
|
982
|
+
const { selectedUTXOs, fee } = getStakingTxInputUTXOsAndFees(
|
|
983
|
+
inputUTXOs,
|
|
984
|
+
amount,
|
|
985
|
+
feeRate,
|
|
986
|
+
stakingOutputs
|
|
987
|
+
);
|
|
988
|
+
const tx = new import_bitcoinjs_lib8.Transaction();
|
|
989
|
+
tx.version = TRANSACTION_VERSION;
|
|
990
|
+
for (let i = 0; i < selectedUTXOs.length; ++i) {
|
|
991
|
+
const input = selectedUTXOs[i];
|
|
992
|
+
tx.addInput(
|
|
993
|
+
transactionIdToHash(input.txid),
|
|
994
|
+
input.vout,
|
|
995
|
+
NON_RBF_SEQUENCE
|
|
996
|
+
);
|
|
997
|
+
}
|
|
998
|
+
stakingOutputs.forEach((o) => {
|
|
999
|
+
tx.addOutput(o.scriptPubKey, o.value);
|
|
1000
|
+
});
|
|
1001
|
+
const inputsSum = inputValueSum(selectedUTXOs);
|
|
1002
|
+
if (inputsSum - (amount + fee) > BTC_DUST_SAT) {
|
|
1003
|
+
tx.addOutput(
|
|
1004
|
+
import_bitcoinjs_lib8.address.toOutputScript(changeAddress, network),
|
|
1005
|
+
inputsSum - (amount + fee)
|
|
1006
|
+
);
|
|
1007
|
+
}
|
|
1008
|
+
if (lockHeight) {
|
|
1009
|
+
if (lockHeight >= BTC_LOCKTIME_HEIGHT_TIME_CUTOFF) {
|
|
1010
|
+
throw new Error("Invalid lock height");
|
|
1011
|
+
}
|
|
1012
|
+
tx.locktime = lockHeight;
|
|
1013
|
+
}
|
|
1014
|
+
return {
|
|
1015
|
+
transaction: tx,
|
|
1016
|
+
fee
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
1019
|
+
function stakingExpansionTransaction(network, scripts, amount, changeAddress, feeRate, inputUTXOs, previousStakingTxInfo) {
|
|
1020
|
+
if (amount <= 0 || feeRate <= 0) {
|
|
1021
|
+
throw new Error("Amount and fee rate must be bigger than 0");
|
|
1022
|
+
} else if (!isValidBitcoinAddress(changeAddress, network)) {
|
|
1023
|
+
throw new Error("Invalid BTC change address");
|
|
1024
|
+
}
|
|
1025
|
+
const previousStakingOutputInfo = deriveStakingOutputInfo(
|
|
1026
|
+
previousStakingTxInfo.scripts,
|
|
1027
|
+
network
|
|
1028
|
+
);
|
|
1029
|
+
const previousStakingOutputIndex = findMatchingTxOutputIndex(
|
|
1030
|
+
previousStakingTxInfo.stakingTx,
|
|
1031
|
+
previousStakingOutputInfo.outputAddress,
|
|
1032
|
+
network
|
|
1033
|
+
);
|
|
1034
|
+
const previousStakingAmount = previousStakingTxInfo.stakingTx.outs[previousStakingOutputIndex].value;
|
|
1035
|
+
if (amount !== previousStakingAmount) {
|
|
1036
|
+
throw new Error(
|
|
1037
|
+
"Expansion staking transaction amount must be equal to the previous staking amount. Increase of the staking amount is not supported yet."
|
|
1038
|
+
);
|
|
1039
|
+
}
|
|
1040
|
+
const stakingOutputs = buildStakingTransactionOutputs(
|
|
1041
|
+
scripts,
|
|
1042
|
+
network,
|
|
1043
|
+
amount
|
|
1044
|
+
);
|
|
1045
|
+
const { selectedUTXO, fee } = getStakingExpansionTxFundingUTXOAndFees(
|
|
1046
|
+
inputUTXOs,
|
|
1047
|
+
feeRate,
|
|
1048
|
+
stakingOutputs
|
|
1049
|
+
);
|
|
1050
|
+
const tx = new import_bitcoinjs_lib8.Transaction();
|
|
1051
|
+
tx.version = TRANSACTION_VERSION;
|
|
1052
|
+
tx.addInput(
|
|
1053
|
+
previousStakingTxInfo.stakingTx.getHash(),
|
|
1054
|
+
previousStakingOutputIndex,
|
|
1055
|
+
NON_RBF_SEQUENCE
|
|
1056
|
+
);
|
|
1057
|
+
tx.addInput(
|
|
1058
|
+
transactionIdToHash(selectedUTXO.txid),
|
|
1059
|
+
selectedUTXO.vout,
|
|
1060
|
+
NON_RBF_SEQUENCE
|
|
1061
|
+
);
|
|
1062
|
+
stakingOutputs.forEach((o) => {
|
|
1063
|
+
tx.addOutput(o.scriptPubKey, o.value);
|
|
1064
|
+
});
|
|
1065
|
+
if (selectedUTXO.value - fee > BTC_DUST_SAT) {
|
|
1066
|
+
tx.addOutput(
|
|
1067
|
+
import_bitcoinjs_lib8.address.toOutputScript(changeAddress, network),
|
|
1068
|
+
selectedUTXO.value - fee
|
|
1069
|
+
);
|
|
1070
|
+
}
|
|
1071
|
+
return {
|
|
1072
|
+
transaction: tx,
|
|
1073
|
+
fee,
|
|
1074
|
+
fundingUTXO: selectedUTXO
|
|
1075
|
+
};
|
|
1076
|
+
}
|
|
1077
|
+
function withdrawEarlyUnbondedTransaction(scripts, unbondingTx, withdrawalAddress, network, feeRate) {
|
|
1078
|
+
const scriptTree = [
|
|
1079
|
+
{
|
|
1080
|
+
output: scripts.slashingScript
|
|
1081
|
+
},
|
|
1082
|
+
{ output: scripts.unbondingTimelockScript }
|
|
1083
|
+
];
|
|
1084
|
+
return withdrawalTransaction(
|
|
1085
|
+
{
|
|
1086
|
+
timelockScript: scripts.unbondingTimelockScript
|
|
1087
|
+
},
|
|
1088
|
+
scriptTree,
|
|
1089
|
+
unbondingTx,
|
|
1090
|
+
withdrawalAddress,
|
|
1091
|
+
network,
|
|
1092
|
+
feeRate,
|
|
1093
|
+
0
|
|
1094
|
+
// unbonding always has a single output
|
|
1095
|
+
);
|
|
1096
|
+
}
|
|
1097
|
+
function withdrawTimelockUnbondedTransaction(scripts, tx, withdrawalAddress, network, feeRate, outputIndex = 0) {
|
|
1098
|
+
const scriptTree = [
|
|
1099
|
+
{
|
|
1100
|
+
output: scripts.slashingScript
|
|
1101
|
+
},
|
|
1102
|
+
[{ output: scripts.unbondingScript }, { output: scripts.timelockScript }]
|
|
1103
|
+
];
|
|
1104
|
+
return withdrawalTransaction(
|
|
1105
|
+
scripts,
|
|
1106
|
+
scriptTree,
|
|
1107
|
+
tx,
|
|
1108
|
+
withdrawalAddress,
|
|
1109
|
+
network,
|
|
1110
|
+
feeRate,
|
|
1111
|
+
outputIndex
|
|
1112
|
+
);
|
|
1113
|
+
}
|
|
1114
|
+
function withdrawSlashingTransaction(scripts, slashingTx, withdrawalAddress, network, feeRate, outputIndex) {
|
|
1115
|
+
const scriptTree = { output: scripts.unbondingTimelockScript };
|
|
1116
|
+
return withdrawalTransaction(
|
|
1117
|
+
{
|
|
1118
|
+
timelockScript: scripts.unbondingTimelockScript
|
|
1119
|
+
},
|
|
1120
|
+
scriptTree,
|
|
1121
|
+
slashingTx,
|
|
1122
|
+
withdrawalAddress,
|
|
1123
|
+
network,
|
|
1124
|
+
feeRate,
|
|
1125
|
+
outputIndex
|
|
1126
|
+
);
|
|
1127
|
+
}
|
|
1128
|
+
function withdrawalTransaction(scripts, scriptTree, tx, withdrawalAddress, network, feeRate, outputIndex = 0) {
|
|
1129
|
+
if (feeRate <= 0) {
|
|
1130
|
+
throw new Error("Withdrawal feeRate must be bigger than 0");
|
|
1131
|
+
}
|
|
1132
|
+
if (outputIndex < 0) {
|
|
1133
|
+
throw new Error("Output index must be bigger or equal to 0");
|
|
1134
|
+
}
|
|
1135
|
+
const timePosition = 2;
|
|
1136
|
+
const decompiled = import_bitcoinjs_lib8.script.decompile(scripts.timelockScript);
|
|
1137
|
+
if (!decompiled) {
|
|
1138
|
+
throw new Error("Timelock script is not valid");
|
|
1139
|
+
}
|
|
1140
|
+
let timelock = 0;
|
|
1141
|
+
if (typeof decompiled[timePosition] !== "number") {
|
|
1142
|
+
const timeBuffer = decompiled[timePosition];
|
|
1143
|
+
timelock = import_bitcoinjs_lib8.script.number.decode(timeBuffer);
|
|
1144
|
+
} else {
|
|
1145
|
+
const wrap = decompiled[timePosition] % 16;
|
|
1146
|
+
timelock = wrap === 0 ? 16 : wrap;
|
|
1147
|
+
}
|
|
1148
|
+
const redeem = {
|
|
1149
|
+
output: scripts.timelockScript,
|
|
1150
|
+
redeemVersion: REDEEM_VERSION
|
|
1151
|
+
};
|
|
1152
|
+
const p2tr = import_bitcoinjs_lib8.payments.p2tr({
|
|
1153
|
+
internalPubkey,
|
|
1154
|
+
scriptTree,
|
|
1155
|
+
redeem,
|
|
1156
|
+
network
|
|
1157
|
+
});
|
|
1158
|
+
const tapLeafScript = {
|
|
1159
|
+
leafVersion: redeem.redeemVersion,
|
|
1160
|
+
script: redeem.output,
|
|
1161
|
+
controlBlock: p2tr.witness[p2tr.witness.length - 1]
|
|
1162
|
+
};
|
|
1253
1163
|
const psbt = new import_bitcoinjs_lib8.Psbt({ network });
|
|
1254
|
-
|
|
1255
|
-
|
|
1164
|
+
psbt.setVersion(TRANSACTION_VERSION);
|
|
1165
|
+
psbt.addInput({
|
|
1166
|
+
hash: tx.getHash(),
|
|
1167
|
+
index: outputIndex,
|
|
1168
|
+
tapInternalKey: internalPubkey,
|
|
1169
|
+
witnessUtxo: {
|
|
1170
|
+
value: tx.outs[outputIndex].value,
|
|
1171
|
+
script: tx.outs[outputIndex].script
|
|
1172
|
+
},
|
|
1173
|
+
tapLeafScript: [tapLeafScript],
|
|
1174
|
+
sequence: timelock
|
|
1175
|
+
});
|
|
1176
|
+
const estimatedFee = getWithdrawTxFee(feeRate);
|
|
1177
|
+
const outputValue = tx.outs[outputIndex].value - estimatedFee;
|
|
1178
|
+
if (outputValue < 0) {
|
|
1179
|
+
throw new Error(
|
|
1180
|
+
"Not enough funds to cover the fee for withdrawal transaction"
|
|
1181
|
+
);
|
|
1256
1182
|
}
|
|
1257
|
-
if (
|
|
1258
|
-
|
|
1183
|
+
if (outputValue < BTC_DUST_SAT) {
|
|
1184
|
+
throw new Error("Output value is less than dust limit");
|
|
1185
|
+
}
|
|
1186
|
+
psbt.addOutput({
|
|
1187
|
+
address: withdrawalAddress,
|
|
1188
|
+
value: outputValue
|
|
1189
|
+
});
|
|
1190
|
+
psbt.setLocktime(0);
|
|
1191
|
+
return {
|
|
1192
|
+
psbt,
|
|
1193
|
+
fee: estimatedFee
|
|
1194
|
+
};
|
|
1195
|
+
}
|
|
1196
|
+
function slashTimelockUnbondedTransaction(scripts, stakingTransaction2, slashingPkScriptHex, slashingRate, minimumFee, network, outputIndex = 0) {
|
|
1197
|
+
const slashingScriptTree = [
|
|
1198
|
+
{
|
|
1199
|
+
output: scripts.slashingScript
|
|
1200
|
+
},
|
|
1201
|
+
[{ output: scripts.unbondingScript }, { output: scripts.timelockScript }]
|
|
1202
|
+
];
|
|
1203
|
+
return slashingTransaction(
|
|
1204
|
+
{
|
|
1205
|
+
unbondingTimelockScript: scripts.unbondingTimelockScript,
|
|
1206
|
+
slashingScript: scripts.slashingScript
|
|
1207
|
+
},
|
|
1208
|
+
slashingScriptTree,
|
|
1209
|
+
stakingTransaction2,
|
|
1210
|
+
slashingPkScriptHex,
|
|
1211
|
+
slashingRate,
|
|
1212
|
+
minimumFee,
|
|
1213
|
+
network,
|
|
1214
|
+
outputIndex
|
|
1215
|
+
);
|
|
1216
|
+
}
|
|
1217
|
+
function slashEarlyUnbondedTransaction(scripts, unbondingTx, slashingPkScriptHex, slashingRate, minimumSlashingFee, network) {
|
|
1218
|
+
const unbondingScriptTree = [
|
|
1219
|
+
{
|
|
1220
|
+
output: scripts.slashingScript
|
|
1221
|
+
},
|
|
1222
|
+
{
|
|
1223
|
+
output: scripts.unbondingTimelockScript
|
|
1224
|
+
}
|
|
1225
|
+
];
|
|
1226
|
+
return slashingTransaction(
|
|
1227
|
+
{
|
|
1228
|
+
unbondingTimelockScript: scripts.unbondingTimelockScript,
|
|
1229
|
+
slashingScript: scripts.slashingScript
|
|
1230
|
+
},
|
|
1231
|
+
unbondingScriptTree,
|
|
1232
|
+
unbondingTx,
|
|
1233
|
+
slashingPkScriptHex,
|
|
1234
|
+
slashingRate,
|
|
1235
|
+
minimumSlashingFee,
|
|
1236
|
+
network,
|
|
1237
|
+
0
|
|
1238
|
+
// unbonding always has a single output
|
|
1239
|
+
);
|
|
1240
|
+
}
|
|
1241
|
+
function slashingTransaction(scripts, scriptTree, transaction, slashingPkScriptHex, slashingRate, minimumFee, network, outputIndex = 0) {
|
|
1242
|
+
if (slashingRate <= 0 || slashingRate >= 1) {
|
|
1243
|
+
throw new Error("Slashing rate must be between 0 and 1");
|
|
1244
|
+
}
|
|
1245
|
+
slashingRate = parseFloat(slashingRate.toFixed(BTC_SLASHING_FRACTION_DIGITS));
|
|
1246
|
+
if (minimumFee <= 0 || !Number.isInteger(minimumFee)) {
|
|
1247
|
+
throw new Error("Minimum fee must be a positve integer");
|
|
1248
|
+
}
|
|
1249
|
+
if (outputIndex < 0 || !Number.isInteger(outputIndex)) {
|
|
1250
|
+
throw new Error("Output index must be an integer bigger or equal to 0");
|
|
1251
|
+
}
|
|
1252
|
+
if (!transaction.outs[outputIndex]) {
|
|
1253
|
+
throw new Error("Output index is out of range");
|
|
1259
1254
|
}
|
|
1260
|
-
const
|
|
1261
|
-
|
|
1262
|
-
const inputScriptTree = [
|
|
1263
|
-
{ output: scripts.slashingScript },
|
|
1264
|
-
[{ output: scripts.unbondingScript }, { output: scripts.timelockScript }]
|
|
1265
|
-
];
|
|
1266
|
-
const inputRedeem = {
|
|
1267
|
-
output: scripts.unbondingScript,
|
|
1255
|
+
const redeem = {
|
|
1256
|
+
output: scripts.slashingScript,
|
|
1268
1257
|
redeemVersion: REDEEM_VERSION
|
|
1269
1258
|
};
|
|
1270
1259
|
const p2tr = import_bitcoinjs_lib8.payments.p2tr({
|
|
1271
1260
|
internalPubkey,
|
|
1272
|
-
scriptTree
|
|
1273
|
-
redeem
|
|
1261
|
+
scriptTree,
|
|
1262
|
+
redeem,
|
|
1274
1263
|
network
|
|
1275
1264
|
});
|
|
1276
|
-
const
|
|
1277
|
-
leafVersion:
|
|
1278
|
-
script:
|
|
1265
|
+
const tapLeafScript = {
|
|
1266
|
+
leafVersion: redeem.redeemVersion,
|
|
1267
|
+
script: redeem.output,
|
|
1279
1268
|
controlBlock: p2tr.witness[p2tr.witness.length - 1]
|
|
1280
1269
|
};
|
|
1270
|
+
const stakingAmount = transaction.outs[outputIndex].value;
|
|
1271
|
+
const slashingAmount = Math.round(stakingAmount * slashingRate);
|
|
1272
|
+
const slashingOutput = Buffer.from(slashingPkScriptHex, "hex");
|
|
1273
|
+
if (import_bitcoinjs_lib8.opcodes.OP_RETURN != slashingOutput[0]) {
|
|
1274
|
+
if (slashingAmount <= BTC_DUST_SAT) {
|
|
1275
|
+
throw new Error("Slashing amount is less than dust limit");
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
const userFunds = stakingAmount - slashingAmount - minimumFee;
|
|
1279
|
+
if (userFunds <= BTC_DUST_SAT) {
|
|
1280
|
+
throw new Error("User funds are less than dust limit");
|
|
1281
|
+
}
|
|
1282
|
+
const psbt = new import_bitcoinjs_lib8.Psbt({ network });
|
|
1283
|
+
psbt.setVersion(TRANSACTION_VERSION);
|
|
1281
1284
|
psbt.addInput({
|
|
1282
|
-
hash:
|
|
1283
|
-
index:
|
|
1284
|
-
sequence: input.sequence,
|
|
1285
|
+
hash: transaction.getHash(),
|
|
1286
|
+
index: outputIndex,
|
|
1285
1287
|
tapInternalKey: internalPubkey,
|
|
1286
1288
|
witnessUtxo: {
|
|
1287
|
-
value:
|
|
1288
|
-
script:
|
|
1289
|
+
value: stakingAmount,
|
|
1290
|
+
script: transaction.outs[outputIndex].script
|
|
1289
1291
|
},
|
|
1290
|
-
tapLeafScript: [
|
|
1292
|
+
tapLeafScript: [tapLeafScript],
|
|
1293
|
+
// not RBF-able
|
|
1294
|
+
sequence: NON_RBF_SEQUENCE
|
|
1291
1295
|
});
|
|
1292
1296
|
psbt.addOutput({
|
|
1293
|
-
script:
|
|
1294
|
-
value:
|
|
1297
|
+
script: slashingOutput,
|
|
1298
|
+
value: slashingAmount
|
|
1295
1299
|
});
|
|
1296
|
-
|
|
1300
|
+
const changeOutput = import_bitcoinjs_lib8.payments.p2tr({
|
|
1301
|
+
internalPubkey,
|
|
1302
|
+
scriptTree: { output: scripts.unbondingTimelockScript },
|
|
1303
|
+
network
|
|
1304
|
+
});
|
|
1305
|
+
psbt.addOutput({
|
|
1306
|
+
address: changeOutput.address,
|
|
1307
|
+
value: userFunds
|
|
1308
|
+
});
|
|
1309
|
+
psbt.setLocktime(0);
|
|
1310
|
+
return { psbt };
|
|
1311
|
+
}
|
|
1312
|
+
function unbondingTransaction(scripts, stakingTx, unbondingFee, network, outputIndex = 0) {
|
|
1313
|
+
if (unbondingFee <= 0) {
|
|
1314
|
+
throw new Error("Unbonding fee must be bigger than 0");
|
|
1315
|
+
}
|
|
1316
|
+
if (outputIndex < 0) {
|
|
1317
|
+
throw new Error("Output index must be bigger or equal to 0");
|
|
1318
|
+
}
|
|
1319
|
+
const tx = new import_bitcoinjs_lib8.Transaction();
|
|
1320
|
+
tx.version = TRANSACTION_VERSION;
|
|
1321
|
+
tx.addInput(
|
|
1322
|
+
stakingTx.getHash(),
|
|
1323
|
+
outputIndex,
|
|
1324
|
+
NON_RBF_SEQUENCE
|
|
1325
|
+
// not RBF-able
|
|
1326
|
+
);
|
|
1327
|
+
const unbondingOutputInfo = deriveUnbondingOutputInfo(scripts, network);
|
|
1328
|
+
const outputValue = stakingTx.outs[outputIndex].value - unbondingFee;
|
|
1329
|
+
if (outputValue < BTC_DUST_SAT) {
|
|
1330
|
+
throw new Error("Output value is less than dust limit for unbonding transaction");
|
|
1331
|
+
}
|
|
1332
|
+
if (!unbondingOutputInfo.outputAddress) {
|
|
1333
|
+
throw new Error("Unbonding output address is not defined");
|
|
1334
|
+
}
|
|
1335
|
+
tx.addOutput(
|
|
1336
|
+
unbondingOutputInfo.scriptPubKey,
|
|
1337
|
+
outputValue
|
|
1338
|
+
);
|
|
1339
|
+
tx.locktime = 0;
|
|
1340
|
+
return {
|
|
1341
|
+
transaction: tx,
|
|
1342
|
+
fee: unbondingFee
|
|
1343
|
+
};
|
|
1344
|
+
}
|
|
1345
|
+
var createCovenantWitness = (originalWitness, paramsCovenants, covenantSigs, covenantQuorum) => {
|
|
1346
|
+
if (covenantSigs.length < covenantQuorum) {
|
|
1347
|
+
throw new Error(
|
|
1348
|
+
`Not enough covenant signatures. Required: ${covenantQuorum}, got: ${covenantSigs.length}`
|
|
1349
|
+
);
|
|
1350
|
+
}
|
|
1351
|
+
const filteredCovenantSigs = covenantSigs.filter((sig) => {
|
|
1352
|
+
const btcPkHexBuf = Buffer.from(sig.btcPkHex, "hex");
|
|
1353
|
+
return paramsCovenants.some((covenant) => covenant.equals(btcPkHexBuf));
|
|
1354
|
+
});
|
|
1355
|
+
if (filteredCovenantSigs.length < covenantQuorum) {
|
|
1356
|
+
throw new Error(
|
|
1357
|
+
`Not enough valid covenant signatures. Required: ${covenantQuorum}, got: ${filteredCovenantSigs.length}`
|
|
1358
|
+
);
|
|
1359
|
+
}
|
|
1360
|
+
const covenantSigsBuffers = covenantSigs.slice(0, covenantQuorum).map((sig) => ({
|
|
1361
|
+
btcPkHex: Buffer.from(sig.btcPkHex, "hex"),
|
|
1362
|
+
sigHex: Buffer.from(sig.sigHex, "hex")
|
|
1363
|
+
}));
|
|
1364
|
+
const paramsCovenantsSorted = [...paramsCovenants].sort(Buffer.compare).reverse();
|
|
1365
|
+
const composedCovenantSigs = paramsCovenantsSorted.map((covenant) => {
|
|
1366
|
+
const covenantSig = covenantSigsBuffers.find(
|
|
1367
|
+
(sig) => sig.btcPkHex.compare(covenant) === 0
|
|
1368
|
+
);
|
|
1369
|
+
return covenantSig?.sigHex || Buffer.alloc(0);
|
|
1370
|
+
});
|
|
1371
|
+
return [...composedCovenantSigs, ...originalWitness];
|
|
1372
|
+
};
|
|
1373
|
+
|
|
1374
|
+
// src/constants/unbonding.ts
|
|
1375
|
+
var MIN_UNBONDING_OUTPUT_VALUE = 1e3;
|
|
1376
|
+
|
|
1377
|
+
// src/utils/babylon.ts
|
|
1378
|
+
var import_encoding = require("@cosmjs/encoding");
|
|
1379
|
+
var isValidBabylonAddress = (address4) => {
|
|
1380
|
+
try {
|
|
1381
|
+
const { prefix } = (0, import_encoding.fromBech32)(address4);
|
|
1382
|
+
return prefix === "bbn";
|
|
1383
|
+
} catch (error) {
|
|
1384
|
+
return false;
|
|
1385
|
+
}
|
|
1386
|
+
};
|
|
1387
|
+
|
|
1388
|
+
// src/utils/staking/validation.ts
|
|
1389
|
+
var validateStakingExpansionInputs = ({
|
|
1390
|
+
babylonBtcTipHeight,
|
|
1391
|
+
inputUTXOs,
|
|
1392
|
+
stakingInput,
|
|
1393
|
+
previousStakingInput,
|
|
1394
|
+
babylonAddress
|
|
1395
|
+
}) => {
|
|
1396
|
+
if (babylonBtcTipHeight === 0) {
|
|
1397
|
+
throw new StakingError(
|
|
1398
|
+
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
1399
|
+
"Babylon BTC tip height cannot be 0"
|
|
1400
|
+
);
|
|
1401
|
+
}
|
|
1402
|
+
if (!inputUTXOs || inputUTXOs.length === 0) {
|
|
1403
|
+
throw new StakingError(
|
|
1404
|
+
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
1405
|
+
"No input UTXOs provided"
|
|
1406
|
+
);
|
|
1407
|
+
}
|
|
1408
|
+
if (babylonAddress && !isValidBabylonAddress(babylonAddress)) {
|
|
1409
|
+
throw new StakingError(
|
|
1410
|
+
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
1411
|
+
"Invalid Babylon address"
|
|
1412
|
+
);
|
|
1413
|
+
}
|
|
1414
|
+
if (stakingInput.stakingAmountSat !== previousStakingInput.stakingAmountSat) {
|
|
1415
|
+
throw new StakingError(
|
|
1416
|
+
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
1417
|
+
"Staking expansion amount must equal the previous staking amount"
|
|
1418
|
+
);
|
|
1419
|
+
}
|
|
1420
|
+
const currentFPs = stakingInput.finalityProviderPksNoCoordHex;
|
|
1421
|
+
const previousFPs = previousStakingInput.finalityProviderPksNoCoordHex;
|
|
1422
|
+
const missingPreviousFPs = previousFPs.filter((prevFp) => !currentFPs.includes(prevFp));
|
|
1423
|
+
if (missingPreviousFPs.length > 0) {
|
|
1424
|
+
throw new StakingError(
|
|
1425
|
+
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
1426
|
+
`Invalid staking expansion: all finality providers from the previous
|
|
1427
|
+
staking must be included. Missing: ${missingPreviousFPs.join(", ")}`
|
|
1428
|
+
);
|
|
1429
|
+
}
|
|
1430
|
+
};
|
|
1431
|
+
var validateStakingTxInputData = (stakingAmountSat, timelock, params, inputUTXOs, feeRate) => {
|
|
1432
|
+
if (stakingAmountSat < params.minStakingAmountSat || stakingAmountSat > params.maxStakingAmountSat) {
|
|
1433
|
+
throw new StakingError(
|
|
1434
|
+
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
1435
|
+
"Invalid staking amount"
|
|
1436
|
+
);
|
|
1437
|
+
}
|
|
1438
|
+
if (timelock < params.minStakingTimeBlocks || timelock > params.maxStakingTimeBlocks) {
|
|
1439
|
+
throw new StakingError("INVALID_INPUT" /* INVALID_INPUT */, "Invalid timelock");
|
|
1440
|
+
}
|
|
1441
|
+
if (inputUTXOs.length == 0) {
|
|
1442
|
+
throw new StakingError(
|
|
1443
|
+
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
1444
|
+
"No input UTXOs provided"
|
|
1445
|
+
);
|
|
1446
|
+
}
|
|
1447
|
+
if (feeRate <= 0) {
|
|
1448
|
+
throw new StakingError("INVALID_INPUT" /* INVALID_INPUT */, "Invalid fee rate");
|
|
1449
|
+
}
|
|
1450
|
+
};
|
|
1451
|
+
var validateParams = (params) => {
|
|
1452
|
+
if (params.covenantNoCoordPks.length == 0) {
|
|
1453
|
+
throw new StakingError(
|
|
1454
|
+
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
1455
|
+
"Could not find any covenant public keys"
|
|
1456
|
+
);
|
|
1457
|
+
}
|
|
1458
|
+
if (params.covenantNoCoordPks.length < params.covenantQuorum) {
|
|
1459
|
+
throw new StakingError(
|
|
1460
|
+
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
1461
|
+
"Covenant public keys must be greater than or equal to the quorum"
|
|
1462
|
+
);
|
|
1463
|
+
}
|
|
1464
|
+
params.covenantNoCoordPks.forEach((pk) => {
|
|
1465
|
+
if (!isValidNoCoordPublicKey(pk)) {
|
|
1466
|
+
throw new StakingError(
|
|
1467
|
+
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
1468
|
+
"Covenant public key should contains no coordinate"
|
|
1469
|
+
);
|
|
1470
|
+
}
|
|
1471
|
+
});
|
|
1472
|
+
if (params.unbondingTime <= 0) {
|
|
1473
|
+
throw new StakingError(
|
|
1474
|
+
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
1475
|
+
"Unbonding time must be greater than 0"
|
|
1476
|
+
);
|
|
1477
|
+
}
|
|
1478
|
+
if (params.unbondingFeeSat <= 0) {
|
|
1479
|
+
throw new StakingError(
|
|
1480
|
+
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
1481
|
+
"Unbonding fee must be greater than 0"
|
|
1482
|
+
);
|
|
1483
|
+
}
|
|
1484
|
+
if (params.maxStakingAmountSat < params.minStakingAmountSat) {
|
|
1485
|
+
throw new StakingError(
|
|
1486
|
+
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
1487
|
+
"Max staking amount must be greater or equal to min staking amount"
|
|
1488
|
+
);
|
|
1489
|
+
}
|
|
1490
|
+
if (params.minStakingAmountSat < params.unbondingFeeSat + MIN_UNBONDING_OUTPUT_VALUE) {
|
|
1491
|
+
throw new StakingError(
|
|
1492
|
+
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
1493
|
+
`Min staking amount must be greater than unbonding fee plus ${MIN_UNBONDING_OUTPUT_VALUE}`
|
|
1494
|
+
);
|
|
1495
|
+
}
|
|
1496
|
+
if (params.maxStakingTimeBlocks < params.minStakingTimeBlocks) {
|
|
1497
|
+
throw new StakingError(
|
|
1498
|
+
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
1499
|
+
"Max staking time must be greater or equal to min staking time"
|
|
1500
|
+
);
|
|
1501
|
+
}
|
|
1502
|
+
if (params.minStakingTimeBlocks <= 0) {
|
|
1503
|
+
throw new StakingError(
|
|
1504
|
+
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
1505
|
+
"Min staking time must be greater than 0"
|
|
1506
|
+
);
|
|
1507
|
+
}
|
|
1508
|
+
if (params.covenantQuorum <= 0) {
|
|
1509
|
+
throw new StakingError(
|
|
1510
|
+
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
1511
|
+
"Covenant quorum must be greater than 0"
|
|
1512
|
+
);
|
|
1513
|
+
}
|
|
1514
|
+
if (params.slashing) {
|
|
1515
|
+
if (params.slashing.slashingRate <= 0) {
|
|
1516
|
+
throw new StakingError(
|
|
1517
|
+
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
1518
|
+
"Slashing rate must be greater than 0"
|
|
1519
|
+
);
|
|
1520
|
+
}
|
|
1521
|
+
if (params.slashing.slashingRate > 1) {
|
|
1522
|
+
throw new StakingError(
|
|
1523
|
+
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
1524
|
+
"Slashing rate must be less or equal to 1"
|
|
1525
|
+
);
|
|
1526
|
+
}
|
|
1527
|
+
if (params.slashing.slashingPkScriptHex.length == 0) {
|
|
1528
|
+
throw new StakingError(
|
|
1529
|
+
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
1530
|
+
"Slashing public key script is missing"
|
|
1531
|
+
);
|
|
1532
|
+
}
|
|
1533
|
+
if (params.slashing.minSlashingTxFeeSat <= 0) {
|
|
1534
|
+
throw new StakingError(
|
|
1535
|
+
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
1536
|
+
"Minimum slashing transaction fee must be greater than 0"
|
|
1537
|
+
);
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1297
1540
|
};
|
|
1298
|
-
var
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
"
|
|
1541
|
+
var validateStakingTimelock = (stakingTimelock, params) => {
|
|
1542
|
+
if (stakingTimelock < params.minStakingTimeBlocks || stakingTimelock > params.maxStakingTimeBlocks) {
|
|
1543
|
+
throw new StakingError(
|
|
1544
|
+
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
1545
|
+
"Staking transaction timelock is out of range"
|
|
1546
|
+
);
|
|
1547
|
+
}
|
|
1548
|
+
};
|
|
1549
|
+
var validateStakingExpansionCovenantQuorum = (paramsForPreviousStakingTx, paramsForCurrentStakingTx) => {
|
|
1550
|
+
const previousCovenantMembers = paramsForPreviousStakingTx.covenantNoCoordPks;
|
|
1551
|
+
const currentCovenantMembers = paramsForCurrentStakingTx.covenantNoCoordPks;
|
|
1552
|
+
const requiredQuorum = paramsForPreviousStakingTx.covenantQuorum;
|
|
1553
|
+
const activePreviousMembers = previousCovenantMembers.filter(
|
|
1554
|
+
(prevMember) => currentCovenantMembers.includes(prevMember)
|
|
1555
|
+
).length;
|
|
1556
|
+
if (activePreviousMembers < requiredQuorum) {
|
|
1557
|
+
throw new StakingError(
|
|
1558
|
+
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
1559
|
+
`Staking expansion failed: insufficient covenant quorum. Required: ${requiredQuorum}, Available: ${activePreviousMembers}. Too many covenant members have rotated out.`
|
|
1303
1560
|
);
|
|
1304
1561
|
}
|
|
1305
1562
|
};
|
|
1306
1563
|
|
|
1307
1564
|
// src/staking/index.ts
|
|
1308
|
-
var Staking = class {
|
|
1309
|
-
constructor(network, stakerInfo, params,
|
|
1565
|
+
var Staking = class _Staking {
|
|
1566
|
+
constructor(network, stakerInfo, params, finalityProviderPksNoCoordHex, stakingTimelock) {
|
|
1310
1567
|
if (!isValidBitcoinAddress(stakerInfo.address, network)) {
|
|
1311
1568
|
throw new StakingError(
|
|
1312
1569
|
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
@@ -1319,10 +1576,10 @@ var Staking = class {
|
|
|
1319
1576
|
"Invalid staker public key"
|
|
1320
1577
|
);
|
|
1321
1578
|
}
|
|
1322
|
-
if (!isValidNoCoordPublicKey
|
|
1579
|
+
if (finalityProviderPksNoCoordHex.length === 0 || !finalityProviderPksNoCoordHex.every(isValidNoCoordPublicKey)) {
|
|
1323
1580
|
throw new StakingError(
|
|
1324
1581
|
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
1325
|
-
"Invalid finality
|
|
1582
|
+
"Invalid finality providers public keys"
|
|
1326
1583
|
);
|
|
1327
1584
|
}
|
|
1328
1585
|
validateParams(params);
|
|
@@ -1330,14 +1587,14 @@ var Staking = class {
|
|
|
1330
1587
|
this.network = network;
|
|
1331
1588
|
this.stakerInfo = stakerInfo;
|
|
1332
1589
|
this.params = params;
|
|
1333
|
-
this.
|
|
1590
|
+
this.finalityProviderPksNoCoordHex = finalityProviderPksNoCoordHex;
|
|
1334
1591
|
this.stakingTimelock = stakingTimelock;
|
|
1335
1592
|
}
|
|
1336
1593
|
/**
|
|
1337
1594
|
* buildScripts builds the staking scripts for the staking transaction.
|
|
1338
1595
|
* Note: different staking types may have different scripts.
|
|
1339
1596
|
* e.g the observable staking script has a data embed script.
|
|
1340
|
-
*
|
|
1597
|
+
*
|
|
1341
1598
|
* @returns {StakingScripts} - The staking scripts.
|
|
1342
1599
|
*/
|
|
1343
1600
|
buildScripts() {
|
|
@@ -1346,7 +1603,7 @@ var Staking = class {
|
|
|
1346
1603
|
try {
|
|
1347
1604
|
stakingScriptData = new StakingScriptData(
|
|
1348
1605
|
Buffer.from(this.stakerInfo.publicKeyNoCoordHex, "hex"),
|
|
1349
|
-
|
|
1606
|
+
this.finalityProviderPksNoCoordHex.map((pk) => Buffer.from(pk, "hex")),
|
|
1350
1607
|
toBuffers(covenantNoCoordPks),
|
|
1351
1608
|
covenantQuorum,
|
|
1352
1609
|
this.stakingTimelock,
|
|
@@ -1373,9 +1630,9 @@ var Staking = class {
|
|
|
1373
1630
|
}
|
|
1374
1631
|
/**
|
|
1375
1632
|
* Create a staking transaction for staking.
|
|
1376
|
-
*
|
|
1633
|
+
*
|
|
1377
1634
|
* @param {number} stakingAmountSat - The amount to stake in satoshis.
|
|
1378
|
-
* @param {UTXO[]} inputUTXOs - The UTXOs to use as inputs for the staking
|
|
1635
|
+
* @param {UTXO[]} inputUTXOs - The UTXOs to use as inputs for the staking
|
|
1379
1636
|
* transaction.
|
|
1380
1637
|
* @param {number} feeRate - The fee rate for the transaction in satoshis per byte.
|
|
1381
1638
|
* @returns {TransactionResult} - An object containing the unsigned
|
|
@@ -1413,10 +1670,85 @@ var Staking = class {
|
|
|
1413
1670
|
}
|
|
1414
1671
|
}
|
|
1415
1672
|
/**
|
|
1416
|
-
*
|
|
1673
|
+
* Creates a staking expansion transaction that extends an existing BTC stake
|
|
1674
|
+
* to new finality providers or renews the timelock.
|
|
1675
|
+
*
|
|
1676
|
+
* This method implements RFC 037 BTC Stake Expansion,
|
|
1677
|
+
* allowing existing active BTC staking transactions
|
|
1678
|
+
* to extend their delegation to new finality providers without going through
|
|
1679
|
+
* the full unbonding process.
|
|
1417
1680
|
*
|
|
1681
|
+
* The expansion transaction:
|
|
1682
|
+
* 1. Spends the previous staking transaction output as the first input
|
|
1683
|
+
* 2. Uses funding UTXO as additional input to cover transaction fees or
|
|
1684
|
+
* to increase the staking amount
|
|
1685
|
+
* 3. Creates a new staking output with expanded finality provider coverage or
|
|
1686
|
+
* renews the timelock
|
|
1687
|
+
* 4. Has an output returning the remaining funds as change (if any) to the
|
|
1688
|
+
* staker BTC address
|
|
1689
|
+
*
|
|
1690
|
+
* @param {number} stakingAmountSat - The total staking amount in satoshis
|
|
1691
|
+
* (The amount had to be equal to the previous staking amount for now, this
|
|
1692
|
+
* lib does not yet support increasing the staking amount at this stage)
|
|
1693
|
+
* @param {UTXO[]} inputUTXOs - Available UTXOs to use for funding the
|
|
1694
|
+
* expansion transaction fees. Only one will be selected for the expansion
|
|
1695
|
+
* @param {number} feeRate - Fee rate in satoshis per byte for the
|
|
1696
|
+
* expansion transaction
|
|
1697
|
+
* @param {StakingParams} paramsForPreviousStakingTx - Staking parameters
|
|
1698
|
+
* used in the previous staking transaction
|
|
1699
|
+
* @param {Object} previousStakingTxInfo - Necessary information to spend the
|
|
1700
|
+
* previous staking transaction.
|
|
1701
|
+
* @returns {TransactionResult & { fundingUTXO: UTXO }} - An object containing
|
|
1702
|
+
* the unsigned expansion transaction and calculated fee, and the funding UTXO
|
|
1703
|
+
* @throws {StakingError} - If the transaction cannot be built or validation
|
|
1704
|
+
* fails
|
|
1705
|
+
*/
|
|
1706
|
+
createStakingExpansionTransaction(stakingAmountSat, inputUTXOs, feeRate, paramsForPreviousStakingTx, previousStakingTxInfo) {
|
|
1707
|
+
validateStakingTxInputData(
|
|
1708
|
+
stakingAmountSat,
|
|
1709
|
+
this.stakingTimelock,
|
|
1710
|
+
this.params,
|
|
1711
|
+
inputUTXOs,
|
|
1712
|
+
feeRate
|
|
1713
|
+
);
|
|
1714
|
+
validateStakingExpansionCovenantQuorum(
|
|
1715
|
+
paramsForPreviousStakingTx,
|
|
1716
|
+
this.params
|
|
1717
|
+
);
|
|
1718
|
+
const previousStaking = new _Staking(
|
|
1719
|
+
this.network,
|
|
1720
|
+
this.stakerInfo,
|
|
1721
|
+
paramsForPreviousStakingTx,
|
|
1722
|
+
previousStakingTxInfo.stakingInput.finalityProviderPksNoCoordHex,
|
|
1723
|
+
previousStakingTxInfo.stakingInput.stakingTimelock
|
|
1724
|
+
);
|
|
1725
|
+
const {
|
|
1726
|
+
transaction: stakingExpansionTx,
|
|
1727
|
+
fee: stakingExpansionTxFee,
|
|
1728
|
+
fundingUTXO
|
|
1729
|
+
} = stakingExpansionTransaction(
|
|
1730
|
+
this.network,
|
|
1731
|
+
this.buildScripts(),
|
|
1732
|
+
stakingAmountSat,
|
|
1733
|
+
this.stakerInfo.address,
|
|
1734
|
+
feeRate,
|
|
1735
|
+
inputUTXOs,
|
|
1736
|
+
{
|
|
1737
|
+
stakingTx: previousStakingTxInfo.stakingTx,
|
|
1738
|
+
scripts: previousStaking.buildScripts()
|
|
1739
|
+
}
|
|
1740
|
+
);
|
|
1741
|
+
return {
|
|
1742
|
+
transaction: stakingExpansionTx,
|
|
1743
|
+
fee: stakingExpansionTxFee,
|
|
1744
|
+
fundingUTXO
|
|
1745
|
+
};
|
|
1746
|
+
}
|
|
1747
|
+
/**
|
|
1748
|
+
* Create a staking psbt based on the existing staking transaction.
|
|
1749
|
+
*
|
|
1418
1750
|
* @param {Transaction} stakingTx - The staking transaction.
|
|
1419
|
-
* @param {UTXO[]} inputUTXOs - The UTXOs to use as inputs for the staking
|
|
1751
|
+
* @param {UTXO[]} inputUTXOs - The UTXOs to use as inputs for the staking
|
|
1420
1752
|
* transaction. The UTXOs that were used to create the staking transaction should
|
|
1421
1753
|
* be included in this array.
|
|
1422
1754
|
* @returns {Psbt} - The psbt.
|
|
@@ -1433,15 +1765,54 @@ var Staking = class {
|
|
|
1433
1765
|
stakingTx,
|
|
1434
1766
|
this.network,
|
|
1435
1767
|
inputUTXOs,
|
|
1436
|
-
isTaproot(
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1768
|
+
isTaproot(this.stakerInfo.address, this.network) ? Buffer.from(this.stakerInfo.publicKeyNoCoordHex, "hex") : void 0
|
|
1769
|
+
);
|
|
1770
|
+
}
|
|
1771
|
+
/**
|
|
1772
|
+
* Convert a staking expansion transaction to a PSBT.
|
|
1773
|
+
*
|
|
1774
|
+
* @param {Transaction} stakingExpansionTx - The staking expansion
|
|
1775
|
+
* transaction to convert
|
|
1776
|
+
* @param {UTXO[]} inputUTXOs - Available UTXOs for the
|
|
1777
|
+
* funding input (second input)
|
|
1778
|
+
* @param {StakingParams} paramsForPreviousStakingTx - Staking parameters
|
|
1779
|
+
* used for the previous staking transaction
|
|
1780
|
+
* @param {Object} previousStakingTxInfo - Information about the previous
|
|
1781
|
+
* staking transaction
|
|
1782
|
+
* @returns {Psbt} The PSBT for the staking expansion transaction
|
|
1783
|
+
* @throws {Error} If the previous staking output cannot be found or
|
|
1784
|
+
* validation fails
|
|
1785
|
+
*/
|
|
1786
|
+
toStakingExpansionPsbt(stakingExpansionTx, inputUTXOs, paramsForPreviousStakingTx, previousStakingTxInfo) {
|
|
1787
|
+
const previousStaking = new _Staking(
|
|
1788
|
+
this.network,
|
|
1789
|
+
this.stakerInfo,
|
|
1790
|
+
paramsForPreviousStakingTx,
|
|
1791
|
+
previousStakingTxInfo.stakingInput.finalityProviderPksNoCoordHex,
|
|
1792
|
+
previousStakingTxInfo.stakingInput.stakingTimelock
|
|
1793
|
+
);
|
|
1794
|
+
const previousScripts = previousStaking.buildScripts();
|
|
1795
|
+
const { outputAddress } = deriveStakingOutputInfo(previousScripts, this.network);
|
|
1796
|
+
const previousStakingOutputIndex = findMatchingTxOutputIndex(
|
|
1797
|
+
previousStakingTxInfo.stakingTx,
|
|
1798
|
+
outputAddress,
|
|
1799
|
+
this.network
|
|
1800
|
+
);
|
|
1801
|
+
return stakingExpansionPsbt(
|
|
1802
|
+
this.network,
|
|
1803
|
+
stakingExpansionTx,
|
|
1804
|
+
{
|
|
1805
|
+
stakingTx: previousStakingTxInfo.stakingTx,
|
|
1806
|
+
outputIndex: previousStakingOutputIndex
|
|
1807
|
+
},
|
|
1808
|
+
inputUTXOs,
|
|
1809
|
+
previousScripts,
|
|
1810
|
+
isTaproot(this.stakerInfo.address, this.network) ? Buffer.from(this.stakerInfo.publicKeyNoCoordHex, "hex") : void 0
|
|
1440
1811
|
);
|
|
1441
1812
|
}
|
|
1442
1813
|
/**
|
|
1443
1814
|
* Create an unbonding transaction for staking.
|
|
1444
|
-
*
|
|
1815
|
+
*
|
|
1445
1816
|
* @param {Transaction} stakingTx - The staking transaction to unbond.
|
|
1446
1817
|
* @returns {TransactionResult} - An object containing the unsigned
|
|
1447
1818
|
* transaction, and fee
|
|
@@ -1478,10 +1849,10 @@ var Staking = class {
|
|
|
1478
1849
|
/**
|
|
1479
1850
|
* Create an unbonding psbt based on the existing unbonding transaction and
|
|
1480
1851
|
* staking transaction.
|
|
1481
|
-
*
|
|
1852
|
+
*
|
|
1482
1853
|
* @param {Transaction} unbondingTx - The unbonding transaction.
|
|
1483
1854
|
* @param {Transaction} stakingTx - The staking transaction.
|
|
1484
|
-
*
|
|
1855
|
+
*
|
|
1485
1856
|
* @returns {Psbt} - The psbt.
|
|
1486
1857
|
*/
|
|
1487
1858
|
toUnbondingPsbt(unbondingTx, stakingTx) {
|
|
@@ -1496,7 +1867,7 @@ var Staking = class {
|
|
|
1496
1867
|
* Creates a withdrawal transaction that spends from an unbonding or slashing
|
|
1497
1868
|
* transaction. The timelock on the input transaction must have expired before
|
|
1498
1869
|
* this withdrawal can be valid.
|
|
1499
|
-
*
|
|
1870
|
+
*
|
|
1500
1871
|
* @param {Transaction} earlyUnbondedTx - The unbonding or slashing
|
|
1501
1872
|
* transaction to withdraw from
|
|
1502
1873
|
* @param {number} feeRate - Fee rate in satoshis per byte for the withdrawal
|
|
@@ -1524,9 +1895,9 @@ var Staking = class {
|
|
|
1524
1895
|
}
|
|
1525
1896
|
}
|
|
1526
1897
|
/**
|
|
1527
|
-
* Create a withdrawal psbt that spends a naturally expired staking
|
|
1898
|
+
* Create a withdrawal psbt that spends a naturally expired staking
|
|
1528
1899
|
* transaction.
|
|
1529
|
-
*
|
|
1900
|
+
*
|
|
1530
1901
|
* @param {Transaction} stakingTx - The staking transaction to withdraw from.
|
|
1531
1902
|
* @param {number} feeRate - The fee rate for the transaction in satoshis per byte.
|
|
1532
1903
|
* @returns {PsbtResult} - An object containing the unsigned psbt and fee
|
|
@@ -1559,7 +1930,7 @@ var Staking = class {
|
|
|
1559
1930
|
}
|
|
1560
1931
|
/**
|
|
1561
1932
|
* Create a slashing psbt spending from the staking output.
|
|
1562
|
-
*
|
|
1933
|
+
*
|
|
1563
1934
|
* @param {Transaction} stakingTx - The staking transaction to slash.
|
|
1564
1935
|
* @returns {PsbtResult} - An object containing the unsigned psbt and fee
|
|
1565
1936
|
* @throws {StakingError} - If the delegation is invalid or the transaction cannot be built
|
|
@@ -1572,6 +1943,12 @@ var Staking = class {
|
|
|
1572
1943
|
);
|
|
1573
1944
|
}
|
|
1574
1945
|
const scripts = this.buildScripts();
|
|
1946
|
+
const { outputAddress } = deriveStakingOutputInfo(scripts, this.network);
|
|
1947
|
+
const stakingOutputIndex = findMatchingTxOutputIndex(
|
|
1948
|
+
stakingTx,
|
|
1949
|
+
outputAddress,
|
|
1950
|
+
this.network
|
|
1951
|
+
);
|
|
1575
1952
|
try {
|
|
1576
1953
|
const { psbt } = slashTimelockUnbondedTransaction(
|
|
1577
1954
|
scripts,
|
|
@@ -1579,7 +1956,8 @@ var Staking = class {
|
|
|
1579
1956
|
this.params.slashing.slashingPkScriptHex,
|
|
1580
1957
|
this.params.slashing.slashingRate,
|
|
1581
1958
|
this.params.slashing.minSlashingTxFeeSat,
|
|
1582
|
-
this.network
|
|
1959
|
+
this.network,
|
|
1960
|
+
stakingOutputIndex
|
|
1583
1961
|
);
|
|
1584
1962
|
return {
|
|
1585
1963
|
psbt,
|
|
@@ -1595,7 +1973,7 @@ var Staking = class {
|
|
|
1595
1973
|
}
|
|
1596
1974
|
/**
|
|
1597
1975
|
* Create a slashing psbt for an unbonding output.
|
|
1598
|
-
*
|
|
1976
|
+
*
|
|
1599
1977
|
* @param {Transaction} unbondingTx - The unbonding transaction to slash.
|
|
1600
1978
|
* @returns {PsbtResult} - An object containing the unsigned psbt and fee
|
|
1601
1979
|
* @throws {StakingError} - If the delegation is invalid or the transaction cannot be built
|
|
@@ -1632,7 +2010,7 @@ var Staking = class {
|
|
|
1632
2010
|
/**
|
|
1633
2011
|
* Create a withdraw slashing psbt that spends a slashing transaction from the
|
|
1634
2012
|
* staking output.
|
|
1635
|
-
*
|
|
2013
|
+
*
|
|
1636
2014
|
* @param {Transaction} slashingTx - The slashing transaction.
|
|
1637
2015
|
* @param {number} feeRate - The fee rate for the transaction in satoshis per byte.
|
|
1638
2016
|
* @returns {PsbtResult} - An object containing the unsigned psbt and fee
|
|
@@ -1659,253 +2037,27 @@ var Staking = class {
|
|
|
1659
2037
|
throw StakingError.fromUnknown(
|
|
1660
2038
|
error,
|
|
1661
2039
|
"BUILD_TRANSACTION_FAILURE" /* BUILD_TRANSACTION_FAILURE */,
|
|
1662
|
-
"Cannot build withdraw slashing transaction"
|
|
1663
|
-
);
|
|
1664
|
-
}
|
|
1665
|
-
}
|
|
1666
|
-
};
|
|
1667
|
-
|
|
1668
|
-
// src/staking/observable/observableStakingScript.ts
|
|
1669
|
-
var import_bitcoinjs_lib9 = require("bitcoinjs-lib");
|
|
1670
|
-
var ObservableStakingScriptData = class extends StakingScriptData {
|
|
1671
|
-
constructor(stakerKey, finalityProviderKeys, covenantKeys, covenantThreshold, stakingTimelock, unbondingTimelock, magicBytes) {
|
|
1672
|
-
super(
|
|
1673
|
-
stakerKey,
|
|
1674
|
-
finalityProviderKeys,
|
|
1675
|
-
covenantKeys,
|
|
1676
|
-
covenantThreshold,
|
|
1677
|
-
stakingTimelock,
|
|
1678
|
-
unbondingTimelock
|
|
1679
|
-
);
|
|
1680
|
-
if (!magicBytes) {
|
|
1681
|
-
throw new Error("Missing required input values");
|
|
1682
|
-
}
|
|
1683
|
-
if (magicBytes.length != MAGIC_BYTES_LEN) {
|
|
1684
|
-
throw new Error("Invalid script data provided");
|
|
1685
|
-
}
|
|
1686
|
-
this.magicBytes = magicBytes;
|
|
1687
|
-
}
|
|
1688
|
-
/**
|
|
1689
|
-
* Builds a data embed script for staking in the form:
|
|
1690
|
-
* OP_RETURN || <serializedStakingData>
|
|
1691
|
-
* where serializedStakingData is the concatenation of:
|
|
1692
|
-
* MagicBytes || Version || StakerPublicKey || FinalityProviderPublicKey || StakingTimeLock
|
|
1693
|
-
* Note: Only a single finality provider key is supported for now in phase 1
|
|
1694
|
-
* @throws {Error} If the number of finality provider keys is not equal to 1.
|
|
1695
|
-
* @returns {Buffer} The compiled data embed script.
|
|
1696
|
-
*/
|
|
1697
|
-
buildDataEmbedScript() {
|
|
1698
|
-
if (this.finalityProviderKeys.length != 1) {
|
|
1699
|
-
throw new Error("Only a single finality provider key is supported");
|
|
1700
|
-
}
|
|
1701
|
-
const version = Buffer.alloc(1);
|
|
1702
|
-
version.writeUInt8(0);
|
|
1703
|
-
const stakingTimeLock = Buffer.alloc(2);
|
|
1704
|
-
stakingTimeLock.writeUInt16BE(this.stakingTimeLock);
|
|
1705
|
-
const serializedStakingData = Buffer.concat([
|
|
1706
|
-
this.magicBytes,
|
|
1707
|
-
version,
|
|
1708
|
-
this.stakerKey,
|
|
1709
|
-
this.finalityProviderKeys[0],
|
|
1710
|
-
stakingTimeLock
|
|
1711
|
-
]);
|
|
1712
|
-
return import_bitcoinjs_lib9.script.compile([import_bitcoinjs_lib9.opcodes.OP_RETURN, serializedStakingData]);
|
|
1713
|
-
}
|
|
1714
|
-
/**
|
|
1715
|
-
* Builds the staking scripts.
|
|
1716
|
-
* @returns {ObservableStakingScripts} The staking scripts that can be used to stake.
|
|
1717
|
-
* contains the timelockScript, unbondingScript, slashingScript,
|
|
1718
|
-
* unbondingTimelockScript, and dataEmbedScript.
|
|
1719
|
-
* @throws {Error} If script data is invalid.
|
|
1720
|
-
*/
|
|
1721
|
-
buildScripts() {
|
|
1722
|
-
const scripts = super.buildScripts();
|
|
1723
|
-
return {
|
|
1724
|
-
...scripts,
|
|
1725
|
-
dataEmbedScript: this.buildDataEmbedScript()
|
|
1726
|
-
};
|
|
1727
|
-
}
|
|
1728
|
-
};
|
|
1729
|
-
|
|
1730
|
-
// src/staking/observable/index.ts
|
|
1731
|
-
var ObservableStaking = class extends Staking {
|
|
1732
|
-
constructor(network, stakerInfo, params, finalityProviderPkNoCoordHex, stakingTimelock) {
|
|
1733
|
-
super(
|
|
1734
|
-
network,
|
|
1735
|
-
stakerInfo,
|
|
1736
|
-
params,
|
|
1737
|
-
finalityProviderPkNoCoordHex,
|
|
1738
|
-
stakingTimelock
|
|
1739
|
-
);
|
|
1740
|
-
if (!params.tag) {
|
|
1741
|
-
throw new StakingError(
|
|
1742
|
-
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
1743
|
-
"Observable staking parameters must include tag"
|
|
1744
|
-
);
|
|
1745
|
-
}
|
|
1746
|
-
if (!params.btcActivationHeight) {
|
|
1747
|
-
throw new StakingError(
|
|
1748
|
-
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
1749
|
-
"Observable staking parameters must include a positive activation height"
|
|
1750
|
-
);
|
|
1751
|
-
}
|
|
1752
|
-
this.params = params;
|
|
1753
|
-
}
|
|
1754
|
-
/**
|
|
1755
|
-
* Build the staking scripts for observable staking.
|
|
1756
|
-
* This method overwrites the base method to include the OP_RETURN tag based
|
|
1757
|
-
* on the tag provided in the parameters.
|
|
1758
|
-
*
|
|
1759
|
-
* @returns {ObservableStakingScripts} - The staking scripts for observable staking.
|
|
1760
|
-
* @throws {StakingError} - If the scripts cannot be built.
|
|
1761
|
-
*/
|
|
1762
|
-
buildScripts() {
|
|
1763
|
-
const { covenantQuorum, covenantNoCoordPks, unbondingTime, tag } = this.params;
|
|
1764
|
-
let stakingScriptData;
|
|
1765
|
-
try {
|
|
1766
|
-
stakingScriptData = new ObservableStakingScriptData(
|
|
1767
|
-
Buffer.from(this.stakerInfo.publicKeyNoCoordHex, "hex"),
|
|
1768
|
-
[Buffer.from(this.finalityProviderPkNoCoordHex, "hex")],
|
|
1769
|
-
toBuffers(covenantNoCoordPks),
|
|
1770
|
-
covenantQuorum,
|
|
1771
|
-
this.stakingTimelock,
|
|
1772
|
-
unbondingTime,
|
|
1773
|
-
Buffer.from(tag, "hex")
|
|
1774
|
-
);
|
|
1775
|
-
} catch (error) {
|
|
1776
|
-
throw StakingError.fromUnknown(
|
|
1777
|
-
error,
|
|
1778
|
-
"SCRIPT_FAILURE" /* SCRIPT_FAILURE */,
|
|
1779
|
-
"Cannot build staking script data"
|
|
1780
|
-
);
|
|
1781
|
-
}
|
|
1782
|
-
let scripts;
|
|
1783
|
-
try {
|
|
1784
|
-
scripts = stakingScriptData.buildScripts();
|
|
1785
|
-
} catch (error) {
|
|
1786
|
-
throw StakingError.fromUnknown(
|
|
1787
|
-
error,
|
|
1788
|
-
"SCRIPT_FAILURE" /* SCRIPT_FAILURE */,
|
|
1789
|
-
"Cannot build staking scripts"
|
|
1790
|
-
);
|
|
1791
|
-
}
|
|
1792
|
-
return scripts;
|
|
1793
|
-
}
|
|
1794
|
-
/**
|
|
1795
|
-
* Create a staking transaction for observable staking.
|
|
1796
|
-
* This overwrites the method from the Staking class with the addtion
|
|
1797
|
-
* of the
|
|
1798
|
-
* 1. OP_RETURN tag in the staking scripts
|
|
1799
|
-
* 2. lockHeight parameter
|
|
1800
|
-
*
|
|
1801
|
-
* @param {number} stakingAmountSat - The amount to stake in satoshis.
|
|
1802
|
-
* @param {UTXO[]} inputUTXOs - The UTXOs to use as inputs for the staking
|
|
1803
|
-
* transaction.
|
|
1804
|
-
* @param {number} feeRate - The fee rate for the transaction in satoshis per byte.
|
|
1805
|
-
* @returns {TransactionResult} - An object containing the unsigned transaction,
|
|
1806
|
-
* and fee
|
|
1807
|
-
*/
|
|
1808
|
-
createStakingTransaction(stakingAmountSat, inputUTXOs, feeRate) {
|
|
1809
|
-
validateStakingTxInputData(
|
|
1810
|
-
stakingAmountSat,
|
|
1811
|
-
this.stakingTimelock,
|
|
1812
|
-
this.params,
|
|
1813
|
-
inputUTXOs,
|
|
1814
|
-
feeRate
|
|
1815
|
-
);
|
|
1816
|
-
const scripts = this.buildScripts();
|
|
1817
|
-
try {
|
|
1818
|
-
const { transaction, fee } = stakingTransaction(
|
|
1819
|
-
scripts,
|
|
1820
|
-
stakingAmountSat,
|
|
1821
|
-
this.stakerInfo.address,
|
|
1822
|
-
inputUTXOs,
|
|
1823
|
-
this.network,
|
|
1824
|
-
feeRate,
|
|
1825
|
-
// `lockHeight` is exclusive of the provided value.
|
|
1826
|
-
// For example, if a Bitcoin height of X is provided,
|
|
1827
|
-
// the transaction will be included starting from height X+1.
|
|
1828
|
-
// https://learnmeabitcoin.com/technical/transaction/locktime/
|
|
1829
|
-
this.params.btcActivationHeight - 1
|
|
1830
|
-
);
|
|
1831
|
-
return {
|
|
1832
|
-
transaction,
|
|
1833
|
-
fee
|
|
1834
|
-
};
|
|
1835
|
-
} catch (error) {
|
|
1836
|
-
throw StakingError.fromUnknown(
|
|
1837
|
-
error,
|
|
1838
|
-
"BUILD_TRANSACTION_FAILURE" /* BUILD_TRANSACTION_FAILURE */,
|
|
1839
|
-
"Cannot build unsigned staking transaction"
|
|
2040
|
+
"Cannot build withdraw slashing transaction"
|
|
1840
2041
|
);
|
|
1841
2042
|
}
|
|
1842
2043
|
}
|
|
1843
|
-
/**
|
|
1844
|
-
* Create a staking psbt for observable staking.
|
|
1845
|
-
*
|
|
1846
|
-
* @param {Transaction} stakingTx - The staking transaction.
|
|
1847
|
-
* @param {UTXO[]} inputUTXOs - The UTXOs to use as inputs for the staking
|
|
1848
|
-
* transaction.
|
|
1849
|
-
* @returns {Psbt} - The psbt.
|
|
1850
|
-
*/
|
|
1851
|
-
toStakingPsbt(stakingTx, inputUTXOs) {
|
|
1852
|
-
return stakingPsbt(
|
|
1853
|
-
stakingTx,
|
|
1854
|
-
this.network,
|
|
1855
|
-
inputUTXOs,
|
|
1856
|
-
isTaproot(
|
|
1857
|
-
this.stakerInfo.address,
|
|
1858
|
-
this.network
|
|
1859
|
-
) ? Buffer.from(this.stakerInfo.publicKeyNoCoordHex, "hex") : void 0
|
|
1860
|
-
);
|
|
1861
|
-
}
|
|
1862
|
-
};
|
|
1863
|
-
|
|
1864
|
-
// src/utils/babylon.ts
|
|
1865
|
-
var import_encoding = require("@cosmjs/encoding");
|
|
1866
|
-
var isValidBabylonAddress = (address4) => {
|
|
1867
|
-
try {
|
|
1868
|
-
const { prefix } = (0, import_encoding.fromBech32)(address4);
|
|
1869
|
-
return prefix === "bbn";
|
|
1870
|
-
} catch (error) {
|
|
1871
|
-
return false;
|
|
1872
|
-
}
|
|
1873
|
-
};
|
|
1874
|
-
|
|
1875
|
-
// src/utils/staking/param.ts
|
|
1876
|
-
var getBabylonParamByBtcHeight = (height, babylonParamsVersions) => {
|
|
1877
|
-
const sortedParams = [...babylonParamsVersions].sort(
|
|
1878
|
-
(a, b) => b.btcActivationHeight - a.btcActivationHeight
|
|
1879
|
-
);
|
|
1880
|
-
const params = sortedParams.find(
|
|
1881
|
-
(p) => height >= p.btcActivationHeight
|
|
1882
|
-
);
|
|
1883
|
-
if (!params)
|
|
1884
|
-
throw new Error(`Babylon params not found for height ${height}`);
|
|
1885
|
-
return params;
|
|
1886
|
-
};
|
|
1887
|
-
var getBabylonParamByVersion = (version, babylonParams) => {
|
|
1888
|
-
const params = babylonParams.find((p) => p.version === version);
|
|
1889
|
-
if (!params)
|
|
1890
|
-
throw new Error(`Babylon params not found for version ${version}`);
|
|
1891
|
-
return params;
|
|
1892
2044
|
};
|
|
1893
2045
|
|
|
1894
2046
|
// src/staking/manager.ts
|
|
1895
|
-
var import_bitcoinjs_lib10 = require("bitcoinjs-lib");
|
|
1896
2047
|
var import_babylon_proto_ts = require("@babylonlabs-io/babylon-proto-ts");
|
|
1897
2048
|
var import_pop = require("@babylonlabs-io/babylon-proto-ts/dist/generated/babylon/btcstaking/v1/pop");
|
|
2049
|
+
var import_bitcoinjs_lib9 = require("bitcoinjs-lib");
|
|
1898
2050
|
|
|
1899
2051
|
// src/constants/registry.ts
|
|
1900
2052
|
var BABYLON_REGISTRY_TYPE_URLS = {
|
|
1901
|
-
MsgCreateBTCDelegation: "/babylon.btcstaking.v1.MsgCreateBTCDelegation"
|
|
2053
|
+
MsgCreateBTCDelegation: "/babylon.btcstaking.v1.MsgCreateBTCDelegation",
|
|
2054
|
+
MsgBtcStakeExpand: "/babylon.btcstaking.v1.MsgBtcStakeExpand"
|
|
1902
2055
|
};
|
|
1903
2056
|
|
|
1904
2057
|
// src/utils/index.ts
|
|
1905
2058
|
var reverseBuffer = (buffer) => {
|
|
1906
2059
|
const clonedBuffer = new Uint8Array(buffer);
|
|
1907
|
-
if (clonedBuffer.length < 1)
|
|
1908
|
-
return clonedBuffer;
|
|
2060
|
+
if (clonedBuffer.length < 1) return clonedBuffer;
|
|
1909
2061
|
for (let i = 0, j = clonedBuffer.length - 1; i < clonedBuffer.length / 2; i++, j--) {
|
|
1910
2062
|
let tmp = clonedBuffer[i];
|
|
1911
2063
|
clonedBuffer[i] = clonedBuffer[j];
|
|
@@ -1914,28 +2066,56 @@ var reverseBuffer = (buffer) => {
|
|
|
1914
2066
|
return clonedBuffer;
|
|
1915
2067
|
};
|
|
1916
2068
|
|
|
2069
|
+
// src/utils/pop.ts
|
|
2070
|
+
var import_crypto = require("bitcoinjs-lib/src/crypto");
|
|
2071
|
+
|
|
2072
|
+
// src/constants/staking.ts
|
|
2073
|
+
var STAKING_MODULE_ADDRESS = "bbn13837feaxn8t0zvwcjwhw7lhpgdcx4s36eqteah";
|
|
2074
|
+
|
|
2075
|
+
// src/utils/pop.ts
|
|
2076
|
+
function createStakerPopContext(chainId, popContextVersion = 0) {
|
|
2077
|
+
const contextString = `btcstaking/${popContextVersion}/staker_pop/${chainId}/${STAKING_MODULE_ADDRESS}`;
|
|
2078
|
+
return (0, import_crypto.sha256)(Buffer.from(contextString, "utf8")).toString("hex");
|
|
2079
|
+
}
|
|
2080
|
+
function buildPopMessage(bech32Address, currentHeight, chainId, upgradeConfig) {
|
|
2081
|
+
if (chainId !== void 0 && upgradeConfig?.upgradeHeight !== void 0 && upgradeConfig.version !== void 0 && currentHeight !== void 0 && currentHeight >= upgradeConfig.upgradeHeight) {
|
|
2082
|
+
const contextHash = createStakerPopContext(chainId, upgradeConfig.version);
|
|
2083
|
+
return contextHash + bech32Address;
|
|
2084
|
+
}
|
|
2085
|
+
return bech32Address;
|
|
2086
|
+
}
|
|
2087
|
+
|
|
2088
|
+
// src/utils/staking/param.ts
|
|
2089
|
+
var getBabylonParamByBtcHeight = (height, babylonParamsVersions) => {
|
|
2090
|
+
const sortedParams = [...babylonParamsVersions].sort(
|
|
2091
|
+
(a, b) => b.btcActivationHeight - a.btcActivationHeight
|
|
2092
|
+
);
|
|
2093
|
+
const params = sortedParams.find(
|
|
2094
|
+
(p) => height >= p.btcActivationHeight
|
|
2095
|
+
);
|
|
2096
|
+
if (!params) throw new Error(`Babylon params not found for height ${height}`);
|
|
2097
|
+
return params;
|
|
2098
|
+
};
|
|
2099
|
+
var getBabylonParamByVersion = (version, babylonParams) => {
|
|
2100
|
+
const params = babylonParams.find((p) => p.version === version);
|
|
2101
|
+
if (!params) throw new Error(`Babylon params not found for version ${version}`);
|
|
2102
|
+
return params;
|
|
2103
|
+
};
|
|
2104
|
+
|
|
1917
2105
|
// src/staking/manager.ts
|
|
1918
|
-
var SigningStep = /* @__PURE__ */ ((SigningStep2) => {
|
|
1919
|
-
SigningStep2["STAKING_SLASHING"] = "staking-slashing";
|
|
1920
|
-
SigningStep2["UNBONDING_SLASHING"] = "unbonding-slashing";
|
|
1921
|
-
SigningStep2["PROOF_OF_POSSESSION"] = "proof-of-possession";
|
|
1922
|
-
SigningStep2["CREATE_BTC_DELEGATION_MSG"] = "create-btc-delegation-msg";
|
|
1923
|
-
SigningStep2["STAKING"] = "staking";
|
|
1924
|
-
SigningStep2["UNBONDING"] = "unbonding";
|
|
1925
|
-
SigningStep2["WITHDRAW_STAKING_EXPIRED"] = "withdraw-staking-expired";
|
|
1926
|
-
SigningStep2["WITHDRAW_EARLY_UNBONDED"] = "withdraw-early-unbonded";
|
|
1927
|
-
SigningStep2["WITHDRAW_SLASHING"] = "withdraw-slashing";
|
|
1928
|
-
return SigningStep2;
|
|
1929
|
-
})(SigningStep || {});
|
|
1930
2106
|
var BabylonBtcStakingManager = class {
|
|
1931
|
-
constructor(network, stakingParams, btcProvider, babylonProvider) {
|
|
2107
|
+
constructor(network, stakingParams, btcProvider, babylonProvider, ee, upgradeConfig) {
|
|
1932
2108
|
this.network = network;
|
|
2109
|
+
this.stakingParams = stakingParams;
|
|
1933
2110
|
this.btcProvider = btcProvider;
|
|
1934
2111
|
this.babylonProvider = babylonProvider;
|
|
2112
|
+
this.ee = ee;
|
|
2113
|
+
this.network = network;
|
|
1935
2114
|
if (stakingParams.length === 0) {
|
|
1936
2115
|
throw new Error("No staking parameters provided");
|
|
1937
2116
|
}
|
|
1938
2117
|
this.stakingParams = stakingParams;
|
|
2118
|
+
this.upgradeConfig = upgradeConfig;
|
|
1939
2119
|
}
|
|
1940
2120
|
/**
|
|
1941
2121
|
* Creates a signed Pre-Staking Registration transaction that is ready to be
|
|
@@ -1950,7 +2130,7 @@ var BabylonBtcStakingManager = class {
|
|
|
1950
2130
|
* fee rate is above 1. If the fee rate is too low, the transaction will not
|
|
1951
2131
|
* be included in a block.
|
|
1952
2132
|
* @param babylonAddress - The Babylon bech32 encoded address of the staker.
|
|
1953
|
-
* @returns The signed babylon pre-staking registration transaction in base64
|
|
2133
|
+
* @returns The signed babylon pre-staking registration transaction in base64
|
|
1954
2134
|
* format.
|
|
1955
2135
|
*/
|
|
1956
2136
|
async preStakeRegistrationBabylonTransaction(stakerBtcInfo, stakingInput, babylonBtcTipHeight, inputUTXOs, feeRate, babylonAddress) {
|
|
@@ -1963,23 +2143,17 @@ var BabylonBtcStakingManager = class {
|
|
|
1963
2143
|
if (!isValidBabylonAddress(babylonAddress)) {
|
|
1964
2144
|
throw new Error("Invalid Babylon address");
|
|
1965
2145
|
}
|
|
1966
|
-
const params = getBabylonParamByBtcHeight(
|
|
1967
|
-
babylonBtcTipHeight,
|
|
1968
|
-
this.stakingParams
|
|
1969
|
-
);
|
|
2146
|
+
const params = getBabylonParamByBtcHeight(babylonBtcTipHeight, this.stakingParams);
|
|
1970
2147
|
const staking = new Staking(
|
|
1971
2148
|
this.network,
|
|
1972
2149
|
stakerBtcInfo,
|
|
1973
2150
|
params,
|
|
1974
|
-
stakingInput.
|
|
2151
|
+
stakingInput.finalityProviderPksNoCoordHex,
|
|
1975
2152
|
stakingInput.stakingTimelock
|
|
1976
2153
|
);
|
|
1977
|
-
const { transaction } = staking.createStakingTransaction(
|
|
1978
|
-
stakingInput.stakingAmountSat,
|
|
1979
|
-
inputUTXOs,
|
|
1980
|
-
feeRate
|
|
1981
|
-
);
|
|
2154
|
+
const { transaction } = staking.createStakingTransaction(stakingInput.stakingAmountSat, inputUTXOs, feeRate);
|
|
1982
2155
|
const msg = await this.createBtcDelegationMsg(
|
|
2156
|
+
"delegation:create",
|
|
1983
2157
|
staking,
|
|
1984
2158
|
stakingInput,
|
|
1985
2159
|
transaction,
|
|
@@ -1987,23 +2161,127 @@ var BabylonBtcStakingManager = class {
|
|
|
1987
2161
|
stakerBtcInfo,
|
|
1988
2162
|
params
|
|
1989
2163
|
);
|
|
2164
|
+
this.ee?.emit("delegation:create", {
|
|
2165
|
+
type: "create-btc-delegation-msg"
|
|
2166
|
+
});
|
|
1990
2167
|
return {
|
|
1991
|
-
signedBabylonTx: await this.babylonProvider.signTransaction(
|
|
1992
|
-
"create-btc-delegation-msg" /* CREATE_BTC_DELEGATION_MSG */,
|
|
1993
|
-
msg
|
|
1994
|
-
),
|
|
2168
|
+
signedBabylonTx: await this.babylonProvider.signTransaction(msg),
|
|
1995
2169
|
stakingTx: transaction
|
|
1996
2170
|
};
|
|
1997
2171
|
}
|
|
1998
2172
|
/**
|
|
1999
|
-
*
|
|
2000
|
-
*
|
|
2001
|
-
|
|
2173
|
+
* Create a signed staking expansion transaction that is ready to be sent to
|
|
2174
|
+
* the Babylon chain.
|
|
2175
|
+
*/
|
|
2176
|
+
async stakingExpansionRegistrationBabylonTransaction(stakerBtcInfo, stakingInput, babylonBtcTipHeight, inputUTXOs, feeRate, babylonAddress, previousStakingTxInfo) {
|
|
2177
|
+
validateStakingExpansionInputs({
|
|
2178
|
+
babylonBtcTipHeight,
|
|
2179
|
+
inputUTXOs,
|
|
2180
|
+
stakingInput,
|
|
2181
|
+
previousStakingInput: previousStakingTxInfo.stakingInput,
|
|
2182
|
+
babylonAddress
|
|
2183
|
+
});
|
|
2184
|
+
const params = getBabylonParamByBtcHeight(babylonBtcTipHeight, this.stakingParams);
|
|
2185
|
+
const paramsForPreviousStakingTx = getBabylonParamByVersion(previousStakingTxInfo.paramVersion, this.stakingParams);
|
|
2186
|
+
const stakingInstance = new Staking(
|
|
2187
|
+
this.network,
|
|
2188
|
+
stakerBtcInfo,
|
|
2189
|
+
params,
|
|
2190
|
+
stakingInput.finalityProviderPksNoCoordHex,
|
|
2191
|
+
stakingInput.stakingTimelock
|
|
2192
|
+
);
|
|
2193
|
+
const { transaction: stakingExpansionTx, fundingUTXO } = stakingInstance.createStakingExpansionTransaction(
|
|
2194
|
+
stakingInput.stakingAmountSat,
|
|
2195
|
+
inputUTXOs,
|
|
2196
|
+
feeRate,
|
|
2197
|
+
paramsForPreviousStakingTx,
|
|
2198
|
+
previousStakingTxInfo
|
|
2199
|
+
);
|
|
2200
|
+
let fundingTx;
|
|
2201
|
+
try {
|
|
2202
|
+
fundingTx = await this.btcProvider.getTransactionHex(fundingUTXO.txid);
|
|
2203
|
+
} catch (error) {
|
|
2204
|
+
throw StakingError.fromUnknown(
|
|
2205
|
+
error,
|
|
2206
|
+
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
2207
|
+
"Failed to retrieve funding transaction hex"
|
|
2208
|
+
);
|
|
2209
|
+
}
|
|
2210
|
+
const msg = await this.createBtcDelegationMsg(
|
|
2211
|
+
"delegation:expand",
|
|
2212
|
+
stakingInstance,
|
|
2213
|
+
stakingInput,
|
|
2214
|
+
stakingExpansionTx,
|
|
2215
|
+
babylonAddress,
|
|
2216
|
+
stakerBtcInfo,
|
|
2217
|
+
params,
|
|
2218
|
+
{
|
|
2219
|
+
delegationExpansionInfo: {
|
|
2220
|
+
previousStakingTx: previousStakingTxInfo.stakingTx,
|
|
2221
|
+
fundingTx: import_bitcoinjs_lib9.Transaction.fromHex(fundingTx)
|
|
2222
|
+
}
|
|
2223
|
+
}
|
|
2224
|
+
);
|
|
2225
|
+
this.ee?.emit("delegation:expand", {
|
|
2226
|
+
type: "create-btc-delegation-msg"
|
|
2227
|
+
});
|
|
2228
|
+
return {
|
|
2229
|
+
signedBabylonTx: await this.babylonProvider.signTransaction(msg),
|
|
2230
|
+
stakingTx: stakingExpansionTx
|
|
2231
|
+
};
|
|
2232
|
+
}
|
|
2233
|
+
/**
|
|
2234
|
+
* Estimates the transaction fee for a BTC staking expansion transaction.
|
|
2235
|
+
*
|
|
2236
|
+
* @param {StakerInfo} stakerBtcInfo - The staker's Bitcoin information
|
|
2237
|
+
* including address and public key
|
|
2238
|
+
* @param {number} babylonBtcTipHeight - The current Babylon BTC tip height
|
|
2239
|
+
* used to determine staking parameters
|
|
2240
|
+
* @param {StakingInputs} stakingInput - The new staking input parameters for
|
|
2241
|
+
* the expansion
|
|
2242
|
+
* @param {UTXO[]} inputUTXOs - Available UTXOs that can be used for funding
|
|
2243
|
+
* the expansion transaction
|
|
2244
|
+
* @param {number} feeRate - Fee rate in satoshis per byte for the expansion
|
|
2245
|
+
* transaction
|
|
2246
|
+
* @param {Object} previousStakingTxInfo - Information about the previous
|
|
2247
|
+
* staking transaction being expanded
|
|
2248
|
+
* @returns {number} - The estimated transaction fee in satoshis
|
|
2249
|
+
* @throws {Error} - If validation fails or the fee cannot be calculated
|
|
2250
|
+
*/
|
|
2251
|
+
estimateBtcStakingExpansionFee(stakerBtcInfo, babylonBtcTipHeight, stakingInput, inputUTXOs, feeRate, previousStakingTxInfo) {
|
|
2252
|
+
validateStakingExpansionInputs({
|
|
2253
|
+
babylonBtcTipHeight,
|
|
2254
|
+
inputUTXOs,
|
|
2255
|
+
stakingInput,
|
|
2256
|
+
previousStakingInput: previousStakingTxInfo.stakingInput
|
|
2257
|
+
});
|
|
2258
|
+
const params = getBabylonParamByBtcHeight(babylonBtcTipHeight, this.stakingParams);
|
|
2259
|
+
const paramsForPreviousStakingTx = getBabylonParamByVersion(previousStakingTxInfo.paramVersion, this.stakingParams);
|
|
2260
|
+
const stakingInstance = new Staking(
|
|
2261
|
+
this.network,
|
|
2262
|
+
stakerBtcInfo,
|
|
2263
|
+
params,
|
|
2264
|
+
stakingInput.finalityProviderPksNoCoordHex,
|
|
2265
|
+
stakingInput.stakingTimelock
|
|
2266
|
+
);
|
|
2267
|
+
const { fee } = stakingInstance.createStakingExpansionTransaction(
|
|
2268
|
+
stakingInput.stakingAmountSat,
|
|
2269
|
+
inputUTXOs,
|
|
2270
|
+
feeRate,
|
|
2271
|
+
paramsForPreviousStakingTx,
|
|
2272
|
+
previousStakingTxInfo
|
|
2273
|
+
);
|
|
2274
|
+
return fee;
|
|
2275
|
+
}
|
|
2276
|
+
/**
|
|
2277
|
+
* Creates a signed post-staking registration transaction that is ready to be
|
|
2278
|
+
* sent to the Babylon chain. This is used when a staking transaction is
|
|
2279
|
+
* already created and included in a BTC block and we want to register it on
|
|
2002
2280
|
* the Babylon chain.
|
|
2003
2281
|
* @param stakerBtcInfo - The staker BTC info which includes the BTC address
|
|
2004
2282
|
* and the no-coord public key in hex format.
|
|
2005
2283
|
* @param stakingTx - The staking transaction.
|
|
2006
|
-
* @param stakingTxHeight - The BTC height in which the staking transaction
|
|
2284
|
+
* @param stakingTxHeight - The BTC height in which the staking transaction
|
|
2007
2285
|
* is included.
|
|
2008
2286
|
* @param stakingInput - The staking inputs.
|
|
2009
2287
|
* @param inclusionProof - Merkle Proof of Inclusion: Verifies transaction
|
|
@@ -2020,30 +2298,29 @@ var BabylonBtcStakingManager = class {
|
|
|
2020
2298
|
this.network,
|
|
2021
2299
|
stakerBtcInfo,
|
|
2022
2300
|
params,
|
|
2023
|
-
stakingInput.
|
|
2301
|
+
stakingInput.finalityProviderPksNoCoordHex,
|
|
2024
2302
|
stakingInput.stakingTimelock
|
|
2025
2303
|
);
|
|
2026
2304
|
const scripts = stakingInstance.buildScripts();
|
|
2027
2305
|
const stakingOutputInfo = deriveStakingOutputInfo(scripts, this.network);
|
|
2028
|
-
findMatchingTxOutputIndex(
|
|
2029
|
-
stakingTx,
|
|
2030
|
-
stakingOutputInfo.outputAddress,
|
|
2031
|
-
this.network
|
|
2032
|
-
);
|
|
2306
|
+
findMatchingTxOutputIndex(stakingTx, stakingOutputInfo.outputAddress, this.network);
|
|
2033
2307
|
const delegationMsg = await this.createBtcDelegationMsg(
|
|
2308
|
+
"delegation:register",
|
|
2034
2309
|
stakingInstance,
|
|
2035
2310
|
stakingInput,
|
|
2036
2311
|
stakingTx,
|
|
2037
2312
|
babylonAddress,
|
|
2038
2313
|
stakerBtcInfo,
|
|
2039
2314
|
params,
|
|
2040
|
-
|
|
2315
|
+
{
|
|
2316
|
+
inclusionProof: this.getInclusionProof(inclusionProof)
|
|
2317
|
+
}
|
|
2041
2318
|
);
|
|
2319
|
+
this.ee?.emit("delegation:register", {
|
|
2320
|
+
type: "create-btc-delegation-msg"
|
|
2321
|
+
});
|
|
2042
2322
|
return {
|
|
2043
|
-
signedBabylonTx: await this.babylonProvider.signTransaction(
|
|
2044
|
-
"create-btc-delegation-msg" /* CREATE_BTC_DELEGATION_MSG */,
|
|
2045
|
-
delegationMsg
|
|
2046
|
-
)
|
|
2323
|
+
signedBabylonTx: await this.babylonProvider.signTransaction(delegationMsg)
|
|
2047
2324
|
};
|
|
2048
2325
|
}
|
|
2049
2326
|
/**
|
|
@@ -2064,26 +2341,19 @@ var BabylonBtcStakingManager = class {
|
|
|
2064
2341
|
if (babylonBtcTipHeight === 0) {
|
|
2065
2342
|
throw new Error("Babylon BTC tip height cannot be 0");
|
|
2066
2343
|
}
|
|
2067
|
-
const params = getBabylonParamByBtcHeight(
|
|
2068
|
-
babylonBtcTipHeight,
|
|
2069
|
-
this.stakingParams
|
|
2070
|
-
);
|
|
2344
|
+
const params = getBabylonParamByBtcHeight(babylonBtcTipHeight, this.stakingParams);
|
|
2071
2345
|
const staking = new Staking(
|
|
2072
2346
|
this.network,
|
|
2073
2347
|
stakerBtcInfo,
|
|
2074
2348
|
params,
|
|
2075
|
-
stakingInput.
|
|
2349
|
+
stakingInput.finalityProviderPksNoCoordHex,
|
|
2076
2350
|
stakingInput.stakingTimelock
|
|
2077
2351
|
);
|
|
2078
|
-
const { fee: stakingFee } = staking.createStakingTransaction(
|
|
2079
|
-
stakingInput.stakingAmountSat,
|
|
2080
|
-
inputUTXOs,
|
|
2081
|
-
feeRate
|
|
2082
|
-
);
|
|
2352
|
+
const { fee: stakingFee } = staking.createStakingTransaction(stakingInput.stakingAmountSat, inputUTXOs, feeRate);
|
|
2083
2353
|
return stakingFee;
|
|
2084
2354
|
}
|
|
2085
2355
|
/**
|
|
2086
|
-
* Creates a signed staking transaction that is ready to be sent to the BTC
|
|
2356
|
+
* Creates a signed staking transaction that is ready to be sent to the BTC
|
|
2087
2357
|
* network.
|
|
2088
2358
|
* @param stakerBtcInfo - The staker BTC info which includes the BTC address
|
|
2089
2359
|
* and the no-coord public key in hex format.
|
|
@@ -2104,18 +2374,129 @@ var BabylonBtcStakingManager = class {
|
|
|
2104
2374
|
this.network,
|
|
2105
2375
|
stakerBtcInfo,
|
|
2106
2376
|
params,
|
|
2107
|
-
stakingInput.
|
|
2377
|
+
stakingInput.finalityProviderPksNoCoordHex,
|
|
2108
2378
|
stakingInput.stakingTimelock
|
|
2109
2379
|
);
|
|
2110
|
-
const stakingPsbt2 = staking.toStakingPsbt(
|
|
2111
|
-
|
|
2112
|
-
|
|
2380
|
+
const stakingPsbt2 = staking.toStakingPsbt(unsignedStakingTx, inputUTXOs);
|
|
2381
|
+
const contracts = [
|
|
2382
|
+
{
|
|
2383
|
+
id: "babylon:staking" /* STAKING */,
|
|
2384
|
+
params: {
|
|
2385
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2386
|
+
finalityProviders: stakingInput.finalityProviderPksNoCoordHex,
|
|
2387
|
+
covenantPks: params.covenantNoCoordPks,
|
|
2388
|
+
covenantThreshold: params.covenantQuorum,
|
|
2389
|
+
minUnbondingTime: params.unbondingTime,
|
|
2390
|
+
stakingDuration: stakingInput.stakingTimelock
|
|
2391
|
+
}
|
|
2392
|
+
}
|
|
2393
|
+
];
|
|
2394
|
+
this.ee?.emit("delegation:stake", {
|
|
2395
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2396
|
+
finalityProviders: stakingInput.finalityProviderPksNoCoordHex,
|
|
2397
|
+
covenantPks: params.covenantNoCoordPks,
|
|
2398
|
+
covenantThreshold: params.covenantQuorum,
|
|
2399
|
+
unbondingTimeBlocks: params.unbondingTime,
|
|
2400
|
+
stakingDuration: stakingInput.stakingTimelock,
|
|
2401
|
+
type: "staking"
|
|
2402
|
+
});
|
|
2403
|
+
const signedStakingPsbtHex = await this.btcProvider.signPsbt(stakingPsbt2.toHex(), {
|
|
2404
|
+
contracts,
|
|
2405
|
+
action: {
|
|
2406
|
+
name: "sign-btc-staking-transaction" /* SIGN_BTC_STAKING_TRANSACTION */
|
|
2407
|
+
}
|
|
2408
|
+
});
|
|
2409
|
+
return import_bitcoinjs_lib9.Psbt.fromHex(signedStakingPsbtHex).extractTransaction();
|
|
2410
|
+
}
|
|
2411
|
+
/**
|
|
2412
|
+
* Creates a signed staking expansion transaction that is ready to be sent to
|
|
2413
|
+
* the BTC network.
|
|
2414
|
+
*
|
|
2415
|
+
* @param {StakerInfo} stakerBtcInfo - The staker's BTC information including
|
|
2416
|
+
* address and public key
|
|
2417
|
+
* @param {StakingInputs} stakingInput - The staking inputs for the expansion
|
|
2418
|
+
* @param {Transaction} unsignedStakingExpansionTx - The unsigned staking
|
|
2419
|
+
* expansion transaction
|
|
2420
|
+
* @param {UTXO[]} inputUTXOs - Available UTXOs for the funding input
|
|
2421
|
+
* @param {number} stakingParamsVersion - The version of staking parameters
|
|
2422
|
+
* that was used when registering the staking expansion delegation.
|
|
2423
|
+
* @param {Object} previousStakingTxInfo - Information about the previous
|
|
2424
|
+
* staking transaction
|
|
2425
|
+
* @param {Array} covenantStakingExpansionSignatures - Covenant committee
|
|
2426
|
+
* signatures for the expansion
|
|
2427
|
+
* @returns {Promise<Transaction>} The fully signed staking expansion
|
|
2428
|
+
* transaction
|
|
2429
|
+
* @throws {Error} If signing fails, validation fails, or required data is
|
|
2430
|
+
* missing
|
|
2431
|
+
*/
|
|
2432
|
+
async createSignedBtcStakingExpansionTransaction(stakerBtcInfo, stakingInput, unsignedStakingExpansionTx, inputUTXOs, stakingParamsVersion, previousStakingTxInfo, covenantStakingExpansionSignatures) {
|
|
2433
|
+
validateStakingExpansionInputs({
|
|
2434
|
+
inputUTXOs,
|
|
2435
|
+
stakingInput,
|
|
2436
|
+
previousStakingInput: previousStakingTxInfo.stakingInput
|
|
2437
|
+
});
|
|
2438
|
+
const params = getBabylonParamByVersion(stakingParamsVersion, this.stakingParams);
|
|
2439
|
+
if (inputUTXOs.length === 0) {
|
|
2440
|
+
throw new Error("No input UTXOs provided");
|
|
2441
|
+
}
|
|
2442
|
+
const staking = new Staking(
|
|
2443
|
+
this.network,
|
|
2444
|
+
stakerBtcInfo,
|
|
2445
|
+
params,
|
|
2446
|
+
stakingInput.finalityProviderPksNoCoordHex,
|
|
2447
|
+
stakingInput.stakingTimelock
|
|
2113
2448
|
);
|
|
2114
|
-
const
|
|
2115
|
-
|
|
2116
|
-
|
|
2449
|
+
const previousParams = getBabylonParamByVersion(previousStakingTxInfo.paramVersion, this.stakingParams);
|
|
2450
|
+
const stakingExpansionPsbt2 = staking.toStakingExpansionPsbt(
|
|
2451
|
+
unsignedStakingExpansionTx,
|
|
2452
|
+
inputUTXOs,
|
|
2453
|
+
previousParams,
|
|
2454
|
+
previousStakingTxInfo
|
|
2455
|
+
);
|
|
2456
|
+
const contracts = [
|
|
2457
|
+
{
|
|
2458
|
+
id: "babylon:staking" /* STAKING */,
|
|
2459
|
+
params: {
|
|
2460
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2461
|
+
finalityProviders: stakingInput.finalityProviderPksNoCoordHex,
|
|
2462
|
+
covenantPks: params.covenantNoCoordPks,
|
|
2463
|
+
covenantThreshold: params.covenantQuorum,
|
|
2464
|
+
minUnbondingTime: params.unbondingTime,
|
|
2465
|
+
stakingDuration: stakingInput.stakingTimelock
|
|
2466
|
+
}
|
|
2467
|
+
}
|
|
2468
|
+
];
|
|
2469
|
+
this.ee?.emit("delegation:stake", {
|
|
2470
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2471
|
+
finalityProviders: stakingInput.finalityProviderPksNoCoordHex,
|
|
2472
|
+
covenantPks: params.covenantNoCoordPks,
|
|
2473
|
+
covenantThreshold: params.covenantQuorum,
|
|
2474
|
+
unbondingTimeBlocks: params.unbondingTime,
|
|
2475
|
+
stakingDuration: stakingInput.stakingTimelock,
|
|
2476
|
+
type: "staking"
|
|
2477
|
+
});
|
|
2478
|
+
const signedStakingPsbtHex = await this.btcProvider.signPsbt(stakingExpansionPsbt2.toHex(), {
|
|
2479
|
+
contracts,
|
|
2480
|
+
action: {
|
|
2481
|
+
name: "sign-btc-staking-transaction" /* SIGN_BTC_STAKING_TRANSACTION */
|
|
2482
|
+
}
|
|
2483
|
+
});
|
|
2484
|
+
const signedStakingExpansionTx = import_bitcoinjs_lib9.Psbt.fromHex(signedStakingPsbtHex).extractTransaction();
|
|
2485
|
+
if (signedStakingExpansionTx.getId() !== unsignedStakingExpansionTx.getId()) {
|
|
2486
|
+
throw new Error("Staking expansion transaction hash does not match the computed hash");
|
|
2487
|
+
}
|
|
2488
|
+
const covenantBuffers = previousParams.covenantNoCoordPks.map((covenant) => Buffer.from(covenant, "hex"));
|
|
2489
|
+
const witness = createCovenantWitness(
|
|
2490
|
+
// The first input of the staking expansion transaction is the previous
|
|
2491
|
+
// staking output. We will attach the covenant signatures to this input
|
|
2492
|
+
// to unbond the previousstaking output.
|
|
2493
|
+
signedStakingExpansionTx.ins[0].witness,
|
|
2494
|
+
covenantBuffers,
|
|
2495
|
+
covenantStakingExpansionSignatures,
|
|
2496
|
+
previousParams.covenantQuorum
|
|
2117
2497
|
);
|
|
2118
|
-
|
|
2498
|
+
signedStakingExpansionTx.ins[0].witness = witness;
|
|
2499
|
+
return signedStakingExpansionTx;
|
|
2119
2500
|
}
|
|
2120
2501
|
/**
|
|
2121
2502
|
* Creates a partial signed unbonding transaction that is only signed by the
|
|
@@ -2132,36 +2513,64 @@ var BabylonBtcStakingManager = class {
|
|
|
2132
2513
|
* @returns The partial signed unbonding transaction and its fee.
|
|
2133
2514
|
*/
|
|
2134
2515
|
async createPartialSignedBtcUnbondingTransaction(stakerBtcInfo, stakingInput, stakingParamsVersion, stakingTx) {
|
|
2135
|
-
const params = getBabylonParamByVersion(
|
|
2136
|
-
stakingParamsVersion,
|
|
2137
|
-
this.stakingParams
|
|
2138
|
-
);
|
|
2516
|
+
const params = getBabylonParamByVersion(stakingParamsVersion, this.stakingParams);
|
|
2139
2517
|
const staking = new Staking(
|
|
2140
2518
|
this.network,
|
|
2141
2519
|
stakerBtcInfo,
|
|
2142
2520
|
params,
|
|
2143
|
-
stakingInput.
|
|
2521
|
+
stakingInput.finalityProviderPksNoCoordHex,
|
|
2144
2522
|
stakingInput.stakingTimelock
|
|
2145
2523
|
);
|
|
2146
|
-
const {
|
|
2147
|
-
transaction: unbondingTx,
|
|
2148
|
-
fee
|
|
2149
|
-
} = staking.createUnbondingTransaction(stakingTx);
|
|
2524
|
+
const { transaction: unbondingTx, fee } = staking.createUnbondingTransaction(stakingTx);
|
|
2150
2525
|
const psbt = staking.toUnbondingPsbt(unbondingTx, stakingTx);
|
|
2151
|
-
const
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2157
|
-
|
|
2526
|
+
const contracts = [
|
|
2527
|
+
{
|
|
2528
|
+
id: "babylon:staking" /* STAKING */,
|
|
2529
|
+
params: {
|
|
2530
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2531
|
+
finalityProviders: stakingInput.finalityProviderPksNoCoordHex,
|
|
2532
|
+
covenantPks: params.covenantNoCoordPks,
|
|
2533
|
+
covenantThreshold: params.covenantQuorum,
|
|
2534
|
+
minUnbondingTime: params.unbondingTime,
|
|
2535
|
+
stakingDuration: stakingInput.stakingTimelock
|
|
2536
|
+
}
|
|
2537
|
+
},
|
|
2538
|
+
{
|
|
2539
|
+
id: "babylon:unbonding" /* UNBONDING */,
|
|
2540
|
+
params: {
|
|
2541
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2542
|
+
finalityProviders: stakingInput.finalityProviderPksNoCoordHex,
|
|
2543
|
+
covenantPks: params.covenantNoCoordPks,
|
|
2544
|
+
covenantThreshold: params.covenantQuorum,
|
|
2545
|
+
unbondingTimeBlocks: params.unbondingTime,
|
|
2546
|
+
unbondingFeeSat: params.unbondingFeeSat
|
|
2547
|
+
}
|
|
2548
|
+
}
|
|
2549
|
+
];
|
|
2550
|
+
this.ee?.emit("delegation:unbond", {
|
|
2551
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2552
|
+
finalityProviders: stakingInput.finalityProviderPksNoCoordHex,
|
|
2553
|
+
covenantPks: params.covenantNoCoordPks,
|
|
2554
|
+
covenantThreshold: params.covenantQuorum,
|
|
2555
|
+
stakingDuration: stakingInput.stakingTimelock,
|
|
2556
|
+
unbondingTimeBlocks: params.unbondingTime,
|
|
2557
|
+
unbondingFeeSat: params.unbondingFeeSat,
|
|
2558
|
+
type: "unbonding"
|
|
2559
|
+
});
|
|
2560
|
+
const signedUnbondingPsbtHex = await this.btcProvider.signPsbt(psbt.toHex(), {
|
|
2561
|
+
contracts,
|
|
2562
|
+
action: {
|
|
2563
|
+
name: "sign-btc-unbonding-transaction" /* SIGN_BTC_UNBONDING_TRANSACTION */
|
|
2564
|
+
}
|
|
2565
|
+
});
|
|
2566
|
+
const signedUnbondingTx = import_bitcoinjs_lib9.Psbt.fromHex(signedUnbondingPsbtHex).extractTransaction();
|
|
2158
2567
|
return {
|
|
2159
2568
|
transaction: signedUnbondingTx,
|
|
2160
2569
|
fee
|
|
2161
2570
|
};
|
|
2162
2571
|
}
|
|
2163
2572
|
/**
|
|
2164
|
-
* Creates a signed unbonding transaction that is ready to be sent to the BTC
|
|
2573
|
+
* Creates a signed unbonding transaction that is ready to be sent to the BTC
|
|
2165
2574
|
* network.
|
|
2166
2575
|
* @param stakerBtcInfo - The staker BTC info which includes the BTC address
|
|
2167
2576
|
* and the no-coord public key in hex format.
|
|
@@ -2175,27 +2584,17 @@ var BabylonBtcStakingManager = class {
|
|
|
2175
2584
|
* @returns The signed unbonding transaction and its fee.
|
|
2176
2585
|
*/
|
|
2177
2586
|
async createSignedBtcUnbondingTransaction(stakerBtcInfo, stakingInput, stakingParamsVersion, stakingTx, unsignedUnbondingTx, covenantUnbondingSignatures) {
|
|
2178
|
-
const params = getBabylonParamByVersion(
|
|
2179
|
-
|
|
2180
|
-
this.stakingParams
|
|
2181
|
-
);
|
|
2182
|
-
const {
|
|
2183
|
-
transaction: signedUnbondingTx,
|
|
2184
|
-
fee
|
|
2185
|
-
} = await this.createPartialSignedBtcUnbondingTransaction(
|
|
2587
|
+
const params = getBabylonParamByVersion(stakingParamsVersion, this.stakingParams);
|
|
2588
|
+
const { transaction: signedUnbondingTx, fee } = await this.createPartialSignedBtcUnbondingTransaction(
|
|
2186
2589
|
stakerBtcInfo,
|
|
2187
2590
|
stakingInput,
|
|
2188
2591
|
stakingParamsVersion,
|
|
2189
2592
|
stakingTx
|
|
2190
2593
|
);
|
|
2191
2594
|
if (signedUnbondingTx.getId() !== unsignedUnbondingTx.getId()) {
|
|
2192
|
-
throw new Error(
|
|
2193
|
-
"Unbonding transaction hash does not match the computed hash"
|
|
2194
|
-
);
|
|
2595
|
+
throw new Error("Unbonding transaction hash does not match the computed hash");
|
|
2195
2596
|
}
|
|
2196
|
-
const covenantBuffers = params.covenantNoCoordPks.map(
|
|
2197
|
-
(covenant) => Buffer.from(covenant, "hex")
|
|
2198
|
-
);
|
|
2597
|
+
const covenantBuffers = params.covenantNoCoordPks.map((covenant) => Buffer.from(covenant, "hex"));
|
|
2199
2598
|
const witness = createCovenantWitness(
|
|
2200
2599
|
// Since unbonding transactions always have a single input and output,
|
|
2201
2600
|
// we expect exactly one signature in TaprootScriptSpendSig when the
|
|
@@ -2212,7 +2611,7 @@ var BabylonBtcStakingManager = class {
|
|
|
2212
2611
|
};
|
|
2213
2612
|
}
|
|
2214
2613
|
/**
|
|
2215
|
-
* Creates a signed withdrawal transaction on the unbodning output expiry path
|
|
2614
|
+
* Creates a signed withdrawal transaction on the unbodning output expiry path
|
|
2216
2615
|
* that is ready to be sent to the BTC network.
|
|
2217
2616
|
* @param stakingInput - The staking inputs.
|
|
2218
2617
|
* @param stakingParamsVersion - The params version that was used to create the
|
|
@@ -2224,32 +2623,42 @@ var BabylonBtcStakingManager = class {
|
|
|
2224
2623
|
* @returns The signed withdrawal transaction and its fee.
|
|
2225
2624
|
*/
|
|
2226
2625
|
async createSignedBtcWithdrawEarlyUnbondedTransaction(stakerBtcInfo, stakingInput, stakingParamsVersion, earlyUnbondingTx, feeRate) {
|
|
2227
|
-
const params = getBabylonParamByVersion(
|
|
2228
|
-
stakingParamsVersion,
|
|
2229
|
-
this.stakingParams
|
|
2230
|
-
);
|
|
2626
|
+
const params = getBabylonParamByVersion(stakingParamsVersion, this.stakingParams);
|
|
2231
2627
|
const staking = new Staking(
|
|
2232
2628
|
this.network,
|
|
2233
2629
|
stakerBtcInfo,
|
|
2234
2630
|
params,
|
|
2235
|
-
stakingInput.
|
|
2631
|
+
stakingInput.finalityProviderPksNoCoordHex,
|
|
2236
2632
|
stakingInput.stakingTimelock
|
|
2237
2633
|
);
|
|
2238
|
-
const { psbt: unbondingPsbt2, fee } = staking.createWithdrawEarlyUnbondedTransaction(
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2634
|
+
const { psbt: unbondingPsbt2, fee } = staking.createWithdrawEarlyUnbondedTransaction(earlyUnbondingTx, feeRate);
|
|
2635
|
+
const contracts = [
|
|
2636
|
+
{
|
|
2637
|
+
id: "babylon:withdraw" /* WITHDRAW */,
|
|
2638
|
+
params: {
|
|
2639
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2640
|
+
timelockBlocks: params.unbondingTime
|
|
2641
|
+
}
|
|
2642
|
+
}
|
|
2643
|
+
];
|
|
2644
|
+
this.ee?.emit("delegation:withdraw", {
|
|
2645
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2646
|
+
timelockBlocks: params.unbondingTime,
|
|
2647
|
+
type: "early-unbonded"
|
|
2648
|
+
});
|
|
2649
|
+
const signedWithdrawalPsbtHex = await this.btcProvider.signPsbt(unbondingPsbt2.toHex(), {
|
|
2650
|
+
contracts,
|
|
2651
|
+
action: {
|
|
2652
|
+
name: "sign-btc-withdraw-transaction" /* SIGN_BTC_WITHDRAW_TRANSACTION */
|
|
2653
|
+
}
|
|
2654
|
+
});
|
|
2246
2655
|
return {
|
|
2247
|
-
transaction:
|
|
2656
|
+
transaction: import_bitcoinjs_lib9.Psbt.fromHex(signedWithdrawalPsbtHex).extractTransaction(),
|
|
2248
2657
|
fee
|
|
2249
2658
|
};
|
|
2250
2659
|
}
|
|
2251
2660
|
/**
|
|
2252
|
-
* Creates a signed withdrawal transaction on the staking output expiry path
|
|
2661
|
+
* Creates a signed withdrawal transaction on the staking output expiry path
|
|
2253
2662
|
* that is ready to be sent to the BTC network.
|
|
2254
2663
|
* @param stakerBtcInfo - The staker BTC info which includes the BTC address
|
|
2255
2664
|
* and the no-coord public key in hex format.
|
|
@@ -2263,32 +2672,42 @@ var BabylonBtcStakingManager = class {
|
|
|
2263
2672
|
* @returns The signed withdrawal transaction and its fee.
|
|
2264
2673
|
*/
|
|
2265
2674
|
async createSignedBtcWithdrawStakingExpiredTransaction(stakerBtcInfo, stakingInput, stakingParamsVersion, stakingTx, feeRate) {
|
|
2266
|
-
const params = getBabylonParamByVersion(
|
|
2267
|
-
stakingParamsVersion,
|
|
2268
|
-
this.stakingParams
|
|
2269
|
-
);
|
|
2675
|
+
const params = getBabylonParamByVersion(stakingParamsVersion, this.stakingParams);
|
|
2270
2676
|
const staking = new Staking(
|
|
2271
2677
|
this.network,
|
|
2272
2678
|
stakerBtcInfo,
|
|
2273
2679
|
params,
|
|
2274
|
-
stakingInput.
|
|
2680
|
+
stakingInput.finalityProviderPksNoCoordHex,
|
|
2275
2681
|
stakingInput.stakingTimelock
|
|
2276
2682
|
);
|
|
2277
|
-
const { psbt, fee } = staking.createWithdrawStakingExpiredPsbt(
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2683
|
+
const { psbt, fee } = staking.createWithdrawStakingExpiredPsbt(stakingTx, feeRate);
|
|
2684
|
+
const contracts = [
|
|
2685
|
+
{
|
|
2686
|
+
id: "babylon:withdraw" /* WITHDRAW */,
|
|
2687
|
+
params: {
|
|
2688
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2689
|
+
timelockBlocks: stakingInput.stakingTimelock
|
|
2690
|
+
}
|
|
2691
|
+
}
|
|
2692
|
+
];
|
|
2693
|
+
this.ee?.emit("delegation:withdraw", {
|
|
2694
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2695
|
+
timelockBlocks: stakingInput.stakingTimelock,
|
|
2696
|
+
type: "staking-expired"
|
|
2697
|
+
});
|
|
2698
|
+
const signedWithdrawalPsbtHex = await this.btcProvider.signPsbt(psbt.toHex(), {
|
|
2699
|
+
contracts,
|
|
2700
|
+
action: {
|
|
2701
|
+
name: "sign-btc-withdraw-transaction" /* SIGN_BTC_WITHDRAW_TRANSACTION */
|
|
2702
|
+
}
|
|
2703
|
+
});
|
|
2285
2704
|
return {
|
|
2286
|
-
transaction:
|
|
2705
|
+
transaction: import_bitcoinjs_lib9.Psbt.fromHex(signedWithdrawalPsbtHex).extractTransaction(),
|
|
2287
2706
|
fee
|
|
2288
2707
|
};
|
|
2289
2708
|
}
|
|
2290
2709
|
/**
|
|
2291
|
-
* Creates a signed withdrawal transaction for the expired slashing output that
|
|
2710
|
+
* Creates a signed withdrawal transaction for the expired slashing output that
|
|
2292
2711
|
* is ready to be sent to the BTC network.
|
|
2293
2712
|
* @param stakerBtcInfo - The staker BTC info which includes the BTC address
|
|
2294
2713
|
* and the no-coord public key in hex format.
|
|
@@ -2302,44 +2721,70 @@ var BabylonBtcStakingManager = class {
|
|
|
2302
2721
|
* @returns The signed withdrawal transaction and its fee.
|
|
2303
2722
|
*/
|
|
2304
2723
|
async createSignedBtcWithdrawSlashingTransaction(stakerBtcInfo, stakingInput, stakingParamsVersion, slashingTx, feeRate) {
|
|
2305
|
-
const params = getBabylonParamByVersion(
|
|
2306
|
-
stakingParamsVersion,
|
|
2307
|
-
this.stakingParams
|
|
2308
|
-
);
|
|
2724
|
+
const params = getBabylonParamByVersion(stakingParamsVersion, this.stakingParams);
|
|
2309
2725
|
const staking = new Staking(
|
|
2310
2726
|
this.network,
|
|
2311
2727
|
stakerBtcInfo,
|
|
2312
2728
|
params,
|
|
2313
|
-
stakingInput.
|
|
2729
|
+
stakingInput.finalityProviderPksNoCoordHex,
|
|
2314
2730
|
stakingInput.stakingTimelock
|
|
2315
2731
|
);
|
|
2316
|
-
const { psbt, fee } = staking.createWithdrawSlashingPsbt(
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2732
|
+
const { psbt, fee } = staking.createWithdrawSlashingPsbt(slashingTx, feeRate);
|
|
2733
|
+
const contracts = [
|
|
2734
|
+
{
|
|
2735
|
+
id: "babylon:withdraw" /* WITHDRAW */,
|
|
2736
|
+
params: {
|
|
2737
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2738
|
+
timelockBlocks: params.unbondingTime
|
|
2739
|
+
}
|
|
2740
|
+
}
|
|
2741
|
+
];
|
|
2742
|
+
this.ee?.emit("delegation:withdraw", {
|
|
2743
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2744
|
+
timelockBlocks: params.unbondingTime,
|
|
2745
|
+
type: "slashing"
|
|
2746
|
+
});
|
|
2747
|
+
const signedWithrawSlashingPsbtHex = await this.btcProvider.signPsbt(psbt.toHex(), {
|
|
2748
|
+
contracts,
|
|
2749
|
+
action: {
|
|
2750
|
+
name: "sign-btc-withdraw-transaction" /* SIGN_BTC_WITHDRAW_TRANSACTION */
|
|
2751
|
+
}
|
|
2752
|
+
});
|
|
2324
2753
|
return {
|
|
2325
|
-
transaction:
|
|
2754
|
+
transaction: import_bitcoinjs_lib9.Psbt.fromHex(signedWithrawSlashingPsbtHex).extractTransaction(),
|
|
2326
2755
|
fee
|
|
2327
2756
|
};
|
|
2328
2757
|
}
|
|
2329
2758
|
/**
|
|
2330
2759
|
* Creates a proof of possession for the staker based on ECDSA signature.
|
|
2331
|
-
* @param bech32Address - The staker's bech32 address
|
|
2332
|
-
* @param stakerBtcAddress - The staker's BTC address.
|
|
2760
|
+
* @param bech32Address - The staker's bech32 address.
|
|
2333
2761
|
* @returns The proof of possession.
|
|
2334
2762
|
*/
|
|
2335
|
-
async createProofOfPossession(bech32Address, stakerBtcAddress) {
|
|
2763
|
+
async createProofOfPossession(channel, bech32Address, stakerBtcAddress) {
|
|
2336
2764
|
let sigType = import_pop.BTCSigType.ECDSA;
|
|
2337
2765
|
if (isTaproot(stakerBtcAddress, this.network) || isNativeSegwit(stakerBtcAddress, this.network)) {
|
|
2338
2766
|
sigType = import_pop.BTCSigType.BIP322;
|
|
2339
2767
|
}
|
|
2340
|
-
const
|
|
2341
|
-
|
|
2768
|
+
const [chainId, babyTipHeight] = await Promise.all([
|
|
2769
|
+
this.babylonProvider.getChainId?.(),
|
|
2770
|
+
this.babylonProvider.getCurrentHeight?.()
|
|
2771
|
+
]);
|
|
2772
|
+
const upgradeConfig = this.upgradeConfig?.pop;
|
|
2773
|
+
const messageToSign = buildPopMessage(
|
|
2342
2774
|
bech32Address,
|
|
2775
|
+
babyTipHeight,
|
|
2776
|
+
chainId,
|
|
2777
|
+
upgradeConfig && {
|
|
2778
|
+
upgradeHeight: upgradeConfig.upgradeHeight,
|
|
2779
|
+
version: upgradeConfig.version
|
|
2780
|
+
}
|
|
2781
|
+
);
|
|
2782
|
+
this.ee?.emit(channel, {
|
|
2783
|
+
messageToSign,
|
|
2784
|
+
type: "proof-of-possession"
|
|
2785
|
+
});
|
|
2786
|
+
const signedBabylonAddress = await this.btcProvider.signMessage(
|
|
2787
|
+
messageToSign,
|
|
2343
2788
|
sigType === import_pop.BTCSigType.BIP322 ? "bip322-simple" : "ecdsa"
|
|
2344
2789
|
);
|
|
2345
2790
|
let btcSig;
|
|
@@ -2358,11 +2803,11 @@ var BabylonBtcStakingManager = class {
|
|
|
2358
2803
|
};
|
|
2359
2804
|
}
|
|
2360
2805
|
/**
|
|
2361
|
-
* Creates the unbonding, slashing, and unbonding slashing transactions and
|
|
2806
|
+
* Creates the unbonding, slashing, and unbonding slashing transactions and
|
|
2362
2807
|
* PSBTs.
|
|
2363
2808
|
* @param stakingInstance - The staking instance.
|
|
2364
2809
|
* @param stakingTx - The staking transaction.
|
|
2365
|
-
* @returns The unbonding, slashing, and unbonding slashing transactions and
|
|
2810
|
+
* @returns The unbonding, slashing, and unbonding slashing transactions and
|
|
2366
2811
|
* PSBTs.
|
|
2367
2812
|
*/
|
|
2368
2813
|
async createDelegationTransactionsAndPsbts(stakingInstance, stakingTx) {
|
|
@@ -2377,84 +2822,164 @@ var BabylonBtcStakingManager = class {
|
|
|
2377
2822
|
}
|
|
2378
2823
|
/**
|
|
2379
2824
|
* Creates a protobuf message for the BTC delegation.
|
|
2825
|
+
* @param channel - The event channel to emit the message on.
|
|
2380
2826
|
* @param stakingInstance - The staking instance.
|
|
2381
2827
|
* @param stakingInput - The staking inputs.
|
|
2382
2828
|
* @param stakingTx - The staking transaction.
|
|
2383
2829
|
* @param bech32Address - The staker's babylon chain bech32 address
|
|
2384
|
-
* @param stakerBtcInfo - The staker's BTC information such as address and
|
|
2830
|
+
* @param stakerBtcInfo - The staker's BTC information such as address and
|
|
2385
2831
|
* public key
|
|
2386
2832
|
* @param params - The staking parameters.
|
|
2387
|
-
* @param
|
|
2833
|
+
* @param options - The options for the BTC delegation.
|
|
2834
|
+
* @param options.inclusionProof - The inclusion proof of the staking
|
|
2835
|
+
* transaction.
|
|
2836
|
+
* @param options.delegationExpansionInfo - The information for the BTC
|
|
2837
|
+
* delegation expansion.
|
|
2388
2838
|
* @returns The protobuf message.
|
|
2389
2839
|
*/
|
|
2390
|
-
async createBtcDelegationMsg(stakingInstance, stakingInput, stakingTx, bech32Address, stakerBtcInfo, params,
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2840
|
+
async createBtcDelegationMsg(channel, stakingInstance, stakingInput, stakingTx, bech32Address, stakerBtcInfo, params, options) {
|
|
2841
|
+
if (!params.slashing) {
|
|
2842
|
+
throw new StakingError(
|
|
2843
|
+
"INVALID_PARAMS" /* INVALID_PARAMS */,
|
|
2844
|
+
"Slashing parameters are required for creating delegation message"
|
|
2845
|
+
);
|
|
2846
|
+
}
|
|
2847
|
+
const { unbondingTx, slashingPsbt, unbondingSlashingPsbt } = await this.createDelegationTransactionsAndPsbts(
|
|
2396
2848
|
stakingInstance,
|
|
2397
|
-
stakingTx
|
|
2398
|
-
);
|
|
2399
|
-
const signedSlashingPsbtHex = await this.btcProvider.signPsbt(
|
|
2400
|
-
"staking-slashing" /* STAKING_SLASHING */,
|
|
2401
|
-
slashingPsbt.toHex()
|
|
2402
|
-
);
|
|
2403
|
-
const signedSlashingTx = import_bitcoinjs_lib10.Psbt.fromHex(
|
|
2404
|
-
signedSlashingPsbtHex
|
|
2405
|
-
).extractTransaction();
|
|
2406
|
-
const slashingSig = extractFirstSchnorrSignatureFromTransaction(
|
|
2407
|
-
signedSlashingTx
|
|
2849
|
+
stakingTx
|
|
2408
2850
|
);
|
|
2851
|
+
const slashingContracts = [
|
|
2852
|
+
{
|
|
2853
|
+
id: "babylon:staking" /* STAKING */,
|
|
2854
|
+
params: {
|
|
2855
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2856
|
+
finalityProviders: stakingInput.finalityProviderPksNoCoordHex,
|
|
2857
|
+
covenantPks: params.covenantNoCoordPks,
|
|
2858
|
+
covenantThreshold: params.covenantQuorum,
|
|
2859
|
+
minUnbondingTime: params.unbondingTime,
|
|
2860
|
+
stakingDuration: stakingInput.stakingTimelock
|
|
2861
|
+
}
|
|
2862
|
+
},
|
|
2863
|
+
{
|
|
2864
|
+
id: "babylon:slashing" /* SLASHING */,
|
|
2865
|
+
params: {
|
|
2866
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2867
|
+
unbondingTimeBlocks: params.unbondingTime,
|
|
2868
|
+
slashingFeeSat: params.slashing.minSlashingTxFeeSat
|
|
2869
|
+
}
|
|
2870
|
+
},
|
|
2871
|
+
{
|
|
2872
|
+
id: "babylon:slashing-burn" /* SLASHING_BURN */,
|
|
2873
|
+
params: {
|
|
2874
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2875
|
+
slashingPkScriptHex: params.slashing.slashingPkScriptHex
|
|
2876
|
+
}
|
|
2877
|
+
}
|
|
2878
|
+
];
|
|
2879
|
+
this.ee?.emit(channel, {
|
|
2880
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2881
|
+
finalityProviders: stakingInput.finalityProviderPksNoCoordHex,
|
|
2882
|
+
covenantPks: params.covenantNoCoordPks,
|
|
2883
|
+
covenantThreshold: params.covenantQuorum,
|
|
2884
|
+
unbondingTimeBlocks: params.unbondingTime,
|
|
2885
|
+
stakingDuration: stakingInput.stakingTimelock,
|
|
2886
|
+
slashingFeeSat: params.slashing.minSlashingTxFeeSat,
|
|
2887
|
+
slashingPkScriptHex: params.slashing.slashingPkScriptHex,
|
|
2888
|
+
type: "staking-slashing"
|
|
2889
|
+
});
|
|
2890
|
+
const signedSlashingPsbtHex = await this.btcProvider.signPsbt(slashingPsbt.toHex(), {
|
|
2891
|
+
contracts: slashingContracts,
|
|
2892
|
+
action: {
|
|
2893
|
+
name: "sign-btc-slashing-transaction" /* SIGN_BTC_SLASHING_TRANSACTION */
|
|
2894
|
+
}
|
|
2895
|
+
});
|
|
2896
|
+
const signedSlashingTx = import_bitcoinjs_lib9.Psbt.fromHex(signedSlashingPsbtHex).extractTransaction();
|
|
2897
|
+
const slashingSig = extractFirstSchnorrSignatureFromTransaction(signedSlashingTx);
|
|
2409
2898
|
if (!slashingSig) {
|
|
2410
2899
|
throw new Error("No signature found in the staking output slashing PSBT");
|
|
2411
2900
|
}
|
|
2412
|
-
const
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2901
|
+
const unbondingSlashingContracts = [
|
|
2902
|
+
{
|
|
2903
|
+
id: "babylon:unbonding" /* UNBONDING */,
|
|
2904
|
+
params: {
|
|
2905
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2906
|
+
finalityProviders: stakingInput.finalityProviderPksNoCoordHex,
|
|
2907
|
+
covenantPks: params.covenantNoCoordPks,
|
|
2908
|
+
covenantThreshold: params.covenantQuorum,
|
|
2909
|
+
unbondingTimeBlocks: params.unbondingTime,
|
|
2910
|
+
unbondingFeeSat: params.unbondingFeeSat
|
|
2911
|
+
}
|
|
2912
|
+
},
|
|
2913
|
+
{
|
|
2914
|
+
id: "babylon:slashing" /* SLASHING */,
|
|
2915
|
+
params: {
|
|
2916
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2917
|
+
unbondingTimeBlocks: params.unbondingTime,
|
|
2918
|
+
slashingFeeSat: params.slashing.minSlashingTxFeeSat
|
|
2919
|
+
}
|
|
2920
|
+
},
|
|
2921
|
+
{
|
|
2922
|
+
id: "babylon:slashing-burn" /* SLASHING_BURN */,
|
|
2923
|
+
params: {
|
|
2924
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2925
|
+
slashingPkScriptHex: params.slashing.slashingPkScriptHex
|
|
2926
|
+
}
|
|
2927
|
+
}
|
|
2928
|
+
];
|
|
2929
|
+
this.ee?.emit(channel, {
|
|
2930
|
+
stakerPk: stakerBtcInfo.publicKeyNoCoordHex,
|
|
2931
|
+
finalityProviders: stakingInput.finalityProviderPksNoCoordHex,
|
|
2932
|
+
covenantPks: params.covenantNoCoordPks,
|
|
2933
|
+
covenantThreshold: params.covenantQuorum,
|
|
2934
|
+
unbondingTimeBlocks: params.unbondingTime,
|
|
2935
|
+
unbondingFeeSat: params.unbondingFeeSat,
|
|
2936
|
+
slashingFeeSat: params.slashing.minSlashingTxFeeSat,
|
|
2937
|
+
slashingPkScriptHex: params.slashing.slashingPkScriptHex,
|
|
2938
|
+
type: "unbonding-slashing"
|
|
2939
|
+
});
|
|
2940
|
+
const signedUnbondingSlashingPsbtHex = await this.btcProvider.signPsbt(unbondingSlashingPsbt.toHex(), {
|
|
2941
|
+
contracts: unbondingSlashingContracts,
|
|
2942
|
+
action: {
|
|
2943
|
+
name: "sign-btc-unbonding-slashing-transaction" /* SIGN_BTC_UNBONDING_SLASHING_TRANSACTION */
|
|
2944
|
+
}
|
|
2945
|
+
});
|
|
2946
|
+
const signedUnbondingSlashingTx = import_bitcoinjs_lib9.Psbt.fromHex(signedUnbondingSlashingPsbtHex).extractTransaction();
|
|
2947
|
+
const unbondingSignatures = extractFirstSchnorrSignatureFromTransaction(signedUnbondingSlashingTx);
|
|
2422
2948
|
if (!unbondingSignatures) {
|
|
2423
2949
|
throw new Error("No signature found in the unbonding output slashing PSBT");
|
|
2424
2950
|
}
|
|
2425
|
-
const proofOfPossession = await this.createProofOfPossession(
|
|
2426
|
-
|
|
2427
|
-
stakerBtcInfo.address
|
|
2428
|
-
);
|
|
2429
|
-
const msg = import_babylon_proto_ts.btcstakingtx.MsgCreateBTCDelegation.fromPartial({
|
|
2951
|
+
const proofOfPossession = await this.createProofOfPossession(channel, bech32Address, stakerBtcInfo.address);
|
|
2952
|
+
const commonMsg = {
|
|
2430
2953
|
stakerAddr: bech32Address,
|
|
2431
2954
|
pop: proofOfPossession,
|
|
2432
|
-
btcPk: Uint8Array.from(
|
|
2433
|
-
|
|
2434
|
-
),
|
|
2435
|
-
fpBtcPkList: [
|
|
2436
|
-
Uint8Array.from(
|
|
2437
|
-
Buffer.from(stakingInput.finalityProviderPkNoCoordHex, "hex")
|
|
2438
|
-
)
|
|
2439
|
-
],
|
|
2955
|
+
btcPk: Uint8Array.from(Buffer.from(stakerBtcInfo.publicKeyNoCoordHex, "hex")),
|
|
2956
|
+
fpBtcPkList: stakingInput.finalityProviderPksNoCoordHex.map((pk) => Uint8Array.from(Buffer.from(pk, "hex"))),
|
|
2440
2957
|
stakingTime: stakingInput.stakingTimelock,
|
|
2441
2958
|
stakingValue: stakingInput.stakingAmountSat,
|
|
2442
2959
|
stakingTx: Uint8Array.from(stakingTx.toBuffer()),
|
|
2443
|
-
slashingTx: Uint8Array.from(
|
|
2444
|
-
Buffer.from(clearTxSignatures(signedSlashingTx).toHex(), "hex")
|
|
2445
|
-
),
|
|
2960
|
+
slashingTx: Uint8Array.from(Buffer.from(clearTxSignatures(signedSlashingTx).toHex(), "hex")),
|
|
2446
2961
|
delegatorSlashingSig: Uint8Array.from(slashingSig),
|
|
2447
2962
|
unbondingTime: params.unbondingTime,
|
|
2448
2963
|
unbondingTx: Uint8Array.from(unbondingTx.toBuffer()),
|
|
2449
2964
|
unbondingValue: stakingInput.stakingAmountSat - params.unbondingFeeSat,
|
|
2450
|
-
unbondingSlashingTx: Uint8Array.from(
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2965
|
+
unbondingSlashingTx: Uint8Array.from(Buffer.from(clearTxSignatures(signedUnbondingSlashingTx).toHex(), "hex")),
|
|
2966
|
+
delegatorUnbondingSlashingSig: Uint8Array.from(unbondingSignatures)
|
|
2967
|
+
};
|
|
2968
|
+
if (options?.delegationExpansionInfo) {
|
|
2969
|
+
const fundingTx = Uint8Array.from(options.delegationExpansionInfo.fundingTx.toBuffer());
|
|
2970
|
+
const msg2 = import_babylon_proto_ts.btcstakingtx.MsgBtcStakeExpand.fromPartial({
|
|
2971
|
+
...commonMsg,
|
|
2972
|
+
previousStakingTxHash: options.delegationExpansionInfo.previousStakingTx.getId(),
|
|
2973
|
+
fundingTx
|
|
2974
|
+
});
|
|
2975
|
+
return {
|
|
2976
|
+
typeUrl: BABYLON_REGISTRY_TYPE_URLS.MsgBtcStakeExpand,
|
|
2977
|
+
value: msg2
|
|
2978
|
+
};
|
|
2979
|
+
}
|
|
2980
|
+
const msg = import_babylon_proto_ts.btcstakingtx.MsgCreateBTCDelegation.fromPartial({
|
|
2981
|
+
...commonMsg,
|
|
2982
|
+
stakingTxInclusionProof: options?.inclusionProof
|
|
2458
2983
|
});
|
|
2459
2984
|
return {
|
|
2460
2985
|
typeUrl: BABYLON_REGISTRY_TYPE_URLS.MsgCreateBTCDelegation,
|
|
@@ -2468,11 +2993,7 @@ var BabylonBtcStakingManager = class {
|
|
|
2468
2993
|
* @returns The inclusion proof.
|
|
2469
2994
|
*/
|
|
2470
2995
|
getInclusionProof(inclusionProof) {
|
|
2471
|
-
const {
|
|
2472
|
-
pos,
|
|
2473
|
-
merkle,
|
|
2474
|
-
blockHashHex
|
|
2475
|
-
} = inclusionProof;
|
|
2996
|
+
const { pos, merkle, blockHashHex } = inclusionProof;
|
|
2476
2997
|
const proofHex = deriveMerkleProof(merkle);
|
|
2477
2998
|
const hash = reverseBuffer(Uint8Array.from(Buffer.from(blockHashHex, "hex")));
|
|
2478
2999
|
const inclusionProofKey = import_babylon_proto_ts.btccheckpoint.TransactionKey.fromPartial({
|
|
@@ -2485,55 +3006,236 @@ var BabylonBtcStakingManager = class {
|
|
|
2485
3006
|
});
|
|
2486
3007
|
}
|
|
2487
3008
|
};
|
|
2488
|
-
var extractFirstSchnorrSignatureFromTransaction = (singedTransaction) => {
|
|
2489
|
-
for (const input of singedTransaction.ins) {
|
|
2490
|
-
if (input.witness && input.witness.length > 0) {
|
|
2491
|
-
const schnorrSignature = input.witness[0];
|
|
2492
|
-
if (schnorrSignature.length === 64) {
|
|
2493
|
-
return schnorrSignature;
|
|
2494
|
-
}
|
|
2495
|
-
}
|
|
2496
|
-
}
|
|
2497
|
-
return void 0;
|
|
2498
|
-
};
|
|
2499
|
-
var clearTxSignatures = (tx) => {
|
|
2500
|
-
tx.ins.forEach((input) => {
|
|
2501
|
-
input.script = Buffer.alloc(0);
|
|
2502
|
-
input.witness = [];
|
|
2503
|
-
});
|
|
2504
|
-
return tx;
|
|
2505
|
-
};
|
|
2506
|
-
var deriveMerkleProof = (merkle) => {
|
|
2507
|
-
const proofHex = merkle.reduce((acc, m) => {
|
|
2508
|
-
return acc + Buffer.from(m, "hex").reverse().toString("hex");
|
|
2509
|
-
}, "");
|
|
2510
|
-
return proofHex;
|
|
2511
|
-
};
|
|
2512
3009
|
var getUnbondingTxStakerSignature = (unbondingTx) => {
|
|
2513
3010
|
try {
|
|
2514
3011
|
return unbondingTx.ins[0].witness[0].toString("hex");
|
|
2515
3012
|
} catch (error) {
|
|
2516
|
-
throw StakingError.fromUnknown(
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
|
|
3013
|
+
throw StakingError.fromUnknown(error, "INVALID_INPUT" /* INVALID_INPUT */, "Failed to get staker signature");
|
|
3014
|
+
}
|
|
3015
|
+
};
|
|
3016
|
+
|
|
3017
|
+
// src/staking/observable/observableStakingScript.ts
|
|
3018
|
+
var import_bitcoinjs_lib10 = require("bitcoinjs-lib");
|
|
3019
|
+
var ObservableStakingScriptData = class extends StakingScriptData {
|
|
3020
|
+
constructor(stakerKey, finalityProviderKeys, covenantKeys, covenantThreshold, stakingTimelock, unbondingTimelock, magicBytes) {
|
|
3021
|
+
super(
|
|
3022
|
+
stakerKey,
|
|
3023
|
+
finalityProviderKeys,
|
|
3024
|
+
covenantKeys,
|
|
3025
|
+
covenantThreshold,
|
|
3026
|
+
stakingTimelock,
|
|
3027
|
+
unbondingTimelock
|
|
3028
|
+
);
|
|
3029
|
+
if (!magicBytes) {
|
|
3030
|
+
throw new Error("Missing required input values");
|
|
3031
|
+
}
|
|
3032
|
+
if (magicBytes.length != MAGIC_BYTES_LEN) {
|
|
3033
|
+
throw new Error("Invalid script data provided");
|
|
3034
|
+
}
|
|
3035
|
+
this.magicBytes = magicBytes;
|
|
3036
|
+
}
|
|
3037
|
+
/**
|
|
3038
|
+
* Builds a data embed script for staking in the form:
|
|
3039
|
+
* OP_RETURN || <serializedStakingData>
|
|
3040
|
+
* where serializedStakingData is the concatenation of:
|
|
3041
|
+
* MagicBytes || Version || StakerPublicKey || FinalityProviderPublicKey || StakingTimeLock
|
|
3042
|
+
* Note: Only a single finality provider key is supported for now in phase 1
|
|
3043
|
+
* @throws {Error} If the number of finality provider keys is not equal to 1.
|
|
3044
|
+
* @returns {Buffer} The compiled data embed script.
|
|
3045
|
+
*/
|
|
3046
|
+
buildDataEmbedScript() {
|
|
3047
|
+
if (this.finalityProviderKeys.length != 1) {
|
|
3048
|
+
throw new Error("Only a single finality provider key is supported");
|
|
3049
|
+
}
|
|
3050
|
+
const version = Buffer.alloc(1);
|
|
3051
|
+
version.writeUInt8(0);
|
|
3052
|
+
const stakingTimeLock = Buffer.alloc(2);
|
|
3053
|
+
stakingTimeLock.writeUInt16BE(this.stakingTimeLock);
|
|
3054
|
+
const serializedStakingData = Buffer.concat([
|
|
3055
|
+
this.magicBytes,
|
|
3056
|
+
version,
|
|
3057
|
+
this.stakerKey,
|
|
3058
|
+
this.finalityProviderKeys[0],
|
|
3059
|
+
stakingTimeLock
|
|
3060
|
+
]);
|
|
3061
|
+
return import_bitcoinjs_lib10.script.compile([import_bitcoinjs_lib10.opcodes.OP_RETURN, serializedStakingData]);
|
|
3062
|
+
}
|
|
3063
|
+
/**
|
|
3064
|
+
* Builds the staking scripts.
|
|
3065
|
+
* @returns {ObservableStakingScripts} The staking scripts that can be used to stake.
|
|
3066
|
+
* contains the timelockScript, unbondingScript, slashingScript,
|
|
3067
|
+
* unbondingTimelockScript, and dataEmbedScript.
|
|
3068
|
+
* @throws {Error} If script data is invalid.
|
|
3069
|
+
*/
|
|
3070
|
+
buildScripts() {
|
|
3071
|
+
const scripts = super.buildScripts();
|
|
3072
|
+
return {
|
|
3073
|
+
...scripts,
|
|
3074
|
+
dataEmbedScript: this.buildDataEmbedScript()
|
|
3075
|
+
};
|
|
3076
|
+
}
|
|
3077
|
+
};
|
|
3078
|
+
|
|
3079
|
+
// src/staking/observable/index.ts
|
|
3080
|
+
var ObservableStaking = class extends Staking {
|
|
3081
|
+
constructor(network, stakerInfo, params, finalityProviderPksNoCoordHex, stakingTimelock) {
|
|
3082
|
+
super(
|
|
3083
|
+
network,
|
|
3084
|
+
stakerInfo,
|
|
3085
|
+
params,
|
|
3086
|
+
finalityProviderPksNoCoordHex,
|
|
3087
|
+
stakingTimelock
|
|
3088
|
+
);
|
|
3089
|
+
if (!params.tag) {
|
|
3090
|
+
throw new StakingError(
|
|
3091
|
+
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
3092
|
+
"Observable staking parameters must include tag"
|
|
3093
|
+
);
|
|
3094
|
+
}
|
|
3095
|
+
if (!params.btcActivationHeight) {
|
|
3096
|
+
throw new StakingError(
|
|
3097
|
+
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
3098
|
+
"Observable staking parameters must include a positive activation height"
|
|
3099
|
+
);
|
|
3100
|
+
}
|
|
3101
|
+
if (finalityProviderPksNoCoordHex.length !== 1) {
|
|
3102
|
+
throw new StakingError(
|
|
3103
|
+
"INVALID_INPUT" /* INVALID_INPUT */,
|
|
3104
|
+
"Observable staking requires exactly one finality provider public key"
|
|
3105
|
+
);
|
|
3106
|
+
}
|
|
3107
|
+
this.params = params;
|
|
3108
|
+
}
|
|
3109
|
+
/**
|
|
3110
|
+
* Build the staking scripts for observable staking.
|
|
3111
|
+
* This method overwrites the base method to include the OP_RETURN tag based
|
|
3112
|
+
* on the tag provided in the parameters.
|
|
3113
|
+
*
|
|
3114
|
+
* @returns {ObservableStakingScripts} - The staking scripts for observable staking.
|
|
3115
|
+
* @throws {StakingError} - If the scripts cannot be built.
|
|
3116
|
+
*/
|
|
3117
|
+
buildScripts() {
|
|
3118
|
+
const { covenantQuorum, covenantNoCoordPks, unbondingTime, tag } = this.params;
|
|
3119
|
+
let stakingScriptData;
|
|
3120
|
+
try {
|
|
3121
|
+
stakingScriptData = new ObservableStakingScriptData(
|
|
3122
|
+
Buffer.from(this.stakerInfo.publicKeyNoCoordHex, "hex"),
|
|
3123
|
+
this.finalityProviderPksNoCoordHex.map((pk) => Buffer.from(pk, "hex")),
|
|
3124
|
+
toBuffers(covenantNoCoordPks),
|
|
3125
|
+
covenantQuorum,
|
|
3126
|
+
this.stakingTimelock,
|
|
3127
|
+
unbondingTime,
|
|
3128
|
+
Buffer.from(tag, "hex")
|
|
3129
|
+
);
|
|
3130
|
+
} catch (error) {
|
|
3131
|
+
throw StakingError.fromUnknown(
|
|
3132
|
+
error,
|
|
3133
|
+
"SCRIPT_FAILURE" /* SCRIPT_FAILURE */,
|
|
3134
|
+
"Cannot build staking script data"
|
|
3135
|
+
);
|
|
3136
|
+
}
|
|
3137
|
+
let scripts;
|
|
3138
|
+
try {
|
|
3139
|
+
scripts = stakingScriptData.buildScripts();
|
|
3140
|
+
} catch (error) {
|
|
3141
|
+
throw StakingError.fromUnknown(
|
|
3142
|
+
error,
|
|
3143
|
+
"SCRIPT_FAILURE" /* SCRIPT_FAILURE */,
|
|
3144
|
+
"Cannot build staking scripts"
|
|
3145
|
+
);
|
|
3146
|
+
}
|
|
3147
|
+
return scripts;
|
|
3148
|
+
}
|
|
3149
|
+
/**
|
|
3150
|
+
* Create a staking transaction for observable staking.
|
|
3151
|
+
* This overwrites the method from the Staking class with the addtion
|
|
3152
|
+
* of the
|
|
3153
|
+
* 1. OP_RETURN tag in the staking scripts
|
|
3154
|
+
* 2. lockHeight parameter
|
|
3155
|
+
*
|
|
3156
|
+
* @param {number} stakingAmountSat - The amount to stake in satoshis.
|
|
3157
|
+
* @param {UTXO[]} inputUTXOs - The UTXOs to use as inputs for the staking
|
|
3158
|
+
* transaction.
|
|
3159
|
+
* @param {number} feeRate - The fee rate for the transaction in satoshis per byte.
|
|
3160
|
+
* @returns {TransactionResult} - An object containing the unsigned transaction,
|
|
3161
|
+
* and fee
|
|
3162
|
+
*/
|
|
3163
|
+
createStakingTransaction(stakingAmountSat, inputUTXOs, feeRate) {
|
|
3164
|
+
validateStakingTxInputData(
|
|
3165
|
+
stakingAmountSat,
|
|
3166
|
+
this.stakingTimelock,
|
|
3167
|
+
this.params,
|
|
3168
|
+
inputUTXOs,
|
|
3169
|
+
feeRate
|
|
3170
|
+
);
|
|
3171
|
+
const scripts = this.buildScripts();
|
|
3172
|
+
try {
|
|
3173
|
+
const { transaction, fee } = stakingTransaction(
|
|
3174
|
+
scripts,
|
|
3175
|
+
stakingAmountSat,
|
|
3176
|
+
this.stakerInfo.address,
|
|
3177
|
+
inputUTXOs,
|
|
3178
|
+
this.network,
|
|
3179
|
+
feeRate,
|
|
3180
|
+
// `lockHeight` is exclusive of the provided value.
|
|
3181
|
+
// For example, if a Bitcoin height of X is provided,
|
|
3182
|
+
// the transaction will be included starting from height X+1.
|
|
3183
|
+
// https://learnmeabitcoin.com/technical/transaction/locktime/
|
|
3184
|
+
this.params.btcActivationHeight - 1
|
|
3185
|
+
);
|
|
3186
|
+
return {
|
|
3187
|
+
transaction,
|
|
3188
|
+
fee
|
|
3189
|
+
};
|
|
3190
|
+
} catch (error) {
|
|
3191
|
+
throw StakingError.fromUnknown(
|
|
3192
|
+
error,
|
|
3193
|
+
"BUILD_TRANSACTION_FAILURE" /* BUILD_TRANSACTION_FAILURE */,
|
|
3194
|
+
"Cannot build unsigned staking transaction"
|
|
3195
|
+
);
|
|
3196
|
+
}
|
|
3197
|
+
}
|
|
3198
|
+
/**
|
|
3199
|
+
* Create a staking psbt for observable staking.
|
|
3200
|
+
*
|
|
3201
|
+
* @param {Transaction} stakingTx - The staking transaction.
|
|
3202
|
+
* @param {UTXO[]} inputUTXOs - The UTXOs to use as inputs for the staking
|
|
3203
|
+
* transaction.
|
|
3204
|
+
* @returns {Psbt} - The psbt.
|
|
3205
|
+
*/
|
|
3206
|
+
toStakingPsbt(stakingTx, inputUTXOs) {
|
|
3207
|
+
return stakingPsbt(
|
|
3208
|
+
stakingTx,
|
|
3209
|
+
this.network,
|
|
3210
|
+
inputUTXOs,
|
|
3211
|
+
isTaproot(
|
|
3212
|
+
this.stakerInfo.address,
|
|
3213
|
+
this.network
|
|
3214
|
+
) ? Buffer.from(this.stakerInfo.publicKeyNoCoordHex, "hex") : void 0
|
|
2520
3215
|
);
|
|
2521
3216
|
}
|
|
2522
3217
|
};
|
|
3218
|
+
|
|
3219
|
+
// src/types/params.ts
|
|
3220
|
+
function hasSlashing(params) {
|
|
3221
|
+
return params.slashing !== void 0;
|
|
3222
|
+
}
|
|
2523
3223
|
// Annotate the CommonJS export names for ESM import in node:
|
|
2524
3224
|
0 && (module.exports = {
|
|
2525
3225
|
BabylonBtcStakingManager,
|
|
2526
3226
|
BitcoinScriptType,
|
|
2527
3227
|
ObservableStaking,
|
|
2528
3228
|
ObservableStakingScriptData,
|
|
2529
|
-
SigningStep,
|
|
2530
3229
|
Staking,
|
|
2531
3230
|
StakingScriptData,
|
|
2532
3231
|
buildStakingTransactionOutputs,
|
|
3232
|
+
clearTxSignatures,
|
|
2533
3233
|
createCovenantWitness,
|
|
3234
|
+
deriveMerkleProof,
|
|
2534
3235
|
deriveSlashingOutput,
|
|
2535
3236
|
deriveStakingOutputInfo,
|
|
2536
3237
|
deriveUnbondingOutputInfo,
|
|
3238
|
+
extractFirstSchnorrSignatureFromTransaction,
|
|
2537
3239
|
findInputUTXO,
|
|
2538
3240
|
findMatchingTxOutputIndex,
|
|
2539
3241
|
getBabylonParamByBtcHeight,
|
|
@@ -2542,6 +3244,7 @@ var getUnbondingTxStakerSignature = (unbondingTx) => {
|
|
|
2542
3244
|
getPublicKeyNoCoord,
|
|
2543
3245
|
getScriptType,
|
|
2544
3246
|
getUnbondingTxStakerSignature,
|
|
3247
|
+
hasSlashing,
|
|
2545
3248
|
initBTCCurve,
|
|
2546
3249
|
isNativeSegwit,
|
|
2547
3250
|
isTaproot,
|
|
@@ -2550,14 +3253,13 @@ var getUnbondingTxStakerSignature = (unbondingTx) => {
|
|
|
2550
3253
|
isValidNoCoordPublicKey,
|
|
2551
3254
|
slashEarlyUnbondedTransaction,
|
|
2552
3255
|
slashTimelockUnbondedTransaction,
|
|
3256
|
+
stakingExpansionTransaction,
|
|
2553
3257
|
stakingTransaction,
|
|
2554
3258
|
toBuffers,
|
|
2555
3259
|
transactionIdToHash,
|
|
2556
3260
|
unbondingTransaction,
|
|
2557
|
-
validateParams,
|
|
2558
|
-
validateStakingTimelock,
|
|
2559
|
-
validateStakingTxInputData,
|
|
2560
3261
|
withdrawEarlyUnbondedTransaction,
|
|
2561
3262
|
withdrawSlashingTransaction,
|
|
2562
3263
|
withdrawTimelockUnbondedTransaction
|
|
2563
3264
|
});
|
|
3265
|
+
//# sourceMappingURL=index.cjs.map
|