@babylonlabs-io/ts-sdk 0.3.0 → 0.4.1-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{PayoutManager-BkOfdb2_.js → PayoutManager-B8vLG1dE.js} +414 -373
- package/dist/PayoutManager-B8vLG1dE.js.map +1 -0
- package/dist/PayoutManager-Shw7TY5P.cjs +2 -0
- package/dist/PayoutManager-Shw7TY5P.cjs.map +1 -0
- package/dist/challengeAssert-ClJYoKxJ.cjs +2 -0
- package/dist/challengeAssert-ClJYoKxJ.cjs.map +1 -0
- package/dist/challengeAssert-ZXE00zkT.js +617 -0
- package/dist/challengeAssert-ZXE00zkT.js.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +70 -63
- package/dist/index.js.map +1 -1
- package/dist/tbv/core/contracts/abis/BTCVaultsManager.abi.d.ts +50 -4
- package/dist/tbv/core/contracts/abis/BTCVaultsManager.abi.d.ts.map +1 -1
- package/dist/tbv/core/index.cjs +1 -1
- package/dist/tbv/core/index.js +65 -60
- package/dist/tbv/core/managers/PeginManager.d.ts +92 -49
- package/dist/tbv/core/managers/PeginManager.d.ts.map +1 -1
- package/dist/tbv/core/managers/__tests__/PeginManager.test.d.ts +1 -1
- package/dist/tbv/core/managers/index.d.ts +4 -4
- package/dist/tbv/core/managers/index.d.ts.map +1 -1
- package/dist/tbv/core/primitives/__tests__/challengers.test.d.ts +2 -0
- package/dist/tbv/core/primitives/__tests__/challengers.test.d.ts.map +1 -0
- package/dist/tbv/core/primitives/challengers.d.ts +23 -0
- package/dist/tbv/core/primitives/challengers.d.ts.map +1 -0
- package/dist/tbv/core/primitives/index.cjs +1 -1
- package/dist/tbv/core/primitives/index.d.ts +9 -3
- package/dist/tbv/core/primitives/index.d.ts.map +1 -1
- package/dist/tbv/core/primitives/index.js +23 -18
- package/dist/tbv/core/primitives/psbt/__tests__/pegin.test.d.ts +1 -1
- package/dist/tbv/core/primitives/psbt/index.d.ts +8 -3
- package/dist/tbv/core/primitives/psbt/index.d.ts.map +1 -1
- package/dist/tbv/core/primitives/psbt/pegin.d.ts +76 -59
- package/dist/tbv/core/primitives/psbt/pegin.d.ts.map +1 -1
- package/dist/tbv/core/primitives/psbt/peginInput.d.ts +69 -0
- package/dist/tbv/core/primitives/psbt/peginInput.d.ts.map +1 -0
- package/dist/tbv/core/primitives/utils/bitcoin.d.ts +11 -0
- package/dist/tbv/core/primitives/utils/bitcoin.d.ts.map +1 -1
- package/dist/tbv/core/primitives/utils/index.d.ts +1 -1
- package/dist/tbv/core/primitives/utils/index.d.ts.map +1 -1
- package/dist/tbv/index.cjs +1 -1
- package/dist/tbv/index.js +65 -60
- package/dist/tbv/integrations/aave/clients/abis/AaveIntegrationController.abi.json.d.ts +16 -1
- package/dist/tbv/integrations/aave/index.cjs +1 -1
- package/dist/tbv/integrations/aave/index.cjs.map +1 -1
- package/dist/tbv/integrations/aave/index.js +3 -3
- package/package.json +20 -20
- package/LICENSE +0 -169
- package/dist/PayoutManager-BkOfdb2_.js.map +0 -1
- package/dist/PayoutManager-DZ7EIcAJ.cjs +0 -2
- package/dist/PayoutManager-DZ7EIcAJ.cjs.map +0 -1
- package/dist/challengeAssert-34HqeVFH.cjs +0 -2
- package/dist/challengeAssert-34HqeVFH.cjs.map +0 -1
- package/dist/challengeAssert-DVErOd8l.js +0 -423
- package/dist/challengeAssert-DVErOd8l.js.map +0 -1
|
@@ -0,0 +1,617 @@
|
|
|
1
|
+
import { createPrePeginTransaction as W, buildPeginTxFromPrePegin as F, getPrePeginHtlcConnectorInfo as K, tapInternalPubkey as k, createPayoutConnector as N, getPeginPayoutScript as q, getAssertNoPayoutScriptInfo as M, getChallengeAssertScriptInfo as z } from "@babylonlabs-io/babylon-tbv-rust-wasm";
|
|
2
|
+
import * as U from "bitcoinjs-lib";
|
|
3
|
+
import { payments as b, networks as T, initEccLib as H, Transaction as y, Psbt as w } from "bitcoinjs-lib";
|
|
4
|
+
import { Buffer as a } from "buffer";
|
|
5
|
+
import * as A from "@bitcoin-js/tiny-secp256k1-asmjs";
|
|
6
|
+
function h(t) {
|
|
7
|
+
return t.startsWith("0x") ? t.slice(2) : t;
|
|
8
|
+
}
|
|
9
|
+
function ut(t) {
|
|
10
|
+
return t.startsWith("0x") ? t : `0x${t}`;
|
|
11
|
+
}
|
|
12
|
+
function d(t) {
|
|
13
|
+
const n = h(t);
|
|
14
|
+
if (!B(n))
|
|
15
|
+
throw new Error(`Invalid hex string: ${t}`);
|
|
16
|
+
const e = new Uint8Array(n.length / 2);
|
|
17
|
+
for (let o = 0; o < n.length; o += 2)
|
|
18
|
+
e[o / 2] = parseInt(n.slice(o, o + 2), 16);
|
|
19
|
+
return e;
|
|
20
|
+
}
|
|
21
|
+
function x(t) {
|
|
22
|
+
return Array.from(t).map((n) => n.toString(16).padStart(2, "0")).join("");
|
|
23
|
+
}
|
|
24
|
+
function D(t) {
|
|
25
|
+
return t.length === 32 ? t : t.slice(1, 33);
|
|
26
|
+
}
|
|
27
|
+
function B(t) {
|
|
28
|
+
return /^[0-9a-fA-F]*$/.test(t) && t.length % 2 === 0;
|
|
29
|
+
}
|
|
30
|
+
function $(t) {
|
|
31
|
+
const n = h(t);
|
|
32
|
+
if (!B(n))
|
|
33
|
+
throw new Error(`Invalid hex characters in public key: ${t}`);
|
|
34
|
+
if (n.length === 64)
|
|
35
|
+
return n;
|
|
36
|
+
if (n.length !== 66 && n.length !== 130)
|
|
37
|
+
throw new Error(
|
|
38
|
+
`Invalid public key length: ${n.length} (expected 64, 66, or 130 hex chars)`
|
|
39
|
+
);
|
|
40
|
+
const e = d(n);
|
|
41
|
+
return x(D(e));
|
|
42
|
+
}
|
|
43
|
+
function at(t) {
|
|
44
|
+
const n = h(t);
|
|
45
|
+
return B(n);
|
|
46
|
+
}
|
|
47
|
+
function lt(t, n) {
|
|
48
|
+
const e = $(t), o = n ?? e;
|
|
49
|
+
if (e.toLowerCase() !== o.toLowerCase())
|
|
50
|
+
throw new Error(
|
|
51
|
+
`Wallet public key does not match vault depositor. Expected: ${o}, Got: ${e}. Please connect the wallet that was used to create this vault.`
|
|
52
|
+
);
|
|
53
|
+
return { walletPubkeyRaw: t, walletPubkeyXOnly: e, depositorPubkey: o };
|
|
54
|
+
}
|
|
55
|
+
let _ = !1;
|
|
56
|
+
function X() {
|
|
57
|
+
_ || (H(A), _ = !0);
|
|
58
|
+
}
|
|
59
|
+
function V(t) {
|
|
60
|
+
switch (t) {
|
|
61
|
+
case "bitcoin":
|
|
62
|
+
return T.bitcoin;
|
|
63
|
+
case "testnet":
|
|
64
|
+
case "signet":
|
|
65
|
+
return T.testnet;
|
|
66
|
+
case "regtest":
|
|
67
|
+
return T.regtest;
|
|
68
|
+
default:
|
|
69
|
+
throw new Error(`Unknown network: ${t}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function Y(t, n) {
|
|
73
|
+
X();
|
|
74
|
+
const e = d($(t)), { address: o } = b.p2tr({
|
|
75
|
+
internalPubkey: a.from(e),
|
|
76
|
+
network: V(n)
|
|
77
|
+
});
|
|
78
|
+
if (!o)
|
|
79
|
+
throw new Error("Failed to derive taproot address from public key");
|
|
80
|
+
return o;
|
|
81
|
+
}
|
|
82
|
+
function Q(t, n) {
|
|
83
|
+
const e = h(t);
|
|
84
|
+
if (e.length !== 66)
|
|
85
|
+
throw new Error(
|
|
86
|
+
`Native SegWit requires a compressed public key (66 hex chars), got ${e.length}`
|
|
87
|
+
);
|
|
88
|
+
const { address: o } = b.p2wpkh({
|
|
89
|
+
pubkey: a.from(d(e)),
|
|
90
|
+
network: V(n)
|
|
91
|
+
});
|
|
92
|
+
if (!o)
|
|
93
|
+
throw new Error(
|
|
94
|
+
"Failed to derive native segwit address from public key"
|
|
95
|
+
);
|
|
96
|
+
return o;
|
|
97
|
+
}
|
|
98
|
+
function pt(t, n, e) {
|
|
99
|
+
const o = h(n);
|
|
100
|
+
try {
|
|
101
|
+
if (t === Y(o, e))
|
|
102
|
+
return !0;
|
|
103
|
+
} catch {
|
|
104
|
+
}
|
|
105
|
+
const r = [];
|
|
106
|
+
o.length === 66 ? r.push(o) : o.length === 64 && r.push(`02${o}`, `03${o}`);
|
|
107
|
+
for (const s of r)
|
|
108
|
+
try {
|
|
109
|
+
if (t === Q(s, e))
|
|
110
|
+
return !0;
|
|
111
|
+
} catch {
|
|
112
|
+
}
|
|
113
|
+
return !1;
|
|
114
|
+
}
|
|
115
|
+
function I(t) {
|
|
116
|
+
return $(t).toLowerCase();
|
|
117
|
+
}
|
|
118
|
+
function ft(t, n, e) {
|
|
119
|
+
const o = /* @__PURE__ */ new Set();
|
|
120
|
+
o.add(I(t));
|
|
121
|
+
for (const r of n)
|
|
122
|
+
o.add(I(r));
|
|
123
|
+
return o.delete(I(e)), o.size;
|
|
124
|
+
}
|
|
125
|
+
const ht = 58, dt = 43, gt = 11, G = 546, Z = BigInt(G), j = 30, J = 2, Pt = 1.1;
|
|
126
|
+
function yt(t) {
|
|
127
|
+
return t <= J ? j : 0;
|
|
128
|
+
}
|
|
129
|
+
const xt = 5;
|
|
130
|
+
function R(t) {
|
|
131
|
+
const e = t.substring(8, 12) === "0001" ? 12 : 8, o = parseInt(
|
|
132
|
+
t.substring(e, e + 2),
|
|
133
|
+
16
|
|
134
|
+
), r = parseInt(
|
|
135
|
+
t.substring(e + 2, e + 4),
|
|
136
|
+
16
|
|
137
|
+
);
|
|
138
|
+
if (o !== 0)
|
|
139
|
+
throw new Error(`Expected 0 inputs from WASM, got ${o}`);
|
|
140
|
+
if (r === 0)
|
|
141
|
+
throw new Error("Expected at least 1 output from WASM, got 0");
|
|
142
|
+
const s = a.from(t.substring(0, 8), "hex").readUInt32LE(0), u = a.from(
|
|
143
|
+
t.substring(t.length - 8),
|
|
144
|
+
"hex"
|
|
145
|
+
).readUInt32LE(0), c = [];
|
|
146
|
+
let i = e + 4;
|
|
147
|
+
for (let p = 0; p < r; p++) {
|
|
148
|
+
const l = t.substring(i, i + 16), f = Number(a.from(l, "hex").readBigUInt64LE(0));
|
|
149
|
+
i += 16;
|
|
150
|
+
const g = parseInt(t.substring(i, i + 2), 16);
|
|
151
|
+
i += 2;
|
|
152
|
+
const P = t.substring(i, i + g * 2), v = a.from(P, "hex");
|
|
153
|
+
i += g * 2, c.push({ value: f, script: v });
|
|
154
|
+
}
|
|
155
|
+
return { version: s, locktime: u, outputs: c };
|
|
156
|
+
}
|
|
157
|
+
function kt(t) {
|
|
158
|
+
const { unfundedTxHex: n, selectedUTXOs: e, changeAddress: o, changeAmount: r, network: s } = t, { version: u, locktime: c, outputs: i } = R(n), p = new U.Transaction();
|
|
159
|
+
p.version = u, p.locktime = c;
|
|
160
|
+
for (const l of e) {
|
|
161
|
+
const f = a.from(l.txid, "hex").reverse();
|
|
162
|
+
p.addInput(f, l.vout);
|
|
163
|
+
}
|
|
164
|
+
for (const l of i)
|
|
165
|
+
p.addOutput(l.script, l.value);
|
|
166
|
+
if (r > Z) {
|
|
167
|
+
const l = U.address.toOutputScript(o, s);
|
|
168
|
+
p.addOutput(l, Number(r));
|
|
169
|
+
}
|
|
170
|
+
return p.toHex();
|
|
171
|
+
}
|
|
172
|
+
async function wt(t) {
|
|
173
|
+
const n = await W({
|
|
174
|
+
depositorPubkey: t.depositorPubkey,
|
|
175
|
+
vaultProviderPubkey: t.vaultProviderPubkey,
|
|
176
|
+
vaultKeeperPubkeys: t.vaultKeeperPubkeys,
|
|
177
|
+
universalChallengerPubkeys: t.universalChallengerPubkeys,
|
|
178
|
+
hashH: t.hashH,
|
|
179
|
+
timelockRefund: t.timelockRefund,
|
|
180
|
+
pegInAmount: t.pegInAmount,
|
|
181
|
+
feeRate: t.feeRate,
|
|
182
|
+
numLocalChallengers: t.numLocalChallengers,
|
|
183
|
+
councilQuorum: t.councilQuorum,
|
|
184
|
+
councilSize: t.councilSize,
|
|
185
|
+
network: t.network
|
|
186
|
+
}), o = R(n.txHex).outputs.reduce(
|
|
187
|
+
(r, s) => r + BigInt(s.value),
|
|
188
|
+
0n
|
|
189
|
+
);
|
|
190
|
+
return {
|
|
191
|
+
psbtHex: n.txHex,
|
|
192
|
+
totalOutputValue: o,
|
|
193
|
+
htlcValue: n.htlcValue,
|
|
194
|
+
htlcScriptPubKey: n.htlcScriptPubKey,
|
|
195
|
+
htlcAddress: n.htlcAddress,
|
|
196
|
+
peginAmount: n.peginAmount,
|
|
197
|
+
depositorClaimValue: n.depositorClaimValue
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
async function vt(t) {
|
|
201
|
+
const n = await F(
|
|
202
|
+
{
|
|
203
|
+
depositorPubkey: t.prePeginParams.depositorPubkey,
|
|
204
|
+
vaultProviderPubkey: t.prePeginParams.vaultProviderPubkey,
|
|
205
|
+
vaultKeeperPubkeys: t.prePeginParams.vaultKeeperPubkeys,
|
|
206
|
+
universalChallengerPubkeys: t.prePeginParams.universalChallengerPubkeys,
|
|
207
|
+
hashH: t.prePeginParams.hashH,
|
|
208
|
+
timelockRefund: t.prePeginParams.timelockRefund,
|
|
209
|
+
pegInAmount: t.prePeginParams.pegInAmount,
|
|
210
|
+
feeRate: t.prePeginParams.feeRate,
|
|
211
|
+
numLocalChallengers: t.prePeginParams.numLocalChallengers,
|
|
212
|
+
councilQuorum: t.prePeginParams.councilQuorum,
|
|
213
|
+
councilSize: t.prePeginParams.councilSize,
|
|
214
|
+
network: t.prePeginParams.network
|
|
215
|
+
},
|
|
216
|
+
t.timelockPegin,
|
|
217
|
+
t.fundedPrePeginTxid
|
|
218
|
+
);
|
|
219
|
+
return {
|
|
220
|
+
txHex: n.txHex,
|
|
221
|
+
txid: n.txid,
|
|
222
|
+
vaultScriptPubKey: n.vaultScriptPubKey,
|
|
223
|
+
vaultValue: n.vaultValue
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
async function bt(t) {
|
|
227
|
+
const n = h(t.peginTxHex), e = h(t.fundedPrePeginTxHex), o = await K({
|
|
228
|
+
depositorPubkey: t.depositorPubkey,
|
|
229
|
+
vaultProviderPubkey: t.vaultProviderPubkey,
|
|
230
|
+
vaultKeeperPubkeys: t.vaultKeeperPubkeys,
|
|
231
|
+
universalChallengerPubkeys: t.universalChallengerPubkeys,
|
|
232
|
+
hashH: t.hashH,
|
|
233
|
+
timelockRefund: t.timelockRefund,
|
|
234
|
+
network: t.network
|
|
235
|
+
}), r = y.fromHex(n), s = y.fromHex(e);
|
|
236
|
+
if (r.ins.length !== 1)
|
|
237
|
+
throw new Error(
|
|
238
|
+
`PegIn transaction must have exactly 1 input, got ${r.ins.length}`
|
|
239
|
+
);
|
|
240
|
+
const u = r.ins[0], c = s.getId(), i = x(
|
|
241
|
+
new Uint8Array(u.hash).slice().reverse()
|
|
242
|
+
);
|
|
243
|
+
if (i !== c)
|
|
244
|
+
throw new Error(
|
|
245
|
+
`PegIn input does not reference the Pre-PegIn transaction. Expected ${c}, got ${i}`
|
|
246
|
+
);
|
|
247
|
+
const p = s.outs[u.index];
|
|
248
|
+
if (!p)
|
|
249
|
+
throw new Error(
|
|
250
|
+
`Pre-PegIn output ${u.index} not found (Pre-PegIn has ${s.outs.length} outputs)`
|
|
251
|
+
);
|
|
252
|
+
const l = d(o.hashlockScript), f = d(o.hashlockControlBlock), g = new w();
|
|
253
|
+
g.setVersion(r.version), g.setLocktime(r.locktime), g.addInput({
|
|
254
|
+
hash: u.hash,
|
|
255
|
+
index: u.index,
|
|
256
|
+
sequence: u.sequence,
|
|
257
|
+
witnessUtxo: {
|
|
258
|
+
script: p.script,
|
|
259
|
+
value: p.value
|
|
260
|
+
},
|
|
261
|
+
tapLeafScript: [
|
|
262
|
+
{
|
|
263
|
+
leafVersion: 192,
|
|
264
|
+
script: a.from(l),
|
|
265
|
+
controlBlock: a.from(f)
|
|
266
|
+
}
|
|
267
|
+
],
|
|
268
|
+
tapInternalKey: a.from(k)
|
|
269
|
+
// sighashType omitted — defaults to SIGHASH_DEFAULT (0x00) for Taproot
|
|
270
|
+
});
|
|
271
|
+
for (const P of r.outs)
|
|
272
|
+
g.addOutput({
|
|
273
|
+
script: P.script,
|
|
274
|
+
value: P.value
|
|
275
|
+
});
|
|
276
|
+
return { psbtHex: g.toHex() };
|
|
277
|
+
}
|
|
278
|
+
function mt(t, n) {
|
|
279
|
+
const o = w.fromHex(t).data.inputs[0];
|
|
280
|
+
if (!o)
|
|
281
|
+
throw new Error("PegIn PSBT has no inputs");
|
|
282
|
+
if (o.tapScriptSig && o.tapScriptSig.length > 0) {
|
|
283
|
+
const r = a.from(
|
|
284
|
+
d(n)
|
|
285
|
+
);
|
|
286
|
+
for (const s of o.tapScriptSig)
|
|
287
|
+
if (s.pubkey.equals(r))
|
|
288
|
+
return tt(s.signature);
|
|
289
|
+
throw new Error(
|
|
290
|
+
`No PegIn input signature found for depositor pubkey: ${n}`
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
throw o.finalScriptWitness && o.finalScriptWitness.length > 0 ? new Error(
|
|
294
|
+
"PegIn input PSBT is already finalized. Cannot reliably extract the depositor signature from the witness stack. Ensure the wallet returns a non-finalized PSBT with tapScriptSig entries."
|
|
295
|
+
) : new Error(
|
|
296
|
+
"No tapScriptSig or finalScriptWitness found in signed PegIn input PSBT"
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
function tt(t) {
|
|
300
|
+
if (t.length === 64)
|
|
301
|
+
return x(new Uint8Array(t));
|
|
302
|
+
if (t.length === 65)
|
|
303
|
+
return x(new Uint8Array(t.subarray(0, 64)));
|
|
304
|
+
throw new Error(`Unexpected PegIn input signature length: ${t.length}`);
|
|
305
|
+
}
|
|
306
|
+
async function et(t) {
|
|
307
|
+
const n = await N(
|
|
308
|
+
{
|
|
309
|
+
depositor: t.depositor,
|
|
310
|
+
vaultProvider: t.vaultProvider,
|
|
311
|
+
vaultKeepers: t.vaultKeepers,
|
|
312
|
+
universalChallengers: t.universalChallengers,
|
|
313
|
+
timelockPegin: t.timelockPegin
|
|
314
|
+
},
|
|
315
|
+
t.network
|
|
316
|
+
);
|
|
317
|
+
return {
|
|
318
|
+
payoutScript: n.payoutScript,
|
|
319
|
+
taprootScriptHash: n.taprootScriptHash,
|
|
320
|
+
scriptPubKey: n.scriptPubKey,
|
|
321
|
+
address: n.address
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
H(A);
|
|
325
|
+
async function St(t) {
|
|
326
|
+
const n = h(t.payoutTxHex), e = h(t.peginTxHex), o = h(t.assertTxHex), r = await et({
|
|
327
|
+
depositor: t.depositorBtcPubkey,
|
|
328
|
+
vaultProvider: t.vaultProviderBtcPubkey,
|
|
329
|
+
vaultKeepers: t.vaultKeeperBtcPubkeys,
|
|
330
|
+
universalChallengers: t.universalChallengerBtcPubkeys,
|
|
331
|
+
timelockPegin: t.timelockPegin,
|
|
332
|
+
network: t.network
|
|
333
|
+
}), s = d(r.payoutScript), u = ot(k, s), c = y.fromHex(n), i = y.fromHex(e), p = y.fromHex(o), l = new w();
|
|
334
|
+
if (l.setVersion(c.version), l.setLocktime(c.locktime), c.ins.length !== 2)
|
|
335
|
+
throw new Error(
|
|
336
|
+
`Payout transaction must have exactly 2 inputs, got ${c.ins.length}`
|
|
337
|
+
);
|
|
338
|
+
const f = c.ins[0], g = c.ins[1], P = x(
|
|
339
|
+
new Uint8Array(f.hash).slice().reverse()
|
|
340
|
+
), v = i.getId();
|
|
341
|
+
if (P !== v)
|
|
342
|
+
throw new Error(
|
|
343
|
+
`Input 0 does not reference pegin transaction. Expected ${v}, got ${P}`
|
|
344
|
+
);
|
|
345
|
+
const m = x(
|
|
346
|
+
new Uint8Array(g.hash).slice().reverse()
|
|
347
|
+
), C = p.getId();
|
|
348
|
+
if (m !== C)
|
|
349
|
+
throw new Error(
|
|
350
|
+
`Input 1 does not reference assert transaction. Expected ${C}, got ${m}`
|
|
351
|
+
);
|
|
352
|
+
const S = i.outs[f.index];
|
|
353
|
+
if (!S)
|
|
354
|
+
throw new Error(
|
|
355
|
+
`Previous output not found for input 0 (txid: ${P}, index: ${f.index})`
|
|
356
|
+
);
|
|
357
|
+
const E = p.outs[g.index];
|
|
358
|
+
if (!E)
|
|
359
|
+
throw new Error(
|
|
360
|
+
`Previous output not found for input 1 (txid: ${m}, index: ${g.index})`
|
|
361
|
+
);
|
|
362
|
+
l.addInput({
|
|
363
|
+
hash: f.hash,
|
|
364
|
+
index: f.index,
|
|
365
|
+
sequence: f.sequence,
|
|
366
|
+
witnessUtxo: {
|
|
367
|
+
script: S.script,
|
|
368
|
+
value: S.value
|
|
369
|
+
},
|
|
370
|
+
tapLeafScript: [
|
|
371
|
+
{
|
|
372
|
+
leafVersion: 192,
|
|
373
|
+
script: a.from(s),
|
|
374
|
+
controlBlock: a.from(u)
|
|
375
|
+
}
|
|
376
|
+
],
|
|
377
|
+
tapInternalKey: a.from(k)
|
|
378
|
+
// sighashType omitted - defaults to SIGHASH_DEFAULT (0x00) for Taproot
|
|
379
|
+
}), l.addInput({
|
|
380
|
+
hash: g.hash,
|
|
381
|
+
index: g.index,
|
|
382
|
+
sequence: g.sequence,
|
|
383
|
+
witnessUtxo: {
|
|
384
|
+
script: E.script,
|
|
385
|
+
value: E.value
|
|
386
|
+
}
|
|
387
|
+
// No tapLeafScript - depositor doesn't sign this input
|
|
388
|
+
});
|
|
389
|
+
for (const L of c.outs)
|
|
390
|
+
l.addOutput({
|
|
391
|
+
script: L.script,
|
|
392
|
+
value: L.value
|
|
393
|
+
});
|
|
394
|
+
return {
|
|
395
|
+
psbtHex: l.toHex()
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
function Et(t, n, e = 0) {
|
|
399
|
+
const o = w.fromHex(t);
|
|
400
|
+
if (e >= o.data.inputs.length)
|
|
401
|
+
throw new Error(
|
|
402
|
+
`Input index ${e} out of range (${o.data.inputs.length} inputs)`
|
|
403
|
+
);
|
|
404
|
+
const r = o.data.inputs[e];
|
|
405
|
+
if (r.tapScriptSig && r.tapScriptSig.length > 0) {
|
|
406
|
+
const s = d(n);
|
|
407
|
+
for (const u of r.tapScriptSig)
|
|
408
|
+
if (u.pubkey.equals(a.from(s)))
|
|
409
|
+
return O(u.signature, e);
|
|
410
|
+
throw new Error(
|
|
411
|
+
`No signature found for depositor pubkey: ${n} at input ${e}`
|
|
412
|
+
);
|
|
413
|
+
}
|
|
414
|
+
if (r.finalScriptWitness && r.finalScriptWitness.length > 0) {
|
|
415
|
+
const s = nt(r.finalScriptWitness);
|
|
416
|
+
if (s.length >= 1)
|
|
417
|
+
return O(s[0], e);
|
|
418
|
+
}
|
|
419
|
+
throw new Error(
|
|
420
|
+
`No tapScriptSig or finalScriptWitness found in signed PSBT at input ${e}`
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
function O(t, n) {
|
|
424
|
+
if (t.length === 64)
|
|
425
|
+
return x(new Uint8Array(t));
|
|
426
|
+
if (t.length === 65)
|
|
427
|
+
return x(new Uint8Array(t.subarray(0, 64)));
|
|
428
|
+
throw new Error(
|
|
429
|
+
`Unexpected signature length at input ${n}: ${t.length}`
|
|
430
|
+
);
|
|
431
|
+
}
|
|
432
|
+
function nt(t) {
|
|
433
|
+
const n = [];
|
|
434
|
+
let e = 0;
|
|
435
|
+
const o = () => {
|
|
436
|
+
const s = t[e++];
|
|
437
|
+
if (s < 253) return s;
|
|
438
|
+
if (s === 253) {
|
|
439
|
+
const u = t[e] | t[e + 1] << 8;
|
|
440
|
+
return e += 2, u;
|
|
441
|
+
}
|
|
442
|
+
if (s === 254) {
|
|
443
|
+
const u = t[e] | t[e + 1] << 8 | t[e + 2] << 16 | t[e + 3] << 24;
|
|
444
|
+
return e += 4, u;
|
|
445
|
+
}
|
|
446
|
+
return e += 8, 0;
|
|
447
|
+
}, r = o();
|
|
448
|
+
for (let s = 0; s < r; s++) {
|
|
449
|
+
const u = o();
|
|
450
|
+
n.push(t.subarray(e, e + u)), e += u;
|
|
451
|
+
}
|
|
452
|
+
return n;
|
|
453
|
+
}
|
|
454
|
+
function ot(t, n) {
|
|
455
|
+
const e = { output: a.from(n) }, r = b.p2tr({
|
|
456
|
+
internalPubkey: a.from(t),
|
|
457
|
+
scriptTree: e
|
|
458
|
+
}).pubkey;
|
|
459
|
+
if (!r)
|
|
460
|
+
throw new Error("Failed to compute output key");
|
|
461
|
+
const s = 192, u = r[0] === 3 ? 1 : 0, c = s | u, i = new Uint8Array(1 + t.length);
|
|
462
|
+
return i[0] = c, i.set(t, 1), i;
|
|
463
|
+
}
|
|
464
|
+
H(A);
|
|
465
|
+
async function Tt(t) {
|
|
466
|
+
const n = h(t.payoutTxHex), e = y.fromHex(n), o = await q(t.connectorParams), r = d(o), s = rt(k, r), u = new w();
|
|
467
|
+
u.setVersion(e.version), u.setLocktime(e.locktime);
|
|
468
|
+
for (let c = 0; c < e.ins.length; c++) {
|
|
469
|
+
const i = e.ins[c], p = t.prevouts[c];
|
|
470
|
+
if (!p)
|
|
471
|
+
throw new Error(`Missing prevout data for input ${c}`);
|
|
472
|
+
const l = {
|
|
473
|
+
hash: i.hash,
|
|
474
|
+
index: i.index,
|
|
475
|
+
sequence: i.sequence,
|
|
476
|
+
witnessUtxo: {
|
|
477
|
+
script: a.from(d(h(p.script_pubkey))),
|
|
478
|
+
value: p.value
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
c === 0 && (l.tapLeafScript = [
|
|
482
|
+
{
|
|
483
|
+
leafVersion: 192,
|
|
484
|
+
script: a.from(r),
|
|
485
|
+
controlBlock: a.from(s)
|
|
486
|
+
}
|
|
487
|
+
], l.tapInternalKey = a.from(k)), u.addInput(l);
|
|
488
|
+
}
|
|
489
|
+
for (const c of e.outs)
|
|
490
|
+
u.addOutput({
|
|
491
|
+
script: c.script,
|
|
492
|
+
value: c.value
|
|
493
|
+
});
|
|
494
|
+
return u.toHex();
|
|
495
|
+
}
|
|
496
|
+
function rt(t, n) {
|
|
497
|
+
const e = { output: a.from(n) }, r = b.p2tr({
|
|
498
|
+
internalPubkey: a.from(t),
|
|
499
|
+
scriptTree: e
|
|
500
|
+
}).pubkey;
|
|
501
|
+
if (!r)
|
|
502
|
+
throw new Error("Failed to compute output key");
|
|
503
|
+
const s = 192, u = r[0] === 3 ? 1 : 0, c = s | u, i = new Uint8Array(1 + t.length);
|
|
504
|
+
return i[0] = c, i.set(t, 1), i;
|
|
505
|
+
}
|
|
506
|
+
async function It(t) {
|
|
507
|
+
const n = h(t.noPayoutTxHex), e = y.fromHex(n), { noPayoutScript: o, noPayoutControlBlock: r } = await M(
|
|
508
|
+
t.connectorParams,
|
|
509
|
+
t.challengerPubkey
|
|
510
|
+
), s = d(o), u = d(r), c = new w();
|
|
511
|
+
c.setVersion(e.version), c.setLocktime(e.locktime);
|
|
512
|
+
for (let i = 0; i < e.ins.length; i++) {
|
|
513
|
+
const p = e.ins[i], l = t.prevouts[i];
|
|
514
|
+
if (!l)
|
|
515
|
+
throw new Error(`Missing prevout data for input ${i}`);
|
|
516
|
+
const f = {
|
|
517
|
+
hash: p.hash,
|
|
518
|
+
index: p.index,
|
|
519
|
+
sequence: p.sequence,
|
|
520
|
+
witnessUtxo: {
|
|
521
|
+
script: a.from(d(h(l.script_pubkey))),
|
|
522
|
+
value: l.value
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
i === 0 && (f.tapLeafScript = [
|
|
526
|
+
{
|
|
527
|
+
leafVersion: 192,
|
|
528
|
+
script: a.from(s),
|
|
529
|
+
controlBlock: a.from(u)
|
|
530
|
+
}
|
|
531
|
+
], f.tapInternalKey = a.from(k)), c.addInput(f);
|
|
532
|
+
}
|
|
533
|
+
for (const i of e.outs)
|
|
534
|
+
c.addOutput({
|
|
535
|
+
script: i.script,
|
|
536
|
+
value: i.value
|
|
537
|
+
});
|
|
538
|
+
return c.toHex();
|
|
539
|
+
}
|
|
540
|
+
async function Ht(t) {
|
|
541
|
+
const n = h(t.challengeAssertTxHex), e = y.fromHex(n);
|
|
542
|
+
if (t.connectorParamsPerInput.length !== e.ins.length)
|
|
543
|
+
throw new Error(
|
|
544
|
+
`Expected ${e.ins.length} connector params, got ${t.connectorParamsPerInput.length}`
|
|
545
|
+
);
|
|
546
|
+
const o = await Promise.all(
|
|
547
|
+
t.connectorParamsPerInput.map((s) => z(s))
|
|
548
|
+
), r = new w();
|
|
549
|
+
r.setVersion(e.version), r.setLocktime(e.locktime);
|
|
550
|
+
for (let s = 0; s < e.ins.length; s++) {
|
|
551
|
+
const u = e.ins[s], c = t.prevouts[s];
|
|
552
|
+
if (!c)
|
|
553
|
+
throw new Error(`Missing prevout data for input ${s}`);
|
|
554
|
+
const { script: i, controlBlock: p } = o[s], l = d(i), f = d(p);
|
|
555
|
+
r.addInput({
|
|
556
|
+
hash: u.hash,
|
|
557
|
+
index: u.index,
|
|
558
|
+
sequence: u.sequence,
|
|
559
|
+
witnessUtxo: {
|
|
560
|
+
script: a.from(d(h(c.script_pubkey))),
|
|
561
|
+
value: c.value
|
|
562
|
+
},
|
|
563
|
+
tapLeafScript: [
|
|
564
|
+
{
|
|
565
|
+
leafVersion: 192,
|
|
566
|
+
script: a.from(l),
|
|
567
|
+
controlBlock: a.from(f)
|
|
568
|
+
}
|
|
569
|
+
],
|
|
570
|
+
tapInternalKey: a.from(k)
|
|
571
|
+
});
|
|
572
|
+
}
|
|
573
|
+
for (const s of e.outs)
|
|
574
|
+
r.addOutput({
|
|
575
|
+
script: s.script,
|
|
576
|
+
value: s.value
|
|
577
|
+
});
|
|
578
|
+
return r.toHex();
|
|
579
|
+
}
|
|
580
|
+
export {
|
|
581
|
+
G as B,
|
|
582
|
+
Z as D,
|
|
583
|
+
Pt as F,
|
|
584
|
+
j as L,
|
|
585
|
+
dt as M,
|
|
586
|
+
ht as P,
|
|
587
|
+
xt as S,
|
|
588
|
+
gt as T,
|
|
589
|
+
J as W,
|
|
590
|
+
vt as a,
|
|
591
|
+
wt as b,
|
|
592
|
+
ft as c,
|
|
593
|
+
bt as d,
|
|
594
|
+
mt as e,
|
|
595
|
+
St as f,
|
|
596
|
+
Et as g,
|
|
597
|
+
Tt as h,
|
|
598
|
+
It as i,
|
|
599
|
+
Ht as j,
|
|
600
|
+
et as k,
|
|
601
|
+
Q as l,
|
|
602
|
+
Y as m,
|
|
603
|
+
d as n,
|
|
604
|
+
pt as o,
|
|
605
|
+
at as p,
|
|
606
|
+
ut as q,
|
|
607
|
+
$ as r,
|
|
608
|
+
h as s,
|
|
609
|
+
D as t,
|
|
610
|
+
x as u,
|
|
611
|
+
lt as v,
|
|
612
|
+
yt as w,
|
|
613
|
+
R as x,
|
|
614
|
+
kt as y,
|
|
615
|
+
V as z
|
|
616
|
+
};
|
|
617
|
+
//# sourceMappingURL=challengeAssert-ZXE00zkT.js.map
|