@bestinslot/wallet-kit 0.14.3 → 0.15.0-rc.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 (34) hide show
  1. package/README.md +3 -1
  2. package/dist/balances-CcnzsQly.js +10930 -0
  3. package/dist/browser.js +110 -95
  4. package/dist/core.js +40 -34
  5. package/dist/{mint-CSk_3TfL.js → helpers-BvhfqxFB.js} +28787 -28389
  6. package/dist/node.js +726 -1166
  7. package/dist/react.js +4 -4
  8. package/dist/register-browser-providers-DxplsCV5.js +810 -0
  9. package/dist/types/api/helpers.d.ts +3 -0
  10. package/dist/types/api/swap.d.ts +3 -3
  11. package/dist/types/api/wallet.d.ts +2 -2
  12. package/dist/types/core/bis-providers.d.ts +49 -0
  13. package/dist/types/core/bis.d.ts +2 -0
  14. package/dist/types/core/bis_swap.d.ts +32 -7
  15. package/dist/types/core/brc20-send.d.ts +2 -1
  16. package/dist/types/core/chain-providers.d.ts +133 -0
  17. package/dist/types/core/helpers.d.ts +48 -31
  18. package/dist/types/core/mint.d.ts +82 -8
  19. package/dist/types/core/providers.d.ts +11 -3
  20. package/dist/types/core/sign-progress.d.ts +3 -1
  21. package/dist/types/core/store-network.d.ts +8 -0
  22. package/dist/types/core.d.ts +2 -0
  23. package/dist/types/provider/api.d.ts +20 -0
  24. package/dist/types/provider/bitcoin-wallet.d.ts +45 -0
  25. package/dist/types/provider/metamask.d.ts +6 -0
  26. package/dist/types/provider/phantom.d.ts +2 -0
  27. package/dist/types/provider/wallet-standard.d.ts +14 -0
  28. package/dist/types/types/common.d.ts +1 -1
  29. package/dist/types/types/global.d.ts +3 -5
  30. package/dist/vue.js +3 -3
  31. package/package.json +29 -25
  32. package/dist/balances-DYYM7kpl.js +0 -10363
  33. package/dist/register-browser-providers-BdTwEVwB.js +0 -500
  34. package/dist/types/provider/leather.d.ts +0 -2
@@ -0,0 +1,810 @@
1
+ import { M as f, L as K, h as S, N, O as gn, P as fn, Q as pn, R as hn, f as bn, J as b, K as C, U as G, e as h, I as H, Z as L, _ as v, X as B } from "./helpers-BvhfqxFB.js";
2
+ import { d as xn, r as yn } from "./balances-CcnzsQly.js";
3
+ const W = "[Best in Slot] wants to know your public key.";
4
+ function _(n) {
5
+ return f.from(n).toString("hex");
6
+ }
7
+ function mn(n, e) {
8
+ const t = G();
9
+ return e.toLowerCase().startsWith(t.bech32) ? b.payments.p2wpkh({ pubkey: n, network: t }).address : b.payments.p2pkh({ pubkey: n, network: t }).address;
10
+ }
11
+ function Pn(n, e, t) {
12
+ const s = [], i = f.from(e);
13
+ if (i.length === 65) {
14
+ const r = i[0] - 27, d = r & 3, a = r >= 4, p = xn.magicHash(n, G().messagePrefix), m = yn(p, i.subarray(1), d, a);
15
+ m && s.push(f.from(m));
16
+ } else {
17
+ let r = 0;
18
+ const d = i[r++] ?? 0;
19
+ for (let a = 0; a < d && r < i.length; a++) {
20
+ const p = i[r++] ?? 0;
21
+ s.push(i.subarray(r, r + p)), r += p;
22
+ }
23
+ }
24
+ const o = s.find(
25
+ (r) => r.length === 33 && mn(r, t) === t
26
+ );
27
+ return o ? o.toString("hex") : null;
28
+ }
29
+ function Tn(n) {
30
+ const { unsignedPsbtHex: e, paymentAddr: t, ordAddr: s, ordAddrIdxes: i, noSignIdxes: o } = n, r = b.Psbt.fromHex(e), d = [];
31
+ for (let a = 0; a < r.inputCount; a++)
32
+ o?.includes(a) || i.includes(a) || d.push(a);
33
+ return t === s ? [{ address: t, signingIndexes: [...d, ...i] }] : [
34
+ { address: t, signingIndexes: d },
35
+ { address: s, signingIndexes: i }
36
+ ].filter((a) => a.signingIndexes.length > 0);
37
+ }
38
+ function U(n, e) {
39
+ const t = b.Psbt.fromBuffer(f.from(n));
40
+ C(t, e);
41
+ const s = t.extractTransaction();
42
+ return { txId: s.getId(), signedTxHex: s.toHex() };
43
+ }
44
+ function z(n, e) {
45
+ const t = () => {
46
+ const c = e();
47
+ if (!c) throw new Error(`${n} is not installed.`);
48
+ return c;
49
+ }, s = async (c, l) => {
50
+ try {
51
+ const u = await t().signMessage(
52
+ c,
53
+ new TextEncoder().encode(l)
54
+ );
55
+ return _(u);
56
+ } catch (u) {
57
+ throw console.error("Failed to sign message", u), new Error("Failed to sign message.");
58
+ }
59
+ }, i = async () => {
60
+ const c = t(), l = await c.connect(), u = l.find((g) => g.purpose === "payment");
61
+ if (!u) throw new Error(`${n} returned no payment address.`);
62
+ const w = l.find((g) => g.purpose === "ordinals"), x = async (g) => {
63
+ if (g.publicKey) return g.publicKey;
64
+ const y = await c.signMessage(
65
+ g.address,
66
+ new TextEncoder().encode(W)
67
+ ), M = Pn(W, y, g.address);
68
+ if (!M) throw new Error(`Could not read the public key of ${g.address}.`);
69
+ return M;
70
+ };
71
+ return !w || w.address === u.address ? [{ address: u.address, pubkey: await x(u), purpose: "all" }] : [
72
+ { address: u.address, pubkey: await x(u), purpose: "payment" },
73
+ { address: w.address, pubkey: await x(w), purpose: "ordinals" }
74
+ ];
75
+ }, o = async (c, l, u) => {
76
+ const w = u || (l === "ordinals" ? bn() : S())?.address;
77
+ if (!w) throw new Error("No wallet address to sign with.");
78
+ return s(w, c);
79
+ }, r = async (c) => {
80
+ const l = S()?.address;
81
+ if (!l) throw new Error("No payment wallet found.");
82
+ return { signature: await s(l, c), address: l };
83
+ }, d = async (c, l, u) => {
84
+ const [w] = await t().signPsbts([
85
+ {
86
+ psbt: f.from(c, "base64"),
87
+ inputs: u.map((y) => ({
88
+ address: y.address,
89
+ signingIndexes: y.signingIndexes
90
+ }))
91
+ }
92
+ ]);
93
+ if (!w) throw new Error(`${n} returned no signed PSBT.`);
94
+ if (!l) return _(w);
95
+ const { txId: x, signedTxHex: g } = U(w);
96
+ if (await K([g]) == null) throw new Error("Failed to broadcast.");
97
+ return x;
98
+ }, a = async (c) => {
99
+ const l = await t().signPsbts(
100
+ c.map((u) => ({
101
+ psbt: f.from(u.unsignedPsbtHex, "hex"),
102
+ inputs: Tn(u)
103
+ }))
104
+ );
105
+ if (l.length !== c.length)
106
+ throw new Error(`${n} returned the wrong PSBT count.`);
107
+ return l.map((u, w) => U(u, c[w].noSignIdxes));
108
+ }, p = async (c, l, u, w, x, g) => {
109
+ const [y] = await a([
110
+ { unsignedPsbtHex: c, paymentAddr: l, ordAddr: u, ordAddrIdxes: w, useTweakSignerIdxes: x, noSignIdxes: g }
111
+ ]);
112
+ return y;
113
+ };
114
+ return {
115
+ getWallets: i,
116
+ getAccounts: async () => e()?.accounts() ?? [],
117
+ onAccountsChanged: (c) => e()?.onAccountsChanged(c) ?? (() => {
118
+ }),
119
+ signMessage: o,
120
+ signMessageDeterministic: r,
121
+ sendBTC: async (c, l, u) => {
122
+ if (!Number.isInteger(c) || c <= 0)
123
+ throw new Error("amountSats must be a positive integer (satoshis).");
124
+ if (u == null || !Number.isFinite(u) || u <= 0)
125
+ throw new Error(`feeRate (sat/vB) must be a positive number for ${n}.`);
126
+ const w = S();
127
+ if (!w) throw new Error("No payment wallet found.");
128
+ const x = new N(!1, null, w.address, null, w.pubkey), g = new N(!1, null, l, null, null), y = await gn(w.address), M = fn(
129
+ y,
130
+ [],
131
+ x,
132
+ [],
133
+ g,
134
+ x,
135
+ u,
136
+ c
137
+ ), ln = await pn(M.tx, y, x, []), X = await p(ln.toHex(), w.address, w.address, []), F = await hn([X.signedTxHex]);
138
+ if (F == null) throw new Error("Send BTC validation failed (testmempoolaccept failed).");
139
+ for (const O of F)
140
+ if (!O.allowed) throw new Error(O["reject-reason"]);
141
+ if (await K([X.signedTxHex]) == null)
142
+ throw new Error("Failed to broadcast transaction.");
143
+ return X.txId;
144
+ },
145
+ signPSBT: d,
146
+ sign: p,
147
+ getSignMany: () => e()?.signsManyAtOnce ? a : null
148
+ };
149
+ }
150
+ var Sn = function(n, e, t, s) {
151
+ if (t === "a" && !s) throw new TypeError("Private accessor was defined without a getter");
152
+ if (typeof e == "function" ? n !== e || !s : !e.has(n)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
153
+ return t === "m" ? s : t === "a" ? s.call(n) : s ? s.value : e.get(n);
154
+ }, kn = function(n, e, t, s, i) {
155
+ if (s === "m") throw new TypeError("Private method is not writable");
156
+ if (s === "a" && !i) throw new TypeError("Private accessor was defined without a setter");
157
+ if (typeof e == "function" ? n !== e || !i : !e.has(n)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
158
+ return s === "a" ? i.call(n, t) : i ? i.value = t : e.set(n, t), t;
159
+ }, A;
160
+ let k;
161
+ const $ = /* @__PURE__ */ new Set();
162
+ function En(n) {
163
+ E = void 0, $.add(n);
164
+ }
165
+ function vn(n) {
166
+ E = void 0, $.delete(n);
167
+ }
168
+ const T = {};
169
+ function Bn() {
170
+ if (k || (k = Object.freeze({ register: D, get: In, on: Mn }), typeof window > "u"))
171
+ return k;
172
+ const n = Object.freeze({ register: D });
173
+ try {
174
+ window.addEventListener("wallet-standard:register-wallet", ({ detail: e }) => e(n));
175
+ } catch (e) {
176
+ console.error(`wallet-standard:register-wallet event listener could not be added
177
+ `, e);
178
+ }
179
+ try {
180
+ window.dispatchEvent(new An(n));
181
+ } catch (e) {
182
+ console.error(`wallet-standard:app-ready event could not be dispatched
183
+ `, e);
184
+ }
185
+ return k;
186
+ }
187
+ function D(...n) {
188
+ return n = n.filter((e) => !$.has(e)), n.length ? (n.forEach((e) => En(e)), T.register?.forEach((e) => R(() => e(...n))), function() {
189
+ n.forEach((t) => vn(t)), T.unregister?.forEach((t) => R(() => t(...n)));
190
+ }) : () => {
191
+ };
192
+ }
193
+ let E;
194
+ function In() {
195
+ return E || (E = [...$]), E;
196
+ }
197
+ function Mn(n, e) {
198
+ return T[n]?.push(e) || (T[n] = [e]), function() {
199
+ T[n] = T[n]?.filter((s) => e !== s);
200
+ };
201
+ }
202
+ function R(n) {
203
+ try {
204
+ n();
205
+ } catch (e) {
206
+ console.error(e);
207
+ }
208
+ }
209
+ class An extends Event {
210
+ get detail() {
211
+ return Sn(this, A, "f");
212
+ }
213
+ get type() {
214
+ return "wallet-standard:app-ready";
215
+ }
216
+ constructor(e) {
217
+ super("wallet-standard:app-ready", {
218
+ bubbles: !1,
219
+ cancelable: !1,
220
+ composed: !1
221
+ }), A.set(this, void 0), kn(this, A, e, "f");
222
+ }
223
+ /** @deprecated */
224
+ preventDefault() {
225
+ throw new Error("preventDefault cannot be called");
226
+ }
227
+ /** @deprecated */
228
+ stopImmediatePropagation() {
229
+ throw new Error("stopImmediatePropagation cannot be called");
230
+ }
231
+ /** @deprecated */
232
+ stopPropagation() {
233
+ throw new Error("stopPropagation cannot be called");
234
+ }
235
+ }
236
+ A = /* @__PURE__ */ new WeakMap();
237
+ const V = "bitcoin:connect", J = "bitcoin:signTransaction", Q = "bitcoin:signMessage", Cn = ["bitcoin:events", "standard:events"];
238
+ let j;
239
+ function Y(n) {
240
+ return typeof window > "u" ? null : (j ??= Bn(), j.get().find(
241
+ (e) => n.test(e.name) && V in e.features && J in e.features && Q in e.features
242
+ ) ?? null);
243
+ }
244
+ function Hn(n) {
245
+ return n.length === 33 || n.length === 32 ? f.from(Array.from(n)).toString("hex") : null;
246
+ }
247
+ function Z(n) {
248
+ const e = (r) => n.features[r], t = () => `bitcoin:${h()}`, s = (r) => {
249
+ const d = n.accounts.find((a) => a.address === r);
250
+ if (!d) throw new Error(`${n.name} has no connected account ${r}.`);
251
+ return d;
252
+ }, i = async () => {
253
+ if (!n.chains.includes(t()))
254
+ throw new Error(`${n.name} does not support Bitcoin ${h()}.`);
255
+ const { accounts: r } = await e(V).connect({
256
+ purposes: ["payment", "ordinals"]
257
+ });
258
+ return r.slice(0, 2).map((d, a) => ({
259
+ address: d.address,
260
+ publicKey: Hn(d.publicKey),
261
+ purpose: a === 0 ? "payment" : "ordinals"
262
+ }));
263
+ }, o = Cn.map((r) => n.features[r]).find(
264
+ Boolean
265
+ );
266
+ return {
267
+ connect: i,
268
+ accounts: () => n.accounts.map((r) => r.address),
269
+ onAccountsChanged: (r) => o?.on("change", ({ accounts: d }) => {
270
+ d && r(d.map((a) => a.address));
271
+ }) ?? (() => {
272
+ }),
273
+ signMessage: async (r, d) => {
274
+ const [a] = await e(Q).signMessage({
275
+ account: s(r),
276
+ message: d
277
+ });
278
+ if (!a) throw new Error(`${n.name} returned no signature.`);
279
+ return a.signature;
280
+ },
281
+ signPsbts: async (r) => (await e(J).signTransaction(
282
+ ...r.map((a) => ({
283
+ psbt: a.psbt,
284
+ inputsToSign: a.inputs.map((p) => ({
285
+ account: s(p.address),
286
+ signingIndexes: p.signingIndexes
287
+ })),
288
+ chain: t()
289
+ }))
290
+ )).map((a) => a.signedPsbt),
291
+ signsManyAtOnce: !0
292
+ };
293
+ }
294
+ const $n = z("MetaMask", () => {
295
+ const n = Y(/metamask/i);
296
+ return n ? Z(n) : null;
297
+ });
298
+ async function Xn() {
299
+ if (!window.okxwallet) throw new Error("OKX extension not found.");
300
+ const n = h();
301
+ let e;
302
+ if (n === "mainnet") e = await window.okxwallet.bitcoin.connect();
303
+ else if (n === "testnet") e = await window.okxwallet.bitcoinTestnet.connect();
304
+ else if (n === "signet") e = await window.okxwallet.bitcoinSignet.connect();
305
+ else throw new Error("Unsupported network for OKX.");
306
+ if (!e) throw new Error("Error fetching wallet data.");
307
+ return [
308
+ {
309
+ address: e.address,
310
+ pubkey: e.publicKey,
311
+ purpose: "all"
312
+ }
313
+ ];
314
+ }
315
+ function q() {
316
+ const n = h();
317
+ if (n === "mainnet") return window.okxwallet?.bitcoin;
318
+ if (n === "testnet") return window.okxwallet?.bitcoinTestnet;
319
+ if (n === "signet") return window.okxwallet?.bitcoinSignet;
320
+ }
321
+ async function Fn() {
322
+ const n = q();
323
+ return typeof n?.getAccounts != "function" ? [] : await n.getAccounts() ?? [];
324
+ }
325
+ function On(n) {
326
+ const e = q();
327
+ if (typeof e?.on != "function") return () => {
328
+ };
329
+ const t = (s) => n(s?.address ? [s.address] : []);
330
+ return e.on("accountChanged", t), () => e.removeListener?.("accountChanged", t);
331
+ }
332
+ async function Kn(n) {
333
+ if (!window.okxwallet) throw new Error("OKX extension not found.");
334
+ const e = h();
335
+ let t;
336
+ if (e === "mainnet")
337
+ t = await window.okxwallet.bitcoin.signMessage(n, "bip322-simple");
338
+ else if (e === "testnet")
339
+ t = await window.okxwallet.bitcoinTestnet.signMessage(n, "bip322-simple");
340
+ else if (e === "signet")
341
+ t = await window.okxwallet.bitcoinSignet.signMessage(n, "bip322-simple");
342
+ else throw new Error("Unsupported network for OKX.");
343
+ return f.from(t, "base64").toString("hex");
344
+ }
345
+ async function Nn(n) {
346
+ if (!window.okxwallet) throw new Error("OKX extension not found.");
347
+ const e = S();
348
+ if (!e) throw new Error("No payment wallet found.");
349
+ const t = e.address, s = h();
350
+ let i;
351
+ if (s === "mainnet")
352
+ i = await window.okxwallet.bitcoin.signMessage(n, "ecdsa");
353
+ else if (s === "testnet")
354
+ i = await window.okxwallet.bitcoinTestnet.signMessage(n, "ecdsa");
355
+ else if (s === "signet")
356
+ i = await window.okxwallet.bitcoinSignet.signMessage(n, "ecdsa");
357
+ else throw new Error("Unsupported network for OKX.");
358
+ return {
359
+ signature: f.from(i, "base64").toString("hex"),
360
+ address: t
361
+ };
362
+ }
363
+ async function Wn(n, e) {
364
+ if (!window.okxwallet) throw new Error("OKX extension not found.");
365
+ const t = h();
366
+ let s;
367
+ if (t === "mainnet")
368
+ s = await window.okxwallet.bitcoin.sendBitcoin(e, n);
369
+ else if (t === "testnet")
370
+ s = await window.okxwallet.bitcoinTestnet.sendBitcoin(e, n);
371
+ else if (t === "signet")
372
+ s = await window.okxwallet.bitcoinSignet.sendBitcoin(e, n);
373
+ else
374
+ throw new Error("Unsupported network for OKX.");
375
+ return s;
376
+ }
377
+ async function nn(n, e, t) {
378
+ const s = L(n);
379
+ let i = null;
380
+ if (t.length !== 0) {
381
+ const o = [];
382
+ for (const r of t)
383
+ for (let d = 0; d < r.signingIndexes.length; d++)
384
+ o.push({
385
+ address: r.address,
386
+ index: r.signingIndexes[d]
387
+ // disableTweakSigner: i == 0 ? true : false // TODO: remove this hack!!
388
+ });
389
+ i = {
390
+ autoFinalized: !0,
391
+ toSignInputs: o
392
+ };
393
+ }
394
+ if (h() === "mainnet") {
395
+ let o = null;
396
+ return i ? o = await window.okxwallet.bitcoin.signPsbt(s, i) : o = await window.okxwallet.bitcoin.signPsbt(s), e && await window.okxwallet.bitcoin.pushPsbt(o), o;
397
+ } else if (h() === "testnet") {
398
+ if (e)
399
+ throw new Error("Cannot broadcast on testnet with okx");
400
+ let o = null;
401
+ return i ? o = await window.okxwallet.bitcoinTestnet.signPsbt(s, i) : o = await window.okxwallet.bitcoinTestnet.signPsbt(s), o;
402
+ } else if (h() === "signet") {
403
+ if (e)
404
+ throw new Error("Cannot broadcast on signet with okx");
405
+ let o = null;
406
+ return i ? o = await window.okxwallet.bitcoinSignet.signPsbt(s, i) : o = await window.okxwallet.bitcoinSignet.signPsbt(s), o;
407
+ } else
408
+ throw new Error("Unsupported network for OKX.");
409
+ }
410
+ function en(n) {
411
+ const { unsignedPsbtHex: e, paymentAddr: t, ordAddr: s, ordAddrIdxes: i, noSignIdxes: o } = n;
412
+ if (!t) return [];
413
+ const r = b.Psbt.fromHex(e), d = [];
414
+ for (let a = 0; a < r.inputCount; a++)
415
+ o && o.includes(a) || i.includes(a) || d.push(a);
416
+ return [
417
+ { address: t, signingIndexes: d },
418
+ { address: s, signingIndexes: i }
419
+ ];
420
+ }
421
+ function tn(n, e) {
422
+ const t = b.Psbt.fromHex(n);
423
+ C(t, e);
424
+ const s = t.extractTransaction();
425
+ return { txId: s.getId(), signedTxHex: s.toHex() };
426
+ }
427
+ async function _n(n, e, t, s, i, o) {
428
+ const r = en({
429
+ unsignedPsbtHex: n,
430
+ paymentAddr: e,
431
+ ordAddr: t,
432
+ ordAddrIdxes: s,
433
+ noSignIdxes: o
434
+ }), d = await nn(H(n), !1, r);
435
+ return tn(d, o);
436
+ }
437
+ function sn() {
438
+ const n = h();
439
+ if (n === "mainnet") return window.okxwallet?.bitcoin;
440
+ if (n === "testnet") return window.okxwallet?.bitcoinTestnet;
441
+ if (n === "signet") return window.okxwallet?.bitcoinSignet;
442
+ }
443
+ async function Un(n) {
444
+ const e = sn();
445
+ if (typeof e?.signPsbts != "function")
446
+ throw new Error("OKX cannot sign several PSBTs at once.");
447
+ const t = await e.signPsbts(
448
+ n.map((s) => s.unsignedPsbtHex),
449
+ n.map((s) => {
450
+ const i = en(s).flatMap(
451
+ (o) => o.signingIndexes.map((r) => ({ address: o.address, index: r }))
452
+ );
453
+ return i.length > 0 ? { autoFinalized: !0, toSignInputs: i } : { autoFinalized: !0 };
454
+ })
455
+ );
456
+ if (t.length !== n.length) throw new Error("OKX returned the wrong PSBT count.");
457
+ return t.map((s, i) => tn(s, n[i].noSignIdxes));
458
+ }
459
+ function Dn() {
460
+ return typeof window < "u" && typeof sn()?.signPsbts == "function" ? Un : null;
461
+ }
462
+ const Rn = {
463
+ getWallets: Xn,
464
+ getAccounts: Fn,
465
+ onAccountsChanged: On,
466
+ signMessage: Kn,
467
+ signMessageDeterministic: Nn,
468
+ sendBTC: Wn,
469
+ signPSBT: nn,
470
+ sign: _n,
471
+ getSignMany: Dn
472
+ };
473
+ function jn(n) {
474
+ return typeof n != "string" ? n : /^[0-9a-f]+$/i.test(n) ? f.from(n, "hex") : f.from(n, "base64");
475
+ }
476
+ function Gn(n) {
477
+ let e = [];
478
+ return {
479
+ connect: async () => {
480
+ const t = await n.requestAccounts();
481
+ return e = t.map((s) => s.address), t.map((s) => ({
482
+ address: s.address,
483
+ publicKey: s.publicKey || null,
484
+ purpose: s.purpose
485
+ }));
486
+ },
487
+ accounts: () => e,
488
+ onAccountsChanged: (t) => {
489
+ if (!n.on) return () => {
490
+ };
491
+ const s = (i) => {
492
+ e = (i ?? []).map((o) => o.address), t(e);
493
+ };
494
+ return n.on("accountsChanged", s), () => n.removeListener?.("accountsChanged", s);
495
+ },
496
+ signMessage: async (t, s) => (await n.signMessage(t, s)).signature,
497
+ signPsbts: async (t) => {
498
+ const s = [];
499
+ for (const i of t)
500
+ s.push(jn(await n.signPSBT(i.psbt, { inputsToSign: i.inputs })));
501
+ return s;
502
+ },
503
+ signsManyAtOnce: !1
504
+ };
505
+ }
506
+ function Ln() {
507
+ const n = Y(/phantom/i);
508
+ if (n) return Z(n);
509
+ if (typeof window > "u") return null;
510
+ const e = window.phantom?.bitcoin;
511
+ return e?.isPhantom ? Gn(e) : null;
512
+ }
513
+ const zn = z("Phantom", Ln);
514
+ async function P() {
515
+ if (!window.unisat) throw new Error("UniSat not found.");
516
+ const n = await window.unisat.getNetwork(), e = h();
517
+ n !== "livenet" && e === "mainnet" ? await window.unisat.switchNetwork("livenet") : n === "livenet" && (e === "testnet" || e === "signet") && await window.unisat.switchNetwork("testnet");
518
+ }
519
+ async function Vn() {
520
+ await P();
521
+ const n = await window.unisat?.requestAccounts(), e = await window.unisat?.getPublicKey();
522
+ if (!n) throw new Error("Failed to get wallets.");
523
+ return n.map((s) => ({
524
+ address: s,
525
+ pubkey: e,
526
+ purpose: "all"
527
+ }));
528
+ }
529
+ async function Jn() {
530
+ return typeof window.unisat?.getAccounts != "function" ? [] : await window.unisat.getAccounts() ?? [];
531
+ }
532
+ function Qn(n) {
533
+ const e = window.unisat;
534
+ if (!e?.on) return () => {
535
+ };
536
+ const t = (s) => n(s ?? []);
537
+ return e.on("accountsChanged", t), () => e.removeListener?.("accountsChanged", t);
538
+ }
539
+ async function Yn(n) {
540
+ await P();
541
+ try {
542
+ const e = await window.unisat.signMessage(n, "bip322-simple");
543
+ return f.from(e, "base64").toString("hex");
544
+ } catch (e) {
545
+ throw console.error("Failed to sign message", e), new Error("Failed to sign message.");
546
+ }
547
+ }
548
+ async function Zn(n) {
549
+ await P();
550
+ const e = S();
551
+ if (!e) throw new Error("No payment wallet found.");
552
+ const t = e.address;
553
+ try {
554
+ const s = await window.unisat.signMessage(n, "ecdsa");
555
+ return {
556
+ signature: f.from(s, "base64").toString("hex"),
557
+ address: t
558
+ };
559
+ } catch (s) {
560
+ throw console.error("Failed to sign message", s), new Error("Failed to sign message.");
561
+ }
562
+ }
563
+ async function qn(n, e) {
564
+ await P();
565
+ try {
566
+ return await window.unisat.sendBitcoin(e, n);
567
+ } catch (t) {
568
+ throw console.error("Failed to send BTC", t), new Error("Failed to send BTC.");
569
+ }
570
+ }
571
+ async function on(n, e, t) {
572
+ if (await P(), !window.unisat) throw new Error("UniSat not found.");
573
+ const s = L(n);
574
+ let i = null;
575
+ return t.length === 0 ? i = await window.unisat.signPsbt(s) : i = await window.unisat.signPsbt(s, rn(t)), e && (i = await window.unisat.pushPsbt(i)), i;
576
+ }
577
+ function rn(n) {
578
+ const e = [];
579
+ for (const t of n)
580
+ for (let s = 0; s < t.signingIndexes.length; s++)
581
+ e.push({
582
+ address: t.address,
583
+ index: t.signingIndexes[s],
584
+ useTweakedSigner: t.useTweakedSigner ? t.useTweakedSigner[s] : void 0
585
+ });
586
+ return { autoFinalized: !0, toSignInputs: e };
587
+ }
588
+ function an(n) {
589
+ const { unsignedPsbtHex: e, paymentAddr: t, ordAddr: s, ordAddrIdxes: i, useTweakSignerIdxes: o, noSignIdxes: r } = n;
590
+ if (!t) return [];
591
+ const d = b.Psbt.fromHex(e), a = [], p = [], m = [];
592
+ for (let c = 0; c < d.inputCount; c++)
593
+ if (!(r && r.includes(c))) {
594
+ if (i.includes(c)) {
595
+ o && o.includes(c) ? m.push(!0) : o && m.push(!1);
596
+ continue;
597
+ }
598
+ a.push(c), o && o.includes(c) ? p.push(!0) : o && p.push(!1);
599
+ }
600
+ return [
601
+ {
602
+ address: t,
603
+ signingIndexes: a,
604
+ useTweakedSigner: p
605
+ },
606
+ {
607
+ address: s,
608
+ signingIndexes: i,
609
+ useTweakedSigner: m
610
+ }
611
+ ];
612
+ }
613
+ function dn(n, e) {
614
+ const t = b.Psbt.fromHex(n);
615
+ C(t, e);
616
+ const s = t.extractTransaction();
617
+ return { txId: s.getId(), signedTxHex: s.toHex() };
618
+ }
619
+ async function ne(n, e, t, s, i, o) {
620
+ const r = an({
621
+ unsignedPsbtHex: n,
622
+ paymentAddr: e,
623
+ ordAddr: t,
624
+ ordAddrIdxes: s,
625
+ useTweakSignerIdxes: i,
626
+ noSignIdxes: o
627
+ }), d = await on(H(n), !1, r);
628
+ return dn(d, o);
629
+ }
630
+ async function ee(n) {
631
+ if (await P(), !window.unisat?.signPsbts) throw new Error("UniSat cannot sign several PSBTs at once.");
632
+ const e = await window.unisat.signPsbts(
633
+ n.map((t) => t.unsignedPsbtHex),
634
+ n.map((t) => rn(an(t)))
635
+ );
636
+ if (e.length !== n.length) throw new Error("UniSat returned the wrong PSBT count.");
637
+ return e.map((t, s) => dn(t, n[s].noSignIdxes));
638
+ }
639
+ function te() {
640
+ return typeof window < "u" && typeof window.unisat?.signPsbts == "function" ? ee : null;
641
+ }
642
+ const se = {
643
+ checkNetwork: P,
644
+ getWallets: Vn,
645
+ getAccounts: Jn,
646
+ onAccountsChanged: Qn,
647
+ signMessage: Yn,
648
+ signMessageDeterministic: Zn,
649
+ sendBTC: qn,
650
+ signPSBT: on,
651
+ sign: ne,
652
+ getSignMany: te
653
+ };
654
+ function I() {
655
+ const n = h();
656
+ if (n === "mainnet") return "Mainnet";
657
+ if (n === "testnet") return "Testnet4";
658
+ if (n === "signet") return "Signet";
659
+ throw new Error("Unknown BTC network type.");
660
+ }
661
+ async function ie() {
662
+ if (!window.XverseProviders?.BitcoinProvider) throw new Error("Xverse extension not found.");
663
+ const n = v({
664
+ purposes: ["ordinals", "payment"],
665
+ message: "[Best in Slot] wants to know your addresses!",
666
+ network: {
667
+ type: I()
668
+ }
669
+ }), e = await window.XverseProviders.BitcoinProvider?.connect(n);
670
+ if (!e) throw new Error("Error fetching wallet data.");
671
+ return e.addresses.map((s) => ({
672
+ address: s.address,
673
+ pubkey: s.publicKey,
674
+ purpose: s.purpose
675
+ }));
676
+ }
677
+ async function oe(n, e, t) {
678
+ if (!window.XverseProviders?.BitcoinProvider) throw new Error("Xverse extension not found.");
679
+ const s = v({
680
+ address: t,
681
+ message: n,
682
+ protocol: "BIP322",
683
+ network: {
684
+ type: I()
685
+ }
686
+ }), i = await window.XverseProviders.BitcoinProvider.signMessage(s).catch(
687
+ (o) => {
688
+ throw console.error("Failed to sign message.", o), new Error("Failed to sign message.");
689
+ }
690
+ );
691
+ return f.from(i, "base64").toString("hex");
692
+ }
693
+ async function re(n) {
694
+ if (!window.XverseProviders?.BitcoinProvider) throw new Error("Xverse extension not found.");
695
+ const e = S();
696
+ if (!e) throw new Error("No payment wallet found.");
697
+ const t = e.address, s = v({
698
+ address: t,
699
+ message: n,
700
+ protocol: "ECDSA",
701
+ network: {
702
+ type: I()
703
+ }
704
+ }), i = await window.XverseProviders.BitcoinProvider.signMessage(s).catch(
705
+ (o) => {
706
+ throw console.error("Failed to sign message.", o), new Error("Failed to sign message.");
707
+ }
708
+ );
709
+ return {
710
+ signature: f.from(i, "base64").toString("hex"),
711
+ address: t
712
+ };
713
+ }
714
+ async function ae(n, e) {
715
+ if (!window.XverseProviders?.BitcoinProvider) throw new Error("Xverse extension not found.");
716
+ return (await window.XverseProviders.BitcoinProvider.request("sendTransfer", {
717
+ recipients: [
718
+ {
719
+ address: e,
720
+ amount: n
721
+ }
722
+ ]
723
+ })).result.txid;
724
+ }
725
+ async function cn(n, e, t, s) {
726
+ if (!window.XverseProviders?.BitcoinProvider) throw new Error("Xverse extension not found.");
727
+ const i = v({
728
+ network: {
729
+ type: I()
730
+ },
731
+ message: s || "Sign Transaction",
732
+ psbtBase64: n,
733
+ broadcast: e,
734
+ inputsToSign: t
735
+ });
736
+ return await window.XverseProviders.BitcoinProvider.signTransaction(i);
737
+ }
738
+ function un(n) {
739
+ const { unsignedPsbtHex: e, paymentAddr: t, ordAddr: s, ordAddrIdxes: i, noSignIdxes: o } = n, r = b.Psbt.fromHex(e), d = [];
740
+ for (let a = 0; a < r.inputCount; a++)
741
+ o && o.includes(a) || i.includes(a) || d.push(a);
742
+ return [
743
+ {
744
+ address: t,
745
+ signingIndexes: d,
746
+ sigHash: b.Transaction.SIGHASH_DEFAULT
747
+ },
748
+ {
749
+ address: s,
750
+ signingIndexes: i,
751
+ sigHash: b.Transaction.SIGHASH_DEFAULT
752
+ }
753
+ ];
754
+ }
755
+ function wn(n, e) {
756
+ const t = b.Psbt.fromBase64(n);
757
+ C(t, e);
758
+ const s = t.extractTransaction();
759
+ return { txId: s.getId(), signedTxHex: s.toHex() };
760
+ }
761
+ async function de(n, e, t, s, i, o) {
762
+ const r = await cn(
763
+ H(n),
764
+ !1,
765
+ un({
766
+ unsignedPsbtHex: n,
767
+ paymentAddr: e,
768
+ ordAddr: t,
769
+ ordAddrIdxes: s,
770
+ noSignIdxes: o
771
+ }),
772
+ "sign this pls"
773
+ );
774
+ return wn(r.psbtBase64, o);
775
+ }
776
+ async function ce(n) {
777
+ const e = window.XverseProviders?.BitcoinProvider;
778
+ if (typeof e?.signMultipleTransactions != "function")
779
+ throw new Error("Xverse cannot sign several PSBTs at once.");
780
+ const t = v({
781
+ network: { type: I() },
782
+ message: "Sign Transactions",
783
+ psbts: n.map((i) => ({
784
+ psbtBase64: H(i.unsignedPsbtHex),
785
+ inputsToSign: un(i)
786
+ }))
787
+ }), s = await e.signMultipleTransactions(t);
788
+ if (s.length !== n.length) throw new Error("Xverse returned the wrong PSBT count.");
789
+ return s.map((i, o) => wn(i.psbtBase64, n[o].noSignIdxes));
790
+ }
791
+ function ue() {
792
+ return typeof window < "u" && typeof window.XverseProviders?.BitcoinProvider?.signMultipleTransactions == "function" ? ce : null;
793
+ }
794
+ const we = {
795
+ getWallets: ie,
796
+ signMessage: oe,
797
+ signMessageDeterministic: re,
798
+ sendBTC: ae,
799
+ signPSBT: cn,
800
+ sign: de,
801
+ getSignMany: ue
802
+ };
803
+ B("okx", Rn);
804
+ B("unisat", se);
805
+ B("xverse", we);
806
+ B("phantom", zn);
807
+ B("metamask", $n);
808
+ export {
809
+ Y as f
810
+ };