@gardenfi/wallet-connectors 2.5.0 → 3.0.0-beta.10

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 (47) hide show
  1. package/dist/index.cjs +1 -1
  2. package/dist/index.js +10 -5
  3. package/dist/index10.cjs +1 -1
  4. package/dist/index10.js +72 -70
  5. package/dist/index11.cjs +1 -1
  6. package/dist/index11.js +82 -40
  7. package/dist/index12.cjs +1 -1
  8. package/dist/index12.js +71 -159
  9. package/dist/index13.cjs +1 -1
  10. package/dist/index13.js +40 -180
  11. package/dist/index14.cjs +1 -1
  12. package/dist/index14.js +169 -3
  13. package/dist/index15.cjs +1 -1
  14. package/dist/index15.js +206 -2
  15. package/dist/index16.cjs +1 -1
  16. package/dist/index16.js +8 -2
  17. package/dist/index17.cjs +1 -3
  18. package/dist/index17.js +2 -114
  19. package/dist/index18.cjs +1 -1
  20. package/dist/index18.js +3 -2
  21. package/dist/index19.cjs +1 -0
  22. package/dist/index19.js +4 -0
  23. package/dist/index2.cjs +1 -1
  24. package/dist/index2.js +5 -5
  25. package/dist/index20.cjs +1 -0
  26. package/dist/index20.js +4 -0
  27. package/dist/index21.cjs +3 -0
  28. package/dist/index21.js +116 -0
  29. package/dist/index22.cjs +1 -0
  30. package/dist/index22.js +4 -0
  31. package/dist/index5.cjs +1 -1
  32. package/dist/index5.js +2 -2
  33. package/dist/index7.cjs +1 -1
  34. package/dist/index7.js +110 -60
  35. package/dist/index8.cjs +1 -1
  36. package/dist/index8.js +90 -72
  37. package/dist/index9.cjs +1 -1
  38. package/dist/index9.js +38 -84
  39. package/dist/src/index.d.ts +1 -0
  40. package/dist/src/spark/constants.d.ts +14 -0
  41. package/dist/src/spark/index.d.ts +5 -0
  42. package/dist/src/spark/providers/sparkWalletsProvider.d.ts +13 -0
  43. package/dist/src/spark/providers/sparkWalletsProvider.types.d.ts +22 -0
  44. package/dist/src/spark/providers/xverse/provider.d.ts +28 -0
  45. package/dist/src/spark/providers/xverse/xverse.types.d.ts +11 -0
  46. package/dist/src/spark/spark.types.d.ts +51 -0
  47. package/package.json +4 -3
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./index2.cjs"),l=require("./index3.cjs"),r=require("./index4.cjs"),t=require("./index5.cjs"),o=require("./index6.cjs");exports.BTCWalletProvider=e.BTCWalletProvider;exports.useBitcoinWallet=e.useBitcoinWallet;exports.SupportedBTCWallets=l.SupportedWallets;exports.getBalance=r.getBalance;exports.LTCWalletProvider=t.LTCWalletProvider;exports.useLitecoinWallet=t.useLitecoinWallet;exports.SupportedLTCWallets=o.SupportedWallets;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./index2.cjs"),l=require("./index3.cjs"),a=require("./index4.cjs"),r=require("./index5.cjs"),o=require("./index6.cjs"),t=require("./index7.cjs"),i=require("./index8.cjs");exports.BTCWalletProvider=e.BTCWalletProvider;exports.useBitcoinWallet=e.useBitcoinWallet;exports.SupportedBTCWallets=l.SupportedWallets;exports.getBalance=a.getBalance;exports.LTCWalletProvider=r.LTCWalletProvider;exports.useLitecoinWallet=r.useLitecoinWallet;exports.SupportedLTCWallets=o.SupportedWallets;exports.SparkWalletProvider=t.SparkWalletProvider;exports.useSparkWallet=t.useSparkWallet;exports.XverseSparkProvider=i.XverseSparkProvider;
package/dist/index.js CHANGED
@@ -1,14 +1,19 @@
1
- import { BTCWalletProvider as o, useBitcoinWallet as r } from "./index2.js";
1
+ import { BTCWalletProvider as o, useBitcoinWallet as t } from "./index2.js";
2
2
  import { SupportedWallets as p } from "./index3.js";
3
3
  import { getBalance as s } from "./index4.js";
4
4
  import { LTCWalletProvider as d, useLitecoinWallet as i } from "./index5.js";
5
- import { SupportedWallets as f } from "./index6.js";
5
+ import { SupportedWallets as m } from "./index6.js";
6
+ import { SparkWalletProvider as x, useSparkWallet as S } from "./index7.js";
7
+ import { XverseSparkProvider as B } from "./index8.js";
6
8
  export {
7
9
  o as BTCWalletProvider,
8
10
  d as LTCWalletProvider,
11
+ x as SparkWalletProvider,
9
12
  p as SupportedBTCWallets,
10
- f as SupportedLTCWallets,
13
+ m as SupportedLTCWallets,
14
+ B as XverseSparkProvider,
11
15
  s as getBalance,
12
- r as useBitcoinWallet,
13
- i as useLitecoinWallet
16
+ t as useBitcoinWallet,
17
+ i as useLitecoinWallet,
18
+ S as useSparkWallet
14
19
  };
package/dist/index10.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var l=n=>{throw TypeError(n)};var d=(n,e,t)=>e.has(n)||l("Cannot "+t);var o=(n,e,t)=>(d(n,e,"read from private field"),t?t.call(n):e.get(n)),N=(n,e,t)=>e.has(n)?l("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(n):e.set(n,t),k=(n,e,t,s)=>(d(n,e,"write to private field"),s?s.call(n,t):e.set(n,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("./index3.cjs"),u=require("./index15.cjs"),r=require("@gardenfi/utils");var i;class y{constructor(e){N(this,i);this.address="",this.id=w.WALLET_CONFIG.Xverse.id,this.name=w.WALLET_CONFIG.Xverse.name,this.icon=w.WALLET_CONFIG.Xverse.icon,this.connect=async t=>{try{t||(t=r.Network.MAINNET),await o(this,i).request("wallet_connect",null);const s=await this.getNetwork();if(s.error)return r.Err("Could not get network",s.error);if(s.val!==t){const c=await this.switchNetwork();if(c.error)return r.Err("Failed to switch network",c.error)}const a=await this.getAccounts();return a.ok&&a.val.length>0&&(this.address=a.val[0]),r.Ok({address:this.address,provider:this,network:t,id:w.WALLET_CONFIG.Xverse.id})}catch(s){return r.Err("Error while connecting to the XVerse wallet",s)}},this.getBalance=async()=>await r.executeWithTryCatch(async()=>(await o(this,i).request("getBalance",{})).result,"Error while getting balance from XVerse wallet"),this.requestAccounts=async()=>await r.executeWithTryCatch(async()=>(await o(this,i).request("getAccounts",{purposes:["payment"],message:"I want to connect"})).result.map(s=>s.address)),this.getAccounts=async()=>await r.executeWithTryCatch(async()=>(await o(this,i).request("getAddresses",{purposes:["payment"]})).result.addresses.map(s=>s.address)),this.sendBitcoin=async(t,s)=>await r.executeWithTryCatch(async()=>{var h;const a=await o(this,i).request("sendTransfer",{recipients:[{address:t,amount:s}]}),c=(h=a.result)==null?void 0:h.txid;if(c)return r.Ok(c);throw new Error(a.error)},"Error while sending bitcoin from Xverse wallet"),this.on=()=>{},this.off=()=>{},this.disconnect=()=>(this.address="",Promise.resolve(r.Ok("Disconnected"))),k(this,i,e)}async getNetwork(){return await r.executeWithTryCatch(async()=>{const e=await o(this,i).request("wallet_getNetwork",null);if(e.result.bitcoin.name===u.XverseBitcoinNetworkType.Mainnet)return r.Network.MAINNET;if(e.result.bitcoin.name===u.XverseBitcoinNetworkType.Testnet4)return r.Network.TESTNET;throw new Error(e.result)},"Error while getting network from Xverse wallet")}async switchNetwork(){try{const e=await this.getNetwork();if(e.error)return r.Err("Failed to get current network");const t=e.val===r.Network.MAINNET?u.XverseBitcoinNetworkType.Testnet4:u.XverseBitcoinNetworkType.Mainnet;await o(this,i).request("wallet_changeNetwork",{name:t});const s=await this.getNetwork();return s.ok?r.Ok(s.val):r.Err("Failed to verify network switch")}catch(e){return r.Err("Error while switching network in Xverse:",e)}}}i=new WeakMap;exports.XverseProvider=y;
1
+ "use strict";var N=n=>{throw TypeError(n)};var T=(n,t,r)=>t.has(n)||N("Cannot "+r);var s=(n,t,r)=>(T(n,t,"read from private field"),r?r.call(n):t.get(n)),w=(n,t,r)=>t.has(n)?N("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(n):t.set(n,r),E=(n,t,r,o)=>(T(n,t,"write to private field"),o?o.call(n,r):t.set(n,r),r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("./index18.cjs"),e=require("@gardenfi/utils"),u=require("./index3.cjs");var i,c;class d{constructor(t,r){w(this,i);w(this,c);this.address="",this.id=u.WALLET_CONFIG.Unisat.id,this.name=u.WALLET_CONFIG.Unisat.name,this.icon=u.WALLET_CONFIG.Unisat.icon,this.disconnect=()=>(this.address="",Promise.resolve(e.Ok("Disconnected unisat wallet"))),E(this,i,t),E(this,c,r)}async connect(t){try{t||(t=s(this,c));const r=await this.getNetwork(),o=await s(this,i).getChain();if(r.error)return e.Err("Could not get network",r.error);if(r.val!==t||o.enum===a.UnisatChainEnum.BITCOIN_TESTNET){const l=await this.switchNetwork();if(l.error)return e.Err("Failed to switch network",l.error)}const h=await s(this,i).requestAccounts();return h.length>0&&(this.address=h[0]),e.Ok({address:this.address,provider:this,network:t,id:u.WALLET_CONFIG.Unisat.id})}catch(r){return e.Err("Error while connecting to the Unisat wallet",r)}}async requestAccounts(){return await e.executeWithTryCatch(async()=>await s(this,i).requestAccounts(),"Error while requesting accounts from the Unisat wallet")}async getAccounts(){return await e.executeWithTryCatch(async()=>await s(this,i).getAccounts(),"Error while getting accounts from the Unisat wallet")}async getNetwork(){return await e.executeWithTryCatch(async()=>{const t=await s(this,i).getNetwork();if(t===a.UnisatNetworkEnum.LIVENET)return e.Network.MAINNET;if(t===a.UnisatNetworkEnum.TESTNET)return e.Network.TESTNET;throw new Error("Invalid or unsupported network"+t)},"Error while getting network from Unisat wallet")}async switchNetwork(){try{const t=await this.getNetwork(),r=await s(this,i).getChain();if(t.error)return e.Err("Failed to get current network");const o=t.val===e.Network.MAINNET||s(this,c)===e.Network.TESTNET&&r.enum===a.UnisatChainEnum.BITCOIN_TESTNET?a.UnisatChainEnum.BITCOIN_TESTNET4:a.UnisatChainEnum.BITCOIN_MAINNET;await s(this,i).switchChain(o);const h=await this.getNetwork();return h.ok?e.Ok(h.val):e.Err("Failed to verify network switch")}catch(t){return e.Err("Error while switching network in Unisat:",t)}}async getBalance(){return await e.executeWithTryCatch(async()=>await s(this,i).getBalance(),"Error while getting balance from Unisat wallet")}async sendBitcoin(t,r){return await e.executeWithTryCatch(async()=>await s(this,i).sendBitcoin(t,r),"Error while sending bitcoin from Unisat wallet")}on(t,r){s(this,i).on(t,r)}off(t,r){s(this,i).removeListener(t,r)}}i=new WeakMap,c=new WeakMap;exports.UnisatProvider=d;
package/dist/index10.js CHANGED
@@ -1,83 +1,85 @@
1
- var N = (s) => {
2
- throw TypeError(s);
1
+ var g = (e) => {
2
+ throw TypeError(e);
3
3
  };
4
- var k = (s, e, t) => e.has(s) || N("Cannot " + t);
5
- var i = (s, e, t) => (k(s, e, "read from private field"), t ? t.call(s) : e.get(s)), m = (s, e, t) => e.has(s) ? N("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(s) : e.set(s, t), f = (s, e, t, r) => (k(s, e, "write to private field"), r ? r.call(s, t) : e.set(s, t), t);
6
- import { WALLET_CONFIG as u } from "./index3.js";
7
- import { XverseBitcoinNetworkType as h } from "./index15.js";
8
- import { Network as l, Err as a, Ok as d, executeWithTryCatch as w } from "@gardenfi/utils";
9
- var n;
10
- class T {
11
- constructor(e) {
12
- m(this, n);
13
- this.address = "", this.id = u.Xverse.id, this.name = u.Xverse.name, this.icon = u.Xverse.icon, this.connect = async (t) => {
14
- try {
15
- t || (t = l.MAINNET), await i(this, n).request("wallet_connect", null);
16
- const r = await this.getNetwork();
17
- if (r.error)
18
- return a("Could not get network", r.error);
19
- if (r.val !== t) {
20
- const c = await this.switchNetwork();
21
- if (c.error)
22
- return a("Failed to switch network", c.error);
23
- }
24
- const o = await this.getAccounts();
25
- return o.ok && o.val.length > 0 && (this.address = o.val[0]), d({
26
- address: this.address,
27
- provider: this,
28
- network: t,
29
- id: u.Xverse.id
30
- });
31
- } catch (r) {
32
- return a("Error while connecting to the XVerse wallet", r);
4
+ var k = (e, t, r) => t.has(e) || g("Cannot " + r);
5
+ var i = (e, t, r) => (k(e, t, "read from private field"), r ? r.call(e) : t.get(e)), N = (e, t, r) => t.has(e) ? g("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(e) : t.set(e, r), T = (e, t, r, s) => (k(e, t, "write to private field"), s ? s.call(e, r) : t.set(e, r), r);
6
+ import { UnisatChainEnum as w, UnisatNetworkEnum as f } from "./index18.js";
7
+ import { Ok as E, Err as a, executeWithTryCatch as h, Network as u } from "@gardenfi/utils";
8
+ import { WALLET_CONFIG as l } from "./index3.js";
9
+ var n, o;
10
+ class U {
11
+ constructor(t, r) {
12
+ N(this, n);
13
+ N(this, o);
14
+ this.address = "", this.id = l.Unisat.id, this.name = l.Unisat.name, this.icon = l.Unisat.icon, this.disconnect = () => (this.address = "", Promise.resolve(E("Disconnected unisat wallet"))), T(this, n, t), T(this, o, r);
15
+ }
16
+ async connect(t) {
17
+ try {
18
+ t || (t = i(this, o));
19
+ const r = await this.getNetwork(), s = await i(this, n).getChain();
20
+ if (r.error)
21
+ return a("Could not get network", r.error);
22
+ if (r.val !== t || s.enum === w.BITCOIN_TESTNET) {
23
+ const d = await this.switchNetwork();
24
+ if (d.error)
25
+ return a("Failed to switch network", d.error);
33
26
  }
34
- }, this.getBalance = async () => await w(async () => (await i(this, n).request("getBalance", {})).result, "Error while getting balance from XVerse wallet"), this.requestAccounts = async () => await w(async () => (await i(this, n).request("getAccounts", {
35
- purposes: ["payment"],
36
- message: "I want to connect"
37
- })).result.map((r) => r.address)), this.getAccounts = async () => await w(async () => (await i(this, n).request("getAddresses", {
38
- purposes: ["payment"]
39
- })).result.addresses.map((r) => r.address)), this.sendBitcoin = async (t, r) => await w(async () => {
40
- var g;
41
- const o = await i(this, n).request("sendTransfer", {
42
- recipients: [{ address: t, amount: r }]
43
- }), c = (g = o.result) == null ? void 0 : g.txid;
44
- if (c)
45
- return d(c);
46
- throw new Error(o.error);
47
- }, "Error while sending bitcoin from Xverse wallet"), this.on = () => {
48
- }, this.off = () => {
49
- }, this.disconnect = () => (this.address = "", Promise.resolve(d("Disconnected"))), f(this, n, e);
27
+ const c = await i(this, n).requestAccounts();
28
+ return c.length > 0 && (this.address = c[0]), E({
29
+ address: this.address,
30
+ provider: this,
31
+ network: t,
32
+ id: l.Unisat.id
33
+ });
34
+ } catch (r) {
35
+ return a("Error while connecting to the Unisat wallet", r);
36
+ }
37
+ }
38
+ // requests accounts from the wallet, if not connected, it will connect first
39
+ async requestAccounts() {
40
+ return await h(async () => await i(this, n).requestAccounts(), "Error while requesting accounts from the Unisat wallet");
41
+ }
42
+ //silently gets accounts if already connected
43
+ async getAccounts() {
44
+ return await h(async () => await i(this, n).getAccounts(), "Error while getting accounts from the Unisat wallet");
50
45
  }
51
46
  async getNetwork() {
52
- return await w(async () => {
53
- const e = await i(this, n).request(
54
- "wallet_getNetwork",
55
- null
56
- );
57
- if (e.result.bitcoin.name === h.Mainnet)
58
- return l.MAINNET;
59
- if (e.result.bitcoin.name === h.Testnet4)
60
- return l.TESTNET;
61
- throw new Error(e.result);
62
- }, "Error while getting network from Xverse wallet");
47
+ return await h(async () => {
48
+ const t = await i(this, n).getNetwork();
49
+ if (t === f.LIVENET)
50
+ return u.MAINNET;
51
+ if (t === f.TESTNET)
52
+ return u.TESTNET;
53
+ throw new Error("Invalid or unsupported network" + t);
54
+ }, "Error while getting network from Unisat wallet");
63
55
  }
64
56
  async switchNetwork() {
65
57
  try {
66
- const e = await this.getNetwork();
67
- if (e.error)
58
+ const t = await this.getNetwork(), r = await i(this, n).getChain();
59
+ if (t.error)
68
60
  return a("Failed to get current network");
69
- const t = e.val === l.MAINNET ? h.Testnet4 : h.Mainnet;
70
- await i(this, n).request("wallet_changeNetwork", {
71
- name: t
72
- });
73
- const r = await this.getNetwork();
74
- return r.ok ? d(r.val) : a("Failed to verify network switch");
75
- } catch (e) {
76
- return a("Error while switching network in Xverse:", e);
61
+ const s = t.val === u.MAINNET ? w.BITCOIN_TESTNET4 : i(this, o) === u.TESTNET && r.enum === w.BITCOIN_TESTNET ? w.BITCOIN_TESTNET4 : w.BITCOIN_MAINNET;
62
+ await i(this, n).switchChain(s);
63
+ const c = await this.getNetwork();
64
+ return c.ok ? E(c.val) : a("Failed to verify network switch");
65
+ } catch (t) {
66
+ return a("Error while switching network in Unisat:", t);
77
67
  }
78
68
  }
69
+ async getBalance() {
70
+ return await h(async () => await i(this, n).getBalance(), "Error while getting balance from Unisat wallet");
71
+ }
72
+ async sendBitcoin(t, r) {
73
+ return await h(async () => await i(this, n).sendBitcoin(t, r), "Error while sending bitcoin from Unisat wallet");
74
+ }
75
+ on(t, r) {
76
+ i(this, n).on(t, r);
77
+ }
78
+ off(t, r) {
79
+ i(this, n).removeListener(t, r);
80
+ }
79
81
  }
80
- n = new WeakMap();
82
+ n = new WeakMap(), o = new WeakMap();
81
83
  export {
82
- T as XverseProvider
84
+ U as UnisatProvider
83
85
  };
package/dist/index11.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var o=r=>{throw TypeError(r)};var l=(r,e,t)=>e.has(r)||o("Cannot "+t);var s=(r,e,t)=>(l(r,e,"read from private field"),t?t.call(r):e.get(r)),u=(r,e,t)=>e.has(r)?o("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(r):e.set(r,t),h=(r,e,t,c)=>(l(r,e,"write to private field"),c?c.call(r,t):e.set(r,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("./index16.cjs"),a=require("./index3.cjs"),n=require("@gardenfi/utils");var i;class p{constructor(e){u(this,i);this.address="",this.id=a.WALLET_CONFIG.Keplr.id,this.name=a.WALLET_CONFIG.Keplr.name,this.icon=a.WALLET_CONFIG.Keplr.icon,h(this,i,e)}async connect(e){if(e||(e=n.Network.MAINNET),e===n.Network.TESTNET)return n.Err("Keplr wallet does not support testnet4");try{const t=await s(this,i).requestAccounts();return t.length>0&&(this.address=t[0]),n.Ok({address:this.address,provider:this,network:e,id:a.WALLET_CONFIG.Keplr.id})}catch(t){return n.Err("Error while connecting to the Keplr wallet",t)}}async requestAccounts(){return await n.executeWithTryCatch(async()=>await s(this,i).requestAccounts(),"Error while requesting accounts from the Keplr wallet")}async getAccounts(){return await n.executeWithTryCatch(async()=>await s(this,i).getAccounts(),"Error while getting accounts from the Keplr wallet")}async getNetwork(){return await n.executeWithTryCatch(async()=>{const e=await s(this,i).getChain();if(e.enum===w.KeplrBitcoinChainType.MAINNET)return n.Network.MAINNET;if(e.enum===w.KeplrBitcoinChainType.TESTNET)return n.Network.TESTNET;throw new Error("Invalid or unsupported network"+e.enum)},"Error while getting network from the Keplr wallet")}async switchNetwork(){return n.Err("Keplr wallet does not support testnet4")}async getBalance(){return await n.executeWithTryCatch(async()=>await s(this,i).getBalance(),"Error while getting balance from Keplr wallet")}async sendBitcoin(e,t){return await n.executeWithTryCatch(async()=>await s(this,i).sendBitcoin(e,t),"Error while sending bitcoin from Keplr wallet")}on(e,t){s(this,i).on(e,t)}off(e,t){s(this,i).off(e,t)}async disconnect(){return await s(this,i).disconnect(),this.address="",n.Ok("Disconnected Keplr wallet")}}i=new WeakMap;exports.KeplrProvider=p;
1
+ "use strict";var m=r=>{throw TypeError(r)};var f=(r,t,e)=>t.has(r)||m("Cannot "+e);var a=(r,t,e)=>(f(r,t,"read from private field"),e?e.call(r):t.get(r)),w=(r,t,e)=>t.has(r)?m("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(r):t.set(r,e),g=(r,t,e,o)=>(f(r,t,"write to private field"),o?o.call(r,e):t.set(r,e),e);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("bitcoinjs-lib"),A=require("tiny-secp256k1"),n=require("@gardenfi/utils"),i=require("./index3.cjs"),P=require("@gardenfi/core"),x=require("./index4.cjs");function p(r){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(r){for(const e in r)if(e!=="default"){const o=Object.getOwnPropertyDescriptor(r,e);Object.defineProperty(t,e,o.get?o:{enumerable:!0,get:()=>r[e]})}}return t.default=r,Object.freeze(t)}const d=p(y),I=p(A);y.initEccLib(I);var s;class O{constructor(t){w(this,s);this.address="",this.id=i.WALLET_CONFIG.Phantom.id,this.name=i.WALLET_CONFIG.Phantom.name,this.icon=i.WALLET_CONFIG.Phantom.icon,this.disconnect=()=>(this.address="",Promise.resolve(n.Ok("Disconnected Phantom wallet"))),g(this,s,t)}async connect(t){if(t||(t=n.Network.MAINNET),t===n.Network.TESTNET)return n.Err("Phantom wallet does not support testnet");try{const e=await a(this,s).requestAccounts();for(const o of e)o.purpose==="payment"&&(this.address=o.address);return this.address===""?n.Err("Could not connect to Phantom bitcoin payment account"):n.Ok({address:this.address,provider:this,network:t,id:i.WALLET_CONFIG.Phantom.id})}catch(e){return n.Err("Error while connecting to Phantom wallet: "+e)}}async requestAccounts(){return await n.executeWithTryCatch(async()=>{const t=await a(this,s).requestAccounts();return t.length>0&&(this.address=t[0].address),t.map(e=>e.address)},"Error while requesting accounts from Phantom wallet")}async getAccounts(){return this.requestAccounts()}async getNetwork(){return n.Ok(n.Network.MAINNET)}async switchNetwork(){return n.Err("Phantom wallet does not support testnet")}async getBalance(){return await n.executeWithTryCatch(async()=>{const t=await x.getBalance(this.address,n.Network.MAINNET);if(t.ok&&t.val)return t.val;throw new Error(t.error)},"Error while getting balance from Phantom wallet")}async sendBitcoin(t,e){return await n.executeWithTryCatch(async()=>{const o=d.networks.bitcoin,h=new P.BitcoinProvider(n.Network.MAINNET);try{const{txHex:c,utxoCount:b}=await P.BitcoinWallet.generateUnsignedPSBT(h,o,this.address,t,e),E=await a(this,s).signPSBT(this.fromHexString(c),{inputsToSign:[{address:this.address,signingIndexes:Array.from({length:b},(S,T)=>T),sigHash:d.Transaction.SIGHASH_ALL}]}),l=d.Psbt.fromBuffer(Buffer.from(E));l.finalizeAllInputs();const u=l.extractTransaction();console.log("tx",u);const N=u.getId();return await h.broadcast(u.toHex()),N}catch(c){throw new Error(`Failed to send bitcoin: ${c}`)}},"Error while sending bitcoin from Phantom wallet")}fromHexString(t){return Uint8Array.from(t.match(/.{1,2}/g).map(e=>parseInt(e,16)))}on(t,e){a(this,s).on(t,e)}off(t,e){a(this,s).off(t,e)}}s=new WeakMap;exports.PhantomProvider=O;
package/dist/index11.js CHANGED
@@ -1,68 +1,110 @@
1
- var u = (r) => {
2
- throw TypeError(r);
1
+ var g = (n) => {
2
+ throw TypeError(n);
3
3
  };
4
- var h = (r, t, e) => t.has(r) || u("Cannot " + e);
5
- var s = (r, t, e) => (h(r, t, "read from private field"), e ? e.call(r) : t.get(r)), w = (r, t, e) => t.has(r) ? u("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(r) : t.set(r, e), p = (r, t, e, l) => (h(r, t, "write to private field"), l ? l.call(r, e) : t.set(r, e), e);
6
- import { KeplrBitcoinChainType as d } from "./index16.js";
7
- import { WALLET_CONFIG as a } from "./index3.js";
8
- import { Network as o, Err as c, Ok as E, executeWithTryCatch as i } from "@gardenfi/utils";
9
- var n;
10
- class K {
4
+ var p = (n, t, r) => t.has(n) || g("Cannot " + r);
5
+ var s = (n, t, r) => (p(n, t, "read from private field"), r ? r.call(n) : t.get(n)), P = (n, t, r) => t.has(n) ? g("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(n) : t.set(n, r), y = (n, t, r, e) => (p(n, t, "write to private field"), e ? e.call(n, r) : t.set(n, r), r);
6
+ import * as m from "bitcoinjs-lib";
7
+ import { initEccLib as N } from "bitcoinjs-lib";
8
+ import * as b from "tiny-secp256k1";
9
+ import { Ok as u, Network as a, Err as i, executeWithTryCatch as l } from "@gardenfi/utils";
10
+ import { WALLET_CONFIG as c } from "./index3.js";
11
+ import { BitcoinProvider as I, BitcoinWallet as B } from "@gardenfi/core";
12
+ import { getBalance as v } from "./index4.js";
13
+ N(b);
14
+ var o;
15
+ class W {
11
16
  constructor(t) {
12
- w(this, n);
13
- this.address = "", this.id = a.Keplr.id, this.name = a.Keplr.name, this.icon = a.Keplr.icon, p(this, n, t);
17
+ P(this, o);
18
+ this.address = "", this.id = c.Phantom.id, this.name = c.Phantom.name, this.icon = c.Phantom.icon, this.disconnect = () => (this.address = "", Promise.resolve(u("Disconnected Phantom wallet"))), y(this, o, t);
14
19
  }
15
20
  async connect(t) {
16
- if (t || (t = o.MAINNET), t === o.TESTNET)
17
- return c("Keplr wallet does not support testnet4");
21
+ if (t || (t = a.MAINNET), t === a.TESTNET)
22
+ return i("Phantom wallet does not support testnet");
18
23
  try {
19
- const e = await s(this, n).requestAccounts();
20
- return e.length > 0 && (this.address = e[0]), E({
24
+ const r = await s(this, o).requestAccounts();
25
+ for (const e of r)
26
+ e.purpose === "payment" && (this.address = e.address);
27
+ return this.address === "" ? i("Could not connect to Phantom bitcoin payment account") : u({
21
28
  address: this.address,
22
29
  provider: this,
23
30
  network: t,
24
- id: a.Keplr.id
31
+ id: c.Phantom.id
25
32
  });
26
- } catch (e) {
27
- return c("Error while connecting to the Keplr wallet", e);
33
+ } catch (r) {
34
+ return i("Error while connecting to Phantom wallet: " + r);
28
35
  }
29
36
  }
30
37
  async requestAccounts() {
31
- return await i(async () => await s(this, n).requestAccounts(), "Error while requesting accounts from the Keplr wallet");
38
+ return await l(async () => {
39
+ const t = await s(this, o).requestAccounts();
40
+ return t.length > 0 && (this.address = t[0].address), t.map((r) => r.address);
41
+ }, "Error while requesting accounts from Phantom wallet");
32
42
  }
33
43
  async getAccounts() {
34
- return await i(async () => await s(this, n).getAccounts(), "Error while getting accounts from the Keplr wallet");
44
+ return this.requestAccounts();
35
45
  }
46
+ // bitcoin testnet is not supported in Phantom wallet
36
47
  async getNetwork() {
37
- return await i(async () => {
38
- const t = await s(this, n).getChain();
39
- if (t.enum === d.MAINNET)
40
- return o.MAINNET;
41
- if (t.enum === d.TESTNET)
42
- return o.TESTNET;
43
- throw new Error("Invalid or unsupported network" + t.enum);
44
- }, "Error while getting network from the Keplr wallet");
48
+ return u(a.MAINNET);
45
49
  }
46
50
  async switchNetwork() {
47
- return c("Keplr wallet does not support testnet4");
51
+ return i("Phantom wallet does not support testnet");
48
52
  }
49
53
  async getBalance() {
50
- return await i(async () => await s(this, n).getBalance(), "Error while getting balance from Keplr wallet");
54
+ return await l(async () => {
55
+ const t = await v(this.address, a.MAINNET);
56
+ if (t.ok && t.val)
57
+ return t.val;
58
+ throw new Error(t.error);
59
+ }, "Error while getting balance from Phantom wallet");
51
60
  }
52
- async sendBitcoin(t, e) {
53
- return await i(async () => await s(this, n).sendBitcoin(t, e), "Error while sending bitcoin from Keplr wallet");
61
+ async sendBitcoin(t, r) {
62
+ return await l(async () => {
63
+ const e = m.networks.bitcoin, f = new I(a.MAINNET);
64
+ try {
65
+ const { txHex: d, utxoCount: A } = await B.generateUnsignedPSBT(
66
+ f,
67
+ e,
68
+ this.address,
69
+ t,
70
+ r
71
+ ), E = await s(this, o).signPSBT(
72
+ this.fromHexString(d),
73
+ {
74
+ inputsToSign: [
75
+ {
76
+ address: this.address,
77
+ signingIndexes: Array.from({ length: A }, (S, x) => x),
78
+ sigHash: m.Transaction.SIGHASH_ALL
79
+ }
80
+ ]
81
+ }
82
+ ), w = m.Psbt.fromBuffer(
83
+ Buffer.from(E)
84
+ );
85
+ w.finalizeAllInputs();
86
+ const h = w.extractTransaction();
87
+ console.log("tx", h);
88
+ const T = h.getId();
89
+ return await f.broadcast(h.toHex()), T;
90
+ } catch (d) {
91
+ throw new Error(`Failed to send bitcoin: ${d}`);
92
+ }
93
+ }, "Error while sending bitcoin from Phantom wallet");
54
94
  }
55
- on(t, e) {
56
- s(this, n).on(t, e);
95
+ fromHexString(t) {
96
+ return Uint8Array.from(
97
+ t.match(/.{1,2}/g).map((r) => parseInt(r, 16))
98
+ );
57
99
  }
58
- off(t, e) {
59
- s(this, n).off(t, e);
100
+ on(t, r) {
101
+ s(this, o).on(t, r);
60
102
  }
61
- async disconnect() {
62
- return await s(this, n).disconnect(), this.address = "", E("Disconnected Keplr wallet");
103
+ off(t, r) {
104
+ s(this, o).off(t, r);
63
105
  }
64
106
  }
65
- n = new WeakMap();
107
+ o = new WeakMap();
66
108
  export {
67
- K as KeplrProvider
109
+ W as PhantomProvider
68
110
  };
package/dist/index12.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";var O=s=>{throw TypeError(s)};var m=(s,t,r)=>t.has(s)||O("Cannot "+r);var i=(s,t,r)=>(m(s,t,"read from private field"),r?r.call(s):t.get(s)),v=(s,t,r)=>t.has(s)?O("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(s):t.set(s,r),E=(s,t,r,n)=>(m(s,t,"write to private field"),n?n.call(s,r):t.set(s,r),r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const I=require("bitcoinjs-lib"),A=require("tiny-secp256k1"),e=require("@gardenfi/utils"),p=require("./index6.cjs"),b=require("./index17.cjs");function P(s){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(s){for(const r in s)if(r!=="default"){const n=Object.getOwnPropertyDescriptor(s,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:()=>s[r]})}}return t.default=s,Object.freeze(t)}const N=P(I),L=P(A);N.initEccLib(L);var o,a;class x{constructor(t){v(this,o);v(this,a);this.address="",this.id=p.WALLET_CONFIG.Enkrypt.id,this.name=p.WALLET_CONFIG.Enkrypt.name,this.icon=p.WALLET_CONFIG.Enkrypt.icon,this.disconnect=()=>(this.address="",Promise.resolve(e.Ok("Disconnected Enkrypt wallet"))),E(this,o,t),E(this,a,e.Network.MAINNET)}async connect(t){if(t||(t=e.Network.MAINNET),t===e.Network.TESTNET)return e.Err("Enkrypt wallet does not support testnet");try{const r=await i(this,o).requestAccounts();return r.length>0&&(this.address=r[0]),e.Ok({address:this.address,provider:this,network:t,id:p.WALLET_CONFIG.Enkrypt.id})}catch(r){return e.Err("Error while connecting to Enkrypt wallet: "+r)}}async requestAccounts(){return await e.executeWithTryCatch(async()=>await i(this,o).requestAccounts(),"Error while requesting accounts from the Enkrypt wallet")}async getAccounts(){return this.requestAccounts()}async getNetwork(){return e.Ok(i(this,a))}async switchNetwork(){E(this,a,i(this,a)===e.Network.MAINNET?e.Network.TESTNET:e.Network.MAINNET);const t=await this.connect();return t.error?e.Err(`Failed to connect to ${i(this,a)}: ${t.error}`):e.Ok(i(this,a))}async getBalance(){let t=await this.getAccounts();if(!t.ok)return e.Err("Failed to get address");this.address=t.val[0];const r=await this.getNetwork();return r.ok?(E(this,a,r.val),await b.getBalance(this.address,i(this,a))):e.Err("Failed to get network")}async sendLitecoin(t,r){try{const n=await this.getAccounts();if(!n.ok)return e.Err("Failed to get address");this.address=n.val[0];const g=await this.getNetwork();if(!g.ok)return e.Err("Failed to get network");if(E(this,a,g.val),!t||t.trim()==="")return e.Err("Invalid destination address");if(r<=0)return e.Err("Amount must be greater than 0");const l=await this.getBalance();if(!l.ok)return e.Err("Failed to get balance",l.error);if(l.val.total<r)return e.Err("Insufficient balance");const u=await b.getUTXOs(this.address,i(this,a));if(!u.ok)return e.Err("Failed to fetch UTXOs",u.error);const w=u.val;if(!w||w.length===0)return e.Err("No UTXOs available");const h=await this.buildTransaction(w,t,r,this.address);if(!h.ok)return e.Err("Failed to build transaction",h.error);const f=await this.signPsbt(h.val);if(!f.ok)return e.Err("Failed to sign transaction",f.error);const c=await this.broadcastTransaction(f.val);return c.ok?e.Ok(c.val):e.Err("Failed to broadcast transaction",c.error)}catch(n){return n instanceof Error?n.message.includes("User rejected")||n.message.includes("cancelled")||n.message.includes("denied")?e.Err("Transaction cancelled by user"):e.Err("Error while sending Litecoin: "+n.message):e.Err("Error while sending Litecoin",String(n))}}async buildTransaction(t,r,n,g){try{const l=b.getLitecoinNetwork(i(this,a)),u=new N.Psbt({network:l}),w=t.sort((d,k)=>k.value-d.value),h=await b.getRecommendedFeeRate(i(this,a));if(!h.ok)return e.Err("Failed to fetch recommended fee rate",h.error);const f=h.val;let c=0;for(const d of w){if(c>=n+f*250)break;let k;try{k={script:N.address.toOutputScript(g,l),value:d.value}}catch{if(d.scriptPubKey)k={script:Buffer.from(d.scriptPubKey,"hex"),value:d.value};else throw new Error("Cannot determine script for UTXO")}u.addInput({hash:d.txid,index:d.vout,witnessUtxo:k}),c+=d.value}if(c===0)return e.Err("No valid UTXOs found");const y=(u.txInputs.length*148+2*34+10)*f;if(c<n+y)return e.Err(`Insufficient funds: need ${n+y} sats, have ${c} sats`);u.addOutput({address:r,value:n});const T=c-n-y;return T>546&&u.addOutput({address:g,value:T}),e.Ok(u.toBase64())}catch(l){return e.Err("Error building transaction",String(l))}}async signPsbt(t){return await e.executeWithTryCatch(async()=>await i(this,o).signPsbt(t),"Error while signing transaction")}async broadcastTransaction(t){const r=typeof i(this,o).pushPsbt=="function"?i(this,o).pushPsbt.bind(i(this,o)):void 0;return await b.broadcastTransaction(t,i(this,a),r)}on(t,r){i(this,o).on(t,r)}off(t,r){i(this,o).off(t,r)}}o=new WeakMap,a=new WeakMap;exports.EnkryptProvider=x;
1
+ "use strict";var l=n=>{throw TypeError(n)};var d=(n,e,t)=>e.has(n)||l("Cannot "+t);var o=(n,e,t)=>(d(n,e,"read from private field"),t?t.call(n):e.get(n)),N=(n,e,t)=>e.has(n)?l("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(n):e.set(n,t),k=(n,e,t,s)=>(d(n,e,"write to private field"),s?s.call(n,t):e.set(n,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("./index3.cjs"),u=require("./index19.cjs"),r=require("@gardenfi/utils");var i;class y{constructor(e){N(this,i);this.address="",this.id=w.WALLET_CONFIG.Xverse.id,this.name=w.WALLET_CONFIG.Xverse.name,this.icon=w.WALLET_CONFIG.Xverse.icon,this.connect=async t=>{try{t||(t=r.Network.MAINNET),await o(this,i).request("wallet_connect",null);const s=await this.getNetwork();if(s.error)return r.Err("Could not get network",s.error);if(s.val!==t){const c=await this.switchNetwork();if(c.error)return r.Err("Failed to switch network",c.error)}const a=await this.getAccounts();return a.ok&&a.val.length>0&&(this.address=a.val[0]),r.Ok({address:this.address,provider:this,network:t,id:w.WALLET_CONFIG.Xverse.id})}catch(s){return r.Err("Error while connecting to the XVerse wallet",s)}},this.getBalance=async()=>await r.executeWithTryCatch(async()=>(await o(this,i).request("getBalance",{})).result,"Error while getting balance from XVerse wallet"),this.requestAccounts=async()=>await r.executeWithTryCatch(async()=>(await o(this,i).request("getAccounts",{purposes:["payment"],message:"I want to connect"})).result.map(s=>s.address)),this.getAccounts=async()=>await r.executeWithTryCatch(async()=>(await o(this,i).request("getAddresses",{purposes:["payment"]})).result.addresses.map(s=>s.address)),this.sendBitcoin=async(t,s)=>await r.executeWithTryCatch(async()=>{var h;const a=await o(this,i).request("sendTransfer",{recipients:[{address:t,amount:s}]}),c=(h=a.result)==null?void 0:h.txid;if(c)return r.Ok(c);throw new Error(a.error)},"Error while sending bitcoin from Xverse wallet"),this.on=()=>{},this.off=()=>{},this.disconnect=()=>(this.address="",Promise.resolve(r.Ok("Disconnected"))),k(this,i,e)}async getNetwork(){return await r.executeWithTryCatch(async()=>{const e=await o(this,i).request("wallet_getNetwork",null);if(e.result.bitcoin.name===u.XverseBitcoinNetworkType.Mainnet)return r.Network.MAINNET;if(e.result.bitcoin.name===u.XverseBitcoinNetworkType.Testnet4)return r.Network.TESTNET;throw new Error(e.result)},"Error while getting network from Xverse wallet")}async switchNetwork(){try{const e=await this.getNetwork();if(e.error)return r.Err("Failed to get current network");const t=e.val===r.Network.MAINNET?u.XverseBitcoinNetworkType.Testnet4:u.XverseBitcoinNetworkType.Mainnet;await o(this,i).request("wallet_changeNetwork",{name:t});const s=await this.getNetwork();return s.ok?r.Ok(s.val):r.Err("Failed to verify network switch")}catch(e){return r.Err("Error while switching network in Xverse:",e)}}}i=new WeakMap;exports.XverseProvider=y;