@tronweb3/tronwallet-adapter-tokenpocket 1.0.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/LICENSE +20 -0
- package/README.md +84 -0
- package/lib/cjs/adapter.js +316 -0
- package/lib/cjs/adapter.js.map +1 -0
- package/lib/cjs/index.js +19 -0
- package/lib/cjs/index.js.map +1 -0
- package/lib/cjs/package.json +1 -0
- package/lib/cjs/utils.js +37 -0
- package/lib/cjs/utils.js.map +1 -0
- package/lib/esm/adapter.js +286 -0
- package/lib/esm/adapter.js.map +1 -0
- package/lib/esm/index.js +3 -0
- package/lib/esm/index.js.map +1 -0
- package/lib/esm/utils.js +31 -0
- package/lib/esm/utils.js.map +1 -0
- package/lib/types/adapter.d.ts +55 -0
- package/lib/types/adapter.d.ts.map +1 -0
- package/lib/types/index.d.ts +3 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/utils.d.ts +8 -0
- package/lib/types/utils.d.ts.map +1 -0
- package/package.json +52 -0
- package/src/adapter.ts +321 -0
- package/src/index.ts +2 -0
- package/src/utils.ts +33 -0
package/src/adapter.ts
ADDED
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Adapter,
|
|
3
|
+
AdapterState,
|
|
4
|
+
isInBrowser,
|
|
5
|
+
WalletReadyState,
|
|
6
|
+
WalletSignMessageError,
|
|
7
|
+
WalletNotFoundError,
|
|
8
|
+
WalletDisconnectedError,
|
|
9
|
+
WalletSignTransactionError,
|
|
10
|
+
isInMobileBrowser,
|
|
11
|
+
WalletGetNetworkError,
|
|
12
|
+
} from '@tronweb3/tronwallet-abstract-adapter';
|
|
13
|
+
import type {
|
|
14
|
+
Transaction,
|
|
15
|
+
SignedTransaction,
|
|
16
|
+
AdapterName,
|
|
17
|
+
BaseAdapterConfig,
|
|
18
|
+
Network,
|
|
19
|
+
} from '@tronweb3/tronwallet-abstract-adapter';
|
|
20
|
+
import { getNetworkInfoByTronWeb } from '@tronweb3/tronwallet-adapter-tronlink';
|
|
21
|
+
import type { TronLinkWallet } from '@tronweb3/tronwallet-adapter-tronlink';
|
|
22
|
+
import { openTokenPocket, supportTokenPocket } from './utils.js';
|
|
23
|
+
|
|
24
|
+
export interface TokenPocketAdapterConfig extends BaseAdapterConfig {
|
|
25
|
+
/**
|
|
26
|
+
* Timeout in millisecond for checking if is in TokenPocket App.
|
|
27
|
+
* Default is 2 * 1000ms
|
|
28
|
+
*/
|
|
29
|
+
checkTimeout?: number;
|
|
30
|
+
/**
|
|
31
|
+
* Set if open TokenPocket app using DeepLink.
|
|
32
|
+
* Default is true.
|
|
33
|
+
*/
|
|
34
|
+
openAppWithDeeplink?: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const TokenPocketAdapterName = 'TokenPocket' as AdapterName<'TokenPocket'>;
|
|
38
|
+
|
|
39
|
+
export class TokenPocketAdapter extends Adapter {
|
|
40
|
+
name = TokenPocketAdapterName;
|
|
41
|
+
url = 'https://tokenpocket.pro/';
|
|
42
|
+
icon =
|
|
43
|
+
'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTIwIiBoZWlnaHQ9IjEyMCIgdmlld0JveD0iMCAwIDEwMjQgMTAyNCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPGc+CjxwYXRoIGQ9Ik0xMDQxLjUyIDBILTI3VjEwMjRIMTA0MS41MlYwWiIgZmlsbD0iIzI5ODBGRSIvPgo8ZyBjbGlwLXBhdGg9InVybCgjY2xpcDBfNDA4XzIyNSkiPgo8cGF0aCBkPSJNNDA2Ljc5NiA0MzguNjQzSDQwNi45MjdDNDA2Ljc5NiA0MzcuODU3IDQwNi43OTYgNDM2Ljk0IDQwNi43OTYgNDM2LjE1NFY0MzguNjQzWiIgZmlsbD0iIzI5QUVGRiIvPgo8cGF0aCBkPSJNNjY3LjYwMiA0NjMuNTMzSDUyMy4yNDl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1c2VyU3BhY2VPblVzZSI+CjxzdG9wIHN0b3AtY29sb3I9IndoaXRlIi8+CjxzdG9wIG9mZnNldD0iMC45NjY3IiBzdG9wLWNvbG9yPSJ3aGl0ZSIgc3RvcC1vcGFjaXR5PSIwLjMyMzMiLz4KPHN0b3Agb2Zmc2V0PSIxIiBzdG9wLWNvbG9yPSJ3aGl0ZSIgc3RvcC1vcGFjaXR5PSIwLjMiLz4KPC9saW5lYXJHcmFkaWVudD4KPGNsaXBQYXRoIGlkPSJjbGlwMF80MDhfMjI1Ij4KPHJlY3Qgd2lkdGg9IjcyOC40NDgiIGhlaWdodD0iNDcwIiBmaWxsPSJ3aGl0ZSIgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoMTY4IDI3NykiLz4KPC9jbGlwUGF0aD4KPC9kZWZzPgo8L3N2Zz4K';
|
|
44
|
+
|
|
45
|
+
config: Required<TokenPocketAdapterConfig>;
|
|
46
|
+
private _readyState: WalletReadyState = isInBrowser() ? WalletReadyState.Loading : WalletReadyState.NotFound;
|
|
47
|
+
private _state: AdapterState = AdapterState.Loading;
|
|
48
|
+
private _connecting: boolean;
|
|
49
|
+
private _wallet: TronLinkWallet | null;
|
|
50
|
+
private _address: string | null;
|
|
51
|
+
|
|
52
|
+
constructor(config: TokenPocketAdapterConfig = {}) {
|
|
53
|
+
super();
|
|
54
|
+
const { checkTimeout = 2 * 1000, openUrlWhenWalletNotFound = true, openAppWithDeeplink = true } = config;
|
|
55
|
+
if (typeof checkTimeout !== 'number') {
|
|
56
|
+
throw new Error('[TokenPocketAdapter] config.checkTimeout should be a number');
|
|
57
|
+
}
|
|
58
|
+
this.config = {
|
|
59
|
+
checkTimeout,
|
|
60
|
+
openAppWithDeeplink,
|
|
61
|
+
openUrlWhenWalletNotFound,
|
|
62
|
+
};
|
|
63
|
+
this._connecting = false;
|
|
64
|
+
this._wallet = null;
|
|
65
|
+
this._address = null;
|
|
66
|
+
|
|
67
|
+
if (!isInMobileBrowser()) {
|
|
68
|
+
// Currently TokenPocket extension does not support Tron.
|
|
69
|
+
this._readyState = WalletReadyState.NotFound;
|
|
70
|
+
this._state = AdapterState.NotFound;
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
if (supportTokenPocket()) {
|
|
74
|
+
this._readyState = WalletReadyState.Found;
|
|
75
|
+
this._updateWallet();
|
|
76
|
+
} else {
|
|
77
|
+
this._checkWallet().then(() => {
|
|
78
|
+
if (this.connected) {
|
|
79
|
+
this.emit('connect', this.address || '');
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
get address() {
|
|
86
|
+
return this._address;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
get state() {
|
|
90
|
+
return this._state;
|
|
91
|
+
}
|
|
92
|
+
get readyState() {
|
|
93
|
+
return this._readyState;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
get connecting() {
|
|
97
|
+
return this._connecting;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Get network information.
|
|
102
|
+
* @returns {Network} Current network information.
|
|
103
|
+
*/
|
|
104
|
+
async network(): Promise<Network> {
|
|
105
|
+
try {
|
|
106
|
+
await this._checkWallet();
|
|
107
|
+
if (this.state !== AdapterState.Connected) throw new WalletDisconnectedError();
|
|
108
|
+
const wallet = this._wallet;
|
|
109
|
+
if (!wallet || !wallet.tronWeb) throw new WalletDisconnectedError();
|
|
110
|
+
try {
|
|
111
|
+
return await getNetworkInfoByTronWeb(wallet.tronWeb);
|
|
112
|
+
} catch (e: any) {
|
|
113
|
+
throw new WalletGetNetworkError(e?.message, e);
|
|
114
|
+
}
|
|
115
|
+
} catch (e: any) {
|
|
116
|
+
this.emit('error', e);
|
|
117
|
+
throw e;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async connect(): Promise<void> {
|
|
122
|
+
try {
|
|
123
|
+
this.checkIfOpenApp();
|
|
124
|
+
if (this.connected || this.connecting) return;
|
|
125
|
+
await this._checkWallet();
|
|
126
|
+
if (this.readyState === WalletReadyState.NotFound) {
|
|
127
|
+
if (this.config.openUrlWhenWalletNotFound !== false && isInBrowser()) {
|
|
128
|
+
window.open(this.url, '_blank');
|
|
129
|
+
}
|
|
130
|
+
throw new WalletNotFoundError();
|
|
131
|
+
}
|
|
132
|
+
if (!this._wallet) return;
|
|
133
|
+
this._connecting = true;
|
|
134
|
+
const wallet = this._wallet as TronLinkWallet;
|
|
135
|
+
const address = wallet.tronWeb.defaultAddress?.base58 || '';
|
|
136
|
+
this.setAddress(address);
|
|
137
|
+
this.setState(AdapterState.Connected);
|
|
138
|
+
this.emit('connect', this.address || '');
|
|
139
|
+
} catch (error: any) {
|
|
140
|
+
this.emit('error', error);
|
|
141
|
+
throw error;
|
|
142
|
+
} finally {
|
|
143
|
+
this._connecting = false;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async disconnect(): Promise<void> {
|
|
148
|
+
if (this.state !== AdapterState.Connected) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
this.setAddress(null);
|
|
152
|
+
this.setState(AdapterState.Disconnect);
|
|
153
|
+
this.emit('disconnect');
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async signTransaction(transaction: Transaction, privateKey?: string): Promise<SignedTransaction> {
|
|
157
|
+
try {
|
|
158
|
+
const wallet = await this.checkAndGetWallet();
|
|
159
|
+
try {
|
|
160
|
+
return await wallet.tronWeb.trx.sign(transaction, privateKey);
|
|
161
|
+
} catch (error: any) {
|
|
162
|
+
if (error instanceof Error) {
|
|
163
|
+
throw new WalletSignTransactionError(error.message, error);
|
|
164
|
+
} else {
|
|
165
|
+
throw new WalletSignTransactionError(error, new Error(error));
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
} catch (error: any) {
|
|
169
|
+
this.emit('error', error);
|
|
170
|
+
throw error;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async multiSign(...args: any[]): Promise<SignedTransaction> {
|
|
175
|
+
try {
|
|
176
|
+
const wallet = await this.checkAndGetWallet();
|
|
177
|
+
try {
|
|
178
|
+
return await wallet.tronWeb.trx.multiSign(...args);
|
|
179
|
+
} catch (error: any) {
|
|
180
|
+
if (error instanceof Error) {
|
|
181
|
+
throw new WalletSignTransactionError(error.message, error);
|
|
182
|
+
} else {
|
|
183
|
+
throw new WalletSignTransactionError(error, new Error(error));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
} catch (error: any) {
|
|
187
|
+
this.emit('error', error);
|
|
188
|
+
throw error;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
async signMessage(message: string, privateKey?: string): Promise<string> {
|
|
193
|
+
try {
|
|
194
|
+
const wallet = await this.checkAndGetWallet();
|
|
195
|
+
try {
|
|
196
|
+
return await wallet.tronWeb.trx.signMessageV2(message, privateKey);
|
|
197
|
+
} catch (error: any) {
|
|
198
|
+
if (error instanceof Error) {
|
|
199
|
+
throw new WalletSignMessageError(error.message, error);
|
|
200
|
+
} else {
|
|
201
|
+
throw new WalletSignMessageError(error, new Error(error));
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
} catch (error: any) {
|
|
205
|
+
this.emit('error', error);
|
|
206
|
+
throw error;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
private async checkAndGetWallet() {
|
|
211
|
+
this.checkIfOpenApp();
|
|
212
|
+
await this._checkWallet();
|
|
213
|
+
if (!this.connected) throw new WalletDisconnectedError();
|
|
214
|
+
const wallet = this._wallet;
|
|
215
|
+
if (!wallet || !wallet.tronWeb) throw new WalletDisconnectedError();
|
|
216
|
+
return wallet as TronLinkWallet;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
private checkIfOpenApp() {
|
|
220
|
+
if (this.config.openAppWithDeeplink === false) {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
if (openTokenPocket()) {
|
|
224
|
+
throw new WalletNotFoundError();
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
private checkReadyInterval: ReturnType<typeof setInterval> | null = null;
|
|
229
|
+
private checkForWalletReady() {
|
|
230
|
+
if (this.checkReadyInterval) {
|
|
231
|
+
return;
|
|
232
|
+
}
|
|
233
|
+
let times = 0;
|
|
234
|
+
const maxTimes = Math.floor(this.config.checkTimeout / 200);
|
|
235
|
+
const check = () => {
|
|
236
|
+
if (window?.tronWeb?.ready) {
|
|
237
|
+
this.checkReadyInterval && clearInterval(this.checkReadyInterval);
|
|
238
|
+
this.checkReadyInterval = null;
|
|
239
|
+
this._updateWallet();
|
|
240
|
+
this.emit('connect', this.address || '');
|
|
241
|
+
} else if (times > maxTimes) {
|
|
242
|
+
this.checkReadyInterval && clearInterval(this.checkReadyInterval);
|
|
243
|
+
this.checkReadyInterval = null;
|
|
244
|
+
} else {
|
|
245
|
+
times++;
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
this.checkReadyInterval = setInterval(check, 200);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private _checkPromise: Promise<boolean> | null = null;
|
|
252
|
+
/**
|
|
253
|
+
* check if wallet exists by interval, the promise only resolve when wallet detected or timeout
|
|
254
|
+
* @returns if wallet exists
|
|
255
|
+
*/
|
|
256
|
+
private _checkWallet(): Promise<boolean> {
|
|
257
|
+
if (this.readyState === WalletReadyState.Found) {
|
|
258
|
+
return Promise.resolve(true);
|
|
259
|
+
}
|
|
260
|
+
if (this._checkPromise) {
|
|
261
|
+
return this._checkPromise;
|
|
262
|
+
}
|
|
263
|
+
const interval = 100;
|
|
264
|
+
const maxTimes = Math.floor(this.config.checkTimeout / interval);
|
|
265
|
+
let times = 0,
|
|
266
|
+
timer: ReturnType<typeof setInterval>;
|
|
267
|
+
this._checkPromise = new Promise((resolve) => {
|
|
268
|
+
const check = () => {
|
|
269
|
+
times++;
|
|
270
|
+
const isSupport = supportTokenPocket();
|
|
271
|
+
if (isSupport || times > maxTimes) {
|
|
272
|
+
timer && clearInterval(timer);
|
|
273
|
+
this._readyState = isSupport ? WalletReadyState.Found : WalletReadyState.NotFound;
|
|
274
|
+
this._updateWallet();
|
|
275
|
+
this.emit('readyStateChanged', this.readyState);
|
|
276
|
+
resolve(isSupport);
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
timer = setInterval(check, interval);
|
|
280
|
+
check();
|
|
281
|
+
});
|
|
282
|
+
return this._checkPromise;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
private _updateWallet = () => {
|
|
286
|
+
let state = this.state;
|
|
287
|
+
let address = this.address;
|
|
288
|
+
if (supportTokenPocket()) {
|
|
289
|
+
// fake tronLink
|
|
290
|
+
this._wallet = {
|
|
291
|
+
ready: window.tronWeb?.ready,
|
|
292
|
+
tronWeb: window.tronWeb,
|
|
293
|
+
request: () => Promise.resolve(true) as any,
|
|
294
|
+
} as TronLinkWallet;
|
|
295
|
+
address = this._wallet.tronWeb.defaultAddress?.base58 || null;
|
|
296
|
+
state = window.tronWeb?.ready ? AdapterState.Connected : AdapterState.Disconnect;
|
|
297
|
+
if (!window.tronWeb?.ready) {
|
|
298
|
+
this.checkForWalletReady();
|
|
299
|
+
}
|
|
300
|
+
} else {
|
|
301
|
+
// no tronlink support
|
|
302
|
+
this._wallet = null;
|
|
303
|
+
address = null;
|
|
304
|
+
state = AdapterState.NotFound;
|
|
305
|
+
}
|
|
306
|
+
this.setAddress(address);
|
|
307
|
+
this.setState(state);
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
private setAddress(address: string | null) {
|
|
311
|
+
this._address = address;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
private setState(state: AdapterState) {
|
|
315
|
+
const preState = this.state;
|
|
316
|
+
if (state !== preState) {
|
|
317
|
+
this._state = state;
|
|
318
|
+
this.emit('stateChanged', state);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
package/src/index.ts
ADDED
package/src/utils.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { isInBrowser, isInMobileBrowser } from '@tronweb3/tronwallet-abstract-adapter';
|
|
2
|
+
|
|
3
|
+
export function supportTokenPocket() {
|
|
4
|
+
return !!window.tronWeb && typeof (window as any).tokenpocket !== 'undefined';
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Detect if in TokenPocketApp
|
|
9
|
+
* There will be a `tokenpocket` object on window
|
|
10
|
+
*/
|
|
11
|
+
export function isInTokenPocket() {
|
|
12
|
+
return isInBrowser() && typeof (window as any).tokenpocket !== 'undefined';
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function openTokenPocket() {
|
|
16
|
+
if (!supportTokenPocket() && isInMobileBrowser() && !isInTokenPocket()) {
|
|
17
|
+
const { origin, pathname, search, hash } = window.location;
|
|
18
|
+
const url = origin + pathname + search + hash;
|
|
19
|
+
const params = {
|
|
20
|
+
action: 'open',
|
|
21
|
+
actionId: Date.now() + '',
|
|
22
|
+
callbackUrl: 'http://someurl.com', // no need callback
|
|
23
|
+
blockchain: 'Tron',
|
|
24
|
+
chain: 'Tron',
|
|
25
|
+
url,
|
|
26
|
+
protocol: 'TokenPocket',
|
|
27
|
+
version: '1.0',
|
|
28
|
+
};
|
|
29
|
+
window.location.href = `tpdapp://open?params=${encodeURIComponent(JSON.stringify(params))}`;
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
return false;
|
|
33
|
+
}
|