btc-wallet 0.5.34-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.
package/README.md CHANGED
@@ -76,6 +76,7 @@ interface ExecuteBTCDepositAndActionParams<T extends boolean = true> {
76
76
  pollResult?: T; // optional: whether to poll for transaction result
77
77
  registerDeposit?: string; // optional: whether to register deposit,default 0.000125 NEAR
78
78
  newAccountMinDepositAmount?: boolean; // default is true, if true, new account minimum deposit BTC amount 1000sat, otherwise 0
79
+ registerContractId?: string; // if registerContractId is provided, it will be used to register the contract, otherwise it will be the default contract id
79
80
  }
80
81
 
81
82
  // Example 1: dApp one-click BTC deposit
@@ -52,12 +52,14 @@ interface ExecuteBTCDepositAndActionParams<T extends boolean = true> {
52
52
  env?: ENV;
53
53
  pollResult?: T;
54
54
  newAccountMinDepositAmount?: boolean;
55
+ /** if registerContractId is provided, it will be used to register the contract, otherwise it will be the default contract id */
56
+ registerContractId?: string;
55
57
  }
56
58
  /**
57
59
  * @param T - if true, return the poll result, otherwise return the btcTxHash
58
60
  */
59
61
  type ExecuteBTCDepositAndActionReturn<T extends boolean> = T extends true ? FinalExecutionOutcome[] : string;
60
- export declare function executeBTCDepositAndAction<T extends boolean = true>({ action, amount, feeRate, pollResult, registerDeposit, env, newAccountMinDepositAmount, }: ExecuteBTCDepositAndActionParams<T>): Promise<ExecuteBTCDepositAndActionReturn<T>>;
62
+ export declare function executeBTCDepositAndAction<T extends boolean = true>({ action, amount, feeRate, pollResult, registerDeposit, env, newAccountMinDepositAmount, registerContractId, }: ExecuteBTCDepositAndActionParams<T>): Promise<ExecuteBTCDepositAndActionReturn<T>>;
61
63
  export declare function checkSatoshiWhitelist(btcAccountId: string, env?: ENV): Promise<void>;
62
64
  interface WithdrawParams {
63
65
  amount: string | number;
@@ -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
  }
@@ -4053,7 +4041,8 @@ function executeBTCDepositAndAction(_0) {
4053
4041
  pollResult = true,
4054
4042
  registerDeposit,
4055
4043
  env = "mainnet",
4056
- newAccountMinDepositAmount
4044
+ newAccountMinDepositAmount,
4045
+ registerContractId
4057
4046
  }) {
4058
4047
  var _a;
4059
4048
  try {
@@ -4091,17 +4080,14 @@ function executeBTCDepositAndAction(_0) {
4091
4080
  }));
4092
4081
  }
4093
4082
  const storageDepositMsg = {};
4094
- const registerContractId = ((action == null ? void 0 : action.receiver_id) || config.btcToken).replace(
4095
- config.accountContractId,
4096
- config.btcToken
4097
- );
4098
- console.log("executeBTCDepositAndAction registerContractId", registerContractId);
4099
- const registerRes = yield nearCall(registerContractId, "storage_balance_of", {
4083
+ const _registerContractId = registerContractId || ((action == null ? void 0 : action.receiver_id) || config.btcToken).replace(config.accountContractId, config.btcToken);
4084
+ console.log("executeBTCDepositAndAction registerContractId", _registerContractId);
4085
+ const registerRes = yield nearCall(_registerContractId, "storage_balance_of", {
4100
4086
  account_id: csna
4101
4087
  });
4102
4088
  if (!(registerRes == null ? void 0 : registerRes.available)) {
4103
4089
  storageDepositMsg.storage_deposit_msg = {
4104
- contract_id: registerContractId,
4090
+ contract_id: _registerContractId,
4105
4091
  deposit: registerDeposit || NEAR_STORAGE_DEPOSIT_AMOUNT,
4106
4092
  registration_only: true
4107
4093
  };
@@ -4607,7 +4593,6 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
4607
4593
  console.log("setupBtcContextListeners clear");
4608
4594
  }
4609
4595
  const valid = validateWalletState();
4610
- console.log("setupBtcContextListeners wallet state valid:", valid);
4611
4596
  if (!valid) {
4612
4597
  return;
4613
4598
  }
@@ -4927,7 +4912,7 @@ function getGroup(state) {
4927
4912
 
4928
4913
  // src/index.ts
4929
4914
  var getVersion = () => {
4930
- return "0.5.34-beta";
4915
+ return "0.5.36-beta";
4931
4916
  };
4932
4917
  if (typeof window !== "undefined") {
4933
4918
  window.__BTC_WALLET_VERSION = getVersion();