@enclave-hq/wallet-sdk 1.2.3 → 1.2.4
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/README.md +51 -1
- package/dist/index.d.mts +413 -10
- package/dist/index.d.ts +413 -10
- package/dist/index.js +3492 -107
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3454 -76
- package/dist/index.mjs.map +1 -1
- package/dist/react/index.d.mts +88 -9
- package/dist/react/index.d.ts +88 -9
- package/dist/react/index.js +3341 -79
- package/dist/react/index.js.map +1 -1
- package/dist/react/index.mjs +3315 -57
- package/dist/react/index.mjs.map +1 -1
- package/package.json +7 -2
package/dist/index.mjs
CHANGED
|
@@ -1,9 +1,597 @@
|
|
|
1
|
-
import EventEmitter from 'eventemitter3';
|
|
2
1
|
import { ChainType as ChainType$1 } from '@enclave-hq/chain-utils';
|
|
3
|
-
import
|
|
2
|
+
import EventEmitter from 'eventemitter3';
|
|
3
|
+
import { createWalletClient, custom, createPublicClient, http, isAddress, getAddress, verifyMessage } from 'viem';
|
|
4
4
|
import { privateKeyToAccount } from 'viem/accounts';
|
|
5
|
+
import EthereumProvider from '@walletconnect/ethereum-provider';
|
|
6
|
+
import { WalletConnectChainID, WalletConnectWallet } from '@tronweb3/walletconnect-tron';
|
|
7
|
+
import QRCode from 'qrcode';
|
|
8
|
+
|
|
9
|
+
var __defProp = Object.defineProperty;
|
|
10
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
11
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
12
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
13
|
+
var __esm = (fn, res) => function __init() {
|
|
14
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
15
|
+
};
|
|
16
|
+
var __export = (target, all) => {
|
|
17
|
+
for (var name in all)
|
|
18
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
19
|
+
};
|
|
20
|
+
var __copyProps = (to, from, except, desc) => {
|
|
21
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
22
|
+
for (let key of __getOwnPropNames(from))
|
|
23
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
24
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
25
|
+
}
|
|
26
|
+
return to;
|
|
27
|
+
};
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var ChainType, WalletType, WalletState;
|
|
30
|
+
var init_types = __esm({
|
|
31
|
+
"src/core/types.ts"() {
|
|
32
|
+
ChainType = ChainType$1;
|
|
33
|
+
WalletType = /* @__PURE__ */ ((WalletType3) => {
|
|
34
|
+
WalletType3["METAMASK"] = "metamask";
|
|
35
|
+
WalletType3["WALLETCONNECT"] = "walletconnect";
|
|
36
|
+
WalletType3["COINBASE_WALLET"] = "coinbase-wallet";
|
|
37
|
+
WalletType3["TRONLINK"] = "tronlink";
|
|
38
|
+
WalletType3["WALLETCONNECT_TRON"] = "walletconnect-tron";
|
|
39
|
+
WalletType3["PRIVATE_KEY"] = "private-key";
|
|
40
|
+
WalletType3["DEEP_LINK_EVM"] = "deep-link-evm";
|
|
41
|
+
WalletType3["DEEP_LINK_TRON"] = "deep-link-tron";
|
|
42
|
+
return WalletType3;
|
|
43
|
+
})(WalletType || {});
|
|
44
|
+
WalletState = /* @__PURE__ */ ((WalletState2) => {
|
|
45
|
+
WalletState2["DISCONNECTED"] = "disconnected";
|
|
46
|
+
WalletState2["CONNECTING"] = "connecting";
|
|
47
|
+
WalletState2["CONNECTED"] = "connected";
|
|
48
|
+
WalletState2["ERROR"] = "error";
|
|
49
|
+
return WalletState2;
|
|
50
|
+
})(WalletState || {});
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// src/adapters/deep-link/providers/tokenpocket.ts
|
|
55
|
+
var tokenpocket_exports = {};
|
|
56
|
+
__export(tokenpocket_exports, {
|
|
57
|
+
TokenPocketDeepLinkProvider: () => TokenPocketDeepLinkProvider
|
|
58
|
+
});
|
|
59
|
+
var TokenPocketDeepLinkProvider;
|
|
60
|
+
var init_tokenpocket = __esm({
|
|
61
|
+
"src/adapters/deep-link/providers/tokenpocket.ts"() {
|
|
62
|
+
init_types();
|
|
63
|
+
TokenPocketDeepLinkProvider = class {
|
|
64
|
+
constructor(options) {
|
|
65
|
+
this.name = "TokenPocket";
|
|
66
|
+
this.icon = "https://tokenpocket.pro/icon.png";
|
|
67
|
+
this.supportedChainTypes = [ChainType.EVM, ChainType.TRON];
|
|
68
|
+
this.callbackUrl = options?.callbackUrl;
|
|
69
|
+
this.callbackSchema = options?.callbackSchema;
|
|
70
|
+
}
|
|
71
|
+
async isAvailable() {
|
|
72
|
+
if (typeof window === "undefined") {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
const isTelegramMiniApp = !!(window.Telegram && window.Telegram.WebApp);
|
|
76
|
+
if (isTelegramMiniApp) {
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
const isMobile = /Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
|
80
|
+
navigator.userAgent
|
|
81
|
+
);
|
|
82
|
+
return isMobile;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Generate unique actionId
|
|
86
|
+
*/
|
|
87
|
+
generateActionId() {
|
|
88
|
+
return `web-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Get callback configuration
|
|
92
|
+
*/
|
|
93
|
+
getCallbackConfig() {
|
|
94
|
+
if (this.callbackSchema) {
|
|
95
|
+
return { callbackSchema: this.callbackSchema };
|
|
96
|
+
}
|
|
97
|
+
if (this.callbackUrl) {
|
|
98
|
+
return { callbackUrl: this.callbackUrl };
|
|
99
|
+
}
|
|
100
|
+
if (typeof window !== "undefined" && window.location) {
|
|
101
|
+
return {
|
|
102
|
+
callbackSchema: `${window.location.protocol}//${window.location.host}${window.location.pathname}`
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
return {};
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Get blockchain configuration based on chain type
|
|
109
|
+
*/
|
|
110
|
+
getBlockchainConfig(chainId, chainType) {
|
|
111
|
+
if (chainType === ChainType.TRON) {
|
|
112
|
+
return {
|
|
113
|
+
chainId: String(chainId),
|
|
114
|
+
network: "tron"
|
|
115
|
+
};
|
|
116
|
+
} else if (chainType === ChainType.EVM) {
|
|
117
|
+
return {
|
|
118
|
+
chainId: String(chainId),
|
|
119
|
+
network: "ethereum"
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
throw new Error(`Unsupported chain type: ${chainType}`);
|
|
123
|
+
}
|
|
124
|
+
buildSignMessageLink(params) {
|
|
125
|
+
const actionId = this.generateActionId();
|
|
126
|
+
const callback = this.getCallbackConfig();
|
|
127
|
+
const blockchain = this.getBlockchainConfig(params.chainId, params.chainType);
|
|
128
|
+
const param = {
|
|
129
|
+
action: "sign",
|
|
130
|
+
actionId,
|
|
131
|
+
message: params.message,
|
|
132
|
+
hash: false,
|
|
133
|
+
signType: params.chainType === ChainType.TRON ? "ethPersonalSign" : "ethPersonalSign",
|
|
134
|
+
memo: `${params.chainType} message signature`,
|
|
135
|
+
blockchains: [blockchain],
|
|
136
|
+
dappName: "Enclave Wallet SDK",
|
|
137
|
+
dappIcon: "https://walletconnect.com/walletconnect-logo.svg",
|
|
138
|
+
protocol: "TokenPocket",
|
|
139
|
+
version: "1.1.8",
|
|
140
|
+
expired: 0,
|
|
141
|
+
...callback
|
|
142
|
+
};
|
|
143
|
+
const encodedParam = encodeURIComponent(JSON.stringify(param));
|
|
144
|
+
const url = `tpoutside://pull.activity?param=${encodedParam}`;
|
|
145
|
+
return {
|
|
146
|
+
url,
|
|
147
|
+
actionId,
|
|
148
|
+
...callback
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
buildSignTransactionLink(params) {
|
|
152
|
+
const actionId = this.generateActionId();
|
|
153
|
+
const callback = this.getCallbackConfig();
|
|
154
|
+
const blockchain = this.getBlockchainConfig(params.chainId, params.chainType);
|
|
155
|
+
let transactionData;
|
|
156
|
+
if (typeof params.transaction === "string") {
|
|
157
|
+
transactionData = params.transaction;
|
|
158
|
+
} else {
|
|
159
|
+
transactionData = JSON.stringify(params.transaction);
|
|
160
|
+
}
|
|
161
|
+
const param = {
|
|
162
|
+
action: "pushTransaction",
|
|
163
|
+
actionId,
|
|
164
|
+
txData: transactionData,
|
|
165
|
+
blockchains: [blockchain],
|
|
166
|
+
dappName: "Enclave Wallet SDK",
|
|
167
|
+
dappIcon: "https://walletconnect.com/walletconnect-logo.svg",
|
|
168
|
+
protocol: "TokenPocket",
|
|
169
|
+
version: "1.1.8",
|
|
170
|
+
expired: 0,
|
|
171
|
+
...callback
|
|
172
|
+
};
|
|
173
|
+
const encodedParam = encodeURIComponent(JSON.stringify(param));
|
|
174
|
+
const url = `tpoutside://pull.activity?param=${encodedParam}`;
|
|
175
|
+
return {
|
|
176
|
+
url,
|
|
177
|
+
actionId,
|
|
178
|
+
...callback
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
buildConnectLink(params) {
|
|
182
|
+
const actionId = this.generateActionId();
|
|
183
|
+
const blockchain = this.getBlockchainConfig(params.chainId, params.chainType);
|
|
184
|
+
const param = {
|
|
185
|
+
action: "login",
|
|
186
|
+
actionId,
|
|
187
|
+
blockchains: [blockchain],
|
|
188
|
+
dappName: "Enclave Wallet SDK",
|
|
189
|
+
dappIcon: "https://walletconnect.com/walletconnect-logo.svg",
|
|
190
|
+
protocol: "TokenPocket",
|
|
191
|
+
version: "1.0",
|
|
192
|
+
expired: 1602
|
|
193
|
+
// 30 minutes
|
|
194
|
+
};
|
|
195
|
+
const encodedParam = encodeURIComponent(JSON.stringify(param));
|
|
196
|
+
const url = `tpoutside://pull.activity?param=${encodedParam}`;
|
|
197
|
+
return {
|
|
198
|
+
url,
|
|
199
|
+
actionId
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
parseCallbackResult(urlParams) {
|
|
203
|
+
const actionId = urlParams.get("actionId");
|
|
204
|
+
const resultParam = urlParams.get("result");
|
|
205
|
+
const error = urlParams.get("error");
|
|
206
|
+
let result = null;
|
|
207
|
+
if (resultParam) {
|
|
208
|
+
try {
|
|
209
|
+
result = JSON.parse(decodeURIComponent(resultParam));
|
|
210
|
+
} catch (e) {
|
|
211
|
+
result = resultParam;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return {
|
|
215
|
+
actionId,
|
|
216
|
+
result,
|
|
217
|
+
error
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
getDefaultCallbackSchema() {
|
|
221
|
+
if (typeof window !== "undefined" && window.location) {
|
|
222
|
+
return `${window.location.protocol}//${window.location.host}${window.location.pathname}`;
|
|
223
|
+
}
|
|
224
|
+
return "";
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
// src/adapters/deep-link/providers/tronlink.ts
|
|
231
|
+
var tronlink_exports = {};
|
|
232
|
+
__export(tronlink_exports, {
|
|
233
|
+
TronLinkDeepLinkProvider: () => TronLinkDeepLinkProvider
|
|
234
|
+
});
|
|
235
|
+
var TronLinkDeepLinkProvider;
|
|
236
|
+
var init_tronlink = __esm({
|
|
237
|
+
"src/adapters/deep-link/providers/tronlink.ts"() {
|
|
238
|
+
init_types();
|
|
239
|
+
TronLinkDeepLinkProvider = class {
|
|
240
|
+
constructor() {
|
|
241
|
+
this.name = "TronLink";
|
|
242
|
+
this.icon = "https://www.tronlink.org/static/logoIcon.svg";
|
|
243
|
+
this.supportedChainTypes = [ChainType.TRON];
|
|
244
|
+
}
|
|
245
|
+
async isAvailable() {
|
|
246
|
+
if (typeof window === "undefined") {
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
const isTelegramMiniApp = !!(window.Telegram && window.Telegram.WebApp);
|
|
250
|
+
if (isTelegramMiniApp) {
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
const isMobile = /Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
|
254
|
+
navigator.userAgent
|
|
255
|
+
);
|
|
256
|
+
return isMobile;
|
|
257
|
+
}
|
|
258
|
+
buildSignMessageLink(params) {
|
|
259
|
+
if (params.chainType !== ChainType.TRON) {
|
|
260
|
+
throw new Error("TronLink only supports TRON chain");
|
|
261
|
+
}
|
|
262
|
+
const encodedMessage = encodeURIComponent(params.message);
|
|
263
|
+
const url = `tronlink://signMessage?message=${encodedMessage}`;
|
|
264
|
+
const actionId = `tronlink-${Date.now()}`;
|
|
265
|
+
return {
|
|
266
|
+
url,
|
|
267
|
+
actionId
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
buildSignTransactionLink(params) {
|
|
271
|
+
if (params.chainType !== ChainType.TRON) {
|
|
272
|
+
throw new Error("TronLink only supports TRON chain");
|
|
273
|
+
}
|
|
274
|
+
const transactionData = typeof params.transaction === "string" ? params.transaction : JSON.stringify(params.transaction);
|
|
275
|
+
const encodedData = encodeURIComponent(transactionData);
|
|
276
|
+
const url = `tronlink://signTransaction?transaction=${encodedData}`;
|
|
277
|
+
const actionId = `tronlink-${Date.now()}`;
|
|
278
|
+
return {
|
|
279
|
+
url,
|
|
280
|
+
actionId
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
buildConnectLink(params) {
|
|
284
|
+
if (params.chainType !== ChainType.TRON) {
|
|
285
|
+
throw new Error("TronLink only supports TRON chain");
|
|
286
|
+
}
|
|
287
|
+
const url = `tronlink://open?action=connect&network=tron`;
|
|
288
|
+
return {
|
|
289
|
+
url
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
parseCallbackResult(_urlParams) {
|
|
293
|
+
return {
|
|
294
|
+
actionId: null,
|
|
295
|
+
result: null,
|
|
296
|
+
error: null
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
// src/adapters/deep-link/providers/imtoken.ts
|
|
304
|
+
var imtoken_exports = {};
|
|
305
|
+
__export(imtoken_exports, {
|
|
306
|
+
ImTokenDeepLinkProvider: () => ImTokenDeepLinkProvider
|
|
307
|
+
});
|
|
308
|
+
var ImTokenDeepLinkProvider;
|
|
309
|
+
var init_imtoken = __esm({
|
|
310
|
+
"src/adapters/deep-link/providers/imtoken.ts"() {
|
|
311
|
+
init_types();
|
|
312
|
+
ImTokenDeepLinkProvider = class {
|
|
313
|
+
constructor(options) {
|
|
314
|
+
this.name = "ImToken";
|
|
315
|
+
this.icon = "https://token.im/static/img/logo.png";
|
|
316
|
+
this.supportedChainTypes = [ChainType.EVM, ChainType.TRON];
|
|
317
|
+
this.callbackUrl = options?.callbackUrl;
|
|
318
|
+
this.callbackSchema = options?.callbackSchema;
|
|
319
|
+
}
|
|
320
|
+
async isAvailable() {
|
|
321
|
+
if (typeof window === "undefined") {
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
const isTelegramMiniApp = !!(window.Telegram && window.Telegram.WebApp);
|
|
325
|
+
if (isTelegramMiniApp) {
|
|
326
|
+
return true;
|
|
327
|
+
}
|
|
328
|
+
const isMobile = /Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
|
329
|
+
navigator.userAgent
|
|
330
|
+
);
|
|
331
|
+
return isMobile;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Generate unique actionId
|
|
335
|
+
*/
|
|
336
|
+
generateActionId() {
|
|
337
|
+
return `imtoken-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Get callback configuration
|
|
341
|
+
*/
|
|
342
|
+
getCallbackConfig() {
|
|
343
|
+
if (this.callbackSchema) {
|
|
344
|
+
return { callbackSchema: this.callbackSchema };
|
|
345
|
+
}
|
|
346
|
+
if (this.callbackUrl) {
|
|
347
|
+
return { callbackUrl: this.callbackUrl };
|
|
348
|
+
}
|
|
349
|
+
if (typeof window !== "undefined" && window.location) {
|
|
350
|
+
return {
|
|
351
|
+
callbackSchema: `${window.location.protocol}//${window.location.host}${window.location.pathname}`
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
return {};
|
|
355
|
+
}
|
|
356
|
+
buildSignMessageLink(params) {
|
|
357
|
+
const actionId = this.generateActionId();
|
|
358
|
+
const callback = this.getCallbackConfig();
|
|
359
|
+
const dappUrl = `${window.location.origin}${window.location.pathname}?action=signMessage&message=${encodeURIComponent(params.message)}&chainId=${params.chainId}`;
|
|
360
|
+
const encodedDappUrl = encodeURIComponent(dappUrl);
|
|
361
|
+
const url = `imtokenv2://navigate/DappView?url=${encodedDappUrl}`;
|
|
362
|
+
return {
|
|
363
|
+
url,
|
|
364
|
+
actionId,
|
|
365
|
+
...callback
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
buildSignTransactionLink(params) {
|
|
369
|
+
const actionId = this.generateActionId();
|
|
370
|
+
const callback = this.getCallbackConfig();
|
|
371
|
+
const transactionData = typeof params.transaction === "string" ? params.transaction : JSON.stringify(params.transaction);
|
|
372
|
+
const dappUrl = `${window.location.origin}${window.location.pathname}?action=signTransaction&transaction=${encodeURIComponent(transactionData)}&chainId=${params.chainId}`;
|
|
373
|
+
const encodedDappUrl = encodeURIComponent(dappUrl);
|
|
374
|
+
const url = `imtokenv2://navigate/DappView?url=${encodedDappUrl}`;
|
|
375
|
+
return {
|
|
376
|
+
url,
|
|
377
|
+
actionId,
|
|
378
|
+
...callback
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
buildConnectLink(params) {
|
|
382
|
+
const dappUrl = `${window.location.origin}${window.location.pathname}?action=connect&chainId=${params.chainId}`;
|
|
383
|
+
const encodedDappUrl = encodeURIComponent(dappUrl);
|
|
384
|
+
const url = `imtokenv2://navigate/DappView?url=${encodedDappUrl}`;
|
|
385
|
+
return {
|
|
386
|
+
url
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
parseCallbackResult(urlParams) {
|
|
390
|
+
const actionId = urlParams.get("actionId");
|
|
391
|
+
const resultParam = urlParams.get("result");
|
|
392
|
+
const error = urlParams.get("error");
|
|
393
|
+
let result = null;
|
|
394
|
+
if (resultParam) {
|
|
395
|
+
try {
|
|
396
|
+
result = JSON.parse(decodeURIComponent(resultParam));
|
|
397
|
+
} catch (e) {
|
|
398
|
+
result = resultParam;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
return {
|
|
402
|
+
actionId,
|
|
403
|
+
result,
|
|
404
|
+
error
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
getDefaultCallbackSchema() {
|
|
408
|
+
if (typeof window !== "undefined" && window.location) {
|
|
409
|
+
return `${window.location.protocol}//${window.location.host}${window.location.pathname}`;
|
|
410
|
+
}
|
|
411
|
+
return "";
|
|
412
|
+
}
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
});
|
|
416
|
+
|
|
417
|
+
// src/adapters/deep-link/providers/metamask.ts
|
|
418
|
+
var metamask_exports = {};
|
|
419
|
+
__export(metamask_exports, {
|
|
420
|
+
MetaMaskDeepLinkProvider: () => MetaMaskDeepLinkProvider
|
|
421
|
+
});
|
|
422
|
+
var MetaMaskDeepLinkProvider;
|
|
423
|
+
var init_metamask = __esm({
|
|
424
|
+
"src/adapters/deep-link/providers/metamask.ts"() {
|
|
425
|
+
init_types();
|
|
426
|
+
MetaMaskDeepLinkProvider = class {
|
|
427
|
+
constructor() {
|
|
428
|
+
this.name = "MetaMask";
|
|
429
|
+
this.icon = "https://upload.wikimedia.org/wikipedia/commons/3/36/MetaMask_Fox.svg";
|
|
430
|
+
this.supportedChainTypes = [ChainType.EVM];
|
|
431
|
+
}
|
|
432
|
+
async isAvailable() {
|
|
433
|
+
if (typeof window === "undefined") {
|
|
434
|
+
return false;
|
|
435
|
+
}
|
|
436
|
+
const isTelegramMiniApp = !!(window.Telegram && window.Telegram.WebApp);
|
|
437
|
+
if (isTelegramMiniApp) {
|
|
438
|
+
return true;
|
|
439
|
+
}
|
|
440
|
+
const isMobile = /Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
|
441
|
+
navigator.userAgent
|
|
442
|
+
);
|
|
443
|
+
return isMobile;
|
|
444
|
+
}
|
|
445
|
+
buildSignMessageLink(params) {
|
|
446
|
+
if (params.chainType !== ChainType.EVM) {
|
|
447
|
+
throw new Error("MetaMask only supports EVM chains");
|
|
448
|
+
}
|
|
449
|
+
const dappUrl = `${window.location.origin}${window.location.pathname}?action=signMessage&message=${encodeURIComponent(params.message)}&chainId=${params.chainId}`;
|
|
450
|
+
const encodedDappUrl = encodeURIComponent(dappUrl);
|
|
451
|
+
const url = `https://link.metamask.io/dapp/${encodedDappUrl}`;
|
|
452
|
+
const actionId = `metamask-${Date.now()}`;
|
|
453
|
+
return {
|
|
454
|
+
url,
|
|
455
|
+
actionId
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
buildSignTransactionLink(params) {
|
|
459
|
+
if (params.chainType !== ChainType.EVM) {
|
|
460
|
+
throw new Error("MetaMask only supports EVM chains");
|
|
461
|
+
}
|
|
462
|
+
const transactionData = typeof params.transaction === "string" ? params.transaction : JSON.stringify(params.transaction);
|
|
463
|
+
const dappUrl = `${window.location.origin}${window.location.pathname}?action=signTransaction&transaction=${encodeURIComponent(transactionData)}&chainId=${params.chainId}`;
|
|
464
|
+
const encodedDappUrl = encodeURIComponent(dappUrl);
|
|
465
|
+
const url = `https://link.metamask.io/dapp/${encodedDappUrl}`;
|
|
466
|
+
const actionId = `metamask-${Date.now()}`;
|
|
467
|
+
return {
|
|
468
|
+
url,
|
|
469
|
+
actionId
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
buildConnectLink(params) {
|
|
473
|
+
if (params.chainType !== ChainType.EVM) {
|
|
474
|
+
throw new Error("MetaMask only supports EVM chains");
|
|
475
|
+
}
|
|
476
|
+
const dappUrl = `${window.location.origin}${window.location.pathname}?action=connect&chainId=${params.chainId}`;
|
|
477
|
+
const encodedDappUrl = encodeURIComponent(dappUrl);
|
|
478
|
+
const url = `https://link.metamask.io/dapp/${encodedDappUrl}`;
|
|
479
|
+
return {
|
|
480
|
+
url
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
parseCallbackResult(urlParams) {
|
|
484
|
+
const actionId = urlParams.get("actionId");
|
|
485
|
+
const resultParam = urlParams.get("result");
|
|
486
|
+
const error = urlParams.get("error");
|
|
487
|
+
let result = null;
|
|
488
|
+
if (resultParam) {
|
|
489
|
+
try {
|
|
490
|
+
result = JSON.parse(decodeURIComponent(resultParam));
|
|
491
|
+
} catch (e) {
|
|
492
|
+
result = resultParam;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
return {
|
|
496
|
+
actionId,
|
|
497
|
+
result,
|
|
498
|
+
error
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
getDefaultCallbackSchema() {
|
|
502
|
+
if (typeof window !== "undefined" && window.location) {
|
|
503
|
+
return `${window.location.protocol}//${window.location.host}${window.location.pathname}`;
|
|
504
|
+
}
|
|
505
|
+
return "";
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
});
|
|
5
510
|
|
|
6
|
-
// src/
|
|
511
|
+
// src/adapters/deep-link/providers/okx.ts
|
|
512
|
+
var okx_exports = {};
|
|
513
|
+
__export(okx_exports, {
|
|
514
|
+
OKXDeepLinkProvider: () => OKXDeepLinkProvider
|
|
515
|
+
});
|
|
516
|
+
var OKXDeepLinkProvider;
|
|
517
|
+
var init_okx = __esm({
|
|
518
|
+
"src/adapters/deep-link/providers/okx.ts"() {
|
|
519
|
+
init_types();
|
|
520
|
+
OKXDeepLinkProvider = class {
|
|
521
|
+
constructor() {
|
|
522
|
+
this.name = "OKX";
|
|
523
|
+
this.icon = "https://www.okx.com/favicon.ico";
|
|
524
|
+
this.supportedChainTypes = [ChainType.EVM, ChainType.TRON];
|
|
525
|
+
}
|
|
526
|
+
async isAvailable() {
|
|
527
|
+
if (typeof window === "undefined") {
|
|
528
|
+
return false;
|
|
529
|
+
}
|
|
530
|
+
const isTelegramMiniApp = !!(window.Telegram && window.Telegram.WebApp);
|
|
531
|
+
if (isTelegramMiniApp) {
|
|
532
|
+
return true;
|
|
533
|
+
}
|
|
534
|
+
const isMobile = /Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
|
535
|
+
navigator.userAgent
|
|
536
|
+
);
|
|
537
|
+
return isMobile;
|
|
538
|
+
}
|
|
539
|
+
buildSignMessageLink(params) {
|
|
540
|
+
const dappUrl = `${window.location.origin}${window.location.pathname}?action=signMessage&message=${encodeURIComponent(params.message)}&chainId=${params.chainId}`;
|
|
541
|
+
const encodedDappUrl = encodeURIComponent(dappUrl);
|
|
542
|
+
const url = `okx://wallet/dapp/url?dappUrl=${encodedDappUrl}`;
|
|
543
|
+
const actionId = `okx-${Date.now()}`;
|
|
544
|
+
return {
|
|
545
|
+
url,
|
|
546
|
+
actionId
|
|
547
|
+
};
|
|
548
|
+
}
|
|
549
|
+
buildSignTransactionLink(params) {
|
|
550
|
+
const transactionData = typeof params.transaction === "string" ? params.transaction : JSON.stringify(params.transaction);
|
|
551
|
+
const dappUrl = `${window.location.origin}${window.location.pathname}?action=signTransaction&transaction=${encodeURIComponent(transactionData)}&chainId=${params.chainId}`;
|
|
552
|
+
const encodedDappUrl = encodeURIComponent(dappUrl);
|
|
553
|
+
const url = `okx://wallet/dapp/url?dappUrl=${encodedDappUrl}`;
|
|
554
|
+
const actionId = `okx-${Date.now()}`;
|
|
555
|
+
return {
|
|
556
|
+
url,
|
|
557
|
+
actionId
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
buildConnectLink(params) {
|
|
561
|
+
const dappUrl = `${window.location.origin}${window.location.pathname}?action=connect&chainId=${params.chainId}`;
|
|
562
|
+
const encodedDappUrl = encodeURIComponent(dappUrl);
|
|
563
|
+
const url = `okx://wallet/dapp/url?dappUrl=${encodedDappUrl}`;
|
|
564
|
+
return {
|
|
565
|
+
url
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
parseCallbackResult(urlParams) {
|
|
569
|
+
const actionId = urlParams.get("actionId");
|
|
570
|
+
const resultParam = urlParams.get("result");
|
|
571
|
+
const error = urlParams.get("error");
|
|
572
|
+
let result = null;
|
|
573
|
+
if (resultParam) {
|
|
574
|
+
try {
|
|
575
|
+
result = JSON.parse(decodeURIComponent(resultParam));
|
|
576
|
+
} catch (e) {
|
|
577
|
+
result = resultParam;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
return {
|
|
581
|
+
actionId,
|
|
582
|
+
result,
|
|
583
|
+
error
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
getDefaultCallbackSchema() {
|
|
587
|
+
if (typeof window !== "undefined" && window.location) {
|
|
588
|
+
return `${window.location.protocol}//${window.location.host}${window.location.pathname}`;
|
|
589
|
+
}
|
|
590
|
+
return "";
|
|
591
|
+
}
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
});
|
|
7
595
|
var TypedEventEmitter = class {
|
|
8
596
|
constructor() {
|
|
9
597
|
this.emitter = new EventEmitter();
|
|
@@ -32,23 +620,12 @@ var TypedEventEmitter = class {
|
|
|
32
620
|
return this;
|
|
33
621
|
}
|
|
34
622
|
};
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
WalletType3["WALLETCONNECT_TRON"] = "walletconnect-tron";
|
|
42
|
-
WalletType3["PRIVATE_KEY"] = "private-key";
|
|
43
|
-
return WalletType3;
|
|
44
|
-
})(WalletType || {});
|
|
45
|
-
var WalletState = /* @__PURE__ */ ((WalletState2) => {
|
|
46
|
-
WalletState2["DISCONNECTED"] = "disconnected";
|
|
47
|
-
WalletState2["CONNECTING"] = "connecting";
|
|
48
|
-
WalletState2["CONNECTED"] = "connected";
|
|
49
|
-
WalletState2["ERROR"] = "error";
|
|
50
|
-
return WalletState2;
|
|
51
|
-
})(WalletState || {});
|
|
623
|
+
|
|
624
|
+
// src/core/adapter-registry.ts
|
|
625
|
+
init_types();
|
|
626
|
+
|
|
627
|
+
// src/adapters/base/wallet-adapter.ts
|
|
628
|
+
init_types();
|
|
52
629
|
|
|
53
630
|
// src/core/errors.ts
|
|
54
631
|
var WalletSDKError = class _WalletSDKError extends Error {
|
|
@@ -150,6 +727,13 @@ var WalletAdapter = class extends EventEmitter {
|
|
|
150
727
|
this.state = "disconnected" /* DISCONNECTED */;
|
|
151
728
|
this.currentAccount = null;
|
|
152
729
|
}
|
|
730
|
+
/**
|
|
731
|
+
* Check if the wallet is currently connected
|
|
732
|
+
* @returns true if the wallet is connected (state is CONNECTED and has an account)
|
|
733
|
+
*/
|
|
734
|
+
isConnected() {
|
|
735
|
+
return this.state === "connected" /* CONNECTED */ && this.currentAccount !== null;
|
|
736
|
+
}
|
|
153
737
|
/**
|
|
154
738
|
* Get the signer's address (implements ISigner interface)
|
|
155
739
|
* Returns the native address of the current account
|
|
@@ -219,6 +803,7 @@ var WalletAdapter = class extends EventEmitter {
|
|
|
219
803
|
};
|
|
220
804
|
|
|
221
805
|
// src/adapters/base/browser-wallet-adapter.ts
|
|
806
|
+
init_types();
|
|
222
807
|
var BrowserWalletAdapter = class extends WalletAdapter {
|
|
223
808
|
/**
|
|
224
809
|
* 检查钱包是否可用
|
|
@@ -252,6 +837,9 @@ var BrowserWalletAdapter = class extends WalletAdapter {
|
|
|
252
837
|
}
|
|
253
838
|
};
|
|
254
839
|
|
|
840
|
+
// src/adapters/evm/metamask.ts
|
|
841
|
+
init_types();
|
|
842
|
+
|
|
255
843
|
// src/utils/address/universal-address.ts
|
|
256
844
|
function createUniversalAddress(chainId, address) {
|
|
257
845
|
return `${chainId}:${address}`;
|
|
@@ -550,6 +1138,7 @@ var MetaMaskAdapter = class extends BrowserWalletAdapter {
|
|
|
550
1138
|
*/
|
|
551
1139
|
async connect(chainId) {
|
|
552
1140
|
await this.ensureAvailable();
|
|
1141
|
+
const targetChainId = Array.isArray(chainId) ? chainId[0] : chainId;
|
|
553
1142
|
try {
|
|
554
1143
|
this.setState("connecting" /* CONNECTING */);
|
|
555
1144
|
const provider = this.getBrowserProvider();
|
|
@@ -563,10 +1152,10 @@ var MetaMaskAdapter = class extends BrowserWalletAdapter {
|
|
|
563
1152
|
method: "eth_chainId"
|
|
564
1153
|
});
|
|
565
1154
|
const parsedChainId = parseInt(currentChainId, 16);
|
|
566
|
-
if (
|
|
567
|
-
await this.switchChain(
|
|
1155
|
+
if (targetChainId && targetChainId !== parsedChainId) {
|
|
1156
|
+
await this.switchChain(targetChainId);
|
|
568
1157
|
}
|
|
569
|
-
const finalChainId =
|
|
1158
|
+
const finalChainId = targetChainId || parsedChainId;
|
|
570
1159
|
const viemChain = this.getViemChain(finalChainId);
|
|
571
1160
|
this.walletClient = createWalletClient({
|
|
572
1161
|
account: accounts[0],
|
|
@@ -952,6 +1541,7 @@ var MetaMaskAdapter = class extends BrowserWalletAdapter {
|
|
|
952
1541
|
};
|
|
953
1542
|
|
|
954
1543
|
// src/adapters/tron/tronlink.ts
|
|
1544
|
+
init_types();
|
|
955
1545
|
var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
|
|
956
1546
|
constructor() {
|
|
957
1547
|
super(...arguments);
|
|
@@ -999,6 +1589,7 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
|
|
|
999
1589
|
*/
|
|
1000
1590
|
async connect(chainId) {
|
|
1001
1591
|
await this.ensureAvailable();
|
|
1592
|
+
const targetChainId = Array.isArray(chainId) ? chainId[0] : chainId;
|
|
1002
1593
|
try {
|
|
1003
1594
|
this.setState("connecting" /* CONNECTING */);
|
|
1004
1595
|
const w = window;
|
|
@@ -1053,7 +1644,7 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
|
|
|
1053
1644
|
if (!address) {
|
|
1054
1645
|
throw new Error("Failed to get Tron address. Please make sure your wallet is unlocked and try again.");
|
|
1055
1646
|
}
|
|
1056
|
-
const tronChainId =
|
|
1647
|
+
const tronChainId = targetChainId || _TronLinkAdapter.TRON_MAINNET_CHAIN_ID;
|
|
1057
1648
|
const account = {
|
|
1058
1649
|
universalAddress: createUniversalAddress(tronChainId, address),
|
|
1059
1650
|
nativeAddress: address,
|
|
@@ -1383,6 +1974,7 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
|
|
|
1383
1974
|
// Tron 主网链 ID
|
|
1384
1975
|
_TronLinkAdapter.TRON_MAINNET_CHAIN_ID = 195;
|
|
1385
1976
|
var TronLinkAdapter = _TronLinkAdapter;
|
|
1977
|
+
init_types();
|
|
1386
1978
|
var EVMPrivateKeyAdapter = class extends WalletAdapter {
|
|
1387
1979
|
constructor() {
|
|
1388
1980
|
super(...arguments);
|
|
@@ -1396,27 +1988,28 @@ var EVMPrivateKeyAdapter = class extends WalletAdapter {
|
|
|
1396
1988
|
/**
|
|
1397
1989
|
* 连接(导入私钥)
|
|
1398
1990
|
*/
|
|
1399
|
-
async connect(chainId
|
|
1991
|
+
async connect(chainId) {
|
|
1400
1992
|
if (!this.privateKey) {
|
|
1401
1993
|
throw new Error("Private key not set. Call setPrivateKey() first.");
|
|
1402
1994
|
}
|
|
1403
1995
|
try {
|
|
1404
1996
|
this.setState("connecting" /* CONNECTING */);
|
|
1405
1997
|
const account = privateKeyToAccount(this.privateKey);
|
|
1998
|
+
const targetChainId = Array.isArray(chainId) ? chainId[0] : chainId || 1;
|
|
1406
1999
|
this.walletClient = createWalletClient({
|
|
1407
2000
|
account,
|
|
1408
|
-
chain: this.getViemChain(
|
|
2001
|
+
chain: this.getViemChain(targetChainId),
|
|
1409
2002
|
transport: http()
|
|
1410
2003
|
});
|
|
1411
2004
|
this.publicClient = createPublicClient({
|
|
1412
|
-
chain: this.getViemChain(
|
|
2005
|
+
chain: this.getViemChain(targetChainId),
|
|
1413
2006
|
transport: http()
|
|
1414
2007
|
});
|
|
1415
2008
|
const address = formatEVMAddress(account.address);
|
|
1416
2009
|
const accountInfo = {
|
|
1417
|
-
universalAddress: createUniversalAddress(
|
|
2010
|
+
universalAddress: createUniversalAddress(targetChainId, address),
|
|
1418
2011
|
nativeAddress: address,
|
|
1419
|
-
chainId,
|
|
2012
|
+
chainId: targetChainId,
|
|
1420
2013
|
chainType: ChainType.EVM,
|
|
1421
2014
|
isActive: true
|
|
1422
2015
|
};
|
|
@@ -1628,66 +2221,2312 @@ var EVMPrivateKeyAdapter = class extends WalletAdapter {
|
|
|
1628
2221
|
};
|
|
1629
2222
|
}
|
|
1630
2223
|
};
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
this.
|
|
1636
|
-
this.
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
this.
|
|
1643
|
-
|
|
1644
|
-
|
|
2224
|
+
init_types();
|
|
2225
|
+
var _WalletConnectAdapter = class _WalletConnectAdapter extends WalletAdapter {
|
|
2226
|
+
constructor(projectId) {
|
|
2227
|
+
super();
|
|
2228
|
+
this.type = "walletconnect" /* WALLETCONNECT */;
|
|
2229
|
+
this.chainType = ChainType.EVM;
|
|
2230
|
+
this.name = "WalletConnect";
|
|
2231
|
+
this.icon = "https://avatars.githubusercontent.com/u/37784886";
|
|
2232
|
+
this.provider = null;
|
|
2233
|
+
this.walletClient = null;
|
|
2234
|
+
this.publicClient = null;
|
|
2235
|
+
this.supportedChains = [];
|
|
2236
|
+
/**
|
|
2237
|
+
* Handle accounts changed
|
|
2238
|
+
*/
|
|
2239
|
+
this.handleAccountsChanged = (accounts) => {
|
|
2240
|
+
if (accounts.length === 0) {
|
|
2241
|
+
this.setState("disconnected" /* DISCONNECTED */);
|
|
2242
|
+
this.setAccount(null);
|
|
2243
|
+
this.emitAccountChanged(null);
|
|
2244
|
+
} else {
|
|
2245
|
+
const address = formatEVMAddress(accounts[0]);
|
|
2246
|
+
const account = {
|
|
2247
|
+
universalAddress: createUniversalAddress(this.currentAccount.chainId, address),
|
|
2248
|
+
nativeAddress: address,
|
|
2249
|
+
chainId: this.currentAccount.chainId,
|
|
2250
|
+
chainType: ChainType.EVM,
|
|
2251
|
+
isActive: true
|
|
2252
|
+
};
|
|
2253
|
+
this.setAccount(account);
|
|
2254
|
+
this.emitAccountChanged(account);
|
|
2255
|
+
}
|
|
2256
|
+
};
|
|
2257
|
+
/**
|
|
2258
|
+
* Handle chain changed
|
|
2259
|
+
*/
|
|
2260
|
+
this.handleChainChanged = (chainIdHex) => {
|
|
2261
|
+
const chainId = parseInt(chainIdHex, 16);
|
|
2262
|
+
if (this.currentAccount) {
|
|
2263
|
+
const account = {
|
|
2264
|
+
...this.currentAccount,
|
|
2265
|
+
chainId,
|
|
2266
|
+
universalAddress: createUniversalAddress(chainId, this.currentAccount.nativeAddress)
|
|
2267
|
+
};
|
|
2268
|
+
this.setAccount(account);
|
|
2269
|
+
this.emitChainChanged(chainId);
|
|
2270
|
+
const viemChain = this.getViemChain(chainId);
|
|
2271
|
+
const chainInfo = getChainInfo(chainId);
|
|
2272
|
+
const primaryRpcUrl = chainInfo?.rpcUrls[0];
|
|
2273
|
+
if (this.provider) {
|
|
2274
|
+
this.walletClient = createWalletClient({
|
|
2275
|
+
account: this.currentAccount.nativeAddress,
|
|
2276
|
+
chain: viemChain,
|
|
2277
|
+
transport: custom(this.provider)
|
|
2278
|
+
});
|
|
2279
|
+
this.publicClient = createPublicClient({
|
|
2280
|
+
chain: viemChain,
|
|
2281
|
+
transport: primaryRpcUrl ? http(primaryRpcUrl) : custom(this.provider)
|
|
2282
|
+
});
|
|
2283
|
+
}
|
|
2284
|
+
}
|
|
2285
|
+
};
|
|
2286
|
+
/**
|
|
2287
|
+
* Handle disconnect
|
|
2288
|
+
*/
|
|
2289
|
+
this.handleDisconnect = () => {
|
|
2290
|
+
this.setState("disconnected" /* DISCONNECTED */);
|
|
2291
|
+
this.setAccount(null);
|
|
2292
|
+
if (_WalletConnectAdapter.providerInstance === this.provider) {
|
|
2293
|
+
_WalletConnectAdapter.providerInstance = null;
|
|
2294
|
+
_WalletConnectAdapter.providerProjectId = null;
|
|
2295
|
+
}
|
|
2296
|
+
this.provider = null;
|
|
2297
|
+
this.walletClient = null;
|
|
2298
|
+
this.publicClient = null;
|
|
2299
|
+
this.emitDisconnected();
|
|
2300
|
+
};
|
|
2301
|
+
if (!projectId) {
|
|
2302
|
+
throw new ConfigurationError("WalletConnect projectId is required");
|
|
2303
|
+
}
|
|
2304
|
+
this.projectId = projectId;
|
|
1645
2305
|
}
|
|
1646
2306
|
/**
|
|
1647
|
-
*
|
|
2307
|
+
* Check if WalletConnect is available
|
|
2308
|
+
* WalletConnect is always available (it's a web-based connection)
|
|
2309
|
+
* Also works in Telegram Mini Apps
|
|
1648
2310
|
*/
|
|
1649
|
-
|
|
1650
|
-
|
|
2311
|
+
async isAvailable() {
|
|
2312
|
+
return typeof window !== "undefined";
|
|
1651
2313
|
}
|
|
1652
2314
|
/**
|
|
1653
|
-
*
|
|
2315
|
+
* Check if running in Telegram environment (Mini App or Web)
|
|
2316
|
+
* Both Telegram Mini App (in client) and Telegram Web (web.telegram.org)
|
|
2317
|
+
* provide window.Telegram.WebApp API, so they are treated the same way.
|
|
2318
|
+
*
|
|
2319
|
+
* Reference: https://docs.reown.com/appkit/integrations/telegram-mini-apps
|
|
1654
2320
|
*/
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
2321
|
+
isTelegramMiniApp() {
|
|
2322
|
+
if (typeof window === "undefined") return false;
|
|
2323
|
+
const tg = window.Telegram?.WebApp;
|
|
2324
|
+
if (!tg) return false;
|
|
2325
|
+
const platform = tg.platform || "unknown";
|
|
2326
|
+
console.log("[WalletConnect] Telegram environment detected:", {
|
|
2327
|
+
platform,
|
|
2328
|
+
version: tg.version,
|
|
2329
|
+
isMiniApp: platform !== "web",
|
|
2330
|
+
// Mini App if not web platform
|
|
2331
|
+
isWeb: platform === "web"
|
|
2332
|
+
// Telegram Web if web platform
|
|
2333
|
+
});
|
|
2334
|
+
return true;
|
|
1661
2335
|
}
|
|
1662
2336
|
/**
|
|
1663
|
-
*
|
|
2337
|
+
* Get Telegram WebApp instance if available
|
|
1664
2338
|
*/
|
|
1665
|
-
|
|
1666
|
-
|
|
2339
|
+
getTelegramWebApp() {
|
|
2340
|
+
if (typeof window === "undefined") return null;
|
|
2341
|
+
return window.Telegram?.WebApp || null;
|
|
1667
2342
|
}
|
|
1668
2343
|
/**
|
|
1669
|
-
*
|
|
2344
|
+
* Close Telegram deep link popup (wc:// links)
|
|
2345
|
+
* In Telegram Mini Apps, WalletConnect may open a wc:// deep link popup
|
|
2346
|
+
* that doesn't automatically close after the operation completes.
|
|
2347
|
+
* This method attempts to close it by:
|
|
2348
|
+
* 1. Trying to close any open windows/popups
|
|
2349
|
+
* 2. Using Telegram WebApp API if available
|
|
2350
|
+
* 3. Navigating back or closing the popup
|
|
1670
2351
|
*/
|
|
1671
|
-
|
|
1672
|
-
|
|
2352
|
+
closeTelegramDeepLinkPopup() {
|
|
2353
|
+
if (!this.isTelegramMiniApp()) {
|
|
2354
|
+
return;
|
|
2355
|
+
}
|
|
2356
|
+
try {
|
|
2357
|
+
const tg = this.getTelegramWebApp();
|
|
2358
|
+
if (!tg) {
|
|
2359
|
+
return;
|
|
2360
|
+
}
|
|
2361
|
+
if (typeof window !== "undefined") {
|
|
2362
|
+
window.focus();
|
|
2363
|
+
if (tg.BackButton && tg.BackButton.isVisible) {
|
|
2364
|
+
console.log("[WalletConnect] Closing Telegram deep link popup via BackButton");
|
|
2365
|
+
}
|
|
2366
|
+
setTimeout(() => {
|
|
2367
|
+
if (document.hasFocus()) {
|
|
2368
|
+
console.log("[WalletConnect] Main window has focus, popup likely closed");
|
|
2369
|
+
} else {
|
|
2370
|
+
window.focus();
|
|
2371
|
+
console.log("[WalletConnect] Attempted to focus main window to close popup");
|
|
2372
|
+
}
|
|
2373
|
+
}, 500);
|
|
2374
|
+
const handleVisibilityChange = () => {
|
|
2375
|
+
if (document.visibilityState === "visible") {
|
|
2376
|
+
console.log("[WalletConnect] Page became visible, popup may have closed");
|
|
2377
|
+
document.removeEventListener("visibilitychange", handleVisibilityChange);
|
|
2378
|
+
}
|
|
2379
|
+
};
|
|
2380
|
+
document.addEventListener("visibilitychange", handleVisibilityChange);
|
|
2381
|
+
setTimeout(() => {
|
|
2382
|
+
document.removeEventListener("visibilitychange", handleVisibilityChange);
|
|
2383
|
+
}, 2e3);
|
|
2384
|
+
}
|
|
2385
|
+
} catch (error) {
|
|
2386
|
+
console.warn("[WalletConnect] Error closing Telegram deep link popup:", error);
|
|
2387
|
+
}
|
|
1673
2388
|
}
|
|
1674
2389
|
/**
|
|
1675
|
-
*
|
|
2390
|
+
* Connect wallet
|
|
2391
|
+
*
|
|
2392
|
+
* @param chainId - Single chain ID or array of chain IDs to request
|
|
2393
|
+
* If array is provided, wallet will be requested to connect to multiple chains
|
|
2394
|
+
* When multiple chains are requested, the wallet can switch between them
|
|
2395
|
+
* Default: 1 (Ethereum Mainnet)
|
|
2396
|
+
*
|
|
2397
|
+
* @example
|
|
2398
|
+
* // Single chain
|
|
2399
|
+
* await adapter.connect(1) // Ethereum only
|
|
2400
|
+
*
|
|
2401
|
+
* @example
|
|
2402
|
+
* // Multiple chains
|
|
2403
|
+
* await adapter.connect([1, 56, 137]) // Ethereum, BSC, Polygon
|
|
1676
2404
|
*/
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
const adapter = this.getAdapter(type);
|
|
1681
|
-
if (adapter && adapter.chainType === chainType) {
|
|
1682
|
-
types.push(type);
|
|
1683
|
-
}
|
|
2405
|
+
async connect(chainId) {
|
|
2406
|
+
if (typeof window === "undefined") {
|
|
2407
|
+
throw new Error("WalletConnect requires a browser environment");
|
|
1684
2408
|
}
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
2409
|
+
if (_WalletConnectAdapter.providerInstance && _WalletConnectAdapter.providerProjectId === this.projectId) {
|
|
2410
|
+
const existingProvider = _WalletConnectAdapter.providerInstance;
|
|
2411
|
+
if (existingProvider.accounts && existingProvider.accounts.length > 0) {
|
|
2412
|
+
this.provider = existingProvider;
|
|
2413
|
+
let targetChains;
|
|
2414
|
+
if (Array.isArray(chainId)) {
|
|
2415
|
+
targetChains = chainId.length > 0 ? chainId : [1];
|
|
2416
|
+
} else if (chainId) {
|
|
2417
|
+
targetChains = [chainId];
|
|
2418
|
+
} else {
|
|
2419
|
+
targetChains = [1];
|
|
2420
|
+
}
|
|
2421
|
+
const existingChains = this.supportedChains || [];
|
|
2422
|
+
const mergedChains = [.../* @__PURE__ */ new Set([...existingChains, ...targetChains])];
|
|
2423
|
+
this.supportedChains = mergedChains;
|
|
2424
|
+
const currentChainId = existingProvider.chainId || targetChains[0];
|
|
2425
|
+
const address = formatEVMAddress(existingProvider.accounts[0]);
|
|
2426
|
+
const account = {
|
|
2427
|
+
universalAddress: createUniversalAddress(currentChainId, address),
|
|
2428
|
+
nativeAddress: address,
|
|
2429
|
+
chainId: currentChainId,
|
|
2430
|
+
chainType: ChainType.EVM,
|
|
2431
|
+
isActive: true
|
|
2432
|
+
};
|
|
2433
|
+
this.setState("connected" /* CONNECTED */);
|
|
2434
|
+
this.setAccount(account);
|
|
2435
|
+
if (!this.walletClient) {
|
|
2436
|
+
const viemChain = this.getViemChain(currentChainId);
|
|
2437
|
+
this.walletClient = createWalletClient({
|
|
2438
|
+
account: existingProvider.accounts[0],
|
|
2439
|
+
chain: viemChain,
|
|
2440
|
+
transport: custom(existingProvider)
|
|
2441
|
+
});
|
|
2442
|
+
const chainInfo = getChainInfo(currentChainId);
|
|
2443
|
+
const primaryRpcUrl = chainInfo?.rpcUrls[0];
|
|
2444
|
+
this.publicClient = createPublicClient({
|
|
2445
|
+
chain: viemChain,
|
|
2446
|
+
transport: primaryRpcUrl ? http(primaryRpcUrl) : custom(existingProvider)
|
|
2447
|
+
});
|
|
2448
|
+
this.setupEventListeners();
|
|
2449
|
+
}
|
|
2450
|
+
console.log("[WalletConnect] Reusing existing provider session");
|
|
2451
|
+
return account;
|
|
2452
|
+
}
|
|
2453
|
+
}
|
|
2454
|
+
if (this.state === "connected" /* CONNECTED */ && this.currentAccount && this.provider) {
|
|
2455
|
+
if (this.provider.accounts && this.provider.accounts.length > 0) {
|
|
2456
|
+
let targetChains;
|
|
2457
|
+
if (Array.isArray(chainId)) {
|
|
2458
|
+
targetChains = chainId.length > 0 ? chainId : [1];
|
|
2459
|
+
} else if (chainId) {
|
|
2460
|
+
targetChains = [chainId];
|
|
2461
|
+
} else {
|
|
2462
|
+
targetChains = [1];
|
|
2463
|
+
}
|
|
2464
|
+
const existingChains = this.supportedChains || [];
|
|
2465
|
+
const mergedChains = [.../* @__PURE__ */ new Set([...existingChains, ...targetChains])];
|
|
2466
|
+
this.supportedChains = mergedChains;
|
|
2467
|
+
console.log("[WalletConnect] Already connected, reusing existing connection");
|
|
2468
|
+
return this.currentAccount;
|
|
2469
|
+
} else {
|
|
2470
|
+
this.setState("disconnected" /* DISCONNECTED */);
|
|
2471
|
+
this.setAccount(null);
|
|
2472
|
+
this.provider = null;
|
|
2473
|
+
}
|
|
2474
|
+
}
|
|
2475
|
+
try {
|
|
2476
|
+
this.setState("connecting" /* CONNECTING */);
|
|
2477
|
+
let targetChains;
|
|
2478
|
+
if (Array.isArray(chainId)) {
|
|
2479
|
+
targetChains = chainId.length > 0 ? chainId : [1];
|
|
2480
|
+
} else if (chainId) {
|
|
2481
|
+
targetChains = [chainId];
|
|
2482
|
+
} else {
|
|
2483
|
+
targetChains = [1];
|
|
2484
|
+
}
|
|
2485
|
+
this.supportedChains = targetChains;
|
|
2486
|
+
const primaryChain = targetChains[0];
|
|
2487
|
+
const optionalChains = targetChains.slice(1);
|
|
2488
|
+
const isTelegram = this.isTelegramMiniApp();
|
|
2489
|
+
const telegramWebApp = this.getTelegramWebApp();
|
|
2490
|
+
let appUrl = "";
|
|
2491
|
+
if (typeof window !== "undefined") {
|
|
2492
|
+
try {
|
|
2493
|
+
if (window.location && window.location.origin) {
|
|
2494
|
+
appUrl = window.location.origin;
|
|
2495
|
+
} else if (window.location && window.location.href) {
|
|
2496
|
+
const url = new URL(window.location.href);
|
|
2497
|
+
appUrl = url.origin;
|
|
2498
|
+
}
|
|
2499
|
+
} catch (error) {
|
|
2500
|
+
console.warn("[WalletConnect] Failed to get origin from window.location:", error);
|
|
2501
|
+
}
|
|
2502
|
+
if (!appUrl) {
|
|
2503
|
+
appUrl = "https://enclave.network";
|
|
2504
|
+
}
|
|
2505
|
+
} else {
|
|
2506
|
+
appUrl = "https://enclave.network";
|
|
2507
|
+
}
|
|
2508
|
+
if (!appUrl || !appUrl.startsWith("http://") && !appUrl.startsWith("https://")) {
|
|
2509
|
+
appUrl = "https://enclave.network";
|
|
2510
|
+
}
|
|
2511
|
+
const icons = [
|
|
2512
|
+
"https://walletconnect.com/walletconnect-logo.svg",
|
|
2513
|
+
"https://avatars.githubusercontent.com/u/37784886"
|
|
2514
|
+
// WalletConnect GitHub avatar
|
|
2515
|
+
];
|
|
2516
|
+
const initOptions = {
|
|
2517
|
+
projectId: this.projectId,
|
|
2518
|
+
chains: [primaryChain],
|
|
2519
|
+
// Primary chain (required)
|
|
2520
|
+
showQrModal: true,
|
|
2521
|
+
// QR modal works in Telegram Mini Apps
|
|
2522
|
+
metadata: {
|
|
2523
|
+
name: "Enclave Wallet SDK",
|
|
2524
|
+
description: "Multi-chain wallet adapter for Enclave",
|
|
2525
|
+
url: appUrl,
|
|
2526
|
+
icons
|
|
2527
|
+
}
|
|
2528
|
+
};
|
|
2529
|
+
if (isTelegram && telegramWebApp) {
|
|
2530
|
+
const platform = telegramWebApp.platform || "unknown";
|
|
2531
|
+
const isMiniApp = platform !== "web";
|
|
2532
|
+
console.log("[WalletConnect] Detected Telegram environment:", {
|
|
2533
|
+
platform,
|
|
2534
|
+
isMiniApp,
|
|
2535
|
+
isWeb: platform === "web"
|
|
2536
|
+
});
|
|
2537
|
+
if (telegramWebApp.isExpanded === false) {
|
|
2538
|
+
telegramWebApp.expand();
|
|
2539
|
+
}
|
|
2540
|
+
}
|
|
2541
|
+
if (optionalChains.length > 0) {
|
|
2542
|
+
initOptions.optionalChains = optionalChains;
|
|
2543
|
+
} else {
|
|
2544
|
+
initOptions.optionalChains = [primaryChain];
|
|
2545
|
+
}
|
|
2546
|
+
const hasExistingProvider = _WalletConnectAdapter.providerInstance && _WalletConnectAdapter.providerProjectId === this.projectId;
|
|
2547
|
+
const needsReinit = hasExistingProvider && _WalletConnectAdapter.providerChains !== null && JSON.stringify(_WalletConnectAdapter.providerChains.sort()) !== JSON.stringify(targetChains.sort());
|
|
2548
|
+
if (needsReinit) {
|
|
2549
|
+
console.log("[WalletConnect] Provider initialized with different chains, reinitializing...", {
|
|
2550
|
+
existing: _WalletConnectAdapter.providerChains,
|
|
2551
|
+
requested: targetChains
|
|
2552
|
+
});
|
|
2553
|
+
const existingProvider = _WalletConnectAdapter.providerInstance;
|
|
2554
|
+
if (existingProvider) {
|
|
2555
|
+
try {
|
|
2556
|
+
if (existingProvider.accounts && existingProvider.accounts.length > 0) {
|
|
2557
|
+
await existingProvider.disconnect();
|
|
2558
|
+
}
|
|
2559
|
+
} catch (error) {
|
|
2560
|
+
console.warn("[WalletConnect] Error disconnecting existing provider:", error);
|
|
2561
|
+
}
|
|
2562
|
+
}
|
|
2563
|
+
_WalletConnectAdapter.providerInstance = null;
|
|
2564
|
+
_WalletConnectAdapter.providerChains = null;
|
|
2565
|
+
}
|
|
2566
|
+
if (_WalletConnectAdapter.providerInstance && _WalletConnectAdapter.providerProjectId === this.projectId) {
|
|
2567
|
+
this.provider = _WalletConnectAdapter.providerInstance;
|
|
2568
|
+
console.log("[WalletConnect] Reusing existing provider instance");
|
|
2569
|
+
if (this.provider.accounts && this.provider.accounts.length > 0) {
|
|
2570
|
+
console.log("[WalletConnect] Provider already has accounts, skipping enable()");
|
|
2571
|
+
} else {
|
|
2572
|
+
const hasSession = this.provider.session !== void 0 && this.provider.session !== null;
|
|
2573
|
+
console.log("[WalletConnect] Provider has no accounts, calling enable() to show QR modal");
|
|
2574
|
+
console.log("[WalletConnect] Provider state:", {
|
|
2575
|
+
accounts: this.provider.accounts,
|
|
2576
|
+
chainId: this.provider.chainId,
|
|
2577
|
+
hasSession,
|
|
2578
|
+
sessionTopic: this.provider.session?.topic
|
|
2579
|
+
});
|
|
2580
|
+
if (hasSession && (!this.provider.accounts || this.provider.accounts.length === 0)) {
|
|
2581
|
+
console.log("[WalletConnect] Found stale session, disconnecting before reconnecting...");
|
|
2582
|
+
try {
|
|
2583
|
+
await this.provider.disconnect();
|
|
2584
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
2585
|
+
} catch (disconnectError) {
|
|
2586
|
+
console.warn("[WalletConnect] Error disconnecting stale session:", disconnectError);
|
|
2587
|
+
}
|
|
2588
|
+
}
|
|
2589
|
+
try {
|
|
2590
|
+
console.log("[WalletConnect] Calling enable()...");
|
|
2591
|
+
const enableResult = await this.provider.enable();
|
|
2592
|
+
console.log("[WalletConnect] enable() completed, result:", enableResult);
|
|
2593
|
+
console.log("[WalletConnect] Provider state after enable():", {
|
|
2594
|
+
accounts: this.provider.accounts,
|
|
2595
|
+
chainId: this.provider.chainId,
|
|
2596
|
+
session: this.provider.session ? {
|
|
2597
|
+
topic: this.provider.session.topic,
|
|
2598
|
+
namespaces: this.provider.session.namespaces ? Object.keys(this.provider.session.namespaces) : "none"
|
|
2599
|
+
} : "none"
|
|
2600
|
+
});
|
|
2601
|
+
} catch (error) {
|
|
2602
|
+
console.error("[WalletConnect] enable() error:", error);
|
|
2603
|
+
if (error.code === 4001 || error.message?.includes("rejected") || error.message?.includes("User rejected")) {
|
|
2604
|
+
throw new ConnectionRejectedError(this.type);
|
|
2605
|
+
}
|
|
2606
|
+
throw error;
|
|
2607
|
+
}
|
|
2608
|
+
}
|
|
2609
|
+
} else if (_WalletConnectAdapter.providerInstance && _WalletConnectAdapter.providerProjectId === this.projectId) {
|
|
2610
|
+
this.provider = _WalletConnectAdapter.providerInstance;
|
|
2611
|
+
console.log("[WalletConnect] Reusing existing provider instance (not connected)");
|
|
2612
|
+
if (this.provider.accounts && this.provider.accounts.length > 0) {
|
|
2613
|
+
console.log("[WalletConnect] Provider already has accounts after init, skipping enable()");
|
|
2614
|
+
} else {
|
|
2615
|
+
const hasSession = this.provider.session !== void 0 && this.provider.session !== null;
|
|
2616
|
+
console.log("[WalletConnect] Provider has no accounts, calling enable() to show QR modal");
|
|
2617
|
+
console.log("[WalletConnect] Provider state:", {
|
|
2618
|
+
accounts: this.provider.accounts,
|
|
2619
|
+
chainId: this.provider.chainId,
|
|
2620
|
+
hasSession,
|
|
2621
|
+
sessionTopic: this.provider.session?.topic
|
|
2622
|
+
});
|
|
2623
|
+
if (hasSession && (!this.provider.accounts || this.provider.accounts.length === 0)) {
|
|
2624
|
+
console.log("[WalletConnect] Found stale session after init, disconnecting before reconnecting...");
|
|
2625
|
+
try {
|
|
2626
|
+
await this.provider.disconnect();
|
|
2627
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
2628
|
+
} catch (disconnectError) {
|
|
2629
|
+
console.warn("[WalletConnect] Error disconnecting stale session:", disconnectError);
|
|
2630
|
+
}
|
|
2631
|
+
}
|
|
2632
|
+
try {
|
|
2633
|
+
await this.provider.enable();
|
|
2634
|
+
} catch (error) {
|
|
2635
|
+
console.error("[WalletConnect] enable() error:", error);
|
|
2636
|
+
if (error.code === 4001 || error.message?.includes("rejected") || error.message?.includes("User rejected")) {
|
|
2637
|
+
throw new ConnectionRejectedError(this.type);
|
|
2638
|
+
}
|
|
2639
|
+
throw error;
|
|
2640
|
+
}
|
|
2641
|
+
}
|
|
2642
|
+
} else if (_WalletConnectAdapter.isInitializing && _WalletConnectAdapter.initPromise) {
|
|
2643
|
+
console.log("[WalletConnect] Waiting for ongoing initialization...");
|
|
2644
|
+
this.provider = await _WalletConnectAdapter.initPromise;
|
|
2645
|
+
_WalletConnectAdapter.providerInstance = this.provider;
|
|
2646
|
+
_WalletConnectAdapter.providerProjectId = this.projectId;
|
|
2647
|
+
_WalletConnectAdapter.providerChains = targetChains;
|
|
2648
|
+
if (this.provider.accounts && this.provider.accounts.length > 0) {
|
|
2649
|
+
console.log("[WalletConnect] Provider already has accounts after init, skipping enable()");
|
|
2650
|
+
} else {
|
|
2651
|
+
try {
|
|
2652
|
+
await this.provider.enable();
|
|
2653
|
+
} catch (error) {
|
|
2654
|
+
if (error.code === 4001 || error.message?.includes("rejected") || error.message?.includes("User rejected")) {
|
|
2655
|
+
throw new ConnectionRejectedError(this.type);
|
|
2656
|
+
}
|
|
2657
|
+
throw error;
|
|
2658
|
+
}
|
|
2659
|
+
}
|
|
2660
|
+
} else {
|
|
2661
|
+
console.log("[WalletConnect] Initializing new provider with chains:", {
|
|
2662
|
+
primary: primaryChain,
|
|
2663
|
+
optional: optionalChains,
|
|
2664
|
+
all: targetChains
|
|
2665
|
+
});
|
|
2666
|
+
_WalletConnectAdapter.isInitializing = true;
|
|
2667
|
+
_WalletConnectAdapter.initPromise = EthereumProvider.init(initOptions);
|
|
2668
|
+
try {
|
|
2669
|
+
this.provider = await _WalletConnectAdapter.initPromise;
|
|
2670
|
+
_WalletConnectAdapter.providerInstance = this.provider;
|
|
2671
|
+
_WalletConnectAdapter.providerProjectId = this.projectId;
|
|
2672
|
+
_WalletConnectAdapter.providerChains = targetChains;
|
|
2673
|
+
if (this.provider.accounts && this.provider.accounts.length > 0) {
|
|
2674
|
+
console.log("[WalletConnect] Provider has restored session, skipping enable()");
|
|
2675
|
+
} else {
|
|
2676
|
+
const hasSession = this.provider.session !== void 0 && this.provider.session !== null;
|
|
2677
|
+
console.log("[WalletConnect] New provider initialized, calling enable() to show QR modal");
|
|
2678
|
+
console.log("[WalletConnect] Provider state:", {
|
|
2679
|
+
accounts: this.provider.accounts,
|
|
2680
|
+
chainId: this.provider.chainId,
|
|
2681
|
+
hasSession,
|
|
2682
|
+
sessionTopic: this.provider.session?.topic
|
|
2683
|
+
});
|
|
2684
|
+
if (hasSession && (!this.provider.accounts || this.provider.accounts.length === 0)) {
|
|
2685
|
+
console.log("[WalletConnect] Found stale session after init, disconnecting before reconnecting...");
|
|
2686
|
+
try {
|
|
2687
|
+
await this.provider.disconnect();
|
|
2688
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
2689
|
+
} catch (disconnectError) {
|
|
2690
|
+
console.warn("[WalletConnect] Error disconnecting stale session:", disconnectError);
|
|
2691
|
+
}
|
|
2692
|
+
}
|
|
2693
|
+
try {
|
|
2694
|
+
await this.provider.enable();
|
|
2695
|
+
} catch (error) {
|
|
2696
|
+
console.error("[WalletConnect] enable() error:", error);
|
|
2697
|
+
if (error.code === 4001 || error.message?.includes("rejected") || error.message?.includes("User rejected")) {
|
|
2698
|
+
throw new ConnectionRejectedError(this.type);
|
|
2699
|
+
}
|
|
2700
|
+
throw error;
|
|
2701
|
+
}
|
|
2702
|
+
}
|
|
2703
|
+
} finally {
|
|
2704
|
+
_WalletConnectAdapter.isInitializing = false;
|
|
2705
|
+
_WalletConnectAdapter.initPromise = null;
|
|
2706
|
+
}
|
|
2707
|
+
}
|
|
2708
|
+
let accounts = this.provider.accounts;
|
|
2709
|
+
if (!accounts || accounts.length === 0) {
|
|
2710
|
+
console.log("[WalletConnect] provider.accounts is empty, checking session.namespaces.eip155.accounts...");
|
|
2711
|
+
const session = this.provider.session;
|
|
2712
|
+
if (session && session.namespaces?.eip155?.accounts) {
|
|
2713
|
+
const sessionAccounts = session.namespaces.eip155.accounts.map((acc) => {
|
|
2714
|
+
const parts = acc.split(":");
|
|
2715
|
+
if (parts.length >= 3 && parts[0] === "eip155") {
|
|
2716
|
+
return parts[2];
|
|
2717
|
+
}
|
|
2718
|
+
return null;
|
|
2719
|
+
}).filter((addr) => addr !== null && addr.startsWith("0x"));
|
|
2720
|
+
if (sessionAccounts.length > 0) {
|
|
2721
|
+
const uniqueAccounts = [...new Set(sessionAccounts)];
|
|
2722
|
+
console.log("[WalletConnect] Found accounts in session.namespaces.eip155.accounts:", {
|
|
2723
|
+
raw: session.namespaces.eip155.accounts,
|
|
2724
|
+
extracted: uniqueAccounts,
|
|
2725
|
+
chains: session.namespaces.eip155.chains
|
|
2726
|
+
});
|
|
2727
|
+
accounts = uniqueAccounts;
|
|
2728
|
+
}
|
|
2729
|
+
}
|
|
2730
|
+
}
|
|
2731
|
+
if (!accounts || accounts.length === 0) {
|
|
2732
|
+
console.log("[WalletConnect] Accounts not available, waiting for provider.accounts to populate...");
|
|
2733
|
+
const maxWaitTime = 3e3;
|
|
2734
|
+
const checkInterval = 100;
|
|
2735
|
+
const maxChecks = maxWaitTime / checkInterval;
|
|
2736
|
+
for (let i = 0; i < maxChecks; i++) {
|
|
2737
|
+
await new Promise((resolve) => setTimeout(resolve, checkInterval));
|
|
2738
|
+
accounts = this.provider.accounts;
|
|
2739
|
+
if (accounts && accounts.length > 0) {
|
|
2740
|
+
console.log(`[WalletConnect] Accounts available after ${(i + 1) * checkInterval}ms`);
|
|
2741
|
+
break;
|
|
2742
|
+
}
|
|
2743
|
+
}
|
|
2744
|
+
}
|
|
2745
|
+
if (!accounts || accounts.length === 0) {
|
|
2746
|
+
const session = this.provider.session;
|
|
2747
|
+
const providerState = {
|
|
2748
|
+
providerAccounts: this.provider.accounts,
|
|
2749
|
+
providerChainId: this.provider.chainId,
|
|
2750
|
+
session: session ? {
|
|
2751
|
+
exists: true,
|
|
2752
|
+
topic: session.topic,
|
|
2753
|
+
namespaces: session.namespaces ? Object.keys(session.namespaces) : "none",
|
|
2754
|
+
eip155Namespace: session.namespaces?.eip155 ? {
|
|
2755
|
+
accounts: session.namespaces.eip155.accounts,
|
|
2756
|
+
// CAIP-10 format
|
|
2757
|
+
chains: session.namespaces.eip155.chains,
|
|
2758
|
+
// CAIP-2 format
|
|
2759
|
+
methods: session.namespaces.eip155.methods,
|
|
2760
|
+
events: session.namespaces.eip155.events
|
|
2761
|
+
} : "none",
|
|
2762
|
+
// Log full session structure for debugging
|
|
2763
|
+
fullSession: JSON.stringify(session, null, 2)
|
|
2764
|
+
} : "none",
|
|
2765
|
+
// Check if provider has any other properties that might contain accounts
|
|
2766
|
+
providerKeys: Object.keys(this.provider)
|
|
2767
|
+
};
|
|
2768
|
+
console.error("[WalletConnect] No accounts available after enable() and wait", providerState);
|
|
2769
|
+
console.error("[WalletConnect] Full provider object:", this.provider);
|
|
2770
|
+
console.error("[WalletConnect] Full session object:", session);
|
|
2771
|
+
console.error("[WalletConnect] Session namespaces structure:", session?.namespaces);
|
|
2772
|
+
throw new Error("WalletConnect connection established but no accounts available. Please check session.namespaces.eip155.accounts in the console logs above.");
|
|
2773
|
+
}
|
|
2774
|
+
const currentChainId = this.provider.chainId || targetChains[0];
|
|
2775
|
+
const viemChain = this.getViemChain(currentChainId);
|
|
2776
|
+
this.walletClient = createWalletClient({
|
|
2777
|
+
account: accounts[0],
|
|
2778
|
+
chain: viemChain,
|
|
2779
|
+
transport: custom(this.provider)
|
|
2780
|
+
});
|
|
2781
|
+
const chainInfo = getChainInfo(currentChainId);
|
|
2782
|
+
const primaryRpcUrl = chainInfo?.rpcUrls[0];
|
|
2783
|
+
this.publicClient = createPublicClient({
|
|
2784
|
+
chain: viemChain,
|
|
2785
|
+
transport: primaryRpcUrl ? http(primaryRpcUrl) : custom(this.provider)
|
|
2786
|
+
});
|
|
2787
|
+
const address = formatEVMAddress(accounts[0]);
|
|
2788
|
+
const account = {
|
|
2789
|
+
universalAddress: createUniversalAddress(currentChainId, address),
|
|
2790
|
+
nativeAddress: address,
|
|
2791
|
+
chainId: currentChainId,
|
|
2792
|
+
chainType: ChainType.EVM,
|
|
2793
|
+
isActive: true
|
|
2794
|
+
};
|
|
2795
|
+
this.setState("connected" /* CONNECTED */);
|
|
2796
|
+
this.setAccount(account);
|
|
2797
|
+
this.setupEventListeners();
|
|
2798
|
+
return account;
|
|
2799
|
+
} catch (error) {
|
|
2800
|
+
this.setState("error" /* ERROR */);
|
|
2801
|
+
this.setAccount(null);
|
|
2802
|
+
const origin = typeof window !== "undefined" && window.location ? window.location.origin : "";
|
|
2803
|
+
const errorCode = error?.code;
|
|
2804
|
+
const errorMessage = error?.message || String(error);
|
|
2805
|
+
const isOriginNotAllowed = errorCode === 3e3 || /origin not allowed/i.test(errorMessage) || /Unauthorized:\s*origin not allowed/i.test(errorMessage);
|
|
2806
|
+
const session = this.provider?.session;
|
|
2807
|
+
const providerState = this.provider ? {
|
|
2808
|
+
accounts: this.provider.accounts,
|
|
2809
|
+
chainId: this.provider.chainId,
|
|
2810
|
+
session: session ? {
|
|
2811
|
+
exists: true,
|
|
2812
|
+
topic: session.topic,
|
|
2813
|
+
namespaces: session.namespaces ? Object.keys(session.namespaces) : "none",
|
|
2814
|
+
eip155Namespace: session.namespaces?.eip155 ? {
|
|
2815
|
+
accounts: session.namespaces.eip155.accounts,
|
|
2816
|
+
chains: session.namespaces.eip155.chains,
|
|
2817
|
+
methods: session.namespaces.eip155.methods,
|
|
2818
|
+
events: session.namespaces.eip155.events
|
|
2819
|
+
} : "none"
|
|
2820
|
+
} : "none",
|
|
2821
|
+
providerKeys: Object.keys(this.provider)
|
|
2822
|
+
} : "no provider";
|
|
2823
|
+
console.error("[WalletConnect] Connection error:", {
|
|
2824
|
+
error,
|
|
2825
|
+
code: error.code,
|
|
2826
|
+
message: error.message,
|
|
2827
|
+
stack: error.stack,
|
|
2828
|
+
providerState
|
|
2829
|
+
});
|
|
2830
|
+
if (this.provider) {
|
|
2831
|
+
console.error("[WalletConnect] Full provider object:", this.provider);
|
|
2832
|
+
console.error("[WalletConnect] Full session object:", session);
|
|
2833
|
+
}
|
|
2834
|
+
if (error.code === 4001 || error.message && (error.message.includes("User rejected") || error.message.includes("rejected by user") || error.message.includes("User cancelled"))) {
|
|
2835
|
+
throw new ConnectionRejectedError(this.type);
|
|
2836
|
+
}
|
|
2837
|
+
if (isOriginNotAllowed) {
|
|
2838
|
+
throw new ConfigurationError(
|
|
2839
|
+
`WalletConnect relayer rejected this origin (code 3000: Unauthorized: origin not allowed).
|
|
2840
|
+
|
|
2841
|
+
Fix:
|
|
2842
|
+
1) Open WalletConnect Cloud \u2192 your project (${this.projectId})
|
|
2843
|
+
2) Add this site origin to the allowlist:
|
|
2844
|
+
- ${origin || "(unknown origin)"}
|
|
2845
|
+
|
|
2846
|
+
Common dev origins to allow:
|
|
2847
|
+
- http://localhost:5173
|
|
2848
|
+
- http://192.168.0.221:5173 (your LAN dev URL)
|
|
2849
|
+
- https://wallet-test.enclave-hq.com (your Cloudflare Tunnel/custom domain)
|
|
2850
|
+
|
|
2851
|
+
Original error: ${errorMessage}`
|
|
2852
|
+
);
|
|
2853
|
+
}
|
|
2854
|
+
throw error;
|
|
2855
|
+
}
|
|
2856
|
+
}
|
|
2857
|
+
/**
|
|
2858
|
+
* Disconnect wallet
|
|
2859
|
+
*/
|
|
2860
|
+
async disconnect() {
|
|
2861
|
+
if (this.provider) {
|
|
2862
|
+
try {
|
|
2863
|
+
await this.provider.disconnect();
|
|
2864
|
+
} catch (error) {
|
|
2865
|
+
console.warn("[WalletConnect] Error during disconnect:", error);
|
|
2866
|
+
}
|
|
2867
|
+
if (_WalletConnectAdapter.providerInstance === this.provider) {
|
|
2868
|
+
_WalletConnectAdapter.providerInstance = null;
|
|
2869
|
+
_WalletConnectAdapter.providerProjectId = null;
|
|
2870
|
+
_WalletConnectAdapter.providerChains = null;
|
|
2871
|
+
}
|
|
2872
|
+
this.provider = null;
|
|
2873
|
+
}
|
|
2874
|
+
this.removeEventListeners();
|
|
2875
|
+
this.walletClient = null;
|
|
2876
|
+
this.publicClient = null;
|
|
2877
|
+
this.setState("disconnected" /* DISCONNECTED */);
|
|
2878
|
+
this.setAccount(null);
|
|
2879
|
+
this.emitDisconnected();
|
|
2880
|
+
}
|
|
2881
|
+
/**
|
|
2882
|
+
* Sign message
|
|
2883
|
+
*/
|
|
2884
|
+
async signMessage(message) {
|
|
2885
|
+
this.ensureConnected();
|
|
2886
|
+
try {
|
|
2887
|
+
if (!this.provider) {
|
|
2888
|
+
throw new Error("Provider not initialized");
|
|
2889
|
+
}
|
|
2890
|
+
const signature = await this.provider.request({
|
|
2891
|
+
method: "personal_sign",
|
|
2892
|
+
params: [message, this.currentAccount.nativeAddress]
|
|
2893
|
+
});
|
|
2894
|
+
this.closeTelegramDeepLinkPopup();
|
|
2895
|
+
return signature;
|
|
2896
|
+
} catch (error) {
|
|
2897
|
+
if (error.code === 4001 || error.message?.includes("rejected")) {
|
|
2898
|
+
throw new SignatureRejectedError();
|
|
2899
|
+
}
|
|
2900
|
+
throw error;
|
|
2901
|
+
}
|
|
2902
|
+
}
|
|
2903
|
+
/**
|
|
2904
|
+
* Sign TypedData (EIP-712)
|
|
2905
|
+
*/
|
|
2906
|
+
async signTypedData(typedData) {
|
|
2907
|
+
this.ensureConnected();
|
|
2908
|
+
try {
|
|
2909
|
+
if (!this.provider) {
|
|
2910
|
+
throw new Error("Provider not initialized");
|
|
2911
|
+
}
|
|
2912
|
+
const signature = await this.provider.request({
|
|
2913
|
+
method: "eth_signTypedData_v4",
|
|
2914
|
+
params: [this.currentAccount.nativeAddress, JSON.stringify(typedData)]
|
|
2915
|
+
});
|
|
2916
|
+
this.closeTelegramDeepLinkPopup();
|
|
2917
|
+
return signature;
|
|
2918
|
+
} catch (error) {
|
|
2919
|
+
if (error.code === 4001 || error.message?.includes("rejected")) {
|
|
2920
|
+
throw new SignatureRejectedError();
|
|
2921
|
+
}
|
|
2922
|
+
throw error;
|
|
2923
|
+
}
|
|
2924
|
+
}
|
|
2925
|
+
/**
|
|
2926
|
+
* Sign transaction
|
|
2927
|
+
*/
|
|
2928
|
+
async signTransaction(transaction) {
|
|
2929
|
+
this.ensureConnected();
|
|
2930
|
+
try {
|
|
2931
|
+
if (!this.provider) {
|
|
2932
|
+
throw new Error("Provider not initialized");
|
|
2933
|
+
}
|
|
2934
|
+
const tx = {
|
|
2935
|
+
from: this.currentAccount.nativeAddress,
|
|
2936
|
+
to: transaction.to,
|
|
2937
|
+
value: transaction.value ? `0x${BigInt(transaction.value).toString(16)}` : void 0,
|
|
2938
|
+
data: transaction.data || "0x",
|
|
2939
|
+
gas: transaction.gas ? `0x${BigInt(transaction.gas).toString(16)}` : void 0,
|
|
2940
|
+
gasPrice: transaction.gasPrice && transaction.gasPrice !== "auto" ? `0x${BigInt(transaction.gasPrice).toString(16)}` : void 0,
|
|
2941
|
+
maxFeePerGas: transaction.maxFeePerGas ? `0x${BigInt(transaction.maxFeePerGas).toString(16)}` : void 0,
|
|
2942
|
+
maxPriorityFeePerGas: transaction.maxPriorityFeePerGas ? `0x${BigInt(transaction.maxPriorityFeePerGas).toString(16)}` : void 0,
|
|
2943
|
+
nonce: transaction.nonce !== void 0 ? `0x${transaction.nonce.toString(16)}` : void 0,
|
|
2944
|
+
chainId: transaction.chainId || this.currentAccount.chainId
|
|
2945
|
+
};
|
|
2946
|
+
const signature = await this.provider.request({
|
|
2947
|
+
method: "eth_signTransaction",
|
|
2948
|
+
params: [tx]
|
|
2949
|
+
});
|
|
2950
|
+
this.closeTelegramDeepLinkPopup();
|
|
2951
|
+
return signature;
|
|
2952
|
+
} catch (error) {
|
|
2953
|
+
if (error.code === 4001 || error.message?.includes("rejected")) {
|
|
2954
|
+
throw new SignatureRejectedError("Transaction signature was rejected by user");
|
|
2955
|
+
}
|
|
2956
|
+
throw error;
|
|
2957
|
+
}
|
|
2958
|
+
}
|
|
2959
|
+
/**
|
|
2960
|
+
* Get supported chains from current connection
|
|
2961
|
+
* Returns the chains that were requested during connection
|
|
2962
|
+
*/
|
|
2963
|
+
getSupportedChains() {
|
|
2964
|
+
return [...this.supportedChains];
|
|
2965
|
+
}
|
|
2966
|
+
/**
|
|
2967
|
+
* Switch chain
|
|
2968
|
+
*
|
|
2969
|
+
* Note: WalletConnect v2 with mobile wallets may not support chain switching reliably.
|
|
2970
|
+
* Some wallets may ignore the switch request or fail silently.
|
|
2971
|
+
* It's recommended to include all needed chains in the initial connection.
|
|
2972
|
+
*
|
|
2973
|
+
* Reference: https://specs.walletconnect.com/2.0/specs/clients/sign/namespaces
|
|
2974
|
+
*/
|
|
2975
|
+
async switchChain(chainId) {
|
|
2976
|
+
if (!this.provider) {
|
|
2977
|
+
throw new Error("Provider not initialized");
|
|
2978
|
+
}
|
|
2979
|
+
const session = this.provider.session;
|
|
2980
|
+
const supportedChains = session?.namespaces?.eip155?.chains || [];
|
|
2981
|
+
const targetChainCAIP = `eip155:${chainId}`;
|
|
2982
|
+
const isChainApproved = supportedChains.includes(targetChainCAIP);
|
|
2983
|
+
if (!isChainApproved) {
|
|
2984
|
+
console.warn(`[WalletConnect] Chain ${chainId} (${targetChainCAIP}) not in session approved chains:`, supportedChains);
|
|
2985
|
+
console.warn("[WalletConnect] Chain switching may fail. Consider including all chains in initial connection.");
|
|
2986
|
+
}
|
|
2987
|
+
try {
|
|
2988
|
+
console.log(`[WalletConnect] Attempting to switch to chain ${chainId} (${targetChainCAIP})`);
|
|
2989
|
+
const result = await this.provider.request({
|
|
2990
|
+
method: "wallet_switchEthereumChain",
|
|
2991
|
+
params: [{ chainId: `0x${chainId.toString(16)}` }]
|
|
2992
|
+
});
|
|
2993
|
+
if (result !== null && result !== void 0) {
|
|
2994
|
+
console.warn("[WalletConnect] wallet_switchEthereumChain returned non-null result:", result);
|
|
2995
|
+
}
|
|
2996
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
2997
|
+
const currentChainId = this.provider.chainId;
|
|
2998
|
+
if (currentChainId !== chainId) {
|
|
2999
|
+
console.warn(`[WalletConnect] Chain switch may have failed. Expected ${chainId}, got ${currentChainId}`);
|
|
3000
|
+
console.warn("[WalletConnect] Some mobile wallets may not support chain switching via WalletConnect.");
|
|
3001
|
+
console.warn("[WalletConnect] User may need to manually switch chains in the wallet app.");
|
|
3002
|
+
}
|
|
3003
|
+
if (this.currentAccount) {
|
|
3004
|
+
const updatedAccount = {
|
|
3005
|
+
...this.currentAccount,
|
|
3006
|
+
chainId,
|
|
3007
|
+
universalAddress: createUniversalAddress(chainId, this.currentAccount.nativeAddress)
|
|
3008
|
+
};
|
|
3009
|
+
this.setAccount(updatedAccount);
|
|
3010
|
+
this.emitChainChanged(chainId);
|
|
3011
|
+
const viemChain = this.getViemChain(chainId);
|
|
3012
|
+
const chainInfo = getChainInfo(chainId);
|
|
3013
|
+
const primaryRpcUrl = chainInfo?.rpcUrls[0];
|
|
3014
|
+
this.walletClient = createWalletClient({
|
|
3015
|
+
account: this.currentAccount.nativeAddress,
|
|
3016
|
+
chain: viemChain,
|
|
3017
|
+
transport: custom(this.provider)
|
|
3018
|
+
});
|
|
3019
|
+
this.publicClient = createPublicClient({
|
|
3020
|
+
chain: viemChain,
|
|
3021
|
+
transport: primaryRpcUrl ? http(primaryRpcUrl) : custom(this.provider)
|
|
3022
|
+
});
|
|
3023
|
+
}
|
|
3024
|
+
} catch (error) {
|
|
3025
|
+
console.error("[WalletConnect] Chain switch error:", {
|
|
3026
|
+
chainId,
|
|
3027
|
+
errorCode: error.code,
|
|
3028
|
+
errorMessage: error.message,
|
|
3029
|
+
supportedChains
|
|
3030
|
+
});
|
|
3031
|
+
if (error.code === 4902) {
|
|
3032
|
+
console.log(`[WalletConnect] Chain ${chainId} not found in wallet, attempting to add...`);
|
|
3033
|
+
const chainInfo = getChainInfo(chainId);
|
|
3034
|
+
if (chainInfo) {
|
|
3035
|
+
try {
|
|
3036
|
+
await this.addChain({
|
|
3037
|
+
chainId: chainInfo.id,
|
|
3038
|
+
chainName: chainInfo.name,
|
|
3039
|
+
nativeCurrency: chainInfo.nativeCurrency,
|
|
3040
|
+
rpcUrls: chainInfo.rpcUrls,
|
|
3041
|
+
blockExplorerUrls: chainInfo.blockExplorerUrls
|
|
3042
|
+
});
|
|
3043
|
+
console.log(`[WalletConnect] Chain added, attempting to switch again...`);
|
|
3044
|
+
await this.switchChain(chainId);
|
|
3045
|
+
} catch (addError) {
|
|
3046
|
+
console.error("[WalletConnect] Failed to add chain:", addError);
|
|
3047
|
+
throw new Error(`Failed to add chain ${chainId}: ${addError.message}`);
|
|
3048
|
+
}
|
|
3049
|
+
} else {
|
|
3050
|
+
throw new Error(`Chain ${chainId} not supported`);
|
|
3051
|
+
}
|
|
3052
|
+
} else if (error.code === 4001) {
|
|
3053
|
+
throw new Error("User rejected chain switch");
|
|
3054
|
+
} else if (error.code === 4100) {
|
|
3055
|
+
throw new Error("Wallet does not support wallet_switchEthereumChain. Please switch chains manually in your wallet app.");
|
|
3056
|
+
} else {
|
|
3057
|
+
console.warn("[WalletConnect] Chain switch may not be supported by this wallet. User may need to switch manually.");
|
|
3058
|
+
throw error;
|
|
3059
|
+
}
|
|
3060
|
+
}
|
|
3061
|
+
}
|
|
3062
|
+
/**
|
|
3063
|
+
* Add chain
|
|
3064
|
+
*/
|
|
3065
|
+
async addChain(chainConfig) {
|
|
3066
|
+
if (!this.provider) {
|
|
3067
|
+
throw new Error("Provider not initialized");
|
|
3068
|
+
}
|
|
3069
|
+
await this.provider.request({
|
|
3070
|
+
method: "wallet_addEthereumChain",
|
|
3071
|
+
params: [{
|
|
3072
|
+
chainId: `0x${chainConfig.chainId.toString(16)}`,
|
|
3073
|
+
chainName: chainConfig.chainName,
|
|
3074
|
+
nativeCurrency: chainConfig.nativeCurrency,
|
|
3075
|
+
rpcUrls: chainConfig.rpcUrls,
|
|
3076
|
+
blockExplorerUrls: chainConfig.blockExplorerUrls
|
|
3077
|
+
}]
|
|
3078
|
+
});
|
|
3079
|
+
}
|
|
3080
|
+
/**
|
|
3081
|
+
* Read contract
|
|
3082
|
+
*/
|
|
3083
|
+
async readContract(params) {
|
|
3084
|
+
if (!this.publicClient) {
|
|
3085
|
+
throw new Error("Public client not initialized");
|
|
3086
|
+
}
|
|
3087
|
+
const result = await this.publicClient.readContract({
|
|
3088
|
+
address: params.address,
|
|
3089
|
+
abi: params.abi,
|
|
3090
|
+
functionName: params.functionName,
|
|
3091
|
+
...params.args ? { args: params.args } : {}
|
|
3092
|
+
});
|
|
3093
|
+
return result;
|
|
3094
|
+
}
|
|
3095
|
+
/**
|
|
3096
|
+
* Write contract
|
|
3097
|
+
*/
|
|
3098
|
+
async writeContract(params) {
|
|
3099
|
+
this.ensureConnected();
|
|
3100
|
+
if (!this.walletClient) {
|
|
3101
|
+
throw new Error("Wallet client not initialized");
|
|
3102
|
+
}
|
|
3103
|
+
try {
|
|
3104
|
+
const txOptions = {
|
|
3105
|
+
address: params.address,
|
|
3106
|
+
abi: params.abi,
|
|
3107
|
+
functionName: params.functionName,
|
|
3108
|
+
...params.args ? { args: params.args } : {},
|
|
3109
|
+
value: params.value ? BigInt(params.value) : void 0,
|
|
3110
|
+
gas: params.gas ? BigInt(params.gas) : void 0
|
|
3111
|
+
};
|
|
3112
|
+
if (params.maxFeePerGas || params.maxPriorityFeePerGas) {
|
|
3113
|
+
if (params.maxFeePerGas) {
|
|
3114
|
+
txOptions.maxFeePerGas = BigInt(params.maxFeePerGas);
|
|
3115
|
+
}
|
|
3116
|
+
if (params.maxPriorityFeePerGas) {
|
|
3117
|
+
txOptions.maxPriorityFeePerGas = BigInt(params.maxPriorityFeePerGas);
|
|
3118
|
+
}
|
|
3119
|
+
} else if (params.gasPrice && params.gasPrice !== "auto") {
|
|
3120
|
+
txOptions.gasPrice = BigInt(params.gasPrice);
|
|
3121
|
+
} else {
|
|
3122
|
+
if (this.publicClient) {
|
|
3123
|
+
try {
|
|
3124
|
+
const feesPerGas = await this.publicClient.estimateFeesPerGas().catch(() => null);
|
|
3125
|
+
if (feesPerGas) {
|
|
3126
|
+
const minPriorityFeeWei = BigInt(1e8);
|
|
3127
|
+
const maxPriorityFeePerGas = feesPerGas.maxPriorityFeePerGas > minPriorityFeeWei ? feesPerGas.maxPriorityFeePerGas : minPriorityFeeWei;
|
|
3128
|
+
const adjustedMaxFeePerGas = feesPerGas.maxFeePerGas > maxPriorityFeePerGas ? feesPerGas.maxFeePerGas : maxPriorityFeePerGas + BigInt(1e9);
|
|
3129
|
+
txOptions.maxFeePerGas = adjustedMaxFeePerGas;
|
|
3130
|
+
txOptions.maxPriorityFeePerGas = maxPriorityFeePerGas;
|
|
3131
|
+
} else {
|
|
3132
|
+
const gasPrice = await this.publicClient.getGasPrice();
|
|
3133
|
+
txOptions.gasPrice = gasPrice;
|
|
3134
|
+
}
|
|
3135
|
+
} catch (err) {
|
|
3136
|
+
}
|
|
3137
|
+
}
|
|
3138
|
+
}
|
|
3139
|
+
const txHash = await this.walletClient.writeContract(txOptions);
|
|
3140
|
+
this.closeTelegramDeepLinkPopup();
|
|
3141
|
+
return txHash;
|
|
3142
|
+
} catch (error) {
|
|
3143
|
+
if (error.code === 4001 || error.message?.includes("rejected")) {
|
|
3144
|
+
throw new SignatureRejectedError("Transaction was rejected by user");
|
|
3145
|
+
}
|
|
3146
|
+
throw error;
|
|
3147
|
+
}
|
|
3148
|
+
}
|
|
3149
|
+
/**
|
|
3150
|
+
* Estimate gas
|
|
3151
|
+
*/
|
|
3152
|
+
async estimateGas(params) {
|
|
3153
|
+
if (!this.publicClient) {
|
|
3154
|
+
throw new Error("Public client not initialized");
|
|
3155
|
+
}
|
|
3156
|
+
const gas = await this.publicClient.estimateContractGas({
|
|
3157
|
+
address: params.address,
|
|
3158
|
+
abi: params.abi,
|
|
3159
|
+
functionName: params.functionName,
|
|
3160
|
+
...params.args ? { args: params.args } : {},
|
|
3161
|
+
value: params.value ? BigInt(params.value) : void 0,
|
|
3162
|
+
account: this.currentAccount.nativeAddress
|
|
3163
|
+
});
|
|
3164
|
+
return gas;
|
|
3165
|
+
}
|
|
3166
|
+
/**
|
|
3167
|
+
* Wait for transaction
|
|
3168
|
+
*/
|
|
3169
|
+
async waitForTransaction(txHash, confirmations = 1) {
|
|
3170
|
+
if (!this.publicClient) {
|
|
3171
|
+
throw new Error("Public client not initialized");
|
|
3172
|
+
}
|
|
3173
|
+
const receipt = await this.publicClient.waitForTransactionReceipt({
|
|
3174
|
+
hash: txHash,
|
|
3175
|
+
confirmations
|
|
3176
|
+
});
|
|
3177
|
+
if (receipt.status === "reverted") {
|
|
3178
|
+
throw new TransactionFailedError(txHash, "Transaction reverted");
|
|
3179
|
+
}
|
|
3180
|
+
return {
|
|
3181
|
+
transactionHash: receipt.transactionHash,
|
|
3182
|
+
blockNumber: Number(receipt.blockNumber),
|
|
3183
|
+
blockHash: receipt.blockHash,
|
|
3184
|
+
from: receipt.from,
|
|
3185
|
+
to: receipt.to || void 0,
|
|
3186
|
+
status: receipt.status === "success" ? "success" : "failed",
|
|
3187
|
+
gasUsed: receipt.gasUsed.toString(),
|
|
3188
|
+
effectiveGasPrice: receipt.effectiveGasPrice?.toString(),
|
|
3189
|
+
logs: receipt.logs
|
|
3190
|
+
};
|
|
3191
|
+
}
|
|
3192
|
+
/**
|
|
3193
|
+
* Get provider
|
|
3194
|
+
*/
|
|
3195
|
+
getProvider() {
|
|
3196
|
+
return this.provider;
|
|
3197
|
+
}
|
|
3198
|
+
/**
|
|
3199
|
+
* Get signer
|
|
3200
|
+
*/
|
|
3201
|
+
getSigner() {
|
|
3202
|
+
return this.walletClient;
|
|
3203
|
+
}
|
|
3204
|
+
/**
|
|
3205
|
+
* Setup event listeners
|
|
3206
|
+
*/
|
|
3207
|
+
setupEventListeners() {
|
|
3208
|
+
if (!this.provider) return;
|
|
3209
|
+
this.provider.on("accountsChanged", this.handleAccountsChanged);
|
|
3210
|
+
this.provider.on("chainChanged", this.handleChainChanged);
|
|
3211
|
+
this.provider.on("disconnect", this.handleDisconnect);
|
|
3212
|
+
}
|
|
3213
|
+
/**
|
|
3214
|
+
* Remove event listeners
|
|
3215
|
+
*/
|
|
3216
|
+
removeEventListeners() {
|
|
3217
|
+
if (!this.provider) return;
|
|
3218
|
+
this.provider.removeListener("accountsChanged", this.handleAccountsChanged);
|
|
3219
|
+
this.provider.removeListener("chainChanged", this.handleChainChanged);
|
|
3220
|
+
this.provider.removeListener("disconnect", this.handleDisconnect);
|
|
3221
|
+
}
|
|
3222
|
+
/**
|
|
3223
|
+
* Get viem chain config
|
|
3224
|
+
*/
|
|
3225
|
+
getViemChain(chainId) {
|
|
3226
|
+
const chainInfo = getChainInfo(chainId);
|
|
3227
|
+
if (chainInfo) {
|
|
3228
|
+
return {
|
|
3229
|
+
id: chainId,
|
|
3230
|
+
name: chainInfo.name,
|
|
3231
|
+
network: chainInfo.name.toLowerCase().replace(/\s+/g, "-"),
|
|
3232
|
+
nativeCurrency: chainInfo.nativeCurrency,
|
|
3233
|
+
rpcUrls: {
|
|
3234
|
+
default: { http: chainInfo.rpcUrls },
|
|
3235
|
+
public: { http: chainInfo.rpcUrls }
|
|
3236
|
+
},
|
|
3237
|
+
blockExplorers: chainInfo.blockExplorerUrls ? {
|
|
3238
|
+
default: { name: "Explorer", url: chainInfo.blockExplorerUrls[0] }
|
|
3239
|
+
} : void 0
|
|
3240
|
+
};
|
|
3241
|
+
}
|
|
3242
|
+
return {
|
|
3243
|
+
id: chainId,
|
|
3244
|
+
name: `Chain ${chainId}`,
|
|
3245
|
+
network: `chain-${chainId}`,
|
|
3246
|
+
nativeCurrency: {
|
|
3247
|
+
name: "ETH",
|
|
3248
|
+
symbol: "ETH",
|
|
3249
|
+
decimals: 18
|
|
3250
|
+
},
|
|
3251
|
+
rpcUrls: {
|
|
3252
|
+
default: { http: [] },
|
|
3253
|
+
public: { http: [] }
|
|
3254
|
+
}
|
|
3255
|
+
};
|
|
3256
|
+
}
|
|
3257
|
+
};
|
|
3258
|
+
// Store supported chains from connection
|
|
3259
|
+
// Static provider instance to avoid multiple initializations
|
|
3260
|
+
_WalletConnectAdapter.providerInstance = null;
|
|
3261
|
+
_WalletConnectAdapter.providerProjectId = null;
|
|
3262
|
+
_WalletConnectAdapter.providerChains = null;
|
|
3263
|
+
// Store the chains used during initialization
|
|
3264
|
+
_WalletConnectAdapter.isInitializing = false;
|
|
3265
|
+
_WalletConnectAdapter.initPromise = null;
|
|
3266
|
+
var WalletConnectAdapter = _WalletConnectAdapter;
|
|
3267
|
+
init_types();
|
|
3268
|
+
var _WalletConnectTronAdapter = class _WalletConnectTronAdapter extends WalletAdapter {
|
|
3269
|
+
constructor(projectId) {
|
|
3270
|
+
super();
|
|
3271
|
+
this.type = "walletconnect-tron" /* WALLETCONNECT_TRON */;
|
|
3272
|
+
this.chainType = ChainType.TRON;
|
|
3273
|
+
this.name = "WalletConnect (Tron)";
|
|
3274
|
+
this.icon = "https://avatars.githubusercontent.com/u/37784886";
|
|
3275
|
+
this.wallet = null;
|
|
3276
|
+
this.currentAddress = null;
|
|
3277
|
+
if (!projectId) {
|
|
3278
|
+
throw new ConfigurationError("WalletConnect projectId is required");
|
|
3279
|
+
}
|
|
3280
|
+
this.projectId = projectId;
|
|
3281
|
+
}
|
|
3282
|
+
/**
|
|
3283
|
+
* Check if WalletConnect is available
|
|
3284
|
+
*/
|
|
3285
|
+
async isAvailable() {
|
|
3286
|
+
return typeof window !== "undefined";
|
|
3287
|
+
}
|
|
3288
|
+
/**
|
|
3289
|
+
* Check if running in Telegram environment (Mini App or Web)
|
|
3290
|
+
* Both Telegram Mini App (in client) and Telegram Web (web.telegram.org)
|
|
3291
|
+
* provide window.Telegram.WebApp API, so they are treated the same way.
|
|
3292
|
+
*/
|
|
3293
|
+
isTelegramMiniApp() {
|
|
3294
|
+
if (typeof window === "undefined") return false;
|
|
3295
|
+
const tg = window.Telegram?.WebApp;
|
|
3296
|
+
if (!tg) return false;
|
|
3297
|
+
const platform = tg.platform || "unknown";
|
|
3298
|
+
console.log("[WalletConnect Tron] Telegram environment detected:", {
|
|
3299
|
+
platform,
|
|
3300
|
+
version: tg.version,
|
|
3301
|
+
isMiniApp: platform !== "web",
|
|
3302
|
+
// Mini App if not web platform
|
|
3303
|
+
isWeb: platform === "web"
|
|
3304
|
+
// Telegram Web if web platform
|
|
3305
|
+
});
|
|
3306
|
+
return true;
|
|
3307
|
+
}
|
|
3308
|
+
/**
|
|
3309
|
+
* Restore session from existing wallet (for storage restoration)
|
|
3310
|
+
*/
|
|
3311
|
+
async restoreSession(chainId) {
|
|
3312
|
+
if (typeof window === "undefined") {
|
|
3313
|
+
return null;
|
|
3314
|
+
}
|
|
3315
|
+
try {
|
|
3316
|
+
const targetChainId = Array.isArray(chainId) ? chainId[0] || _WalletConnectTronAdapter.TRON_MAINNET_CHAIN_ID : chainId || _WalletConnectTronAdapter.TRON_MAINNET_CHAIN_ID;
|
|
3317
|
+
if (!_WalletConnectTronAdapter.walletInstance || _WalletConnectTronAdapter.walletProjectId !== this.projectId) {
|
|
3318
|
+
this.initializeWallet(targetChainId);
|
|
3319
|
+
}
|
|
3320
|
+
this.wallet = _WalletConnectTronAdapter.walletInstance;
|
|
3321
|
+
if (!this.wallet) {
|
|
3322
|
+
return null;
|
|
3323
|
+
}
|
|
3324
|
+
const status = await this.wallet.checkConnectStatus();
|
|
3325
|
+
if (status && status.address) {
|
|
3326
|
+
this.currentAddress = status.address;
|
|
3327
|
+
const account = {
|
|
3328
|
+
universalAddress: createUniversalAddress(targetChainId, status.address),
|
|
3329
|
+
nativeAddress: status.address,
|
|
3330
|
+
chainId: targetChainId,
|
|
3331
|
+
chainType: ChainType.TRON,
|
|
3332
|
+
isActive: true
|
|
3333
|
+
};
|
|
3334
|
+
this.setState("connected" /* CONNECTED */);
|
|
3335
|
+
this.setAccount(account);
|
|
3336
|
+
this.setupEventListeners();
|
|
3337
|
+
return account;
|
|
3338
|
+
}
|
|
3339
|
+
return null;
|
|
3340
|
+
} catch (error) {
|
|
3341
|
+
console.debug("[WalletConnect Tron] Restore session failed:", error);
|
|
3342
|
+
return null;
|
|
3343
|
+
}
|
|
3344
|
+
}
|
|
3345
|
+
/**
|
|
3346
|
+
* Initialize WalletConnect wallet instance
|
|
3347
|
+
* @param chainId - Optional chain ID to determine network (default: Mainnet)
|
|
3348
|
+
*/
|
|
3349
|
+
initializeWallet(chainId) {
|
|
3350
|
+
if (_WalletConnectTronAdapter.walletInstance && _WalletConnectTronAdapter.walletProjectId === this.projectId) {
|
|
3351
|
+
return;
|
|
3352
|
+
}
|
|
3353
|
+
let appUrl = "";
|
|
3354
|
+
if (typeof window !== "undefined") {
|
|
3355
|
+
try {
|
|
3356
|
+
if (window.location && window.location.origin) {
|
|
3357
|
+
appUrl = window.location.origin;
|
|
3358
|
+
} else if (window.location && window.location.href) {
|
|
3359
|
+
const url = new URL(window.location.href);
|
|
3360
|
+
appUrl = url.origin;
|
|
3361
|
+
}
|
|
3362
|
+
} catch (error) {
|
|
3363
|
+
console.warn("[WalletConnect Tron] Failed to get origin from window.location:", error);
|
|
3364
|
+
}
|
|
3365
|
+
if (appUrl && (appUrl.includes("serveo.net") || appUrl.includes("loca.lt") || appUrl.includes("ngrok.io") || appUrl.includes("ngrok-free.app") || appUrl.includes("cloudflared.io"))) {
|
|
3366
|
+
console.log("[WalletConnect Tron] Detected tunnel service URL:", appUrl);
|
|
3367
|
+
console.log("[WalletConnect Tron] \u26A0\uFE0F Make sure this URL is added to WalletConnect Cloud project allowlist");
|
|
3368
|
+
}
|
|
3369
|
+
if (!appUrl) {
|
|
3370
|
+
const tg = window.Telegram?.WebApp;
|
|
3371
|
+
if (tg && tg.initDataUnsafe?.start_param) {
|
|
3372
|
+
appUrl = "https://enclave.network";
|
|
3373
|
+
} else {
|
|
3374
|
+
appUrl = "https://enclave.network";
|
|
3375
|
+
}
|
|
3376
|
+
}
|
|
3377
|
+
} else {
|
|
3378
|
+
appUrl = "https://enclave.network";
|
|
3379
|
+
}
|
|
3380
|
+
if (!appUrl || !appUrl.startsWith("http://") && !appUrl.startsWith("https://")) {
|
|
3381
|
+
appUrl = "https://enclave.network";
|
|
3382
|
+
}
|
|
3383
|
+
const icons = [
|
|
3384
|
+
"https://walletconnect.com/walletconnect-logo.svg",
|
|
3385
|
+
"https://avatars.githubusercontent.com/u/37784886"
|
|
3386
|
+
// WalletConnect GitHub avatar
|
|
3387
|
+
];
|
|
3388
|
+
let network = WalletConnectChainID.Mainnet;
|
|
3389
|
+
if (chainId !== void 0) {
|
|
3390
|
+
if (chainId === 195 || chainId === _WalletConnectTronAdapter.TRON_MAINNET_CHAIN_ID) {
|
|
3391
|
+
network = WalletConnectChainID.Mainnet;
|
|
3392
|
+
} else if (chainId === 201910292) {
|
|
3393
|
+
network = WalletConnectChainID.Shasta;
|
|
3394
|
+
} else if (chainId === 2494104990) {
|
|
3395
|
+
network = WalletConnectChainID.Nile;
|
|
3396
|
+
}
|
|
3397
|
+
}
|
|
3398
|
+
const metadataInfo = {
|
|
3399
|
+
name: "Enclave Wallet SDK",
|
|
3400
|
+
description: "Multi-chain wallet adapter for Enclave",
|
|
3401
|
+
url: appUrl,
|
|
3402
|
+
icons,
|
|
3403
|
+
network,
|
|
3404
|
+
chainId,
|
|
3405
|
+
isTelegram: this.isTelegramMiniApp(),
|
|
3406
|
+
projectId: this.projectId,
|
|
3407
|
+
urlValid: appUrl && (appUrl.startsWith("http://") || appUrl.startsWith("https://")),
|
|
3408
|
+
iconsValid: icons && icons.length > 0 && icons.every((icon) => icon && icon.startsWith("http")),
|
|
3409
|
+
currentLocation: typeof window !== "undefined" ? window.location.href : "N/A",
|
|
3410
|
+
telegramPlatform: typeof window !== "undefined" && window.Telegram?.WebApp?.platform || "N/A"
|
|
3411
|
+
};
|
|
3412
|
+
console.log("[WalletConnect Tron] Initializing with metadata:", metadataInfo);
|
|
3413
|
+
if (!metadataInfo.urlValid) {
|
|
3414
|
+
console.warn("[WalletConnect Tron] \u26A0\uFE0F Invalid URL in metadata:", appUrl);
|
|
3415
|
+
}
|
|
3416
|
+
if (!metadataInfo.iconsValid) {
|
|
3417
|
+
console.warn("[WalletConnect Tron] \u26A0\uFE0F Invalid icons in metadata:", icons);
|
|
3418
|
+
}
|
|
3419
|
+
console.log("[WalletConnect Tron] Initializing wallet...", {
|
|
3420
|
+
network,
|
|
3421
|
+
chainId,
|
|
3422
|
+
note: "If no wallets are in WalletConnect Explorer for TRON, QR code will be displayed for scanning"
|
|
3423
|
+
});
|
|
3424
|
+
_WalletConnectTronAdapter.walletInstance = new WalletConnectWallet({
|
|
3425
|
+
network,
|
|
3426
|
+
options: {
|
|
3427
|
+
projectId: this.projectId,
|
|
3428
|
+
metadata: {
|
|
3429
|
+
name: "Enclave Wallet SDK",
|
|
3430
|
+
description: "Multi-chain wallet adapter for Enclave",
|
|
3431
|
+
url: appUrl,
|
|
3432
|
+
icons
|
|
3433
|
+
}
|
|
3434
|
+
},
|
|
3435
|
+
// Theme configuration
|
|
3436
|
+
themeMode: "light",
|
|
3437
|
+
themeVariables: {
|
|
3438
|
+
"--w3m-z-index": 1e4
|
|
3439
|
+
// Ensure modal appears above Telegram UI
|
|
3440
|
+
},
|
|
3441
|
+
// Web3Modal configuration for recommended wallets
|
|
3442
|
+
// According to official docs: https://developers.tron.network/docs/walletconnect-tron
|
|
3443
|
+
// Note: If no wallets are registered in WalletConnect Explorer for TRON,
|
|
3444
|
+
// explorerRecommendedWalletIds will have no effect, and QR code will be shown instead.
|
|
3445
|
+
// @ts-ignore - web3ModalConfig is supported but may not be in TypeScript types
|
|
3446
|
+
web3ModalConfig: {
|
|
3447
|
+
themeMode: "light",
|
|
3448
|
+
themeVariables: {
|
|
3449
|
+
"--w3m-z-index": 1e4
|
|
3450
|
+
},
|
|
3451
|
+
/**
|
|
3452
|
+
* Recommended Wallets are fetched from WalletConnect explore api:
|
|
3453
|
+
* https://walletconnect.com/explorer?type=wallet&version=2
|
|
3454
|
+
*
|
|
3455
|
+
* IMPORTANT: If wallets are not registered in Explorer for TRON, this list will be ignored.
|
|
3456
|
+
* The AppKit will show a QR code instead, which users can scan with any WalletConnect-compatible wallet.
|
|
3457
|
+
*
|
|
3458
|
+
* Wallet IDs (for reference, may not work if not in Explorer):
|
|
3459
|
+
* - TokenPocket: 20459438007b75f4f4acb98bf29aa3b800550309646d375da5fd4aac6c2a2c66
|
|
3460
|
+
* - TronLink: 1ae92b26df02f0abca6304df07debccd18262fdf5fe82daa81593582dac9a369
|
|
3461
|
+
*/
|
|
3462
|
+
explorerRecommendedWalletIds: [
|
|
3463
|
+
// These IDs are kept for when wallets register in WalletConnect Explorer
|
|
3464
|
+
// Currently, if no TRON wallets are in Explorer, QR code will be shown
|
|
3465
|
+
"20459438007b75f4f4acb98bf29aa3b800550309646d375da5fd4aac6c2a2c66",
|
|
3466
|
+
// TokenPocket
|
|
3467
|
+
"1ae92b26df02f0abca6304df07debccd18262fdf5fe82daa81593582dac9a369",
|
|
3468
|
+
// TronLink
|
|
3469
|
+
"4622a2b2d6af1c9844944291e5e7351a6aa24cd7b23099efac1b2fd875da31a0"
|
|
3470
|
+
// TokenPocket (backup)
|
|
3471
|
+
]
|
|
3472
|
+
}
|
|
3473
|
+
});
|
|
3474
|
+
_WalletConnectTronAdapter.walletProjectId = this.projectId;
|
|
3475
|
+
}
|
|
3476
|
+
/**
|
|
3477
|
+
* Connect wallet
|
|
3478
|
+
*/
|
|
3479
|
+
async connect(chainId) {
|
|
3480
|
+
if (typeof window === "undefined") {
|
|
3481
|
+
throw new Error("WalletConnect requires a browser environment");
|
|
3482
|
+
}
|
|
3483
|
+
const currentState = this.state;
|
|
3484
|
+
if (currentState === "connecting" /* CONNECTING */) {
|
|
3485
|
+
console.warn("[WalletConnect Tron] Connection already in progress, waiting...");
|
|
3486
|
+
let attempts = 0;
|
|
3487
|
+
while (this.state === "connecting" /* CONNECTING */ && attempts < 50) {
|
|
3488
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
3489
|
+
attempts++;
|
|
3490
|
+
}
|
|
3491
|
+
if (this.state === "connected" /* CONNECTED */ && this.currentAccount) {
|
|
3492
|
+
return this.currentAccount;
|
|
3493
|
+
}
|
|
3494
|
+
if (this.state === "connecting" /* CONNECTING */) {
|
|
3495
|
+
throw new Error("Connection timeout - previous connection attempt is still pending");
|
|
3496
|
+
}
|
|
3497
|
+
}
|
|
3498
|
+
if (this.state === "connected" /* CONNECTED */ && this.currentAccount) {
|
|
3499
|
+
return this.currentAccount;
|
|
3500
|
+
}
|
|
3501
|
+
try {
|
|
3502
|
+
this.setState("connecting" /* CONNECTING */);
|
|
3503
|
+
const targetChainId = Array.isArray(chainId) ? chainId[0] || _WalletConnectTronAdapter.TRON_MAINNET_CHAIN_ID : chainId || _WalletConnectTronAdapter.TRON_MAINNET_CHAIN_ID;
|
|
3504
|
+
if (!_WalletConnectTronAdapter.walletInstance || _WalletConnectTronAdapter.walletProjectId !== this.projectId) {
|
|
3505
|
+
this.initializeWallet(targetChainId);
|
|
3506
|
+
}
|
|
3507
|
+
this.wallet = _WalletConnectTronAdapter.walletInstance;
|
|
3508
|
+
if (!this.wallet) {
|
|
3509
|
+
throw new Error("Failed to initialize WalletConnect wallet");
|
|
3510
|
+
}
|
|
3511
|
+
let network = WalletConnectChainID.Mainnet;
|
|
3512
|
+
if (targetChainId === 195) {
|
|
3513
|
+
network = WalletConnectChainID.Mainnet;
|
|
3514
|
+
} else if (targetChainId === 201910292) {
|
|
3515
|
+
network = WalletConnectChainID.Shasta;
|
|
3516
|
+
} else if (targetChainId === 2494104990) {
|
|
3517
|
+
network = WalletConnectChainID.Nile;
|
|
3518
|
+
}
|
|
3519
|
+
let address;
|
|
3520
|
+
try {
|
|
3521
|
+
console.log("[WalletConnect Tron] Attempting to connect...", {
|
|
3522
|
+
network,
|
|
3523
|
+
chainId: targetChainId,
|
|
3524
|
+
isTelegram: this.isTelegramMiniApp(),
|
|
3525
|
+
projectId: this.projectId
|
|
3526
|
+
});
|
|
3527
|
+
const result = await this.wallet.connect();
|
|
3528
|
+
address = result.address;
|
|
3529
|
+
if (!address) {
|
|
3530
|
+
throw new ConnectionRejectedError(this.type);
|
|
3531
|
+
}
|
|
3532
|
+
console.log("[WalletConnect Tron] Connection successful:", {
|
|
3533
|
+
address,
|
|
3534
|
+
network,
|
|
3535
|
+
chainId: targetChainId,
|
|
3536
|
+
isTelegram: this.isTelegramMiniApp()
|
|
3537
|
+
});
|
|
3538
|
+
} catch (error) {
|
|
3539
|
+
const errorMessage = error.message || String(error);
|
|
3540
|
+
const errorCode = error.code || error.error?.code;
|
|
3541
|
+
const origin = typeof window !== "undefined" && window.location ? window.location.origin : "";
|
|
3542
|
+
let detailedError = errorMessage;
|
|
3543
|
+
if (error.error) {
|
|
3544
|
+
if (typeof error.error === "string") {
|
|
3545
|
+
detailedError = error.error;
|
|
3546
|
+
} else if (error.error.message) {
|
|
3547
|
+
detailedError = error.error.message;
|
|
3548
|
+
} else if (error.error.data) {
|
|
3549
|
+
detailedError = JSON.stringify(error.error.data);
|
|
3550
|
+
}
|
|
3551
|
+
}
|
|
3552
|
+
const isNoWalletFound = errorMessage.includes("\u6CA1\u6709\u627E\u5230\u652F\u6301\u7684\u94B1\u5305") || errorMessage.includes("No matching wallet") || errorMessage.includes("No wallet found") || errorMessage.includes("\u627E\u4E0D\u5230\u94B1\u5305") || errorMessage.includes("not found") || errorMessage.includes("no matching");
|
|
3553
|
+
const isTimeout = errorMessage.includes("timeout") || errorMessage.includes("\u8D85\u65F6") || errorCode === "TIMEOUT";
|
|
3554
|
+
const isRejected = errorMessage.includes("rejected") || errorMessage.includes("\u62D2\u7EDD") || errorCode === 4001;
|
|
3555
|
+
const isOriginNotAllowed = errorCode === 3e3 || /origin not allowed/i.test(errorMessage) || /Unauthorized:\s*origin not allowed/i.test(errorMessage);
|
|
3556
|
+
const currentMetadata = this.wallet ? {
|
|
3557
|
+
// Try to get metadata from wallet instance if available
|
|
3558
|
+
projectId: this.projectId,
|
|
3559
|
+
network
|
|
3560
|
+
} : null;
|
|
3561
|
+
const errorDetails = {
|
|
3562
|
+
error: errorMessage,
|
|
3563
|
+
detailedError,
|
|
3564
|
+
code: errorCode,
|
|
3565
|
+
isTelegram: this.isTelegramMiniApp(),
|
|
3566
|
+
network,
|
|
3567
|
+
chainId: targetChainId,
|
|
3568
|
+
projectId: this.projectId,
|
|
3569
|
+
metadata: currentMetadata,
|
|
3570
|
+
// Get URL from window.location if available
|
|
3571
|
+
currentUrl: typeof window !== "undefined" ? window.location.href : "N/A",
|
|
3572
|
+
telegramPlatform: typeof window !== "undefined" && window.Telegram?.WebApp?.platform || "N/A",
|
|
3573
|
+
errorType: isNoWalletFound ? "NO_WALLET_FOUND" : isTimeout ? "TIMEOUT" : isRejected ? "REJECTED" : "UNKNOWN"
|
|
3574
|
+
};
|
|
3575
|
+
console.error("[WalletConnect Tron] Connection error - Full details:", errorDetails);
|
|
3576
|
+
console.error("[WalletConnect Tron] Error object:", error);
|
|
3577
|
+
console.error("[WalletConnect Tron] Error stack:", error.stack);
|
|
3578
|
+
if (isNoWalletFound) {
|
|
3579
|
+
const noWalletErrorDetails = [
|
|
3580
|
+
`
|
|
3581
|
+
=== WalletConnect Tron: No Matching Wallet Found ===`,
|
|
3582
|
+
`Error: ${errorMessage}`,
|
|
3583
|
+
`Detailed: ${detailedError}`,
|
|
3584
|
+
`Code: ${errorCode || "N/A"}`,
|
|
3585
|
+
``,
|
|
3586
|
+
`Environment:`,
|
|
3587
|
+
` - Telegram Mini App: ${this.isTelegramMiniApp() ? "Yes" : "No"}`,
|
|
3588
|
+
` - Platform: ${errorDetails.telegramPlatform}`,
|
|
3589
|
+
` - Current URL: ${errorDetails.currentUrl}`,
|
|
3590
|
+
``,
|
|
3591
|
+
`Configuration:`,
|
|
3592
|
+
` - Project ID: ${this.projectId ? "Set" : "Missing"}`,
|
|
3593
|
+
` - Network: ${network}`,
|
|
3594
|
+
` - Chain ID: ${targetChainId}`,
|
|
3595
|
+
` - Metadata URL: ${typeof window !== "undefined" ? window.location.origin : "N/A"}`,
|
|
3596
|
+
``,
|
|
3597
|
+
`Possible Causes:`,
|
|
3598
|
+
` 1. No WalletConnect-compatible wallet (TokenPocket, etc.) installed on device`,
|
|
3599
|
+
` 2. Wallet app not opened or not responding to deep link (wc://)`,
|
|
3600
|
+
` 3. Deep link handling issue in Telegram Mini App environment`,
|
|
3601
|
+
` 4. WalletConnect session timeout (user took too long to approve)`,
|
|
3602
|
+
` 5. Network connectivity issue preventing WalletConnect relay connection`,
|
|
3603
|
+
``,
|
|
3604
|
+
`Solutions:`,
|
|
3605
|
+
` 1. Ensure TokenPocket or other WalletConnect-compatible wallet is installed`,
|
|
3606
|
+
` 2. Try opening the wallet app manually before connecting`,
|
|
3607
|
+
` 3. In Telegram Mini App, ensure the deep link popup is not blocked`,
|
|
3608
|
+
` 4. Try connecting again (may need to wait a few seconds)`,
|
|
3609
|
+
` 5. Check network connection and WalletConnect relay server accessibility`,
|
|
3610
|
+
``,
|
|
3611
|
+
`For more details, see the error object logged above.`,
|
|
3612
|
+
`===========================================
|
|
3613
|
+
`
|
|
3614
|
+
].join("\n");
|
|
3615
|
+
console.error(noWalletErrorDetails);
|
|
3616
|
+
throw new ConnectionRejectedError(
|
|
3617
|
+
`WalletConnect Tron: \u6CA1\u6709\u627E\u5230\u652F\u6301\u7684\u94B1\u5305 (No matching wallet found)
|
|
3618
|
+
|
|
3619
|
+
\u53EF\u80FD\u7684\u539F\u56E0\uFF1A
|
|
3620
|
+
1. \u8BBE\u5907\u4E0A\u672A\u5B89\u88C5\u652F\u6301 WalletConnect \u7684\u94B1\u5305\uFF08\u5982 TokenPocket\uFF09
|
|
3621
|
+
2. \u94B1\u5305\u5E94\u7528\u672A\u6253\u5F00\u6216\u672A\u54CD\u5E94 deep link (wc://)
|
|
3622
|
+
3. \u5728 Telegram Mini App \u4E2D\uFF0Cdeep link \u5904\u7406\u53EF\u80FD\u6709\u95EE\u9898
|
|
3623
|
+
4. \u8FDE\u63A5\u8D85\u65F6\uFF08\u7528\u6237\u672A\u53CA\u65F6\u6279\u51C6\uFF09
|
|
3624
|
+
5. \u7F51\u7EDC\u8FDE\u63A5\u95EE\u9898
|
|
3625
|
+
|
|
3626
|
+
\u89E3\u51B3\u65B9\u6848\uFF1A
|
|
3627
|
+
1. \u786E\u4FDD\u5DF2\u5B89\u88C5 TokenPocket \u6216\u5176\u4ED6\u652F\u6301 WalletConnect \u7684\u94B1\u5305
|
|
3628
|
+
2. \u5C1D\u8BD5\u624B\u52A8\u6253\u5F00\u94B1\u5305\u5E94\u7528\u540E\u518D\u8FDE\u63A5
|
|
3629
|
+
3. \u5728 Telegram Mini App \u4E2D\uFF0C\u786E\u4FDD deep link \u5F39\u7A97\u672A\u88AB\u963B\u6B62
|
|
3630
|
+
4. \u7A0D\u7B49\u51E0\u79D2\u540E\u91CD\u8BD5\u8FDE\u63A5
|
|
3631
|
+
5. \u68C0\u67E5\u7F51\u7EDC\u8FDE\u63A5\u548C WalletConnect \u4E2D\u7EE7\u670D\u52A1\u5668\u53EF\u8BBF\u95EE\u6027
|
|
3632
|
+
|
|
3633
|
+
\u8BE6\u7EC6\u9519\u8BEF\u4FE1\u606F\u8BF7\u67E5\u770B\u63A7\u5236\u53F0\u65E5\u5FD7\u3002`
|
|
3634
|
+
);
|
|
3635
|
+
}
|
|
3636
|
+
if (errorMessage.includes("Invalid") || errorMessage.includes("Configuration") || errorMessage.includes("App Config") || errorMessage.includes("Invalid App")) {
|
|
3637
|
+
const configErrorDetails = [
|
|
3638
|
+
`
|
|
3639
|
+
=== WalletConnect Tron Configuration Error ===`,
|
|
3640
|
+
`Error: ${errorMessage}`,
|
|
3641
|
+
`Detailed: ${detailedError}`,
|
|
3642
|
+
`Code: ${errorCode || "N/A"}`,
|
|
3643
|
+
`
|
|
3644
|
+
Environment:`,
|
|
3645
|
+
` - Telegram Mini App: ${this.isTelegramMiniApp() ? "Yes" : "No"}`,
|
|
3646
|
+
` - Platform: ${errorDetails.telegramPlatform}`,
|
|
3647
|
+
` - Current URL: ${errorDetails.currentUrl}`,
|
|
3648
|
+
`
|
|
3649
|
+
Configuration:`,
|
|
3650
|
+
` - Project ID: ${this.projectId ? "Set" : "Missing"}`,
|
|
3651
|
+
` - Network: ${network}`,
|
|
3652
|
+
` - Chain ID: ${targetChainId}`,
|
|
3653
|
+
`
|
|
3654
|
+
Possible Causes:`,
|
|
3655
|
+
` 1. Deep link (wc://) handling issue in Telegram Mini App`,
|
|
3656
|
+
` 2. Invalid metadata configuration (URL or icons not accessible)`,
|
|
3657
|
+
` 3. Network/chainId mismatch`,
|
|
3658
|
+
` 4. WalletConnect project ID not configured correctly`,
|
|
3659
|
+
` 5. Domain not added to WalletConnect Cloud allowlist`,
|
|
3660
|
+
`
|
|
3661
|
+
Please check:`,
|
|
3662
|
+
` - WalletConnect Project ID is valid and active`,
|
|
3663
|
+
` - Domain is added to WalletConnect Cloud allowlist (for serveo.net, etc.)`,
|
|
3664
|
+
` - Metadata URL is accessible: Check console for metadata logs`,
|
|
3665
|
+
` - Icons are accessible: Check console for icon URLs`,
|
|
3666
|
+
` - Network matches chainId: Expected ${network} for chainId ${targetChainId}`,
|
|
3667
|
+
`
|
|
3668
|
+
For more details, see the error object logged above.`,
|
|
3669
|
+
`===========================================
|
|
3670
|
+
`
|
|
3671
|
+
].join("\n");
|
|
3672
|
+
console.error(configErrorDetails);
|
|
3673
|
+
throw new ConfigurationError(
|
|
3674
|
+
`WalletConnect Tron connection failed: ${errorMessage}
|
|
3675
|
+
|
|
3676
|
+
Configuration Details:
|
|
3677
|
+
- Telegram Mini App: ${this.isTelegramMiniApp() ? "Yes" : "No"}
|
|
3678
|
+
- Platform: ${errorDetails.telegramPlatform}
|
|
3679
|
+
- Origin: ${origin || "(unknown)"}
|
|
3680
|
+
- Project ID: ${this.projectId ? "Set" : "Missing"}
|
|
3681
|
+
- Network: ${network}
|
|
3682
|
+
- Chain ID: ${targetChainId}
|
|
3683
|
+
|
|
3684
|
+
This "Invalid App Configuration" error may be caused by:
|
|
3685
|
+
1. Deep link (wc://) handling issue in Telegram Mini App
|
|
3686
|
+
2. Invalid metadata configuration (URL or icons)
|
|
3687
|
+
3. Network/chainId mismatch
|
|
3688
|
+
4. Domain not added to WalletConnect Cloud allowlist
|
|
3689
|
+
|
|
3690
|
+
Please check the console for detailed error information.`
|
|
3691
|
+
);
|
|
3692
|
+
}
|
|
3693
|
+
if (isOriginNotAllowed) {
|
|
3694
|
+
throw new ConfigurationError(
|
|
3695
|
+
`WalletConnect Tron relayer rejected this origin (code 3000: Unauthorized: origin not allowed).
|
|
3696
|
+
|
|
3697
|
+
Fix:
|
|
3698
|
+
1) Open WalletConnect Cloud \u2192 your project (${this.projectId})
|
|
3699
|
+
2) Add this site origin to the allowlist:
|
|
3700
|
+
- ${origin || "(unknown origin)"}
|
|
3701
|
+
|
|
3702
|
+
Common dev origins to allow:
|
|
3703
|
+
- http://localhost:5173
|
|
3704
|
+
- http://192.168.0.221:5173 (your LAN dev URL)
|
|
3705
|
+
- https://wallet-test.enclave-hq.com (your Cloudflare Tunnel/custom domain)
|
|
3706
|
+
|
|
3707
|
+
Original error: ${errorMessage}`
|
|
3708
|
+
);
|
|
3709
|
+
}
|
|
3710
|
+
if (isTimeout) {
|
|
3711
|
+
throw new ConnectionRejectedError(
|
|
3712
|
+
`WalletConnect Tron connection timeout. Please try again and ensure your wallet app is open and ready.`
|
|
3713
|
+
);
|
|
3714
|
+
}
|
|
3715
|
+
if (isRejected) {
|
|
3716
|
+
throw new ConnectionRejectedError(this.type);
|
|
3717
|
+
}
|
|
3718
|
+
throw error;
|
|
3719
|
+
}
|
|
3720
|
+
this.currentAddress = address;
|
|
3721
|
+
const account = {
|
|
3722
|
+
universalAddress: createUniversalAddress(targetChainId, address),
|
|
3723
|
+
nativeAddress: address,
|
|
3724
|
+
chainId: targetChainId,
|
|
3725
|
+
chainType: ChainType.TRON,
|
|
3726
|
+
isActive: true
|
|
3727
|
+
};
|
|
3728
|
+
this.setState("connected" /* CONNECTED */);
|
|
3729
|
+
this.setAccount(account);
|
|
3730
|
+
this.setupEventListeners();
|
|
3731
|
+
return account;
|
|
3732
|
+
} catch (error) {
|
|
3733
|
+
this.setState("error" /* ERROR */);
|
|
3734
|
+
this.setAccount(null);
|
|
3735
|
+
this.currentAddress = null;
|
|
3736
|
+
if (error.message?.includes("rejected") || error.code === 4001) {
|
|
3737
|
+
throw new ConnectionRejectedError(this.type);
|
|
3738
|
+
}
|
|
3739
|
+
throw error;
|
|
3740
|
+
}
|
|
3741
|
+
}
|
|
3742
|
+
/**
|
|
3743
|
+
* Disconnect wallet
|
|
3744
|
+
*/
|
|
3745
|
+
async disconnect() {
|
|
3746
|
+
this.removeEventListeners();
|
|
3747
|
+
if (this.wallet) {
|
|
3748
|
+
try {
|
|
3749
|
+
await this.wallet.disconnect();
|
|
3750
|
+
} catch (error) {
|
|
3751
|
+
console.warn("[WalletConnect Tron] Error during disconnect:", error);
|
|
3752
|
+
}
|
|
3753
|
+
}
|
|
3754
|
+
this.wallet = null;
|
|
3755
|
+
this.currentAddress = null;
|
|
3756
|
+
this.setState("disconnected" /* DISCONNECTED */);
|
|
3757
|
+
this.setAccount(null);
|
|
3758
|
+
this.emitDisconnected();
|
|
3759
|
+
}
|
|
3760
|
+
/**
|
|
3761
|
+
* Sign message
|
|
3762
|
+
*/
|
|
3763
|
+
async signMessage(message) {
|
|
3764
|
+
this.ensureConnected();
|
|
3765
|
+
try {
|
|
3766
|
+
if (!this.wallet) {
|
|
3767
|
+
throw new Error("Wallet not initialized");
|
|
3768
|
+
}
|
|
3769
|
+
const signature = await this.wallet.signMessage(message);
|
|
3770
|
+
if (typeof signature === "string") {
|
|
3771
|
+
return signature;
|
|
3772
|
+
} else if (signature && typeof signature === "object") {
|
|
3773
|
+
if ("signature" in signature) {
|
|
3774
|
+
return signature.signature;
|
|
3775
|
+
} else if ("result" in signature) {
|
|
3776
|
+
return signature.result;
|
|
3777
|
+
} else {
|
|
3778
|
+
return JSON.stringify(signature);
|
|
3779
|
+
}
|
|
3780
|
+
}
|
|
3781
|
+
throw new Error("Invalid signature format returned from wallet");
|
|
3782
|
+
} catch (error) {
|
|
3783
|
+
console.error("[WalletConnect Tron] Sign message error:", error);
|
|
3784
|
+
let errorMessage = "Unknown error";
|
|
3785
|
+
if (typeof error === "string") {
|
|
3786
|
+
errorMessage = error;
|
|
3787
|
+
} else if (error?.message) {
|
|
3788
|
+
errorMessage = error.message;
|
|
3789
|
+
} else if (error?.error?.message) {
|
|
3790
|
+
errorMessage = error.error.message;
|
|
3791
|
+
} else {
|
|
3792
|
+
try {
|
|
3793
|
+
errorMessage = JSON.stringify(error);
|
|
3794
|
+
} catch {
|
|
3795
|
+
errorMessage = String(error);
|
|
3796
|
+
}
|
|
3797
|
+
}
|
|
3798
|
+
if (errorMessage?.includes("rejected") || errorMessage?.includes("declined") || errorMessage?.includes("User rejected") || error?.code === 4001 || error?.code === "USER_REJECTED" || error?.error?.code === 4001) {
|
|
3799
|
+
throw new SignatureRejectedError();
|
|
3800
|
+
}
|
|
3801
|
+
if (errorMessage?.includes("not supported") || errorMessage?.includes("method not found") || errorMessage?.includes("Method not found") || error?.code === -32601 || error?.error?.code === -32601) {
|
|
3802
|
+
throw new Error("tron_signMessage is not supported by the connected wallet. Please use a wallet that supports WalletConnect Tron signing, or use TronLink extension for browser-based signing.");
|
|
3803
|
+
}
|
|
3804
|
+
throw new Error(`WalletConnect Tron sign message failed: ${errorMessage}`);
|
|
3805
|
+
}
|
|
3806
|
+
}
|
|
3807
|
+
/**
|
|
3808
|
+
* Sign transaction
|
|
3809
|
+
*
|
|
3810
|
+
* @param transaction - Tron transaction object
|
|
3811
|
+
* Can be created using TronWeb (if available) or any TRON transaction builder
|
|
3812
|
+
* Format: { raw_data: {...}, raw_data_hex: "...", txID: "..." }
|
|
3813
|
+
* @returns Signed transaction object or signature
|
|
3814
|
+
*/
|
|
3815
|
+
async signTransaction(transaction) {
|
|
3816
|
+
this.ensureConnected();
|
|
3817
|
+
try {
|
|
3818
|
+
if (!this.wallet) {
|
|
3819
|
+
throw new Error("Wallet not initialized");
|
|
3820
|
+
}
|
|
3821
|
+
if (!transaction) {
|
|
3822
|
+
throw new Error("Transaction object is required");
|
|
3823
|
+
}
|
|
3824
|
+
console.log("[WalletConnect Tron] Signing transaction:", {
|
|
3825
|
+
hasRawData: !!transaction.raw_data,
|
|
3826
|
+
hasRawDataHex: !!transaction.raw_data_hex,
|
|
3827
|
+
hasTxID: !!transaction.txID
|
|
3828
|
+
});
|
|
3829
|
+
const result = await this.wallet.signTransaction(transaction);
|
|
3830
|
+
if (typeof result === "string") {
|
|
3831
|
+
return result;
|
|
3832
|
+
} else if (result && typeof result === "object") {
|
|
3833
|
+
if ("txID" in result && typeof result.txID === "string") {
|
|
3834
|
+
return result.txID;
|
|
3835
|
+
} else if ("txid" in result && typeof result.txid === "string") {
|
|
3836
|
+
return result.txid;
|
|
3837
|
+
} else if ("signature" in result) {
|
|
3838
|
+
return JSON.stringify(result);
|
|
3839
|
+
} else {
|
|
3840
|
+
return JSON.stringify(result);
|
|
3841
|
+
}
|
|
3842
|
+
}
|
|
3843
|
+
throw new Error("Invalid signature format returned from wallet");
|
|
3844
|
+
} catch (error) {
|
|
3845
|
+
console.error("[WalletConnect Tron] Sign transaction error:", error);
|
|
3846
|
+
let errorMessage = "Unknown error";
|
|
3847
|
+
if (typeof error === "string") {
|
|
3848
|
+
errorMessage = error;
|
|
3849
|
+
} else if (error?.message) {
|
|
3850
|
+
errorMessage = error.message;
|
|
3851
|
+
} else if (error?.error?.message) {
|
|
3852
|
+
errorMessage = error.error.message;
|
|
3853
|
+
} else if (error?.data?.message) {
|
|
3854
|
+
errorMessage = error.data.message;
|
|
3855
|
+
} else {
|
|
3856
|
+
try {
|
|
3857
|
+
errorMessage = JSON.stringify(error);
|
|
3858
|
+
} catch {
|
|
3859
|
+
errorMessage = String(error);
|
|
3860
|
+
}
|
|
3861
|
+
}
|
|
3862
|
+
if (errorMessage?.includes("rejected") || errorMessage?.includes("declined") || errorMessage?.includes("User rejected") || error?.code === 4001 || error?.code === "USER_REJECTED" || error?.error?.code === 4001) {
|
|
3863
|
+
throw new SignatureRejectedError("Transaction signature was rejected by user");
|
|
3864
|
+
}
|
|
3865
|
+
if (errorMessage?.includes("not supported") || errorMessage?.includes("method not found") || errorMessage?.includes("Method not found") || errorMessage?.includes("Not support") || error?.code === -32601 || error?.error?.code === -32601) {
|
|
3866
|
+
throw new Error("tron_signTransaction is not supported by the connected wallet. Please use a wallet that supports WalletConnect Tron signing, or use TronLink extension for browser-based signing.");
|
|
3867
|
+
}
|
|
3868
|
+
throw new Error(`WalletConnect Tron sign transaction failed: ${errorMessage}`);
|
|
3869
|
+
}
|
|
3870
|
+
}
|
|
3871
|
+
/**
|
|
3872
|
+
* Read contract (not supported by WalletConnect)
|
|
3873
|
+
*/
|
|
3874
|
+
async readContract(_params) {
|
|
3875
|
+
this.ensureConnected();
|
|
3876
|
+
throw new Error("WalletConnect Tron does not support direct contract reading. Please use direct Tron RPC calls or a wallet extension (like TronLink) for read operations.");
|
|
3877
|
+
}
|
|
3878
|
+
/**
|
|
3879
|
+
* Write contract (not yet implemented)
|
|
3880
|
+
*/
|
|
3881
|
+
async writeContract(_params) {
|
|
3882
|
+
throw new Error("Contract write not yet implemented for WalletConnect Tron");
|
|
3883
|
+
}
|
|
3884
|
+
/**
|
|
3885
|
+
* Estimate gas (not yet implemented)
|
|
3886
|
+
*/
|
|
3887
|
+
async estimateGas(_params) {
|
|
3888
|
+
throw new Error("Gas estimation not yet implemented for WalletConnect Tron");
|
|
3889
|
+
}
|
|
3890
|
+
/**
|
|
3891
|
+
* Wait for transaction (not yet implemented)
|
|
3892
|
+
*/
|
|
3893
|
+
async waitForTransaction(_txHash, _confirmations) {
|
|
3894
|
+
throw new Error("Transaction waiting not yet implemented for WalletConnect Tron");
|
|
3895
|
+
}
|
|
3896
|
+
/**
|
|
3897
|
+
* Setup event listeners
|
|
3898
|
+
*/
|
|
3899
|
+
setupEventListeners() {
|
|
3900
|
+
if (!this.wallet) {
|
|
3901
|
+
return;
|
|
3902
|
+
}
|
|
3903
|
+
this.wallet.on("accountsChanged", (accounts) => {
|
|
3904
|
+
if (accounts && accounts.length > 0 && accounts[0] !== this.currentAddress) {
|
|
3905
|
+
const newAddress = accounts[0];
|
|
3906
|
+
this.currentAddress = newAddress;
|
|
3907
|
+
if (this.currentAccount) {
|
|
3908
|
+
const newAccount = {
|
|
3909
|
+
...this.currentAccount,
|
|
3910
|
+
nativeAddress: newAddress,
|
|
3911
|
+
universalAddress: createUniversalAddress(this.currentAccount.chainId, newAddress)
|
|
3912
|
+
};
|
|
3913
|
+
this.setAccount(newAccount);
|
|
3914
|
+
this.emit("accountChanged", newAccount);
|
|
3915
|
+
}
|
|
3916
|
+
} else if (!accounts || accounts.length === 0) {
|
|
3917
|
+
this.disconnect();
|
|
3918
|
+
}
|
|
3919
|
+
});
|
|
3920
|
+
this.wallet.on("disconnect", () => {
|
|
3921
|
+
this.disconnect();
|
|
3922
|
+
});
|
|
3923
|
+
}
|
|
3924
|
+
/**
|
|
3925
|
+
* Remove event listeners
|
|
3926
|
+
*/
|
|
3927
|
+
removeEventListeners() {
|
|
3928
|
+
if (!this.wallet) {
|
|
3929
|
+
return;
|
|
3930
|
+
}
|
|
3931
|
+
this.wallet.removeAllListeners("accountsChanged");
|
|
3932
|
+
this.wallet.removeAllListeners("disconnect");
|
|
3933
|
+
}
|
|
3934
|
+
/**
|
|
3935
|
+
* Get provider (returns wallet instance)
|
|
3936
|
+
*/
|
|
3937
|
+
getProvider() {
|
|
3938
|
+
return this.wallet;
|
|
3939
|
+
}
|
|
3940
|
+
/**
|
|
3941
|
+
* Clear static wallet instance (for complete cleanup)
|
|
3942
|
+
*/
|
|
3943
|
+
static clearWalletInstance() {
|
|
3944
|
+
if (_WalletConnectTronAdapter.walletInstance) {
|
|
3945
|
+
_WalletConnectTronAdapter.walletInstance.disconnect().catch(() => {
|
|
3946
|
+
});
|
|
3947
|
+
_WalletConnectTronAdapter.walletInstance = null;
|
|
3948
|
+
_WalletConnectTronAdapter.walletProjectId = null;
|
|
3949
|
+
}
|
|
3950
|
+
}
|
|
3951
|
+
};
|
|
3952
|
+
// Tron 主网链 ID
|
|
3953
|
+
_WalletConnectTronAdapter.TRON_MAINNET_CHAIN_ID = 195;
|
|
3954
|
+
// Static wallet instance to avoid multiple initializations
|
|
3955
|
+
_WalletConnectTronAdapter.walletInstance = null;
|
|
3956
|
+
_WalletConnectTronAdapter.walletProjectId = null;
|
|
3957
|
+
var WalletConnectTronAdapter = _WalletConnectTronAdapter;
|
|
3958
|
+
|
|
3959
|
+
// src/adapters/deep-link/adapter.ts
|
|
3960
|
+
init_types();
|
|
3961
|
+
var DeepLinkProviderType = /* @__PURE__ */ ((DeepLinkProviderType2) => {
|
|
3962
|
+
DeepLinkProviderType2["TOKENPOCKET"] = "tokenpocket";
|
|
3963
|
+
DeepLinkProviderType2["TRONLINK"] = "tronlink";
|
|
3964
|
+
DeepLinkProviderType2["IMTOKEN"] = "imtoken";
|
|
3965
|
+
DeepLinkProviderType2["METAMASK"] = "metamask";
|
|
3966
|
+
DeepLinkProviderType2["OKX"] = "okx";
|
|
3967
|
+
return DeepLinkProviderType2;
|
|
3968
|
+
})(DeepLinkProviderType || {});
|
|
3969
|
+
var _DeepLinkAdapter = class _DeepLinkAdapter extends WalletAdapter {
|
|
3970
|
+
constructor(config) {
|
|
3971
|
+
super();
|
|
3972
|
+
this.currentChainId = null;
|
|
3973
|
+
this.currentChainType = null;
|
|
3974
|
+
this.provider = this.createProvider(config);
|
|
3975
|
+
this.name = `${this.provider.name} (Deep Link)`;
|
|
3976
|
+
this.icon = this.provider.icon;
|
|
3977
|
+
if (this.provider.supportedChainTypes.includes(ChainType.EVM)) {
|
|
3978
|
+
this.chainType = ChainType.EVM;
|
|
3979
|
+
this.type = "deep-link-evm" /* DEEP_LINK_EVM */;
|
|
3980
|
+
} else if (this.provider.supportedChainTypes.includes(ChainType.TRON)) {
|
|
3981
|
+
this.chainType = ChainType.TRON;
|
|
3982
|
+
this.type = "deep-link-tron" /* DEEP_LINK_TRON */;
|
|
3983
|
+
} else {
|
|
3984
|
+
this.chainType = ChainType.EVM;
|
|
3985
|
+
this.type = "deep-link-evm" /* DEEP_LINK_EVM */;
|
|
3986
|
+
}
|
|
3987
|
+
if (typeof window !== "undefined") {
|
|
3988
|
+
this.setupCallbackHandler();
|
|
3989
|
+
}
|
|
3990
|
+
}
|
|
3991
|
+
/**
|
|
3992
|
+
* Create provider instance based on type
|
|
3993
|
+
*/
|
|
3994
|
+
createProvider(config) {
|
|
3995
|
+
switch (config.providerType) {
|
|
3996
|
+
case "tokenpocket" /* TOKENPOCKET */: {
|
|
3997
|
+
const { TokenPocketDeepLinkProvider: TokenPocketDeepLinkProvider2 } = (init_tokenpocket(), __toCommonJS(tokenpocket_exports));
|
|
3998
|
+
return new TokenPocketDeepLinkProvider2({
|
|
3999
|
+
callbackUrl: config.callbackUrl,
|
|
4000
|
+
callbackSchema: config.callbackSchema
|
|
4001
|
+
});
|
|
4002
|
+
}
|
|
4003
|
+
case "tronlink" /* TRONLINK */: {
|
|
4004
|
+
const { TronLinkDeepLinkProvider: TronLinkDeepLinkProvider2 } = (init_tronlink(), __toCommonJS(tronlink_exports));
|
|
4005
|
+
return new TronLinkDeepLinkProvider2();
|
|
4006
|
+
}
|
|
4007
|
+
case "imtoken" /* IMTOKEN */: {
|
|
4008
|
+
const { ImTokenDeepLinkProvider: ImTokenDeepLinkProvider2 } = (init_imtoken(), __toCommonJS(imtoken_exports));
|
|
4009
|
+
return new ImTokenDeepLinkProvider2({
|
|
4010
|
+
callbackUrl: config.callbackUrl,
|
|
4011
|
+
callbackSchema: config.callbackSchema
|
|
4012
|
+
});
|
|
4013
|
+
}
|
|
4014
|
+
case "metamask" /* METAMASK */: {
|
|
4015
|
+
const { MetaMaskDeepLinkProvider: MetaMaskDeepLinkProvider2 } = (init_metamask(), __toCommonJS(metamask_exports));
|
|
4016
|
+
return new MetaMaskDeepLinkProvider2();
|
|
4017
|
+
}
|
|
4018
|
+
case "okx" /* OKX */: {
|
|
4019
|
+
const { OKXDeepLinkProvider: OKXDeepLinkProvider2 } = (init_okx(), __toCommonJS(okx_exports));
|
|
4020
|
+
return new OKXDeepLinkProvider2();
|
|
4021
|
+
}
|
|
4022
|
+
default:
|
|
4023
|
+
throw new Error(`Unsupported deep link provider type: ${config.providerType}`);
|
|
4024
|
+
}
|
|
4025
|
+
}
|
|
4026
|
+
/**
|
|
4027
|
+
* Setup callback handler for deep link results
|
|
4028
|
+
*/
|
|
4029
|
+
setupCallbackHandler() {
|
|
4030
|
+
if (typeof window === "undefined") {
|
|
4031
|
+
return;
|
|
4032
|
+
}
|
|
4033
|
+
const handleUrlChange = () => {
|
|
4034
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
4035
|
+
const result = this.provider.parseCallbackResult(urlParams);
|
|
4036
|
+
if (result.actionId && _DeepLinkAdapter.pendingActions.has(result.actionId)) {
|
|
4037
|
+
const callback = _DeepLinkAdapter.pendingActions.get(result.actionId);
|
|
4038
|
+
if (result.error) {
|
|
4039
|
+
callback.reject(new Error(result.error));
|
|
4040
|
+
} else if (result.result) {
|
|
4041
|
+
callback.resolve(result.result);
|
|
4042
|
+
}
|
|
4043
|
+
_DeepLinkAdapter.pendingActions.delete(result.actionId);
|
|
4044
|
+
}
|
|
4045
|
+
};
|
|
4046
|
+
window.addEventListener("popstate", handleUrlChange);
|
|
4047
|
+
window.addEventListener("hashchange", handleUrlChange);
|
|
4048
|
+
handleUrlChange();
|
|
4049
|
+
}
|
|
4050
|
+
/**
|
|
4051
|
+
* Check if deep link is available
|
|
4052
|
+
*/
|
|
4053
|
+
async isAvailable() {
|
|
4054
|
+
return this.provider.isAvailable();
|
|
4055
|
+
}
|
|
4056
|
+
/**
|
|
4057
|
+
* Connect to wallet via deep link
|
|
4058
|
+
*
|
|
4059
|
+
* Note: Deep links typically don't support persistent connections
|
|
4060
|
+
* This method may throw ConnectionRejectedError as deep links are
|
|
4061
|
+
* primarily used for signing operations, not connection
|
|
4062
|
+
*/
|
|
4063
|
+
async connect(chainId) {
|
|
4064
|
+
const targetChainId = Array.isArray(chainId) ? chainId[0] : chainId || 1;
|
|
4065
|
+
let chainType;
|
|
4066
|
+
if (targetChainId === 195) {
|
|
4067
|
+
chainType = ChainType.TRON;
|
|
4068
|
+
} else {
|
|
4069
|
+
chainType = ChainType.EVM;
|
|
4070
|
+
}
|
|
4071
|
+
if (!this.provider.supportedChainTypes.includes(chainType)) {
|
|
4072
|
+
throw new Error(
|
|
4073
|
+
`Provider ${this.provider.name} does not support chain type ${chainType}`
|
|
4074
|
+
);
|
|
4075
|
+
}
|
|
4076
|
+
if (this.provider.buildConnectLink) {
|
|
4077
|
+
const linkInfo = this.provider.buildConnectLink({
|
|
4078
|
+
chainId: targetChainId,
|
|
4079
|
+
chainType
|
|
4080
|
+
});
|
|
4081
|
+
if (linkInfo.actionId) {
|
|
4082
|
+
return new Promise((resolve, reject) => {
|
|
4083
|
+
_DeepLinkAdapter.pendingActions.set(linkInfo.actionId, {
|
|
4084
|
+
resolve: (result) => {
|
|
4085
|
+
const address = result?.address || result?.account || result;
|
|
4086
|
+
if (!address || typeof address !== "string") {
|
|
4087
|
+
reject(new ConnectionRejectedError("Invalid connection result: no address found"));
|
|
4088
|
+
return;
|
|
4089
|
+
}
|
|
4090
|
+
const account = {
|
|
4091
|
+
universalAddress: createUniversalAddress(targetChainId, address),
|
|
4092
|
+
nativeAddress: address,
|
|
4093
|
+
chainId: targetChainId,
|
|
4094
|
+
chainType,
|
|
4095
|
+
isActive: true
|
|
4096
|
+
};
|
|
4097
|
+
this.setState("connected" /* CONNECTED */);
|
|
4098
|
+
this.setAccount(account);
|
|
4099
|
+
this.emit("connected", account);
|
|
4100
|
+
resolve(account);
|
|
4101
|
+
},
|
|
4102
|
+
reject: (error) => {
|
|
4103
|
+
this.setState("disconnected" /* DISCONNECTED */);
|
|
4104
|
+
reject(error);
|
|
4105
|
+
}
|
|
4106
|
+
});
|
|
4107
|
+
window.location.href = linkInfo.url;
|
|
4108
|
+
setTimeout(() => {
|
|
4109
|
+
if (_DeepLinkAdapter.pendingActions.has(linkInfo.actionId)) {
|
|
4110
|
+
_DeepLinkAdapter.pendingActions.delete(linkInfo.actionId);
|
|
4111
|
+
this.setState("disconnected" /* DISCONNECTED */);
|
|
4112
|
+
reject(new ConnectionRejectedError("Deep link connection timeout"));
|
|
4113
|
+
}
|
|
4114
|
+
}, 3e4);
|
|
4115
|
+
});
|
|
4116
|
+
} else {
|
|
4117
|
+
window.location.href = linkInfo.url;
|
|
4118
|
+
throw new ConnectionRejectedError(
|
|
4119
|
+
"Deep link connection initiated. Please complete the connection in your wallet app."
|
|
4120
|
+
);
|
|
4121
|
+
}
|
|
4122
|
+
} else {
|
|
4123
|
+
throw new ConnectionRejectedError(
|
|
4124
|
+
`Deep link connection is not supported by ${this.provider.name}. Deep links are primarily used for signing operations.`
|
|
4125
|
+
);
|
|
4126
|
+
}
|
|
4127
|
+
}
|
|
4128
|
+
/**
|
|
4129
|
+
* Disconnect from wallet
|
|
4130
|
+
*/
|
|
4131
|
+
async disconnect() {
|
|
4132
|
+
this.setState("disconnected" /* DISCONNECTED */);
|
|
4133
|
+
this.setAccount(null);
|
|
4134
|
+
this.currentChainId = null;
|
|
4135
|
+
this.currentChainType = null;
|
|
4136
|
+
this.emitDisconnected();
|
|
4137
|
+
}
|
|
4138
|
+
/**
|
|
4139
|
+
* Sign a message
|
|
4140
|
+
*/
|
|
4141
|
+
async signMessage(message) {
|
|
4142
|
+
this.ensureConnected();
|
|
4143
|
+
if (!this.currentChainId || !this.currentChainType) {
|
|
4144
|
+
throw new WalletNotConnectedError(this.type);
|
|
4145
|
+
}
|
|
4146
|
+
const linkInfo = this.provider.buildSignMessageLink({
|
|
4147
|
+
message,
|
|
4148
|
+
chainId: this.currentChainId,
|
|
4149
|
+
chainType: this.currentChainType
|
|
4150
|
+
});
|
|
4151
|
+
if (linkInfo.callbackSchema || linkInfo.callbackUrl) {
|
|
4152
|
+
return new Promise((resolve, reject) => {
|
|
4153
|
+
_DeepLinkAdapter.pendingActions.set(linkInfo.actionId, { resolve, reject });
|
|
4154
|
+
window.location.href = linkInfo.url;
|
|
4155
|
+
setTimeout(() => {
|
|
4156
|
+
if (_DeepLinkAdapter.pendingActions.has(linkInfo.actionId)) {
|
|
4157
|
+
_DeepLinkAdapter.pendingActions.delete(linkInfo.actionId);
|
|
4158
|
+
reject(new SignatureRejectedError("Message signature timeout"));
|
|
4159
|
+
}
|
|
4160
|
+
}, 3e4);
|
|
4161
|
+
});
|
|
4162
|
+
} else {
|
|
4163
|
+
window.location.href = linkInfo.url;
|
|
4164
|
+
throw new SignatureRejectedError(
|
|
4165
|
+
"Deep link signature initiated. Please complete the signature in your wallet app."
|
|
4166
|
+
);
|
|
4167
|
+
}
|
|
4168
|
+
}
|
|
4169
|
+
/**
|
|
4170
|
+
* Sign a transaction
|
|
4171
|
+
*/
|
|
4172
|
+
async signTransaction(transaction) {
|
|
4173
|
+
this.ensureConnected();
|
|
4174
|
+
if (!this.currentChainId || !this.currentChainType) {
|
|
4175
|
+
throw new WalletNotConnectedError(this.type);
|
|
4176
|
+
}
|
|
4177
|
+
const linkInfo = this.provider.buildSignTransactionLink({
|
|
4178
|
+
transaction,
|
|
4179
|
+
chainId: this.currentChainId,
|
|
4180
|
+
chainType: this.currentChainType
|
|
4181
|
+
});
|
|
4182
|
+
if (linkInfo.callbackSchema || linkInfo.callbackUrl) {
|
|
4183
|
+
return new Promise((resolve, reject) => {
|
|
4184
|
+
_DeepLinkAdapter.pendingActions.set(linkInfo.actionId, { resolve, reject });
|
|
4185
|
+
window.location.href = linkInfo.url;
|
|
4186
|
+
setTimeout(() => {
|
|
4187
|
+
if (_DeepLinkAdapter.pendingActions.has(linkInfo.actionId)) {
|
|
4188
|
+
_DeepLinkAdapter.pendingActions.delete(linkInfo.actionId);
|
|
4189
|
+
reject(new SignatureRejectedError("Transaction signature timeout"));
|
|
4190
|
+
}
|
|
4191
|
+
}, 3e4);
|
|
4192
|
+
});
|
|
4193
|
+
} else {
|
|
4194
|
+
window.location.href = linkInfo.url;
|
|
4195
|
+
throw new SignatureRejectedError(
|
|
4196
|
+
"Deep link transaction signature initiated. Please complete the signature in your wallet app."
|
|
4197
|
+
);
|
|
4198
|
+
}
|
|
4199
|
+
}
|
|
4200
|
+
/**
|
|
4201
|
+
* Get provider (not applicable for deep links)
|
|
4202
|
+
*/
|
|
4203
|
+
getProvider() {
|
|
4204
|
+
return null;
|
|
4205
|
+
}
|
|
4206
|
+
/**
|
|
4207
|
+
* Static method to handle callback from wallet apps
|
|
4208
|
+
* This can be called from anywhere in the application
|
|
4209
|
+
*/
|
|
4210
|
+
static handleCallback() {
|
|
4211
|
+
if (typeof window === "undefined") {
|
|
4212
|
+
return;
|
|
4213
|
+
}
|
|
4214
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
4215
|
+
const actionId = urlParams.get("actionId");
|
|
4216
|
+
if (actionId && _DeepLinkAdapter.pendingActions.has(actionId)) {
|
|
4217
|
+
const callback = _DeepLinkAdapter.pendingActions.get(actionId);
|
|
4218
|
+
const result = urlParams.get("result");
|
|
4219
|
+
const error = urlParams.get("error");
|
|
4220
|
+
if (error) {
|
|
4221
|
+
callback.reject(new Error(error));
|
|
4222
|
+
} else if (result) {
|
|
4223
|
+
try {
|
|
4224
|
+
const parsedResult = JSON.parse(decodeURIComponent(result));
|
|
4225
|
+
callback.resolve(parsedResult);
|
|
4226
|
+
} catch (e) {
|
|
4227
|
+
callback.resolve(result);
|
|
4228
|
+
}
|
|
4229
|
+
}
|
|
4230
|
+
_DeepLinkAdapter.pendingActions.delete(actionId);
|
|
4231
|
+
}
|
|
4232
|
+
}
|
|
4233
|
+
/**
|
|
4234
|
+
* Set current account (called after successful connection)
|
|
4235
|
+
*/
|
|
4236
|
+
setAccount(account) {
|
|
4237
|
+
this.currentAccount = account;
|
|
4238
|
+
if (account) {
|
|
4239
|
+
this.currentChainId = account.chainId;
|
|
4240
|
+
this.currentChainType = account.chainType;
|
|
4241
|
+
}
|
|
4242
|
+
}
|
|
4243
|
+
/**
|
|
4244
|
+
* Emit disconnected event
|
|
4245
|
+
*/
|
|
4246
|
+
emitDisconnected() {
|
|
4247
|
+
this.emit("disconnected");
|
|
4248
|
+
}
|
|
4249
|
+
};
|
|
4250
|
+
// Static map to store pending actions across all instances
|
|
4251
|
+
// Key: actionId, Value: { resolve, reject }
|
|
4252
|
+
_DeepLinkAdapter.pendingActions = /* @__PURE__ */ new Map();
|
|
4253
|
+
var DeepLinkAdapter = _DeepLinkAdapter;
|
|
4254
|
+
|
|
4255
|
+
// src/core/adapter-registry.ts
|
|
4256
|
+
var AdapterRegistry = class {
|
|
4257
|
+
constructor(config = {}) {
|
|
4258
|
+
this.adapters = /* @__PURE__ */ new Map();
|
|
4259
|
+
this.config = config;
|
|
4260
|
+
this.registerDefaultAdapters();
|
|
4261
|
+
}
|
|
4262
|
+
/**
|
|
4263
|
+
* Register default adapters
|
|
4264
|
+
*/
|
|
4265
|
+
registerDefaultAdapters() {
|
|
4266
|
+
this.register("metamask" /* METAMASK */, () => new MetaMaskAdapter());
|
|
4267
|
+
this.register("private-key" /* PRIVATE_KEY */, () => new EVMPrivateKeyAdapter());
|
|
4268
|
+
if (this.config.walletConnectProjectId) {
|
|
4269
|
+
this.register(
|
|
4270
|
+
"walletconnect" /* WALLETCONNECT */,
|
|
4271
|
+
() => new WalletConnectAdapter(this.config.walletConnectProjectId)
|
|
4272
|
+
);
|
|
4273
|
+
this.register(
|
|
4274
|
+
"walletconnect-tron" /* WALLETCONNECT_TRON */,
|
|
4275
|
+
() => new WalletConnectTronAdapter(this.config.walletConnectProjectId)
|
|
4276
|
+
);
|
|
4277
|
+
}
|
|
4278
|
+
this.register("tronlink" /* TRONLINK */, () => new TronLinkAdapter());
|
|
4279
|
+
this.register(
|
|
4280
|
+
"deep-link-evm" /* DEEP_LINK_EVM */,
|
|
4281
|
+
() => new DeepLinkAdapter({
|
|
4282
|
+
providerType: "tokenpocket" /* TOKENPOCKET */
|
|
4283
|
+
})
|
|
4284
|
+
);
|
|
4285
|
+
this.register(
|
|
4286
|
+
"deep-link-tron" /* DEEP_LINK_TRON */,
|
|
4287
|
+
() => new DeepLinkAdapter({
|
|
4288
|
+
providerType: "tokenpocket" /* TOKENPOCKET */
|
|
4289
|
+
})
|
|
4290
|
+
);
|
|
4291
|
+
}
|
|
4292
|
+
/**
|
|
4293
|
+
* Register adapter
|
|
4294
|
+
*/
|
|
4295
|
+
register(type, factory) {
|
|
4296
|
+
this.adapters.set(type, factory);
|
|
4297
|
+
}
|
|
4298
|
+
/**
|
|
4299
|
+
* Get adapter
|
|
4300
|
+
*/
|
|
4301
|
+
getAdapter(type) {
|
|
4302
|
+
const factory = this.adapters.get(type);
|
|
4303
|
+
if (!factory) {
|
|
4304
|
+
return null;
|
|
4305
|
+
}
|
|
4306
|
+
return factory();
|
|
4307
|
+
}
|
|
4308
|
+
/**
|
|
4309
|
+
* Check if adapter is registered
|
|
4310
|
+
*/
|
|
4311
|
+
has(type) {
|
|
4312
|
+
return this.adapters.has(type);
|
|
4313
|
+
}
|
|
4314
|
+
/**
|
|
4315
|
+
* Get all registered adapter types
|
|
4316
|
+
*/
|
|
4317
|
+
getRegisteredTypes() {
|
|
4318
|
+
return Array.from(this.adapters.keys());
|
|
4319
|
+
}
|
|
4320
|
+
/**
|
|
4321
|
+
* 根据链类型获取适配器类型列表
|
|
4322
|
+
*/
|
|
4323
|
+
getAdapterTypesByChainType(chainType) {
|
|
4324
|
+
const types = [];
|
|
4325
|
+
for (const type of this.adapters.keys()) {
|
|
4326
|
+
const adapter = this.getAdapter(type);
|
|
4327
|
+
if (adapter && adapter.chainType === chainType) {
|
|
4328
|
+
types.push(type);
|
|
4329
|
+
}
|
|
4330
|
+
}
|
|
4331
|
+
return types;
|
|
4332
|
+
}
|
|
4333
|
+
};
|
|
4334
|
+
|
|
4335
|
+
// src/core/wallet-manager.ts
|
|
4336
|
+
init_types();
|
|
4337
|
+
var QRCodeSignStatus = /* @__PURE__ */ ((QRCodeSignStatus2) => {
|
|
4338
|
+
QRCodeSignStatus2["WAITING"] = "waiting";
|
|
4339
|
+
QRCodeSignStatus2["PENDING"] = "pending";
|
|
4340
|
+
QRCodeSignStatus2["SUCCESS"] = "success";
|
|
4341
|
+
QRCodeSignStatus2["FAILED"] = "failed";
|
|
4342
|
+
QRCodeSignStatus2["TIMEOUT"] = "timeout";
|
|
4343
|
+
QRCodeSignStatus2["CANCELLED"] = "cancelled";
|
|
4344
|
+
return QRCodeSignStatus2;
|
|
4345
|
+
})(QRCodeSignStatus || {});
|
|
4346
|
+
var QRCodeSigner = class {
|
|
4347
|
+
constructor(config) {
|
|
4348
|
+
this.pollTimer = null;
|
|
4349
|
+
this.timeoutTimer = null;
|
|
4350
|
+
this.status = "waiting" /* WAITING */;
|
|
4351
|
+
this.qrCodeDataUrl = null;
|
|
4352
|
+
this.result = null;
|
|
4353
|
+
this.config = {
|
|
4354
|
+
requestId: config.requestId,
|
|
4355
|
+
requestUrl: config.requestUrl,
|
|
4356
|
+
pollUrl: config.pollUrl || "",
|
|
4357
|
+
pollInterval: config.pollInterval || 2e3,
|
|
4358
|
+
timeout: config.timeout || 3e5,
|
|
4359
|
+
// 5 minutes
|
|
4360
|
+
pollFn: config.pollFn
|
|
4361
|
+
};
|
|
4362
|
+
}
|
|
4363
|
+
/**
|
|
4364
|
+
* 生成二维码图片(Data URL)
|
|
4365
|
+
*/
|
|
4366
|
+
async generateQRCode(options) {
|
|
4367
|
+
if (this.qrCodeDataUrl) {
|
|
4368
|
+
return this.qrCodeDataUrl;
|
|
4369
|
+
}
|
|
4370
|
+
try {
|
|
4371
|
+
const qrCodeOptions = {
|
|
4372
|
+
width: options?.width || 300,
|
|
4373
|
+
margin: options?.margin || 2,
|
|
4374
|
+
color: {
|
|
4375
|
+
dark: options?.color?.dark || "#000000",
|
|
4376
|
+
light: options?.color?.light || "#FFFFFF"
|
|
4377
|
+
}
|
|
4378
|
+
};
|
|
4379
|
+
this.qrCodeDataUrl = await QRCode.toDataURL(this.config.requestUrl, qrCodeOptions);
|
|
4380
|
+
return this.qrCodeDataUrl;
|
|
4381
|
+
} catch (error) {
|
|
4382
|
+
throw new Error(`Failed to generate QR code: ${error instanceof Error ? error.message : String(error)}`);
|
|
4383
|
+
}
|
|
4384
|
+
}
|
|
4385
|
+
/**
|
|
4386
|
+
* 开始轮询签名结果
|
|
4387
|
+
*/
|
|
4388
|
+
async startPolling(onStatusChange, onResult) {
|
|
4389
|
+
if (this.status === "success" /* SUCCESS */ && this.result?.signature) {
|
|
4390
|
+
return this.result.signature;
|
|
4391
|
+
}
|
|
4392
|
+
if (this.status === "cancelled" /* CANCELLED */ || this.status === "timeout" /* TIMEOUT */) {
|
|
4393
|
+
throw new SignatureRejectedError("Signature request was cancelled or timed out");
|
|
4394
|
+
}
|
|
4395
|
+
this.timeoutTimer = setTimeout(() => {
|
|
4396
|
+
this.stopPolling();
|
|
4397
|
+
this.status = "timeout" /* TIMEOUT */;
|
|
4398
|
+
onStatusChange?.(this.status);
|
|
4399
|
+
throw new SignatureRejectedError("Signature request timed out");
|
|
4400
|
+
}, this.config.timeout);
|
|
4401
|
+
return new Promise((resolve, reject) => {
|
|
4402
|
+
const poll = async () => {
|
|
4403
|
+
try {
|
|
4404
|
+
let result = null;
|
|
4405
|
+
if (this.config.pollFn) {
|
|
4406
|
+
result = await this.config.pollFn(this.config.requestId);
|
|
4407
|
+
} else if (this.config.pollUrl) {
|
|
4408
|
+
result = await this.defaultPoll(this.config.requestId);
|
|
4409
|
+
} else {
|
|
4410
|
+
return;
|
|
4411
|
+
}
|
|
4412
|
+
if (result?.completed) {
|
|
4413
|
+
this.stopPolling();
|
|
4414
|
+
this.result = result;
|
|
4415
|
+
if (result.signature) {
|
|
4416
|
+
this.status = "success" /* SUCCESS */;
|
|
4417
|
+
onStatusChange?.(this.status);
|
|
4418
|
+
onResult?.(result);
|
|
4419
|
+
resolve(result.signature);
|
|
4420
|
+
} else if (result.error) {
|
|
4421
|
+
this.status = "failed" /* FAILED */;
|
|
4422
|
+
onStatusChange?.(this.status);
|
|
4423
|
+
reject(new SignatureRejectedError(result.error));
|
|
4424
|
+
}
|
|
4425
|
+
} else if (result) {
|
|
4426
|
+
if (this.status === "waiting" /* WAITING */) {
|
|
4427
|
+
this.status = "pending" /* PENDING */;
|
|
4428
|
+
onStatusChange?.(this.status);
|
|
4429
|
+
}
|
|
4430
|
+
this.pollTimer = setTimeout(poll, this.config.pollInterval);
|
|
4431
|
+
} else {
|
|
4432
|
+
this.pollTimer = setTimeout(poll, this.config.pollInterval);
|
|
4433
|
+
}
|
|
4434
|
+
} catch (error) {
|
|
4435
|
+
this.stopPolling();
|
|
4436
|
+
this.status = "failed" /* FAILED */;
|
|
4437
|
+
onStatusChange?.(this.status);
|
|
4438
|
+
reject(error);
|
|
4439
|
+
}
|
|
4440
|
+
};
|
|
4441
|
+
poll();
|
|
4442
|
+
});
|
|
4443
|
+
}
|
|
4444
|
+
/**
|
|
4445
|
+
* 默认 HTTP 轮询函数
|
|
4446
|
+
*/
|
|
4447
|
+
async defaultPoll(requestId) {
|
|
4448
|
+
if (!this.config.pollUrl) {
|
|
4449
|
+
return null;
|
|
4450
|
+
}
|
|
4451
|
+
try {
|
|
4452
|
+
const url = `${this.config.pollUrl}?requestId=${encodeURIComponent(requestId)}`;
|
|
4453
|
+
const response = await fetch(url, {
|
|
4454
|
+
method: "GET",
|
|
4455
|
+
headers: {
|
|
4456
|
+
"Content-Type": "application/json"
|
|
4457
|
+
}
|
|
4458
|
+
});
|
|
4459
|
+
if (!response.ok) {
|
|
4460
|
+
if (response.status === 404) {
|
|
4461
|
+
return null;
|
|
4462
|
+
}
|
|
4463
|
+
throw new NetworkError(`Poll request failed: ${response.statusText}`);
|
|
4464
|
+
}
|
|
4465
|
+
const data = await response.json();
|
|
4466
|
+
return {
|
|
4467
|
+
completed: data.completed === true,
|
|
4468
|
+
signature: data.signature,
|
|
4469
|
+
error: data.error,
|
|
4470
|
+
signer: data.signer
|
|
4471
|
+
};
|
|
4472
|
+
} catch (error) {
|
|
4473
|
+
if (error instanceof NetworkError) {
|
|
4474
|
+
throw error;
|
|
4475
|
+
}
|
|
4476
|
+
return null;
|
|
4477
|
+
}
|
|
4478
|
+
}
|
|
4479
|
+
/**
|
|
4480
|
+
* 停止轮询
|
|
4481
|
+
*/
|
|
4482
|
+
stopPolling() {
|
|
4483
|
+
if (this.pollTimer) {
|
|
4484
|
+
clearTimeout(this.pollTimer);
|
|
4485
|
+
this.pollTimer = null;
|
|
4486
|
+
}
|
|
4487
|
+
if (this.timeoutTimer) {
|
|
4488
|
+
clearTimeout(this.timeoutTimer);
|
|
4489
|
+
this.timeoutTimer = null;
|
|
4490
|
+
}
|
|
4491
|
+
}
|
|
4492
|
+
/**
|
|
4493
|
+
* 取消签名请求
|
|
4494
|
+
*/
|
|
4495
|
+
cancel() {
|
|
4496
|
+
this.stopPolling();
|
|
4497
|
+
this.status = "cancelled" /* CANCELLED */;
|
|
4498
|
+
}
|
|
4499
|
+
/**
|
|
4500
|
+
* 获取当前状态
|
|
4501
|
+
*/
|
|
4502
|
+
getStatus() {
|
|
4503
|
+
return this.status;
|
|
4504
|
+
}
|
|
4505
|
+
/**
|
|
4506
|
+
* 获取二维码 URL
|
|
4507
|
+
*/
|
|
4508
|
+
getQRCodeUrl() {
|
|
4509
|
+
return this.config.requestUrl;
|
|
4510
|
+
}
|
|
4511
|
+
/**
|
|
4512
|
+
* 获取结果
|
|
4513
|
+
*/
|
|
4514
|
+
getResult() {
|
|
4515
|
+
return this.result;
|
|
4516
|
+
}
|
|
4517
|
+
/**
|
|
4518
|
+
* 清理资源
|
|
4519
|
+
*/
|
|
4520
|
+
cleanup() {
|
|
4521
|
+
this.stopPolling();
|
|
4522
|
+
this.qrCodeDataUrl = null;
|
|
4523
|
+
this.result = null;
|
|
4524
|
+
this.status = "waiting" /* WAITING */;
|
|
4525
|
+
}
|
|
4526
|
+
};
|
|
4527
|
+
|
|
4528
|
+
// src/core/wallet-manager.ts
|
|
4529
|
+
var WalletManager = class extends TypedEventEmitter {
|
|
1691
4530
|
constructor(config = {}) {
|
|
1692
4531
|
super();
|
|
1693
4532
|
// Primary wallet
|
|
@@ -1701,9 +4540,15 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1701
4540
|
defaultTronChainId: config.defaultTronChainId ?? 195,
|
|
1702
4541
|
walletConnectProjectId: config.walletConnectProjectId ?? ""
|
|
1703
4542
|
};
|
|
1704
|
-
this.registry = new AdapterRegistry();
|
|
4543
|
+
this.registry = new AdapterRegistry(this.config);
|
|
1705
4544
|
}
|
|
1706
4545
|
// ===== Connection Management =====
|
|
4546
|
+
/**
|
|
4547
|
+
* Check if adapter is registered for a wallet type
|
|
4548
|
+
*/
|
|
4549
|
+
hasAdapter(type) {
|
|
4550
|
+
return this.registry.has(type);
|
|
4551
|
+
}
|
|
1707
4552
|
/**
|
|
1708
4553
|
* Connect primary wallet
|
|
1709
4554
|
*/
|
|
@@ -1770,7 +4615,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1770
4615
|
this.connectedWallets.delete(chainType);
|
|
1771
4616
|
this.primaryWallet = null;
|
|
1772
4617
|
if (this.config.enableStorage) {
|
|
1773
|
-
this.
|
|
4618
|
+
this.clearStorage();
|
|
1774
4619
|
}
|
|
1775
4620
|
this.emit("disconnected");
|
|
1776
4621
|
}
|
|
@@ -1859,6 +4704,35 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1859
4704
|
}
|
|
1860
4705
|
return adapter.signMessage(message);
|
|
1861
4706
|
}
|
|
4707
|
+
/**
|
|
4708
|
+
* Create QR code signer for message signing
|
|
4709
|
+
*
|
|
4710
|
+
* This method creates a QR code signer that can be used to display a QR code
|
|
4711
|
+
* for users to scan with their wallet app to sign a message.
|
|
4712
|
+
*
|
|
4713
|
+
* @param message - Message to sign
|
|
4714
|
+
* @param config - QR code signer configuration
|
|
4715
|
+
* @returns QRCodeSigner instance
|
|
4716
|
+
*
|
|
4717
|
+
* @example
|
|
4718
|
+
* ```typescript
|
|
4719
|
+
* const signer = walletManager.createQRCodeSigner('Hello World', {
|
|
4720
|
+
* requestId: 'sign-123',
|
|
4721
|
+
* requestUrl: 'https://example.com/sign?requestId=sign-123&message=Hello%20World',
|
|
4722
|
+
* pollUrl: 'https://api.example.com/sign/status',
|
|
4723
|
+
* })
|
|
4724
|
+
*
|
|
4725
|
+
* const qrCodeUrl = await signer.generateQRCode()
|
|
4726
|
+
* const signature = await signer.startPolling()
|
|
4727
|
+
* ```
|
|
4728
|
+
*/
|
|
4729
|
+
createQRCodeSigner(message, config) {
|
|
4730
|
+
return new QRCodeSigner({
|
|
4731
|
+
...config,
|
|
4732
|
+
// Encode message in request URL if not already encoded
|
|
4733
|
+
requestUrl: config.requestUrl.includes(encodeURIComponent(message)) ? config.requestUrl : `${config.requestUrl}${config.requestUrl.includes("?") ? "&" : "?"}message=${encodeURIComponent(message)}`
|
|
4734
|
+
});
|
|
4735
|
+
}
|
|
1862
4736
|
/**
|
|
1863
4737
|
* Sign TypedData (EVM only)
|
|
1864
4738
|
*/
|
|
@@ -2169,6 +5043,29 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
2169
5043
|
console.debug("Silent TronLink connection failed:", silentError);
|
|
2170
5044
|
}
|
|
2171
5045
|
}
|
|
5046
|
+
if (data.primaryWalletType === "walletconnect-tron" /* WALLETCONNECT_TRON */) {
|
|
5047
|
+
try {
|
|
5048
|
+
const wcAdapter = adapter;
|
|
5049
|
+
if (typeof wcAdapter.restoreSession === "function") {
|
|
5050
|
+
console.debug("[WalletManager] Attempting to restore WalletConnect Tron session...");
|
|
5051
|
+
const account2 = await wcAdapter.restoreSession(data.primaryChainId);
|
|
5052
|
+
if (account2) {
|
|
5053
|
+
console.debug("[WalletManager] WalletConnect Tron session restored successfully");
|
|
5054
|
+
this.setPrimaryWallet(adapter);
|
|
5055
|
+
this.connectedWallets.set(adapter.chainType, adapter);
|
|
5056
|
+
this.setupAdapterListeners(adapter, true);
|
|
5057
|
+
this.emit("accountChanged", account2);
|
|
5058
|
+
return account2;
|
|
5059
|
+
} else {
|
|
5060
|
+
console.debug("[WalletManager] No valid WalletConnect Tron session found");
|
|
5061
|
+
return null;
|
|
5062
|
+
}
|
|
5063
|
+
}
|
|
5064
|
+
} catch (restoreError) {
|
|
5065
|
+
console.debug("[WalletManager] WalletConnect Tron restore failed:", restoreError);
|
|
5066
|
+
return null;
|
|
5067
|
+
}
|
|
5068
|
+
}
|
|
2172
5069
|
const account = await adapter.connect(data.primaryChainId);
|
|
2173
5070
|
this.setPrimaryWallet(adapter);
|
|
2174
5071
|
this.connectedWallets.set(adapter.chainType, adapter);
|
|
@@ -2215,7 +5112,466 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
2215
5112
|
}
|
|
2216
5113
|
};
|
|
2217
5114
|
|
|
5115
|
+
// src/index.ts
|
|
5116
|
+
init_types();
|
|
5117
|
+
init_tokenpocket();
|
|
5118
|
+
init_tronlink();
|
|
5119
|
+
init_imtoken();
|
|
5120
|
+
init_metamask();
|
|
5121
|
+
init_okx();
|
|
5122
|
+
|
|
5123
|
+
// src/adapters/tron/deep-link.ts
|
|
5124
|
+
init_types();
|
|
5125
|
+
var DeepLinkWalletType = /* @__PURE__ */ ((DeepLinkWalletType2) => {
|
|
5126
|
+
DeepLinkWalletType2["TOKENPOCKET"] = "tokenpocket";
|
|
5127
|
+
DeepLinkWalletType2["TRONLINK"] = "tronlink";
|
|
5128
|
+
return DeepLinkWalletType2;
|
|
5129
|
+
})(DeepLinkWalletType || {});
|
|
5130
|
+
var _TronDeepLinkAdapter = class _TronDeepLinkAdapter extends WalletAdapter {
|
|
5131
|
+
constructor(walletType = "tokenpocket" /* TOKENPOCKET */, options) {
|
|
5132
|
+
super();
|
|
5133
|
+
this.type = "tronlink" /* TRONLINK */;
|
|
5134
|
+
// Reuse TRONLINK type for now
|
|
5135
|
+
this.chainType = ChainType.TRON;
|
|
5136
|
+
this.pendingActions = /* @__PURE__ */ new Map();
|
|
5137
|
+
this.walletType = walletType;
|
|
5138
|
+
this.callbackUrl = options?.callbackUrl;
|
|
5139
|
+
this.callbackSchema = options?.callbackSchema;
|
|
5140
|
+
if (walletType === "tokenpocket" /* TOKENPOCKET */) {
|
|
5141
|
+
this.name = "TokenPocket (Deep Link)";
|
|
5142
|
+
this.icon = "https://tokenpocket.pro/icon.png";
|
|
5143
|
+
} else if (walletType === "tronlink" /* TRONLINK */) {
|
|
5144
|
+
this.name = "TronLink (Deep Link)";
|
|
5145
|
+
this.icon = "https://www.tronlink.org/static/logoIcon.svg";
|
|
5146
|
+
} else {
|
|
5147
|
+
this.name = "TRON Deep Link";
|
|
5148
|
+
this.icon = "https://www.tronlink.org/static/logoIcon.svg";
|
|
5149
|
+
}
|
|
5150
|
+
if (walletType === "tokenpocket" /* TOKENPOCKET */ && typeof window !== "undefined") {
|
|
5151
|
+
this.setupCallbackHandler();
|
|
5152
|
+
}
|
|
5153
|
+
}
|
|
5154
|
+
/**
|
|
5155
|
+
* Setup callback handler for TokenPocket deep link results
|
|
5156
|
+
*
|
|
5157
|
+
* According to TokenPocket docs:
|
|
5158
|
+
* - callbackUrl: Wallet sends result to this URL (server-side callback)
|
|
5159
|
+
* - callbackSchema: Wallet launches H5 app through this schema (client-side callback)
|
|
5160
|
+
*
|
|
5161
|
+
* For H5 apps, we use callbackSchema to receive results in the same page
|
|
5162
|
+
*/
|
|
5163
|
+
setupCallbackHandler() {
|
|
5164
|
+
if (typeof window === "undefined") {
|
|
5165
|
+
return;
|
|
5166
|
+
}
|
|
5167
|
+
document.addEventListener("visibilitychange", () => {
|
|
5168
|
+
if (document.visibilityState === "visible") {
|
|
5169
|
+
console.log("[TronDeepLink] Page became visible, user may have returned from wallet app");
|
|
5170
|
+
this.checkCallbackFromUrl();
|
|
5171
|
+
}
|
|
5172
|
+
});
|
|
5173
|
+
this.checkCallbackFromUrl();
|
|
5174
|
+
}
|
|
5175
|
+
/**
|
|
5176
|
+
* Check if callback result is in URL parameters
|
|
5177
|
+
* TokenPocket may append callback results to URL when using callbackSchema
|
|
5178
|
+
*/
|
|
5179
|
+
checkCallbackFromUrl() {
|
|
5180
|
+
if (typeof window === "undefined") {
|
|
5181
|
+
return;
|
|
5182
|
+
}
|
|
5183
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
5184
|
+
const actionId = urlParams.get("actionId");
|
|
5185
|
+
const result = urlParams.get("result");
|
|
5186
|
+
const error = urlParams.get("error");
|
|
5187
|
+
if (actionId && this.pendingActions.has(actionId)) {
|
|
5188
|
+
const { resolve, reject } = this.pendingActions.get(actionId);
|
|
5189
|
+
if (error) {
|
|
5190
|
+
reject(new Error(error));
|
|
5191
|
+
} else if (result) {
|
|
5192
|
+
try {
|
|
5193
|
+
const parsedResult = JSON.parse(decodeURIComponent(result));
|
|
5194
|
+
resolve(parsedResult);
|
|
5195
|
+
} catch (e) {
|
|
5196
|
+
resolve(result);
|
|
5197
|
+
}
|
|
5198
|
+
}
|
|
5199
|
+
this.pendingActions.delete(actionId);
|
|
5200
|
+
const newUrl = window.location.pathname;
|
|
5201
|
+
window.history.replaceState({}, "", newUrl);
|
|
5202
|
+
}
|
|
5203
|
+
}
|
|
5204
|
+
/**
|
|
5205
|
+
* Check if deep link is available (mobile device or Telegram Mini App)
|
|
5206
|
+
*
|
|
5207
|
+
* Note: In Telegram Mini App, we're more lenient - even if platform detection
|
|
5208
|
+
* fails, deep links may still work, so the caller should handle Telegram Mini App
|
|
5209
|
+
* separately if needed.
|
|
5210
|
+
*/
|
|
5211
|
+
async isAvailable() {
|
|
5212
|
+
if (typeof window === "undefined") {
|
|
5213
|
+
return false;
|
|
5214
|
+
}
|
|
5215
|
+
const isTelegramMiniApp = !!(window.Telegram && window.Telegram.WebApp);
|
|
5216
|
+
if (isTelegramMiniApp) {
|
|
5217
|
+
const platform = window.Telegram.WebApp.platform || "unknown";
|
|
5218
|
+
const version = window.Telegram.WebApp.version || "unknown";
|
|
5219
|
+
console.log(`[TronDeepLink] Telegram Mini App detected, platform: ${platform}, version: ${version}`);
|
|
5220
|
+
if (platform === "unknown") {
|
|
5221
|
+
const userAgent = navigator.userAgent || "";
|
|
5222
|
+
if (/iPhone|iPad|iPod/i.test(userAgent)) {
|
|
5223
|
+
console.log(`[TronDeepLink] Detected iOS from user agent`);
|
|
5224
|
+
} else if (/Android/i.test(userAgent)) {
|
|
5225
|
+
console.log(`[TronDeepLink] Detected Android from user agent`);
|
|
5226
|
+
}
|
|
5227
|
+
}
|
|
5228
|
+
return true;
|
|
5229
|
+
}
|
|
5230
|
+
const isMobile = /Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
|
5231
|
+
navigator.userAgent
|
|
5232
|
+
);
|
|
5233
|
+
return isMobile;
|
|
5234
|
+
}
|
|
5235
|
+
/**
|
|
5236
|
+
* Connect wallet via deep link
|
|
5237
|
+
*
|
|
5238
|
+
* IMPORTANT: Deep links for TRON wallets are primarily for SIGNING, not connection.
|
|
5239
|
+
* TokenPocket and TronLink deep links support:
|
|
5240
|
+
* - Signing transactions: tron:signTransaction-version=1.0&protocol=TokenPocket&network=tron&chain_id=195&data={...}
|
|
5241
|
+
* - Signing messages: tron:signMessage-version=1.0&protocol=TokenPocket&network=tron&chain_id=195&data={...}
|
|
5242
|
+
*
|
|
5243
|
+
* For CONNECTION, you should use:
|
|
5244
|
+
* - WalletConnect (recommended for mobile)
|
|
5245
|
+
* - Browser extension (TronWeb/TronLink)
|
|
5246
|
+
*
|
|
5247
|
+
* Note: You can sign directly using signMessage() or signTransaction() without calling connect() first.
|
|
5248
|
+
* The wallet app will open and use the user's account automatically.
|
|
5249
|
+
*
|
|
5250
|
+
* This method attempts to open the wallet app, but cannot establish a connection
|
|
5251
|
+
* or retrieve the wallet address directly. The user must complete connection
|
|
5252
|
+
* through WalletConnect or browser extension after opening the app.
|
|
5253
|
+
*/
|
|
5254
|
+
async connect(chainId) {
|
|
5255
|
+
if (typeof window === "undefined") {
|
|
5256
|
+
throw new Error("Deep link requires a browser environment");
|
|
5257
|
+
}
|
|
5258
|
+
const targetChainId = Array.isArray(chainId) ? chainId[0] || _TronDeepLinkAdapter.TRON_MAINNET_CHAIN_ID : chainId || _TronDeepLinkAdapter.TRON_MAINNET_CHAIN_ID;
|
|
5259
|
+
try {
|
|
5260
|
+
this.setState("connecting" /* CONNECTING */);
|
|
5261
|
+
const isAvailable = await this.isAvailable();
|
|
5262
|
+
if (!isAvailable) {
|
|
5263
|
+
const isTelegram = !!(window.Telegram && window.Telegram.WebApp);
|
|
5264
|
+
if (isTelegram) {
|
|
5265
|
+
const platform = window.Telegram?.WebApp?.platform || "unknown";
|
|
5266
|
+
throw new Error(
|
|
5267
|
+
`Deep link is not available in Telegram Mini App on ${platform} platform. Please use WalletConnect or browser extension, or test on a mobile device.`
|
|
5268
|
+
);
|
|
5269
|
+
} else {
|
|
5270
|
+
throw new Error("Deep link is only available on mobile devices or Telegram Mini App. Please use WalletConnect or browser extension.");
|
|
5271
|
+
}
|
|
5272
|
+
}
|
|
5273
|
+
let deepLinkUrl = "";
|
|
5274
|
+
if (this.walletType === "tokenpocket" /* TOKENPOCKET */) {
|
|
5275
|
+
const actionId = `web-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
|
5276
|
+
const param = {
|
|
5277
|
+
action: "login",
|
|
5278
|
+
// Login action to open wallet
|
|
5279
|
+
actionId,
|
|
5280
|
+
blockchains: [{
|
|
5281
|
+
chainId: String(targetChainId),
|
|
5282
|
+
network: "tron"
|
|
5283
|
+
}],
|
|
5284
|
+
dappName: "Enclave Wallet SDK",
|
|
5285
|
+
dappIcon: "https://walletconnect.com/walletconnect-logo.svg",
|
|
5286
|
+
protocol: "TokenPocket",
|
|
5287
|
+
version: "1.0",
|
|
5288
|
+
expired: 1602
|
|
5289
|
+
// 30 minutes
|
|
5290
|
+
};
|
|
5291
|
+
const encodedParam = encodeURIComponent(JSON.stringify(param));
|
|
5292
|
+
deepLinkUrl = `tpoutside://pull.activity?param=${encodedParam}`;
|
|
5293
|
+
} else if (this.walletType === "tronlink" /* TRONLINK */) {
|
|
5294
|
+
deepLinkUrl = `tronlink://open?action=connect&network=tron`;
|
|
5295
|
+
}
|
|
5296
|
+
if (!deepLinkUrl) {
|
|
5297
|
+
throw new Error(`Unsupported wallet type: ${this.walletType}`);
|
|
5298
|
+
}
|
|
5299
|
+
console.log(`[TronDeepLink] ===== Connect via Deep Link =====`);
|
|
5300
|
+
console.log(`[TronDeepLink] Wallet Type:`, this.walletType);
|
|
5301
|
+
console.log(`[TronDeepLink] Chain ID:`, targetChainId);
|
|
5302
|
+
console.log(`[TronDeepLink] Deep Link URL:`, deepLinkUrl);
|
|
5303
|
+
console.log(`[TronDeepLink] Note: Deep links are for signing, not connection. After opening the app, please use WalletConnect to establish connection.`);
|
|
5304
|
+
console.log(`[TronDeepLink] ==================================`);
|
|
5305
|
+
window.location.href = deepLinkUrl;
|
|
5306
|
+
await new Promise((resolve) => setTimeout(resolve, 1e3));
|
|
5307
|
+
throw new ConnectionRejectedError(
|
|
5308
|
+
`\u5DF2\u6253\u5F00 ${this.walletType === "tokenpocket" /* TOKENPOCKET */ ? "TokenPocket" : "TronLink"} \u5E94\u7528\u3002
|
|
5309
|
+
|
|
5310
|
+
\u6CE8\u610F\uFF1A\u6DF1\u5EA6\u94FE\u63A5\u4E3B\u8981\u7528\u4E8E\u7B7E\u540D\u4EA4\u6613\uFF0C\u4E0D\u80FD\u76F4\u63A5\u5EFA\u7ACB\u8FDE\u63A5\u3002
|
|
5311
|
+
|
|
5312
|
+
\u8BF7\u4F7F\u7528\u4EE5\u4E0B\u65B9\u5F0F\u5B8C\u6210\u8FDE\u63A5\uFF1A
|
|
5313
|
+
1. \u5728\u94B1\u5305\u5E94\u7528\u4E2D\u4F7F\u7528 WalletConnect \u626B\u63CF\u4E8C\u7EF4\u7801
|
|
5314
|
+
2. \u6216\u4F7F\u7528\u6D4F\u89C8\u5668\u6269\u5C55\uFF08\u5982 TronLink\uFF09\u8FDE\u63A5
|
|
5315
|
+
|
|
5316
|
+
Deep link opened ${this.walletType} app. Please use WalletConnect or browser extension to complete the connection.`
|
|
5317
|
+
);
|
|
5318
|
+
} catch (error) {
|
|
5319
|
+
this.setState("error" /* ERROR */);
|
|
5320
|
+
this.setAccount(null);
|
|
5321
|
+
if (error instanceof ConnectionRejectedError) {
|
|
5322
|
+
throw error;
|
|
5323
|
+
}
|
|
5324
|
+
throw new Error(`Failed to open ${this.walletType} via deep link: ${error.message}`);
|
|
5325
|
+
}
|
|
5326
|
+
}
|
|
5327
|
+
/**
|
|
5328
|
+
* Disconnect wallet
|
|
5329
|
+
*/
|
|
5330
|
+
async disconnect() {
|
|
5331
|
+
this.setState("disconnected" /* DISCONNECTED */);
|
|
5332
|
+
this.setAccount(null);
|
|
5333
|
+
}
|
|
5334
|
+
/**
|
|
5335
|
+
* Sign message via deep link
|
|
5336
|
+
*
|
|
5337
|
+
* TokenPocket deep link format for signing:
|
|
5338
|
+
* tron:signMessage-version=1.0&protocol=TokenPocket&network=tron&chain_id=195&data={message}
|
|
5339
|
+
*
|
|
5340
|
+
* Reference: https://help.tokenpocket.pro/developer-cn/scan-protocol/tron
|
|
5341
|
+
*
|
|
5342
|
+
* Note: Deep links can sign directly without establishing a connection first.
|
|
5343
|
+
* The wallet app will open and use the user's account to sign the message.
|
|
5344
|
+
* The signature result will be returned via callback URL or app-to-app communication.
|
|
5345
|
+
*/
|
|
5346
|
+
async signMessage(message) {
|
|
5347
|
+
if (typeof window === "undefined") {
|
|
5348
|
+
throw new Error("Deep link requires a browser environment");
|
|
5349
|
+
}
|
|
5350
|
+
const isAvailable = await this.isAvailable();
|
|
5351
|
+
if (!isAvailable) {
|
|
5352
|
+
const isTelegram = !!(window.Telegram && window.Telegram.WebApp);
|
|
5353
|
+
if (isTelegram) {
|
|
5354
|
+
const platform = window.Telegram?.WebApp?.platform || "unknown";
|
|
5355
|
+
throw new Error(
|
|
5356
|
+
`Deep link signing is not available in Telegram Mini App on ${platform} platform. Please use WalletConnect or test on a mobile device.`
|
|
5357
|
+
);
|
|
5358
|
+
} else {
|
|
5359
|
+
throw new Error("Deep link signing is only available on mobile devices or Telegram Mini App.");
|
|
5360
|
+
}
|
|
5361
|
+
}
|
|
5362
|
+
let deepLinkUrl = "";
|
|
5363
|
+
if (this.walletType === "tokenpocket" /* TOKENPOCKET */) {
|
|
5364
|
+
const actionId = `web-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
|
5365
|
+
const param = {
|
|
5366
|
+
action: "sign",
|
|
5367
|
+
actionId,
|
|
5368
|
+
message,
|
|
5369
|
+
hash: false,
|
|
5370
|
+
signType: "ethPersonalSign",
|
|
5371
|
+
// For TRON, we use ethPersonalSign as it's similar
|
|
5372
|
+
memo: "TRON message signature",
|
|
5373
|
+
blockchains: [{
|
|
5374
|
+
chainId: "195",
|
|
5375
|
+
// TRON Mainnet chain ID as string
|
|
5376
|
+
network: "tron"
|
|
5377
|
+
}],
|
|
5378
|
+
dappName: "Enclave Wallet SDK",
|
|
5379
|
+
dappIcon: "https://walletconnect.com/walletconnect-logo.svg",
|
|
5380
|
+
protocol: "TokenPocket",
|
|
5381
|
+
version: "1.1.8",
|
|
5382
|
+
expired: 0
|
|
5383
|
+
// No expiration
|
|
5384
|
+
// callbackUrl: optional, if you want to receive callback
|
|
5385
|
+
// callbackSchema: optional, custom schema for callback
|
|
5386
|
+
};
|
|
5387
|
+
const encodedParam = encodeURIComponent(JSON.stringify(param));
|
|
5388
|
+
deepLinkUrl = `tpoutside://pull.activity?param=${encodedParam}`;
|
|
5389
|
+
console.log(`[TronDeepLink] Using TokenPocket tpoutside:// format for signMessage`);
|
|
5390
|
+
console.log(`[TronDeepLink] Param object:`, param);
|
|
5391
|
+
} else if (this.walletType === "tronlink" /* TRONLINK */) {
|
|
5392
|
+
const encodedMessage = encodeURIComponent(message);
|
|
5393
|
+
deepLinkUrl = `tronlink://signMessage?message=${encodedMessage}`;
|
|
5394
|
+
}
|
|
5395
|
+
if (!deepLinkUrl) {
|
|
5396
|
+
throw new MethodNotSupportedError("signMessage", this.type);
|
|
5397
|
+
}
|
|
5398
|
+
console.log(`[TronDeepLink] ===== Sign Message via Deep Link =====`);
|
|
5399
|
+
console.log(`[TronDeepLink] Wallet Type:`, this.walletType);
|
|
5400
|
+
console.log(`[TronDeepLink] Message:`, message);
|
|
5401
|
+
console.log(`[TronDeepLink] Deep Link URL:`, deepLinkUrl);
|
|
5402
|
+
console.log(`[TronDeepLink] Full URL length:`, deepLinkUrl.length);
|
|
5403
|
+
console.log(`[TronDeepLink] ========================================`);
|
|
5404
|
+
window.location.href = deepLinkUrl;
|
|
5405
|
+
throw new Error(
|
|
5406
|
+
`Deep link opened ${this.walletType} for signing. The signature will be handled by the wallet app. This adapter cannot retrieve the signature directly from deep links. Consider using WalletConnect or browser extension for programmatic signing.`
|
|
5407
|
+
);
|
|
5408
|
+
}
|
|
5409
|
+
/**
|
|
5410
|
+
* Sign transaction via deep link
|
|
5411
|
+
*
|
|
5412
|
+
* TokenPocket deep link format:
|
|
5413
|
+
* tron:signTransaction-version=1.0&protocol=TokenPocket&network=tron&chain_id=195&data={transaction}
|
|
5414
|
+
*
|
|
5415
|
+
* Reference: https://help.tokenpocket.pro/developer-cn/scan-protocol/tron
|
|
5416
|
+
*
|
|
5417
|
+
* Note: Deep links can sign directly without establishing a connection first.
|
|
5418
|
+
* The wallet app will open and use the user's account to sign the transaction.
|
|
5419
|
+
* The transaction data should be a JSON string containing the TRON transaction object.
|
|
5420
|
+
* The signature result will be returned via callback URL or app-to-app communication.
|
|
5421
|
+
*/
|
|
5422
|
+
async signTransaction(transaction) {
|
|
5423
|
+
if (typeof window === "undefined") {
|
|
5424
|
+
throw new Error("Deep link requires a browser environment");
|
|
5425
|
+
}
|
|
5426
|
+
const isAvailable = await this.isAvailable();
|
|
5427
|
+
if (!isAvailable) {
|
|
5428
|
+
const isTelegram = !!(window.Telegram && window.Telegram.WebApp);
|
|
5429
|
+
if (isTelegram) {
|
|
5430
|
+
const platform = window.Telegram?.WebApp?.platform || "unknown";
|
|
5431
|
+
throw new Error(
|
|
5432
|
+
`Deep link signing is not available in Telegram Mini App on ${platform} platform. Please use WalletConnect or test on a mobile device.`
|
|
5433
|
+
);
|
|
5434
|
+
} else {
|
|
5435
|
+
throw new Error("Deep link signing is only available on mobile devices or Telegram Mini App.");
|
|
5436
|
+
}
|
|
5437
|
+
}
|
|
5438
|
+
let deepLinkUrl = "";
|
|
5439
|
+
let actionId = "";
|
|
5440
|
+
let param = null;
|
|
5441
|
+
if (this.walletType === "tokenpocket" /* TOKENPOCKET */) {
|
|
5442
|
+
actionId = `web-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
|
5443
|
+
let transactionData;
|
|
5444
|
+
if (typeof transaction === "string") {
|
|
5445
|
+
transactionData = transaction;
|
|
5446
|
+
} else if (transaction.raw_data_hex) {
|
|
5447
|
+
transactionData = JSON.stringify(transaction);
|
|
5448
|
+
} else {
|
|
5449
|
+
transactionData = JSON.stringify(transaction);
|
|
5450
|
+
}
|
|
5451
|
+
param = {
|
|
5452
|
+
action: "pushTransaction",
|
|
5453
|
+
actionId,
|
|
5454
|
+
txData: transactionData,
|
|
5455
|
+
// Transaction data as JSON string
|
|
5456
|
+
blockchains: [{
|
|
5457
|
+
chainId: "195",
|
|
5458
|
+
// TRON Mainnet chain ID as string
|
|
5459
|
+
network: "tron"
|
|
5460
|
+
}],
|
|
5461
|
+
dappName: "Enclave Wallet SDK",
|
|
5462
|
+
dappIcon: "https://walletconnect.com/walletconnect-logo.svg",
|
|
5463
|
+
protocol: "TokenPocket",
|
|
5464
|
+
version: "1.1.8",
|
|
5465
|
+
expired: 0
|
|
5466
|
+
// No expiration
|
|
5467
|
+
};
|
|
5468
|
+
if (this.callbackSchema) {
|
|
5469
|
+
param.callbackSchema = this.callbackSchema;
|
|
5470
|
+
} else if (this.callbackUrl) {
|
|
5471
|
+
param.callbackUrl = this.callbackUrl;
|
|
5472
|
+
} else {
|
|
5473
|
+
if (typeof window !== "undefined" && window.location) {
|
|
5474
|
+
param.callbackSchema = `${window.location.protocol}//${window.location.host}${window.location.pathname}`;
|
|
5475
|
+
}
|
|
5476
|
+
}
|
|
5477
|
+
const encodedParam = encodeURIComponent(JSON.stringify(param));
|
|
5478
|
+
deepLinkUrl = `tpoutside://pull.activity?param=${encodedParam}`;
|
|
5479
|
+
console.log(`[TronDeepLink] Using TokenPocket tpoutside:// format for signTransaction`);
|
|
5480
|
+
console.log(`[TronDeepLink] Param object (without txData):`, { ...param, txData: "[TRANSACTION DATA]" });
|
|
5481
|
+
} else if (this.walletType === "tronlink" /* TRONLINK */) {
|
|
5482
|
+
const transactionData = typeof transaction === "string" ? transaction : JSON.stringify(transaction);
|
|
5483
|
+
const encodedData = encodeURIComponent(transactionData);
|
|
5484
|
+
deepLinkUrl = `tronlink://signTransaction?transaction=${encodedData}`;
|
|
5485
|
+
}
|
|
5486
|
+
if (!deepLinkUrl) {
|
|
5487
|
+
throw new MethodNotSupportedError("signTransaction", this.type);
|
|
5488
|
+
}
|
|
5489
|
+
console.log(`[TronDeepLink] ===== Sign Transaction via Deep Link =====`);
|
|
5490
|
+
console.log(`[TronDeepLink] Wallet Type:`, this.walletType);
|
|
5491
|
+
console.log(`[TronDeepLink] Transaction:`, transaction);
|
|
5492
|
+
console.log(`[TronDeepLink] Transaction keys:`, transaction ? Object.keys(transaction) : "N/A");
|
|
5493
|
+
if (transaction && typeof transaction === "object") {
|
|
5494
|
+
console.log(`[TronDeepLink] Transaction has raw_data:`, !!transaction.raw_data);
|
|
5495
|
+
console.log(`[TronDeepLink] Transaction has raw_data_hex:`, !!transaction.raw_data_hex);
|
|
5496
|
+
console.log(`[TronDeepLink] Transaction has txID:`, !!transaction.txID);
|
|
5497
|
+
}
|
|
5498
|
+
if (actionId) {
|
|
5499
|
+
console.log(`[TronDeepLink] Action ID:`, actionId);
|
|
5500
|
+
}
|
|
5501
|
+
if (param) {
|
|
5502
|
+
console.log(`[TronDeepLink] Callback Schema:`, param.callbackSchema || param.callbackUrl || "None");
|
|
5503
|
+
}
|
|
5504
|
+
console.log(`[TronDeepLink] Deep Link URL:`, deepLinkUrl);
|
|
5505
|
+
console.log(`[TronDeepLink] Full URL length:`, deepLinkUrl.length);
|
|
5506
|
+
if (deepLinkUrl.length > 200) {
|
|
5507
|
+
console.log(`[TronDeepLink] URL (first 200 chars):`, deepLinkUrl.substring(0, 200) + "...");
|
|
5508
|
+
console.log(`[TronDeepLink] URL (last 100 chars):`, "..." + deepLinkUrl.substring(deepLinkUrl.length - 100));
|
|
5509
|
+
}
|
|
5510
|
+
console.log(`[TronDeepLink] ===========================================`);
|
|
5511
|
+
if (this.walletType === "tokenpocket" /* TOKENPOCKET */ && param && param.callbackSchema) {
|
|
5512
|
+
return new Promise((resolve, reject) => {
|
|
5513
|
+
this.pendingActions.set(actionId, { resolve, reject });
|
|
5514
|
+
window.location.href = deepLinkUrl;
|
|
5515
|
+
setTimeout(() => {
|
|
5516
|
+
if (this.pendingActions.has(actionId)) {
|
|
5517
|
+
this.pendingActions.delete(actionId);
|
|
5518
|
+
reject(new Error("Transaction signature timeout: No response from wallet app"));
|
|
5519
|
+
}
|
|
5520
|
+
}, 3e4);
|
|
5521
|
+
});
|
|
5522
|
+
}
|
|
5523
|
+
window.location.href = deepLinkUrl;
|
|
5524
|
+
throw new SignatureRejectedError(
|
|
5525
|
+
`\u5DF2\u6253\u5F00 ${this.walletType === "tokenpocket" /* TOKENPOCKET */ ? "TokenPocket" : "TronLink"} \u8FDB\u884C\u4EA4\u6613\u7B7E\u540D\u3002
|
|
5526
|
+
|
|
5527
|
+
\u6CE8\u610F\uFF1A\u6DF1\u5EA6\u94FE\u63A5\u65E0\u6CD5\u76F4\u63A5\u8FD4\u56DE\u7B7E\u540D\u7ED3\u679C\u3002
|
|
5528
|
+
|
|
5529
|
+
\u7B7E\u540D\u7ED3\u679C\u5C06\u901A\u8FC7\u4EE5\u4E0B\u65B9\u5F0F\u8FD4\u56DE\uFF1A
|
|
5530
|
+
1. \u94B1\u5305\u5E94\u7528\u7684\u56DE\u8C03 URL
|
|
5531
|
+
2. \u5E94\u7528\u95F4\u901A\u4FE1\uFF08App-to-App\uFF09
|
|
5532
|
+
|
|
5533
|
+
\u5982\u9700\u7A0B\u5E8F\u5316\u83B7\u53D6\u7B7E\u540D\uFF0C\u8BF7\u4F7F\u7528 WalletConnect \u6216\u6D4F\u89C8\u5668\u6269\u5C55\u3002
|
|
5534
|
+
|
|
5535
|
+
Deep link opened ${this.walletType} for transaction signing. The signature will be handled by the wallet app via callback. For programmatic signing, use WalletConnect or browser extension.`
|
|
5536
|
+
);
|
|
5537
|
+
}
|
|
5538
|
+
/**
|
|
5539
|
+
* Read contract (not supported via deep link)
|
|
5540
|
+
*/
|
|
5541
|
+
async readContract(_params) {
|
|
5542
|
+
throw new MethodNotSupportedError("readContract", this.type);
|
|
5543
|
+
}
|
|
5544
|
+
/**
|
|
5545
|
+
* Write contract (not supported via deep link)
|
|
5546
|
+
*/
|
|
5547
|
+
async writeContract(_params) {
|
|
5548
|
+
throw new MethodNotSupportedError("writeContract", this.type);
|
|
5549
|
+
}
|
|
5550
|
+
/**
|
|
5551
|
+
* Estimate gas (not supported)
|
|
5552
|
+
*/
|
|
5553
|
+
async estimateGas(_params) {
|
|
5554
|
+
throw new MethodNotSupportedError("estimateGas", this.type);
|
|
5555
|
+
}
|
|
5556
|
+
/**
|
|
5557
|
+
* Wait for transaction (not supported)
|
|
5558
|
+
*/
|
|
5559
|
+
async waitForTransaction(_txHash, _confirmations) {
|
|
5560
|
+
throw new MethodNotSupportedError("waitForTransaction", this.type);
|
|
5561
|
+
}
|
|
5562
|
+
/**
|
|
5563
|
+
* Get provider (not applicable for deep links)
|
|
5564
|
+
*/
|
|
5565
|
+
getProvider() {
|
|
5566
|
+
return null;
|
|
5567
|
+
}
|
|
5568
|
+
};
|
|
5569
|
+
// TRON Mainnet chain ID
|
|
5570
|
+
_TronDeepLinkAdapter.TRON_MAINNET_CHAIN_ID = 195;
|
|
5571
|
+
var TronDeepLinkAdapter = _TronDeepLinkAdapter;
|
|
5572
|
+
|
|
2218
5573
|
// src/auth/message-generator.ts
|
|
5574
|
+
init_types();
|
|
2219
5575
|
var AuthMessageGenerator = class {
|
|
2220
5576
|
constructor(domain) {
|
|
2221
5577
|
this.domain = domain;
|
|
@@ -2290,6 +5646,9 @@ var AuthMessageGenerator = class {
|
|
|
2290
5646
|
return Array.from(array, (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
2291
5647
|
}
|
|
2292
5648
|
};
|
|
5649
|
+
|
|
5650
|
+
// src/auth/signature-verifier.ts
|
|
5651
|
+
init_types();
|
|
2293
5652
|
var SignatureVerifier = class {
|
|
2294
5653
|
/**
|
|
2295
5654
|
* 验证签名
|
|
@@ -2331,7 +5690,11 @@ var SignatureVerifier = class {
|
|
|
2331
5690
|
}
|
|
2332
5691
|
};
|
|
2333
5692
|
|
|
5693
|
+
// src/detection/detector.ts
|
|
5694
|
+
init_types();
|
|
5695
|
+
|
|
2334
5696
|
// src/detection/supported-wallets.ts
|
|
5697
|
+
init_types();
|
|
2335
5698
|
var SUPPORTED_WALLETS = {
|
|
2336
5699
|
["metamask" /* METAMASK */]: {
|
|
2337
5700
|
type: "metamask" /* METAMASK */,
|
|
@@ -2378,6 +5741,20 @@ var SUPPORTED_WALLETS = {
|
|
|
2378
5741
|
chainType: ChainType.EVM,
|
|
2379
5742
|
// 可以用于任何链
|
|
2380
5743
|
description: "Import wallet using private key (for development)"
|
|
5744
|
+
},
|
|
5745
|
+
["deep-link-evm" /* DEEP_LINK_EVM */]: {
|
|
5746
|
+
type: "deep-link-evm" /* DEEP_LINK_EVM */,
|
|
5747
|
+
name: "Deep Link (EVM)",
|
|
5748
|
+
chainType: ChainType.EVM,
|
|
5749
|
+
icon: "https://tokenpocket.pro/icon.png",
|
|
5750
|
+
description: "Deep link connection for EVM chains (TokenPocket, ImToken, etc.)"
|
|
5751
|
+
},
|
|
5752
|
+
["deep-link-tron" /* DEEP_LINK_TRON */]: {
|
|
5753
|
+
type: "deep-link-tron" /* DEEP_LINK_TRON */,
|
|
5754
|
+
name: "Deep Link (TRON)",
|
|
5755
|
+
chainType: ChainType.TRON,
|
|
5756
|
+
icon: "https://tokenpocket.pro/icon.png",
|
|
5757
|
+
description: "Deep link connection for TRON chain (TokenPocket, TronLink, etc.)"
|
|
2381
5758
|
}
|
|
2382
5759
|
};
|
|
2383
5760
|
function getWalletMetadata(type) {
|
|
@@ -2525,6 +5902,7 @@ function shortenTronAddress(address, chars = 4) {
|
|
|
2525
5902
|
}
|
|
2526
5903
|
|
|
2527
5904
|
// src/utils/validation.ts
|
|
5905
|
+
init_types();
|
|
2528
5906
|
function validateAddress(address, chainType) {
|
|
2529
5907
|
switch (chainType) {
|
|
2530
5908
|
case ChainType.EVM:
|
|
@@ -2594,6 +5972,6 @@ function removeHexPrefix(value) {
|
|
|
2594
5972
|
// src/index.ts
|
|
2595
5973
|
var index_default = WalletManager;
|
|
2596
5974
|
|
|
2597
|
-
export { AdapterRegistry, AuthMessageGenerator, BrowserWalletAdapter, CHAIN_INFO, ChainNotSupportedError, ChainType, ConfigurationError, ConnectionRejectedError, EVMPrivateKeyAdapter, MetaMaskAdapter, MethodNotSupportedError, NetworkError, SUPPORTED_WALLETS, SignatureRejectedError, SignatureVerifier, TransactionFailedError, TronLinkAdapter, WalletAdapter, WalletDetector, WalletManager, WalletNotAvailableError, WalletNotConnectedError, WalletSDKError, WalletState, WalletType, compareEVMAddresses, compareTronAddresses, compareUniversalAddresses, createUniversalAddress, index_default as default, ensureHexPrefix, formatEVMAddress, fromHex, getAddressFromUniversalAddress, getChainIdFromUniversalAddress, getChainInfo, getChainType, getEVMWallets, getTronWallets, getWalletMetadata, hexToNumber, isEVMChain, isHex, isTronChain, isValidChainId, isValidEVMAddress, isValidSignature, isValidTransactionHash, isValidTronAddress, isValidTronHexAddress, isValidUniversalAddress, numberToHex, parseUniversalAddress, removeHexPrefix, shortenAddress, shortenTronAddress, toHex, validateAddress, validateAddressForChain };
|
|
5975
|
+
export { AdapterRegistry, AuthMessageGenerator, BrowserWalletAdapter, CHAIN_INFO, ChainNotSupportedError, ChainType, ConfigurationError, ConnectionRejectedError, DeepLinkAdapter, DeepLinkProviderType, DeepLinkWalletType, EVMPrivateKeyAdapter, ImTokenDeepLinkProvider, MetaMaskAdapter, MetaMaskDeepLinkProvider, MethodNotSupportedError, NetworkError, OKXDeepLinkProvider, QRCodeSignStatus, QRCodeSigner, SUPPORTED_WALLETS, SignatureRejectedError, SignatureVerifier, TokenPocketDeepLinkProvider, TransactionFailedError, TronDeepLinkAdapter, TronLinkAdapter, TronLinkDeepLinkProvider, WalletAdapter, WalletConnectAdapter, WalletConnectTronAdapter, WalletDetector, WalletManager, WalletNotAvailableError, WalletNotConnectedError, WalletSDKError, WalletState, WalletType, compareEVMAddresses, compareTronAddresses, compareUniversalAddresses, createUniversalAddress, index_default as default, ensureHexPrefix, formatEVMAddress, fromHex, getAddressFromUniversalAddress, getChainIdFromUniversalAddress, getChainInfo, getChainType, getEVMWallets, getTronWallets, getWalletMetadata, hexToNumber, isEVMChain, isHex, isTronChain, isValidChainId, isValidEVMAddress, isValidSignature, isValidTransactionHash, isValidTronAddress, isValidTronHexAddress, isValidUniversalAddress, numberToHex, parseUniversalAddress, removeHexPrefix, shortenAddress, shortenTronAddress, toHex, validateAddress, validateAddressForChain };
|
|
2598
5976
|
//# sourceMappingURL=index.mjs.map
|
|
2599
5977
|
//# sourceMappingURL=index.mjs.map
|