btc-wallet 0.3.18 → 0.3.19

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/dist/index.js CHANGED
@@ -329,7 +329,7 @@ var _network, _event;
329
329
  var XverseConnector = class extends BaseConnector {
330
330
  constructor() {
331
331
  super();
332
- __privateAdd(this, _network, "Testnet");
332
+ __privateAdd(this, _network, "Mainnet");
333
333
  __privateAdd(this, _event, new import_events.default());
334
334
  this.metadata = {
335
335
  id: "xverse",
@@ -2589,21 +2589,18 @@ function useBtcWalletSelector() {
2589
2589
  return context;
2590
2590
  },
2591
2591
  getNetwork: getNetwork2,
2592
- switchNetwork: (network) => __async(this, null, function* () {
2593
- console.log("switchNetwork:", network);
2594
- yield switchNetwork(network);
2595
- }),
2592
+ switchNetwork,
2596
2593
  sendBitcoin: sendBitcoin2
2597
2594
  };
2598
2595
  }, [
2599
2596
  accounts,
2600
- context,
2601
- disconnect,
2602
2597
  getNetwork2,
2603
2598
  switchNetwork,
2599
+ sendBitcoin2,
2604
2600
  openConnectModal,
2605
2601
  requestDirectAccount,
2606
- sendBitcoin2
2602
+ context,
2603
+ disconnect
2607
2604
  ]);
2608
2605
  return hook;
2609
2606
  }
@@ -3541,6 +3538,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3541
3538
  getAccounts,
3542
3539
  verifyOwner,
3543
3540
  signMessage,
3541
+ isSignedIn,
3544
3542
  signAndSendTransaction,
3545
3543
  signAndSendTransactions
3546
3544
  };
@@ -3548,25 +3546,43 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3548
3546
  const currentConfig = isDev ? walletConfig.dev : walletConfig[options.network.networkId];
3549
3547
  const walletNetwork = isDev ? "dev" : options.network.networkId;
3550
3548
  yield initBtcContext();
3551
- function setupBtcContextListeners(btcContext) {
3549
+ function setupBtcContextListeners() {
3552
3550
  return __async(this, null, function* () {
3553
- const context = btcContext.getContext();
3551
+ const handleConnectionUpdate = () => __async(this, null, function* () {
3552
+ yield checkBtcNetwork(walletNetwork);
3553
+ const accountId = state.getAccount();
3554
+ const btcContext = window.btcContext;
3555
+ if (accountId && btcContext.account) {
3556
+ setupWalletButton(walletNetwork, wallet, btcContext);
3557
+ } else {
3558
+ removeWalletButton();
3559
+ setTimeout(() => {
3560
+ handleConnectionUpdate();
3561
+ }, 5e3);
3562
+ }
3563
+ });
3564
+ const context = window.btcContext.getContext();
3554
3565
  context.on("updatePublicKey", (btcPublicKey) => __async(this, null, function* () {
3555
- const { nearTempAddress } = yield getNearAccountByBtcPublicKey(btcPublicKey);
3556
- removeWalletButton();
3557
- initConnected(walletNetwork, wallet);
3566
+ console.log("updatePublicKey");
3567
+ const { nearAddress } = yield getNearAccountByBtcPublicKey(btcPublicKey);
3558
3568
  emitter.emit("accountsChanged", {
3559
- accounts: [{ accountId: nearTempAddress }]
3569
+ accounts: [{ accountId: nearAddress }]
3560
3570
  });
3571
+ yield handleConnectionUpdate();
3561
3572
  }));
3562
3573
  context.on("btcLoginError", () => __async(this, null, function* () {
3574
+ console.log("btcLoginError");
3563
3575
  emitter.emit("accountsChanged", { accounts: [] });
3576
+ yield handleConnectionUpdate();
3564
3577
  }));
3565
3578
  context.on("btcLogOut", () => __async(this, null, function* () {
3579
+ console.log("btcLogOut");
3566
3580
  emitter.emit("accountsChanged", { accounts: [] });
3581
+ yield handleConnectionUpdate();
3567
3582
  }));
3583
+ yield handleConnectionUpdate();
3568
3584
  if ("autoConnect" in metadata && metadata.autoConnect && localStorage.getItem("near-wallet-selector:selectedWalletId") === '"btc-wallet"') {
3569
- yield btcContext.autoConnect();
3585
+ yield window.btcContext.autoConnect();
3570
3586
  }
3571
3587
  });
3572
3588
  }
@@ -3587,7 +3603,7 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3587
3603
  delayMs: 500
3588
3604
  }
3589
3605
  );
3590
- yield setupBtcContextListeners(btcContext);
3606
+ yield setupBtcContextListeners();
3591
3607
  return btcContext;
3592
3608
  });
3593
3609
  }
@@ -3598,22 +3614,22 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3598
3614
  }
3599
3615
  function getNearAccountByBtcPublicKey(btcPublicKey) {
3600
3616
  return __async(this, null, function* () {
3601
- const nearTempAddress = yield nearCall2(
3617
+ const nearAddress = yield nearCall2(
3602
3618
  currentConfig.accountContractId,
3603
3619
  "get_chain_signature_near_account_id",
3604
3620
  { btc_public_key: btcPublicKey }
3605
3621
  );
3606
- const nearTempPublicKey = yield nearCall2(
3622
+ const nearPublicKey = yield nearCall2(
3607
3623
  currentConfig.accountContractId,
3608
3624
  "get_chain_signature_near_account_public_key",
3609
3625
  { btc_public_key: btcPublicKey }
3610
3626
  );
3611
- state.saveAccount(nearTempAddress);
3612
- state.savePublicKey(nearTempPublicKey);
3627
+ state.saveAccount(nearAddress);
3628
+ state.savePublicKey(nearPublicKey);
3613
3629
  state.saveBtcPublicKey(btcPublicKey);
3614
3630
  return {
3615
- nearTempAddress,
3616
- nearTempPublicKey
3631
+ nearAddress,
3632
+ nearPublicKey
3617
3633
  };
3618
3634
  });
3619
3635
  }
@@ -3631,12 +3647,11 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3631
3647
  if (!btcPublicKey) {
3632
3648
  throw new Error("No connected BTC wallet, please connect your BTC wallet first.");
3633
3649
  }
3634
- const { nearTempAddress, nearTempPublicKey } = yield getNearAccountByBtcPublicKey(btcPublicKey);
3635
- initConnected(walletNetwork, wallet);
3650
+ const { nearAddress, nearPublicKey } = yield getNearAccountByBtcPublicKey(btcPublicKey);
3636
3651
  return [
3637
3652
  {
3638
- accountId: nearTempAddress,
3639
- publicKey: nearTempPublicKey
3653
+ accountId: nearAddress,
3654
+ publicKey: nearPublicKey
3640
3655
  }
3641
3656
  ];
3642
3657
  });
@@ -3651,13 +3666,17 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3651
3666
  const btcContext = window.btcContext;
3652
3667
  if (metadata.syncLogOut) {
3653
3668
  btcContext.logout();
3654
- yield delay(300);
3655
3669
  }
3656
3670
  state.clear();
3657
3671
  window.localStorage.removeItem("near-wallet-selector:selectedWalletId");
3658
3672
  removeWalletButton();
3659
3673
  });
3660
3674
  }
3675
+ function isSignedIn() {
3676
+ const accountId = state.getAccount();
3677
+ const publicKey = state.getPublicKey();
3678
+ return accountId && publicKey;
3679
+ }
3661
3680
  function getAccounts() {
3662
3681
  return __async(this, null, function* () {
3663
3682
  return [{ accountId: state.getAccount() }];
@@ -3891,22 +3910,18 @@ var BTCWallet = (_0) => __async(void 0, [_0], function* ({
3891
3910
  return { txBytes, txHex, hash };
3892
3911
  });
3893
3912
  }
3894
- function initConnected(network, wallet2) {
3913
+ function checkBtcNetwork(network) {
3895
3914
  return __async(this, null, function* () {
3896
- const checkAndSetupWalletButton = () => {
3897
- const accountId = state.getAccount();
3898
- const btcContext = window.btcContext;
3899
- if (accountId && btcContext.account) {
3900
- setupWalletButton(network, wallet2, btcContext);
3901
- } else {
3902
- removeWalletButton();
3903
- setTimeout(() => {
3904
- checkAndSetupWalletButton();
3905
- }, 5e3);
3906
- }
3907
- };
3908
- yield delay(1e3);
3909
- checkAndSetupWalletButton();
3915
+ const btcContext = window.btcContext;
3916
+ if (!btcContext.account)
3917
+ return;
3918
+ const btcNetwork = yield btcContext.getNetwork();
3919
+ console.log("btcNetwork:", btcNetwork, network);
3920
+ if (network === "mainnet" && btcNetwork !== "livenet") {
3921
+ yield btcContext.switchNetwork("livenet");
3922
+ } else if (network === "testnet" && btcNetwork !== "testnet") {
3923
+ yield btcContext.switchNetwork("testnet");
3924
+ }
3910
3925
  });
3911
3926
  }
3912
3927
  return wallet;
@@ -3950,7 +3965,7 @@ function setupBTCWallet({
3950
3965
 
3951
3966
  // src/index.ts
3952
3967
  var getVersion = () => {
3953
- return "0.3.18";
3968
+ return "0.3.19";
3954
3969
  };
3955
3970
  if (typeof window !== "undefined") {
3956
3971
  window.__BTC_WALLET_VERSION = getVersion();