@babylonlabs-io/ts-sdk 0.74.0 → 0.74.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{PeginManager-E-UHA1un.cjs → PeginManager-DEByBX5R.cjs} +2 -2
- package/dist/{PeginManager-E-UHA1un.cjs.map → PeginManager-DEByBX5R.cjs.map} +1 -1
- package/dist/{PeginManager-C9RPOd0L.js → PeginManager-DNimKlmw.js} +11 -11
- package/dist/{PeginManager-C9RPOd0L.js.map → PeginManager-DNimKlmw.js.map} +1 -1
- package/dist/{bip322Verify-D6sBBUHE.js → bip322Verify-DSco3Yhl.js} +2 -2
- package/dist/{bip322Verify-D6sBBUHE.js.map → bip322Verify-DSco3Yhl.js.map} +1 -1
- package/dist/buildAndBroadcastReclaim-Cqc6Jiqk.cjs +2 -0
- package/dist/{buildAndBroadcastReclaim-U-PyFfDg.cjs.map → buildAndBroadcastReclaim-Cqc6Jiqk.cjs.map} +1 -1
- package/dist/{buildAndBroadcastReclaim-BXclIcu_.js → buildAndBroadcastReclaim-DCGcupg7.js} +6 -6
- package/dist/{buildAndBroadcastReclaim-BXclIcu_.js.map → buildAndBroadcastReclaim-DCGcupg7.js.map} +1 -1
- package/dist/constants-BTZdyOuk.cjs +2 -0
- package/dist/constants-BTZdyOuk.cjs.map +1 -0
- package/dist/constants-D5vORbQE.js +29 -0
- package/dist/constants-D5vORbQE.js.map +1 -0
- package/dist/fundPeginTransaction-54-Rwb2k.cjs +2 -0
- package/dist/fundPeginTransaction-54-Rwb2k.cjs.map +1 -0
- package/dist/fundPeginTransaction-CwhRbCQC.js +88 -0
- package/dist/fundPeginTransaction-CwhRbCQC.js.map +1 -0
- package/dist/{index-Cs7_Evtk.cjs → index-CK1Xvq4K.cjs} +2 -2
- package/dist/{index-Cs7_Evtk.cjs.map → index-CK1Xvq4K.cjs.map} +1 -1
- package/dist/{index-LgmJhdEN.js → index-DQ5bfoT3.js} +2 -2
- package/dist/{index-LgmJhdEN.js.map → index-DQ5bfoT3.js.map} +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +15 -15
- package/dist/noPayout-4RZB2g1e.cjs +2 -0
- package/dist/noPayout-4RZB2g1e.cjs.map +1 -0
- package/dist/noPayout-DiGr4eas.js +361 -0
- package/dist/noPayout-DiGr4eas.js.map +1 -0
- package/dist/{parseFundingOutpoints-B67CoXpF.js → parseFundingOutpoints-BQWvINfm.js} +2 -2
- package/dist/{parseFundingOutpoints-B67CoXpF.js.map → parseFundingOutpoints-BQWvINfm.js.map} +1 -1
- package/dist/pegin-registration-client-CXK3B_D1.js +882 -0
- package/dist/pegin-registration-client-CXK3B_D1.js.map +1 -0
- package/dist/pegin-registration-client-DLsMNm8Z.cjs +2 -0
- package/dist/pegin-registration-client-DLsMNm8Z.cjs.map +1 -0
- package/dist/{primeVpAuth-DBHohVKM.cjs → primeVpAuth-BOGKniTl.cjs} +2 -2
- package/dist/{primeVpAuth-DBHohVKM.cjs.map → primeVpAuth-BOGKniTl.cjs.map} +1 -1
- package/dist/{primeVpAuth-DvaD45q_.js → primeVpAuth-DdfHSC53.js} +4 -4
- package/dist/{primeVpAuth-DvaD45q_.js.map → primeVpAuth-DdfHSC53.js.map} +1 -1
- package/dist/{psbtInputFields-BIxBpPj2.cjs → psbtInputFields-BrV_ZfL_.cjs} +2 -2
- package/dist/{psbtInputFields-BIxBpPj2.cjs.map → psbtInputFields-BrV_ZfL_.cjs.map} +1 -1
- package/dist/{psbtInputFields-B3e05Xe6.js → psbtInputFields-CCVyaQMi.js} +2 -2
- package/dist/{psbtInputFields-B3e05Xe6.js.map → psbtInputFields-CCVyaQMi.js.map} +1 -1
- package/dist/secp256k1-D-H9jmd7.js +1766 -0
- package/dist/secp256k1-D-H9jmd7.js.map +1 -0
- package/dist/secp256k1-DDHQ1_za.cjs +2 -0
- package/dist/secp256k1-DDHQ1_za.cjs.map +1 -0
- package/dist/{sha2-CrlWM5S8.js → sha2-CLpQd0Rd.js} +8 -8
- package/dist/{sha2-CrlWM5S8.js.map → sha2-CLpQd0Rd.js.map} +1 -1
- package/dist/{signPsbtsWithFallback-DMSpIQWH.cjs → signPsbtsWithFallback-DtwzL5AA.cjs} +2 -2
- package/dist/{signPsbtsWithFallback-DMSpIQWH.cjs.map → signPsbtsWithFallback-DtwzL5AA.cjs.map} +1 -1
- package/dist/{signPsbtsWithFallback-0VBALhjS.js → signPsbtsWithFallback-f7QLp0q8.js} +4 -4
- package/dist/{signPsbtsWithFallback-0VBALhjS.js.map → signPsbtsWithFallback-f7QLp0q8.js.map} +1 -1
- package/dist/tbv/core/clients/eth/index.cjs +1 -1
- package/dist/tbv/core/clients/eth/index.js +3 -3
- package/dist/tbv/core/clients/index.cjs +1 -1
- package/dist/tbv/core/clients/index.js +5 -5
- package/dist/tbv/core/clients/vault-provider/status/index.cjs +1 -1
- package/dist/tbv/core/clients/vault-provider/status/index.js +1 -1
- package/dist/tbv/core/index.cjs +1 -1
- package/dist/tbv/core/index.js +15 -15
- package/dist/tbv/core/managers/index.cjs +1 -1
- package/dist/tbv/core/managers/index.js +2 -2
- package/dist/tbv/core/primitives/index.cjs +1 -1
- package/dist/tbv/core/primitives/index.js +4 -4
- package/dist/tbv/core/primitives/psbt/__tests__/assertPeginTxShape.test.d.ts +5 -6
- package/dist/tbv/core/primitives/psbt/__tests__/assertPeginTxShape.test.d.ts.map +1 -1
- package/dist/tbv/core/primitives/psbt/__tests__/assertWasmPeginSizing.test.d.ts +2 -2
- package/dist/tbv/core/primitives/psbt/assertWasmPeginSizing.d.ts +36 -14
- package/dist/tbv/core/primitives/psbt/assertWasmPeginSizing.d.ts.map +1 -1
- package/dist/tbv/core/primitives/psbt/constants.d.ts +39 -0
- package/dist/tbv/core/primitives/psbt/constants.d.ts.map +1 -1
- package/dist/tbv/core/primitives/psbt/pegin.d.ts.map +1 -1
- package/dist/tbv/core/primitives/psbt/peginInput.d.ts.map +1 -1
- package/dist/tbv/core/primitives/psbt/refund.d.ts.map +1 -1
- package/dist/tbv/core/services/index.cjs +1 -1
- package/dist/tbv/core/services/index.js +2 -2
- package/dist/tbv/core/utils/fee/constants.d.ts +4 -4
- package/dist/tbv/core/utils/index.cjs +1 -1
- package/dist/tbv/core/utils/index.js +2 -2
- package/dist/tbv/core/utils/transaction/fundPeginTransaction.d.ts.map +1 -1
- package/dist/tbv/core/vault-secrets/index.js +1 -1
- package/dist/tbv/index.cjs +1 -1
- package/dist/tbv/index.js +15 -15
- package/dist/test/setup.browser.d.ts +2 -0
- package/dist/test/setup.browser.d.ts.map +1 -0
- package/dist/testing/index.js +1 -1
- package/dist/{toRefundInputs-C3oIN0aW.js → toRefundInputs-BNSXIymn.js} +7 -7
- package/dist/{toRefundInputs-C3oIN0aW.js.map → toRefundInputs-BNSXIymn.js.map} +1 -1
- package/dist/{toRefundInputs-BwG6T0PP.cjs → toRefundInputs-meTyor7r.cjs} +2 -2
- package/dist/{toRefundInputs-BwG6T0PP.cjs.map → toRefundInputs-meTyor7r.cjs.map} +1 -1
- package/dist/{types-DSlcXtog.cjs → types-BzSOthHH.cjs} +2 -2
- package/dist/{types-DSlcXtog.cjs.map → types-BzSOthHH.cjs.map} +1 -1
- package/dist/{types-CPDLZm3l.js → types-C5DyrC8F.js} +2 -2
- package/dist/{types-CPDLZm3l.js.map → types-C5DyrC8F.js.map} +1 -1
- package/dist/verifyKeyPathSchnorrSignature-BVtNwgK6.js +360 -0
- package/dist/verifyKeyPathSchnorrSignature-BVtNwgK6.js.map +1 -0
- package/dist/verifyKeyPathSchnorrSignature-BjB8Znly.cjs +2 -0
- package/dist/verifyKeyPathSchnorrSignature-BjB8Znly.cjs.map +1 -0
- package/dist/verifyScriptPathSchnorrSignature-D_epga4j.js +1140 -0
- package/dist/verifyScriptPathSchnorrSignature-D_epga4j.js.map +1 -0
- package/dist/verifyScriptPathSchnorrSignature-DaRqvEgW.cjs +2 -0
- package/dist/verifyScriptPathSchnorrSignature-DaRqvEgW.cjs.map +1 -0
- package/package.json +4 -1
- package/dist/buildAndBroadcastReclaim-U-PyFfDg.cjs +0 -2
- package/dist/constants-Bh38W9mq.cjs +0 -2
- package/dist/constants-Bh38W9mq.cjs.map +0 -1
- package/dist/constants-CPzppOdq.js +0 -17
- package/dist/constants-CPzppOdq.js.map +0 -1
- package/dist/fundPeginTransaction-B2wyjqp0.js +0 -83
- package/dist/fundPeginTransaction-B2wyjqp0.js.map +0 -1
- package/dist/fundPeginTransaction-BLD8FRL9.cjs +0 -2
- package/dist/fundPeginTransaction-BLD8FRL9.cjs.map +0 -1
- package/dist/noPayout-DpGRzidi.js +0 -353
- package/dist/noPayout-DpGRzidi.js.map +0 -1
- package/dist/noPayout-zEifyecV.cjs +0 -2
- package/dist/noPayout-zEifyecV.cjs.map +0 -1
- package/dist/pegin-registration-client-6vLk2itL.cjs +0 -2
- package/dist/pegin-registration-client-6vLk2itL.cjs.map +0 -1
- package/dist/pegin-registration-client-BdRiR8br.js +0 -2635
- package/dist/pegin-registration-client-BdRiR8br.js.map +0 -1
- package/dist/vaultCoreVersion-DYfw3qed.cjs +0 -2
- package/dist/vaultCoreVersion-DYfw3qed.cjs.map +0 -1
- package/dist/vaultCoreVersion-D_fP9P3C.js +0 -10
- package/dist/vaultCoreVersion-D_fP9P3C.js.map +0 -1
- package/dist/verifyKeyPathSchnorrSignature-BedSbH2D.cjs +0 -2
- package/dist/verifyKeyPathSchnorrSignature-BedSbH2D.cjs.map +0 -1
- package/dist/verifyKeyPathSchnorrSignature-BrgUJmV0.js +0 -372
- package/dist/verifyKeyPathSchnorrSignature-BrgUJmV0.js.map +0 -1
- package/dist/verifyScriptPathSchnorrSignature-BfNUSgA7.cjs +0 -2
- package/dist/verifyScriptPathSchnorrSignature-BfNUSgA7.cjs.map +0 -1
- package/dist/verifyScriptPathSchnorrSignature-Ciq_xZhw.js +0 -951
- package/dist/verifyScriptPathSchnorrSignature-Ciq_xZhw.js.map +0 -1
|
@@ -0,0 +1,1140 @@
|
|
|
1
|
+
import { script as _, opcodes as E, payments as R, Transaction as b, crypto as H, Psbt as V } from "bitcoinjs-lib";
|
|
2
|
+
import { computeMinClaimValue as Ee, peginP2aAnchorOutput as ye, computeMinPeginFee as _e, tapInternalPubkey as k, buildPeginTxFromPrePegin as ke, createPrePeginTransaction as Ie, validatePeginP2aAnchor as Ae, createPayoutConnector as Te, computePayoutFeeFloor as Oe, getAssertPayoutScriptInfo as Ve } from "./tbv/core/wasm/index.js";
|
|
3
|
+
import { c as M, p as Le, d as Re } from "./fundPeginTransaction-CwhRbCQC.js";
|
|
4
|
+
import { p as He, s as f, d as $e, m as Ke, T as y, h as x, u as U, j as Ne, S as F } from "./bitcoin-C-TuHb-v.js";
|
|
5
|
+
import { s as Ue } from "./secp256k1-D-H9jmd7.js";
|
|
6
|
+
import { Buffer as c } from "buffer";
|
|
7
|
+
import { P as q, c as W, d as X, e as G, f as Be, g as z, h as Me, i as Fe, j as Y, k as D, l as Q, m as Z, n as j, o as J, M as ee, b as te, p as qe, A as me, B as We, V as Xe, N as oe } from "./constants-D5vORbQE.js";
|
|
8
|
+
import * as Se from "@bitcoin-js/tiny-secp256k1-asmjs";
|
|
9
|
+
import { X as ne } from "./validation-OSC0xTHB.js";
|
|
10
|
+
function C(e) {
|
|
11
|
+
return He(e).toLowerCase();
|
|
12
|
+
}
|
|
13
|
+
function Yt(e, t, o) {
|
|
14
|
+
const n = /* @__PURE__ */ new Set();
|
|
15
|
+
n.add(C(e));
|
|
16
|
+
for (const i of t)
|
|
17
|
+
n.add(C(i));
|
|
18
|
+
return n.delete(C(o)), n.size;
|
|
19
|
+
}
|
|
20
|
+
function Ge(e) {
|
|
21
|
+
const t = C(e.claimerBtcPubkey), o = C(e.depositorBtcPubkey), n = e.vaultKeeperBtcPubkeys.map(C);
|
|
22
|
+
if (t === o) {
|
|
23
|
+
const r = n.filter((u) => u !== o);
|
|
24
|
+
if (r.length === 0)
|
|
25
|
+
throw new Error(
|
|
26
|
+
"Cannot derive localChallengers: vault keeper set is empty (or contains only the depositor)"
|
|
27
|
+
);
|
|
28
|
+
if (new Set(r).size !== r.length)
|
|
29
|
+
throw new Error(
|
|
30
|
+
"Cannot derive localChallengers: duplicate vaultKeeper key — signing context is misconfigured"
|
|
31
|
+
);
|
|
32
|
+
return r;
|
|
33
|
+
}
|
|
34
|
+
const s = [
|
|
35
|
+
.../* @__PURE__ */ new Set([C(e.vaultProviderBtcPubkey), ...n])
|
|
36
|
+
].sort().filter((r) => r !== t);
|
|
37
|
+
if (s.length === 0)
|
|
38
|
+
throw new Error(
|
|
39
|
+
`Cannot derive localChallengers: no vault provider or vault keeper remains after excluding claimer ${t}`
|
|
40
|
+
);
|
|
41
|
+
return s;
|
|
42
|
+
}
|
|
43
|
+
const ze = 32, Ye = 65535, De = 2;
|
|
44
|
+
function K(e, t) {
|
|
45
|
+
const o = f(e).toLowerCase();
|
|
46
|
+
if (!/^[0-9a-f]{64}$/.test(o))
|
|
47
|
+
throw new Error(`${t} must be a 32-byte x-only public key.`);
|
|
48
|
+
try {
|
|
49
|
+
Ue.Point.fromBytes(
|
|
50
|
+
c.concat([
|
|
51
|
+
c.from([De]),
|
|
52
|
+
c.from(o, "hex")
|
|
53
|
+
])
|
|
54
|
+
);
|
|
55
|
+
} catch {
|
|
56
|
+
throw new Error(`${t} is not a secp256k1 x-coordinate.`);
|
|
57
|
+
}
|
|
58
|
+
return o;
|
|
59
|
+
}
|
|
60
|
+
function re(e, t) {
|
|
61
|
+
if (e.length === 0)
|
|
62
|
+
throw new Error(`${t} must not be empty.`);
|
|
63
|
+
const o = e.map((n, i) => K(n, `${t}[${i}]`)).sort();
|
|
64
|
+
if (new Set(o).size !== o.length)
|
|
65
|
+
throw new Error(`${t} must not contain duplicate keys.`);
|
|
66
|
+
return o;
|
|
67
|
+
}
|
|
68
|
+
function ie(e, t) {
|
|
69
|
+
const o = [];
|
|
70
|
+
return e.forEach((n, i) => {
|
|
71
|
+
o.push(
|
|
72
|
+
c.from(n, "hex"),
|
|
73
|
+
i === 0 ? E.OP_CHECKSIG : E.OP_CHECKSIGADD
|
|
74
|
+
);
|
|
75
|
+
}), o.push(
|
|
76
|
+
_.number.encode(e.length),
|
|
77
|
+
t ? E.OP_NUMEQUALVERIFY : E.OP_NUMEQUAL
|
|
78
|
+
), o;
|
|
79
|
+
}
|
|
80
|
+
function Qe(e, t) {
|
|
81
|
+
var g;
|
|
82
|
+
Ke();
|
|
83
|
+
const o = K(
|
|
84
|
+
e.depositorPubkey,
|
|
85
|
+
"depositorPubkey"
|
|
86
|
+
), n = K(
|
|
87
|
+
e.vaultProviderPubkey,
|
|
88
|
+
"vaultProviderPubkey"
|
|
89
|
+
), i = re(
|
|
90
|
+
e.vaultKeeperPubkeys,
|
|
91
|
+
"vaultKeeperPubkeys"
|
|
92
|
+
), s = re(
|
|
93
|
+
e.universalChallengerPubkeys,
|
|
94
|
+
"universalChallengerPubkeys"
|
|
95
|
+
), r = f(t).toLowerCase();
|
|
96
|
+
if (!/^[0-9a-f]{64}$/.test(r))
|
|
97
|
+
throw new Error("hashlock must be 32 bytes.");
|
|
98
|
+
if (!Number.isInteger(e.timelockRefund) || e.timelockRefund < 1 || e.timelockRefund > Ye)
|
|
99
|
+
throw new Error("timelockRefund must be an integer from 1 to 65535.");
|
|
100
|
+
const u = _.compile([
|
|
101
|
+
E.OP_SIZE,
|
|
102
|
+
_.number.encode(ze),
|
|
103
|
+
E.OP_EQUALVERIFY,
|
|
104
|
+
E.OP_SHA256,
|
|
105
|
+
c.from(r, "hex"),
|
|
106
|
+
E.OP_EQUALVERIFY,
|
|
107
|
+
c.from(o, "hex"),
|
|
108
|
+
E.OP_CHECKSIGVERIFY,
|
|
109
|
+
c.from(n, "hex"),
|
|
110
|
+
E.OP_CHECKSIGVERIFY,
|
|
111
|
+
...ie(i, !0),
|
|
112
|
+
...ie(s, !1)
|
|
113
|
+
]), h = _.compile([
|
|
114
|
+
c.from(o, "hex"),
|
|
115
|
+
E.OP_CHECKSIGVERIFY,
|
|
116
|
+
_.number.encode(e.timelockRefund),
|
|
117
|
+
E.OP_CHECKSEQUENCEVERIFY
|
|
118
|
+
]), a = [
|
|
119
|
+
{ output: u, version: y },
|
|
120
|
+
{ output: h, version: y }
|
|
121
|
+
], { hash: d, output: l, witness: P } = R.p2tr({
|
|
122
|
+
internalPubkey: c.from(k),
|
|
123
|
+
scriptTree: a,
|
|
124
|
+
redeem: {
|
|
125
|
+
output: u,
|
|
126
|
+
redeemVersion: y
|
|
127
|
+
}
|
|
128
|
+
}), p = P == null ? void 0 : P[P.length - 1], v = R.p2tr({
|
|
129
|
+
internalPubkey: c.from(k),
|
|
130
|
+
scriptTree: a,
|
|
131
|
+
redeem: {
|
|
132
|
+
output: h,
|
|
133
|
+
redeemVersion: y
|
|
134
|
+
}
|
|
135
|
+
}), w = (g = v.witness) == null ? void 0 : g[v.witness.length - 1];
|
|
136
|
+
if (!d || !l || !p || !w)
|
|
137
|
+
throw new Error("Failed to derive the expected Pre-PegIn HTLC output.");
|
|
138
|
+
return {
|
|
139
|
+
hashlockScript: u,
|
|
140
|
+
hashlockControlBlock: p,
|
|
141
|
+
refundScript: h,
|
|
142
|
+
refundControlBlock: w,
|
|
143
|
+
scriptPubKey: l,
|
|
144
|
+
tapMerkleRoot: d
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
async function Ze(e, t) {
|
|
148
|
+
const o = t.pegInAmounts.length;
|
|
149
|
+
if (t.hashlocks.length !== o)
|
|
150
|
+
throw new Error(
|
|
151
|
+
`Pre-PegIn has ${t.hashlocks.length} hashlock(s), expected ${o} (one per requested deposit).`
|
|
152
|
+
);
|
|
153
|
+
if (e.htlcValues.length !== o)
|
|
154
|
+
throw new Error(
|
|
155
|
+
`WASM Pre-PegIn returned ${e.htlcValues.length} HTLC value(s), expected ${o} (one per requested deposit).`
|
|
156
|
+
);
|
|
157
|
+
if (e.peginAmounts.length !== o || e.htlcScriptPubKeys.length !== o || e.htlcAddresses.length !== o)
|
|
158
|
+
throw new Error(
|
|
159
|
+
`WASM Pre-PegIn returned mismatched array lengths (htlcValues=${e.htlcValues.length}, peginAmounts=${e.peginAmounts.length}, htlcScriptPubKeys=${e.htlcScriptPubKeys.length}, htlcAddresses=${e.htlcAddresses.length}); expected ${o} each.`
|
|
160
|
+
);
|
|
161
|
+
if (e.depositorClaimValue <= 0n)
|
|
162
|
+
throw new Error(
|
|
163
|
+
`WASM Pre-PegIn returned non-positive depositorClaimValue ${e.depositorClaimValue}; expected > 0.`
|
|
164
|
+
);
|
|
165
|
+
const n = await Ee(
|
|
166
|
+
// Must price the same graph version the builder constructed.
|
|
167
|
+
t.vaultCoreVersion,
|
|
168
|
+
t.numLocalChallengers,
|
|
169
|
+
t.universalChallengerPubkeys.length,
|
|
170
|
+
t.councilQuorum,
|
|
171
|
+
t.councilSize,
|
|
172
|
+
t.feeRate
|
|
173
|
+
);
|
|
174
|
+
if (e.depositorClaimValue !== n)
|
|
175
|
+
throw new Error(
|
|
176
|
+
`WASM Pre-PegIn depositorClaimValue ${e.depositorClaimValue} does not match the independently computed minimum claim value ${n} (vaultCoreVersion=${t.vaultCoreVersion}, numLocalChallengers=${t.numLocalChallengers}, numUniversalChallengers=${t.universalChallengerPubkeys.length}, councilQuorum=${t.councilQuorum}, councilSize=${t.councilSize}, feeRate=${t.feeRate}).`
|
|
177
|
+
);
|
|
178
|
+
const i = await ye(t.vaultCoreVersion), s = (i == null ? void 0 : i.value) ?? 0n, r = await _e(
|
|
179
|
+
t.vaultCoreVersion,
|
|
180
|
+
t.vaultKeeperPubkeys.length,
|
|
181
|
+
t.universalChallengerPubkeys.length,
|
|
182
|
+
t.minPeginFeeRate
|
|
183
|
+
), u = r + s, h = t.minPeginFeeRate * M;
|
|
184
|
+
for (let a = 0; a < o; a++) {
|
|
185
|
+
const d = t.pegInAmounts[a], l = e.peginAmounts[a], P = e.htlcValues[a];
|
|
186
|
+
if (l !== d)
|
|
187
|
+
throw new Error(
|
|
188
|
+
`WASM Pre-PegIn peginAmount[${a}] ${l} does not match the requested amount ${d}; refusing to build a tx whose recorded amount differs from the depositor's request.`
|
|
189
|
+
);
|
|
190
|
+
if (l <= 0n)
|
|
191
|
+
throw new Error(
|
|
192
|
+
`WASM Pre-PegIn peginAmount[${a}] is non-positive (${l}); expected > 0.`
|
|
193
|
+
);
|
|
194
|
+
if (P <= 0n)
|
|
195
|
+
throw new Error(
|
|
196
|
+
`WASM Pre-PegIn htlcValue[${a}] is non-positive (${P}); expected > 0.`
|
|
197
|
+
);
|
|
198
|
+
const p = P - l - e.depositorClaimValue;
|
|
199
|
+
if (p <= 0n)
|
|
200
|
+
throw new Error(
|
|
201
|
+
`WASM Pre-PegIn htlcValue[${a}] ${P} does not strictly cover peginAmount ${l} + depositorClaimValue ${e.depositorClaimValue} + a PegIn reserve (implied reserve ${p}).`
|
|
202
|
+
);
|
|
203
|
+
if (p > h)
|
|
204
|
+
throw new Error(
|
|
205
|
+
`WASM Pre-PegIn implied reserve for HTLC[${a}] (${p} sat) exceeds the plausibility cap ${h} sat (minPeginFeeRate=${t.minPeginFeeRate} × ${M} vbytes); htlcValue ${P} appears grossly inflated.`
|
|
206
|
+
);
|
|
207
|
+
if (p !== u)
|
|
208
|
+
throw new Error(
|
|
209
|
+
`WASM Pre-PegIn htlcValue[${a}] ${P} implies a PegIn fee+anchor reserve of ${p} sat, expected exactly ${u} sat (minPeginFee ${r} + p2aAnchor ${s} for vaultCoreVersion ${t.vaultCoreVersion}, vaultKeepers=${t.vaultKeeperPubkeys.length}, universalChallengers=${t.universalChallengerPubkeys.length}, minPeginFeeRate=${t.minPeginFeeRate}).`
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
return r;
|
|
213
|
+
}
|
|
214
|
+
class O extends Error {
|
|
215
|
+
constructor(t) {
|
|
216
|
+
super(t), this.name = "HtlcOutputMismatchError";
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
function je(e, t, o) {
|
|
220
|
+
if (e.length < t.length)
|
|
221
|
+
throw new O(
|
|
222
|
+
`Encoded Pre-PegIn tx has ${e.length} output(s), fewer than the ${t.length} HTLC output(s) the cross-check validated.`
|
|
223
|
+
);
|
|
224
|
+
for (let n = 0; n < t.length; n++) {
|
|
225
|
+
const i = BigInt(e[n].value);
|
|
226
|
+
if (i !== t[n])
|
|
227
|
+
throw new O(
|
|
228
|
+
`Encoded Pre-PegIn HTLC output[${n}] value ${i} does not match the cross-checked htlcValue ${t[n]}; the funded/signed tx would not pay the validated amount.`
|
|
229
|
+
);
|
|
230
|
+
const s = e[n].script.toString("hex").toLowerCase(), r = o[n].toLowerCase();
|
|
231
|
+
if (s !== r)
|
|
232
|
+
throw new O(
|
|
233
|
+
`Encoded Pre-PegIn HTLC output[${n}] scriptPubKey ${s} does not match the cross-checked htlcScriptPubKey ${r}.`
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
function Je(e, t, o, n, i, s, r) {
|
|
238
|
+
if (s !== q)
|
|
239
|
+
throw new Error(
|
|
240
|
+
`WASM Pre-PegIn transaction version ${s}; expected ${q}.`
|
|
241
|
+
);
|
|
242
|
+
if (r !== W)
|
|
243
|
+
throw new Error(
|
|
244
|
+
`WASM Pre-PegIn transaction locktime ${r}; expected ${W}.`
|
|
245
|
+
);
|
|
246
|
+
if (t.length < X)
|
|
247
|
+
throw new Error(
|
|
248
|
+
`WASM Pre-PegIn output layout has ${t.length} HTLC output(s); expected at least ${X}.`
|
|
249
|
+
);
|
|
250
|
+
const u = t.length, h = u + +(i !== void 0), a = Le(
|
|
251
|
+
t.length,
|
|
252
|
+
i !== void 0
|
|
253
|
+
);
|
|
254
|
+
if (e.length !== a)
|
|
255
|
+
throw new Error(
|
|
256
|
+
`WASM Pre-PegIn output layout has ${e.length} output(s); expected exactly ${a}.`
|
|
257
|
+
);
|
|
258
|
+
je(e, t, o);
|
|
259
|
+
for (let p = 0; p < t.length; p++) {
|
|
260
|
+
const v = Qe(
|
|
261
|
+
n,
|
|
262
|
+
n.hashlocks[p]
|
|
263
|
+
).scriptPubKey;
|
|
264
|
+
if (!e[p].script.equals(v))
|
|
265
|
+
throw new O(
|
|
266
|
+
`Pre-PegIn HTLC output[${p}] does not match the independently derived scriptPubKey.`
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
if (i !== void 0) {
|
|
270
|
+
const p = e[u];
|
|
271
|
+
if (p.value !== G)
|
|
272
|
+
throw new Error(
|
|
273
|
+
`WASM Pre-PegIn auth output[${u}] value ${p.value} sat; expected exactly ${G} sat.`
|
|
274
|
+
);
|
|
275
|
+
const v = p.script.toString("hex").toLowerCase(), w = Be + i.toLowerCase();
|
|
276
|
+
if (v !== w)
|
|
277
|
+
throw new Error(
|
|
278
|
+
`WASM Pre-PegIn auth output[${u}] scriptPubKey ${v}; expected ${w}.`
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
const d = e[h];
|
|
282
|
+
if (d.value !== z)
|
|
283
|
+
throw new Error(
|
|
284
|
+
`WASM Pre-PegIn CPFP output[${h}] value ${d.value} sat; expected exactly ${z} sat.`
|
|
285
|
+
);
|
|
286
|
+
const l = d.script.toString("hex").toLowerCase(), P = f(
|
|
287
|
+
$e(n.depositorPubkey)
|
|
288
|
+
).toLowerCase();
|
|
289
|
+
if (l !== P)
|
|
290
|
+
throw new Error(
|
|
291
|
+
`WASM Pre-PegIn CPFP output[${h}] scriptPubKey ${l}; expected depositor BIP-86 script ${P}.`
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
const et = 1;
|
|
295
|
+
function tt(e) {
|
|
296
|
+
const t = f(e);
|
|
297
|
+
if (!/^[0-9a-fA-F]{64}$/.test(t))
|
|
298
|
+
throw new Error(
|
|
299
|
+
"Invalid depositor pubkey for the depositor-claim script: must be an x-only key of 64 hex characters (32 bytes)."
|
|
300
|
+
);
|
|
301
|
+
const o = _.compile([
|
|
302
|
+
c.from(x(t)),
|
|
303
|
+
E.OP_CHECKSIG
|
|
304
|
+
]), n = c.from(k), i = { output: o, redeemVersion: y }, { output: s, witness: r } = R.p2tr({
|
|
305
|
+
internalPubkey: n,
|
|
306
|
+
scriptTree: { output: o },
|
|
307
|
+
redeem: i
|
|
308
|
+
});
|
|
309
|
+
if (!s)
|
|
310
|
+
throw new Error(
|
|
311
|
+
"Failed to derive the depositor-claim P2TR scriptPubKey for PegIn output validation"
|
|
312
|
+
);
|
|
313
|
+
const u = r == null ? void 0 : r[r.length - 1];
|
|
314
|
+
if (!u)
|
|
315
|
+
throw new Error(
|
|
316
|
+
"Failed to derive the depositor-claim tapleaf control block; refusing to build an unspendable input"
|
|
317
|
+
);
|
|
318
|
+
return { scriptPubKey: s, leafScript: o, controlBlock: u, internalKey: n };
|
|
319
|
+
}
|
|
320
|
+
function ot(e) {
|
|
321
|
+
return tt(e).scriptPubKey;
|
|
322
|
+
}
|
|
323
|
+
const se = 64, nt = /^[0-9a-fA-F]+$/;
|
|
324
|
+
async function Dt(e) {
|
|
325
|
+
const t = xe(e.authAnchorHash), o = await Ie({
|
|
326
|
+
txGraphVersion: e.vaultCoreVersion,
|
|
327
|
+
depositorPubkey: e.depositorPubkey,
|
|
328
|
+
vaultProviderPubkey: e.vaultProviderPubkey,
|
|
329
|
+
vaultKeeperPubkeys: e.vaultKeeperPubkeys,
|
|
330
|
+
universalChallengerPubkeys: e.universalChallengerPubkeys,
|
|
331
|
+
hashlocks: [...e.hashlocks],
|
|
332
|
+
timelockRefund: e.timelockRefund,
|
|
333
|
+
pegInAmounts: [...e.pegInAmounts],
|
|
334
|
+
feeRate: e.feeRate,
|
|
335
|
+
minPeginFeeRate: e.minPeginFeeRate,
|
|
336
|
+
numLocalChallengers: e.numLocalChallengers,
|
|
337
|
+
councilQuorum: e.councilQuorum,
|
|
338
|
+
councilSize: e.councilSize,
|
|
339
|
+
network: e.network,
|
|
340
|
+
authAnchorHash: t
|
|
341
|
+
}), n = await Ze(o, e), i = Re(o.txHex);
|
|
342
|
+
Je(
|
|
343
|
+
i.outputs,
|
|
344
|
+
o.htlcValues,
|
|
345
|
+
o.htlcScriptPubKeys,
|
|
346
|
+
e,
|
|
347
|
+
t,
|
|
348
|
+
i.version,
|
|
349
|
+
i.locktime
|
|
350
|
+
);
|
|
351
|
+
const s = i.outputs.reduce(
|
|
352
|
+
(u, h) => u + BigInt(h.value),
|
|
353
|
+
0n
|
|
354
|
+
), r = t !== void 0 ? o.htlcValues.length : null;
|
|
355
|
+
return {
|
|
356
|
+
psbtHex: o.txHex,
|
|
357
|
+
totalOutputValue: s,
|
|
358
|
+
htlcValues: o.htlcValues,
|
|
359
|
+
htlcScriptPubKeys: o.htlcScriptPubKeys,
|
|
360
|
+
htlcAddresses: o.htlcAddresses,
|
|
361
|
+
peginAmounts: o.peginAmounts,
|
|
362
|
+
depositorClaimValue: o.depositorClaimValue,
|
|
363
|
+
authAnchorVout: r,
|
|
364
|
+
minPeginFee: n
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
function xe(e) {
|
|
368
|
+
if (e === void 0) return;
|
|
369
|
+
const t = e.startsWith("0x") || e.startsWith("0X") ? e.slice(2) : e;
|
|
370
|
+
if (t.length !== se || !nt.test(t))
|
|
371
|
+
throw new Error(
|
|
372
|
+
`authAnchorHash must be 32-byte hex (${se} chars, no 0x prefix); got length ${t.length}`
|
|
373
|
+
);
|
|
374
|
+
return t.toLowerCase();
|
|
375
|
+
}
|
|
376
|
+
async function Qt(e) {
|
|
377
|
+
const t = await ke(
|
|
378
|
+
{
|
|
379
|
+
txGraphVersion: e.prePeginParams.vaultCoreVersion,
|
|
380
|
+
depositorPubkey: e.prePeginParams.depositorPubkey,
|
|
381
|
+
vaultProviderPubkey: e.prePeginParams.vaultProviderPubkey,
|
|
382
|
+
vaultKeeperPubkeys: e.prePeginParams.vaultKeeperPubkeys,
|
|
383
|
+
universalChallengerPubkeys: e.prePeginParams.universalChallengerPubkeys,
|
|
384
|
+
hashlocks: [...e.prePeginParams.hashlocks],
|
|
385
|
+
timelockRefund: e.prePeginParams.timelockRefund,
|
|
386
|
+
pegInAmounts: [...e.prePeginParams.pegInAmounts],
|
|
387
|
+
feeRate: e.prePeginParams.feeRate,
|
|
388
|
+
minPeginFeeRate: e.prePeginParams.minPeginFeeRate,
|
|
389
|
+
numLocalChallengers: e.prePeginParams.numLocalChallengers,
|
|
390
|
+
councilQuorum: e.prePeginParams.councilQuorum,
|
|
391
|
+
councilSize: e.prePeginParams.councilSize,
|
|
392
|
+
network: e.prePeginParams.network,
|
|
393
|
+
authAnchorHash: xe(
|
|
394
|
+
e.prePeginParams.authAnchorHash
|
|
395
|
+
)
|
|
396
|
+
},
|
|
397
|
+
e.timelockPegin,
|
|
398
|
+
e.fundedPrePeginTxHex,
|
|
399
|
+
e.htlcVout
|
|
400
|
+
);
|
|
401
|
+
return await it(t, e), {
|
|
402
|
+
txHex: t.txHex,
|
|
403
|
+
txid: t.txid,
|
|
404
|
+
vaultScriptPubKey: t.vaultScriptPubKey,
|
|
405
|
+
vaultValue: t.vaultValue
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
const rt = 2;
|
|
409
|
+
async function it(e, t) {
|
|
410
|
+
const o = t.prePeginParams.vaultCoreVersion, n = o === 1 ? Me : o === 2 || o === 3 ? Fe : void 0;
|
|
411
|
+
if (n === void 0)
|
|
412
|
+
throw new Error(`Unsupported vaultCoreVersion ${o} for PegIn tx.`);
|
|
413
|
+
await Ae(o, e.txHex);
|
|
414
|
+
const i = await ye(o), s = rt + (i ? 1 : 0), r = b.fromHex(f(e.txHex));
|
|
415
|
+
if (r.version !== n)
|
|
416
|
+
throw new Error(
|
|
417
|
+
`PegIn tx version ${r.version} does not match vaultCoreVersion ${o}; expected ${n}.`
|
|
418
|
+
);
|
|
419
|
+
if (r.locktime !== Y)
|
|
420
|
+
throw new Error(
|
|
421
|
+
`PegIn tx locktime ${r.locktime} does not match the canonical locktime ${Y}.`
|
|
422
|
+
);
|
|
423
|
+
if (r.ins.length !== 1)
|
|
424
|
+
throw new Error(
|
|
425
|
+
`PegIn tx has ${r.ins.length} input(s), expected exactly 1 (the Pre-PegIn HTLC outpoint).`
|
|
426
|
+
);
|
|
427
|
+
const u = b.fromHex(
|
|
428
|
+
f(t.fundedPrePeginTxHex)
|
|
429
|
+
).getId(), h = U(
|
|
430
|
+
new Uint8Array(r.ins[0].hash).slice().reverse()
|
|
431
|
+
);
|
|
432
|
+
if (h !== u)
|
|
433
|
+
throw new Error(
|
|
434
|
+
`PegIn input spends txid ${h}, expected the funded Pre-PegIn ${u}.`
|
|
435
|
+
);
|
|
436
|
+
if (r.ins[0].index !== t.htlcVout)
|
|
437
|
+
throw new Error(
|
|
438
|
+
`PegIn input spends Pre-PegIn output ${r.ins[0].index}, expected the requested HTLC vout ${t.htlcVout}.`
|
|
439
|
+
);
|
|
440
|
+
if (r.ins[0].sequence !== D)
|
|
441
|
+
throw new Error(
|
|
442
|
+
`PegIn input sequence ${r.ins[0].sequence} does not match the canonical sequence ${D}.`
|
|
443
|
+
);
|
|
444
|
+
if (r.ins[0].script.length !== 0)
|
|
445
|
+
throw new Error("PegIn input scriptSig must be empty.");
|
|
446
|
+
if (r.ins[0].witness.length !== 0)
|
|
447
|
+
throw new Error("PegIn input witness must be empty before signing.");
|
|
448
|
+
if (r.outs.length !== s)
|
|
449
|
+
throw new Error(
|
|
450
|
+
`PegIn tx has ${r.outs.length} output(s), expected exactly ${s} for vaultCoreVersion ${o} (vault + depositor claim${i ? " + P2A anchor" : ""}).`
|
|
451
|
+
);
|
|
452
|
+
const a = t.prePeginParams.pegInAmounts[t.htlcVout];
|
|
453
|
+
if (e.vaultValue !== a)
|
|
454
|
+
throw new Error(
|
|
455
|
+
`PegIn vault output value ${e.vaultValue} does not match the requested peg-in amount ${a} for HTLC ${t.htlcVout}; refusing to sign a vault that locks a different amount than requested.`
|
|
456
|
+
);
|
|
457
|
+
const d = r.outs[0];
|
|
458
|
+
if (BigInt(d.value) !== e.vaultValue)
|
|
459
|
+
throw new Error(
|
|
460
|
+
`Encoded PegIn vault output value ${d.value} does not match the WASM-reported vaultValue ${e.vaultValue}.`
|
|
461
|
+
);
|
|
462
|
+
const l = d.script.toString("hex"), P = f(
|
|
463
|
+
e.vaultScriptPubKey
|
|
464
|
+
).toLowerCase();
|
|
465
|
+
if (l.toLowerCase() !== P)
|
|
466
|
+
throw new Error(
|
|
467
|
+
`Encoded PegIn vault output scriptPubKey ${l} does not match the WASM-reported vaultScriptPubKey ${e.vaultScriptPubKey}.`
|
|
468
|
+
);
|
|
469
|
+
const p = r.getId();
|
|
470
|
+
if (p !== f(e.txid).toLowerCase())
|
|
471
|
+
throw new Error(
|
|
472
|
+
`Encoded PegIn txid ${p} does not match the WASM-reported txid ${e.txid}.`
|
|
473
|
+
);
|
|
474
|
+
const v = await Ee(
|
|
475
|
+
o,
|
|
476
|
+
t.prePeginParams.numLocalChallengers,
|
|
477
|
+
t.prePeginParams.universalChallengerPubkeys.length,
|
|
478
|
+
t.prePeginParams.councilQuorum,
|
|
479
|
+
t.prePeginParams.councilSize,
|
|
480
|
+
t.prePeginParams.feeRate
|
|
481
|
+
), w = r.outs[et];
|
|
482
|
+
if (BigInt(w.value) !== v)
|
|
483
|
+
throw new Error(
|
|
484
|
+
`Encoded PegIn depositor-claim output value ${w.value} does not match the independently computed claim value ${v} for vaultCoreVersion ${o}.`
|
|
485
|
+
);
|
|
486
|
+
const g = ot(
|
|
487
|
+
t.prePeginParams.depositorPubkey
|
|
488
|
+
);
|
|
489
|
+
if (!w.script.equals(g))
|
|
490
|
+
throw new Error(
|
|
491
|
+
`Encoded PegIn depositor-claim output scriptPubKey ${w.script.toString("hex")} does not pay to the depositor's claim script (expected ${g.toString("hex")}).`
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
async function st(e) {
|
|
495
|
+
const t = await Te(
|
|
496
|
+
{
|
|
497
|
+
txGraphVersion: e.vaultCoreVersion,
|
|
498
|
+
depositor: e.depositor,
|
|
499
|
+
vaultProvider: e.vaultProvider,
|
|
500
|
+
vaultKeepers: e.vaultKeepers,
|
|
501
|
+
universalChallengers: e.universalChallengers,
|
|
502
|
+
timelockPegin: e.timelockPegin
|
|
503
|
+
},
|
|
504
|
+
e.network
|
|
505
|
+
);
|
|
506
|
+
return {
|
|
507
|
+
payoutScript: t.payoutScript,
|
|
508
|
+
taprootScriptHash: t.taprootScriptHash,
|
|
509
|
+
scriptPubKey: t.scriptPubKey,
|
|
510
|
+
address: t.address,
|
|
511
|
+
payoutControlBlock: t.payoutControlBlock
|
|
512
|
+
};
|
|
513
|
+
}
|
|
514
|
+
const ue = 253, ut = 254, ct = 65535, at = 4294967295, lt = "TapLeaf", ht = "TapBranch", dt = "TapTweak", A = 33, T = 32, ce = 128, pt = 1, Pt = 254, ft = c.from([81, 32]);
|
|
515
|
+
function gt(e) {
|
|
516
|
+
if (e < ue)
|
|
517
|
+
return c.from([e]);
|
|
518
|
+
if (e <= ct) {
|
|
519
|
+
const t = c.alloc(2);
|
|
520
|
+
return t.writeUInt16LE(e), c.concat([c.from([ue]), t]);
|
|
521
|
+
}
|
|
522
|
+
if (e <= at) {
|
|
523
|
+
const t = c.alloc(4);
|
|
524
|
+
return t.writeUInt32LE(e), c.concat([c.from([ut]), t]);
|
|
525
|
+
}
|
|
526
|
+
throw new Error(`Script too large to encode as CompactSize: ${e} bytes`);
|
|
527
|
+
}
|
|
528
|
+
function Ce(e, t) {
|
|
529
|
+
const o = c.concat([
|
|
530
|
+
c.from([e]),
|
|
531
|
+
gt(t.length),
|
|
532
|
+
c.from(t)
|
|
533
|
+
]);
|
|
534
|
+
return H.taggedHash(lt, o);
|
|
535
|
+
}
|
|
536
|
+
function wt(e) {
|
|
537
|
+
const { leafVersion: t, script: o, controlBlock: n } = e, i = n.length - A;
|
|
538
|
+
if (i < 0 || i % T !== 0 || i / T > ce)
|
|
539
|
+
throw new Error(
|
|
540
|
+
`Malformed Taproot control block: length ${n.length} must be ${A} + 32*m with 0 <= m <= ${ce}`
|
|
541
|
+
);
|
|
542
|
+
const s = n[0] & Pt;
|
|
543
|
+
if (s !== t)
|
|
544
|
+
throw new Error(
|
|
545
|
+
`Taproot control block leaf version 0x${s.toString(16)} does not match the tapLeafScript leaf version 0x${t.toString(16)}`
|
|
546
|
+
);
|
|
547
|
+
const r = c.from(
|
|
548
|
+
n.subarray(1, A)
|
|
549
|
+
);
|
|
550
|
+
let u = Ce(t, o);
|
|
551
|
+
for (let l = A; l < n.length; l += T) {
|
|
552
|
+
const P = c.from(
|
|
553
|
+
n.subarray(l, l + T)
|
|
554
|
+
);
|
|
555
|
+
u = H.taggedHash(
|
|
556
|
+
ht,
|
|
557
|
+
c.compare(u, P) <= 0 ? c.concat([u, P]) : c.concat([P, u])
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
const h = H.taggedHash(
|
|
561
|
+
dt,
|
|
562
|
+
c.concat([r, u])
|
|
563
|
+
), a = Se.xOnlyPointAddTweak(r, h);
|
|
564
|
+
if (a === null)
|
|
565
|
+
throw new Error(
|
|
566
|
+
"Taproot control block does not yield a valid output key (tweak is off-curve)"
|
|
567
|
+
);
|
|
568
|
+
const d = n[0] & pt;
|
|
569
|
+
if (a.parity !== d)
|
|
570
|
+
throw new Error(
|
|
571
|
+
`Taproot output key parity ${a.parity} contradicts the control block's parity bit ${d}`
|
|
572
|
+
);
|
|
573
|
+
return c.concat([
|
|
574
|
+
ft,
|
|
575
|
+
c.from(a.xOnlyPubkey)
|
|
576
|
+
]);
|
|
577
|
+
}
|
|
578
|
+
const vt = 256, Et = 1500, ae = 256, le = 0xffffffffn, yt = 500, $t = 55, mt = 34;
|
|
579
|
+
function St(e) {
|
|
580
|
+
if (typeof e.protocolFeeRate != "bigint" || e.protocolFeeRate <= 0n || e.protocolFeeRate > le)
|
|
581
|
+
throw new Error(
|
|
582
|
+
`protocolFeeRate must be in [1, ${le}] sat/vB, got ${e.protocolFeeRate}`
|
|
583
|
+
);
|
|
584
|
+
for (const [t, o, n, i] of [
|
|
585
|
+
[
|
|
586
|
+
"keepers",
|
|
587
|
+
e.numVaultKeepers,
|
|
588
|
+
vt,
|
|
589
|
+
"fee-floor model limit"
|
|
590
|
+
],
|
|
591
|
+
[
|
|
592
|
+
"challengers",
|
|
593
|
+
e.numUniversalChallengers,
|
|
594
|
+
Et,
|
|
595
|
+
"protocol maximum"
|
|
596
|
+
]
|
|
597
|
+
])
|
|
598
|
+
if (!Number.isInteger(o) || o < 1 || o > n)
|
|
599
|
+
throw new Error(
|
|
600
|
+
`Participant count for ${t} (${o}) is outside the supported range [1, ${n}]${o > n ? ` (${i})` : ""}.`
|
|
601
|
+
);
|
|
602
|
+
if (!Number.isInteger(e.councilSize) || e.councilSize < 1 || e.councilSize > ae)
|
|
603
|
+
throw new Error(
|
|
604
|
+
`councilSize must be an integer in [1, ${ae}], got ${e.councilSize}`
|
|
605
|
+
);
|
|
606
|
+
}
|
|
607
|
+
async function xt(e, t) {
|
|
608
|
+
const { implicitFeeSats: o, out0Len: n, out1Len: i } = t, s = e.numVaultKeepers + e.numUniversalChallengers, r = BigInt(o), u = Math.max(0, n - mt), h = yt + $t * s + u, a = e.protocolFeeRate * BigInt(h);
|
|
609
|
+
if (r > a)
|
|
610
|
+
throw new Error(
|
|
611
|
+
`Payout implicit fee ${o} sats exceeds the safety cap of ${a} sats (${e.protocolFeeRate} sat/vB x ${h} vB for ${s} participants); refusing to sign payout.`
|
|
612
|
+
);
|
|
613
|
+
const d = await Oe(
|
|
614
|
+
e.vaultCoreVersion,
|
|
615
|
+
e.numVaultKeepers,
|
|
616
|
+
e.numUniversalChallengers,
|
|
617
|
+
e.numVaultKeepers,
|
|
618
|
+
e.councilSize,
|
|
619
|
+
n,
|
|
620
|
+
i,
|
|
621
|
+
e.protocolFeeRate
|
|
622
|
+
);
|
|
623
|
+
if (r < d)
|
|
624
|
+
throw new Error(
|
|
625
|
+
`Payout implicit fee ${o} sats is below the floor of ${d} sats (the smallest fee any known vault-provider build produces for this shape); refusing to sign payout.`
|
|
626
|
+
);
|
|
627
|
+
}
|
|
628
|
+
const Ct = 22, bt = 34, _t = 34, kt = 25, It = 23;
|
|
629
|
+
function At(e) {
|
|
630
|
+
return e.length === Ct * 2 && e.startsWith("0014");
|
|
631
|
+
}
|
|
632
|
+
function Tt(e) {
|
|
633
|
+
return e.length === bt * 2 && e.startsWith("0020");
|
|
634
|
+
}
|
|
635
|
+
function Ot(e) {
|
|
636
|
+
return e.length === _t * 2 && e.startsWith("5120");
|
|
637
|
+
}
|
|
638
|
+
function Vt(e) {
|
|
639
|
+
return e.length === kt * 2 && e.startsWith("76a914") && e.endsWith("88ac");
|
|
640
|
+
}
|
|
641
|
+
function Lt(e) {
|
|
642
|
+
return e.length === It * 2 && e.startsWith("a914") && e.endsWith("87");
|
|
643
|
+
}
|
|
644
|
+
function he(e, t) {
|
|
645
|
+
const o = f(e).toLowerCase();
|
|
646
|
+
if (o.length === 0)
|
|
647
|
+
throw new Error(`${t} is empty`);
|
|
648
|
+
if (!/^[0-9a-f]+$/.test(o) || o.length % 2 !== 0)
|
|
649
|
+
throw new Error(`${t} is not valid hex`);
|
|
650
|
+
if (o.startsWith("6a"))
|
|
651
|
+
throw new Error(
|
|
652
|
+
`${t} is an OP_RETURN output, which is provably unspendable`
|
|
653
|
+
);
|
|
654
|
+
if (!Ot(o) && !At(o) && !Tt(o) && !Vt(o) && !Lt(o))
|
|
655
|
+
throw new Error(
|
|
656
|
+
`${t} is not a standard scriptPubKey (expected P2TR, P2WPKH, P2WSH, P2PKH, or P2SH; got ${o.length / 2} bytes: ${o})`
|
|
657
|
+
);
|
|
658
|
+
}
|
|
659
|
+
const de = 3;
|
|
660
|
+
async function Zt(e) {
|
|
661
|
+
const t = {
|
|
662
|
+
vaultCoreVersion: e.vaultCoreVersion,
|
|
663
|
+
numVaultKeepers: e.vaultKeeperBtcPubkeys.length,
|
|
664
|
+
numUniversalChallengers: e.universalChallengerBtcPubkeys.length,
|
|
665
|
+
councilSize: e.councilMembers.length,
|
|
666
|
+
protocolFeeRate: e.protocolFeeRate
|
|
667
|
+
};
|
|
668
|
+
St(t);
|
|
669
|
+
const o = b.fromHex(f(e.payoutTxHex)), n = b.fromHex(f(e.peginTxHex)), i = b.fromHex(f(e.assertTxHex));
|
|
670
|
+
if (o.ins.length !== Q)
|
|
671
|
+
throw new Error(
|
|
672
|
+
`Payout transaction must have exactly ${Q} inputs, got ${o.ins.length}`
|
|
673
|
+
);
|
|
674
|
+
if (o.version !== Z)
|
|
675
|
+
throw new Error(
|
|
676
|
+
`Payout transaction version ${o.version} must be ${Z}; refusing to sign payout.`
|
|
677
|
+
);
|
|
678
|
+
if (o.locktime !== j)
|
|
679
|
+
throw new Error(
|
|
680
|
+
`Payout transaction locktime ${o.locktime} must be ${j}; refusing to sign payout.`
|
|
681
|
+
);
|
|
682
|
+
const s = pe(
|
|
683
|
+
o.ins[0],
|
|
684
|
+
0,
|
|
685
|
+
n,
|
|
686
|
+
"PegIn",
|
|
687
|
+
qe
|
|
688
|
+
);
|
|
689
|
+
if (o.ins[0].sequence !== e.timelockPegin)
|
|
690
|
+
throw new Error(
|
|
691
|
+
`Payout input 0 sequence ${o.ins[0].sequence} must equal the PegIn CSV timelock ${e.timelockPegin}; refusing to sign payout.`
|
|
692
|
+
);
|
|
693
|
+
const r = pe(
|
|
694
|
+
o.ins[1],
|
|
695
|
+
1,
|
|
696
|
+
i,
|
|
697
|
+
"Assert",
|
|
698
|
+
me
|
|
699
|
+
);
|
|
700
|
+
if (o.ins[1].sequence !== e.timelockAssert)
|
|
701
|
+
throw new Error(
|
|
702
|
+
`Payout input 1 sequence ${o.ins[1].sequence} must equal the Assert CSV timelock ${e.timelockAssert}; refusing to sign payout.`
|
|
703
|
+
);
|
|
704
|
+
const { out0Len: u, out1Len: h } = Kt(
|
|
705
|
+
o,
|
|
706
|
+
s.value,
|
|
707
|
+
e
|
|
708
|
+
), a = s.value + r.value;
|
|
709
|
+
let d = 0;
|
|
710
|
+
for (const v of o.outs) d += v.value;
|
|
711
|
+
if (d > a)
|
|
712
|
+
throw new Error(
|
|
713
|
+
`Payout outputs (${d} sats) exceed inputs (${a} sats); invalid transaction.`
|
|
714
|
+
);
|
|
715
|
+
const l = a - d;
|
|
716
|
+
await xt(t, {
|
|
717
|
+
implicitFeeSats: l,
|
|
718
|
+
out0Len: u,
|
|
719
|
+
out1Len: h
|
|
720
|
+
});
|
|
721
|
+
const P = await st({
|
|
722
|
+
vaultCoreVersion: e.vaultCoreVersion,
|
|
723
|
+
depositor: e.depositorBtcPubkey,
|
|
724
|
+
vaultProvider: e.vaultProviderBtcPubkey,
|
|
725
|
+
vaultKeepers: e.vaultKeeperBtcPubkeys,
|
|
726
|
+
universalChallengers: e.universalChallengerBtcPubkeys,
|
|
727
|
+
timelockPegin: e.timelockPegin,
|
|
728
|
+
network: e.network
|
|
729
|
+
}), p = await Rt(e, r);
|
|
730
|
+
return {
|
|
731
|
+
psbtHex: Ht(
|
|
732
|
+
o,
|
|
733
|
+
s,
|
|
734
|
+
r,
|
|
735
|
+
P,
|
|
736
|
+
p
|
|
737
|
+
)
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
async function Rt(e, t) {
|
|
741
|
+
const { payoutScript: o, payoutControlBlock: n } = await Ve({
|
|
742
|
+
txGraphVersion: e.vaultCoreVersion,
|
|
743
|
+
claimer: f(e.claimerBtcPubkey).toLowerCase(),
|
|
744
|
+
localChallengers: Ge({
|
|
745
|
+
claimerBtcPubkey: e.claimerBtcPubkey,
|
|
746
|
+
depositorBtcPubkey: e.depositorBtcPubkey,
|
|
747
|
+
vaultProviderBtcPubkey: e.vaultProviderBtcPubkey,
|
|
748
|
+
vaultKeeperBtcPubkeys: e.vaultKeeperBtcPubkeys
|
|
749
|
+
}),
|
|
750
|
+
universalChallengers: e.universalChallengerBtcPubkeys.map(
|
|
751
|
+
(u) => f(u).toLowerCase()
|
|
752
|
+
),
|
|
753
|
+
timelockAssert: e.timelockAssert,
|
|
754
|
+
councilMembers: e.councilMembers.map(
|
|
755
|
+
(u) => f(u).toLowerCase()
|
|
756
|
+
),
|
|
757
|
+
councilQuorum: e.councilQuorum
|
|
758
|
+
}), i = x(o), s = x(n), r = wt({
|
|
759
|
+
leafVersion: y,
|
|
760
|
+
script: i,
|
|
761
|
+
controlBlock: s
|
|
762
|
+
});
|
|
763
|
+
if (!t.script.equals(r))
|
|
764
|
+
throw new Error(
|
|
765
|
+
`Rebuilt Assert:0 payout leaf does not spend the Assert output being referenced: leaf binds to ${r.toString("hex")}, Assert:${me} pays ${t.script.toString("hex")}; refusing to sign payout.`
|
|
766
|
+
);
|
|
767
|
+
return { script: i, controlBlock: s };
|
|
768
|
+
}
|
|
769
|
+
function pe(e, t, o, n, i) {
|
|
770
|
+
const s = U(
|
|
771
|
+
new Uint8Array(e.hash).slice().reverse()
|
|
772
|
+
), r = o.getId();
|
|
773
|
+
if (s !== r || e.index !== i)
|
|
774
|
+
throw new Error(
|
|
775
|
+
`Input ${t} must spend ${n}:${i}. Expected ${r}:${i}, got ${s}:${e.index}`
|
|
776
|
+
);
|
|
777
|
+
const u = o.outs[e.index];
|
|
778
|
+
if (!u)
|
|
779
|
+
throw new Error(
|
|
780
|
+
`Previous output not found for input ${t} (txid: ${s}, index: ${e.index})`
|
|
781
|
+
);
|
|
782
|
+
return u;
|
|
783
|
+
}
|
|
784
|
+
function Ht(e, t, o, n, i) {
|
|
785
|
+
const s = new V();
|
|
786
|
+
s.setVersion(e.version), s.setLocktime(e.locktime);
|
|
787
|
+
const [r, u] = e.ins;
|
|
788
|
+
s.addInput({
|
|
789
|
+
hash: r.hash,
|
|
790
|
+
index: r.index,
|
|
791
|
+
sequence: r.sequence,
|
|
792
|
+
witnessUtxo: {
|
|
793
|
+
script: t.script,
|
|
794
|
+
value: t.value
|
|
795
|
+
},
|
|
796
|
+
tapLeafScript: [
|
|
797
|
+
{
|
|
798
|
+
leafVersion: y,
|
|
799
|
+
script: c.from(x(n.payoutScript)),
|
|
800
|
+
controlBlock: c.from(
|
|
801
|
+
x(n.payoutControlBlock)
|
|
802
|
+
)
|
|
803
|
+
}
|
|
804
|
+
],
|
|
805
|
+
tapInternalKey: c.from(k)
|
|
806
|
+
// sighashType omitted - defaults to SIGHASH_DEFAULT (0x00) for Taproot
|
|
807
|
+
}), s.addInput({
|
|
808
|
+
hash: u.hash,
|
|
809
|
+
index: u.index,
|
|
810
|
+
sequence: u.sequence,
|
|
811
|
+
witnessUtxo: {
|
|
812
|
+
script: o.script,
|
|
813
|
+
value: o.value
|
|
814
|
+
},
|
|
815
|
+
tapLeafScript: [
|
|
816
|
+
{
|
|
817
|
+
leafVersion: y,
|
|
818
|
+
script: c.from(i.script),
|
|
819
|
+
controlBlock: c.from(i.controlBlock)
|
|
820
|
+
}
|
|
821
|
+
],
|
|
822
|
+
tapInternalKey: c.from(k)
|
|
823
|
+
});
|
|
824
|
+
for (const h of e.outs)
|
|
825
|
+
s.addOutput({
|
|
826
|
+
script: h.script,
|
|
827
|
+
value: h.value
|
|
828
|
+
});
|
|
829
|
+
return s.toHex();
|
|
830
|
+
}
|
|
831
|
+
function Pe(e, t) {
|
|
832
|
+
const o = f(e).toLowerCase(), n = f(
|
|
833
|
+
$e(t)
|
|
834
|
+
).toLowerCase();
|
|
835
|
+
return o === n ? [o] : [o, n];
|
|
836
|
+
}
|
|
837
|
+
function fe(e, t) {
|
|
838
|
+
return t.some((o) => e.equals(c.from(o, "hex")));
|
|
839
|
+
}
|
|
840
|
+
function Kt(e, t, o) {
|
|
841
|
+
const {
|
|
842
|
+
claimerBtcPubkey: n,
|
|
843
|
+
vaultProviderBtcPubkey: i,
|
|
844
|
+
depositorBtcPubkey: s,
|
|
845
|
+
vaultKeeperBtcPubkeys: r,
|
|
846
|
+
registeredPayoutScriptPubKey: u,
|
|
847
|
+
commissionBps: h,
|
|
848
|
+
vkClaimerPayoutScriptPubKeys: a,
|
|
849
|
+
vpCommissionScriptPubKey: d
|
|
850
|
+
} = o;
|
|
851
|
+
if (!Ne(u))
|
|
852
|
+
throw new Error("Invalid registeredPayoutScriptPubKey: not valid hex");
|
|
853
|
+
const l = f(n).toLowerCase(), P = f(i).toLowerCase(), p = f(s).toLowerCase(), v = r.map(
|
|
854
|
+
(S) => f(S).toLowerCase()
|
|
855
|
+
);
|
|
856
|
+
let w, g, $;
|
|
857
|
+
if (l === P)
|
|
858
|
+
w = "vp-claimer", g = Xe, $ = [f(u)];
|
|
859
|
+
else if (l === p)
|
|
860
|
+
w = "depositor-as-claimer", g = oe, $ = [f(u)];
|
|
861
|
+
else if (v.includes(l)) {
|
|
862
|
+
w = "vk-claimer", g = oe;
|
|
863
|
+
const S = a[l];
|
|
864
|
+
if (S === void 0)
|
|
865
|
+
throw new Error(
|
|
866
|
+
`No registered payout script resolved for vault keeper claimer ${l}`
|
|
867
|
+
);
|
|
868
|
+
he(
|
|
869
|
+
S,
|
|
870
|
+
`Vault keeper payout script for claimer ${l}`
|
|
871
|
+
), $ = Pe(S, l);
|
|
872
|
+
} else
|
|
873
|
+
throw new Error(
|
|
874
|
+
`Unknown claimer pubkey ${l}: not VP, depositor, or a registered vault keeper`
|
|
875
|
+
);
|
|
876
|
+
if (e.outs.length !== g)
|
|
877
|
+
throw new Error(
|
|
878
|
+
`Payout transaction has ${e.outs.length} output(s), expected exactly ${g} for role ${w}.`
|
|
879
|
+
);
|
|
880
|
+
if (!fe(e.outs[0].script, $))
|
|
881
|
+
throw new Error(
|
|
882
|
+
`Payout transaction output 0 does not pay the expected scriptPubKey for role ${w}. Accepted: ${$.join(" or ")}; got ${e.outs[0].script.toString("hex")}`
|
|
883
|
+
);
|
|
884
|
+
const L = g - 1;
|
|
885
|
+
if (e.outs[L].value !== J)
|
|
886
|
+
throw new Error(
|
|
887
|
+
`Payout CPFP anchor (out ${L}) value ${e.outs[L].value} sats must equal ${J} sats`
|
|
888
|
+
);
|
|
889
|
+
if (w === "vp-claimer") {
|
|
890
|
+
he(
|
|
891
|
+
d,
|
|
892
|
+
"Vault provider commission payout script"
|
|
893
|
+
);
|
|
894
|
+
const S = Pe(
|
|
895
|
+
d,
|
|
896
|
+
P
|
|
897
|
+
);
|
|
898
|
+
if (!fe(e.outs[1].script, S))
|
|
899
|
+
throw new Error(
|
|
900
|
+
`Payout transaction output 1 does not pay the vault provider's commission scriptPubKey. Accepted: ${S.join(" or ")}; got ${e.outs[1].script.toString("hex")}`
|
|
901
|
+
);
|
|
902
|
+
if (!Number.isInteger(h) || h < 0 || h >= ee)
|
|
903
|
+
throw new Error(
|
|
904
|
+
`commissionBps must be an integer in [0, ${ee}), got ${h}`
|
|
905
|
+
);
|
|
906
|
+
const B = Math.floor(
|
|
907
|
+
t * h / We
|
|
908
|
+
);
|
|
909
|
+
if (e.outs[1].value > B)
|
|
910
|
+
throw new Error(
|
|
911
|
+
`Payout VP commission (out 1) value ${e.outs[1].value} sats exceeds cap ${B} sats (${h} bps of peginValue=${t})`
|
|
912
|
+
);
|
|
913
|
+
}
|
|
914
|
+
const I = e.outs[0].script.length;
|
|
915
|
+
if (I === 0 || I > te)
|
|
916
|
+
throw new Error(
|
|
917
|
+
`Payout receiver scriptPubKey length ${I} is outside the contract's registration cap [1, ${te}]; refusing to sign payout.`
|
|
918
|
+
);
|
|
919
|
+
const be = w === "vp-claimer" ? e.outs[1].script.length : void 0;
|
|
920
|
+
return { out0Len: I, out1Len: be };
|
|
921
|
+
}
|
|
922
|
+
function jt(e, t, o = 0) {
|
|
923
|
+
const n = V.fromHex(e);
|
|
924
|
+
if (o >= n.data.inputs.length)
|
|
925
|
+
throw new Error(
|
|
926
|
+
`Input index ${o} out of range (${n.data.inputs.length} inputs)`
|
|
927
|
+
);
|
|
928
|
+
const i = n.data.inputs[o];
|
|
929
|
+
if (i.tapScriptSig && i.tapScriptSig.length > 0) {
|
|
930
|
+
const s = x(t);
|
|
931
|
+
for (const r of i.tapScriptSig)
|
|
932
|
+
if (r.pubkey.equals(c.from(s)))
|
|
933
|
+
return ge(r.signature, o);
|
|
934
|
+
throw new Error(
|
|
935
|
+
`No signature found for depositor pubkey: ${t} at input ${o}`
|
|
936
|
+
);
|
|
937
|
+
}
|
|
938
|
+
if (i.finalScriptWitness && i.finalScriptWitness.length > 0) {
|
|
939
|
+
const s = Nt(i.finalScriptWitness);
|
|
940
|
+
if (s.length !== de)
|
|
941
|
+
throw new Error(
|
|
942
|
+
`Unexpected finalized witness stack size at input ${o}: expected ${de} items (signature, script, controlBlock), got ${s.length}`
|
|
943
|
+
);
|
|
944
|
+
return ge(s[0], o);
|
|
945
|
+
}
|
|
946
|
+
throw new Error(
|
|
947
|
+
`No tapScriptSig or finalScriptWitness found in signed PSBT at input ${o}`
|
|
948
|
+
);
|
|
949
|
+
}
|
|
950
|
+
function ge(e, t) {
|
|
951
|
+
if (e.length === 64)
|
|
952
|
+
return U(new Uint8Array(e));
|
|
953
|
+
throw e.length === 65 ? new Error(
|
|
954
|
+
`Unexpected sighash byte 0x${e[64].toString(16).padStart(2, "0")} at input ${t}. Expected implicit SIGHASH_DEFAULT as a 64-byte signature.`
|
|
955
|
+
) : new Error(
|
|
956
|
+
`Unexpected signature length at input ${t}: ${e.length}`
|
|
957
|
+
);
|
|
958
|
+
}
|
|
959
|
+
function Nt(e) {
|
|
960
|
+
const t = [];
|
|
961
|
+
let o = 0;
|
|
962
|
+
const n = (r) => {
|
|
963
|
+
if (o + r > e.length)
|
|
964
|
+
throw new Error(
|
|
965
|
+
`Malformed witness data: need ${r} byte(s) at offset ${o}, only ${e.length - o} remaining`
|
|
966
|
+
);
|
|
967
|
+
}, i = () => {
|
|
968
|
+
n(1);
|
|
969
|
+
const r = e[o++];
|
|
970
|
+
if (r < 253) return r;
|
|
971
|
+
if (r === 253) {
|
|
972
|
+
n(2);
|
|
973
|
+
const u = (e[o] | e[o + 1] << 8) >>> 0;
|
|
974
|
+
return o += 2, u;
|
|
975
|
+
}
|
|
976
|
+
if (r === 254) {
|
|
977
|
+
n(4);
|
|
978
|
+
const u = (e[o] | e[o + 1] << 8 | e[o + 2] << 16 | e[o + 3] << 24) >>> 0;
|
|
979
|
+
return o += 4, u;
|
|
980
|
+
}
|
|
981
|
+
throw new Error(
|
|
982
|
+
`Malformed witness data: 8-byte varint (0xff) not supported at offset ${o - 1}`
|
|
983
|
+
);
|
|
984
|
+
}, s = i();
|
|
985
|
+
for (let r = 0; r < s; r++) {
|
|
986
|
+
const u = i();
|
|
987
|
+
n(u), t.push(c.from(e.subarray(o, o + u))), o += u;
|
|
988
|
+
}
|
|
989
|
+
if (o !== e.length)
|
|
990
|
+
throw new Error(
|
|
991
|
+
`Malformed witness data: ${e.length - o} trailing byte(s) after parsing ${s} item(s)`
|
|
992
|
+
);
|
|
993
|
+
return t;
|
|
994
|
+
}
|
|
995
|
+
class m extends Error {
|
|
996
|
+
constructor(t) {
|
|
997
|
+
super(
|
|
998
|
+
`Wallet returned a PSBT for a different transaction: ${t}`
|
|
999
|
+
), this.name = "PsbtSubstitutionError";
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
function we(e, t) {
|
|
1003
|
+
try {
|
|
1004
|
+
return V.fromHex(t);
|
|
1005
|
+
} catch (o) {
|
|
1006
|
+
const n = o instanceof Error ? o.message : String(o);
|
|
1007
|
+
throw new Error(`Failed to parse ${e} PSBT: ${n}`);
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
const Ut = 8;
|
|
1011
|
+
function N(e) {
|
|
1012
|
+
return `${e.toString("hex").slice(0, Ut)}…`;
|
|
1013
|
+
}
|
|
1014
|
+
function ve(e) {
|
|
1015
|
+
const t = c.from(e).reverse();
|
|
1016
|
+
return N(t);
|
|
1017
|
+
}
|
|
1018
|
+
function Jt(e) {
|
|
1019
|
+
const t = we("requested", e.requestedPsbtHex), o = we("returned", e.returnedPsbtHex);
|
|
1020
|
+
if (t.version !== o.version)
|
|
1021
|
+
throw new m(
|
|
1022
|
+
`tx version differs (requested=${t.version}, returned=${o.version})`
|
|
1023
|
+
);
|
|
1024
|
+
if (t.locktime !== o.locktime)
|
|
1025
|
+
throw new m(
|
|
1026
|
+
`tx locktime differs (requested=${t.locktime}, returned=${o.locktime})`
|
|
1027
|
+
);
|
|
1028
|
+
if (t.txInputs.length !== o.txInputs.length)
|
|
1029
|
+
throw new m(
|
|
1030
|
+
`input count differs (requested=${t.txInputs.length}, returned=${o.txInputs.length})`
|
|
1031
|
+
);
|
|
1032
|
+
if (t.txOutputs.length !== o.txOutputs.length)
|
|
1033
|
+
throw new m(
|
|
1034
|
+
`output count differs (requested=${t.txOutputs.length}, returned=${o.txOutputs.length})`
|
|
1035
|
+
);
|
|
1036
|
+
for (let n = 0; n < t.txInputs.length; n++) {
|
|
1037
|
+
const i = t.txInputs[n], s = o.txInputs[n];
|
|
1038
|
+
if (!i.hash.equals(s.hash))
|
|
1039
|
+
throw new m(
|
|
1040
|
+
`input ${n} prevout txid differs (requested=${ve(i.hash)}, returned=${ve(s.hash)})`
|
|
1041
|
+
);
|
|
1042
|
+
if (i.index !== s.index)
|
|
1043
|
+
throw new m(
|
|
1044
|
+
`input ${n} prevout vout differs (requested=${i.index}, returned=${s.index})`
|
|
1045
|
+
);
|
|
1046
|
+
if (i.sequence !== s.sequence)
|
|
1047
|
+
throw new m(
|
|
1048
|
+
`input ${n} sequence differs (requested=${i.sequence}, returned=${s.sequence})`
|
|
1049
|
+
);
|
|
1050
|
+
}
|
|
1051
|
+
for (let n = 0; n < t.txOutputs.length; n++) {
|
|
1052
|
+
const i = t.txOutputs[n], s = o.txOutputs[n];
|
|
1053
|
+
if (!i.script.equals(s.script))
|
|
1054
|
+
throw new m(
|
|
1055
|
+
`output ${n} scriptPubKey differs (requested=${N(i.script)}, returned=${N(s.script)})`
|
|
1056
|
+
);
|
|
1057
|
+
if (i.value !== s.value)
|
|
1058
|
+
throw new m(
|
|
1059
|
+
`output ${n} value differs (requested=${i.value}, returned=${s.value})`
|
|
1060
|
+
);
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1063
|
+
function eo(e) {
|
|
1064
|
+
const { requestedPsbtHex: t, signatureHex: o, signerXOnlyPubkeyHex: n, inputIndex: i } = e, s = f(o);
|
|
1065
|
+
if (s.length !== F)
|
|
1066
|
+
throw new Error(
|
|
1067
|
+
`Schnorr signature for input ${i} must be ${F} hex chars (64 bytes), got ${s.length}.`
|
|
1068
|
+
);
|
|
1069
|
+
const r = f(n);
|
|
1070
|
+
if (r.length !== ne)
|
|
1071
|
+
throw new Error(
|
|
1072
|
+
`Signer x-only pubkey for input ${i} must be ${ne} hex chars (32 bytes), got ${r.length}.`
|
|
1073
|
+
);
|
|
1074
|
+
const u = V.fromHex(t);
|
|
1075
|
+
if (i < 0 || i >= u.data.inputs.length)
|
|
1076
|
+
throw new Error(
|
|
1077
|
+
`Input index ${i} out of range (${u.data.inputs.length} inputs).`
|
|
1078
|
+
);
|
|
1079
|
+
const h = [], a = [];
|
|
1080
|
+
for (let g = 0; g < u.data.inputs.length; g++) {
|
|
1081
|
+
const $ = u.data.inputs[g].witnessUtxo;
|
|
1082
|
+
if (!$)
|
|
1083
|
+
throw new Error(
|
|
1084
|
+
`Cannot verify signature: input ${g} of the requested PSBT has no witnessUtxo (required to recompute the Taproot sighash).`
|
|
1085
|
+
);
|
|
1086
|
+
h.push($.script), a.push($.value);
|
|
1087
|
+
}
|
|
1088
|
+
const d = u.data.inputs[i].tapLeafScript;
|
|
1089
|
+
if (!d || d.length !== 1)
|
|
1090
|
+
throw new Error(
|
|
1091
|
+
`Cannot verify signature: input ${i} of the requested PSBT must have exactly one tapLeafScript, got ${(d == null ? void 0 : d.length) ?? 0}.`
|
|
1092
|
+
);
|
|
1093
|
+
const l = d[0];
|
|
1094
|
+
if (l.leafVersion !== y)
|
|
1095
|
+
throw new Error(
|
|
1096
|
+
`Cannot verify signature: input ${i} tapLeafScript has leaf version 0x${l.leafVersion.toString(16)}, expected 0x${y.toString(16)}.`
|
|
1097
|
+
);
|
|
1098
|
+
const P = Ce(l.leafVersion, l.script), p = new b();
|
|
1099
|
+
p.version = u.version, p.locktime = u.locktime;
|
|
1100
|
+
for (const g of u.txInputs)
|
|
1101
|
+
p.addInput(g.hash, g.index, g.sequence);
|
|
1102
|
+
for (const g of u.txOutputs)
|
|
1103
|
+
p.addOutput(g.script, g.value);
|
|
1104
|
+
const v = p.hashForWitnessV1(
|
|
1105
|
+
i,
|
|
1106
|
+
h,
|
|
1107
|
+
a,
|
|
1108
|
+
b.SIGHASH_DEFAULT,
|
|
1109
|
+
P
|
|
1110
|
+
);
|
|
1111
|
+
if (!Se.verifySchnorr(
|
|
1112
|
+
v,
|
|
1113
|
+
x(r),
|
|
1114
|
+
x(s)
|
|
1115
|
+
))
|
|
1116
|
+
throw new Error(
|
|
1117
|
+
`Schnorr signature for input ${i} (signer ${r}) does not verify against the expected Taproot script-path sighash. The wallet may have signed with the tweaked key, signed a different transaction, or returned an invalid signature.`
|
|
1118
|
+
);
|
|
1119
|
+
}
|
|
1120
|
+
export {
|
|
1121
|
+
O as H,
|
|
1122
|
+
et as P,
|
|
1123
|
+
Dt as a,
|
|
1124
|
+
Qt as b,
|
|
1125
|
+
Yt as c,
|
|
1126
|
+
tt as d,
|
|
1127
|
+
ot as e,
|
|
1128
|
+
Zt as f,
|
|
1129
|
+
jt as g,
|
|
1130
|
+
m as h,
|
|
1131
|
+
Jt as i,
|
|
1132
|
+
eo as j,
|
|
1133
|
+
st as k,
|
|
1134
|
+
Qe as l,
|
|
1135
|
+
Ce as m,
|
|
1136
|
+
xe as n,
|
|
1137
|
+
je as o,
|
|
1138
|
+
Ge as p
|
|
1139
|
+
};
|
|
1140
|
+
//# sourceMappingURL=verifyScriptPathSchnorrSignature-D_epga4j.js.map
|