btc-wallet 0.3.17 → 0.3.18

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,8 +11,8 @@ export declare abstract class BaseConnector {
11
11
  abstract getAccounts(): Promise<string[]>;
12
12
  abstract getPublicKey(): Promise<string>;
13
13
  abstract signMessage(signStr: string, type?: 'ecdsa' | 'bip322-simple'): Promise<string>;
14
- abstract on(event: string, handler: (data?: unknown) => void): void;
15
- abstract removeListener(event: string, handler: (data?: unknown) => void): void;
14
+ abstract on(event: string, handler: (data?: any) => void): void;
15
+ abstract removeListener(event: string, handler: (data?: any) => void): void;
16
16
  [key: string]: any;
17
17
  abstract getProvider(): any;
18
18
  abstract getNetwork(): Promise<'livenet' | 'testnet'>;
@@ -9,7 +9,7 @@ export declare function getAccountInfo(csna: string, accountContractId: string):
9
9
  gas_token: Record<string, string>;
10
10
  debt_info?: DebtInfo;
11
11
  } | undefined>;
12
- export declare function checkGasTokenBalance(csna: string, gasToken: string, isDev: boolean): Promise<void>;
12
+ export declare function checkGasTokenBalance(csna: string, gasToken: string, minAmount: string, isDev: boolean): Promise<void>;
13
13
  type CheckGasTokenArrearsReturnType<T extends boolean> = T extends true ? void : {
14
14
  receiver_id: string;
15
15
  amount: string;
@@ -4,7 +4,7 @@ export declare function BtcWalletSelectorContextProvider({ children, }: {
4
4
  autoConnect?: boolean;
5
5
  }): import("react/jsx-runtime").JSX.Element;
6
6
  export declare function useBtcWalletSelector(): {
7
- login: () => Promise<string>;
7
+ login: () => Promise<string | null>;
8
8
  autoConnect: () => Promise<void>;
9
9
  logout: () => void;
10
10
  account: string;
@@ -12,6 +12,7 @@ export declare function useBtcWalletSelector(): {
12
12
  signMessage: (msg: string) => any;
13
13
  getContext: () => any;
14
14
  getNetwork: () => Promise<"livenet" | "testnet">;
15
+ switchNetwork: (network: "livenet" | "testnet") => Promise<void>;
15
16
  sendBitcoin: (toAddress: string, satoshis: number, options?: {
16
17
  feeRate: number;
17
18
  }) => Promise<string>;
package/dist/index.js CHANGED
@@ -143,6 +143,7 @@ var InjectedConnector = class extends BaseConnector {
143
143
  requestAccounts() {
144
144
  return __async(this, null, function* () {
145
145
  const accounts = yield this.getProviderOrThrow().requestAccounts();
146
+ console.log("network:", yield this.getNetwork());
146
147
  console.log("\u{1F680} ~ InjectedConnector ~ requestAccounts ~ accounts:", accounts);
147
148
  return accounts;
148
149
  });
@@ -504,6 +505,7 @@ var XverseConnector = class extends BaseConnector {
504
505
  });
505
506
  }
506
507
  };
508
+ console.log("\u{1F680} ~ XverseConnector ~ sendBitcoin ~ sendBtcOptions:", sendBtcOptions);
507
509
  sendBtcTransaction(sendBtcOptions).catch((e) => reject(e));
508
510
  });
509
511
  return result;
@@ -2490,12 +2492,22 @@ function InitBtcWalletSelectorContext() {
2490
2492
  return null;
2491
2493
  }
2492
2494
  function useBtcWalletSelector() {
2493
- const { openConnectModal, openConnectModalAsync, disconnect, requestDirectAccount } = useConnectModal();
2494
- const { accounts, sendBitcoin: sendBitcoin2, getPublicKey, provider, signMessage, connector, getNetwork: getNetwork2 } = useBTCProvider();
2495
+ const { openConnectModal, disconnect, requestDirectAccount } = useConnectModal();
2496
+ const {
2497
+ accounts,
2498
+ sendBitcoin: sendBitcoin2,
2499
+ getPublicKey,
2500
+ provider,
2501
+ signMessage,
2502
+ connector,
2503
+ getNetwork: getNetwork2,
2504
+ switchNetwork
2505
+ } = useBTCProvider();
2495
2506
  const publicKey = (0, import_react11.useRef)(null);
2496
2507
  const signMessageFn = (0, import_react11.useRef)(null);
2497
2508
  const connectorRef = (0, import_react11.useRef)(null);
2498
2509
  const context = (0, import_react11.useContext)(WalletSelectorContext);
2510
+ const isLoggingIn = (0, import_react11.useRef)(false);
2499
2511
  (0, import_react11.useEffect)(() => {
2500
2512
  if (provider) {
2501
2513
  getPublicKey().then((res) => {
@@ -2530,7 +2542,27 @@ function useBtcWalletSelector() {
2530
2542
  login: () => __async(this, null, function* () {
2531
2543
  const account = accounts == null ? void 0 : accounts[0];
2532
2544
  if (!account) {
2533
- openConnectModal == null ? void 0 : openConnectModal();
2545
+ if (isLoggingIn.current) {
2546
+ return null;
2547
+ }
2548
+ try {
2549
+ isLoggingIn.current = true;
2550
+ openConnectModal == null ? void 0 : openConnectModal();
2551
+ const account1 = yield retryOperation(
2552
+ () => window.btcContext.account,
2553
+ (res) => !!res,
2554
+ {
2555
+ maxRetries: 100,
2556
+ delayMs: 1e3
2557
+ }
2558
+ );
2559
+ if (!account1) {
2560
+ throw new Error("Failed to get account");
2561
+ }
2562
+ return account1;
2563
+ } finally {
2564
+ isLoggingIn.current = false;
2565
+ }
2534
2566
  }
2535
2567
  return account;
2536
2568
  }),
@@ -2557,6 +2589,10 @@ function useBtcWalletSelector() {
2557
2589
  return context;
2558
2590
  },
2559
2591
  getNetwork: getNetwork2,
2592
+ switchNetwork: (network) => __async(this, null, function* () {
2593
+ console.log("switchNetwork:", network);
2594
+ yield switchNetwork(network);
2595
+ }),
2560
2596
  sendBitcoin: sendBitcoin2
2561
2597
  };
2562
2598
  }, [
@@ -2564,6 +2600,7 @@ function useBtcWalletSelector() {
2564
2600
  context,
2565
2601
  disconnect,
2566
2602
  getNetwork2,
2603
+ switchNetwork,
2567
2604
  openConnectModal,
2568
2605
  requestDirectAccount,
2569
2606
  sendBitcoin2
@@ -2576,7 +2613,7 @@ var import_near_api_js2 = require("near-api-js");
2576
2613
  var import_transactions = require("@near-js/transactions");
2577
2614
  var import_key_pair = require("near-api-js/lib/utils/key_pair");
2578
2615
  var import_transaction = require("near-api-js/lib/transaction");
2579
- var import_utils6 = require("@near-js/utils");
2616
+ var import_utils7 = require("@near-js/utils");
2580
2617
  var import_bs58 = __toESM(require("bs58"), 1);
2581
2618
  var import_js_sha256 = require("js-sha256");
2582
2619
 
@@ -3228,11 +3265,11 @@ function getAccountInfo(csna, accountContractId) {
3228
3265
  return accountInfo;
3229
3266
  });
3230
3267
  }
3231
- function checkGasTokenBalance(csna, gasToken, isDev) {
3268
+ function checkGasTokenBalance(csna, gasToken, minAmount, isDev) {
3232
3269
  return __async(this, null, function* () {
3233
3270
  const amount = yield nearCall(gasToken, "ft_balance_of", { account_id: csna });
3234
3271
  console.log("gas token balance:", amount);
3235
- if (new import_big.default(amount).lte(MINIMUM_DEPOSIT_AMOUNT_BASE)) {
3272
+ if (new import_big.default(amount).lt(minAmount)) {
3236
3273
  yield Dialog.confirm({
3237
3274
  title: "Gas token balance is insufficient",
3238
3275
  message: "Please deposit gas token to continue, will open bridge website."
@@ -3488,7 +3525,6 @@ var state = {
3488
3525
  return window.localStorage.getItem("btc-wallet-btc-publickey");
3489
3526
  }
3490
3527
  };
3491
- var inter = null;
3492
3528
  var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3493
3529
  metadata,
3494
3530
  options,
@@ -3511,41 +3547,49 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3511
3547
  const isDev = (_a = "isDev" in metadata && metadata.isDev) != null ? _a : false;
3512
3548
  const currentConfig = isDev ? walletConfig.dev : walletConfig[options.network.networkId];
3513
3549
  const walletNetwork = isDev ? "dev" : options.network.networkId;
3514
- initWalletButton(walletNetwork, wallet);
3515
- if (!inter) {
3516
- inter = setInterval(() => __async(void 0, null, function* () {
3517
- const btcContext = window.btcContext;
3518
- if (btcContext) {
3519
- clearInterval(inter);
3520
- const context = btcContext.getContext();
3521
- context.on("updatePublicKey", (btcPublicKey) => __async(void 0, null, function* () {
3522
- const { nearTempAddress } = yield getNearAccountByBtcPublicKey(btcPublicKey);
3523
- removeWalletButton();
3524
- initWalletButton(walletNetwork, wallet);
3525
- emitter.emit("accountsChanged", {
3526
- accounts: [
3527
- {
3528
- accountId: nearTempAddress
3529
- }
3530
- ]
3531
- });
3532
- }));
3533
- context.on("btcLoginError", (e) => __async(void 0, null, function* () {
3534
- emitter.emit("accountsChanged", {
3535
- accounts: []
3536
- });
3537
- }));
3538
- context.on("btcLogOut", (e) => __async(void 0, null, function* () {
3539
- emitter.emit("accountsChanged", {
3540
- accounts: []
3541
- });
3542
- }));
3543
- if ("autoConnect" in metadata && metadata.autoConnect && localStorage.getItem("near-wallet-selector:selectedWalletId") === '"btc-wallet"') {
3544
- yield btcContext.autoConnect();
3545
- }
3546
- clearInterval(inter);
3550
+ yield initBtcContext();
3551
+ function setupBtcContextListeners(btcContext) {
3552
+ return __async(this, null, function* () {
3553
+ const context = btcContext.getContext();
3554
+ context.on("updatePublicKey", (btcPublicKey) => __async(this, null, function* () {
3555
+ const { nearTempAddress } = yield getNearAccountByBtcPublicKey(btcPublicKey);
3556
+ removeWalletButton();
3557
+ initConnected(walletNetwork, wallet);
3558
+ emitter.emit("accountsChanged", {
3559
+ accounts: [{ accountId: nearTempAddress }]
3560
+ });
3561
+ }));
3562
+ context.on("btcLoginError", () => __async(this, null, function* () {
3563
+ emitter.emit("accountsChanged", { accounts: [] });
3564
+ }));
3565
+ context.on("btcLogOut", () => __async(this, null, function* () {
3566
+ emitter.emit("accountsChanged", { accounts: [] });
3567
+ }));
3568
+ if ("autoConnect" in metadata && metadata.autoConnect && localStorage.getItem("near-wallet-selector:selectedWalletId") === '"btc-wallet"') {
3569
+ yield btcContext.autoConnect();
3547
3570
  }
3548
- }), 500);
3571
+ });
3572
+ }
3573
+ function initBtcContext() {
3574
+ return __async(this, null, function* () {
3575
+ console.log("initBtcContext");
3576
+ const btcContext = yield retryOperation(
3577
+ () => __async(this, null, function* () {
3578
+ const ctx = window.btcContext;
3579
+ if (!ctx) {
3580
+ throw new Error("btcContext not found");
3581
+ }
3582
+ return ctx;
3583
+ }),
3584
+ (res) => !!res,
3585
+ {
3586
+ maxRetries: 10,
3587
+ delayMs: 500
3588
+ }
3589
+ );
3590
+ yield setupBtcContextListeners(btcContext);
3591
+ return btcContext;
3592
+ });
3549
3593
  }
3550
3594
  function nearCall2(contractId, methodName, args) {
3551
3595
  return __async(this, null, function* () {
@@ -3575,28 +3619,20 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3575
3619
  }
3576
3620
  function signIn(_02) {
3577
3621
  return __async(this, arguments, function* ({ contractId, methodNames }) {
3622
+ const btcContext = window.btcContext;
3578
3623
  const accountId = state.getAccount();
3579
3624
  const publicKey = state.getPublicKey();
3580
- const btcContext = window.btcContext;
3581
- initWalletButton(walletNetwork, wallet);
3582
- if (accountId && publicKey) {
3583
- return [
3584
- {
3585
- accountId,
3586
- publicKey
3587
- }
3588
- ];
3625
+ console.log("isLogin:", accountId && publicKey);
3626
+ if (!accountId || !publicKey) {
3627
+ yield btcContext.login();
3589
3628
  }
3590
- yield btcContext.login();
3591
- const btcPublicKey = yield retryOperation(btcContext.getPublicKey, (res) => !!res, {
3592
- maxRetries: 40,
3593
- delayMs: 3e3
3594
- });
3629
+ const btcPublicKey = yield btcContext.getPublicKey();
3595
3630
  console.log("btcPublicKey:", btcPublicKey);
3596
3631
  if (!btcPublicKey) {
3597
3632
  throw new Error("No connected BTC wallet, please connect your BTC wallet first.");
3598
3633
  }
3599
3634
  const { nearTempAddress, nearTempPublicKey } = yield getNearAccountByBtcPublicKey(btcPublicKey);
3635
+ initConnected(walletNetwork, wallet);
3600
3636
  return [
3601
3637
  {
3602
3638
  accountId: nearTempAddress,
@@ -3615,6 +3651,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3615
3651
  const btcContext = window.btcContext;
3616
3652
  if (metadata.syncLogOut) {
3617
3653
  btcContext.logout();
3654
+ yield delay(300);
3618
3655
  }
3619
3656
  state.clear();
3620
3657
  window.localStorage.removeItem("near-wallet-selector:selectedWalletId");
@@ -3652,7 +3689,6 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3652
3689
  const btcContext = window.btcContext;
3653
3690
  const accountId = state.getAccount();
3654
3691
  const accountInfo = yield getAccountInfo(accountId, currentConfig.accountContractId);
3655
- yield checkGasTokenBalance(accountId, currentConfig.token, isDev);
3656
3692
  yield checkGasTokenArrears(accountInfo == null ? void 0 : accountInfo.debt_info, isDev, true);
3657
3693
  const trans = [...params.transactions];
3658
3694
  console.log("raw trans:", trans);
@@ -3664,6 +3700,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3664
3700
  console.log("transferGasTransaction:", transferGasTransaction);
3665
3701
  console.log("useNearPayGas:", useNearPayGas);
3666
3702
  console.log("gasLimit:", gasLimit);
3703
+ yield checkGasTokenBalance(accountId, currentConfig.token, gasLimit, isDev);
3667
3704
  if (transferGasTransaction) {
3668
3705
  trans.unshift(transferGasTransaction);
3669
3706
  }
@@ -3844,7 +3881,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3844
3881
  transaction.receiverId,
3845
3882
  BigInt(nearNonceNumber) + BigInt(index),
3846
3883
  newActions,
3847
- (0, import_utils6.baseDecode)(header.hash)
3884
+ (0, import_utils7.baseDecode)(header.hash)
3848
3885
  );
3849
3886
  const txBytes = (0, import_transaction.encodeTransaction)(_transaction);
3850
3887
  const txHex = Array.from(txBytes, (byte) => ("0" + (byte & 255).toString(16)).slice(-2)).join(
@@ -3854,7 +3891,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3854
3891
  return { txBytes, txHex, hash };
3855
3892
  });
3856
3893
  }
3857
- function initWalletButton(network, wallet2) {
3894
+ function initConnected(network, wallet2) {
3858
3895
  return __async(this, null, function* () {
3859
3896
  const checkAndSetupWalletButton = () => {
3860
3897
  const accountId = state.getAccount();
@@ -3913,7 +3950,7 @@ function setupBTCWallet({
3913
3950
 
3914
3951
  // src/index.ts
3915
3952
  var getVersion = () => {
3916
- return "0.3.17";
3953
+ return "0.3.18";
3917
3954
  };
3918
3955
  if (typeof window !== "undefined") {
3919
3956
  window.__BTC_WALLET_VERSION = getVersion();