@babylonlabs-io/ts-sdk 2.0.2 → 3.0.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.
Files changed (113) hide show
  1. package/dist/{PeginManager-CKf7Bjmj.js → PeginManager-D8WGB8eN.js} +2 -2
  2. package/dist/PeginManager-D8WGB8eN.js.map +1 -0
  3. package/dist/{PeginManager-Dt2Z5ljw.cjs → PeginManager-IgDHy_eU.cjs} +2 -2
  4. package/dist/PeginManager-IgDHy_eU.cjs.map +1 -0
  5. package/dist/{buildAndBroadcastReclaim-B19cyK69.js → buildAndBroadcastReclaim-C55KaRJF.js} +783 -791
  6. package/dist/buildAndBroadcastReclaim-C55KaRJF.js.map +1 -0
  7. package/dist/buildAndBroadcastReclaim-CefzR6i_.cjs +2 -0
  8. package/dist/buildAndBroadcastReclaim-CefzR6i_.cjs.map +1 -0
  9. package/dist/index-D2mybjXD.js +522 -0
  10. package/dist/index-D2mybjXD.js.map +1 -0
  11. package/dist/index-MRdle0fs.cjs +2 -0
  12. package/dist/index-MRdle0fs.cjs.map +1 -0
  13. package/dist/index.cjs +1 -1
  14. package/dist/index.js +215 -214
  15. package/dist/pegin-registration-client-B0j6ugt9.cjs +2 -0
  16. package/dist/pegin-registration-client-B0j6ugt9.cjs.map +1 -0
  17. package/dist/{pegin-registration-client-C5Hktrja.js → pegin-registration-client-ilkV1mLP.js} +103 -88
  18. package/dist/pegin-registration-client-ilkV1mLP.js.map +1 -0
  19. package/dist/primeVpAuth-BDWBKYhv.cjs +2 -0
  20. package/dist/primeVpAuth-BDWBKYhv.cjs.map +1 -0
  21. package/dist/primeVpAuth-D0ZnsjRi.js +805 -0
  22. package/dist/primeVpAuth-D0ZnsjRi.js.map +1 -0
  23. package/dist/state-C4WoNCy1.cjs +2 -0
  24. package/dist/state-C4WoNCy1.cjs.map +1 -0
  25. package/dist/{json-rpc-client-CQLbpuKK.js → state-CFn1rT74.js} +136 -88
  26. package/dist/state-CFn1rT74.js.map +1 -0
  27. package/dist/tbv/core/clients/eth/index.cjs +1 -1
  28. package/dist/tbv/core/clients/eth/index.js +2 -2
  29. package/dist/tbv/core/clients/eth/types.d.ts +12 -5
  30. package/dist/tbv/core/clients/eth/types.d.ts.map +1 -1
  31. package/dist/tbv/core/clients/eth/vault-registry-reader.d.ts +12 -3
  32. package/dist/tbv/core/clients/eth/vault-registry-reader.d.ts.map +1 -1
  33. package/dist/tbv/core/clients/index.cjs +1 -1
  34. package/dist/tbv/core/clients/index.js +52 -51
  35. package/dist/tbv/core/clients/mempool/index.cjs +1 -1
  36. package/dist/tbv/core/clients/mempool/index.cjs.map +1 -1
  37. package/dist/tbv/core/clients/mempool/index.d.ts +1 -1
  38. package/dist/tbv/core/clients/mempool/index.d.ts.map +1 -1
  39. package/dist/tbv/core/clients/mempool/index.js +85 -71
  40. package/dist/tbv/core/clients/mempool/index.js.map +1 -1
  41. package/dist/tbv/core/clients/mempool/mempoolApi.d.ts +10 -0
  42. package/dist/tbv/core/clients/mempool/mempoolApi.d.ts.map +1 -1
  43. package/dist/tbv/core/clients/vault-provider/api.d.ts +17 -13
  44. package/dist/tbv/core/clients/vault-provider/api.d.ts.map +1 -1
  45. package/dist/tbv/core/clients/vault-provider/auth/createAuthenticatedVpClient.d.ts +8 -0
  46. package/dist/tbv/core/clients/vault-provider/auth/createAuthenticatedVpClient.d.ts.map +1 -1
  47. package/dist/tbv/core/clients/vault-provider/auth/index.d.ts +1 -1
  48. package/dist/tbv/core/clients/vault-provider/auth/index.d.ts.map +1 -1
  49. package/dist/tbv/core/clients/vault-provider/auth/primeVpAuth.d.ts +8 -0
  50. package/dist/tbv/core/clients/vault-provider/auth/primeVpAuth.d.ts.map +1 -1
  51. package/dist/tbv/core/clients/vault-provider/auth/tokenProvider.d.ts +18 -2
  52. package/dist/tbv/core/clients/vault-provider/auth/tokenProvider.d.ts.map +1 -1
  53. package/dist/tbv/core/clients/vault-provider/auth/tokenRegistry.d.ts +26 -6
  54. package/dist/tbv/core/clients/vault-provider/auth/tokenRegistry.d.ts.map +1 -1
  55. package/dist/tbv/core/clients/vault-provider/batchAttribution.d.ts +17 -15
  56. package/dist/tbv/core/clients/vault-provider/batchAttribution.d.ts.map +1 -1
  57. package/dist/tbv/core/clients/vault-provider/batchPoll.d.ts +15 -9
  58. package/dist/tbv/core/clients/vault-provider/batchPoll.d.ts.map +1 -1
  59. package/dist/tbv/core/clients/vault-provider/json-rpc-client.d.ts +3 -2
  60. package/dist/tbv/core/clients/vault-provider/json-rpc-client.d.ts.map +1 -1
  61. package/dist/tbv/core/clients/vault-provider/status/index.cjs +1 -1
  62. package/dist/tbv/core/clients/vault-provider/status/index.js +2 -2
  63. package/dist/tbv/core/clients/vault-provider/types.d.ts +36 -23
  64. package/dist/tbv/core/clients/vault-provider/types.d.ts.map +1 -1
  65. package/dist/tbv/core/clients/vault-provider/validators.d.ts +12 -4
  66. package/dist/tbv/core/clients/vault-provider/validators.d.ts.map +1 -1
  67. package/dist/tbv/core/index.cjs +1 -1
  68. package/dist/tbv/core/index.js +216 -215
  69. package/dist/tbv/core/managers/index.cjs +1 -1
  70. package/dist/tbv/core/managers/index.js +1 -1
  71. package/dist/tbv/core/services/deposit/interfaces.d.ts +2 -2
  72. package/dist/tbv/core/services/deposit/interfaces.d.ts.map +1 -1
  73. package/dist/tbv/core/services/deposit/runDepositorPresignFlow.d.ts +3 -1
  74. package/dist/tbv/core/services/deposit/runDepositorPresignFlow.d.ts.map +1 -1
  75. package/dist/tbv/core/services/deposit/submitWotsPublicKey.d.ts +3 -1
  76. package/dist/tbv/core/services/deposit/submitWotsPublicKey.d.ts.map +1 -1
  77. package/dist/tbv/core/services/deposit/waitForPeginStatus.d.ts +11 -2
  78. package/dist/tbv/core/services/deposit/waitForPeginStatus.d.ts.map +1 -1
  79. package/dist/tbv/core/services/index.cjs +1 -1
  80. package/dist/tbv/core/services/index.js +75 -74
  81. package/dist/tbv/core/services/index.js.map +1 -1
  82. package/dist/tbv/core/services/pegout/state.d.ts +10 -5
  83. package/dist/tbv/core/services/pegout/state.d.ts.map +1 -1
  84. package/dist/tbv/index.cjs +1 -1
  85. package/dist/tbv/index.js +216 -215
  86. package/dist/{toRefundInputs-DIIdhjvu.js → toRefundInputs-10RYIfh1.js} +2 -2
  87. package/dist/{toRefundInputs-DIIdhjvu.js.map → toRefundInputs-10RYIfh1.js.map} +1 -1
  88. package/dist/{toRefundInputs-B_GlIXjk.cjs → toRefundInputs-a-0HRyrl.cjs} +2 -2
  89. package/dist/{toRefundInputs-B_GlIXjk.cjs.map → toRefundInputs-a-0HRyrl.cjs.map} +1 -1
  90. package/dist/{types-DJxOEU3P.cjs → types-BueQoMjO.cjs} +2 -2
  91. package/dist/{types-DJxOEU3P.cjs.map → types-BueQoMjO.cjs.map} +1 -1
  92. package/dist/{types-Dz1kVqn6.js → types-DGkejGZH.js} +2 -2
  93. package/dist/{types-Dz1kVqn6.js.map → types-DGkejGZH.js.map} +1 -1
  94. package/package.json +1 -1
  95. package/dist/PeginManager-CKf7Bjmj.js.map +0 -1
  96. package/dist/PeginManager-Dt2Z5ljw.cjs.map +0 -1
  97. package/dist/buildAndBroadcastReclaim-B19cyK69.js.map +0 -1
  98. package/dist/buildAndBroadcastReclaim-DcbsjJdf.cjs +0 -2
  99. package/dist/buildAndBroadcastReclaim-DcbsjJdf.cjs.map +0 -1
  100. package/dist/index-Bnavi_r3.js +0 -521
  101. package/dist/index-Bnavi_r3.js.map +0 -1
  102. package/dist/index-CcdCNFLM.cjs +0 -2
  103. package/dist/index-CcdCNFLM.cjs.map +0 -1
  104. package/dist/json-rpc-client-CQLbpuKK.js.map +0 -1
  105. package/dist/json-rpc-client-DbpFfa_o.cjs +0 -2
  106. package/dist/json-rpc-client-DbpFfa_o.cjs.map +0 -1
  107. package/dist/pegin-registration-client-BdJ6c7si.cjs +0 -2
  108. package/dist/pegin-registration-client-BdJ6c7si.cjs.map +0 -1
  109. package/dist/pegin-registration-client-C5Hktrja.js.map +0 -1
  110. package/dist/primeVpAuth-D21PXgg2.cjs +0 -2
  111. package/dist/primeVpAuth-D21PXgg2.cjs.map +0 -1
  112. package/dist/primeVpAuth-PiUJksAq.js +0 -745
  113. package/dist/primeVpAuth-PiUJksAq.js.map +0 -1
@@ -0,0 +1,805 @@
1
+ var Q = Object.defineProperty;
2
+ var Z = (t, e, r) => e in t ? Q(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r;
3
+ var u = (t, e, r) => Z(t, typeof e != "symbol" ? e + "" : e, r);
4
+ import { s as b, S as ee, h as re, p as J } from "./bitcoin-C-TuHb-v.js";
5
+ import { V as te } from "./index-D2mybjXD.js";
6
+ import { a as ne } from "./state-CFn1rT74.js";
7
+ import * as V from "@bitcoin-js/tiny-secp256k1-asmjs";
8
+ import { X as k, H as _, C as D } from "./validation-OSC0xTHB.js";
9
+ import { v as ie } from "./bip322Verify-BXYrHBh8.js";
10
+ import { s as se } from "./sha2-YqVHJiFj.js";
11
+ function S(t, e) {
12
+ const r = (t & 7) << 5, n = typeof e == "bigint" ? e : BigInt(e);
13
+ if (n < 0n) throw new Error("cborHead: negative argument");
14
+ if (n < 24n) return new Uint8Array([r | Number(n)]);
15
+ if (n < 0x100n) return new Uint8Array([r | 24, Number(n)]);
16
+ if (n < 0x10000n) {
17
+ const i = Number(n);
18
+ return new Uint8Array([r | 25, i >>> 8 & 255, i & 255]);
19
+ }
20
+ if (n < 0x100000000n) {
21
+ const i = Number(n);
22
+ return new Uint8Array([
23
+ r | 26,
24
+ i >>> 24 & 255,
25
+ i >>> 16 & 255,
26
+ i >>> 8 & 255,
27
+ i & 255
28
+ ]);
29
+ }
30
+ const s = new Uint8Array(9);
31
+ s[0] = r | 27;
32
+ for (let i = 7; i >= 0; i--)
33
+ s[1 + i] = Number(n >> BigInt((7 - i) * 8)) & 255;
34
+ return s;
35
+ }
36
+ function j(...t) {
37
+ const e = t.reduce((s, i) => s + i.length, 0), r = new Uint8Array(e);
38
+ let n = 0;
39
+ for (const s of t)
40
+ r.set(s, n), n += s.length;
41
+ return r;
42
+ }
43
+ function I(t) {
44
+ const r = [S(4, t.length)];
45
+ for (const n of t)
46
+ r.push(S(0, n));
47
+ return j(...r);
48
+ }
49
+ function oe(t, e, r) {
50
+ if (!Number.isSafeInteger(r) || r < 0)
51
+ throw new Error(
52
+ `encodeServerIdentityPayload: expires_at must be a non-negative safe integer, got ${r}`
53
+ );
54
+ const n = S(4, 3), s = I(t), i = I(e), o = S(0, r);
55
+ return j(n, s, i, o);
56
+ }
57
+ const ae = new TextEncoder().encode(
58
+ "btc-auth.server-identity.v1"
59
+ ), ce = 2 * 3600;
60
+ class p extends Error {
61
+ constructor(e, r) {
62
+ super(e), this.reason = r, this.name = "ServerIdentityError";
63
+ }
64
+ }
65
+ function v(t) {
66
+ const e = new Uint8Array(t.length / 2);
67
+ for (let r = 0; r < e.length; r++)
68
+ e[r] = parseInt(t.slice(r * 2, r * 2 + 2), 16);
69
+ return e;
70
+ }
71
+ function de(t) {
72
+ const { proof: e, pinnedServerPubkey: r, now: n } = t, s = t.maxLifetimeSecs ?? ce, i = b(r).toLowerCase();
73
+ if (i.length !== k || !_.test(i))
74
+ throw new p(
75
+ `pinnedServerPubkey must be 32-byte hex; got ${i.length} chars`,
76
+ "invalid_pubkey_encoding"
77
+ );
78
+ const o = b(e.server_pubkey).toLowerCase();
79
+ if (o.length !== k || !_.test(o))
80
+ throw new p(
81
+ `server_pubkey must be 32-byte hex; got ${o.length} chars`,
82
+ "invalid_pubkey_encoding"
83
+ );
84
+ if (o !== i)
85
+ throw new p(
86
+ `server_pubkey does not match pinned value: expected ${i}, got ${o}`,
87
+ "pinned_pubkey_mismatch"
88
+ );
89
+ if (!Number.isSafeInteger(e.expires_at))
90
+ throw new p(
91
+ `expires_at must be a finite integer; got ${JSON.stringify(e.expires_at)}`,
92
+ "invalid_expires_at"
93
+ );
94
+ if (!Number.isSafeInteger(n))
95
+ throw new p(
96
+ `now must be a finite integer; got ${JSON.stringify(n)}`,
97
+ "invalid_expires_at"
98
+ );
99
+ if (e.expires_at <= n)
100
+ throw new p(
101
+ `server identity proof expired at ${e.expires_at}, now ${n}`,
102
+ "expired"
103
+ );
104
+ if (!Number.isSafeInteger(s) || s <= 0)
105
+ throw new p(
106
+ `maxLifetimeSecs must be a positive safe integer; got ${JSON.stringify(s)}`,
107
+ "invalid_max_lifetime"
108
+ );
109
+ if (e.expires_at - n > s)
110
+ throw new p(
111
+ `server identity proof expires too far in the future: expires_at=${e.expires_at}, now=${n}, max lifetime=${s}s`,
112
+ "expires_too_far"
113
+ );
114
+ const c = b(e.ephemeral_pubkey).toLowerCase();
115
+ if (c.length !== D || !_.test(c))
116
+ throw new p(
117
+ `ephemeral_pubkey must be 33-byte compressed hex; got ${c.length} chars`,
118
+ "invalid_ephemeral_pubkey"
119
+ );
120
+ const y = c.slice(0, 2);
121
+ if (y !== "02" && y !== "03")
122
+ throw new p(
123
+ `ephemeral_pubkey must be compressed (prefix 02/03); got ${y}`,
124
+ "invalid_ephemeral_pubkey"
125
+ );
126
+ const l = v(c);
127
+ if (!V.isPoint(l))
128
+ throw new p(
129
+ "ephemeral_pubkey is not a valid secp256k1 point",
130
+ "invalid_ephemeral_pubkey"
131
+ );
132
+ const h = b(e.signature).toLowerCase();
133
+ if (h.length !== ee || !_.test(h))
134
+ throw new p(
135
+ `signature must be 64-byte Schnorr hex; got ${h.length} chars`,
136
+ "invalid_signature_encoding"
137
+ );
138
+ const f = oe(
139
+ ae,
140
+ v(c),
141
+ e.expires_at
142
+ );
143
+ if (!ie(
144
+ f,
145
+ v(o),
146
+ v(h)
147
+ ))
148
+ throw new p(
149
+ "BIP-322 signature verification failed — ephemeral key is not attested by pinned server pubkey",
150
+ "signature_verification_failed"
151
+ );
152
+ }
153
+ const ue = /* @__PURE__ */ new Set([
154
+ "vaultProvider_submitDepositorWotsKey",
155
+ "vaultProvider_submitDepositorPresignatures",
156
+ "vaultProvider_requestDepositorPresignTransactions"
157
+ ]), he = /* @__PURE__ */ new Set([
158
+ "vaultProvider_requestDepositorClaimerArtifacts"
159
+ ]), pe = 6e4, C = "auth_createDepositorToken", R = "auth_createDepositorTokenGrpc";
160
+ function F(t, e) {
161
+ return new ne({
162
+ baseUrl: t,
163
+ timeout: pe,
164
+ headers: e,
165
+ retryableFor: (r) => r === C || r === R
166
+ });
167
+ }
168
+ const le = 0, ye = 1, m = 2, ge = 3, fe = 4, _e = 5, be = 6, we = 7, M = 24, xe = 28, ve = 20, ke = 21, Se = 22, N = 256;
169
+ class g extends Error {
170
+ constructor(e) {
171
+ super(`CBOR decode: ${e}`), this.name = "CborDecodeError";
172
+ }
173
+ }
174
+ class K {
175
+ constructor(e) {
176
+ u(this, "buf");
177
+ /** Current read offset. Public so callers can slice encoded sub-ranges. */
178
+ u(this, "pos", 0);
179
+ this.buf = e;
180
+ }
181
+ nextByte() {
182
+ if (this.pos >= this.buf.length)
183
+ throw new g("unexpected end of input");
184
+ return this.buf[this.pos++];
185
+ }
186
+ /**
187
+ * Read an initial byte and its argument. Rejects indefinite-length
188
+ * and reserved additional-info encodings. Arguments wider than
189
+ * {@link Number.MAX_SAFE_INTEGER} are rejected — none of the token's
190
+ * lengths, tags, or timestamps approach that bound.
191
+ */
192
+ readHead() {
193
+ const e = this.nextByte(), r = e >> 5, n = e & 31;
194
+ if (n < M)
195
+ return { major: r, arg: n };
196
+ if (n >= xe)
197
+ throw new g(
198
+ `unsupported additional info ${n} (indefinite-length or reserved)`
199
+ );
200
+ const s = 1 << n - M;
201
+ let i = 0n;
202
+ for (let o = 0; o < s; o++)
203
+ i = i << 8n | BigInt(this.nextByte());
204
+ if (i > BigInt(Number.MAX_SAFE_INTEGER))
205
+ throw new g(`argument ${i} exceeds safe integer range`);
206
+ return { major: r, arg: Number(i) };
207
+ }
208
+ /** Read `length` raw bytes as a sub-array view into the backing buffer. */
209
+ readBytes(e) {
210
+ if (this.pos + e > this.buf.length)
211
+ throw new g("length overruns end of input");
212
+ const r = this.buf.subarray(this.pos, this.pos + e);
213
+ return this.pos += e, r;
214
+ }
215
+ /**
216
+ * Read a byte string (major type 2), returning its content bytes.
217
+ * Throws if the next item is not a byte string.
218
+ */
219
+ readByteString() {
220
+ const e = this.readHead();
221
+ if (e.major !== m)
222
+ throw new g(
223
+ `expected byte string (major ${m}), got major ${e.major}`
224
+ );
225
+ return this.readBytes(e.arg);
226
+ }
227
+ /**
228
+ * Read the next complete data item as a decoded {@link CborValue}.
229
+ *
230
+ * `depth` tracks the current nesting level so a deeply-nested blob is
231
+ * rejected with a {@link CborDecodeError} rather than overflowing the
232
+ * native call stack (see {@link MAX_NESTING_DEPTH}).
233
+ */
234
+ readValue(e = 0) {
235
+ if (e > N)
236
+ throw new g(
237
+ `nesting exceeds maximum depth ${N}`
238
+ );
239
+ const r = this.readHead();
240
+ switch (r.major) {
241
+ case le:
242
+ return r.arg;
243
+ case ye:
244
+ return -1 - r.arg;
245
+ case m:
246
+ return this.readBytes(r.arg);
247
+ case ge:
248
+ return new TextDecoder("utf-8", { fatal: !0 }).decode(
249
+ this.readBytes(r.arg)
250
+ );
251
+ case fe: {
252
+ const n = [];
253
+ for (let s = 0; s < r.arg; s++)
254
+ n.push(this.readValue(e + 1));
255
+ return n;
256
+ }
257
+ case _e: {
258
+ const n = /* @__PURE__ */ new Map();
259
+ for (let s = 0; s < r.arg; s++) {
260
+ const i = this.readValue(e + 1), o = this.readValue(e + 1);
261
+ n.set(i, o);
262
+ }
263
+ return n;
264
+ }
265
+ case be:
266
+ return { tag: r.arg, value: this.readValue(e + 1) };
267
+ case we:
268
+ if (r.arg === ve) return !1;
269
+ if (r.arg === ke) return !0;
270
+ if (r.arg === Se) return null;
271
+ throw new g(
272
+ `unsupported simple/float value ${r.arg}`
273
+ );
274
+ default:
275
+ throw new g(`unsupported major type ${r.major}`);
276
+ }
277
+ }
278
+ }
279
+ function q(t) {
280
+ const e = new K(t), r = e.readValue();
281
+ if (e.pos !== t.length)
282
+ throw new g("trailing bytes after top-level item");
283
+ return r;
284
+ }
285
+ const Ae = "vaultd-jsonrpc", Pe = "vaultd-grpc", L = 18, O = 4, B = -47, me = 1, H = 64, Ee = 128, Te = 96, Ce = 64, Re = 1, Oe = 2, $e = 3, Ie = 4, Me = 5, Ne = 6, Le = 7, G = new TextEncoder().encode("Signature1");
286
+ class a extends Error {
287
+ constructor(e, r) {
288
+ super(e), this.reason = r, this.name = "CwtVerificationError";
289
+ }
290
+ }
291
+ function Be(t) {
292
+ const e = X(
293
+ t.expectedIssuerXOnlyPubkey,
294
+ "expectedIssuerXOnlyPubkey"
295
+ ), r = X(
296
+ t.expectedAudienceXOnlyPubkey,
297
+ "expectedAudienceXOnlyPubkey"
298
+ ), n = Je(t.ephemeralPubkeyHex), s = De(t.token), i = new K(s), o = i.readHead();
299
+ if (o.major !== 6 || o.arg !== L)
300
+ throw new a(
301
+ `token is not a COSE Sign1 tagged value (tag ${L})`,
302
+ "invalid_token_structure"
303
+ );
304
+ const c = i.readHead();
305
+ if (c.major !== 4 || c.arg !== O)
306
+ throw new a(
307
+ `COSE Sign1 must be a ${O}-element array`,
308
+ "invalid_token_structure"
309
+ );
310
+ const y = i.pos, l = i.readByteString(), h = s.subarray(y, i.pos);
311
+ i.readValue();
312
+ const f = i.pos, w = i.readByteString(), A = s.subarray(f, i.pos), P = i.readByteString();
313
+ if (P.length !== H)
314
+ throw new a(
315
+ `COSE signature must be ${H} bytes, got ${P.length}`,
316
+ "invalid_token_structure"
317
+ );
318
+ if (i.pos !== s.length)
319
+ throw new a(
320
+ "COSE Sign1 token has trailing bytes after the signature",
321
+ "invalid_token_structure"
322
+ );
323
+ const $ = He(l);
324
+ if ($ !== B)
325
+ throw new a(
326
+ `unexpected COSE algorithm ${$} (expected ES256K ${B})`,
327
+ "unexpected_algorithm"
328
+ );
329
+ const Y = Ge(h, A), z = se(Y);
330
+ if (!V.verify(z, n, P, !0))
331
+ throw new a(
332
+ "COSE signature does not verify against the server's ephemeral key",
333
+ "signature_verification_failed"
334
+ );
335
+ const d = Xe(w), x = d.audience.toLowerCase();
336
+ if (x.length !== k || !_.test(x))
337
+ throw new a(
338
+ "token `aud` is not a 32-byte x-only pubkey hex",
339
+ "invalid_claims"
340
+ );
341
+ if (d.issuedAt > d.expiresAt)
342
+ throw new a(
343
+ `token iat (${d.issuedAt}) is after exp (${d.expiresAt})`,
344
+ "invalid_claims"
345
+ );
346
+ if (d.issuer.toLowerCase() !== e)
347
+ throw new a(
348
+ `token issuer does not match pinned server pubkey: expected ${e}, got ${d.issuer.toLowerCase()}`,
349
+ "issuer_mismatch"
350
+ );
351
+ if (d.subject !== t.expectedSubject)
352
+ throw new a(
353
+ `token subject mismatch: expected ${t.expectedSubject}, got ${d.subject}`,
354
+ "subject_mismatch"
355
+ );
356
+ if (x !== r)
357
+ throw new a(
358
+ `token audience does not match depositor pubkey: expected ${r}, got ${x}`,
359
+ "audience_mismatch"
360
+ );
361
+ if (d.expiresAt <= t.now)
362
+ throw new a(
363
+ `token expired: exp ${d.expiresAt} <= now ${t.now}`,
364
+ "token_expired"
365
+ );
366
+ if (t.responseExpiresAt !== d.expiresAt)
367
+ throw new a(
368
+ `response expires_at (${t.responseExpiresAt}) does not equal token exp (${d.expiresAt})`,
369
+ "expiry_mismatch"
370
+ );
371
+ if (t.serverIdentityExpiresAt < d.expiresAt)
372
+ throw new a(
373
+ `server identity expires (${t.serverIdentityExpiresAt}) before token exp (${d.expiresAt})`,
374
+ "server_identity_expires_before_token"
375
+ );
376
+ return {
377
+ issuer: d.issuer,
378
+ subject: d.subject,
379
+ audience: x,
380
+ expiresAt: d.expiresAt,
381
+ notBefore: d.notBefore,
382
+ issuedAt: d.issuedAt
383
+ };
384
+ }
385
+ function He(t) {
386
+ if (t.length === 0)
387
+ throw new a(
388
+ "empty COSE protected header (no algorithm)",
389
+ "unexpected_algorithm"
390
+ );
391
+ const e = q(t);
392
+ if (!(e instanceof Map))
393
+ throw new a(
394
+ "COSE protected header is not a map",
395
+ "invalid_token_structure"
396
+ );
397
+ const r = e.get(me);
398
+ if (typeof r != "number")
399
+ throw new a(
400
+ "COSE protected header missing integer algorithm label",
401
+ "unexpected_algorithm"
402
+ );
403
+ return r;
404
+ }
405
+ function Ge(t, e) {
406
+ return je(
407
+ Uint8Array.of(Ee | O),
408
+ Uint8Array.of(Te | G.length),
409
+ G,
410
+ t,
411
+ Uint8Array.of(Ce),
412
+ e
413
+ );
414
+ }
415
+ function Xe(t) {
416
+ const e = q(t);
417
+ if (!(e instanceof Map))
418
+ throw new a(
419
+ "CWT claims root is not a map",
420
+ "invalid_claims"
421
+ );
422
+ if (Ue(e, Le, "cti").length === 0)
423
+ throw new a("token cti is empty", "invalid_claims");
424
+ return {
425
+ issuer: E(e, Re, "iss"),
426
+ subject: E(e, Oe, "sub"),
427
+ audience: E(e, $e, "aud"),
428
+ expiresAt: T(e, Ie, "exp"),
429
+ notBefore: T(e, Me, "nbf"),
430
+ issuedAt: T(e, Ne, "iat")
431
+ };
432
+ }
433
+ function E(t, e, r) {
434
+ const n = t.get(e);
435
+ if (typeof n != "string")
436
+ throw new a(
437
+ `token claim ${r} is missing or not a text string`,
438
+ "invalid_claims"
439
+ );
440
+ return n;
441
+ }
442
+ function Ue(t, e, r) {
443
+ const n = t.get(e);
444
+ if (!(n instanceof Uint8Array))
445
+ throw new a(
446
+ `token claim ${r} is missing or not a byte string`,
447
+ "invalid_claims"
448
+ );
449
+ return n;
450
+ }
451
+ function T(t, e, r) {
452
+ const n = t.get(e);
453
+ if (typeof n != "number" || !Number.isSafeInteger(n) || n < 0)
454
+ throw new a(
455
+ `token claim ${r} is missing or not a non-negative integer timestamp`,
456
+ "invalid_claims"
457
+ );
458
+ return n;
459
+ }
460
+ function X(t, e) {
461
+ const r = b(t).toLowerCase();
462
+ if (r.length !== k || !_.test(r))
463
+ throw new a(
464
+ `${e} must be 32-byte x-only hex; got ${r.length} chars`,
465
+ "invalid_input"
466
+ );
467
+ return r;
468
+ }
469
+ function Je(t) {
470
+ const e = b(t).toLowerCase(), r = e.slice(0, 2);
471
+ if (e.length !== D || !_.test(e) || r !== "02" && r !== "03")
472
+ throw new a(
473
+ "ephemeralPubkeyHex must be 33-byte compressed pubkey hex (prefix 02/03)",
474
+ "invalid_input"
475
+ );
476
+ return re(e);
477
+ }
478
+ const Ve = (() => {
479
+ const t = new Int16Array(128).fill(-1), e = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
480
+ for (let r = 0; r < e.length; r++)
481
+ t[e.charCodeAt(r)] = r;
482
+ return t;
483
+ })();
484
+ function De(t) {
485
+ const e = t.length, r = Math.floor(e / 4), n = e % 4;
486
+ if (n === 1)
487
+ throw new a(
488
+ "invalid base64url length",
489
+ "invalid_token_structure"
490
+ );
491
+ const s = r * 3 + (n === 0 ? 0 : n - 1), i = new Uint8Array(s), o = (l) => {
492
+ const h = l < 128 ? Ve[l] : -1;
493
+ if (h < 0)
494
+ throw new a(
495
+ "invalid base64url character",
496
+ "invalid_token_structure"
497
+ );
498
+ return h;
499
+ };
500
+ let c = 0, y = 0;
501
+ for (let l = 0; l < r; l++) {
502
+ const h = o(t.charCodeAt(c++)), f = o(t.charCodeAt(c++)), w = o(t.charCodeAt(c++)), A = o(t.charCodeAt(c++));
503
+ i[y++] = h << 2 | f >> 4, i[y++] = (f & 15) << 4 | w >> 2, i[y++] = (w & 3) << 6 | A;
504
+ }
505
+ if (n === 2) {
506
+ const l = o(t.charCodeAt(c++)), h = o(t.charCodeAt(c++));
507
+ i[y++] = l << 2 | h >> 4;
508
+ } else if (n === 3) {
509
+ const l = o(t.charCodeAt(c++)), h = o(t.charCodeAt(c++)), f = o(t.charCodeAt(c++));
510
+ i[y++] = l << 2 | h >> 4, i[y++] = (h & 15) << 4 | f >> 2;
511
+ }
512
+ return i;
513
+ }
514
+ function je(...t) {
515
+ const e = t.reduce((s, i) => s + i.length, 0), r = new Uint8Array(e);
516
+ let n = 0;
517
+ for (const s of t)
518
+ r.set(s, n), n += s.length;
519
+ return r;
520
+ }
521
+ const U = 4102444800, Fe = 30;
522
+ class Ke {
523
+ constructor(e) {
524
+ // Most identity-bearing fields remain readonly and are checked against
525
+ // re-registration in the registry's `getOrCreate`. The JSON-RPC pin is the
526
+ // one exception: RFC-006 explicitly permits it to move, but only to a value
527
+ // re-read through the caller's authoritative chain resolver.
528
+ u(this, "client");
529
+ u(this, "peginTxid");
530
+ u(this, "authAnchorHex");
531
+ u(this, "jsonRpcPinnedServerPubkey");
532
+ u(this, "grpcPinnedServerPubkey");
533
+ u(this, "refreshJsonRpcPinnedServerPubkey");
534
+ u(this, "expectedAudienceXOnlyPubkey");
535
+ u(this, "authGatedMethods");
536
+ u(this, "grpcGatedMethods");
537
+ u(this, "refreshSkewSecs");
538
+ u(this, "now");
539
+ /** Cached JSON-RPC-subject bearer (auth_createDepositorToken). */
540
+ u(this, "cachedJsonRpc", null);
541
+ u(this, "inFlightJsonRpc", null);
542
+ /** Cached gRPC-subject bearer (auth_createDepositorTokenGrpc). */
543
+ u(this, "cachedGrpc", null);
544
+ u(this, "inFlightGrpc", null);
545
+ this.client = e.client, this.peginTxid = e.peginTxid, this.authAnchorHex = e.authAnchorHex, this.jsonRpcPinnedServerPubkey = e.pinnedServerPubkey, this.grpcPinnedServerPubkey = e.grpcPinnedServerPubkey, this.refreshJsonRpcPinnedServerPubkey = e.refreshJsonRpcPinnedServerPubkey, this.expectedAudienceXOnlyPubkey = e.expectedAudienceXOnlyPubkey, this.authGatedMethods = e.authGatedMethods, this.grpcGatedMethods = e.grpcGatedMethods, this.refreshSkewSecs = e.refreshSkewSecs ?? Fe, this.now = e.now ?? (() => Math.floor(Date.now() / 1e3));
546
+ }
547
+ /**
548
+ * Return a bearer token for `method`, or `null` if `method` is not
549
+ * auth-gated.
550
+ *
551
+ * Routes by subject: `authGatedMethods` → JSON-RPC bearer (issued via
552
+ * `auth_createDepositorToken`); `grpcGatedMethods` → gRPC bearer
553
+ * (`auth_createDepositorTokenGrpc`). Either path acquires lazily and
554
+ * single-flights concurrent callers; the two cache slots are
555
+ * independent.
556
+ *
557
+ * Both token-issuing methods are hard-exempted from the gate — if
558
+ * either were ever included in the gated sets (caller misconfiguration)
559
+ * the provider would recurse into `acquireSingleFlight` from inside the
560
+ * JSON-RPC header builder before `inFlight` is assigned, defeating the
561
+ * single-flight guard. Returning `null` here breaks that recursion
562
+ * deterministically.
563
+ */
564
+ async getToken(e) {
565
+ return e === C || e === R ? null : this.grpcGatedMethods.has(e) ? this.getTokenForSubject("grpc") : this.authGatedMethods.has(e) ? this.getTokenForSubject("jsonrpc") : null;
566
+ }
567
+ /**
568
+ * Drop both cached tokens. Next `getToken` call re-acquires the slot
569
+ * that's actually needed. Called by `JsonRpcClient` on wire
570
+ * `auth_expired` responses; the client doesn't tell us which subject
571
+ * expired, so we evict both to stay correct under either.
572
+ *
573
+ * Worst case is one extra round-trip on the slot that was still fresh,
574
+ * which is cheaper than carrying a `Subject` argument through
575
+ * `BearerTokenProvider`.
576
+ */
577
+ invalidate() {
578
+ this.cachedJsonRpc = null, this.cachedGrpc = null;
579
+ }
580
+ async getTokenForSubject(e) {
581
+ const r = e === "grpc" ? this.cachedGrpc : this.cachedJsonRpc;
582
+ return r && this.now() + this.refreshSkewSecs < r.expiresAt ? r.token : (await this.acquireSingleFlight(e)).token;
583
+ }
584
+ /**
585
+ * Swap in a different transport for subsequent token-issuing calls.
586
+ * Used by the registry when a later caller registers the same
587
+ * `peginTxid` against a different `baseUrl` — the cached token
588
+ * (bound to identity, not transport) stays valid, but future
589
+ * refreshes hit the new URL. An in-flight acquire keeps using the
590
+ * old client (it captured the reference); next call uses the new.
591
+ */
592
+ setClient(e) {
593
+ this.client = e;
594
+ }
595
+ /** Current subject-specific pin, exposed for registry identity checks. */
596
+ getPinnedServerPubkey(e) {
597
+ return e === "grpc" ? this.grpcPinnedServerPubkey : this.jsonRpcPinnedServerPubkey;
598
+ }
599
+ acquireSingleFlight(e) {
600
+ const r = e === "grpc" ? this.inFlightGrpc : this.inFlightJsonRpc;
601
+ if (r) return r;
602
+ const n = e === "grpc" ? R : C, s = (async () => {
603
+ try {
604
+ const i = await this.acquireAndVerify(e, n, !0);
605
+ return e === "grpc" ? this.cachedGrpc = i : this.cachedJsonRpc = i, i;
606
+ } finally {
607
+ e === "grpc" ? this.inFlightGrpc = null : this.inFlightJsonRpc = null;
608
+ }
609
+ })();
610
+ return e === "grpc" ? this.inFlightGrpc = s : this.inFlightJsonRpc = s, s;
611
+ }
612
+ async acquireAndVerify(e, r, n) {
613
+ const s = await this.client.call(r, {
614
+ pegin_txid: this.peginTxid,
615
+ auth_anchor: this.authAnchorHex
616
+ }), i = this.getPinnedServerPubkey(e);
617
+ try {
618
+ if (de({
619
+ proof: s.server_identity,
620
+ pinnedServerPubkey: i,
621
+ now: this.now()
622
+ }), typeof s.token != "string" || s.token.length === 0)
623
+ throw new Error(
624
+ `VpTokenProvider: invalid token in acquire response (expected non-empty string, got ${typeof s.token})`
625
+ );
626
+ const o = this.now();
627
+ if (!Number.isSafeInteger(s.expires_at) || s.expires_at <= o || s.expires_at > U)
628
+ throw new Error(
629
+ `VpTokenProvider: invalid expires_at in acquire response (got ${JSON.stringify(s.expires_at)}; must be a safe integer in (${o}, ${U}])`
630
+ );
631
+ return Be({
632
+ token: s.token,
633
+ ephemeralPubkeyHex: s.server_identity.ephemeral_pubkey,
634
+ expectedIssuerXOnlyPubkey: i,
635
+ expectedSubject: e === "grpc" ? Pe : Ae,
636
+ expectedAudienceXOnlyPubkey: this.expectedAudienceXOnlyPubkey,
637
+ responseExpiresAt: s.expires_at,
638
+ serverIdentityExpiresAt: s.server_identity.expires_at,
639
+ now: o
640
+ }), {
641
+ token: s.token,
642
+ expiresAt: s.expires_at
643
+ };
644
+ } catch (o) {
645
+ if (n && e === "jsonrpc" && this.refreshJsonRpcPinnedServerPubkey && qe(o)) {
646
+ const c = await this.refreshJsonRpcPinnedServerPubkey();
647
+ if (c !== this.jsonRpcPinnedServerPubkey)
648
+ return this.jsonRpcPinnedServerPubkey = c, this.acquireAndVerify(e, r, !1);
649
+ }
650
+ throw o;
651
+ }
652
+ }
653
+ }
654
+ function qe(t) {
655
+ return t instanceof p && t.reason === "pinned_pubkey_mismatch" || t instanceof a && t.reason === "issuer_mismatch";
656
+ }
657
+ class We {
658
+ constructor() {
659
+ u(this, "entries", /* @__PURE__ */ new Map());
660
+ }
661
+ /**
662
+ * Return the cached `VpTokenProvider` for `peginTxid` if one exists
663
+ * with matching anchor, provider, audience, and subject-specific issuer
664
+ * bindings, otherwise construct and cache a fresh provider. A mismatch
665
+ * throws — silent overwrite would mask derivation drift or cross-provider
666
+ * cache reuse. A legitimate live JSON-RPC key rotation is handled inside
667
+ * `VpTokenProvider` through its chain-backed refresh callback.
668
+ */
669
+ getOrCreate(e) {
670
+ const r = this.entries.get(e.peginTxid);
671
+ if (r) {
672
+ if (r.authAnchorHex !== e.authAnchorHex)
673
+ throw new Error(
674
+ `VpTokenRegistry: peginTxid ${e.peginTxid} already bound to authAnchorHex ${r.authAnchorHex.slice(0, 8)}…; got ${e.authAnchorHex.slice(0, 8)}…`
675
+ );
676
+ if (r.providerAddress.toLowerCase() !== e.providerAddress.toLowerCase())
677
+ throw new Error(
678
+ `VpTokenRegistry: peginTxid ${e.peginTxid} already bound to providerAddress ${r.providerAddress}; got ${e.providerAddress}`
679
+ );
680
+ if (r.provider.getPinnedServerPubkey("jsonrpc") !== e.pinnedServerPubkey)
681
+ throw new Error(
682
+ `VpTokenRegistry: peginTxid ${e.peginTxid} already bound to JSON-RPC pinnedServerPubkey ${r.provider.getPinnedServerPubkey("jsonrpc").slice(0, 8)}…; got ${e.pinnedServerPubkey.slice(0, 8)}…`
683
+ );
684
+ if (r.provider.getPinnedServerPubkey("grpc") !== e.grpcPinnedServerPubkey)
685
+ throw new Error(
686
+ `VpTokenRegistry: peginTxid ${e.peginTxid} already bound to gRPC pinnedServerPubkey ${r.provider.getPinnedServerPubkey("grpc").slice(0, 8)}…; got ${e.grpcPinnedServerPubkey.slice(0, 8)}…`
687
+ );
688
+ if (r.grpcKeyEpoch !== e.grpcKeyEpoch)
689
+ throw new Error(
690
+ `VpTokenRegistry: peginTxid ${e.peginTxid} already bound to gRPC key epoch ${r.grpcKeyEpoch.toString()}; got ${e.grpcKeyEpoch.toString()}`
691
+ );
692
+ if (r.expectedAudienceXOnlyPubkey.toLowerCase() !== e.expectedAudienceXOnlyPubkey.toLowerCase())
693
+ throw new Error(
694
+ `VpTokenRegistry: peginTxid ${e.peginTxid} already bound to expectedAudienceXOnlyPubkey ${r.expectedAudienceXOnlyPubkey.slice(0, 8)}…; got ${e.expectedAudienceXOnlyPubkey.slice(0, 8)}…`
695
+ );
696
+ return r.provider.setClient(e.client), r.provider;
697
+ }
698
+ const n = new Ke({
699
+ client: e.client,
700
+ peginTxid: e.peginTxid,
701
+ authAnchorHex: e.authAnchorHex,
702
+ pinnedServerPubkey: e.pinnedServerPubkey,
703
+ grpcPinnedServerPubkey: e.grpcPinnedServerPubkey,
704
+ refreshJsonRpcPinnedServerPubkey: e.refreshJsonRpcPinnedServerPubkey,
705
+ expectedAudienceXOnlyPubkey: e.expectedAudienceXOnlyPubkey,
706
+ authGatedMethods: ue,
707
+ grpcGatedMethods: he
708
+ });
709
+ return this.entries.set(e.peginTxid, {
710
+ provider: n,
711
+ authAnchorHex: e.authAnchorHex,
712
+ providerAddress: e.providerAddress,
713
+ grpcKeyEpoch: e.grpcKeyEpoch,
714
+ expectedAudienceXOnlyPubkey: e.expectedAudienceXOnlyPubkey
715
+ }), n;
716
+ }
717
+ /**
718
+ * Return the cached provider only when its request-facing identity matches.
719
+ * A missing entry is a normal cold-cache result; a binding mismatch throws
720
+ * so callers cannot attach one provider's bearer to another provider or
721
+ * depositor request.
722
+ */
723
+ peek(e) {
724
+ const r = this.entries.get(e.peginTxid);
725
+ if (r) {
726
+ if (r.providerAddress.toLowerCase() !== e.providerAddress.toLowerCase())
727
+ throw new Error(
728
+ `VpTokenRegistry: peginTxid ${e.peginTxid} already bound to providerAddress ${r.providerAddress}; got ${e.providerAddress}`
729
+ );
730
+ if (r.expectedAudienceXOnlyPubkey.toLowerCase() !== e.expectedAudienceXOnlyPubkey.toLowerCase())
731
+ throw new Error(
732
+ `VpTokenRegistry: peginTxid ${e.peginTxid} already bound to expectedAudienceXOnlyPubkey ${r.expectedAudienceXOnlyPubkey.slice(0, 8)}…; got ${e.expectedAudienceXOnlyPubkey.slice(0, 8)}…`
733
+ );
734
+ return r.provider;
735
+ }
736
+ }
737
+ /**
738
+ * Evict the entry for `peginTxid`. Idempotent. Called on terminal
739
+ * paths — activation success, user-cancel, or component unmount —
740
+ * so `authAnchorHex` doesn't outlive the deposit session.
741
+ */
742
+ release(e) {
743
+ this.entries.delete(e);
744
+ }
745
+ /**
746
+ * Wipe every cached entry. Test-only escape hatch — not exposed on
747
+ * the public {@link VpTokenRegistryPublic} singleton type.
748
+ *
749
+ * @internal
750
+ */
751
+ clear() {
752
+ this.entries.clear();
753
+ }
754
+ get size() {
755
+ return this.entries.size;
756
+ }
757
+ }
758
+ const W = new We();
759
+ function nr(t) {
760
+ var n;
761
+ const e = F(
762
+ t.baseUrl,
763
+ (n = t.options) == null ? void 0 : n.headers
764
+ ), r = W.getOrCreate({
765
+ client: e,
766
+ peginTxid: t.peginTxid,
767
+ authAnchorHex: t.authAnchorHex,
768
+ providerAddress: t.providerAddress,
769
+ pinnedServerPubkey: t.pinnedServerPubkey,
770
+ grpcPinnedServerPubkey: t.grpcPinnedServerPubkey,
771
+ grpcKeyEpoch: t.grpcKeyEpoch,
772
+ refreshJsonRpcPinnedServerPubkey: t.refreshJsonRpcPinnedServerPubkey,
773
+ expectedAudienceXOnlyPubkey: J(
774
+ t.depositorBtcPubkey
775
+ )
776
+ });
777
+ return new te(t.baseUrl, {
778
+ ...t.options,
779
+ tokenProvider: r
780
+ });
781
+ }
782
+ function ir(t) {
783
+ W.getOrCreate({
784
+ client: F(t.baseUrl, t.headers),
785
+ peginTxid: t.peginTxid,
786
+ authAnchorHex: t.authAnchorHex,
787
+ providerAddress: t.providerAddress,
788
+ pinnedServerPubkey: t.pinnedServerPubkey,
789
+ grpcPinnedServerPubkey: t.grpcPinnedServerPubkey,
790
+ grpcKeyEpoch: t.grpcKeyEpoch,
791
+ refreshJsonRpcPinnedServerPubkey: t.refreshJsonRpcPinnedServerPubkey,
792
+ expectedAudienceXOnlyPubkey: J(
793
+ t.depositorBtcPubkey
794
+ )
795
+ });
796
+ }
797
+ export {
798
+ p as S,
799
+ We as V,
800
+ W as a,
801
+ nr as c,
802
+ ir as p,
803
+ de as v
804
+ };
805
+ //# sourceMappingURL=primeVpAuth-D0ZnsjRi.js.map