@tronweb3/tronwallet-adapter-tokenpocket 1.0.5 → 1.0.6

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/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@tronweb3/tronwallet-adapter-tokenpocket",
3
- "version": "1.0.5",
4
- "description": "Wallet adapter for TokenPocket Wallet app.",
3
+ "version": "1.0.6",
4
+ "description": "Wallet adapter for TokenPocket Extension and App wallet.",
5
5
  "keywords": [
6
6
  "TRON",
7
7
  "TronWeb",
8
8
  "TokenPocket"
9
9
  ],
10
- "author": "web3-geek",
10
+ "author": "tronweb3",
11
11
  "repository": {
12
12
  "type": "git",
13
- "url": "https://github.com/web3-geek/tronwallet-adapter"
13
+ "url": "https://github.com/tronweb3/tronwallet-adapter"
14
14
  },
15
15
  "license": "MIT",
16
16
  "type": "module",
@@ -36,8 +36,8 @@
36
36
  "access": "public"
37
37
  },
38
38
  "dependencies": {
39
- "@tronweb3/tronwallet-abstract-adapter": "^1.1.8",
40
- "@tronweb3/tronwallet-adapter-tronlink": "^1.1.11"
39
+ "@tronweb3/tronwallet-abstract-adapter": "^1.1.9",
40
+ "@tronweb3/tronwallet-adapter-tronlink": "^1.1.12"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@testing-library/dom": "^8.20.0",
package/src/adapter.ts CHANGED
@@ -8,6 +8,8 @@ import {
8
8
  WalletDisconnectedError,
9
9
  WalletSignTransactionError,
10
10
  WalletGetNetworkError,
11
+ WalletConnectionError,
12
+ isInMobileBrowser,
11
13
  } from '@tronweb3/tronwallet-abstract-adapter';
12
14
  import type {
13
15
  Transaction,
@@ -18,7 +20,11 @@ import type {
18
20
  TronWeb,
19
21
  } from '@tronweb3/tronwallet-abstract-adapter';
20
22
  import { getNetworkInfoByTronWeb } from '@tronweb3/tronwallet-adapter-tronlink';
21
- import type { Tron } from '@tronweb3/tronwallet-adapter-tronlink';
23
+ import type {
24
+ Tron,
25
+ TronAccountsChangedCallback,
26
+ TronChainChangedCallback,
27
+ } from '@tronweb3/tronwallet-adapter-tronlink';
22
28
  import { openTokenPocket, supportTokenPocket } from './utils.js';
23
29
 
24
30
  export interface TokenPocketAdapterConfig extends BaseAdapterConfig {
@@ -140,10 +146,14 @@ export class TokenPocketAdapter extends Adapter {
140
146
  const wallet = this._wallet as TokenPocketWallet;
141
147
 
142
148
  const res = await wallet.tron.request({ method: 'eth_requestAccounts' });
149
+ if (!res?.[0]) {
150
+ throw new WalletConnectionError('Request connect error.');
151
+ }
143
152
  const address = res[0];
144
153
 
145
154
  this.setAddress(address);
146
155
  this.setState(AdapterState.Connected);
156
+ this.listenTronEvent();
147
157
  this.emit('connect', this.address || '');
148
158
  } catch (error: any) {
149
159
  this.emit('error', error);
@@ -220,6 +230,48 @@ export class TokenPocketAdapter extends Adapter {
220
230
  }
221
231
  }
222
232
 
233
+ private onChainChanged: TronChainChangedCallback = (data) => {
234
+ this.emit('chainChanged', data);
235
+ };
236
+ private onAccountsChanged: TronAccountsChangedCallback = () => {
237
+ const preAddr = this.address || '';
238
+ const curAddr = (this._wallet?.tronWeb && this._wallet?.tronWeb.defaultAddress?.base58) || '';
239
+ if (!curAddr) {
240
+ this.setAddress(null);
241
+ this.setState(AdapterState.Disconnect);
242
+ } else {
243
+ const address = curAddr as string;
244
+ this.setAddress(address);
245
+ this.setState(AdapterState.Connected);
246
+ }
247
+ this.emit('accountsChanged', this.address || '', preAddr);
248
+ if (!preAddr && this.address) {
249
+ this.emit('connect', this.address);
250
+ } else if (preAddr && !this.address) {
251
+ this.emit('disconnect');
252
+ }
253
+ };
254
+ private listenTronEvent() {
255
+ if (isInMobileBrowser()) {
256
+ return;
257
+ }
258
+ this.stopListenTronEvent();
259
+ const wallet = this._wallet;
260
+ if (!wallet || !wallet.tron) return;
261
+ wallet.tron.on('chainChanged', this.onChainChanged);
262
+ wallet.tron.on('accountsChanged', this.onAccountsChanged);
263
+ }
264
+
265
+ private stopListenTronEvent() {
266
+ if (isInMobileBrowser()) {
267
+ return;
268
+ }
269
+ const wallet = this._wallet;
270
+ if (!wallet || !wallet.tron) return;
271
+ wallet.tron.removeListener('chainChanged', this.onChainChanged);
272
+ wallet.tron.removeListener('accountsChanged', this.onAccountsChanged);
273
+ }
274
+
223
275
  private async checkAndGetWallet() {
224
276
  this.checkIfOpenApp();
225
277
  await this._checkWallet();
@@ -246,7 +298,7 @@ export class TokenPocketAdapter extends Adapter {
246
298
  let times = 0;
247
299
  const maxTimes = Math.floor(this.config.checkTimeout / 200);
248
300
  const check = () => {
249
- if (window?.tronWeb?.ready) {
301
+ if (isInMobileBrowser() ? window.tronWeb?.ready : this._wallet?.ready) {
250
302
  this.checkReadyInterval && clearInterval(this.checkReadyInterval);
251
303
  this.checkReadyInterval = null;
252
304
  this._updateWallet();
@@ -299,14 +351,22 @@ export class TokenPocketAdapter extends Adapter {
299
351
  let state = this.state;
300
352
  let address = this.address;
301
353
  if (supportTokenPocket()) {
302
- this._wallet = {
303
- tron: window.tokenpocket?.tron,
304
- ready: window.tronWeb?.ready,
305
- tronWeb: window.tokenpocket?.tronWeb,
306
- } as TokenPocketWallet;
354
+ const tron = window.tokenpocket?.tron as Tron;
355
+ this._wallet = isInMobileBrowser()
356
+ ? ({
357
+ tron,
358
+ ready: window.tronWeb?.ready,
359
+ tronWeb: window.tokenpocket?.tronWeb,
360
+ } as TokenPocketWallet)
361
+ : {
362
+ tron,
363
+ ready: !!(tron?.tronWeb as TronWeb).defaultAddress?.base58 || false,
364
+ tronWeb: tron?.tronWeb as TronWeb,
365
+ };
307
366
  address = this._wallet.tronWeb.defaultAddress?.base58 || null;
308
- state = window.tronWeb?.ready ? AdapterState.Connected : AdapterState.Disconnect;
309
- if (!window.tronWeb?.ready) {
367
+ state = address ? AdapterState.Connected : AdapterState.Disconnect;
368
+ this.listenTronEvent();
369
+ if (!address) {
310
370
  this.checkForWalletReady();
311
371
  }
312
372
  } else {