@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/LICENSE +1 -1
- package/README.md +3 -3
- package/lib/cjs/adapter.js +68 -11
- package/lib/cjs/adapter.js.map +1 -1
- package/lib/esm/adapter.js +69 -12
- package/lib/esm/adapter.js.map +1 -1
- package/lib/types/adapter.d.ts +4 -0
- package/lib/types/adapter.d.ts.map +1 -1
- package/lib/umd/index.js +264 -117788
- package/lib/umd/index.min.js +1 -33
- package/package.json +6 -6
- package/src/adapter.ts +69 -9
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tronweb3/tronwallet-adapter-tokenpocket",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "Wallet adapter for TokenPocket
|
|
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": "
|
|
10
|
+
"author": "tronweb3",
|
|
11
11
|
"repository": {
|
|
12
12
|
"type": "git",
|
|
13
|
-
"url": "https://github.com/
|
|
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.
|
|
40
|
-
"@tronweb3/tronwallet-adapter-tronlink": "^1.1.
|
|
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 {
|
|
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
|
|
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
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
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 =
|
|
309
|
-
|
|
367
|
+
state = address ? AdapterState.Connected : AdapterState.Disconnect;
|
|
368
|
+
this.listenTronEvent();
|
|
369
|
+
if (!address) {
|
|
310
370
|
this.checkForWalletReady();
|
|
311
371
|
}
|
|
312
372
|
} else {
|