@txnlab/use-wallet 3.6.0 → 3.7.0

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.cjs CHANGED
@@ -3409,6 +3409,12 @@ var StorageAdapter = class {
3409
3409
  }
3410
3410
  localStorage.setItem(key, value);
3411
3411
  }
3412
+ static removeItem(key) {
3413
+ if (typeof window === "undefined") {
3414
+ return;
3415
+ }
3416
+ localStorage.removeItem(key);
3417
+ }
3412
3418
  };
3413
3419
 
3414
3420
  // src/store.ts
@@ -3497,6 +3503,24 @@ var BaseWallet = class {
3497
3503
  this.logger.debug(`Removing wallet from store...`);
3498
3504
  removeWallet(this.store, { walletId: this.id });
3499
3505
  };
3506
+ manageWalletConnectSession = (action, targetWalletId) => {
3507
+ const walletId = targetWalletId || this.id;
3508
+ if (action === "backup") {
3509
+ const data = StorageAdapter.getItem("walletconnect");
3510
+ if (data) {
3511
+ StorageAdapter.setItem(`walletconnect-${walletId}`, data);
3512
+ StorageAdapter.removeItem("walletconnect");
3513
+ this.logger.debug(`Backed up WalletConnect session for ${walletId}`);
3514
+ }
3515
+ } else if (action === "restore") {
3516
+ const data = StorageAdapter.getItem(`walletconnect-${walletId}`);
3517
+ if (data) {
3518
+ StorageAdapter.setItem("walletconnect", data);
3519
+ StorageAdapter.removeItem(`walletconnect-${walletId}`);
3520
+ this.logger.debug(`Restored WalletConnect session for ${walletId}`);
3521
+ }
3522
+ }
3523
+ };
3500
3524
  };
3501
3525
 
3502
3526
  // src/wallets/types.ts
@@ -3994,6 +4018,10 @@ var DeflyWallet = class extends BaseWallet {
3994
4018
  }
3995
4019
  connect = async () => {
3996
4020
  this.logger.info("Connecting...");
4021
+ const currentActiveWallet = this.store.state.activeWallet;
4022
+ if (currentActiveWallet && currentActiveWallet !== this.id) {
4023
+ this.manageWalletConnectSession("backup", currentActiveWallet);
4024
+ }
3997
4025
  const client = this.client || await this.initializeClient();
3998
4026
  const accounts = await client.connect();
3999
4027
  client.connector?.on("disconnect", this.onDisconnect);
@@ -4019,10 +4047,28 @@ var DeflyWallet = class extends BaseWallet {
4019
4047
  };
4020
4048
  disconnect = async () => {
4021
4049
  this.logger.info("Disconnecting...");
4022
- this.onDisconnect();
4023
4050
  const client = this.client || await this.initializeClient();
4024
- await client.disconnect();
4025
- this.logger.info("Disconnected.");
4051
+ const currentActiveWallet = this.store.state.activeWallet;
4052
+ if (currentActiveWallet && currentActiveWallet !== this.id) {
4053
+ this.manageWalletConnectSession("backup", currentActiveWallet);
4054
+ this.manageWalletConnectSession("restore", this.id);
4055
+ await client.disconnect();
4056
+ await new Promise((resolve) => setTimeout(resolve, 500));
4057
+ this.manageWalletConnectSession("restore", currentActiveWallet);
4058
+ } else {
4059
+ await client.disconnect();
4060
+ }
4061
+ this.onDisconnect();
4062
+ this.logger.info("Disconnected");
4063
+ };
4064
+ setActive = () => {
4065
+ this.logger.info(`Set active wallet: ${this.id}`);
4066
+ const currentActiveWallet = this.store.state.activeWallet;
4067
+ if (currentActiveWallet && currentActiveWallet !== this.id) {
4068
+ this.manageWalletConnectSession("backup", currentActiveWallet);
4069
+ }
4070
+ this.manageWalletConnectSession("restore");
4071
+ setActiveWallet(this.store, { walletId: this.id });
4026
4072
  };
4027
4073
  resumeSession = async () => {
4028
4074
  try {
@@ -5453,6 +5499,10 @@ var PeraWallet = class extends BaseWallet {
5453
5499
  }
5454
5500
  connect = async () => {
5455
5501
  this.logger.info("Connecting...");
5502
+ const currentActiveWallet = this.store.state.activeWallet;
5503
+ if (currentActiveWallet && currentActiveWallet !== this.id) {
5504
+ this.manageWalletConnectSession("backup", currentActiveWallet);
5505
+ }
5456
5506
  const client = this.client || await this.initializeClient();
5457
5507
  const accounts = await client.connect();
5458
5508
  client.connector?.on("disconnect", this.onDisconnect);
@@ -5478,11 +5528,29 @@ var PeraWallet = class extends BaseWallet {
5478
5528
  };
5479
5529
  disconnect = async () => {
5480
5530
  this.logger.info("Disconnecting...");
5481
- this.onDisconnect();
5482
5531
  const client = this.client || await this.initializeClient();
5483
- await client.disconnect();
5532
+ const currentActiveWallet = this.store.state.activeWallet;
5533
+ if (currentActiveWallet && currentActiveWallet !== this.id) {
5534
+ this.manageWalletConnectSession("backup", currentActiveWallet);
5535
+ this.manageWalletConnectSession("restore", this.id);
5536
+ await client.disconnect();
5537
+ await new Promise((resolve) => setTimeout(resolve, 500));
5538
+ this.manageWalletConnectSession("restore", currentActiveWallet);
5539
+ } else {
5540
+ await client.disconnect();
5541
+ }
5542
+ this.onDisconnect();
5484
5543
  this.logger.info("Disconnected");
5485
5544
  };
5545
+ setActive = () => {
5546
+ this.logger.info(`Set active wallet: ${this.id}`);
5547
+ const currentActiveWallet = this.store.state.activeWallet;
5548
+ if (currentActiveWallet && currentActiveWallet !== this.id) {
5549
+ this.manageWalletConnectSession("backup", currentActiveWallet);
5550
+ }
5551
+ this.manageWalletConnectSession("restore");
5552
+ setActiveWallet(this.store, { walletId: this.id });
5553
+ };
5486
5554
  resumeSession = async () => {
5487
5555
  try {
5488
5556
  const state = this.store.state;