@bestinslot/wallet-kit 0.14.3 → 0.15.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,8 @@ export * as balances from './api/balances';
6
6
  export * from './api/helpers';
7
7
  export { getEndpoints, getNetwork, setEndpoints, setNetwork, subscribeToNetwork, } from './core/store-network';
8
8
  export type { Endpoints, EndpointsConfig } from './core/store-network';
9
+ export { getProviders, setProviders } from './core/chain-providers';
10
+ export type { NetworkProviders, ProvidersConfig } from './core/chain-providers';
9
11
  export type { PaymentOpts } from './types/common';
10
12
  export * as bitcoinjs from 'bitcoinjs-lib';
11
13
  export * as Buff from '@cmdcode/buff-utils';
@@ -32,6 +32,20 @@ export interface SignatureWithAddress {
32
32
  * @returns A promise that resolves to an object containing the signed PSBT as a hexadecimal string and optionally the transaction ID (txid) if the transaction was broadcasted. This allows developers to use the signed PSBT for further processing or to track the transaction on the blockchain. If there is an error in signing the PSBT, the promise will be rejected with a descriptive error message.
33
33
  */
34
34
  export type SignFunction = (unsignedPsbtHex: string, paymentAddr: string, ordAddr: string, ordAddrIdxes: number[], useTweakSignerIdxes?: number[], noSignIdxes?: number[]) => Promise<SignResponse>;
35
+ /** One PSBT of a `SignManyFunction` call, with the arguments a `SignFunction` takes for it. */
36
+ export interface SignPsbtRequest {
37
+ unsignedPsbtHex: string;
38
+ paymentAddr: string;
39
+ ordAddr: string;
40
+ ordAddrIdxes: number[];
41
+ useTweakSignerIdxes?: number[];
42
+ noSignIdxes?: number[];
43
+ }
44
+ /**
45
+ * Signs several PSBTs in one wallet prompt and returns one `SignResponse` per request, in order.
46
+ * The PSBTs may depend on each other (one spending another's outputs), so none is broadcast.
47
+ */
48
+ export type SignManyFunction = (requests: SignPsbtRequest[]) => Promise<SignResponse[]>;
35
49
  /**
36
50
  * Defines the interface for a BISProvider, which includes methods for retrieving wallets, signing messages, and sending Bitcoin. The BISProvider interface specifies the structure that any wallet provider must implement to be compatible with the BIS Wallet Kit. This includes methods for getting the list of wallets, signing messages in both standard and deterministic ways, sending Bitcoin transactions, and signing PSBTs (Partially Signed Bitcoin Transactions). By adhering to this interface, different wallet providers can be integrated into the BIS Wallet Kit while ensuring a consistent API for developers using the kit.
37
51
  */
@@ -110,4 +124,10 @@ export interface BISProvider {
110
124
  * @returns A promise that resolves to an object containing the signed PSBT as a hexadecimal string and optionally the transaction ID (txid) if the transaction was broadcasted. This allows developers to use the signed PSBT for further processing or to track the transaction on the blockchain. If there is an error in signing the PSBT, the promise will be rejected with a descriptive error message.
111
125
  */
112
126
  sign: SignFunction;
127
+ /**
128
+ * Returns a function that signs several PSBTs in one wallet prompt, or null when the wallet
129
+ * (or its installed version) has no such method. Implemented only by providers whose wallet
130
+ * offers batch signing.
131
+ */
132
+ getSignMany?: () => SignManyFunction | null;
113
133
  }
@@ -21,6 +21,8 @@ interface UniSat {
21
21
  switchNetwork: (network: string) => Promise<void>
22
22
  signMessage: (message: string, type?: 'ecdsa' | 'bip322-simple') => Promise<string>
23
23
  signPsbt: (psbt: any, options?: any) => Promise<string>
24
+ /** Signs several PSBT hexes in one prompt; absent before UniSat added batch signing. */
25
+ signPsbts?: (psbts: string[], options?: any[]) => Promise<string[]>
24
26
  pushPsbt: (psbt: any) => Promise<string>
25
27
  getPublicKey: () => Promise<string>
26
28
  sendBitcoin: (toAddress: string, amountSats: number) => Promise<string>
package/dist/vue.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { ref as s, onScopeDispose as u, watch as i } from "vue";
2
- import { e as r, U as c, k as a } from "./mint-CSk_3TfL.js";
3
- function n() {
2
+ import { e as r, Z as c, l as a } from "./mint-FmqHYTrX.js";
3
+ function w() {
4
4
  const o = s(r()), t = c((e) => {
5
5
  o.value !== e && (o.value = e);
6
6
  });
@@ -9,5 +9,5 @@ function n() {
9
9
  }), o;
10
10
  }
11
11
  export {
12
- n as useNetwork
12
+ w as useNetwork
13
13
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@bestinslot/wallet-kit",
3
3
  "type": "module",
4
- "version": "0.14.3",
4
+ "version": "0.15.0-rc.0",
5
5
  "private": false,
6
6
  "description": "A lightweight JavaScript library for connecting Bitcoin wallets with a unified API.",
7
7
  "license": "MIT",
@@ -1,500 +0,0 @@
1
- import { G as l, F as x, H as b, e as d, V as B, J as p, h as S, X as T, T as P } from "./mint-CSk_3TfL.js";
2
- async function C() {
3
- if (!window.LeatherProvider) throw new Error("Leather extension not found.");
4
- const e = await window.LeatherProvider.request("getAddresses");
5
- if (!e) throw new Error("Failed to get wallets.");
6
- return e.result.addresses.map((t) => ({
7
- address: t.address,
8
- pubkey: t.publicKey || null,
9
- purpose: t.type === "p2wpkh" ? "payment" : t.type === "p2tr" ? "ordinals" : "stx"
10
- }));
11
- }
12
- async function $(e, n) {
13
- if (!window.LeatherProvider) throw new Error("Leather extension not found.");
14
- if (!n) throw new Error("Wallet type is required for signing messages with Leather.");
15
- try {
16
- const t = await window.LeatherProvider.request("signMessage", {
17
- message: e,
18
- paymentType: n === "ordinals" ? "p2tr" : "p2wpkh",
19
- network: d()
20
- });
21
- return p.from(t.result.signature, "base64").toString("hex");
22
- } catch (t) {
23
- throw new Error(`Failed to sign message: ${t.message}`);
24
- }
25
- }
26
- async function L(e) {
27
- if (!window.LeatherProvider) throw new Error("Leather extension not found.");
28
- try {
29
- const n = await window.LeatherProvider.request("signMessage", {
30
- message: e,
31
- paymentType: "p2wpkh",
32
- network: d()
33
- });
34
- return {
35
- signature: p.from(n.result.signature, "base64").toString("hex"),
36
- address: n.result.address
37
- };
38
- } catch (n) {
39
- throw new Error(`Failed to sign message: ${n.message}`);
40
- }
41
- }
42
- async function F(e, n) {
43
- if (!window.LeatherProvider) throw new Error("Leather extension not found.");
44
- try {
45
- return (await window.LeatherProvider.request("sendTransfer", {
46
- recipients: [
47
- {
48
- address: n,
49
- amount: e.toString()
50
- }
51
- ],
52
- network: d()
53
- })).result.txid;
54
- } catch (t) {
55
- throw new Error(`Failed to send BTC: ${t.message}`);
56
- }
57
- }
58
- async function M(e, n, t) {
59
- if (!window.LeatherProvider) throw new Error("Leather extension not found.");
60
- const s = [];
61
- for (let i = 0; i < t.length; i++)
62
- for (let r = 0; r < t[i].signingIndexes.length; r++)
63
- s.push(t[i].signingIndexes[r]);
64
- return (await window.LeatherProvider.request("signPsbt", {
65
- hex: B(e),
66
- network: d(),
67
- broadcast: n,
68
- signAtIndex: s
69
- })).result?.hex ?? "";
70
- }
71
- async function I(e, n, t, s, o, i) {
72
- const r = l.Psbt.fromHex(e), a = [];
73
- for (let w = 0; w < r.inputCount; w++)
74
- i && i.includes(w) || s.includes(w) || a.push(w);
75
- const c = await M(x(e), !1, [
76
- {
77
- address: n,
78
- signingIndexes: a,
79
- sigHash: l.Transaction.SIGHASH_DEFAULT
80
- },
81
- {
82
- address: t,
83
- signingIndexes: s,
84
- sigHash: l.Transaction.SIGHASH_DEFAULT
85
- }
86
- ]), u = l.Psbt.fromHex(c);
87
- b(u, i);
88
- const g = u.extractTransaction(), f = g.toHex();
89
- return {
90
- txId: g.getId(),
91
- signedTxHex: f
92
- };
93
- }
94
- const A = {
95
- getWallets: C,
96
- signMessage: $,
97
- signMessageDeterministic: L,
98
- sendBTC: F,
99
- signPSBT: M,
100
- sign: I
101
- };
102
- async function K() {
103
- if (!window.okxwallet) throw new Error("OKX extension not found.");
104
- const e = d();
105
- let n;
106
- if (e === "mainnet") n = await window.okxwallet.bitcoin.connect();
107
- else if (e === "testnet") n = await window.okxwallet.bitcoinTestnet.connect();
108
- else if (e === "signet") n = await window.okxwallet.bitcoinSignet.connect();
109
- else throw new Error("Unsupported network for OKX.");
110
- if (!n) throw new Error("Error fetching wallet data.");
111
- return [
112
- {
113
- address: n.address,
114
- pubkey: n.publicKey,
115
- purpose: "all"
116
- }
117
- ];
118
- }
119
- function H() {
120
- const e = d();
121
- if (e === "mainnet") return window.okxwallet?.bitcoin;
122
- if (e === "testnet") return window.okxwallet?.bitcoinTestnet;
123
- if (e === "signet") return window.okxwallet?.bitcoinSignet;
124
- }
125
- async function U() {
126
- const e = H();
127
- return typeof e?.getAccounts != "function" ? [] : await e.getAccounts() ?? [];
128
- }
129
- function N(e) {
130
- const n = H();
131
- if (typeof n?.on != "function") return () => {
132
- };
133
- const t = (s) => e(s?.address ? [s.address] : []);
134
- return n.on("accountChanged", t), () => n.removeListener?.("accountChanged", t);
135
- }
136
- async function q(e) {
137
- if (!window.okxwallet) throw new Error("OKX extension not found.");
138
- const n = d();
139
- let t;
140
- if (n === "mainnet")
141
- t = await window.okxwallet.bitcoin.signMessage(e, "bip322-simple");
142
- else if (n === "testnet")
143
- t = await window.okxwallet.bitcoinTestnet.signMessage(e, "bip322-simple");
144
- else if (n === "signet")
145
- t = await window.okxwallet.bitcoinSignet.signMessage(e, "bip322-simple");
146
- else throw new Error("Unsupported network for OKX.");
147
- return p.from(t, "base64").toString("hex");
148
- }
149
- async function D(e) {
150
- if (!window.okxwallet) throw new Error("OKX extension not found.");
151
- const n = S();
152
- if (!n) throw new Error("No payment wallet found.");
153
- const t = n.address, s = d();
154
- let o;
155
- if (s === "mainnet")
156
- o = await window.okxwallet.bitcoin.signMessage(e, "ecdsa");
157
- else if (s === "testnet")
158
- o = await window.okxwallet.bitcoinTestnet.signMessage(e, "ecdsa");
159
- else if (s === "signet")
160
- o = await window.okxwallet.bitcoinSignet.signMessage(e, "ecdsa");
161
- else throw new Error("Unsupported network for OKX.");
162
- return {
163
- signature: p.from(o, "base64").toString("hex"),
164
- address: t
165
- };
166
- }
167
- async function O(e, n) {
168
- if (!window.okxwallet) throw new Error("OKX extension not found.");
169
- const t = d();
170
- let s;
171
- if (t === "mainnet")
172
- s = await window.okxwallet.bitcoin.sendBitcoin(n, e);
173
- else if (t === "testnet")
174
- s = await window.okxwallet.bitcoinTestnet.sendBitcoin(n, e);
175
- else if (t === "signet")
176
- s = await window.okxwallet.bitcoinSignet.sendBitcoin(n, e);
177
- else
178
- throw new Error("Unsupported network for OKX.");
179
- return s;
180
- }
181
- async function v(e, n, t) {
182
- const s = B(e);
183
- let o = null;
184
- if (t.length !== 0) {
185
- const i = [];
186
- for (const r of t)
187
- for (let a = 0; a < r.signingIndexes.length; a++)
188
- i.push({
189
- address: r.address,
190
- index: r.signingIndexes[a]
191
- // disableTweakSigner: i == 0 ? true : false // TODO: remove this hack!!
192
- });
193
- o = {
194
- autoFinalized: !0,
195
- toSignInputs: i
196
- };
197
- }
198
- if (d() === "mainnet") {
199
- let i = null;
200
- return o ? i = await window.okxwallet.bitcoin.signPsbt(s, o) : i = await window.okxwallet.bitcoin.signPsbt(s), n && await window.okxwallet.bitcoin.pushPsbt(i), i;
201
- } else if (d() === "testnet") {
202
- if (n)
203
- throw new Error("Cannot broadcast on testnet with okx");
204
- let i = null;
205
- return o ? i = await window.okxwallet.bitcoinTestnet.signPsbt(s, o) : i = await window.okxwallet.bitcoinTestnet.signPsbt(s), i;
206
- } else if (d() === "signet") {
207
- if (n)
208
- throw new Error("Cannot broadcast on signet with okx");
209
- let i = null;
210
- return o ? i = await window.okxwallet.bitcoinSignet.signPsbt(s, o) : i = await window.okxwallet.bitcoinSignet.signPsbt(s), i;
211
- } else
212
- throw new Error("Unsupported network for OKX.");
213
- }
214
- async function W(e, n, t, s, o, i) {
215
- let r = null;
216
- if (!n)
217
- r = await v(x(e), !1, []);
218
- else {
219
- const g = l.Psbt.fromHex(e), f = [];
220
- for (let w = 0; w < g.inputCount; w++)
221
- i && i.includes(w) || s.includes(w) || f.push(w);
222
- r = await v(x(e), !1, [
223
- {
224
- address: n,
225
- signingIndexes: f
226
- },
227
- {
228
- address: t,
229
- signingIndexes: s
230
- }
231
- ]);
232
- }
233
- const a = l.Psbt.fromHex(r);
234
- b(a, i);
235
- const c = a.extractTransaction(), u = c.toHex();
236
- return {
237
- txId: c.getId(),
238
- signedTxHex: u
239
- };
240
- }
241
- const _ = {
242
- getWallets: K,
243
- getAccounts: U,
244
- onAccountsChanged: N,
245
- signMessage: q,
246
- signMessageDeterministic: D,
247
- sendBTC: O,
248
- signPSBT: v,
249
- sign: W
250
- };
251
- async function k() {
252
- if (!window.unisat) throw new Error("UniSat not found.");
253
- const e = await window.unisat.getNetwork(), n = d();
254
- e !== "livenet" && n === "mainnet" ? await window.unisat.switchNetwork("livenet") : e === "livenet" && (n === "testnet" || n === "signet") && await window.unisat.switchNetwork("testnet");
255
- }
256
- async function G() {
257
- await k();
258
- const e = await window.unisat?.requestAccounts(), n = await window.unisat?.getPublicKey();
259
- if (!e) throw new Error("Failed to get wallets.");
260
- return e.map((s) => ({
261
- address: s,
262
- pubkey: n,
263
- purpose: "all"
264
- }));
265
- }
266
- async function z() {
267
- return typeof window.unisat?.getAccounts != "function" ? [] : await window.unisat.getAccounts() ?? [];
268
- }
269
- function R(e) {
270
- const n = window.unisat;
271
- if (!n?.on) return () => {
272
- };
273
- const t = (s) => e(s ?? []);
274
- return n.on("accountsChanged", t), () => n.removeListener?.("accountsChanged", t);
275
- }
276
- async function V(e) {
277
- await k();
278
- try {
279
- const n = await window.unisat.signMessage(e, "bip322-simple");
280
- return p.from(n, "base64").toString("hex");
281
- } catch (n) {
282
- throw console.error("Failed to sign message", n), new Error("Failed to sign message.");
283
- }
284
- }
285
- async function j(e) {
286
- await k();
287
- const n = S();
288
- if (!n) throw new Error("No payment wallet found.");
289
- const t = n.address;
290
- try {
291
- const s = await window.unisat.signMessage(e, "ecdsa");
292
- return {
293
- signature: p.from(s, "base64").toString("hex"),
294
- address: t
295
- };
296
- } catch (s) {
297
- throw console.error("Failed to sign message", s), new Error("Failed to sign message.");
298
- }
299
- }
300
- async function J(e, n) {
301
- await k();
302
- try {
303
- return await window.unisat.sendBitcoin(n, e);
304
- } catch (t) {
305
- throw console.error("Failed to send BTC", t), new Error("Failed to send BTC.");
306
- }
307
- }
308
- async function E(e, n, t) {
309
- if (await k(), !window.unisat) throw new Error("UniSat not found.");
310
- const s = B(e);
311
- let o = null;
312
- if (t.length === 0)
313
- o = await window.unisat.signPsbt(s);
314
- else {
315
- const i = [];
316
- for (const a of t)
317
- for (let c = 0; c < a.signingIndexes.length; c++)
318
- i.push({
319
- address: a.address,
320
- index: a.signingIndexes[c],
321
- useTweakedSigner: a.useTweakedSigner ? a.useTweakedSigner[c] : void 0
322
- });
323
- const r = {
324
- autoFinalized: !0,
325
- toSignInputs: i
326
- };
327
- o = await window.unisat.signPsbt(s, r);
328
- }
329
- return n && (o = await window.unisat.pushPsbt(o)), o;
330
- }
331
- async function Q(e, n, t, s, o, i) {
332
- let r = null;
333
- if (!n)
334
- r = await E(x(e), !1, []);
335
- else {
336
- const g = l.Psbt.fromHex(e), f = [], w = [], m = [];
337
- for (let h = 0; h < g.inputCount; h++)
338
- if (!(i && i.includes(h))) {
339
- if (s.includes(h)) {
340
- o && o.includes(h) ? m.push(!0) : o && m.push(!1);
341
- continue;
342
- }
343
- f.push(h), o && o.includes(h) ? w.push(!0) : o && w.push(!1);
344
- }
345
- r = await E(x(e), !1, [
346
- {
347
- address: n,
348
- signingIndexes: f,
349
- useTweakedSigner: w
350
- },
351
- {
352
- address: t,
353
- signingIndexes: s,
354
- useTweakedSigner: m
355
- }
356
- ]);
357
- }
358
- const a = l.Psbt.fromHex(r);
359
- b(a, i);
360
- const c = a.extractTransaction(), u = c.toHex();
361
- return {
362
- txId: c.getId(),
363
- signedTxHex: u
364
- };
365
- }
366
- const Y = {
367
- checkNetwork: k,
368
- getWallets: G,
369
- getAccounts: z,
370
- onAccountsChanged: R,
371
- signMessage: V,
372
- signMessageDeterministic: j,
373
- sendBTC: J,
374
- signPSBT: E,
375
- sign: Q
376
- };
377
- function y() {
378
- const e = d();
379
- if (e === "mainnet") return "Mainnet";
380
- if (e === "testnet") return "Testnet4";
381
- if (e === "signet") return "Signet";
382
- throw new Error("Unknown BTC network type.");
383
- }
384
- async function Z() {
385
- if (!window.XverseProviders?.BitcoinProvider) throw new Error("Xverse extension not found.");
386
- const e = T({
387
- purposes: ["ordinals", "payment"],
388
- message: "[Best in Slot] wants to know your addresses!",
389
- network: {
390
- type: y()
391
- }
392
- }), n = await window.XverseProviders.BitcoinProvider?.connect(e);
393
- if (!n) throw new Error("Error fetching wallet data.");
394
- return n.addresses.map((s) => ({
395
- address: s.address,
396
- pubkey: s.publicKey,
397
- purpose: s.purpose
398
- }));
399
- }
400
- async function ee(e, n, t) {
401
- if (!window.XverseProviders?.BitcoinProvider) throw new Error("Xverse extension not found.");
402
- const s = T({
403
- address: t,
404
- message: e,
405
- protocol: "BIP322",
406
- network: {
407
- type: y()
408
- }
409
- }), o = await window.XverseProviders.BitcoinProvider.signMessage(s).catch(
410
- (i) => {
411
- throw console.error("Failed to sign message.", i), new Error("Failed to sign message.");
412
- }
413
- );
414
- return p.from(o, "base64").toString("hex");
415
- }
416
- async function ne(e) {
417
- if (!window.XverseProviders?.BitcoinProvider) throw new Error("Xverse extension not found.");
418
- const n = S();
419
- if (!n) throw new Error("No payment wallet found.");
420
- const t = n.address, s = T({
421
- address: t,
422
- message: e,
423
- protocol: "ECDSA",
424
- network: {
425
- type: y()
426
- }
427
- }), o = await window.XverseProviders.BitcoinProvider.signMessage(s).catch(
428
- (i) => {
429
- throw console.error("Failed to sign message.", i), new Error("Failed to sign message.");
430
- }
431
- );
432
- return {
433
- signature: p.from(o, "base64").toString("hex"),
434
- address: t
435
- };
436
- }
437
- async function te(e, n) {
438
- if (!window.XverseProviders?.BitcoinProvider) throw new Error("Xverse extension not found.");
439
- return (await window.XverseProviders.BitcoinProvider.request("sendTransfer", {
440
- recipients: [
441
- {
442
- address: n,
443
- amount: e
444
- }
445
- ]
446
- })).result.txid;
447
- }
448
- async function X(e, n, t, s) {
449
- if (!window.XverseProviders?.BitcoinProvider) throw new Error("Xverse extension not found.");
450
- const o = T({
451
- network: {
452
- type: y()
453
- },
454
- message: s || "Sign Transaction",
455
- psbtBase64: e,
456
- broadcast: n,
457
- inputsToSign: t
458
- });
459
- return await window.XverseProviders.BitcoinProvider.signTransaction(o);
460
- }
461
- async function se(e, n, t, s, o, i) {
462
- const r = l.Psbt.fromHex(e), a = [];
463
- for (let w = 0; w < r.inputCount; w++)
464
- i && i.includes(w) || s.includes(w) || a.push(w);
465
- const c = await X(
466
- x(e),
467
- !1,
468
- [
469
- {
470
- address: n,
471
- signingIndexes: a,
472
- sigHash: l.Transaction.SIGHASH_DEFAULT
473
- },
474
- {
475
- address: t,
476
- signingIndexes: s,
477
- sigHash: l.Transaction.SIGHASH_DEFAULT
478
- }
479
- ],
480
- "sign this pls"
481
- ), u = l.Psbt.fromBase64(c.psbtBase64);
482
- b(u, i);
483
- const g = u.extractTransaction(), f = g.toHex();
484
- return {
485
- txId: g.getId(),
486
- signedTxHex: f
487
- };
488
- }
489
- const ie = {
490
- getWallets: Z,
491
- signMessage: ee,
492
- signMessageDeterministic: ne,
493
- sendBTC: te,
494
- signPSBT: X,
495
- sign: se
496
- };
497
- P("leather", A);
498
- P("okx", _);
499
- P("unisat", Y);
500
- P("xverse", ie);