btc-wallet 0.5.35-beta → 0.5.36-beta

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.
@@ -5,11 +5,11 @@ export declare function BtcWalletSelectorContextProvider({ children, }: {
5
5
  }): import("react/jsx-runtime").JSX.Element;
6
6
  export declare function useBtcWalletSelector(): {
7
7
  login: () => Promise<string | null>;
8
- autoConnect: () => Promise<any>;
8
+ autoConnect: () => Promise<string | null>;
9
9
  logout: () => void;
10
10
  account: string;
11
- getPublicKey: () => Promise<string | undefined>;
12
- signMessage: (msg: string) => any;
11
+ getPublicKey: () => Promise<string>;
12
+ signMessage: (msg: string) => Promise<string>;
13
13
  getContext: () => any;
14
14
  getNetwork: () => Promise<"livenet" | "testnet">;
15
15
  switchNetwork: (network: "livenet" | "testnet") => Promise<void>;
package/dist/index.js CHANGED
@@ -2689,6 +2689,10 @@ function ComfirmBox({ onClose, status = 1, fromChain = {
2689
2689
 
2690
2690
  // src/core/btcWalletSelectorContext.tsx
2691
2691
  var import_jsx_runtime12 = require("react/jsx-runtime");
2692
+ var eventCache = {
2693
+ lastProcessedAccount: "",
2694
+ lastProcessedTime: 0
2695
+ };
2692
2696
  var WalletSelectorContext = import_react11.default.createContext(null);
2693
2697
  function BtcWalletSelectorContextProvider({
2694
2698
  children
@@ -2776,76 +2780,63 @@ function useBtcWalletSelector() {
2776
2780
  getNetwork: getNetwork2,
2777
2781
  switchNetwork
2778
2782
  } = useBTCProvider();
2779
- const publicKey = (0, import_react11.useRef)(null);
2780
- const signMessageFn = (0, import_react11.useRef)(null);
2781
2783
  const connectorRef = (0, import_react11.useRef)(null);
2782
2784
  const context = (0, import_react11.useContext)(WalletSelectorContext);
2783
2785
  (0, import_react11.useEffect)(() => {
2784
- if (provider) {
2786
+ const handleAccountsChanged = (account) => {
2787
+ if (!(account == null ? void 0 : account.length))
2788
+ return;
2789
+ const accountKey = JSON.stringify(account);
2790
+ const now = Date.now();
2791
+ if (accountKey === eventCache.lastProcessedAccount && now - eventCache.lastProcessedTime < 3e3) {
2792
+ return;
2793
+ }
2794
+ eventCache.lastProcessedAccount = accountKey;
2795
+ eventCache.lastProcessedTime = now;
2785
2796
  getPublicKey().then((res) => {
2786
- publicKey.current = res;
2797
+ context.emit("updatePublicKey", res);
2787
2798
  });
2788
- }
2789
- }, [getPublicKey, provider]);
2790
- (0, import_react11.useEffect)(() => {
2791
- signMessageFn.current = signMessage;
2792
- }, [signMessage]);
2793
- (0, import_react11.useEffect)(() => {
2794
- const fn = (account) => {
2795
- console.log("accountsChanged account", account);
2796
- if (account == null ? void 0 : account.length) {
2797
- getPublicKey().then((res) => {
2798
- publicKey.current = res;
2799
- context.emit("updatePublicKey", res);
2800
- });
2801
- }
2802
2799
  };
2803
- if (connector) {
2804
- connector.on("accountsChanged", fn);
2805
- }
2800
+ connector == null ? void 0 : connector.on("accountsChanged", handleAccountsChanged);
2806
2801
  connectorRef.current = connector;
2807
2802
  return () => {
2808
- if (connector) {
2809
- connector.removeListener("accountsChanged", fn);
2810
- }
2803
+ connector == null ? void 0 : connector.removeListener("accountsChanged", handleAccountsChanged);
2811
2804
  };
2812
2805
  }, [connector, context, getPublicKey]);
2813
2806
  const hook = (0, import_react11.useMemo)(() => {
2807
+ const connectWallet = (useModal = false) => __async(this, null, function* () {
2808
+ if (connectModalOpen)
2809
+ return null;
2810
+ const account = accounts == null ? void 0 : accounts[0];
2811
+ if (account)
2812
+ return account;
2813
+ try {
2814
+ if (useModal) {
2815
+ openConnectModal == null ? void 0 : openConnectModal();
2816
+ } else {
2817
+ yield requestDirectAccount(connectorRef.current);
2818
+ }
2819
+ const account2 = yield retryOperation(
2820
+ () => window.btcContext.account,
2821
+ (res) => !!res,
2822
+ {
2823
+ maxRetries: 100,
2824
+ delayMs: 1e3
2825
+ }
2826
+ );
2827
+ return account2 || null;
2828
+ } catch (error) {
2829
+ console.error("btcLoginError", error);
2830
+ context.emit("btcLoginError");
2831
+ return null;
2832
+ }
2833
+ });
2814
2834
  return {
2815
2835
  login: () => __async(this, null, function* () {
2816
- const account = accounts == null ? void 0 : accounts[0];
2817
- console.log("account", account);
2818
- console.log("connecting", connectModalOpen);
2819
- if (!account) {
2820
- if (connectModalOpen) {
2821
- return null;
2822
- }
2823
- try {
2824
- openConnectModal == null ? void 0 : openConnectModal();
2825
- const account1 = yield retryOperation(
2826
- () => window.btcContext.account,
2827
- (res) => !!res,
2828
- {
2829
- maxRetries: 100,
2830
- delayMs: 1e3
2831
- }
2832
- );
2833
- if (!account1) {
2834
- throw new Error("Failed to get account");
2835
- }
2836
- return account1;
2837
- } catch (error) {
2838
- console.error("btcLoginError", error);
2839
- context.emit("btcLoginError");
2840
- }
2841
- }
2842
- return account;
2836
+ return connectWallet(true);
2843
2837
  }),
2844
2838
  autoConnect: () => __async(this, null, function* () {
2845
- return requestDirectAccount(connectorRef.current).catch((e) => {
2846
- console.error("btcLoginError", e);
2847
- context.emit("btcLoginError");
2848
- });
2839
+ return connectWallet(false);
2849
2840
  }),
2850
2841
  logout: () => {
2851
2842
  const accountId = accounts == null ? void 0 : accounts[0];
@@ -2856,29 +2847,25 @@ function useBtcWalletSelector() {
2856
2847
  },
2857
2848
  account: accounts == null ? void 0 : accounts[0],
2858
2849
  getPublicKey: () => __async(this, null, function* () {
2859
- const publicKey2 = yield getPublicKey();
2860
- if (publicKey2)
2861
- return publicKey2;
2862
- if (connectModalOpen)
2863
- return;
2864
- try {
2865
- yield requestDirectAccount(connectorRef.current);
2866
- return yield getPublicKey();
2867
- } catch (error) {
2868
- console.error("btcLoginError", error);
2869
- context.emit("btcLoginError");
2870
- return;
2871
- }
2850
+ const publicKey = yield getPublicKey().catch(() => null);
2851
+ if (publicKey)
2852
+ return publicKey;
2853
+ yield connectWallet(false);
2854
+ return getPublicKey();
2855
+ }),
2856
+ signMessage: (msg) => __async(this, null, function* () {
2857
+ yield connectWallet(false);
2858
+ return signMessage(msg);
2872
2859
  }),
2873
- signMessage: (msg) => {
2874
- return signMessageFn.current(msg);
2875
- },
2876
2860
  getContext: () => {
2877
2861
  return context;
2878
2862
  },
2879
2863
  getNetwork: getNetwork2,
2880
2864
  switchNetwork,
2881
- sendBitcoin: sendBitcoin2
2865
+ sendBitcoin: (toAddress, satoshis, options) => __async(this, null, function* () {
2866
+ yield connectWallet(false);
2867
+ return sendBitcoin2(toAddress, satoshis, options);
2868
+ })
2882
2869
  };
2883
2870
  }, [
2884
2871
  accounts,
@@ -2890,7 +2877,8 @@ function useBtcWalletSelector() {
2890
2877
  context,
2891
2878
  requestDirectAccount,
2892
2879
  disconnect,
2893
- getPublicKey
2880
+ getPublicKey,
2881
+ signMessage
2894
2882
  ]);
2895
2883
  return hook;
2896
2884
  }
@@ -4605,7 +4593,6 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
4605
4593
  console.log("setupBtcContextListeners clear");
4606
4594
  }
4607
4595
  const valid = validateWalletState();
4608
- console.log("setupBtcContextListeners wallet state valid:", valid);
4609
4596
  if (!valid) {
4610
4597
  return;
4611
4598
  }
@@ -4925,7 +4912,7 @@ function getGroup(state) {
4925
4912
 
4926
4913
  // src/index.ts
4927
4914
  var getVersion = () => {
4928
- return "0.5.35-beta";
4915
+ return "0.5.36-beta";
4929
4916
  };
4930
4917
  if (typeof window !== "undefined") {
4931
4918
  window.__BTC_WALLET_VERSION = getVersion();