@enclave-hq/wallet-sdk 1.2.2 → 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/react/index.mjs
CHANGED
|
@@ -1,10 +1,580 @@
|
|
|
1
|
-
import React, { createContext, useState, useCallback, useEffect, useContext } from 'react';
|
|
2
|
-
import EventEmitter from 'eventemitter3';
|
|
3
1
|
import { ChainType as ChainType$1 } from '@enclave-hq/chain-utils';
|
|
4
|
-
import {
|
|
2
|
+
import React2, { createContext, useState, useCallback, useEffect, useContext, useRef } from 'react';
|
|
3
|
+
import EventEmitter from 'eventemitter3';
|
|
4
|
+
import { createWalletClient, custom, createPublicClient, http, isAddress, getAddress } from 'viem';
|
|
5
5
|
import { privateKeyToAccount } from 'viem/accounts';
|
|
6
|
+
import EthereumProvider from '@walletconnect/ethereum-provider';
|
|
7
|
+
import { WalletConnectChainID, WalletConnectWallet } from '@tronweb3/walletconnect-tron';
|
|
8
|
+
import QRCode from 'qrcode';
|
|
6
9
|
|
|
7
|
-
|
|
10
|
+
var __defProp = Object.defineProperty;
|
|
11
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
12
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
13
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
14
|
+
var __esm = (fn, res) => function __init() {
|
|
15
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
16
|
+
};
|
|
17
|
+
var __export = (target, all) => {
|
|
18
|
+
for (var name in all)
|
|
19
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
20
|
+
};
|
|
21
|
+
var __copyProps = (to, from, except, desc) => {
|
|
22
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
23
|
+
for (let key of __getOwnPropNames(from))
|
|
24
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
25
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
26
|
+
}
|
|
27
|
+
return to;
|
|
28
|
+
};
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
var ChainType;
|
|
31
|
+
var init_types = __esm({
|
|
32
|
+
"src/core/types.ts"() {
|
|
33
|
+
ChainType = ChainType$1;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// src/adapters/deep-link/providers/tokenpocket.ts
|
|
38
|
+
var tokenpocket_exports = {};
|
|
39
|
+
__export(tokenpocket_exports, {
|
|
40
|
+
TokenPocketDeepLinkProvider: () => TokenPocketDeepLinkProvider
|
|
41
|
+
});
|
|
42
|
+
var TokenPocketDeepLinkProvider;
|
|
43
|
+
var init_tokenpocket = __esm({
|
|
44
|
+
"src/adapters/deep-link/providers/tokenpocket.ts"() {
|
|
45
|
+
init_types();
|
|
46
|
+
TokenPocketDeepLinkProvider = class {
|
|
47
|
+
constructor(options) {
|
|
48
|
+
this.name = "TokenPocket";
|
|
49
|
+
this.icon = "https://tokenpocket.pro/icon.png";
|
|
50
|
+
this.supportedChainTypes = [ChainType.EVM, ChainType.TRON];
|
|
51
|
+
this.callbackUrl = options?.callbackUrl;
|
|
52
|
+
this.callbackSchema = options?.callbackSchema;
|
|
53
|
+
}
|
|
54
|
+
async isAvailable() {
|
|
55
|
+
if (typeof window === "undefined") {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
const isTelegramMiniApp = !!(window.Telegram && window.Telegram.WebApp);
|
|
59
|
+
if (isTelegramMiniApp) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
const isMobile = /Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
|
63
|
+
navigator.userAgent
|
|
64
|
+
);
|
|
65
|
+
return isMobile;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Generate unique actionId
|
|
69
|
+
*/
|
|
70
|
+
generateActionId() {
|
|
71
|
+
return `web-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Get callback configuration
|
|
75
|
+
*/
|
|
76
|
+
getCallbackConfig() {
|
|
77
|
+
if (this.callbackSchema) {
|
|
78
|
+
return { callbackSchema: this.callbackSchema };
|
|
79
|
+
}
|
|
80
|
+
if (this.callbackUrl) {
|
|
81
|
+
return { callbackUrl: this.callbackUrl };
|
|
82
|
+
}
|
|
83
|
+
if (typeof window !== "undefined" && window.location) {
|
|
84
|
+
return {
|
|
85
|
+
callbackSchema: `${window.location.protocol}//${window.location.host}${window.location.pathname}`
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return {};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Get blockchain configuration based on chain type
|
|
92
|
+
*/
|
|
93
|
+
getBlockchainConfig(chainId, chainType) {
|
|
94
|
+
if (chainType === ChainType.TRON) {
|
|
95
|
+
return {
|
|
96
|
+
chainId: String(chainId),
|
|
97
|
+
network: "tron"
|
|
98
|
+
};
|
|
99
|
+
} else if (chainType === ChainType.EVM) {
|
|
100
|
+
return {
|
|
101
|
+
chainId: String(chainId),
|
|
102
|
+
network: "ethereum"
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
throw new Error(`Unsupported chain type: ${chainType}`);
|
|
106
|
+
}
|
|
107
|
+
buildSignMessageLink(params) {
|
|
108
|
+
const actionId = this.generateActionId();
|
|
109
|
+
const callback = this.getCallbackConfig();
|
|
110
|
+
const blockchain = this.getBlockchainConfig(params.chainId, params.chainType);
|
|
111
|
+
const param = {
|
|
112
|
+
action: "sign",
|
|
113
|
+
actionId,
|
|
114
|
+
message: params.message,
|
|
115
|
+
hash: false,
|
|
116
|
+
signType: params.chainType === ChainType.TRON ? "ethPersonalSign" : "ethPersonalSign",
|
|
117
|
+
memo: `${params.chainType} message signature`,
|
|
118
|
+
blockchains: [blockchain],
|
|
119
|
+
dappName: "Enclave Wallet SDK",
|
|
120
|
+
dappIcon: "https://walletconnect.com/walletconnect-logo.svg",
|
|
121
|
+
protocol: "TokenPocket",
|
|
122
|
+
version: "1.1.8",
|
|
123
|
+
expired: 0,
|
|
124
|
+
...callback
|
|
125
|
+
};
|
|
126
|
+
const encodedParam = encodeURIComponent(JSON.stringify(param));
|
|
127
|
+
const url = `tpoutside://pull.activity?param=${encodedParam}`;
|
|
128
|
+
return {
|
|
129
|
+
url,
|
|
130
|
+
actionId,
|
|
131
|
+
...callback
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
buildSignTransactionLink(params) {
|
|
135
|
+
const actionId = this.generateActionId();
|
|
136
|
+
const callback = this.getCallbackConfig();
|
|
137
|
+
const blockchain = this.getBlockchainConfig(params.chainId, params.chainType);
|
|
138
|
+
let transactionData;
|
|
139
|
+
if (typeof params.transaction === "string") {
|
|
140
|
+
transactionData = params.transaction;
|
|
141
|
+
} else {
|
|
142
|
+
transactionData = JSON.stringify(params.transaction);
|
|
143
|
+
}
|
|
144
|
+
const param = {
|
|
145
|
+
action: "pushTransaction",
|
|
146
|
+
actionId,
|
|
147
|
+
txData: transactionData,
|
|
148
|
+
blockchains: [blockchain],
|
|
149
|
+
dappName: "Enclave Wallet SDK",
|
|
150
|
+
dappIcon: "https://walletconnect.com/walletconnect-logo.svg",
|
|
151
|
+
protocol: "TokenPocket",
|
|
152
|
+
version: "1.1.8",
|
|
153
|
+
expired: 0,
|
|
154
|
+
...callback
|
|
155
|
+
};
|
|
156
|
+
const encodedParam = encodeURIComponent(JSON.stringify(param));
|
|
157
|
+
const url = `tpoutside://pull.activity?param=${encodedParam}`;
|
|
158
|
+
return {
|
|
159
|
+
url,
|
|
160
|
+
actionId,
|
|
161
|
+
...callback
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
buildConnectLink(params) {
|
|
165
|
+
const actionId = this.generateActionId();
|
|
166
|
+
const blockchain = this.getBlockchainConfig(params.chainId, params.chainType);
|
|
167
|
+
const param = {
|
|
168
|
+
action: "login",
|
|
169
|
+
actionId,
|
|
170
|
+
blockchains: [blockchain],
|
|
171
|
+
dappName: "Enclave Wallet SDK",
|
|
172
|
+
dappIcon: "https://walletconnect.com/walletconnect-logo.svg",
|
|
173
|
+
protocol: "TokenPocket",
|
|
174
|
+
version: "1.0",
|
|
175
|
+
expired: 1602
|
|
176
|
+
// 30 minutes
|
|
177
|
+
};
|
|
178
|
+
const encodedParam = encodeURIComponent(JSON.stringify(param));
|
|
179
|
+
const url = `tpoutside://pull.activity?param=${encodedParam}`;
|
|
180
|
+
return {
|
|
181
|
+
url,
|
|
182
|
+
actionId
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
parseCallbackResult(urlParams) {
|
|
186
|
+
const actionId = urlParams.get("actionId");
|
|
187
|
+
const resultParam = urlParams.get("result");
|
|
188
|
+
const error = urlParams.get("error");
|
|
189
|
+
let result = null;
|
|
190
|
+
if (resultParam) {
|
|
191
|
+
try {
|
|
192
|
+
result = JSON.parse(decodeURIComponent(resultParam));
|
|
193
|
+
} catch (e) {
|
|
194
|
+
result = resultParam;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
return {
|
|
198
|
+
actionId,
|
|
199
|
+
result,
|
|
200
|
+
error
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
getDefaultCallbackSchema() {
|
|
204
|
+
if (typeof window !== "undefined" && window.location) {
|
|
205
|
+
return `${window.location.protocol}//${window.location.host}${window.location.pathname}`;
|
|
206
|
+
}
|
|
207
|
+
return "";
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
// src/adapters/deep-link/providers/tronlink.ts
|
|
214
|
+
var tronlink_exports = {};
|
|
215
|
+
__export(tronlink_exports, {
|
|
216
|
+
TronLinkDeepLinkProvider: () => TronLinkDeepLinkProvider
|
|
217
|
+
});
|
|
218
|
+
var TronLinkDeepLinkProvider;
|
|
219
|
+
var init_tronlink = __esm({
|
|
220
|
+
"src/adapters/deep-link/providers/tronlink.ts"() {
|
|
221
|
+
init_types();
|
|
222
|
+
TronLinkDeepLinkProvider = class {
|
|
223
|
+
constructor() {
|
|
224
|
+
this.name = "TronLink";
|
|
225
|
+
this.icon = "https://www.tronlink.org/static/logoIcon.svg";
|
|
226
|
+
this.supportedChainTypes = [ChainType.TRON];
|
|
227
|
+
}
|
|
228
|
+
async isAvailable() {
|
|
229
|
+
if (typeof window === "undefined") {
|
|
230
|
+
return false;
|
|
231
|
+
}
|
|
232
|
+
const isTelegramMiniApp = !!(window.Telegram && window.Telegram.WebApp);
|
|
233
|
+
if (isTelegramMiniApp) {
|
|
234
|
+
return true;
|
|
235
|
+
}
|
|
236
|
+
const isMobile = /Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
|
237
|
+
navigator.userAgent
|
|
238
|
+
);
|
|
239
|
+
return isMobile;
|
|
240
|
+
}
|
|
241
|
+
buildSignMessageLink(params) {
|
|
242
|
+
if (params.chainType !== ChainType.TRON) {
|
|
243
|
+
throw new Error("TronLink only supports TRON chain");
|
|
244
|
+
}
|
|
245
|
+
const encodedMessage = encodeURIComponent(params.message);
|
|
246
|
+
const url = `tronlink://signMessage?message=${encodedMessage}`;
|
|
247
|
+
const actionId = `tronlink-${Date.now()}`;
|
|
248
|
+
return {
|
|
249
|
+
url,
|
|
250
|
+
actionId
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
buildSignTransactionLink(params) {
|
|
254
|
+
if (params.chainType !== ChainType.TRON) {
|
|
255
|
+
throw new Error("TronLink only supports TRON chain");
|
|
256
|
+
}
|
|
257
|
+
const transactionData = typeof params.transaction === "string" ? params.transaction : JSON.stringify(params.transaction);
|
|
258
|
+
const encodedData = encodeURIComponent(transactionData);
|
|
259
|
+
const url = `tronlink://signTransaction?transaction=${encodedData}`;
|
|
260
|
+
const actionId = `tronlink-${Date.now()}`;
|
|
261
|
+
return {
|
|
262
|
+
url,
|
|
263
|
+
actionId
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
buildConnectLink(params) {
|
|
267
|
+
if (params.chainType !== ChainType.TRON) {
|
|
268
|
+
throw new Error("TronLink only supports TRON chain");
|
|
269
|
+
}
|
|
270
|
+
const url = `tronlink://open?action=connect&network=tron`;
|
|
271
|
+
return {
|
|
272
|
+
url
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
parseCallbackResult(_urlParams) {
|
|
276
|
+
return {
|
|
277
|
+
actionId: null,
|
|
278
|
+
result: null,
|
|
279
|
+
error: null
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
// src/adapters/deep-link/providers/imtoken.ts
|
|
287
|
+
var imtoken_exports = {};
|
|
288
|
+
__export(imtoken_exports, {
|
|
289
|
+
ImTokenDeepLinkProvider: () => ImTokenDeepLinkProvider
|
|
290
|
+
});
|
|
291
|
+
var ImTokenDeepLinkProvider;
|
|
292
|
+
var init_imtoken = __esm({
|
|
293
|
+
"src/adapters/deep-link/providers/imtoken.ts"() {
|
|
294
|
+
init_types();
|
|
295
|
+
ImTokenDeepLinkProvider = class {
|
|
296
|
+
constructor(options) {
|
|
297
|
+
this.name = "ImToken";
|
|
298
|
+
this.icon = "https://token.im/static/img/logo.png";
|
|
299
|
+
this.supportedChainTypes = [ChainType.EVM, ChainType.TRON];
|
|
300
|
+
this.callbackUrl = options?.callbackUrl;
|
|
301
|
+
this.callbackSchema = options?.callbackSchema;
|
|
302
|
+
}
|
|
303
|
+
async isAvailable() {
|
|
304
|
+
if (typeof window === "undefined") {
|
|
305
|
+
return false;
|
|
306
|
+
}
|
|
307
|
+
const isTelegramMiniApp = !!(window.Telegram && window.Telegram.WebApp);
|
|
308
|
+
if (isTelegramMiniApp) {
|
|
309
|
+
return true;
|
|
310
|
+
}
|
|
311
|
+
const isMobile = /Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
|
312
|
+
navigator.userAgent
|
|
313
|
+
);
|
|
314
|
+
return isMobile;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Generate unique actionId
|
|
318
|
+
*/
|
|
319
|
+
generateActionId() {
|
|
320
|
+
return `imtoken-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
|
321
|
+
}
|
|
322
|
+
/**
|
|
323
|
+
* Get callback configuration
|
|
324
|
+
*/
|
|
325
|
+
getCallbackConfig() {
|
|
326
|
+
if (this.callbackSchema) {
|
|
327
|
+
return { callbackSchema: this.callbackSchema };
|
|
328
|
+
}
|
|
329
|
+
if (this.callbackUrl) {
|
|
330
|
+
return { callbackUrl: this.callbackUrl };
|
|
331
|
+
}
|
|
332
|
+
if (typeof window !== "undefined" && window.location) {
|
|
333
|
+
return {
|
|
334
|
+
callbackSchema: `${window.location.protocol}//${window.location.host}${window.location.pathname}`
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
return {};
|
|
338
|
+
}
|
|
339
|
+
buildSignMessageLink(params) {
|
|
340
|
+
const actionId = this.generateActionId();
|
|
341
|
+
const callback = this.getCallbackConfig();
|
|
342
|
+
const dappUrl = `${window.location.origin}${window.location.pathname}?action=signMessage&message=${encodeURIComponent(params.message)}&chainId=${params.chainId}`;
|
|
343
|
+
const encodedDappUrl = encodeURIComponent(dappUrl);
|
|
344
|
+
const url = `imtokenv2://navigate/DappView?url=${encodedDappUrl}`;
|
|
345
|
+
return {
|
|
346
|
+
url,
|
|
347
|
+
actionId,
|
|
348
|
+
...callback
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
buildSignTransactionLink(params) {
|
|
352
|
+
const actionId = this.generateActionId();
|
|
353
|
+
const callback = this.getCallbackConfig();
|
|
354
|
+
const transactionData = typeof params.transaction === "string" ? params.transaction : JSON.stringify(params.transaction);
|
|
355
|
+
const dappUrl = `${window.location.origin}${window.location.pathname}?action=signTransaction&transaction=${encodeURIComponent(transactionData)}&chainId=${params.chainId}`;
|
|
356
|
+
const encodedDappUrl = encodeURIComponent(dappUrl);
|
|
357
|
+
const url = `imtokenv2://navigate/DappView?url=${encodedDappUrl}`;
|
|
358
|
+
return {
|
|
359
|
+
url,
|
|
360
|
+
actionId,
|
|
361
|
+
...callback
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
buildConnectLink(params) {
|
|
365
|
+
const dappUrl = `${window.location.origin}${window.location.pathname}?action=connect&chainId=${params.chainId}`;
|
|
366
|
+
const encodedDappUrl = encodeURIComponent(dappUrl);
|
|
367
|
+
const url = `imtokenv2://navigate/DappView?url=${encodedDappUrl}`;
|
|
368
|
+
return {
|
|
369
|
+
url
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
parseCallbackResult(urlParams) {
|
|
373
|
+
const actionId = urlParams.get("actionId");
|
|
374
|
+
const resultParam = urlParams.get("result");
|
|
375
|
+
const error = urlParams.get("error");
|
|
376
|
+
let result = null;
|
|
377
|
+
if (resultParam) {
|
|
378
|
+
try {
|
|
379
|
+
result = JSON.parse(decodeURIComponent(resultParam));
|
|
380
|
+
} catch (e) {
|
|
381
|
+
result = resultParam;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return {
|
|
385
|
+
actionId,
|
|
386
|
+
result,
|
|
387
|
+
error
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
getDefaultCallbackSchema() {
|
|
391
|
+
if (typeof window !== "undefined" && window.location) {
|
|
392
|
+
return `${window.location.protocol}//${window.location.host}${window.location.pathname}`;
|
|
393
|
+
}
|
|
394
|
+
return "";
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
// src/adapters/deep-link/providers/metamask.ts
|
|
401
|
+
var metamask_exports = {};
|
|
402
|
+
__export(metamask_exports, {
|
|
403
|
+
MetaMaskDeepLinkProvider: () => MetaMaskDeepLinkProvider
|
|
404
|
+
});
|
|
405
|
+
var MetaMaskDeepLinkProvider;
|
|
406
|
+
var init_metamask = __esm({
|
|
407
|
+
"src/adapters/deep-link/providers/metamask.ts"() {
|
|
408
|
+
init_types();
|
|
409
|
+
MetaMaskDeepLinkProvider = class {
|
|
410
|
+
constructor() {
|
|
411
|
+
this.name = "MetaMask";
|
|
412
|
+
this.icon = "https://upload.wikimedia.org/wikipedia/commons/3/36/MetaMask_Fox.svg";
|
|
413
|
+
this.supportedChainTypes = [ChainType.EVM];
|
|
414
|
+
}
|
|
415
|
+
async isAvailable() {
|
|
416
|
+
if (typeof window === "undefined") {
|
|
417
|
+
return false;
|
|
418
|
+
}
|
|
419
|
+
const isTelegramMiniApp = !!(window.Telegram && window.Telegram.WebApp);
|
|
420
|
+
if (isTelegramMiniApp) {
|
|
421
|
+
return true;
|
|
422
|
+
}
|
|
423
|
+
const isMobile = /Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
|
424
|
+
navigator.userAgent
|
|
425
|
+
);
|
|
426
|
+
return isMobile;
|
|
427
|
+
}
|
|
428
|
+
buildSignMessageLink(params) {
|
|
429
|
+
if (params.chainType !== ChainType.EVM) {
|
|
430
|
+
throw new Error("MetaMask only supports EVM chains");
|
|
431
|
+
}
|
|
432
|
+
const dappUrl = `${window.location.origin}${window.location.pathname}?action=signMessage&message=${encodeURIComponent(params.message)}&chainId=${params.chainId}`;
|
|
433
|
+
const encodedDappUrl = encodeURIComponent(dappUrl);
|
|
434
|
+
const url = `https://link.metamask.io/dapp/${encodedDappUrl}`;
|
|
435
|
+
const actionId = `metamask-${Date.now()}`;
|
|
436
|
+
return {
|
|
437
|
+
url,
|
|
438
|
+
actionId
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
buildSignTransactionLink(params) {
|
|
442
|
+
if (params.chainType !== ChainType.EVM) {
|
|
443
|
+
throw new Error("MetaMask only supports EVM chains");
|
|
444
|
+
}
|
|
445
|
+
const transactionData = typeof params.transaction === "string" ? params.transaction : JSON.stringify(params.transaction);
|
|
446
|
+
const dappUrl = `${window.location.origin}${window.location.pathname}?action=signTransaction&transaction=${encodeURIComponent(transactionData)}&chainId=${params.chainId}`;
|
|
447
|
+
const encodedDappUrl = encodeURIComponent(dappUrl);
|
|
448
|
+
const url = `https://link.metamask.io/dapp/${encodedDappUrl}`;
|
|
449
|
+
const actionId = `metamask-${Date.now()}`;
|
|
450
|
+
return {
|
|
451
|
+
url,
|
|
452
|
+
actionId
|
|
453
|
+
};
|
|
454
|
+
}
|
|
455
|
+
buildConnectLink(params) {
|
|
456
|
+
if (params.chainType !== ChainType.EVM) {
|
|
457
|
+
throw new Error("MetaMask only supports EVM chains");
|
|
458
|
+
}
|
|
459
|
+
const dappUrl = `${window.location.origin}${window.location.pathname}?action=connect&chainId=${params.chainId}`;
|
|
460
|
+
const encodedDappUrl = encodeURIComponent(dappUrl);
|
|
461
|
+
const url = `https://link.metamask.io/dapp/${encodedDappUrl}`;
|
|
462
|
+
return {
|
|
463
|
+
url
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
parseCallbackResult(urlParams) {
|
|
467
|
+
const actionId = urlParams.get("actionId");
|
|
468
|
+
const resultParam = urlParams.get("result");
|
|
469
|
+
const error = urlParams.get("error");
|
|
470
|
+
let result = null;
|
|
471
|
+
if (resultParam) {
|
|
472
|
+
try {
|
|
473
|
+
result = JSON.parse(decodeURIComponent(resultParam));
|
|
474
|
+
} catch (e) {
|
|
475
|
+
result = resultParam;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
return {
|
|
479
|
+
actionId,
|
|
480
|
+
result,
|
|
481
|
+
error
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
getDefaultCallbackSchema() {
|
|
485
|
+
if (typeof window !== "undefined" && window.location) {
|
|
486
|
+
return `${window.location.protocol}//${window.location.host}${window.location.pathname}`;
|
|
487
|
+
}
|
|
488
|
+
return "";
|
|
489
|
+
}
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
// src/adapters/deep-link/providers/okx.ts
|
|
495
|
+
var okx_exports = {};
|
|
496
|
+
__export(okx_exports, {
|
|
497
|
+
OKXDeepLinkProvider: () => OKXDeepLinkProvider
|
|
498
|
+
});
|
|
499
|
+
var OKXDeepLinkProvider;
|
|
500
|
+
var init_okx = __esm({
|
|
501
|
+
"src/adapters/deep-link/providers/okx.ts"() {
|
|
502
|
+
init_types();
|
|
503
|
+
OKXDeepLinkProvider = class {
|
|
504
|
+
constructor() {
|
|
505
|
+
this.name = "OKX";
|
|
506
|
+
this.icon = "https://www.okx.com/favicon.ico";
|
|
507
|
+
this.supportedChainTypes = [ChainType.EVM, ChainType.TRON];
|
|
508
|
+
}
|
|
509
|
+
async isAvailable() {
|
|
510
|
+
if (typeof window === "undefined") {
|
|
511
|
+
return false;
|
|
512
|
+
}
|
|
513
|
+
const isTelegramMiniApp = !!(window.Telegram && window.Telegram.WebApp);
|
|
514
|
+
if (isTelegramMiniApp) {
|
|
515
|
+
return true;
|
|
516
|
+
}
|
|
517
|
+
const isMobile = /Mobile|Android|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
|
518
|
+
navigator.userAgent
|
|
519
|
+
);
|
|
520
|
+
return isMobile;
|
|
521
|
+
}
|
|
522
|
+
buildSignMessageLink(params) {
|
|
523
|
+
const dappUrl = `${window.location.origin}${window.location.pathname}?action=signMessage&message=${encodeURIComponent(params.message)}&chainId=${params.chainId}`;
|
|
524
|
+
const encodedDappUrl = encodeURIComponent(dappUrl);
|
|
525
|
+
const url = `okx://wallet/dapp/url?dappUrl=${encodedDappUrl}`;
|
|
526
|
+
const actionId = `okx-${Date.now()}`;
|
|
527
|
+
return {
|
|
528
|
+
url,
|
|
529
|
+
actionId
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
buildSignTransactionLink(params) {
|
|
533
|
+
const transactionData = typeof params.transaction === "string" ? params.transaction : JSON.stringify(params.transaction);
|
|
534
|
+
const dappUrl = `${window.location.origin}${window.location.pathname}?action=signTransaction&transaction=${encodeURIComponent(transactionData)}&chainId=${params.chainId}`;
|
|
535
|
+
const encodedDappUrl = encodeURIComponent(dappUrl);
|
|
536
|
+
const url = `okx://wallet/dapp/url?dappUrl=${encodedDappUrl}`;
|
|
537
|
+
const actionId = `okx-${Date.now()}`;
|
|
538
|
+
return {
|
|
539
|
+
url,
|
|
540
|
+
actionId
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
buildConnectLink(params) {
|
|
544
|
+
const dappUrl = `${window.location.origin}${window.location.pathname}?action=connect&chainId=${params.chainId}`;
|
|
545
|
+
const encodedDappUrl = encodeURIComponent(dappUrl);
|
|
546
|
+
const url = `okx://wallet/dapp/url?dappUrl=${encodedDappUrl}`;
|
|
547
|
+
return {
|
|
548
|
+
url
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
parseCallbackResult(urlParams) {
|
|
552
|
+
const actionId = urlParams.get("actionId");
|
|
553
|
+
const resultParam = urlParams.get("result");
|
|
554
|
+
const error = urlParams.get("error");
|
|
555
|
+
let result = null;
|
|
556
|
+
if (resultParam) {
|
|
557
|
+
try {
|
|
558
|
+
result = JSON.parse(decodeURIComponent(resultParam));
|
|
559
|
+
} catch (e) {
|
|
560
|
+
result = resultParam;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
return {
|
|
564
|
+
actionId,
|
|
565
|
+
result,
|
|
566
|
+
error
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
getDefaultCallbackSchema() {
|
|
570
|
+
if (typeof window !== "undefined" && window.location) {
|
|
571
|
+
return `${window.location.protocol}//${window.location.host}${window.location.pathname}`;
|
|
572
|
+
}
|
|
573
|
+
return "";
|
|
574
|
+
}
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
});
|
|
8
578
|
var TypedEventEmitter = class {
|
|
9
579
|
constructor() {
|
|
10
580
|
this.emitter = new EventEmitter();
|
|
@@ -33,7 +603,12 @@ var TypedEventEmitter = class {
|
|
|
33
603
|
return this;
|
|
34
604
|
}
|
|
35
605
|
};
|
|
36
|
-
|
|
606
|
+
|
|
607
|
+
// src/core/adapter-registry.ts
|
|
608
|
+
init_types();
|
|
609
|
+
|
|
610
|
+
// src/adapters/base/wallet-adapter.ts
|
|
611
|
+
init_types();
|
|
37
612
|
|
|
38
613
|
// src/core/errors.ts
|
|
39
614
|
var WalletSDKError = class _WalletSDKError extends Error {
|
|
@@ -104,6 +679,18 @@ var MethodNotSupportedError = class extends WalletSDKError {
|
|
|
104
679
|
this.name = "MethodNotSupportedError";
|
|
105
680
|
}
|
|
106
681
|
};
|
|
682
|
+
var ConfigurationError = class extends WalletSDKError {
|
|
683
|
+
constructor(message, details) {
|
|
684
|
+
super(message, "CONFIGURATION_ERROR", details);
|
|
685
|
+
this.name = "ConfigurationError";
|
|
686
|
+
}
|
|
687
|
+
};
|
|
688
|
+
var NetworkError = class extends WalletSDKError {
|
|
689
|
+
constructor(message, details) {
|
|
690
|
+
super(message, "NETWORK_ERROR", details);
|
|
691
|
+
this.name = "NetworkError";
|
|
692
|
+
}
|
|
693
|
+
};
|
|
107
694
|
|
|
108
695
|
// src/adapters/base/wallet-adapter.ts
|
|
109
696
|
var WalletAdapter = class extends EventEmitter {
|
|
@@ -113,6 +700,13 @@ var WalletAdapter = class extends EventEmitter {
|
|
|
113
700
|
this.state = "disconnected" /* DISCONNECTED */;
|
|
114
701
|
this.currentAccount = null;
|
|
115
702
|
}
|
|
703
|
+
/**
|
|
704
|
+
* Check if the wallet is currently connected
|
|
705
|
+
* @returns true if the wallet is connected (state is CONNECTED and has an account)
|
|
706
|
+
*/
|
|
707
|
+
isConnected() {
|
|
708
|
+
return this.state === "connected" /* CONNECTED */ && this.currentAccount !== null;
|
|
709
|
+
}
|
|
116
710
|
/**
|
|
117
711
|
* Get the signer's address (implements ISigner interface)
|
|
118
712
|
* Returns the native address of the current account
|
|
@@ -182,6 +776,7 @@ var WalletAdapter = class extends EventEmitter {
|
|
|
182
776
|
};
|
|
183
777
|
|
|
184
778
|
// src/adapters/base/browser-wallet-adapter.ts
|
|
779
|
+
init_types();
|
|
185
780
|
var BrowserWalletAdapter = class extends WalletAdapter {
|
|
186
781
|
/**
|
|
187
782
|
* 检查钱包是否可用
|
|
@@ -215,6 +810,9 @@ var BrowserWalletAdapter = class extends WalletAdapter {
|
|
|
215
810
|
}
|
|
216
811
|
};
|
|
217
812
|
|
|
813
|
+
// src/adapters/evm/metamask.ts
|
|
814
|
+
init_types();
|
|
815
|
+
|
|
218
816
|
// src/utils/address/universal-address.ts
|
|
219
817
|
function createUniversalAddress(chainId, address) {
|
|
220
818
|
return `${chainId}:${address}`;
|
|
@@ -459,6 +1057,7 @@ var MetaMaskAdapter = class extends BrowserWalletAdapter {
|
|
|
459
1057
|
*/
|
|
460
1058
|
async connect(chainId) {
|
|
461
1059
|
await this.ensureAvailable();
|
|
1060
|
+
const targetChainId = Array.isArray(chainId) ? chainId[0] : chainId;
|
|
462
1061
|
try {
|
|
463
1062
|
this.setState("connecting" /* CONNECTING */);
|
|
464
1063
|
const provider = this.getBrowserProvider();
|
|
@@ -472,10 +1071,10 @@ var MetaMaskAdapter = class extends BrowserWalletAdapter {
|
|
|
472
1071
|
method: "eth_chainId"
|
|
473
1072
|
});
|
|
474
1073
|
const parsedChainId = parseInt(currentChainId, 16);
|
|
475
|
-
if (
|
|
476
|
-
await this.switchChain(
|
|
1074
|
+
if (targetChainId && targetChainId !== parsedChainId) {
|
|
1075
|
+
await this.switchChain(targetChainId);
|
|
477
1076
|
}
|
|
478
|
-
const finalChainId =
|
|
1077
|
+
const finalChainId = targetChainId || parsedChainId;
|
|
479
1078
|
const viemChain = this.getViemChain(finalChainId);
|
|
480
1079
|
this.walletClient = createWalletClient({
|
|
481
1080
|
account: accounts[0],
|
|
@@ -861,6 +1460,7 @@ var MetaMaskAdapter = class extends BrowserWalletAdapter {
|
|
|
861
1460
|
};
|
|
862
1461
|
|
|
863
1462
|
// src/adapters/tron/tronlink.ts
|
|
1463
|
+
init_types();
|
|
864
1464
|
var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
|
|
865
1465
|
constructor() {
|
|
866
1466
|
super(...arguments);
|
|
@@ -908,6 +1508,7 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
|
|
|
908
1508
|
*/
|
|
909
1509
|
async connect(chainId) {
|
|
910
1510
|
await this.ensureAvailable();
|
|
1511
|
+
const targetChainId = Array.isArray(chainId) ? chainId[0] : chainId;
|
|
911
1512
|
try {
|
|
912
1513
|
this.setState("connecting" /* CONNECTING */);
|
|
913
1514
|
const w = window;
|
|
@@ -962,7 +1563,7 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
|
|
|
962
1563
|
if (!address) {
|
|
963
1564
|
throw new Error("Failed to get Tron address. Please make sure your wallet is unlocked and try again.");
|
|
964
1565
|
}
|
|
965
|
-
const tronChainId =
|
|
1566
|
+
const tronChainId = targetChainId || _TronLinkAdapter.TRON_MAINNET_CHAIN_ID;
|
|
966
1567
|
const account = {
|
|
967
1568
|
universalAddress: createUniversalAddress(tronChainId, address),
|
|
968
1569
|
nativeAddress: address,
|
|
@@ -1292,6 +1893,7 @@ var _TronLinkAdapter = class _TronLinkAdapter extends BrowserWalletAdapter {
|
|
|
1292
1893
|
// Tron 主网链 ID
|
|
1293
1894
|
_TronLinkAdapter.TRON_MAINNET_CHAIN_ID = 195;
|
|
1294
1895
|
var TronLinkAdapter = _TronLinkAdapter;
|
|
1896
|
+
init_types();
|
|
1295
1897
|
var EVMPrivateKeyAdapter = class extends WalletAdapter {
|
|
1296
1898
|
constructor() {
|
|
1297
1899
|
super(...arguments);
|
|
@@ -1305,27 +1907,28 @@ var EVMPrivateKeyAdapter = class extends WalletAdapter {
|
|
|
1305
1907
|
/**
|
|
1306
1908
|
* 连接(导入私钥)
|
|
1307
1909
|
*/
|
|
1308
|
-
async connect(chainId
|
|
1910
|
+
async connect(chainId) {
|
|
1309
1911
|
if (!this.privateKey) {
|
|
1310
1912
|
throw new Error("Private key not set. Call setPrivateKey() first.");
|
|
1311
1913
|
}
|
|
1312
1914
|
try {
|
|
1313
1915
|
this.setState("connecting" /* CONNECTING */);
|
|
1314
1916
|
const account = privateKeyToAccount(this.privateKey);
|
|
1917
|
+
const targetChainId = Array.isArray(chainId) ? chainId[0] : chainId || 1;
|
|
1315
1918
|
this.walletClient = createWalletClient({
|
|
1316
1919
|
account,
|
|
1317
|
-
chain: this.getViemChain(
|
|
1920
|
+
chain: this.getViemChain(targetChainId),
|
|
1318
1921
|
transport: http()
|
|
1319
1922
|
});
|
|
1320
1923
|
this.publicClient = createPublicClient({
|
|
1321
|
-
chain: this.getViemChain(
|
|
1924
|
+
chain: this.getViemChain(targetChainId),
|
|
1322
1925
|
transport: http()
|
|
1323
1926
|
});
|
|
1324
1927
|
const address = formatEVMAddress(account.address);
|
|
1325
1928
|
const accountInfo = {
|
|
1326
|
-
universalAddress: createUniversalAddress(
|
|
1929
|
+
universalAddress: createUniversalAddress(targetChainId, address),
|
|
1327
1930
|
nativeAddress: address,
|
|
1328
|
-
chainId,
|
|
1931
|
+
chainId: targetChainId,
|
|
1329
1932
|
chainType: ChainType.EVM,
|
|
1330
1933
|
isActive: true
|
|
1331
1934
|
};
|
|
@@ -1537,61 +2140,2290 @@ var EVMPrivateKeyAdapter = class extends WalletAdapter {
|
|
|
1537
2140
|
};
|
|
1538
2141
|
}
|
|
1539
2142
|
};
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
this.
|
|
1545
|
-
this.
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
this.
|
|
1552
|
-
|
|
1553
|
-
|
|
2143
|
+
init_types();
|
|
2144
|
+
var _WalletConnectAdapter = class _WalletConnectAdapter extends WalletAdapter {
|
|
2145
|
+
constructor(projectId) {
|
|
2146
|
+
super();
|
|
2147
|
+
this.type = "walletconnect" /* WALLETCONNECT */;
|
|
2148
|
+
this.chainType = ChainType.EVM;
|
|
2149
|
+
this.name = "WalletConnect";
|
|
2150
|
+
this.icon = "https://avatars.githubusercontent.com/u/37784886";
|
|
2151
|
+
this.provider = null;
|
|
2152
|
+
this.walletClient = null;
|
|
2153
|
+
this.publicClient = null;
|
|
2154
|
+
this.supportedChains = [];
|
|
2155
|
+
/**
|
|
2156
|
+
* Handle accounts changed
|
|
2157
|
+
*/
|
|
2158
|
+
this.handleAccountsChanged = (accounts) => {
|
|
2159
|
+
if (accounts.length === 0) {
|
|
2160
|
+
this.setState("disconnected" /* DISCONNECTED */);
|
|
2161
|
+
this.setAccount(null);
|
|
2162
|
+
this.emitAccountChanged(null);
|
|
2163
|
+
} else {
|
|
2164
|
+
const address = formatEVMAddress(accounts[0]);
|
|
2165
|
+
const account = {
|
|
2166
|
+
universalAddress: createUniversalAddress(this.currentAccount.chainId, address),
|
|
2167
|
+
nativeAddress: address,
|
|
2168
|
+
chainId: this.currentAccount.chainId,
|
|
2169
|
+
chainType: ChainType.EVM,
|
|
2170
|
+
isActive: true
|
|
2171
|
+
};
|
|
2172
|
+
this.setAccount(account);
|
|
2173
|
+
this.emitAccountChanged(account);
|
|
2174
|
+
}
|
|
2175
|
+
};
|
|
2176
|
+
/**
|
|
2177
|
+
* Handle chain changed
|
|
2178
|
+
*/
|
|
2179
|
+
this.handleChainChanged = (chainIdHex) => {
|
|
2180
|
+
const chainId = parseInt(chainIdHex, 16);
|
|
2181
|
+
if (this.currentAccount) {
|
|
2182
|
+
const account = {
|
|
2183
|
+
...this.currentAccount,
|
|
2184
|
+
chainId,
|
|
2185
|
+
universalAddress: createUniversalAddress(chainId, this.currentAccount.nativeAddress)
|
|
2186
|
+
};
|
|
2187
|
+
this.setAccount(account);
|
|
2188
|
+
this.emitChainChanged(chainId);
|
|
2189
|
+
const viemChain = this.getViemChain(chainId);
|
|
2190
|
+
const chainInfo = getChainInfo(chainId);
|
|
2191
|
+
const primaryRpcUrl = chainInfo?.rpcUrls[0];
|
|
2192
|
+
if (this.provider) {
|
|
2193
|
+
this.walletClient = createWalletClient({
|
|
2194
|
+
account: this.currentAccount.nativeAddress,
|
|
2195
|
+
chain: viemChain,
|
|
2196
|
+
transport: custom(this.provider)
|
|
2197
|
+
});
|
|
2198
|
+
this.publicClient = createPublicClient({
|
|
2199
|
+
chain: viemChain,
|
|
2200
|
+
transport: primaryRpcUrl ? http(primaryRpcUrl) : custom(this.provider)
|
|
2201
|
+
});
|
|
2202
|
+
}
|
|
2203
|
+
}
|
|
2204
|
+
};
|
|
2205
|
+
/**
|
|
2206
|
+
* Handle disconnect
|
|
2207
|
+
*/
|
|
2208
|
+
this.handleDisconnect = () => {
|
|
2209
|
+
this.setState("disconnected" /* DISCONNECTED */);
|
|
2210
|
+
this.setAccount(null);
|
|
2211
|
+
if (_WalletConnectAdapter.providerInstance === this.provider) {
|
|
2212
|
+
_WalletConnectAdapter.providerInstance = null;
|
|
2213
|
+
_WalletConnectAdapter.providerProjectId = null;
|
|
2214
|
+
}
|
|
2215
|
+
this.provider = null;
|
|
2216
|
+
this.walletClient = null;
|
|
2217
|
+
this.publicClient = null;
|
|
2218
|
+
this.emitDisconnected();
|
|
2219
|
+
};
|
|
2220
|
+
if (!projectId) {
|
|
2221
|
+
throw new ConfigurationError("WalletConnect projectId is required");
|
|
2222
|
+
}
|
|
2223
|
+
this.projectId = projectId;
|
|
1554
2224
|
}
|
|
1555
2225
|
/**
|
|
1556
|
-
*
|
|
2226
|
+
* Check if WalletConnect is available
|
|
2227
|
+
* WalletConnect is always available (it's a web-based connection)
|
|
2228
|
+
* Also works in Telegram Mini Apps
|
|
1557
2229
|
*/
|
|
1558
|
-
|
|
1559
|
-
|
|
2230
|
+
async isAvailable() {
|
|
2231
|
+
return typeof window !== "undefined";
|
|
1560
2232
|
}
|
|
1561
2233
|
/**
|
|
1562
|
-
*
|
|
2234
|
+
* Check if running in Telegram environment (Mini App or Web)
|
|
2235
|
+
* Both Telegram Mini App (in client) and Telegram Web (web.telegram.org)
|
|
2236
|
+
* provide window.Telegram.WebApp API, so they are treated the same way.
|
|
2237
|
+
*
|
|
2238
|
+
* Reference: https://docs.reown.com/appkit/integrations/telegram-mini-apps
|
|
1563
2239
|
*/
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
2240
|
+
isTelegramMiniApp() {
|
|
2241
|
+
if (typeof window === "undefined") return false;
|
|
2242
|
+
const tg = window.Telegram?.WebApp;
|
|
2243
|
+
if (!tg) return false;
|
|
2244
|
+
const platform = tg.platform || "unknown";
|
|
2245
|
+
console.log("[WalletConnect] Telegram environment detected:", {
|
|
2246
|
+
platform,
|
|
2247
|
+
version: tg.version,
|
|
2248
|
+
isMiniApp: platform !== "web",
|
|
2249
|
+
// Mini App if not web platform
|
|
2250
|
+
isWeb: platform === "web"
|
|
2251
|
+
// Telegram Web if web platform
|
|
2252
|
+
});
|
|
2253
|
+
return true;
|
|
1570
2254
|
}
|
|
1571
2255
|
/**
|
|
1572
|
-
*
|
|
2256
|
+
* Get Telegram WebApp instance if available
|
|
1573
2257
|
*/
|
|
1574
|
-
|
|
1575
|
-
|
|
2258
|
+
getTelegramWebApp() {
|
|
2259
|
+
if (typeof window === "undefined") return null;
|
|
2260
|
+
return window.Telegram?.WebApp || null;
|
|
1576
2261
|
}
|
|
1577
2262
|
/**
|
|
1578
|
-
*
|
|
2263
|
+
* Close Telegram deep link popup (wc:// links)
|
|
2264
|
+
* In Telegram Mini Apps, WalletConnect may open a wc:// deep link popup
|
|
2265
|
+
* that doesn't automatically close after the operation completes.
|
|
2266
|
+
* This method attempts to close it by:
|
|
2267
|
+
* 1. Trying to close any open windows/popups
|
|
2268
|
+
* 2. Using Telegram WebApp API if available
|
|
2269
|
+
* 3. Navigating back or closing the popup
|
|
1579
2270
|
*/
|
|
1580
|
-
|
|
1581
|
-
|
|
2271
|
+
closeTelegramDeepLinkPopup() {
|
|
2272
|
+
if (!this.isTelegramMiniApp()) {
|
|
2273
|
+
return;
|
|
2274
|
+
}
|
|
2275
|
+
try {
|
|
2276
|
+
const tg = this.getTelegramWebApp();
|
|
2277
|
+
if (!tg) {
|
|
2278
|
+
return;
|
|
2279
|
+
}
|
|
2280
|
+
if (typeof window !== "undefined") {
|
|
2281
|
+
window.focus();
|
|
2282
|
+
if (tg.BackButton && tg.BackButton.isVisible) {
|
|
2283
|
+
console.log("[WalletConnect] Closing Telegram deep link popup via BackButton");
|
|
2284
|
+
}
|
|
2285
|
+
setTimeout(() => {
|
|
2286
|
+
if (document.hasFocus()) {
|
|
2287
|
+
console.log("[WalletConnect] Main window has focus, popup likely closed");
|
|
2288
|
+
} else {
|
|
2289
|
+
window.focus();
|
|
2290
|
+
console.log("[WalletConnect] Attempted to focus main window to close popup");
|
|
2291
|
+
}
|
|
2292
|
+
}, 500);
|
|
2293
|
+
const handleVisibilityChange = () => {
|
|
2294
|
+
if (document.visibilityState === "visible") {
|
|
2295
|
+
console.log("[WalletConnect] Page became visible, popup may have closed");
|
|
2296
|
+
document.removeEventListener("visibilitychange", handleVisibilityChange);
|
|
2297
|
+
}
|
|
2298
|
+
};
|
|
2299
|
+
document.addEventListener("visibilitychange", handleVisibilityChange);
|
|
2300
|
+
setTimeout(() => {
|
|
2301
|
+
document.removeEventListener("visibilitychange", handleVisibilityChange);
|
|
2302
|
+
}, 2e3);
|
|
2303
|
+
}
|
|
2304
|
+
} catch (error) {
|
|
2305
|
+
console.warn("[WalletConnect] Error closing Telegram deep link popup:", error);
|
|
2306
|
+
}
|
|
1582
2307
|
}
|
|
1583
2308
|
/**
|
|
1584
|
-
*
|
|
2309
|
+
* Connect wallet
|
|
2310
|
+
*
|
|
2311
|
+
* @param chainId - Single chain ID or array of chain IDs to request
|
|
2312
|
+
* If array is provided, wallet will be requested to connect to multiple chains
|
|
2313
|
+
* When multiple chains are requested, the wallet can switch between them
|
|
2314
|
+
* Default: 1 (Ethereum Mainnet)
|
|
2315
|
+
*
|
|
2316
|
+
* @example
|
|
2317
|
+
* // Single chain
|
|
2318
|
+
* await adapter.connect(1) // Ethereum only
|
|
2319
|
+
*
|
|
2320
|
+
* @example
|
|
2321
|
+
* // Multiple chains
|
|
2322
|
+
* await adapter.connect([1, 56, 137]) // Ethereum, BSC, Polygon
|
|
1585
2323
|
*/
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
const adapter = this.getAdapter(type);
|
|
1590
|
-
if (adapter && adapter.chainType === chainType) {
|
|
1591
|
-
types.push(type);
|
|
1592
|
-
}
|
|
2324
|
+
async connect(chainId) {
|
|
2325
|
+
if (typeof window === "undefined") {
|
|
2326
|
+
throw new Error("WalletConnect requires a browser environment");
|
|
1593
2327
|
}
|
|
1594
|
-
|
|
2328
|
+
if (_WalletConnectAdapter.providerInstance && _WalletConnectAdapter.providerProjectId === this.projectId) {
|
|
2329
|
+
const existingProvider = _WalletConnectAdapter.providerInstance;
|
|
2330
|
+
if (existingProvider.accounts && existingProvider.accounts.length > 0) {
|
|
2331
|
+
this.provider = existingProvider;
|
|
2332
|
+
let targetChains;
|
|
2333
|
+
if (Array.isArray(chainId)) {
|
|
2334
|
+
targetChains = chainId.length > 0 ? chainId : [1];
|
|
2335
|
+
} else if (chainId) {
|
|
2336
|
+
targetChains = [chainId];
|
|
2337
|
+
} else {
|
|
2338
|
+
targetChains = [1];
|
|
2339
|
+
}
|
|
2340
|
+
const existingChains = this.supportedChains || [];
|
|
2341
|
+
const mergedChains = [.../* @__PURE__ */ new Set([...existingChains, ...targetChains])];
|
|
2342
|
+
this.supportedChains = mergedChains;
|
|
2343
|
+
const currentChainId = existingProvider.chainId || targetChains[0];
|
|
2344
|
+
const address = formatEVMAddress(existingProvider.accounts[0]);
|
|
2345
|
+
const account = {
|
|
2346
|
+
universalAddress: createUniversalAddress(currentChainId, address),
|
|
2347
|
+
nativeAddress: address,
|
|
2348
|
+
chainId: currentChainId,
|
|
2349
|
+
chainType: ChainType.EVM,
|
|
2350
|
+
isActive: true
|
|
2351
|
+
};
|
|
2352
|
+
this.setState("connected" /* CONNECTED */);
|
|
2353
|
+
this.setAccount(account);
|
|
2354
|
+
if (!this.walletClient) {
|
|
2355
|
+
const viemChain = this.getViemChain(currentChainId);
|
|
2356
|
+
this.walletClient = createWalletClient({
|
|
2357
|
+
account: existingProvider.accounts[0],
|
|
2358
|
+
chain: viemChain,
|
|
2359
|
+
transport: custom(existingProvider)
|
|
2360
|
+
});
|
|
2361
|
+
const chainInfo = getChainInfo(currentChainId);
|
|
2362
|
+
const primaryRpcUrl = chainInfo?.rpcUrls[0];
|
|
2363
|
+
this.publicClient = createPublicClient({
|
|
2364
|
+
chain: viemChain,
|
|
2365
|
+
transport: primaryRpcUrl ? http(primaryRpcUrl) : custom(existingProvider)
|
|
2366
|
+
});
|
|
2367
|
+
this.setupEventListeners();
|
|
2368
|
+
}
|
|
2369
|
+
console.log("[WalletConnect] Reusing existing provider session");
|
|
2370
|
+
return account;
|
|
2371
|
+
}
|
|
2372
|
+
}
|
|
2373
|
+
if (this.state === "connected" /* CONNECTED */ && this.currentAccount && this.provider) {
|
|
2374
|
+
if (this.provider.accounts && this.provider.accounts.length > 0) {
|
|
2375
|
+
let targetChains;
|
|
2376
|
+
if (Array.isArray(chainId)) {
|
|
2377
|
+
targetChains = chainId.length > 0 ? chainId : [1];
|
|
2378
|
+
} else if (chainId) {
|
|
2379
|
+
targetChains = [chainId];
|
|
2380
|
+
} else {
|
|
2381
|
+
targetChains = [1];
|
|
2382
|
+
}
|
|
2383
|
+
const existingChains = this.supportedChains || [];
|
|
2384
|
+
const mergedChains = [.../* @__PURE__ */ new Set([...existingChains, ...targetChains])];
|
|
2385
|
+
this.supportedChains = mergedChains;
|
|
2386
|
+
console.log("[WalletConnect] Already connected, reusing existing connection");
|
|
2387
|
+
return this.currentAccount;
|
|
2388
|
+
} else {
|
|
2389
|
+
this.setState("disconnected" /* DISCONNECTED */);
|
|
2390
|
+
this.setAccount(null);
|
|
2391
|
+
this.provider = null;
|
|
2392
|
+
}
|
|
2393
|
+
}
|
|
2394
|
+
try {
|
|
2395
|
+
this.setState("connecting" /* CONNECTING */);
|
|
2396
|
+
let targetChains;
|
|
2397
|
+
if (Array.isArray(chainId)) {
|
|
2398
|
+
targetChains = chainId.length > 0 ? chainId : [1];
|
|
2399
|
+
} else if (chainId) {
|
|
2400
|
+
targetChains = [chainId];
|
|
2401
|
+
} else {
|
|
2402
|
+
targetChains = [1];
|
|
2403
|
+
}
|
|
2404
|
+
this.supportedChains = targetChains;
|
|
2405
|
+
const primaryChain = targetChains[0];
|
|
2406
|
+
const optionalChains = targetChains.slice(1);
|
|
2407
|
+
const isTelegram = this.isTelegramMiniApp();
|
|
2408
|
+
const telegramWebApp = this.getTelegramWebApp();
|
|
2409
|
+
let appUrl = "";
|
|
2410
|
+
if (typeof window !== "undefined") {
|
|
2411
|
+
try {
|
|
2412
|
+
if (window.location && window.location.origin) {
|
|
2413
|
+
appUrl = window.location.origin;
|
|
2414
|
+
} else if (window.location && window.location.href) {
|
|
2415
|
+
const url = new URL(window.location.href);
|
|
2416
|
+
appUrl = url.origin;
|
|
2417
|
+
}
|
|
2418
|
+
} catch (error) {
|
|
2419
|
+
console.warn("[WalletConnect] Failed to get origin from window.location:", error);
|
|
2420
|
+
}
|
|
2421
|
+
if (!appUrl) {
|
|
2422
|
+
appUrl = "https://enclave.network";
|
|
2423
|
+
}
|
|
2424
|
+
} else {
|
|
2425
|
+
appUrl = "https://enclave.network";
|
|
2426
|
+
}
|
|
2427
|
+
if (!appUrl || !appUrl.startsWith("http://") && !appUrl.startsWith("https://")) {
|
|
2428
|
+
appUrl = "https://enclave.network";
|
|
2429
|
+
}
|
|
2430
|
+
const icons = [
|
|
2431
|
+
"https://walletconnect.com/walletconnect-logo.svg",
|
|
2432
|
+
"https://avatars.githubusercontent.com/u/37784886"
|
|
2433
|
+
// WalletConnect GitHub avatar
|
|
2434
|
+
];
|
|
2435
|
+
const initOptions = {
|
|
2436
|
+
projectId: this.projectId,
|
|
2437
|
+
chains: [primaryChain],
|
|
2438
|
+
// Primary chain (required)
|
|
2439
|
+
showQrModal: true,
|
|
2440
|
+
// QR modal works in Telegram Mini Apps
|
|
2441
|
+
metadata: {
|
|
2442
|
+
name: "Enclave Wallet SDK",
|
|
2443
|
+
description: "Multi-chain wallet adapter for Enclave",
|
|
2444
|
+
url: appUrl,
|
|
2445
|
+
icons
|
|
2446
|
+
}
|
|
2447
|
+
};
|
|
2448
|
+
if (isTelegram && telegramWebApp) {
|
|
2449
|
+
const platform = telegramWebApp.platform || "unknown";
|
|
2450
|
+
const isMiniApp = platform !== "web";
|
|
2451
|
+
console.log("[WalletConnect] Detected Telegram environment:", {
|
|
2452
|
+
platform,
|
|
2453
|
+
isMiniApp,
|
|
2454
|
+
isWeb: platform === "web"
|
|
2455
|
+
});
|
|
2456
|
+
if (telegramWebApp.isExpanded === false) {
|
|
2457
|
+
telegramWebApp.expand();
|
|
2458
|
+
}
|
|
2459
|
+
}
|
|
2460
|
+
if (optionalChains.length > 0) {
|
|
2461
|
+
initOptions.optionalChains = optionalChains;
|
|
2462
|
+
} else {
|
|
2463
|
+
initOptions.optionalChains = [primaryChain];
|
|
2464
|
+
}
|
|
2465
|
+
const hasExistingProvider = _WalletConnectAdapter.providerInstance && _WalletConnectAdapter.providerProjectId === this.projectId;
|
|
2466
|
+
const needsReinit = hasExistingProvider && _WalletConnectAdapter.providerChains !== null && JSON.stringify(_WalletConnectAdapter.providerChains.sort()) !== JSON.stringify(targetChains.sort());
|
|
2467
|
+
if (needsReinit) {
|
|
2468
|
+
console.log("[WalletConnect] Provider initialized with different chains, reinitializing...", {
|
|
2469
|
+
existing: _WalletConnectAdapter.providerChains,
|
|
2470
|
+
requested: targetChains
|
|
2471
|
+
});
|
|
2472
|
+
const existingProvider = _WalletConnectAdapter.providerInstance;
|
|
2473
|
+
if (existingProvider) {
|
|
2474
|
+
try {
|
|
2475
|
+
if (existingProvider.accounts && existingProvider.accounts.length > 0) {
|
|
2476
|
+
await existingProvider.disconnect();
|
|
2477
|
+
}
|
|
2478
|
+
} catch (error) {
|
|
2479
|
+
console.warn("[WalletConnect] Error disconnecting existing provider:", error);
|
|
2480
|
+
}
|
|
2481
|
+
}
|
|
2482
|
+
_WalletConnectAdapter.providerInstance = null;
|
|
2483
|
+
_WalletConnectAdapter.providerChains = null;
|
|
2484
|
+
}
|
|
2485
|
+
if (_WalletConnectAdapter.providerInstance && _WalletConnectAdapter.providerProjectId === this.projectId) {
|
|
2486
|
+
this.provider = _WalletConnectAdapter.providerInstance;
|
|
2487
|
+
console.log("[WalletConnect] Reusing existing provider instance");
|
|
2488
|
+
if (this.provider.accounts && this.provider.accounts.length > 0) {
|
|
2489
|
+
console.log("[WalletConnect] Provider already has accounts, skipping enable()");
|
|
2490
|
+
} else {
|
|
2491
|
+
const hasSession = this.provider.session !== void 0 && this.provider.session !== null;
|
|
2492
|
+
console.log("[WalletConnect] Provider has no accounts, calling enable() to show QR modal");
|
|
2493
|
+
console.log("[WalletConnect] Provider state:", {
|
|
2494
|
+
accounts: this.provider.accounts,
|
|
2495
|
+
chainId: this.provider.chainId,
|
|
2496
|
+
hasSession,
|
|
2497
|
+
sessionTopic: this.provider.session?.topic
|
|
2498
|
+
});
|
|
2499
|
+
if (hasSession && (!this.provider.accounts || this.provider.accounts.length === 0)) {
|
|
2500
|
+
console.log("[WalletConnect] Found stale session, disconnecting before reconnecting...");
|
|
2501
|
+
try {
|
|
2502
|
+
await this.provider.disconnect();
|
|
2503
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
2504
|
+
} catch (disconnectError) {
|
|
2505
|
+
console.warn("[WalletConnect] Error disconnecting stale session:", disconnectError);
|
|
2506
|
+
}
|
|
2507
|
+
}
|
|
2508
|
+
try {
|
|
2509
|
+
console.log("[WalletConnect] Calling enable()...");
|
|
2510
|
+
const enableResult = await this.provider.enable();
|
|
2511
|
+
console.log("[WalletConnect] enable() completed, result:", enableResult);
|
|
2512
|
+
console.log("[WalletConnect] Provider state after enable():", {
|
|
2513
|
+
accounts: this.provider.accounts,
|
|
2514
|
+
chainId: this.provider.chainId,
|
|
2515
|
+
session: this.provider.session ? {
|
|
2516
|
+
topic: this.provider.session.topic,
|
|
2517
|
+
namespaces: this.provider.session.namespaces ? Object.keys(this.provider.session.namespaces) : "none"
|
|
2518
|
+
} : "none"
|
|
2519
|
+
});
|
|
2520
|
+
} catch (error) {
|
|
2521
|
+
console.error("[WalletConnect] enable() error:", error);
|
|
2522
|
+
if (error.code === 4001 || error.message?.includes("rejected") || error.message?.includes("User rejected")) {
|
|
2523
|
+
throw new ConnectionRejectedError(this.type);
|
|
2524
|
+
}
|
|
2525
|
+
throw error;
|
|
2526
|
+
}
|
|
2527
|
+
}
|
|
2528
|
+
} else if (_WalletConnectAdapter.providerInstance && _WalletConnectAdapter.providerProjectId === this.projectId) {
|
|
2529
|
+
this.provider = _WalletConnectAdapter.providerInstance;
|
|
2530
|
+
console.log("[WalletConnect] Reusing existing provider instance (not connected)");
|
|
2531
|
+
if (this.provider.accounts && this.provider.accounts.length > 0) {
|
|
2532
|
+
console.log("[WalletConnect] Provider already has accounts after init, skipping enable()");
|
|
2533
|
+
} else {
|
|
2534
|
+
const hasSession = this.provider.session !== void 0 && this.provider.session !== null;
|
|
2535
|
+
console.log("[WalletConnect] Provider has no accounts, calling enable() to show QR modal");
|
|
2536
|
+
console.log("[WalletConnect] Provider state:", {
|
|
2537
|
+
accounts: this.provider.accounts,
|
|
2538
|
+
chainId: this.provider.chainId,
|
|
2539
|
+
hasSession,
|
|
2540
|
+
sessionTopic: this.provider.session?.topic
|
|
2541
|
+
});
|
|
2542
|
+
if (hasSession && (!this.provider.accounts || this.provider.accounts.length === 0)) {
|
|
2543
|
+
console.log("[WalletConnect] Found stale session after init, disconnecting before reconnecting...");
|
|
2544
|
+
try {
|
|
2545
|
+
await this.provider.disconnect();
|
|
2546
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
2547
|
+
} catch (disconnectError) {
|
|
2548
|
+
console.warn("[WalletConnect] Error disconnecting stale session:", disconnectError);
|
|
2549
|
+
}
|
|
2550
|
+
}
|
|
2551
|
+
try {
|
|
2552
|
+
await this.provider.enable();
|
|
2553
|
+
} catch (error) {
|
|
2554
|
+
console.error("[WalletConnect] enable() error:", error);
|
|
2555
|
+
if (error.code === 4001 || error.message?.includes("rejected") || error.message?.includes("User rejected")) {
|
|
2556
|
+
throw new ConnectionRejectedError(this.type);
|
|
2557
|
+
}
|
|
2558
|
+
throw error;
|
|
2559
|
+
}
|
|
2560
|
+
}
|
|
2561
|
+
} else if (_WalletConnectAdapter.isInitializing && _WalletConnectAdapter.initPromise) {
|
|
2562
|
+
console.log("[WalletConnect] Waiting for ongoing initialization...");
|
|
2563
|
+
this.provider = await _WalletConnectAdapter.initPromise;
|
|
2564
|
+
_WalletConnectAdapter.providerInstance = this.provider;
|
|
2565
|
+
_WalletConnectAdapter.providerProjectId = this.projectId;
|
|
2566
|
+
_WalletConnectAdapter.providerChains = targetChains;
|
|
2567
|
+
if (this.provider.accounts && this.provider.accounts.length > 0) {
|
|
2568
|
+
console.log("[WalletConnect] Provider already has accounts after init, skipping enable()");
|
|
2569
|
+
} else {
|
|
2570
|
+
try {
|
|
2571
|
+
await this.provider.enable();
|
|
2572
|
+
} catch (error) {
|
|
2573
|
+
if (error.code === 4001 || error.message?.includes("rejected") || error.message?.includes("User rejected")) {
|
|
2574
|
+
throw new ConnectionRejectedError(this.type);
|
|
2575
|
+
}
|
|
2576
|
+
throw error;
|
|
2577
|
+
}
|
|
2578
|
+
}
|
|
2579
|
+
} else {
|
|
2580
|
+
console.log("[WalletConnect] Initializing new provider with chains:", {
|
|
2581
|
+
primary: primaryChain,
|
|
2582
|
+
optional: optionalChains,
|
|
2583
|
+
all: targetChains
|
|
2584
|
+
});
|
|
2585
|
+
_WalletConnectAdapter.isInitializing = true;
|
|
2586
|
+
_WalletConnectAdapter.initPromise = EthereumProvider.init(initOptions);
|
|
2587
|
+
try {
|
|
2588
|
+
this.provider = await _WalletConnectAdapter.initPromise;
|
|
2589
|
+
_WalletConnectAdapter.providerInstance = this.provider;
|
|
2590
|
+
_WalletConnectAdapter.providerProjectId = this.projectId;
|
|
2591
|
+
_WalletConnectAdapter.providerChains = targetChains;
|
|
2592
|
+
if (this.provider.accounts && this.provider.accounts.length > 0) {
|
|
2593
|
+
console.log("[WalletConnect] Provider has restored session, skipping enable()");
|
|
2594
|
+
} else {
|
|
2595
|
+
const hasSession = this.provider.session !== void 0 && this.provider.session !== null;
|
|
2596
|
+
console.log("[WalletConnect] New provider initialized, calling enable() to show QR modal");
|
|
2597
|
+
console.log("[WalletConnect] Provider state:", {
|
|
2598
|
+
accounts: this.provider.accounts,
|
|
2599
|
+
chainId: this.provider.chainId,
|
|
2600
|
+
hasSession,
|
|
2601
|
+
sessionTopic: this.provider.session?.topic
|
|
2602
|
+
});
|
|
2603
|
+
if (hasSession && (!this.provider.accounts || this.provider.accounts.length === 0)) {
|
|
2604
|
+
console.log("[WalletConnect] Found stale session after init, disconnecting before reconnecting...");
|
|
2605
|
+
try {
|
|
2606
|
+
await this.provider.disconnect();
|
|
2607
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
2608
|
+
} catch (disconnectError) {
|
|
2609
|
+
console.warn("[WalletConnect] Error disconnecting stale session:", disconnectError);
|
|
2610
|
+
}
|
|
2611
|
+
}
|
|
2612
|
+
try {
|
|
2613
|
+
await this.provider.enable();
|
|
2614
|
+
} catch (error) {
|
|
2615
|
+
console.error("[WalletConnect] enable() error:", error);
|
|
2616
|
+
if (error.code === 4001 || error.message?.includes("rejected") || error.message?.includes("User rejected")) {
|
|
2617
|
+
throw new ConnectionRejectedError(this.type);
|
|
2618
|
+
}
|
|
2619
|
+
throw error;
|
|
2620
|
+
}
|
|
2621
|
+
}
|
|
2622
|
+
} finally {
|
|
2623
|
+
_WalletConnectAdapter.isInitializing = false;
|
|
2624
|
+
_WalletConnectAdapter.initPromise = null;
|
|
2625
|
+
}
|
|
2626
|
+
}
|
|
2627
|
+
let accounts = this.provider.accounts;
|
|
2628
|
+
if (!accounts || accounts.length === 0) {
|
|
2629
|
+
console.log("[WalletConnect] provider.accounts is empty, checking session.namespaces.eip155.accounts...");
|
|
2630
|
+
const session = this.provider.session;
|
|
2631
|
+
if (session && session.namespaces?.eip155?.accounts) {
|
|
2632
|
+
const sessionAccounts = session.namespaces.eip155.accounts.map((acc) => {
|
|
2633
|
+
const parts = acc.split(":");
|
|
2634
|
+
if (parts.length >= 3 && parts[0] === "eip155") {
|
|
2635
|
+
return parts[2];
|
|
2636
|
+
}
|
|
2637
|
+
return null;
|
|
2638
|
+
}).filter((addr) => addr !== null && addr.startsWith("0x"));
|
|
2639
|
+
if (sessionAccounts.length > 0) {
|
|
2640
|
+
const uniqueAccounts = [...new Set(sessionAccounts)];
|
|
2641
|
+
console.log("[WalletConnect] Found accounts in session.namespaces.eip155.accounts:", {
|
|
2642
|
+
raw: session.namespaces.eip155.accounts,
|
|
2643
|
+
extracted: uniqueAccounts,
|
|
2644
|
+
chains: session.namespaces.eip155.chains
|
|
2645
|
+
});
|
|
2646
|
+
accounts = uniqueAccounts;
|
|
2647
|
+
}
|
|
2648
|
+
}
|
|
2649
|
+
}
|
|
2650
|
+
if (!accounts || accounts.length === 0) {
|
|
2651
|
+
console.log("[WalletConnect] Accounts not available, waiting for provider.accounts to populate...");
|
|
2652
|
+
const maxWaitTime = 3e3;
|
|
2653
|
+
const checkInterval = 100;
|
|
2654
|
+
const maxChecks = maxWaitTime / checkInterval;
|
|
2655
|
+
for (let i = 0; i < maxChecks; i++) {
|
|
2656
|
+
await new Promise((resolve) => setTimeout(resolve, checkInterval));
|
|
2657
|
+
accounts = this.provider.accounts;
|
|
2658
|
+
if (accounts && accounts.length > 0) {
|
|
2659
|
+
console.log(`[WalletConnect] Accounts available after ${(i + 1) * checkInterval}ms`);
|
|
2660
|
+
break;
|
|
2661
|
+
}
|
|
2662
|
+
}
|
|
2663
|
+
}
|
|
2664
|
+
if (!accounts || accounts.length === 0) {
|
|
2665
|
+
const session = this.provider.session;
|
|
2666
|
+
const providerState = {
|
|
2667
|
+
providerAccounts: this.provider.accounts,
|
|
2668
|
+
providerChainId: this.provider.chainId,
|
|
2669
|
+
session: session ? {
|
|
2670
|
+
exists: true,
|
|
2671
|
+
topic: session.topic,
|
|
2672
|
+
namespaces: session.namespaces ? Object.keys(session.namespaces) : "none",
|
|
2673
|
+
eip155Namespace: session.namespaces?.eip155 ? {
|
|
2674
|
+
accounts: session.namespaces.eip155.accounts,
|
|
2675
|
+
// CAIP-10 format
|
|
2676
|
+
chains: session.namespaces.eip155.chains,
|
|
2677
|
+
// CAIP-2 format
|
|
2678
|
+
methods: session.namespaces.eip155.methods,
|
|
2679
|
+
events: session.namespaces.eip155.events
|
|
2680
|
+
} : "none",
|
|
2681
|
+
// Log full session structure for debugging
|
|
2682
|
+
fullSession: JSON.stringify(session, null, 2)
|
|
2683
|
+
} : "none",
|
|
2684
|
+
// Check if provider has any other properties that might contain accounts
|
|
2685
|
+
providerKeys: Object.keys(this.provider)
|
|
2686
|
+
};
|
|
2687
|
+
console.error("[WalletConnect] No accounts available after enable() and wait", providerState);
|
|
2688
|
+
console.error("[WalletConnect] Full provider object:", this.provider);
|
|
2689
|
+
console.error("[WalletConnect] Full session object:", session);
|
|
2690
|
+
console.error("[WalletConnect] Session namespaces structure:", session?.namespaces);
|
|
2691
|
+
throw new Error("WalletConnect connection established but no accounts available. Please check session.namespaces.eip155.accounts in the console logs above.");
|
|
2692
|
+
}
|
|
2693
|
+
const currentChainId = this.provider.chainId || targetChains[0];
|
|
2694
|
+
const viemChain = this.getViemChain(currentChainId);
|
|
2695
|
+
this.walletClient = createWalletClient({
|
|
2696
|
+
account: accounts[0],
|
|
2697
|
+
chain: viemChain,
|
|
2698
|
+
transport: custom(this.provider)
|
|
2699
|
+
});
|
|
2700
|
+
const chainInfo = getChainInfo(currentChainId);
|
|
2701
|
+
const primaryRpcUrl = chainInfo?.rpcUrls[0];
|
|
2702
|
+
this.publicClient = createPublicClient({
|
|
2703
|
+
chain: viemChain,
|
|
2704
|
+
transport: primaryRpcUrl ? http(primaryRpcUrl) : custom(this.provider)
|
|
2705
|
+
});
|
|
2706
|
+
const address = formatEVMAddress(accounts[0]);
|
|
2707
|
+
const account = {
|
|
2708
|
+
universalAddress: createUniversalAddress(currentChainId, address),
|
|
2709
|
+
nativeAddress: address,
|
|
2710
|
+
chainId: currentChainId,
|
|
2711
|
+
chainType: ChainType.EVM,
|
|
2712
|
+
isActive: true
|
|
2713
|
+
};
|
|
2714
|
+
this.setState("connected" /* CONNECTED */);
|
|
2715
|
+
this.setAccount(account);
|
|
2716
|
+
this.setupEventListeners();
|
|
2717
|
+
return account;
|
|
2718
|
+
} catch (error) {
|
|
2719
|
+
this.setState("error" /* ERROR */);
|
|
2720
|
+
this.setAccount(null);
|
|
2721
|
+
const origin = typeof window !== "undefined" && window.location ? window.location.origin : "";
|
|
2722
|
+
const errorCode = error?.code;
|
|
2723
|
+
const errorMessage = error?.message || String(error);
|
|
2724
|
+
const isOriginNotAllowed = errorCode === 3e3 || /origin not allowed/i.test(errorMessage) || /Unauthorized:\s*origin not allowed/i.test(errorMessage);
|
|
2725
|
+
const session = this.provider?.session;
|
|
2726
|
+
const providerState = this.provider ? {
|
|
2727
|
+
accounts: this.provider.accounts,
|
|
2728
|
+
chainId: this.provider.chainId,
|
|
2729
|
+
session: session ? {
|
|
2730
|
+
exists: true,
|
|
2731
|
+
topic: session.topic,
|
|
2732
|
+
namespaces: session.namespaces ? Object.keys(session.namespaces) : "none",
|
|
2733
|
+
eip155Namespace: session.namespaces?.eip155 ? {
|
|
2734
|
+
accounts: session.namespaces.eip155.accounts,
|
|
2735
|
+
chains: session.namespaces.eip155.chains,
|
|
2736
|
+
methods: session.namespaces.eip155.methods,
|
|
2737
|
+
events: session.namespaces.eip155.events
|
|
2738
|
+
} : "none"
|
|
2739
|
+
} : "none",
|
|
2740
|
+
providerKeys: Object.keys(this.provider)
|
|
2741
|
+
} : "no provider";
|
|
2742
|
+
console.error("[WalletConnect] Connection error:", {
|
|
2743
|
+
error,
|
|
2744
|
+
code: error.code,
|
|
2745
|
+
message: error.message,
|
|
2746
|
+
stack: error.stack,
|
|
2747
|
+
providerState
|
|
2748
|
+
});
|
|
2749
|
+
if (this.provider) {
|
|
2750
|
+
console.error("[WalletConnect] Full provider object:", this.provider);
|
|
2751
|
+
console.error("[WalletConnect] Full session object:", session);
|
|
2752
|
+
}
|
|
2753
|
+
if (error.code === 4001 || error.message && (error.message.includes("User rejected") || error.message.includes("rejected by user") || error.message.includes("User cancelled"))) {
|
|
2754
|
+
throw new ConnectionRejectedError(this.type);
|
|
2755
|
+
}
|
|
2756
|
+
if (isOriginNotAllowed) {
|
|
2757
|
+
throw new ConfigurationError(
|
|
2758
|
+
`WalletConnect relayer rejected this origin (code 3000: Unauthorized: origin not allowed).
|
|
2759
|
+
|
|
2760
|
+
Fix:
|
|
2761
|
+
1) Open WalletConnect Cloud \u2192 your project (${this.projectId})
|
|
2762
|
+
2) Add this site origin to the allowlist:
|
|
2763
|
+
- ${origin || "(unknown origin)"}
|
|
2764
|
+
|
|
2765
|
+
Common dev origins to allow:
|
|
2766
|
+
- http://localhost:5173
|
|
2767
|
+
- http://192.168.0.221:5173 (your LAN dev URL)
|
|
2768
|
+
- https://wallet-test.enclave-hq.com (your Cloudflare Tunnel/custom domain)
|
|
2769
|
+
|
|
2770
|
+
Original error: ${errorMessage}`
|
|
2771
|
+
);
|
|
2772
|
+
}
|
|
2773
|
+
throw error;
|
|
2774
|
+
}
|
|
2775
|
+
}
|
|
2776
|
+
/**
|
|
2777
|
+
* Disconnect wallet
|
|
2778
|
+
*/
|
|
2779
|
+
async disconnect() {
|
|
2780
|
+
if (this.provider) {
|
|
2781
|
+
try {
|
|
2782
|
+
await this.provider.disconnect();
|
|
2783
|
+
} catch (error) {
|
|
2784
|
+
console.warn("[WalletConnect] Error during disconnect:", error);
|
|
2785
|
+
}
|
|
2786
|
+
if (_WalletConnectAdapter.providerInstance === this.provider) {
|
|
2787
|
+
_WalletConnectAdapter.providerInstance = null;
|
|
2788
|
+
_WalletConnectAdapter.providerProjectId = null;
|
|
2789
|
+
_WalletConnectAdapter.providerChains = null;
|
|
2790
|
+
}
|
|
2791
|
+
this.provider = null;
|
|
2792
|
+
}
|
|
2793
|
+
this.removeEventListeners();
|
|
2794
|
+
this.walletClient = null;
|
|
2795
|
+
this.publicClient = null;
|
|
2796
|
+
this.setState("disconnected" /* DISCONNECTED */);
|
|
2797
|
+
this.setAccount(null);
|
|
2798
|
+
this.emitDisconnected();
|
|
2799
|
+
}
|
|
2800
|
+
/**
|
|
2801
|
+
* Sign message
|
|
2802
|
+
*/
|
|
2803
|
+
async signMessage(message) {
|
|
2804
|
+
this.ensureConnected();
|
|
2805
|
+
try {
|
|
2806
|
+
if (!this.provider) {
|
|
2807
|
+
throw new Error("Provider not initialized");
|
|
2808
|
+
}
|
|
2809
|
+
const signature = await this.provider.request({
|
|
2810
|
+
method: "personal_sign",
|
|
2811
|
+
params: [message, this.currentAccount.nativeAddress]
|
|
2812
|
+
});
|
|
2813
|
+
this.closeTelegramDeepLinkPopup();
|
|
2814
|
+
return signature;
|
|
2815
|
+
} catch (error) {
|
|
2816
|
+
if (error.code === 4001 || error.message?.includes("rejected")) {
|
|
2817
|
+
throw new SignatureRejectedError();
|
|
2818
|
+
}
|
|
2819
|
+
throw error;
|
|
2820
|
+
}
|
|
2821
|
+
}
|
|
2822
|
+
/**
|
|
2823
|
+
* Sign TypedData (EIP-712)
|
|
2824
|
+
*/
|
|
2825
|
+
async signTypedData(typedData) {
|
|
2826
|
+
this.ensureConnected();
|
|
2827
|
+
try {
|
|
2828
|
+
if (!this.provider) {
|
|
2829
|
+
throw new Error("Provider not initialized");
|
|
2830
|
+
}
|
|
2831
|
+
const signature = await this.provider.request({
|
|
2832
|
+
method: "eth_signTypedData_v4",
|
|
2833
|
+
params: [this.currentAccount.nativeAddress, JSON.stringify(typedData)]
|
|
2834
|
+
});
|
|
2835
|
+
this.closeTelegramDeepLinkPopup();
|
|
2836
|
+
return signature;
|
|
2837
|
+
} catch (error) {
|
|
2838
|
+
if (error.code === 4001 || error.message?.includes("rejected")) {
|
|
2839
|
+
throw new SignatureRejectedError();
|
|
2840
|
+
}
|
|
2841
|
+
throw error;
|
|
2842
|
+
}
|
|
2843
|
+
}
|
|
2844
|
+
/**
|
|
2845
|
+
* Sign transaction
|
|
2846
|
+
*/
|
|
2847
|
+
async signTransaction(transaction) {
|
|
2848
|
+
this.ensureConnected();
|
|
2849
|
+
try {
|
|
2850
|
+
if (!this.provider) {
|
|
2851
|
+
throw new Error("Provider not initialized");
|
|
2852
|
+
}
|
|
2853
|
+
const tx = {
|
|
2854
|
+
from: this.currentAccount.nativeAddress,
|
|
2855
|
+
to: transaction.to,
|
|
2856
|
+
value: transaction.value ? `0x${BigInt(transaction.value).toString(16)}` : void 0,
|
|
2857
|
+
data: transaction.data || "0x",
|
|
2858
|
+
gas: transaction.gas ? `0x${BigInt(transaction.gas).toString(16)}` : void 0,
|
|
2859
|
+
gasPrice: transaction.gasPrice && transaction.gasPrice !== "auto" ? `0x${BigInt(transaction.gasPrice).toString(16)}` : void 0,
|
|
2860
|
+
maxFeePerGas: transaction.maxFeePerGas ? `0x${BigInt(transaction.maxFeePerGas).toString(16)}` : void 0,
|
|
2861
|
+
maxPriorityFeePerGas: transaction.maxPriorityFeePerGas ? `0x${BigInt(transaction.maxPriorityFeePerGas).toString(16)}` : void 0,
|
|
2862
|
+
nonce: transaction.nonce !== void 0 ? `0x${transaction.nonce.toString(16)}` : void 0,
|
|
2863
|
+
chainId: transaction.chainId || this.currentAccount.chainId
|
|
2864
|
+
};
|
|
2865
|
+
const signature = await this.provider.request({
|
|
2866
|
+
method: "eth_signTransaction",
|
|
2867
|
+
params: [tx]
|
|
2868
|
+
});
|
|
2869
|
+
this.closeTelegramDeepLinkPopup();
|
|
2870
|
+
return signature;
|
|
2871
|
+
} catch (error) {
|
|
2872
|
+
if (error.code === 4001 || error.message?.includes("rejected")) {
|
|
2873
|
+
throw new SignatureRejectedError("Transaction signature was rejected by user");
|
|
2874
|
+
}
|
|
2875
|
+
throw error;
|
|
2876
|
+
}
|
|
2877
|
+
}
|
|
2878
|
+
/**
|
|
2879
|
+
* Get supported chains from current connection
|
|
2880
|
+
* Returns the chains that were requested during connection
|
|
2881
|
+
*/
|
|
2882
|
+
getSupportedChains() {
|
|
2883
|
+
return [...this.supportedChains];
|
|
2884
|
+
}
|
|
2885
|
+
/**
|
|
2886
|
+
* Switch chain
|
|
2887
|
+
*
|
|
2888
|
+
* Note: WalletConnect v2 with mobile wallets may not support chain switching reliably.
|
|
2889
|
+
* Some wallets may ignore the switch request or fail silently.
|
|
2890
|
+
* It's recommended to include all needed chains in the initial connection.
|
|
2891
|
+
*
|
|
2892
|
+
* Reference: https://specs.walletconnect.com/2.0/specs/clients/sign/namespaces
|
|
2893
|
+
*/
|
|
2894
|
+
async switchChain(chainId) {
|
|
2895
|
+
if (!this.provider) {
|
|
2896
|
+
throw new Error("Provider not initialized");
|
|
2897
|
+
}
|
|
2898
|
+
const session = this.provider.session;
|
|
2899
|
+
const supportedChains = session?.namespaces?.eip155?.chains || [];
|
|
2900
|
+
const targetChainCAIP = `eip155:${chainId}`;
|
|
2901
|
+
const isChainApproved = supportedChains.includes(targetChainCAIP);
|
|
2902
|
+
if (!isChainApproved) {
|
|
2903
|
+
console.warn(`[WalletConnect] Chain ${chainId} (${targetChainCAIP}) not in session approved chains:`, supportedChains);
|
|
2904
|
+
console.warn("[WalletConnect] Chain switching may fail. Consider including all chains in initial connection.");
|
|
2905
|
+
}
|
|
2906
|
+
try {
|
|
2907
|
+
console.log(`[WalletConnect] Attempting to switch to chain ${chainId} (${targetChainCAIP})`);
|
|
2908
|
+
const result = await this.provider.request({
|
|
2909
|
+
method: "wallet_switchEthereumChain",
|
|
2910
|
+
params: [{ chainId: `0x${chainId.toString(16)}` }]
|
|
2911
|
+
});
|
|
2912
|
+
if (result !== null && result !== void 0) {
|
|
2913
|
+
console.warn("[WalletConnect] wallet_switchEthereumChain returned non-null result:", result);
|
|
2914
|
+
}
|
|
2915
|
+
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
2916
|
+
const currentChainId = this.provider.chainId;
|
|
2917
|
+
if (currentChainId !== chainId) {
|
|
2918
|
+
console.warn(`[WalletConnect] Chain switch may have failed. Expected ${chainId}, got ${currentChainId}`);
|
|
2919
|
+
console.warn("[WalletConnect] Some mobile wallets may not support chain switching via WalletConnect.");
|
|
2920
|
+
console.warn("[WalletConnect] User may need to manually switch chains in the wallet app.");
|
|
2921
|
+
}
|
|
2922
|
+
if (this.currentAccount) {
|
|
2923
|
+
const updatedAccount = {
|
|
2924
|
+
...this.currentAccount,
|
|
2925
|
+
chainId,
|
|
2926
|
+
universalAddress: createUniversalAddress(chainId, this.currentAccount.nativeAddress)
|
|
2927
|
+
};
|
|
2928
|
+
this.setAccount(updatedAccount);
|
|
2929
|
+
this.emitChainChanged(chainId);
|
|
2930
|
+
const viemChain = this.getViemChain(chainId);
|
|
2931
|
+
const chainInfo = getChainInfo(chainId);
|
|
2932
|
+
const primaryRpcUrl = chainInfo?.rpcUrls[0];
|
|
2933
|
+
this.walletClient = createWalletClient({
|
|
2934
|
+
account: this.currentAccount.nativeAddress,
|
|
2935
|
+
chain: viemChain,
|
|
2936
|
+
transport: custom(this.provider)
|
|
2937
|
+
});
|
|
2938
|
+
this.publicClient = createPublicClient({
|
|
2939
|
+
chain: viemChain,
|
|
2940
|
+
transport: primaryRpcUrl ? http(primaryRpcUrl) : custom(this.provider)
|
|
2941
|
+
});
|
|
2942
|
+
}
|
|
2943
|
+
} catch (error) {
|
|
2944
|
+
console.error("[WalletConnect] Chain switch error:", {
|
|
2945
|
+
chainId,
|
|
2946
|
+
errorCode: error.code,
|
|
2947
|
+
errorMessage: error.message,
|
|
2948
|
+
supportedChains
|
|
2949
|
+
});
|
|
2950
|
+
if (error.code === 4902) {
|
|
2951
|
+
console.log(`[WalletConnect] Chain ${chainId} not found in wallet, attempting to add...`);
|
|
2952
|
+
const chainInfo = getChainInfo(chainId);
|
|
2953
|
+
if (chainInfo) {
|
|
2954
|
+
try {
|
|
2955
|
+
await this.addChain({
|
|
2956
|
+
chainId: chainInfo.id,
|
|
2957
|
+
chainName: chainInfo.name,
|
|
2958
|
+
nativeCurrency: chainInfo.nativeCurrency,
|
|
2959
|
+
rpcUrls: chainInfo.rpcUrls,
|
|
2960
|
+
blockExplorerUrls: chainInfo.blockExplorerUrls
|
|
2961
|
+
});
|
|
2962
|
+
console.log(`[WalletConnect] Chain added, attempting to switch again...`);
|
|
2963
|
+
await this.switchChain(chainId);
|
|
2964
|
+
} catch (addError) {
|
|
2965
|
+
console.error("[WalletConnect] Failed to add chain:", addError);
|
|
2966
|
+
throw new Error(`Failed to add chain ${chainId}: ${addError.message}`);
|
|
2967
|
+
}
|
|
2968
|
+
} else {
|
|
2969
|
+
throw new Error(`Chain ${chainId} not supported`);
|
|
2970
|
+
}
|
|
2971
|
+
} else if (error.code === 4001) {
|
|
2972
|
+
throw new Error("User rejected chain switch");
|
|
2973
|
+
} else if (error.code === 4100) {
|
|
2974
|
+
throw new Error("Wallet does not support wallet_switchEthereumChain. Please switch chains manually in your wallet app.");
|
|
2975
|
+
} else {
|
|
2976
|
+
console.warn("[WalletConnect] Chain switch may not be supported by this wallet. User may need to switch manually.");
|
|
2977
|
+
throw error;
|
|
2978
|
+
}
|
|
2979
|
+
}
|
|
2980
|
+
}
|
|
2981
|
+
/**
|
|
2982
|
+
* Add chain
|
|
2983
|
+
*/
|
|
2984
|
+
async addChain(chainConfig) {
|
|
2985
|
+
if (!this.provider) {
|
|
2986
|
+
throw new Error("Provider not initialized");
|
|
2987
|
+
}
|
|
2988
|
+
await this.provider.request({
|
|
2989
|
+
method: "wallet_addEthereumChain",
|
|
2990
|
+
params: [{
|
|
2991
|
+
chainId: `0x${chainConfig.chainId.toString(16)}`,
|
|
2992
|
+
chainName: chainConfig.chainName,
|
|
2993
|
+
nativeCurrency: chainConfig.nativeCurrency,
|
|
2994
|
+
rpcUrls: chainConfig.rpcUrls,
|
|
2995
|
+
blockExplorerUrls: chainConfig.blockExplorerUrls
|
|
2996
|
+
}]
|
|
2997
|
+
});
|
|
2998
|
+
}
|
|
2999
|
+
/**
|
|
3000
|
+
* Read contract
|
|
3001
|
+
*/
|
|
3002
|
+
async readContract(params) {
|
|
3003
|
+
if (!this.publicClient) {
|
|
3004
|
+
throw new Error("Public client not initialized");
|
|
3005
|
+
}
|
|
3006
|
+
const result = await this.publicClient.readContract({
|
|
3007
|
+
address: params.address,
|
|
3008
|
+
abi: params.abi,
|
|
3009
|
+
functionName: params.functionName,
|
|
3010
|
+
...params.args ? { args: params.args } : {}
|
|
3011
|
+
});
|
|
3012
|
+
return result;
|
|
3013
|
+
}
|
|
3014
|
+
/**
|
|
3015
|
+
* Write contract
|
|
3016
|
+
*/
|
|
3017
|
+
async writeContract(params) {
|
|
3018
|
+
this.ensureConnected();
|
|
3019
|
+
if (!this.walletClient) {
|
|
3020
|
+
throw new Error("Wallet client not initialized");
|
|
3021
|
+
}
|
|
3022
|
+
try {
|
|
3023
|
+
const txOptions = {
|
|
3024
|
+
address: params.address,
|
|
3025
|
+
abi: params.abi,
|
|
3026
|
+
functionName: params.functionName,
|
|
3027
|
+
...params.args ? { args: params.args } : {},
|
|
3028
|
+
value: params.value ? BigInt(params.value) : void 0,
|
|
3029
|
+
gas: params.gas ? BigInt(params.gas) : void 0
|
|
3030
|
+
};
|
|
3031
|
+
if (params.maxFeePerGas || params.maxPriorityFeePerGas) {
|
|
3032
|
+
if (params.maxFeePerGas) {
|
|
3033
|
+
txOptions.maxFeePerGas = BigInt(params.maxFeePerGas);
|
|
3034
|
+
}
|
|
3035
|
+
if (params.maxPriorityFeePerGas) {
|
|
3036
|
+
txOptions.maxPriorityFeePerGas = BigInt(params.maxPriorityFeePerGas);
|
|
3037
|
+
}
|
|
3038
|
+
} else if (params.gasPrice && params.gasPrice !== "auto") {
|
|
3039
|
+
txOptions.gasPrice = BigInt(params.gasPrice);
|
|
3040
|
+
} else {
|
|
3041
|
+
if (this.publicClient) {
|
|
3042
|
+
try {
|
|
3043
|
+
const feesPerGas = await this.publicClient.estimateFeesPerGas().catch(() => null);
|
|
3044
|
+
if (feesPerGas) {
|
|
3045
|
+
const minPriorityFeeWei = BigInt(1e8);
|
|
3046
|
+
const maxPriorityFeePerGas = feesPerGas.maxPriorityFeePerGas > minPriorityFeeWei ? feesPerGas.maxPriorityFeePerGas : minPriorityFeeWei;
|
|
3047
|
+
const adjustedMaxFeePerGas = feesPerGas.maxFeePerGas > maxPriorityFeePerGas ? feesPerGas.maxFeePerGas : maxPriorityFeePerGas + BigInt(1e9);
|
|
3048
|
+
txOptions.maxFeePerGas = adjustedMaxFeePerGas;
|
|
3049
|
+
txOptions.maxPriorityFeePerGas = maxPriorityFeePerGas;
|
|
3050
|
+
} else {
|
|
3051
|
+
const gasPrice = await this.publicClient.getGasPrice();
|
|
3052
|
+
txOptions.gasPrice = gasPrice;
|
|
3053
|
+
}
|
|
3054
|
+
} catch (err) {
|
|
3055
|
+
}
|
|
3056
|
+
}
|
|
3057
|
+
}
|
|
3058
|
+
const txHash = await this.walletClient.writeContract(txOptions);
|
|
3059
|
+
this.closeTelegramDeepLinkPopup();
|
|
3060
|
+
return txHash;
|
|
3061
|
+
} catch (error) {
|
|
3062
|
+
if (error.code === 4001 || error.message?.includes("rejected")) {
|
|
3063
|
+
throw new SignatureRejectedError("Transaction was rejected by user");
|
|
3064
|
+
}
|
|
3065
|
+
throw error;
|
|
3066
|
+
}
|
|
3067
|
+
}
|
|
3068
|
+
/**
|
|
3069
|
+
* Estimate gas
|
|
3070
|
+
*/
|
|
3071
|
+
async estimateGas(params) {
|
|
3072
|
+
if (!this.publicClient) {
|
|
3073
|
+
throw new Error("Public client not initialized");
|
|
3074
|
+
}
|
|
3075
|
+
const gas = await this.publicClient.estimateContractGas({
|
|
3076
|
+
address: params.address,
|
|
3077
|
+
abi: params.abi,
|
|
3078
|
+
functionName: params.functionName,
|
|
3079
|
+
...params.args ? { args: params.args } : {},
|
|
3080
|
+
value: params.value ? BigInt(params.value) : void 0,
|
|
3081
|
+
account: this.currentAccount.nativeAddress
|
|
3082
|
+
});
|
|
3083
|
+
return gas;
|
|
3084
|
+
}
|
|
3085
|
+
/**
|
|
3086
|
+
* Wait for transaction
|
|
3087
|
+
*/
|
|
3088
|
+
async waitForTransaction(txHash, confirmations = 1) {
|
|
3089
|
+
if (!this.publicClient) {
|
|
3090
|
+
throw new Error("Public client not initialized");
|
|
3091
|
+
}
|
|
3092
|
+
const receipt = await this.publicClient.waitForTransactionReceipt({
|
|
3093
|
+
hash: txHash,
|
|
3094
|
+
confirmations
|
|
3095
|
+
});
|
|
3096
|
+
if (receipt.status === "reverted") {
|
|
3097
|
+
throw new TransactionFailedError(txHash, "Transaction reverted");
|
|
3098
|
+
}
|
|
3099
|
+
return {
|
|
3100
|
+
transactionHash: receipt.transactionHash,
|
|
3101
|
+
blockNumber: Number(receipt.blockNumber),
|
|
3102
|
+
blockHash: receipt.blockHash,
|
|
3103
|
+
from: receipt.from,
|
|
3104
|
+
to: receipt.to || void 0,
|
|
3105
|
+
status: receipt.status === "success" ? "success" : "failed",
|
|
3106
|
+
gasUsed: receipt.gasUsed.toString(),
|
|
3107
|
+
effectiveGasPrice: receipt.effectiveGasPrice?.toString(),
|
|
3108
|
+
logs: receipt.logs
|
|
3109
|
+
};
|
|
3110
|
+
}
|
|
3111
|
+
/**
|
|
3112
|
+
* Get provider
|
|
3113
|
+
*/
|
|
3114
|
+
getProvider() {
|
|
3115
|
+
return this.provider;
|
|
3116
|
+
}
|
|
3117
|
+
/**
|
|
3118
|
+
* Get signer
|
|
3119
|
+
*/
|
|
3120
|
+
getSigner() {
|
|
3121
|
+
return this.walletClient;
|
|
3122
|
+
}
|
|
3123
|
+
/**
|
|
3124
|
+
* Setup event listeners
|
|
3125
|
+
*/
|
|
3126
|
+
setupEventListeners() {
|
|
3127
|
+
if (!this.provider) return;
|
|
3128
|
+
this.provider.on("accountsChanged", this.handleAccountsChanged);
|
|
3129
|
+
this.provider.on("chainChanged", this.handleChainChanged);
|
|
3130
|
+
this.provider.on("disconnect", this.handleDisconnect);
|
|
3131
|
+
}
|
|
3132
|
+
/**
|
|
3133
|
+
* Remove event listeners
|
|
3134
|
+
*/
|
|
3135
|
+
removeEventListeners() {
|
|
3136
|
+
if (!this.provider) return;
|
|
3137
|
+
this.provider.removeListener("accountsChanged", this.handleAccountsChanged);
|
|
3138
|
+
this.provider.removeListener("chainChanged", this.handleChainChanged);
|
|
3139
|
+
this.provider.removeListener("disconnect", this.handleDisconnect);
|
|
3140
|
+
}
|
|
3141
|
+
/**
|
|
3142
|
+
* Get viem chain config
|
|
3143
|
+
*/
|
|
3144
|
+
getViemChain(chainId) {
|
|
3145
|
+
const chainInfo = getChainInfo(chainId);
|
|
3146
|
+
if (chainInfo) {
|
|
3147
|
+
return {
|
|
3148
|
+
id: chainId,
|
|
3149
|
+
name: chainInfo.name,
|
|
3150
|
+
network: chainInfo.name.toLowerCase().replace(/\s+/g, "-"),
|
|
3151
|
+
nativeCurrency: chainInfo.nativeCurrency,
|
|
3152
|
+
rpcUrls: {
|
|
3153
|
+
default: { http: chainInfo.rpcUrls },
|
|
3154
|
+
public: { http: chainInfo.rpcUrls }
|
|
3155
|
+
},
|
|
3156
|
+
blockExplorers: chainInfo.blockExplorerUrls ? {
|
|
3157
|
+
default: { name: "Explorer", url: chainInfo.blockExplorerUrls[0] }
|
|
3158
|
+
} : void 0
|
|
3159
|
+
};
|
|
3160
|
+
}
|
|
3161
|
+
return {
|
|
3162
|
+
id: chainId,
|
|
3163
|
+
name: `Chain ${chainId}`,
|
|
3164
|
+
network: `chain-${chainId}`,
|
|
3165
|
+
nativeCurrency: {
|
|
3166
|
+
name: "ETH",
|
|
3167
|
+
symbol: "ETH",
|
|
3168
|
+
decimals: 18
|
|
3169
|
+
},
|
|
3170
|
+
rpcUrls: {
|
|
3171
|
+
default: { http: [] },
|
|
3172
|
+
public: { http: [] }
|
|
3173
|
+
}
|
|
3174
|
+
};
|
|
3175
|
+
}
|
|
3176
|
+
};
|
|
3177
|
+
// Store supported chains from connection
|
|
3178
|
+
// Static provider instance to avoid multiple initializations
|
|
3179
|
+
_WalletConnectAdapter.providerInstance = null;
|
|
3180
|
+
_WalletConnectAdapter.providerProjectId = null;
|
|
3181
|
+
_WalletConnectAdapter.providerChains = null;
|
|
3182
|
+
// Store the chains used during initialization
|
|
3183
|
+
_WalletConnectAdapter.isInitializing = false;
|
|
3184
|
+
_WalletConnectAdapter.initPromise = null;
|
|
3185
|
+
var WalletConnectAdapter = _WalletConnectAdapter;
|
|
3186
|
+
init_types();
|
|
3187
|
+
var _WalletConnectTronAdapter = class _WalletConnectTronAdapter extends WalletAdapter {
|
|
3188
|
+
constructor(projectId) {
|
|
3189
|
+
super();
|
|
3190
|
+
this.type = "walletconnect-tron" /* WALLETCONNECT_TRON */;
|
|
3191
|
+
this.chainType = ChainType.TRON;
|
|
3192
|
+
this.name = "WalletConnect (Tron)";
|
|
3193
|
+
this.icon = "https://avatars.githubusercontent.com/u/37784886";
|
|
3194
|
+
this.wallet = null;
|
|
3195
|
+
this.currentAddress = null;
|
|
3196
|
+
if (!projectId) {
|
|
3197
|
+
throw new ConfigurationError("WalletConnect projectId is required");
|
|
3198
|
+
}
|
|
3199
|
+
this.projectId = projectId;
|
|
3200
|
+
}
|
|
3201
|
+
/**
|
|
3202
|
+
* Check if WalletConnect is available
|
|
3203
|
+
*/
|
|
3204
|
+
async isAvailable() {
|
|
3205
|
+
return typeof window !== "undefined";
|
|
3206
|
+
}
|
|
3207
|
+
/**
|
|
3208
|
+
* Check if running in Telegram environment (Mini App or Web)
|
|
3209
|
+
* Both Telegram Mini App (in client) and Telegram Web (web.telegram.org)
|
|
3210
|
+
* provide window.Telegram.WebApp API, so they are treated the same way.
|
|
3211
|
+
*/
|
|
3212
|
+
isTelegramMiniApp() {
|
|
3213
|
+
if (typeof window === "undefined") return false;
|
|
3214
|
+
const tg = window.Telegram?.WebApp;
|
|
3215
|
+
if (!tg) return false;
|
|
3216
|
+
const platform = tg.platform || "unknown";
|
|
3217
|
+
console.log("[WalletConnect Tron] Telegram environment detected:", {
|
|
3218
|
+
platform,
|
|
3219
|
+
version: tg.version,
|
|
3220
|
+
isMiniApp: platform !== "web",
|
|
3221
|
+
// Mini App if not web platform
|
|
3222
|
+
isWeb: platform === "web"
|
|
3223
|
+
// Telegram Web if web platform
|
|
3224
|
+
});
|
|
3225
|
+
return true;
|
|
3226
|
+
}
|
|
3227
|
+
/**
|
|
3228
|
+
* Restore session from existing wallet (for storage restoration)
|
|
3229
|
+
*/
|
|
3230
|
+
async restoreSession(chainId) {
|
|
3231
|
+
if (typeof window === "undefined") {
|
|
3232
|
+
return null;
|
|
3233
|
+
}
|
|
3234
|
+
try {
|
|
3235
|
+
const targetChainId = Array.isArray(chainId) ? chainId[0] || _WalletConnectTronAdapter.TRON_MAINNET_CHAIN_ID : chainId || _WalletConnectTronAdapter.TRON_MAINNET_CHAIN_ID;
|
|
3236
|
+
if (!_WalletConnectTronAdapter.walletInstance || _WalletConnectTronAdapter.walletProjectId !== this.projectId) {
|
|
3237
|
+
this.initializeWallet(targetChainId);
|
|
3238
|
+
}
|
|
3239
|
+
this.wallet = _WalletConnectTronAdapter.walletInstance;
|
|
3240
|
+
if (!this.wallet) {
|
|
3241
|
+
return null;
|
|
3242
|
+
}
|
|
3243
|
+
const status = await this.wallet.checkConnectStatus();
|
|
3244
|
+
if (status && status.address) {
|
|
3245
|
+
this.currentAddress = status.address;
|
|
3246
|
+
const account = {
|
|
3247
|
+
universalAddress: createUniversalAddress(targetChainId, status.address),
|
|
3248
|
+
nativeAddress: status.address,
|
|
3249
|
+
chainId: targetChainId,
|
|
3250
|
+
chainType: ChainType.TRON,
|
|
3251
|
+
isActive: true
|
|
3252
|
+
};
|
|
3253
|
+
this.setState("connected" /* CONNECTED */);
|
|
3254
|
+
this.setAccount(account);
|
|
3255
|
+
this.setupEventListeners();
|
|
3256
|
+
return account;
|
|
3257
|
+
}
|
|
3258
|
+
return null;
|
|
3259
|
+
} catch (error) {
|
|
3260
|
+
console.debug("[WalletConnect Tron] Restore session failed:", error);
|
|
3261
|
+
return null;
|
|
3262
|
+
}
|
|
3263
|
+
}
|
|
3264
|
+
/**
|
|
3265
|
+
* Initialize WalletConnect wallet instance
|
|
3266
|
+
* @param chainId - Optional chain ID to determine network (default: Mainnet)
|
|
3267
|
+
*/
|
|
3268
|
+
initializeWallet(chainId) {
|
|
3269
|
+
if (_WalletConnectTronAdapter.walletInstance && _WalletConnectTronAdapter.walletProjectId === this.projectId) {
|
|
3270
|
+
return;
|
|
3271
|
+
}
|
|
3272
|
+
let appUrl = "";
|
|
3273
|
+
if (typeof window !== "undefined") {
|
|
3274
|
+
try {
|
|
3275
|
+
if (window.location && window.location.origin) {
|
|
3276
|
+
appUrl = window.location.origin;
|
|
3277
|
+
} else if (window.location && window.location.href) {
|
|
3278
|
+
const url = new URL(window.location.href);
|
|
3279
|
+
appUrl = url.origin;
|
|
3280
|
+
}
|
|
3281
|
+
} catch (error) {
|
|
3282
|
+
console.warn("[WalletConnect Tron] Failed to get origin from window.location:", error);
|
|
3283
|
+
}
|
|
3284
|
+
if (appUrl && (appUrl.includes("serveo.net") || appUrl.includes("loca.lt") || appUrl.includes("ngrok.io") || appUrl.includes("ngrok-free.app") || appUrl.includes("cloudflared.io"))) {
|
|
3285
|
+
console.log("[WalletConnect Tron] Detected tunnel service URL:", appUrl);
|
|
3286
|
+
console.log("[WalletConnect Tron] \u26A0\uFE0F Make sure this URL is added to WalletConnect Cloud project allowlist");
|
|
3287
|
+
}
|
|
3288
|
+
if (!appUrl) {
|
|
3289
|
+
const tg = window.Telegram?.WebApp;
|
|
3290
|
+
if (tg && tg.initDataUnsafe?.start_param) {
|
|
3291
|
+
appUrl = "https://enclave.network";
|
|
3292
|
+
} else {
|
|
3293
|
+
appUrl = "https://enclave.network";
|
|
3294
|
+
}
|
|
3295
|
+
}
|
|
3296
|
+
} else {
|
|
3297
|
+
appUrl = "https://enclave.network";
|
|
3298
|
+
}
|
|
3299
|
+
if (!appUrl || !appUrl.startsWith("http://") && !appUrl.startsWith("https://")) {
|
|
3300
|
+
appUrl = "https://enclave.network";
|
|
3301
|
+
}
|
|
3302
|
+
const icons = [
|
|
3303
|
+
"https://walletconnect.com/walletconnect-logo.svg",
|
|
3304
|
+
"https://avatars.githubusercontent.com/u/37784886"
|
|
3305
|
+
// WalletConnect GitHub avatar
|
|
3306
|
+
];
|
|
3307
|
+
let network = WalletConnectChainID.Mainnet;
|
|
3308
|
+
if (chainId !== void 0) {
|
|
3309
|
+
if (chainId === 195 || chainId === _WalletConnectTronAdapter.TRON_MAINNET_CHAIN_ID) {
|
|
3310
|
+
network = WalletConnectChainID.Mainnet;
|
|
3311
|
+
} else if (chainId === 201910292) {
|
|
3312
|
+
network = WalletConnectChainID.Shasta;
|
|
3313
|
+
} else if (chainId === 2494104990) {
|
|
3314
|
+
network = WalletConnectChainID.Nile;
|
|
3315
|
+
}
|
|
3316
|
+
}
|
|
3317
|
+
const metadataInfo = {
|
|
3318
|
+
name: "Enclave Wallet SDK",
|
|
3319
|
+
description: "Multi-chain wallet adapter for Enclave",
|
|
3320
|
+
url: appUrl,
|
|
3321
|
+
icons,
|
|
3322
|
+
network,
|
|
3323
|
+
chainId,
|
|
3324
|
+
isTelegram: this.isTelegramMiniApp(),
|
|
3325
|
+
projectId: this.projectId,
|
|
3326
|
+
urlValid: appUrl && (appUrl.startsWith("http://") || appUrl.startsWith("https://")),
|
|
3327
|
+
iconsValid: icons && icons.length > 0 && icons.every((icon) => icon && icon.startsWith("http")),
|
|
3328
|
+
currentLocation: typeof window !== "undefined" ? window.location.href : "N/A",
|
|
3329
|
+
telegramPlatform: typeof window !== "undefined" && window.Telegram?.WebApp?.platform || "N/A"
|
|
3330
|
+
};
|
|
3331
|
+
console.log("[WalletConnect Tron] Initializing with metadata:", metadataInfo);
|
|
3332
|
+
if (!metadataInfo.urlValid) {
|
|
3333
|
+
console.warn("[WalletConnect Tron] \u26A0\uFE0F Invalid URL in metadata:", appUrl);
|
|
3334
|
+
}
|
|
3335
|
+
if (!metadataInfo.iconsValid) {
|
|
3336
|
+
console.warn("[WalletConnect Tron] \u26A0\uFE0F Invalid icons in metadata:", icons);
|
|
3337
|
+
}
|
|
3338
|
+
console.log("[WalletConnect Tron] Initializing wallet...", {
|
|
3339
|
+
network,
|
|
3340
|
+
chainId,
|
|
3341
|
+
note: "If no wallets are in WalletConnect Explorer for TRON, QR code will be displayed for scanning"
|
|
3342
|
+
});
|
|
3343
|
+
_WalletConnectTronAdapter.walletInstance = new WalletConnectWallet({
|
|
3344
|
+
network,
|
|
3345
|
+
options: {
|
|
3346
|
+
projectId: this.projectId,
|
|
3347
|
+
metadata: {
|
|
3348
|
+
name: "Enclave Wallet SDK",
|
|
3349
|
+
description: "Multi-chain wallet adapter for Enclave",
|
|
3350
|
+
url: appUrl,
|
|
3351
|
+
icons
|
|
3352
|
+
}
|
|
3353
|
+
},
|
|
3354
|
+
// Theme configuration
|
|
3355
|
+
themeMode: "light",
|
|
3356
|
+
themeVariables: {
|
|
3357
|
+
"--w3m-z-index": 1e4
|
|
3358
|
+
// Ensure modal appears above Telegram UI
|
|
3359
|
+
},
|
|
3360
|
+
// Web3Modal configuration for recommended wallets
|
|
3361
|
+
// According to official docs: https://developers.tron.network/docs/walletconnect-tron
|
|
3362
|
+
// Note: If no wallets are registered in WalletConnect Explorer for TRON,
|
|
3363
|
+
// explorerRecommendedWalletIds will have no effect, and QR code will be shown instead.
|
|
3364
|
+
// @ts-ignore - web3ModalConfig is supported but may not be in TypeScript types
|
|
3365
|
+
web3ModalConfig: {
|
|
3366
|
+
themeMode: "light",
|
|
3367
|
+
themeVariables: {
|
|
3368
|
+
"--w3m-z-index": 1e4
|
|
3369
|
+
},
|
|
3370
|
+
/**
|
|
3371
|
+
* Recommended Wallets are fetched from WalletConnect explore api:
|
|
3372
|
+
* https://walletconnect.com/explorer?type=wallet&version=2
|
|
3373
|
+
*
|
|
3374
|
+
* IMPORTANT: If wallets are not registered in Explorer for TRON, this list will be ignored.
|
|
3375
|
+
* The AppKit will show a QR code instead, which users can scan with any WalletConnect-compatible wallet.
|
|
3376
|
+
*
|
|
3377
|
+
* Wallet IDs (for reference, may not work if not in Explorer):
|
|
3378
|
+
* - TokenPocket: 20459438007b75f4f4acb98bf29aa3b800550309646d375da5fd4aac6c2a2c66
|
|
3379
|
+
* - TronLink: 1ae92b26df02f0abca6304df07debccd18262fdf5fe82daa81593582dac9a369
|
|
3380
|
+
*/
|
|
3381
|
+
explorerRecommendedWalletIds: [
|
|
3382
|
+
// These IDs are kept for when wallets register in WalletConnect Explorer
|
|
3383
|
+
// Currently, if no TRON wallets are in Explorer, QR code will be shown
|
|
3384
|
+
"20459438007b75f4f4acb98bf29aa3b800550309646d375da5fd4aac6c2a2c66",
|
|
3385
|
+
// TokenPocket
|
|
3386
|
+
"1ae92b26df02f0abca6304df07debccd18262fdf5fe82daa81593582dac9a369",
|
|
3387
|
+
// TronLink
|
|
3388
|
+
"4622a2b2d6af1c9844944291e5e7351a6aa24cd7b23099efac1b2fd875da31a0"
|
|
3389
|
+
// TokenPocket (backup)
|
|
3390
|
+
]
|
|
3391
|
+
}
|
|
3392
|
+
});
|
|
3393
|
+
_WalletConnectTronAdapter.walletProjectId = this.projectId;
|
|
3394
|
+
}
|
|
3395
|
+
/**
|
|
3396
|
+
* Connect wallet
|
|
3397
|
+
*/
|
|
3398
|
+
async connect(chainId) {
|
|
3399
|
+
if (typeof window === "undefined") {
|
|
3400
|
+
throw new Error("WalletConnect requires a browser environment");
|
|
3401
|
+
}
|
|
3402
|
+
const currentState = this.state;
|
|
3403
|
+
if (currentState === "connecting" /* CONNECTING */) {
|
|
3404
|
+
console.warn("[WalletConnect Tron] Connection already in progress, waiting...");
|
|
3405
|
+
let attempts = 0;
|
|
3406
|
+
while (this.state === "connecting" /* CONNECTING */ && attempts < 50) {
|
|
3407
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
3408
|
+
attempts++;
|
|
3409
|
+
}
|
|
3410
|
+
if (this.state === "connected" /* CONNECTED */ && this.currentAccount) {
|
|
3411
|
+
return this.currentAccount;
|
|
3412
|
+
}
|
|
3413
|
+
if (this.state === "connecting" /* CONNECTING */) {
|
|
3414
|
+
throw new Error("Connection timeout - previous connection attempt is still pending");
|
|
3415
|
+
}
|
|
3416
|
+
}
|
|
3417
|
+
if (this.state === "connected" /* CONNECTED */ && this.currentAccount) {
|
|
3418
|
+
return this.currentAccount;
|
|
3419
|
+
}
|
|
3420
|
+
try {
|
|
3421
|
+
this.setState("connecting" /* CONNECTING */);
|
|
3422
|
+
const targetChainId = Array.isArray(chainId) ? chainId[0] || _WalletConnectTronAdapter.TRON_MAINNET_CHAIN_ID : chainId || _WalletConnectTronAdapter.TRON_MAINNET_CHAIN_ID;
|
|
3423
|
+
if (!_WalletConnectTronAdapter.walletInstance || _WalletConnectTronAdapter.walletProjectId !== this.projectId) {
|
|
3424
|
+
this.initializeWallet(targetChainId);
|
|
3425
|
+
}
|
|
3426
|
+
this.wallet = _WalletConnectTronAdapter.walletInstance;
|
|
3427
|
+
if (!this.wallet) {
|
|
3428
|
+
throw new Error("Failed to initialize WalletConnect wallet");
|
|
3429
|
+
}
|
|
3430
|
+
let network = WalletConnectChainID.Mainnet;
|
|
3431
|
+
if (targetChainId === 195) {
|
|
3432
|
+
network = WalletConnectChainID.Mainnet;
|
|
3433
|
+
} else if (targetChainId === 201910292) {
|
|
3434
|
+
network = WalletConnectChainID.Shasta;
|
|
3435
|
+
} else if (targetChainId === 2494104990) {
|
|
3436
|
+
network = WalletConnectChainID.Nile;
|
|
3437
|
+
}
|
|
3438
|
+
let address;
|
|
3439
|
+
try {
|
|
3440
|
+
console.log("[WalletConnect Tron] Attempting to connect...", {
|
|
3441
|
+
network,
|
|
3442
|
+
chainId: targetChainId,
|
|
3443
|
+
isTelegram: this.isTelegramMiniApp(),
|
|
3444
|
+
projectId: this.projectId
|
|
3445
|
+
});
|
|
3446
|
+
const result = await this.wallet.connect();
|
|
3447
|
+
address = result.address;
|
|
3448
|
+
if (!address) {
|
|
3449
|
+
throw new ConnectionRejectedError(this.type);
|
|
3450
|
+
}
|
|
3451
|
+
console.log("[WalletConnect Tron] Connection successful:", {
|
|
3452
|
+
address,
|
|
3453
|
+
network,
|
|
3454
|
+
chainId: targetChainId,
|
|
3455
|
+
isTelegram: this.isTelegramMiniApp()
|
|
3456
|
+
});
|
|
3457
|
+
} catch (error) {
|
|
3458
|
+
const errorMessage = error.message || String(error);
|
|
3459
|
+
const errorCode = error.code || error.error?.code;
|
|
3460
|
+
const origin = typeof window !== "undefined" && window.location ? window.location.origin : "";
|
|
3461
|
+
let detailedError = errorMessage;
|
|
3462
|
+
if (error.error) {
|
|
3463
|
+
if (typeof error.error === "string") {
|
|
3464
|
+
detailedError = error.error;
|
|
3465
|
+
} else if (error.error.message) {
|
|
3466
|
+
detailedError = error.error.message;
|
|
3467
|
+
} else if (error.error.data) {
|
|
3468
|
+
detailedError = JSON.stringify(error.error.data);
|
|
3469
|
+
}
|
|
3470
|
+
}
|
|
3471
|
+
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");
|
|
3472
|
+
const isTimeout = errorMessage.includes("timeout") || errorMessage.includes("\u8D85\u65F6") || errorCode === "TIMEOUT";
|
|
3473
|
+
const isRejected = errorMessage.includes("rejected") || errorMessage.includes("\u62D2\u7EDD") || errorCode === 4001;
|
|
3474
|
+
const isOriginNotAllowed = errorCode === 3e3 || /origin not allowed/i.test(errorMessage) || /Unauthorized:\s*origin not allowed/i.test(errorMessage);
|
|
3475
|
+
const currentMetadata = this.wallet ? {
|
|
3476
|
+
// Try to get metadata from wallet instance if available
|
|
3477
|
+
projectId: this.projectId,
|
|
3478
|
+
network
|
|
3479
|
+
} : null;
|
|
3480
|
+
const errorDetails = {
|
|
3481
|
+
error: errorMessage,
|
|
3482
|
+
detailedError,
|
|
3483
|
+
code: errorCode,
|
|
3484
|
+
isTelegram: this.isTelegramMiniApp(),
|
|
3485
|
+
network,
|
|
3486
|
+
chainId: targetChainId,
|
|
3487
|
+
projectId: this.projectId,
|
|
3488
|
+
metadata: currentMetadata,
|
|
3489
|
+
// Get URL from window.location if available
|
|
3490
|
+
currentUrl: typeof window !== "undefined" ? window.location.href : "N/A",
|
|
3491
|
+
telegramPlatform: typeof window !== "undefined" && window.Telegram?.WebApp?.platform || "N/A",
|
|
3492
|
+
errorType: isNoWalletFound ? "NO_WALLET_FOUND" : isTimeout ? "TIMEOUT" : isRejected ? "REJECTED" : "UNKNOWN"
|
|
3493
|
+
};
|
|
3494
|
+
console.error("[WalletConnect Tron] Connection error - Full details:", errorDetails);
|
|
3495
|
+
console.error("[WalletConnect Tron] Error object:", error);
|
|
3496
|
+
console.error("[WalletConnect Tron] Error stack:", error.stack);
|
|
3497
|
+
if (isNoWalletFound) {
|
|
3498
|
+
const noWalletErrorDetails = [
|
|
3499
|
+
`
|
|
3500
|
+
=== WalletConnect Tron: No Matching Wallet Found ===`,
|
|
3501
|
+
`Error: ${errorMessage}`,
|
|
3502
|
+
`Detailed: ${detailedError}`,
|
|
3503
|
+
`Code: ${errorCode || "N/A"}`,
|
|
3504
|
+
``,
|
|
3505
|
+
`Environment:`,
|
|
3506
|
+
` - Telegram Mini App: ${this.isTelegramMiniApp() ? "Yes" : "No"}`,
|
|
3507
|
+
` - Platform: ${errorDetails.telegramPlatform}`,
|
|
3508
|
+
` - Current URL: ${errorDetails.currentUrl}`,
|
|
3509
|
+
``,
|
|
3510
|
+
`Configuration:`,
|
|
3511
|
+
` - Project ID: ${this.projectId ? "Set" : "Missing"}`,
|
|
3512
|
+
` - Network: ${network}`,
|
|
3513
|
+
` - Chain ID: ${targetChainId}`,
|
|
3514
|
+
` - Metadata URL: ${typeof window !== "undefined" ? window.location.origin : "N/A"}`,
|
|
3515
|
+
``,
|
|
3516
|
+
`Possible Causes:`,
|
|
3517
|
+
` 1. No WalletConnect-compatible wallet (TokenPocket, etc.) installed on device`,
|
|
3518
|
+
` 2. Wallet app not opened or not responding to deep link (wc://)`,
|
|
3519
|
+
` 3. Deep link handling issue in Telegram Mini App environment`,
|
|
3520
|
+
` 4. WalletConnect session timeout (user took too long to approve)`,
|
|
3521
|
+
` 5. Network connectivity issue preventing WalletConnect relay connection`,
|
|
3522
|
+
``,
|
|
3523
|
+
`Solutions:`,
|
|
3524
|
+
` 1. Ensure TokenPocket or other WalletConnect-compatible wallet is installed`,
|
|
3525
|
+
` 2. Try opening the wallet app manually before connecting`,
|
|
3526
|
+
` 3. In Telegram Mini App, ensure the deep link popup is not blocked`,
|
|
3527
|
+
` 4. Try connecting again (may need to wait a few seconds)`,
|
|
3528
|
+
` 5. Check network connection and WalletConnect relay server accessibility`,
|
|
3529
|
+
``,
|
|
3530
|
+
`For more details, see the error object logged above.`,
|
|
3531
|
+
`===========================================
|
|
3532
|
+
`
|
|
3533
|
+
].join("\n");
|
|
3534
|
+
console.error(noWalletErrorDetails);
|
|
3535
|
+
throw new ConnectionRejectedError(
|
|
3536
|
+
`WalletConnect Tron: \u6CA1\u6709\u627E\u5230\u652F\u6301\u7684\u94B1\u5305 (No matching wallet found)
|
|
3537
|
+
|
|
3538
|
+
\u53EF\u80FD\u7684\u539F\u56E0\uFF1A
|
|
3539
|
+
1. \u8BBE\u5907\u4E0A\u672A\u5B89\u88C5\u652F\u6301 WalletConnect \u7684\u94B1\u5305\uFF08\u5982 TokenPocket\uFF09
|
|
3540
|
+
2. \u94B1\u5305\u5E94\u7528\u672A\u6253\u5F00\u6216\u672A\u54CD\u5E94 deep link (wc://)
|
|
3541
|
+
3. \u5728 Telegram Mini App \u4E2D\uFF0Cdeep link \u5904\u7406\u53EF\u80FD\u6709\u95EE\u9898
|
|
3542
|
+
4. \u8FDE\u63A5\u8D85\u65F6\uFF08\u7528\u6237\u672A\u53CA\u65F6\u6279\u51C6\uFF09
|
|
3543
|
+
5. \u7F51\u7EDC\u8FDE\u63A5\u95EE\u9898
|
|
3544
|
+
|
|
3545
|
+
\u89E3\u51B3\u65B9\u6848\uFF1A
|
|
3546
|
+
1. \u786E\u4FDD\u5DF2\u5B89\u88C5 TokenPocket \u6216\u5176\u4ED6\u652F\u6301 WalletConnect \u7684\u94B1\u5305
|
|
3547
|
+
2. \u5C1D\u8BD5\u624B\u52A8\u6253\u5F00\u94B1\u5305\u5E94\u7528\u540E\u518D\u8FDE\u63A5
|
|
3548
|
+
3. \u5728 Telegram Mini App \u4E2D\uFF0C\u786E\u4FDD deep link \u5F39\u7A97\u672A\u88AB\u963B\u6B62
|
|
3549
|
+
4. \u7A0D\u7B49\u51E0\u79D2\u540E\u91CD\u8BD5\u8FDE\u63A5
|
|
3550
|
+
5. \u68C0\u67E5\u7F51\u7EDC\u8FDE\u63A5\u548C WalletConnect \u4E2D\u7EE7\u670D\u52A1\u5668\u53EF\u8BBF\u95EE\u6027
|
|
3551
|
+
|
|
3552
|
+
\u8BE6\u7EC6\u9519\u8BEF\u4FE1\u606F\u8BF7\u67E5\u770B\u63A7\u5236\u53F0\u65E5\u5FD7\u3002`
|
|
3553
|
+
);
|
|
3554
|
+
}
|
|
3555
|
+
if (errorMessage.includes("Invalid") || errorMessage.includes("Configuration") || errorMessage.includes("App Config") || errorMessage.includes("Invalid App")) {
|
|
3556
|
+
const configErrorDetails = [
|
|
3557
|
+
`
|
|
3558
|
+
=== WalletConnect Tron Configuration Error ===`,
|
|
3559
|
+
`Error: ${errorMessage}`,
|
|
3560
|
+
`Detailed: ${detailedError}`,
|
|
3561
|
+
`Code: ${errorCode || "N/A"}`,
|
|
3562
|
+
`
|
|
3563
|
+
Environment:`,
|
|
3564
|
+
` - Telegram Mini App: ${this.isTelegramMiniApp() ? "Yes" : "No"}`,
|
|
3565
|
+
` - Platform: ${errorDetails.telegramPlatform}`,
|
|
3566
|
+
` - Current URL: ${errorDetails.currentUrl}`,
|
|
3567
|
+
`
|
|
3568
|
+
Configuration:`,
|
|
3569
|
+
` - Project ID: ${this.projectId ? "Set" : "Missing"}`,
|
|
3570
|
+
` - Network: ${network}`,
|
|
3571
|
+
` - Chain ID: ${targetChainId}`,
|
|
3572
|
+
`
|
|
3573
|
+
Possible Causes:`,
|
|
3574
|
+
` 1. Deep link (wc://) handling issue in Telegram Mini App`,
|
|
3575
|
+
` 2. Invalid metadata configuration (URL or icons not accessible)`,
|
|
3576
|
+
` 3. Network/chainId mismatch`,
|
|
3577
|
+
` 4. WalletConnect project ID not configured correctly`,
|
|
3578
|
+
` 5. Domain not added to WalletConnect Cloud allowlist`,
|
|
3579
|
+
`
|
|
3580
|
+
Please check:`,
|
|
3581
|
+
` - WalletConnect Project ID is valid and active`,
|
|
3582
|
+
` - Domain is added to WalletConnect Cloud allowlist (for serveo.net, etc.)`,
|
|
3583
|
+
` - Metadata URL is accessible: Check console for metadata logs`,
|
|
3584
|
+
` - Icons are accessible: Check console for icon URLs`,
|
|
3585
|
+
` - Network matches chainId: Expected ${network} for chainId ${targetChainId}`,
|
|
3586
|
+
`
|
|
3587
|
+
For more details, see the error object logged above.`,
|
|
3588
|
+
`===========================================
|
|
3589
|
+
`
|
|
3590
|
+
].join("\n");
|
|
3591
|
+
console.error(configErrorDetails);
|
|
3592
|
+
throw new ConfigurationError(
|
|
3593
|
+
`WalletConnect Tron connection failed: ${errorMessage}
|
|
3594
|
+
|
|
3595
|
+
Configuration Details:
|
|
3596
|
+
- Telegram Mini App: ${this.isTelegramMiniApp() ? "Yes" : "No"}
|
|
3597
|
+
- Platform: ${errorDetails.telegramPlatform}
|
|
3598
|
+
- Origin: ${origin || "(unknown)"}
|
|
3599
|
+
- Project ID: ${this.projectId ? "Set" : "Missing"}
|
|
3600
|
+
- Network: ${network}
|
|
3601
|
+
- Chain ID: ${targetChainId}
|
|
3602
|
+
|
|
3603
|
+
This "Invalid App Configuration" error may be caused by:
|
|
3604
|
+
1. Deep link (wc://) handling issue in Telegram Mini App
|
|
3605
|
+
2. Invalid metadata configuration (URL or icons)
|
|
3606
|
+
3. Network/chainId mismatch
|
|
3607
|
+
4. Domain not added to WalletConnect Cloud allowlist
|
|
3608
|
+
|
|
3609
|
+
Please check the console for detailed error information.`
|
|
3610
|
+
);
|
|
3611
|
+
}
|
|
3612
|
+
if (isOriginNotAllowed) {
|
|
3613
|
+
throw new ConfigurationError(
|
|
3614
|
+
`WalletConnect Tron relayer rejected this origin (code 3000: Unauthorized: origin not allowed).
|
|
3615
|
+
|
|
3616
|
+
Fix:
|
|
3617
|
+
1) Open WalletConnect Cloud \u2192 your project (${this.projectId})
|
|
3618
|
+
2) Add this site origin to the allowlist:
|
|
3619
|
+
- ${origin || "(unknown origin)"}
|
|
3620
|
+
|
|
3621
|
+
Common dev origins to allow:
|
|
3622
|
+
- http://localhost:5173
|
|
3623
|
+
- http://192.168.0.221:5173 (your LAN dev URL)
|
|
3624
|
+
- https://wallet-test.enclave-hq.com (your Cloudflare Tunnel/custom domain)
|
|
3625
|
+
|
|
3626
|
+
Original error: ${errorMessage}`
|
|
3627
|
+
);
|
|
3628
|
+
}
|
|
3629
|
+
if (isTimeout) {
|
|
3630
|
+
throw new ConnectionRejectedError(
|
|
3631
|
+
`WalletConnect Tron connection timeout. Please try again and ensure your wallet app is open and ready.`
|
|
3632
|
+
);
|
|
3633
|
+
}
|
|
3634
|
+
if (isRejected) {
|
|
3635
|
+
throw new ConnectionRejectedError(this.type);
|
|
3636
|
+
}
|
|
3637
|
+
throw error;
|
|
3638
|
+
}
|
|
3639
|
+
this.currentAddress = address;
|
|
3640
|
+
const account = {
|
|
3641
|
+
universalAddress: createUniversalAddress(targetChainId, address),
|
|
3642
|
+
nativeAddress: address,
|
|
3643
|
+
chainId: targetChainId,
|
|
3644
|
+
chainType: ChainType.TRON,
|
|
3645
|
+
isActive: true
|
|
3646
|
+
};
|
|
3647
|
+
this.setState("connected" /* CONNECTED */);
|
|
3648
|
+
this.setAccount(account);
|
|
3649
|
+
this.setupEventListeners();
|
|
3650
|
+
return account;
|
|
3651
|
+
} catch (error) {
|
|
3652
|
+
this.setState("error" /* ERROR */);
|
|
3653
|
+
this.setAccount(null);
|
|
3654
|
+
this.currentAddress = null;
|
|
3655
|
+
if (error.message?.includes("rejected") || error.code === 4001) {
|
|
3656
|
+
throw new ConnectionRejectedError(this.type);
|
|
3657
|
+
}
|
|
3658
|
+
throw error;
|
|
3659
|
+
}
|
|
3660
|
+
}
|
|
3661
|
+
/**
|
|
3662
|
+
* Disconnect wallet
|
|
3663
|
+
*/
|
|
3664
|
+
async disconnect() {
|
|
3665
|
+
this.removeEventListeners();
|
|
3666
|
+
if (this.wallet) {
|
|
3667
|
+
try {
|
|
3668
|
+
await this.wallet.disconnect();
|
|
3669
|
+
} catch (error) {
|
|
3670
|
+
console.warn("[WalletConnect Tron] Error during disconnect:", error);
|
|
3671
|
+
}
|
|
3672
|
+
}
|
|
3673
|
+
this.wallet = null;
|
|
3674
|
+
this.currentAddress = null;
|
|
3675
|
+
this.setState("disconnected" /* DISCONNECTED */);
|
|
3676
|
+
this.setAccount(null);
|
|
3677
|
+
this.emitDisconnected();
|
|
3678
|
+
}
|
|
3679
|
+
/**
|
|
3680
|
+
* Sign message
|
|
3681
|
+
*/
|
|
3682
|
+
async signMessage(message) {
|
|
3683
|
+
this.ensureConnected();
|
|
3684
|
+
try {
|
|
3685
|
+
if (!this.wallet) {
|
|
3686
|
+
throw new Error("Wallet not initialized");
|
|
3687
|
+
}
|
|
3688
|
+
const signature = await this.wallet.signMessage(message);
|
|
3689
|
+
if (typeof signature === "string") {
|
|
3690
|
+
return signature;
|
|
3691
|
+
} else if (signature && typeof signature === "object") {
|
|
3692
|
+
if ("signature" in signature) {
|
|
3693
|
+
return signature.signature;
|
|
3694
|
+
} else if ("result" in signature) {
|
|
3695
|
+
return signature.result;
|
|
3696
|
+
} else {
|
|
3697
|
+
return JSON.stringify(signature);
|
|
3698
|
+
}
|
|
3699
|
+
}
|
|
3700
|
+
throw new Error("Invalid signature format returned from wallet");
|
|
3701
|
+
} catch (error) {
|
|
3702
|
+
console.error("[WalletConnect Tron] Sign message error:", error);
|
|
3703
|
+
let errorMessage = "Unknown error";
|
|
3704
|
+
if (typeof error === "string") {
|
|
3705
|
+
errorMessage = error;
|
|
3706
|
+
} else if (error?.message) {
|
|
3707
|
+
errorMessage = error.message;
|
|
3708
|
+
} else if (error?.error?.message) {
|
|
3709
|
+
errorMessage = error.error.message;
|
|
3710
|
+
} else {
|
|
3711
|
+
try {
|
|
3712
|
+
errorMessage = JSON.stringify(error);
|
|
3713
|
+
} catch {
|
|
3714
|
+
errorMessage = String(error);
|
|
3715
|
+
}
|
|
3716
|
+
}
|
|
3717
|
+
if (errorMessage?.includes("rejected") || errorMessage?.includes("declined") || errorMessage?.includes("User rejected") || error?.code === 4001 || error?.code === "USER_REJECTED" || error?.error?.code === 4001) {
|
|
3718
|
+
throw new SignatureRejectedError();
|
|
3719
|
+
}
|
|
3720
|
+
if (errorMessage?.includes("not supported") || errorMessage?.includes("method not found") || errorMessage?.includes("Method not found") || error?.code === -32601 || error?.error?.code === -32601) {
|
|
3721
|
+
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.");
|
|
3722
|
+
}
|
|
3723
|
+
throw new Error(`WalletConnect Tron sign message failed: ${errorMessage}`);
|
|
3724
|
+
}
|
|
3725
|
+
}
|
|
3726
|
+
/**
|
|
3727
|
+
* Sign transaction
|
|
3728
|
+
*
|
|
3729
|
+
* @param transaction - Tron transaction object
|
|
3730
|
+
* Can be created using TronWeb (if available) or any TRON transaction builder
|
|
3731
|
+
* Format: { raw_data: {...}, raw_data_hex: "...", txID: "..." }
|
|
3732
|
+
* @returns Signed transaction object or signature
|
|
3733
|
+
*/
|
|
3734
|
+
async signTransaction(transaction) {
|
|
3735
|
+
this.ensureConnected();
|
|
3736
|
+
try {
|
|
3737
|
+
if (!this.wallet) {
|
|
3738
|
+
throw new Error("Wallet not initialized");
|
|
3739
|
+
}
|
|
3740
|
+
if (!transaction) {
|
|
3741
|
+
throw new Error("Transaction object is required");
|
|
3742
|
+
}
|
|
3743
|
+
console.log("[WalletConnect Tron] Signing transaction:", {
|
|
3744
|
+
hasRawData: !!transaction.raw_data,
|
|
3745
|
+
hasRawDataHex: !!transaction.raw_data_hex,
|
|
3746
|
+
hasTxID: !!transaction.txID
|
|
3747
|
+
});
|
|
3748
|
+
const result = await this.wallet.signTransaction(transaction);
|
|
3749
|
+
if (typeof result === "string") {
|
|
3750
|
+
return result;
|
|
3751
|
+
} else if (result && typeof result === "object") {
|
|
3752
|
+
if ("txID" in result && typeof result.txID === "string") {
|
|
3753
|
+
return result.txID;
|
|
3754
|
+
} else if ("txid" in result && typeof result.txid === "string") {
|
|
3755
|
+
return result.txid;
|
|
3756
|
+
} else if ("signature" in result) {
|
|
3757
|
+
return JSON.stringify(result);
|
|
3758
|
+
} else {
|
|
3759
|
+
return JSON.stringify(result);
|
|
3760
|
+
}
|
|
3761
|
+
}
|
|
3762
|
+
throw new Error("Invalid signature format returned from wallet");
|
|
3763
|
+
} catch (error) {
|
|
3764
|
+
console.error("[WalletConnect Tron] Sign transaction error:", error);
|
|
3765
|
+
let errorMessage = "Unknown error";
|
|
3766
|
+
if (typeof error === "string") {
|
|
3767
|
+
errorMessage = error;
|
|
3768
|
+
} else if (error?.message) {
|
|
3769
|
+
errorMessage = error.message;
|
|
3770
|
+
} else if (error?.error?.message) {
|
|
3771
|
+
errorMessage = error.error.message;
|
|
3772
|
+
} else if (error?.data?.message) {
|
|
3773
|
+
errorMessage = error.data.message;
|
|
3774
|
+
} else {
|
|
3775
|
+
try {
|
|
3776
|
+
errorMessage = JSON.stringify(error);
|
|
3777
|
+
} catch {
|
|
3778
|
+
errorMessage = String(error);
|
|
3779
|
+
}
|
|
3780
|
+
}
|
|
3781
|
+
if (errorMessage?.includes("rejected") || errorMessage?.includes("declined") || errorMessage?.includes("User rejected") || error?.code === 4001 || error?.code === "USER_REJECTED" || error?.error?.code === 4001) {
|
|
3782
|
+
throw new SignatureRejectedError("Transaction signature was rejected by user");
|
|
3783
|
+
}
|
|
3784
|
+
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) {
|
|
3785
|
+
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.");
|
|
3786
|
+
}
|
|
3787
|
+
throw new Error(`WalletConnect Tron sign transaction failed: ${errorMessage}`);
|
|
3788
|
+
}
|
|
3789
|
+
}
|
|
3790
|
+
/**
|
|
3791
|
+
* Read contract (not supported by WalletConnect)
|
|
3792
|
+
*/
|
|
3793
|
+
async readContract(_params) {
|
|
3794
|
+
this.ensureConnected();
|
|
3795
|
+
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.");
|
|
3796
|
+
}
|
|
3797
|
+
/**
|
|
3798
|
+
* Write contract (not yet implemented)
|
|
3799
|
+
*/
|
|
3800
|
+
async writeContract(_params) {
|
|
3801
|
+
throw new Error("Contract write not yet implemented for WalletConnect Tron");
|
|
3802
|
+
}
|
|
3803
|
+
/**
|
|
3804
|
+
* Estimate gas (not yet implemented)
|
|
3805
|
+
*/
|
|
3806
|
+
async estimateGas(_params) {
|
|
3807
|
+
throw new Error("Gas estimation not yet implemented for WalletConnect Tron");
|
|
3808
|
+
}
|
|
3809
|
+
/**
|
|
3810
|
+
* Wait for transaction (not yet implemented)
|
|
3811
|
+
*/
|
|
3812
|
+
async waitForTransaction(_txHash, _confirmations) {
|
|
3813
|
+
throw new Error("Transaction waiting not yet implemented for WalletConnect Tron");
|
|
3814
|
+
}
|
|
3815
|
+
/**
|
|
3816
|
+
* Setup event listeners
|
|
3817
|
+
*/
|
|
3818
|
+
setupEventListeners() {
|
|
3819
|
+
if (!this.wallet) {
|
|
3820
|
+
return;
|
|
3821
|
+
}
|
|
3822
|
+
this.wallet.on("accountsChanged", (accounts) => {
|
|
3823
|
+
if (accounts && accounts.length > 0 && accounts[0] !== this.currentAddress) {
|
|
3824
|
+
const newAddress = accounts[0];
|
|
3825
|
+
this.currentAddress = newAddress;
|
|
3826
|
+
if (this.currentAccount) {
|
|
3827
|
+
const newAccount = {
|
|
3828
|
+
...this.currentAccount,
|
|
3829
|
+
nativeAddress: newAddress,
|
|
3830
|
+
universalAddress: createUniversalAddress(this.currentAccount.chainId, newAddress)
|
|
3831
|
+
};
|
|
3832
|
+
this.setAccount(newAccount);
|
|
3833
|
+
this.emit("accountChanged", newAccount);
|
|
3834
|
+
}
|
|
3835
|
+
} else if (!accounts || accounts.length === 0) {
|
|
3836
|
+
this.disconnect();
|
|
3837
|
+
}
|
|
3838
|
+
});
|
|
3839
|
+
this.wallet.on("disconnect", () => {
|
|
3840
|
+
this.disconnect();
|
|
3841
|
+
});
|
|
3842
|
+
}
|
|
3843
|
+
/**
|
|
3844
|
+
* Remove event listeners
|
|
3845
|
+
*/
|
|
3846
|
+
removeEventListeners() {
|
|
3847
|
+
if (!this.wallet) {
|
|
3848
|
+
return;
|
|
3849
|
+
}
|
|
3850
|
+
this.wallet.removeAllListeners("accountsChanged");
|
|
3851
|
+
this.wallet.removeAllListeners("disconnect");
|
|
3852
|
+
}
|
|
3853
|
+
/**
|
|
3854
|
+
* Get provider (returns wallet instance)
|
|
3855
|
+
*/
|
|
3856
|
+
getProvider() {
|
|
3857
|
+
return this.wallet;
|
|
3858
|
+
}
|
|
3859
|
+
/**
|
|
3860
|
+
* Clear static wallet instance (for complete cleanup)
|
|
3861
|
+
*/
|
|
3862
|
+
static clearWalletInstance() {
|
|
3863
|
+
if (_WalletConnectTronAdapter.walletInstance) {
|
|
3864
|
+
_WalletConnectTronAdapter.walletInstance.disconnect().catch(() => {
|
|
3865
|
+
});
|
|
3866
|
+
_WalletConnectTronAdapter.walletInstance = null;
|
|
3867
|
+
_WalletConnectTronAdapter.walletProjectId = null;
|
|
3868
|
+
}
|
|
3869
|
+
}
|
|
3870
|
+
};
|
|
3871
|
+
// Tron 主网链 ID
|
|
3872
|
+
_WalletConnectTronAdapter.TRON_MAINNET_CHAIN_ID = 195;
|
|
3873
|
+
// Static wallet instance to avoid multiple initializations
|
|
3874
|
+
_WalletConnectTronAdapter.walletInstance = null;
|
|
3875
|
+
_WalletConnectTronAdapter.walletProjectId = null;
|
|
3876
|
+
var WalletConnectTronAdapter = _WalletConnectTronAdapter;
|
|
3877
|
+
|
|
3878
|
+
// src/adapters/deep-link/adapter.ts
|
|
3879
|
+
init_types();
|
|
3880
|
+
var _DeepLinkAdapter = class _DeepLinkAdapter extends WalletAdapter {
|
|
3881
|
+
constructor(config) {
|
|
3882
|
+
super();
|
|
3883
|
+
this.currentChainId = null;
|
|
3884
|
+
this.currentChainType = null;
|
|
3885
|
+
this.provider = this.createProvider(config);
|
|
3886
|
+
this.name = `${this.provider.name} (Deep Link)`;
|
|
3887
|
+
this.icon = this.provider.icon;
|
|
3888
|
+
if (this.provider.supportedChainTypes.includes(ChainType.EVM)) {
|
|
3889
|
+
this.chainType = ChainType.EVM;
|
|
3890
|
+
this.type = "deep-link-evm" /* DEEP_LINK_EVM */;
|
|
3891
|
+
} else if (this.provider.supportedChainTypes.includes(ChainType.TRON)) {
|
|
3892
|
+
this.chainType = ChainType.TRON;
|
|
3893
|
+
this.type = "deep-link-tron" /* DEEP_LINK_TRON */;
|
|
3894
|
+
} else {
|
|
3895
|
+
this.chainType = ChainType.EVM;
|
|
3896
|
+
this.type = "deep-link-evm" /* DEEP_LINK_EVM */;
|
|
3897
|
+
}
|
|
3898
|
+
if (typeof window !== "undefined") {
|
|
3899
|
+
this.setupCallbackHandler();
|
|
3900
|
+
}
|
|
3901
|
+
}
|
|
3902
|
+
/**
|
|
3903
|
+
* Create provider instance based on type
|
|
3904
|
+
*/
|
|
3905
|
+
createProvider(config) {
|
|
3906
|
+
switch (config.providerType) {
|
|
3907
|
+
case "tokenpocket" /* TOKENPOCKET */: {
|
|
3908
|
+
const { TokenPocketDeepLinkProvider: TokenPocketDeepLinkProvider2 } = (init_tokenpocket(), __toCommonJS(tokenpocket_exports));
|
|
3909
|
+
return new TokenPocketDeepLinkProvider2({
|
|
3910
|
+
callbackUrl: config.callbackUrl,
|
|
3911
|
+
callbackSchema: config.callbackSchema
|
|
3912
|
+
});
|
|
3913
|
+
}
|
|
3914
|
+
case "tronlink" /* TRONLINK */: {
|
|
3915
|
+
const { TronLinkDeepLinkProvider: TronLinkDeepLinkProvider2 } = (init_tronlink(), __toCommonJS(tronlink_exports));
|
|
3916
|
+
return new TronLinkDeepLinkProvider2();
|
|
3917
|
+
}
|
|
3918
|
+
case "imtoken" /* IMTOKEN */: {
|
|
3919
|
+
const { ImTokenDeepLinkProvider: ImTokenDeepLinkProvider2 } = (init_imtoken(), __toCommonJS(imtoken_exports));
|
|
3920
|
+
return new ImTokenDeepLinkProvider2({
|
|
3921
|
+
callbackUrl: config.callbackUrl,
|
|
3922
|
+
callbackSchema: config.callbackSchema
|
|
3923
|
+
});
|
|
3924
|
+
}
|
|
3925
|
+
case "metamask" /* METAMASK */: {
|
|
3926
|
+
const { MetaMaskDeepLinkProvider: MetaMaskDeepLinkProvider2 } = (init_metamask(), __toCommonJS(metamask_exports));
|
|
3927
|
+
return new MetaMaskDeepLinkProvider2();
|
|
3928
|
+
}
|
|
3929
|
+
case "okx" /* OKX */: {
|
|
3930
|
+
const { OKXDeepLinkProvider: OKXDeepLinkProvider2 } = (init_okx(), __toCommonJS(okx_exports));
|
|
3931
|
+
return new OKXDeepLinkProvider2();
|
|
3932
|
+
}
|
|
3933
|
+
default:
|
|
3934
|
+
throw new Error(`Unsupported deep link provider type: ${config.providerType}`);
|
|
3935
|
+
}
|
|
3936
|
+
}
|
|
3937
|
+
/**
|
|
3938
|
+
* Setup callback handler for deep link results
|
|
3939
|
+
*/
|
|
3940
|
+
setupCallbackHandler() {
|
|
3941
|
+
if (typeof window === "undefined") {
|
|
3942
|
+
return;
|
|
3943
|
+
}
|
|
3944
|
+
const handleUrlChange = () => {
|
|
3945
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
3946
|
+
const result = this.provider.parseCallbackResult(urlParams);
|
|
3947
|
+
if (result.actionId && _DeepLinkAdapter.pendingActions.has(result.actionId)) {
|
|
3948
|
+
const callback = _DeepLinkAdapter.pendingActions.get(result.actionId);
|
|
3949
|
+
if (result.error) {
|
|
3950
|
+
callback.reject(new Error(result.error));
|
|
3951
|
+
} else if (result.result) {
|
|
3952
|
+
callback.resolve(result.result);
|
|
3953
|
+
}
|
|
3954
|
+
_DeepLinkAdapter.pendingActions.delete(result.actionId);
|
|
3955
|
+
}
|
|
3956
|
+
};
|
|
3957
|
+
window.addEventListener("popstate", handleUrlChange);
|
|
3958
|
+
window.addEventListener("hashchange", handleUrlChange);
|
|
3959
|
+
handleUrlChange();
|
|
3960
|
+
}
|
|
3961
|
+
/**
|
|
3962
|
+
* Check if deep link is available
|
|
3963
|
+
*/
|
|
3964
|
+
async isAvailable() {
|
|
3965
|
+
return this.provider.isAvailable();
|
|
3966
|
+
}
|
|
3967
|
+
/**
|
|
3968
|
+
* Connect to wallet via deep link
|
|
3969
|
+
*
|
|
3970
|
+
* Note: Deep links typically don't support persistent connections
|
|
3971
|
+
* This method may throw ConnectionRejectedError as deep links are
|
|
3972
|
+
* primarily used for signing operations, not connection
|
|
3973
|
+
*/
|
|
3974
|
+
async connect(chainId) {
|
|
3975
|
+
const targetChainId = Array.isArray(chainId) ? chainId[0] : chainId || 1;
|
|
3976
|
+
let chainType;
|
|
3977
|
+
if (targetChainId === 195) {
|
|
3978
|
+
chainType = ChainType.TRON;
|
|
3979
|
+
} else {
|
|
3980
|
+
chainType = ChainType.EVM;
|
|
3981
|
+
}
|
|
3982
|
+
if (!this.provider.supportedChainTypes.includes(chainType)) {
|
|
3983
|
+
throw new Error(
|
|
3984
|
+
`Provider ${this.provider.name} does not support chain type ${chainType}`
|
|
3985
|
+
);
|
|
3986
|
+
}
|
|
3987
|
+
if (this.provider.buildConnectLink) {
|
|
3988
|
+
const linkInfo = this.provider.buildConnectLink({
|
|
3989
|
+
chainId: targetChainId,
|
|
3990
|
+
chainType
|
|
3991
|
+
});
|
|
3992
|
+
if (linkInfo.actionId) {
|
|
3993
|
+
return new Promise((resolve, reject) => {
|
|
3994
|
+
_DeepLinkAdapter.pendingActions.set(linkInfo.actionId, {
|
|
3995
|
+
resolve: (result) => {
|
|
3996
|
+
const address = result?.address || result?.account || result;
|
|
3997
|
+
if (!address || typeof address !== "string") {
|
|
3998
|
+
reject(new ConnectionRejectedError("Invalid connection result: no address found"));
|
|
3999
|
+
return;
|
|
4000
|
+
}
|
|
4001
|
+
const account = {
|
|
4002
|
+
universalAddress: createUniversalAddress(targetChainId, address),
|
|
4003
|
+
nativeAddress: address,
|
|
4004
|
+
chainId: targetChainId,
|
|
4005
|
+
chainType,
|
|
4006
|
+
isActive: true
|
|
4007
|
+
};
|
|
4008
|
+
this.setState("connected" /* CONNECTED */);
|
|
4009
|
+
this.setAccount(account);
|
|
4010
|
+
this.emit("connected", account);
|
|
4011
|
+
resolve(account);
|
|
4012
|
+
},
|
|
4013
|
+
reject: (error) => {
|
|
4014
|
+
this.setState("disconnected" /* DISCONNECTED */);
|
|
4015
|
+
reject(error);
|
|
4016
|
+
}
|
|
4017
|
+
});
|
|
4018
|
+
window.location.href = linkInfo.url;
|
|
4019
|
+
setTimeout(() => {
|
|
4020
|
+
if (_DeepLinkAdapter.pendingActions.has(linkInfo.actionId)) {
|
|
4021
|
+
_DeepLinkAdapter.pendingActions.delete(linkInfo.actionId);
|
|
4022
|
+
this.setState("disconnected" /* DISCONNECTED */);
|
|
4023
|
+
reject(new ConnectionRejectedError("Deep link connection timeout"));
|
|
4024
|
+
}
|
|
4025
|
+
}, 3e4);
|
|
4026
|
+
});
|
|
4027
|
+
} else {
|
|
4028
|
+
window.location.href = linkInfo.url;
|
|
4029
|
+
throw new ConnectionRejectedError(
|
|
4030
|
+
"Deep link connection initiated. Please complete the connection in your wallet app."
|
|
4031
|
+
);
|
|
4032
|
+
}
|
|
4033
|
+
} else {
|
|
4034
|
+
throw new ConnectionRejectedError(
|
|
4035
|
+
`Deep link connection is not supported by ${this.provider.name}. Deep links are primarily used for signing operations.`
|
|
4036
|
+
);
|
|
4037
|
+
}
|
|
4038
|
+
}
|
|
4039
|
+
/**
|
|
4040
|
+
* Disconnect from wallet
|
|
4041
|
+
*/
|
|
4042
|
+
async disconnect() {
|
|
4043
|
+
this.setState("disconnected" /* DISCONNECTED */);
|
|
4044
|
+
this.setAccount(null);
|
|
4045
|
+
this.currentChainId = null;
|
|
4046
|
+
this.currentChainType = null;
|
|
4047
|
+
this.emitDisconnected();
|
|
4048
|
+
}
|
|
4049
|
+
/**
|
|
4050
|
+
* Sign a message
|
|
4051
|
+
*/
|
|
4052
|
+
async signMessage(message) {
|
|
4053
|
+
this.ensureConnected();
|
|
4054
|
+
if (!this.currentChainId || !this.currentChainType) {
|
|
4055
|
+
throw new WalletNotConnectedError(this.type);
|
|
4056
|
+
}
|
|
4057
|
+
const linkInfo = this.provider.buildSignMessageLink({
|
|
4058
|
+
message,
|
|
4059
|
+
chainId: this.currentChainId,
|
|
4060
|
+
chainType: this.currentChainType
|
|
4061
|
+
});
|
|
4062
|
+
if (linkInfo.callbackSchema || linkInfo.callbackUrl) {
|
|
4063
|
+
return new Promise((resolve, reject) => {
|
|
4064
|
+
_DeepLinkAdapter.pendingActions.set(linkInfo.actionId, { resolve, reject });
|
|
4065
|
+
window.location.href = linkInfo.url;
|
|
4066
|
+
setTimeout(() => {
|
|
4067
|
+
if (_DeepLinkAdapter.pendingActions.has(linkInfo.actionId)) {
|
|
4068
|
+
_DeepLinkAdapter.pendingActions.delete(linkInfo.actionId);
|
|
4069
|
+
reject(new SignatureRejectedError("Message signature timeout"));
|
|
4070
|
+
}
|
|
4071
|
+
}, 3e4);
|
|
4072
|
+
});
|
|
4073
|
+
} else {
|
|
4074
|
+
window.location.href = linkInfo.url;
|
|
4075
|
+
throw new SignatureRejectedError(
|
|
4076
|
+
"Deep link signature initiated. Please complete the signature in your wallet app."
|
|
4077
|
+
);
|
|
4078
|
+
}
|
|
4079
|
+
}
|
|
4080
|
+
/**
|
|
4081
|
+
* Sign a transaction
|
|
4082
|
+
*/
|
|
4083
|
+
async signTransaction(transaction) {
|
|
4084
|
+
this.ensureConnected();
|
|
4085
|
+
if (!this.currentChainId || !this.currentChainType) {
|
|
4086
|
+
throw new WalletNotConnectedError(this.type);
|
|
4087
|
+
}
|
|
4088
|
+
const linkInfo = this.provider.buildSignTransactionLink({
|
|
4089
|
+
transaction,
|
|
4090
|
+
chainId: this.currentChainId,
|
|
4091
|
+
chainType: this.currentChainType
|
|
4092
|
+
});
|
|
4093
|
+
if (linkInfo.callbackSchema || linkInfo.callbackUrl) {
|
|
4094
|
+
return new Promise((resolve, reject) => {
|
|
4095
|
+
_DeepLinkAdapter.pendingActions.set(linkInfo.actionId, { resolve, reject });
|
|
4096
|
+
window.location.href = linkInfo.url;
|
|
4097
|
+
setTimeout(() => {
|
|
4098
|
+
if (_DeepLinkAdapter.pendingActions.has(linkInfo.actionId)) {
|
|
4099
|
+
_DeepLinkAdapter.pendingActions.delete(linkInfo.actionId);
|
|
4100
|
+
reject(new SignatureRejectedError("Transaction signature timeout"));
|
|
4101
|
+
}
|
|
4102
|
+
}, 3e4);
|
|
4103
|
+
});
|
|
4104
|
+
} else {
|
|
4105
|
+
window.location.href = linkInfo.url;
|
|
4106
|
+
throw new SignatureRejectedError(
|
|
4107
|
+
"Deep link transaction signature initiated. Please complete the signature in your wallet app."
|
|
4108
|
+
);
|
|
4109
|
+
}
|
|
4110
|
+
}
|
|
4111
|
+
/**
|
|
4112
|
+
* Get provider (not applicable for deep links)
|
|
4113
|
+
*/
|
|
4114
|
+
getProvider() {
|
|
4115
|
+
return null;
|
|
4116
|
+
}
|
|
4117
|
+
/**
|
|
4118
|
+
* Static method to handle callback from wallet apps
|
|
4119
|
+
* This can be called from anywhere in the application
|
|
4120
|
+
*/
|
|
4121
|
+
static handleCallback() {
|
|
4122
|
+
if (typeof window === "undefined") {
|
|
4123
|
+
return;
|
|
4124
|
+
}
|
|
4125
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
4126
|
+
const actionId = urlParams.get("actionId");
|
|
4127
|
+
if (actionId && _DeepLinkAdapter.pendingActions.has(actionId)) {
|
|
4128
|
+
const callback = _DeepLinkAdapter.pendingActions.get(actionId);
|
|
4129
|
+
const result = urlParams.get("result");
|
|
4130
|
+
const error = urlParams.get("error");
|
|
4131
|
+
if (error) {
|
|
4132
|
+
callback.reject(new Error(error));
|
|
4133
|
+
} else if (result) {
|
|
4134
|
+
try {
|
|
4135
|
+
const parsedResult = JSON.parse(decodeURIComponent(result));
|
|
4136
|
+
callback.resolve(parsedResult);
|
|
4137
|
+
} catch (e) {
|
|
4138
|
+
callback.resolve(result);
|
|
4139
|
+
}
|
|
4140
|
+
}
|
|
4141
|
+
_DeepLinkAdapter.pendingActions.delete(actionId);
|
|
4142
|
+
}
|
|
4143
|
+
}
|
|
4144
|
+
/**
|
|
4145
|
+
* Set current account (called after successful connection)
|
|
4146
|
+
*/
|
|
4147
|
+
setAccount(account) {
|
|
4148
|
+
this.currentAccount = account;
|
|
4149
|
+
if (account) {
|
|
4150
|
+
this.currentChainId = account.chainId;
|
|
4151
|
+
this.currentChainType = account.chainType;
|
|
4152
|
+
}
|
|
4153
|
+
}
|
|
4154
|
+
/**
|
|
4155
|
+
* Emit disconnected event
|
|
4156
|
+
*/
|
|
4157
|
+
emitDisconnected() {
|
|
4158
|
+
this.emit("disconnected");
|
|
4159
|
+
}
|
|
4160
|
+
};
|
|
4161
|
+
// Static map to store pending actions across all instances
|
|
4162
|
+
// Key: actionId, Value: { resolve, reject }
|
|
4163
|
+
_DeepLinkAdapter.pendingActions = /* @__PURE__ */ new Map();
|
|
4164
|
+
var DeepLinkAdapter = _DeepLinkAdapter;
|
|
4165
|
+
|
|
4166
|
+
// src/core/adapter-registry.ts
|
|
4167
|
+
var AdapterRegistry = class {
|
|
4168
|
+
constructor(config = {}) {
|
|
4169
|
+
this.adapters = /* @__PURE__ */ new Map();
|
|
4170
|
+
this.config = config;
|
|
4171
|
+
this.registerDefaultAdapters();
|
|
4172
|
+
}
|
|
4173
|
+
/**
|
|
4174
|
+
* Register default adapters
|
|
4175
|
+
*/
|
|
4176
|
+
registerDefaultAdapters() {
|
|
4177
|
+
this.register("metamask" /* METAMASK */, () => new MetaMaskAdapter());
|
|
4178
|
+
this.register("private-key" /* PRIVATE_KEY */, () => new EVMPrivateKeyAdapter());
|
|
4179
|
+
if (this.config.walletConnectProjectId) {
|
|
4180
|
+
this.register(
|
|
4181
|
+
"walletconnect" /* WALLETCONNECT */,
|
|
4182
|
+
() => new WalletConnectAdapter(this.config.walletConnectProjectId)
|
|
4183
|
+
);
|
|
4184
|
+
this.register(
|
|
4185
|
+
"walletconnect-tron" /* WALLETCONNECT_TRON */,
|
|
4186
|
+
() => new WalletConnectTronAdapter(this.config.walletConnectProjectId)
|
|
4187
|
+
);
|
|
4188
|
+
}
|
|
4189
|
+
this.register("tronlink" /* TRONLINK */, () => new TronLinkAdapter());
|
|
4190
|
+
this.register(
|
|
4191
|
+
"deep-link-evm" /* DEEP_LINK_EVM */,
|
|
4192
|
+
() => new DeepLinkAdapter({
|
|
4193
|
+
providerType: "tokenpocket" /* TOKENPOCKET */
|
|
4194
|
+
})
|
|
4195
|
+
);
|
|
4196
|
+
this.register(
|
|
4197
|
+
"deep-link-tron" /* DEEP_LINK_TRON */,
|
|
4198
|
+
() => new DeepLinkAdapter({
|
|
4199
|
+
providerType: "tokenpocket" /* TOKENPOCKET */
|
|
4200
|
+
})
|
|
4201
|
+
);
|
|
4202
|
+
}
|
|
4203
|
+
/**
|
|
4204
|
+
* Register adapter
|
|
4205
|
+
*/
|
|
4206
|
+
register(type, factory) {
|
|
4207
|
+
this.adapters.set(type, factory);
|
|
4208
|
+
}
|
|
4209
|
+
/**
|
|
4210
|
+
* Get adapter
|
|
4211
|
+
*/
|
|
4212
|
+
getAdapter(type) {
|
|
4213
|
+
const factory = this.adapters.get(type);
|
|
4214
|
+
if (!factory) {
|
|
4215
|
+
return null;
|
|
4216
|
+
}
|
|
4217
|
+
return factory();
|
|
4218
|
+
}
|
|
4219
|
+
/**
|
|
4220
|
+
* Check if adapter is registered
|
|
4221
|
+
*/
|
|
4222
|
+
has(type) {
|
|
4223
|
+
return this.adapters.has(type);
|
|
4224
|
+
}
|
|
4225
|
+
/**
|
|
4226
|
+
* Get all registered adapter types
|
|
4227
|
+
*/
|
|
4228
|
+
getRegisteredTypes() {
|
|
4229
|
+
return Array.from(this.adapters.keys());
|
|
4230
|
+
}
|
|
4231
|
+
/**
|
|
4232
|
+
* 根据链类型获取适配器类型列表
|
|
4233
|
+
*/
|
|
4234
|
+
getAdapterTypesByChainType(chainType) {
|
|
4235
|
+
const types = [];
|
|
4236
|
+
for (const type of this.adapters.keys()) {
|
|
4237
|
+
const adapter = this.getAdapter(type);
|
|
4238
|
+
if (adapter && adapter.chainType === chainType) {
|
|
4239
|
+
types.push(type);
|
|
4240
|
+
}
|
|
4241
|
+
}
|
|
4242
|
+
return types;
|
|
4243
|
+
}
|
|
4244
|
+
};
|
|
4245
|
+
|
|
4246
|
+
// src/core/wallet-manager.ts
|
|
4247
|
+
init_types();
|
|
4248
|
+
var QRCodeSigner = class {
|
|
4249
|
+
constructor(config) {
|
|
4250
|
+
this.pollTimer = null;
|
|
4251
|
+
this.timeoutTimer = null;
|
|
4252
|
+
this.status = "waiting" /* WAITING */;
|
|
4253
|
+
this.qrCodeDataUrl = null;
|
|
4254
|
+
this.result = null;
|
|
4255
|
+
this.config = {
|
|
4256
|
+
requestId: config.requestId,
|
|
4257
|
+
requestUrl: config.requestUrl,
|
|
4258
|
+
pollUrl: config.pollUrl || "",
|
|
4259
|
+
pollInterval: config.pollInterval || 2e3,
|
|
4260
|
+
timeout: config.timeout || 3e5,
|
|
4261
|
+
// 5 minutes
|
|
4262
|
+
pollFn: config.pollFn
|
|
4263
|
+
};
|
|
4264
|
+
}
|
|
4265
|
+
/**
|
|
4266
|
+
* 生成二维码图片(Data URL)
|
|
4267
|
+
*/
|
|
4268
|
+
async generateQRCode(options) {
|
|
4269
|
+
if (this.qrCodeDataUrl) {
|
|
4270
|
+
return this.qrCodeDataUrl;
|
|
4271
|
+
}
|
|
4272
|
+
try {
|
|
4273
|
+
const qrCodeOptions = {
|
|
4274
|
+
width: options?.width || 300,
|
|
4275
|
+
margin: options?.margin || 2,
|
|
4276
|
+
color: {
|
|
4277
|
+
dark: options?.color?.dark || "#000000",
|
|
4278
|
+
light: options?.color?.light || "#FFFFFF"
|
|
4279
|
+
}
|
|
4280
|
+
};
|
|
4281
|
+
this.qrCodeDataUrl = await QRCode.toDataURL(this.config.requestUrl, qrCodeOptions);
|
|
4282
|
+
return this.qrCodeDataUrl;
|
|
4283
|
+
} catch (error) {
|
|
4284
|
+
throw new Error(`Failed to generate QR code: ${error instanceof Error ? error.message : String(error)}`);
|
|
4285
|
+
}
|
|
4286
|
+
}
|
|
4287
|
+
/**
|
|
4288
|
+
* 开始轮询签名结果
|
|
4289
|
+
*/
|
|
4290
|
+
async startPolling(onStatusChange, onResult) {
|
|
4291
|
+
if (this.status === "success" /* SUCCESS */ && this.result?.signature) {
|
|
4292
|
+
return this.result.signature;
|
|
4293
|
+
}
|
|
4294
|
+
if (this.status === "cancelled" /* CANCELLED */ || this.status === "timeout" /* TIMEOUT */) {
|
|
4295
|
+
throw new SignatureRejectedError("Signature request was cancelled or timed out");
|
|
4296
|
+
}
|
|
4297
|
+
this.timeoutTimer = setTimeout(() => {
|
|
4298
|
+
this.stopPolling();
|
|
4299
|
+
this.status = "timeout" /* TIMEOUT */;
|
|
4300
|
+
onStatusChange?.(this.status);
|
|
4301
|
+
throw new SignatureRejectedError("Signature request timed out");
|
|
4302
|
+
}, this.config.timeout);
|
|
4303
|
+
return new Promise((resolve, reject) => {
|
|
4304
|
+
const poll = async () => {
|
|
4305
|
+
try {
|
|
4306
|
+
let result = null;
|
|
4307
|
+
if (this.config.pollFn) {
|
|
4308
|
+
result = await this.config.pollFn(this.config.requestId);
|
|
4309
|
+
} else if (this.config.pollUrl) {
|
|
4310
|
+
result = await this.defaultPoll(this.config.requestId);
|
|
4311
|
+
} else {
|
|
4312
|
+
return;
|
|
4313
|
+
}
|
|
4314
|
+
if (result?.completed) {
|
|
4315
|
+
this.stopPolling();
|
|
4316
|
+
this.result = result;
|
|
4317
|
+
if (result.signature) {
|
|
4318
|
+
this.status = "success" /* SUCCESS */;
|
|
4319
|
+
onStatusChange?.(this.status);
|
|
4320
|
+
onResult?.(result);
|
|
4321
|
+
resolve(result.signature);
|
|
4322
|
+
} else if (result.error) {
|
|
4323
|
+
this.status = "failed" /* FAILED */;
|
|
4324
|
+
onStatusChange?.(this.status);
|
|
4325
|
+
reject(new SignatureRejectedError(result.error));
|
|
4326
|
+
}
|
|
4327
|
+
} else if (result) {
|
|
4328
|
+
if (this.status === "waiting" /* WAITING */) {
|
|
4329
|
+
this.status = "pending" /* PENDING */;
|
|
4330
|
+
onStatusChange?.(this.status);
|
|
4331
|
+
}
|
|
4332
|
+
this.pollTimer = setTimeout(poll, this.config.pollInterval);
|
|
4333
|
+
} else {
|
|
4334
|
+
this.pollTimer = setTimeout(poll, this.config.pollInterval);
|
|
4335
|
+
}
|
|
4336
|
+
} catch (error) {
|
|
4337
|
+
this.stopPolling();
|
|
4338
|
+
this.status = "failed" /* FAILED */;
|
|
4339
|
+
onStatusChange?.(this.status);
|
|
4340
|
+
reject(error);
|
|
4341
|
+
}
|
|
4342
|
+
};
|
|
4343
|
+
poll();
|
|
4344
|
+
});
|
|
4345
|
+
}
|
|
4346
|
+
/**
|
|
4347
|
+
* 默认 HTTP 轮询函数
|
|
4348
|
+
*/
|
|
4349
|
+
async defaultPoll(requestId) {
|
|
4350
|
+
if (!this.config.pollUrl) {
|
|
4351
|
+
return null;
|
|
4352
|
+
}
|
|
4353
|
+
try {
|
|
4354
|
+
const url = `${this.config.pollUrl}?requestId=${encodeURIComponent(requestId)}`;
|
|
4355
|
+
const response = await fetch(url, {
|
|
4356
|
+
method: "GET",
|
|
4357
|
+
headers: {
|
|
4358
|
+
"Content-Type": "application/json"
|
|
4359
|
+
}
|
|
4360
|
+
});
|
|
4361
|
+
if (!response.ok) {
|
|
4362
|
+
if (response.status === 404) {
|
|
4363
|
+
return null;
|
|
4364
|
+
}
|
|
4365
|
+
throw new NetworkError(`Poll request failed: ${response.statusText}`);
|
|
4366
|
+
}
|
|
4367
|
+
const data = await response.json();
|
|
4368
|
+
return {
|
|
4369
|
+
completed: data.completed === true,
|
|
4370
|
+
signature: data.signature,
|
|
4371
|
+
error: data.error,
|
|
4372
|
+
signer: data.signer
|
|
4373
|
+
};
|
|
4374
|
+
} catch (error) {
|
|
4375
|
+
if (error instanceof NetworkError) {
|
|
4376
|
+
throw error;
|
|
4377
|
+
}
|
|
4378
|
+
return null;
|
|
4379
|
+
}
|
|
4380
|
+
}
|
|
4381
|
+
/**
|
|
4382
|
+
* 停止轮询
|
|
4383
|
+
*/
|
|
4384
|
+
stopPolling() {
|
|
4385
|
+
if (this.pollTimer) {
|
|
4386
|
+
clearTimeout(this.pollTimer);
|
|
4387
|
+
this.pollTimer = null;
|
|
4388
|
+
}
|
|
4389
|
+
if (this.timeoutTimer) {
|
|
4390
|
+
clearTimeout(this.timeoutTimer);
|
|
4391
|
+
this.timeoutTimer = null;
|
|
4392
|
+
}
|
|
4393
|
+
}
|
|
4394
|
+
/**
|
|
4395
|
+
* 取消签名请求
|
|
4396
|
+
*/
|
|
4397
|
+
cancel() {
|
|
4398
|
+
this.stopPolling();
|
|
4399
|
+
this.status = "cancelled" /* CANCELLED */;
|
|
4400
|
+
}
|
|
4401
|
+
/**
|
|
4402
|
+
* 获取当前状态
|
|
4403
|
+
*/
|
|
4404
|
+
getStatus() {
|
|
4405
|
+
return this.status;
|
|
4406
|
+
}
|
|
4407
|
+
/**
|
|
4408
|
+
* 获取二维码 URL
|
|
4409
|
+
*/
|
|
4410
|
+
getQRCodeUrl() {
|
|
4411
|
+
return this.config.requestUrl;
|
|
4412
|
+
}
|
|
4413
|
+
/**
|
|
4414
|
+
* 获取结果
|
|
4415
|
+
*/
|
|
4416
|
+
getResult() {
|
|
4417
|
+
return this.result;
|
|
4418
|
+
}
|
|
4419
|
+
/**
|
|
4420
|
+
* 清理资源
|
|
4421
|
+
*/
|
|
4422
|
+
cleanup() {
|
|
4423
|
+
this.stopPolling();
|
|
4424
|
+
this.qrCodeDataUrl = null;
|
|
4425
|
+
this.result = null;
|
|
4426
|
+
this.status = "waiting" /* WAITING */;
|
|
1595
4427
|
}
|
|
1596
4428
|
};
|
|
1597
4429
|
|
|
@@ -1610,9 +4442,15 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1610
4442
|
defaultTronChainId: config.defaultTronChainId ?? 195,
|
|
1611
4443
|
walletConnectProjectId: config.walletConnectProjectId ?? ""
|
|
1612
4444
|
};
|
|
1613
|
-
this.registry = new AdapterRegistry();
|
|
4445
|
+
this.registry = new AdapterRegistry(this.config);
|
|
1614
4446
|
}
|
|
1615
4447
|
// ===== Connection Management =====
|
|
4448
|
+
/**
|
|
4449
|
+
* Check if adapter is registered for a wallet type
|
|
4450
|
+
*/
|
|
4451
|
+
hasAdapter(type) {
|
|
4452
|
+
return this.registry.has(type);
|
|
4453
|
+
}
|
|
1616
4454
|
/**
|
|
1617
4455
|
* Connect primary wallet
|
|
1618
4456
|
*/
|
|
@@ -1679,7 +4517,7 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1679
4517
|
this.connectedWallets.delete(chainType);
|
|
1680
4518
|
this.primaryWallet = null;
|
|
1681
4519
|
if (this.config.enableStorage) {
|
|
1682
|
-
this.
|
|
4520
|
+
this.clearStorage();
|
|
1683
4521
|
}
|
|
1684
4522
|
this.emit("disconnected");
|
|
1685
4523
|
}
|
|
@@ -1768,6 +4606,35 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
1768
4606
|
}
|
|
1769
4607
|
return adapter.signMessage(message);
|
|
1770
4608
|
}
|
|
4609
|
+
/**
|
|
4610
|
+
* Create QR code signer for message signing
|
|
4611
|
+
*
|
|
4612
|
+
* This method creates a QR code signer that can be used to display a QR code
|
|
4613
|
+
* for users to scan with their wallet app to sign a message.
|
|
4614
|
+
*
|
|
4615
|
+
* @param message - Message to sign
|
|
4616
|
+
* @param config - QR code signer configuration
|
|
4617
|
+
* @returns QRCodeSigner instance
|
|
4618
|
+
*
|
|
4619
|
+
* @example
|
|
4620
|
+
* ```typescript
|
|
4621
|
+
* const signer = walletManager.createQRCodeSigner('Hello World', {
|
|
4622
|
+
* requestId: 'sign-123',
|
|
4623
|
+
* requestUrl: 'https://example.com/sign?requestId=sign-123&message=Hello%20World',
|
|
4624
|
+
* pollUrl: 'https://api.example.com/sign/status',
|
|
4625
|
+
* })
|
|
4626
|
+
*
|
|
4627
|
+
* const qrCodeUrl = await signer.generateQRCode()
|
|
4628
|
+
* const signature = await signer.startPolling()
|
|
4629
|
+
* ```
|
|
4630
|
+
*/
|
|
4631
|
+
createQRCodeSigner(message, config) {
|
|
4632
|
+
return new QRCodeSigner({
|
|
4633
|
+
...config,
|
|
4634
|
+
// Encode message in request URL if not already encoded
|
|
4635
|
+
requestUrl: config.requestUrl.includes(encodeURIComponent(message)) ? config.requestUrl : `${config.requestUrl}${config.requestUrl.includes("?") ? "&" : "?"}message=${encodeURIComponent(message)}`
|
|
4636
|
+
});
|
|
4637
|
+
}
|
|
1771
4638
|
/**
|
|
1772
4639
|
* Sign TypedData (EVM only)
|
|
1773
4640
|
*/
|
|
@@ -2078,6 +4945,29 @@ var WalletManager = class extends TypedEventEmitter {
|
|
|
2078
4945
|
console.debug("Silent TronLink connection failed:", silentError);
|
|
2079
4946
|
}
|
|
2080
4947
|
}
|
|
4948
|
+
if (data.primaryWalletType === "walletconnect-tron" /* WALLETCONNECT_TRON */) {
|
|
4949
|
+
try {
|
|
4950
|
+
const wcAdapter = adapter;
|
|
4951
|
+
if (typeof wcAdapter.restoreSession === "function") {
|
|
4952
|
+
console.debug("[WalletManager] Attempting to restore WalletConnect Tron session...");
|
|
4953
|
+
const account2 = await wcAdapter.restoreSession(data.primaryChainId);
|
|
4954
|
+
if (account2) {
|
|
4955
|
+
console.debug("[WalletManager] WalletConnect Tron session restored successfully");
|
|
4956
|
+
this.setPrimaryWallet(adapter);
|
|
4957
|
+
this.connectedWallets.set(adapter.chainType, adapter);
|
|
4958
|
+
this.setupAdapterListeners(adapter, true);
|
|
4959
|
+
this.emit("accountChanged", account2);
|
|
4960
|
+
return account2;
|
|
4961
|
+
} else {
|
|
4962
|
+
console.debug("[WalletManager] No valid WalletConnect Tron session found");
|
|
4963
|
+
return null;
|
|
4964
|
+
}
|
|
4965
|
+
}
|
|
4966
|
+
} catch (restoreError) {
|
|
4967
|
+
console.debug("[WalletManager] WalletConnect Tron restore failed:", restoreError);
|
|
4968
|
+
return null;
|
|
4969
|
+
}
|
|
4970
|
+
}
|
|
2081
4971
|
const account = await adapter.connect(data.primaryChainId);
|
|
2082
4972
|
this.setPrimaryWallet(adapter);
|
|
2083
4973
|
this.connectedWallets.set(adapter.chainType, adapter);
|
|
@@ -2226,7 +5116,7 @@ function WalletProvider({ children, walletManager: externalWalletManager }) {
|
|
|
2226
5116
|
signMessage,
|
|
2227
5117
|
signTransaction
|
|
2228
5118
|
};
|
|
2229
|
-
return /* @__PURE__ */
|
|
5119
|
+
return /* @__PURE__ */ React2.createElement(WalletContext.Provider, { value }, children);
|
|
2230
5120
|
}
|
|
2231
5121
|
function useWallet() {
|
|
2232
5122
|
const context = useContext(WalletContext);
|
|
@@ -2357,7 +5247,375 @@ function useSignTransaction() {
|
|
|
2357
5247
|
error
|
|
2358
5248
|
};
|
|
2359
5249
|
}
|
|
5250
|
+
function useQRCodeSigner(config) {
|
|
5251
|
+
const [qrCodeDataUrl, setQrCodeDataUrl] = useState(null);
|
|
5252
|
+
const [status, setStatus] = useState("waiting" /* WAITING */);
|
|
5253
|
+
const [result, setResult] = useState(null);
|
|
5254
|
+
const [isPolling, setIsPolling] = useState(false);
|
|
5255
|
+
const [error, setError] = useState(null);
|
|
5256
|
+
const signerRef = useRef(null);
|
|
5257
|
+
useEffect(() => {
|
|
5258
|
+
signerRef.current = new QRCodeSigner(config);
|
|
5259
|
+
return () => {
|
|
5260
|
+
signerRef.current?.cleanup();
|
|
5261
|
+
};
|
|
5262
|
+
}, [config.requestId, config.requestUrl, config.pollUrl]);
|
|
5263
|
+
const generateQRCode = useCallback(async () => {
|
|
5264
|
+
if (!signerRef.current) return;
|
|
5265
|
+
try {
|
|
5266
|
+
const dataUrl = await signerRef.current.generateQRCode();
|
|
5267
|
+
setQrCodeDataUrl(dataUrl);
|
|
5268
|
+
} catch (err) {
|
|
5269
|
+
const error2 = err instanceof Error ? err : new Error(String(err));
|
|
5270
|
+
setError(error2);
|
|
5271
|
+
}
|
|
5272
|
+
}, []);
|
|
5273
|
+
const startSign = useCallback(async () => {
|
|
5274
|
+
if (!signerRef.current) {
|
|
5275
|
+
throw new Error("QRCodeSigner not initialized");
|
|
5276
|
+
}
|
|
5277
|
+
setError(null);
|
|
5278
|
+
setStatus("waiting" /* WAITING */);
|
|
5279
|
+
setIsPolling(true);
|
|
5280
|
+
try {
|
|
5281
|
+
await generateQRCode();
|
|
5282
|
+
const signature = await signerRef.current.startPolling(
|
|
5283
|
+
(newStatus) => {
|
|
5284
|
+
setStatus(newStatus);
|
|
5285
|
+
},
|
|
5286
|
+
(signResult) => {
|
|
5287
|
+
setResult(signResult);
|
|
5288
|
+
}
|
|
5289
|
+
);
|
|
5290
|
+
setIsPolling(false);
|
|
5291
|
+
return signature;
|
|
5292
|
+
} catch (err) {
|
|
5293
|
+
setIsPolling(false);
|
|
5294
|
+
const error2 = err instanceof Error ? err : new SignatureRejectedError(err instanceof Error ? err.message : String(err));
|
|
5295
|
+
setError(error2);
|
|
5296
|
+
throw error2;
|
|
5297
|
+
}
|
|
5298
|
+
}, [generateQRCode]);
|
|
5299
|
+
const stopPolling = useCallback(() => {
|
|
5300
|
+
signerRef.current?.stopPolling();
|
|
5301
|
+
setIsPolling(false);
|
|
5302
|
+
}, []);
|
|
5303
|
+
const cancel = useCallback(() => {
|
|
5304
|
+
signerRef.current?.cancel();
|
|
5305
|
+
setStatus("cancelled" /* CANCELLED */);
|
|
5306
|
+
setIsPolling(false);
|
|
5307
|
+
}, []);
|
|
5308
|
+
return {
|
|
5309
|
+
qrCodeDataUrl,
|
|
5310
|
+
status,
|
|
5311
|
+
result,
|
|
5312
|
+
isPolling,
|
|
5313
|
+
startSign,
|
|
5314
|
+
stopPolling,
|
|
5315
|
+
cancel,
|
|
5316
|
+
error
|
|
5317
|
+
};
|
|
5318
|
+
}
|
|
5319
|
+
function QRCodeModal({
|
|
5320
|
+
isOpen,
|
|
5321
|
+
onClose,
|
|
5322
|
+
qrCodeDataUrl,
|
|
5323
|
+
status,
|
|
5324
|
+
error,
|
|
5325
|
+
title = "\u626B\u7801\u7B7E\u540D",
|
|
5326
|
+
description
|
|
5327
|
+
}) {
|
|
5328
|
+
useEffect(() => {
|
|
5329
|
+
if (isOpen) {
|
|
5330
|
+
document.body.style.overflow = "hidden";
|
|
5331
|
+
} else {
|
|
5332
|
+
document.body.style.overflow = "";
|
|
5333
|
+
}
|
|
5334
|
+
return () => {
|
|
5335
|
+
document.body.style.overflow = "";
|
|
5336
|
+
};
|
|
5337
|
+
}, [isOpen]);
|
|
5338
|
+
if (!isOpen) {
|
|
5339
|
+
return null;
|
|
5340
|
+
}
|
|
5341
|
+
const getStatusText = () => {
|
|
5342
|
+
switch (status) {
|
|
5343
|
+
case "waiting" /* WAITING */:
|
|
5344
|
+
return "\u8BF7\u4F7F\u7528\u94B1\u5305\u626B\u63CF\u4E8C\u7EF4\u7801";
|
|
5345
|
+
case "pending" /* PENDING */:
|
|
5346
|
+
return "\u5DF2\u626B\u63CF\uFF0C\u8BF7\u5728\u94B1\u5305\u4E2D\u786E\u8BA4\u7B7E\u540D";
|
|
5347
|
+
case "success" /* SUCCESS */:
|
|
5348
|
+
return "\u7B7E\u540D\u6210\u529F";
|
|
5349
|
+
case "failed" /* FAILED */:
|
|
5350
|
+
return "\u7B7E\u540D\u5931\u8D25";
|
|
5351
|
+
case "timeout" /* TIMEOUT */:
|
|
5352
|
+
return "\u7B7E\u540D\u8D85\u65F6";
|
|
5353
|
+
case "cancelled" /* CANCELLED */:
|
|
5354
|
+
return "\u5DF2\u53D6\u6D88";
|
|
5355
|
+
default:
|
|
5356
|
+
return "\u7B49\u5F85\u626B\u63CF";
|
|
5357
|
+
}
|
|
5358
|
+
};
|
|
5359
|
+
const getStatusColor = () => {
|
|
5360
|
+
switch (status) {
|
|
5361
|
+
case "waiting" /* WAITING */:
|
|
5362
|
+
case "pending" /* PENDING */:
|
|
5363
|
+
return "#3b82f6";
|
|
5364
|
+
// blue
|
|
5365
|
+
case "success" /* SUCCESS */:
|
|
5366
|
+
return "#10b981";
|
|
5367
|
+
// green
|
|
5368
|
+
case "failed" /* FAILED */:
|
|
5369
|
+
case "timeout" /* TIMEOUT */:
|
|
5370
|
+
case "cancelled" /* CANCELLED */:
|
|
5371
|
+
return "#ef4444";
|
|
5372
|
+
// red
|
|
5373
|
+
default:
|
|
5374
|
+
return "#6b7280";
|
|
5375
|
+
}
|
|
5376
|
+
};
|
|
5377
|
+
return /* @__PURE__ */ React2.createElement(
|
|
5378
|
+
"div",
|
|
5379
|
+
{
|
|
5380
|
+
style: {
|
|
5381
|
+
position: "fixed",
|
|
5382
|
+
top: 0,
|
|
5383
|
+
left: 0,
|
|
5384
|
+
right: 0,
|
|
5385
|
+
bottom: 0,
|
|
5386
|
+
backgroundColor: "rgba(0, 0, 0, 0.5)",
|
|
5387
|
+
display: "flex",
|
|
5388
|
+
alignItems: "center",
|
|
5389
|
+
justifyContent: "center",
|
|
5390
|
+
zIndex: 1e4,
|
|
5391
|
+
padding: "20px"
|
|
5392
|
+
},
|
|
5393
|
+
onClick: (e) => {
|
|
5394
|
+
if (e.target === e.currentTarget) {
|
|
5395
|
+
onClose();
|
|
5396
|
+
}
|
|
5397
|
+
}
|
|
5398
|
+
},
|
|
5399
|
+
/* @__PURE__ */ React2.createElement(
|
|
5400
|
+
"div",
|
|
5401
|
+
{
|
|
5402
|
+
style: {
|
|
5403
|
+
backgroundColor: "#ffffff",
|
|
5404
|
+
borderRadius: "12px",
|
|
5405
|
+
padding: "24px",
|
|
5406
|
+
maxWidth: "400px",
|
|
5407
|
+
width: "100%",
|
|
5408
|
+
boxShadow: "0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04)"
|
|
5409
|
+
}
|
|
5410
|
+
},
|
|
5411
|
+
/* @__PURE__ */ React2.createElement(
|
|
5412
|
+
"div",
|
|
5413
|
+
{
|
|
5414
|
+
style: {
|
|
5415
|
+
display: "flex",
|
|
5416
|
+
justifyContent: "space-between",
|
|
5417
|
+
alignItems: "center",
|
|
5418
|
+
marginBottom: "20px"
|
|
5419
|
+
}
|
|
5420
|
+
},
|
|
5421
|
+
/* @__PURE__ */ React2.createElement(
|
|
5422
|
+
"h2",
|
|
5423
|
+
{
|
|
5424
|
+
style: {
|
|
5425
|
+
margin: 0,
|
|
5426
|
+
fontSize: "20px",
|
|
5427
|
+
fontWeight: 600,
|
|
5428
|
+
color: "#111827"
|
|
5429
|
+
}
|
|
5430
|
+
},
|
|
5431
|
+
title
|
|
5432
|
+
),
|
|
5433
|
+
/* @__PURE__ */ React2.createElement(
|
|
5434
|
+
"button",
|
|
5435
|
+
{
|
|
5436
|
+
onClick: onClose,
|
|
5437
|
+
style: {
|
|
5438
|
+
background: "none",
|
|
5439
|
+
border: "none",
|
|
5440
|
+
fontSize: "24px",
|
|
5441
|
+
cursor: "pointer",
|
|
5442
|
+
color: "#6b7280",
|
|
5443
|
+
padding: 0,
|
|
5444
|
+
width: "24px",
|
|
5445
|
+
height: "24px",
|
|
5446
|
+
display: "flex",
|
|
5447
|
+
alignItems: "center",
|
|
5448
|
+
justifyContent: "center"
|
|
5449
|
+
}
|
|
5450
|
+
},
|
|
5451
|
+
"\xD7"
|
|
5452
|
+
)
|
|
5453
|
+
),
|
|
5454
|
+
description && /* @__PURE__ */ React2.createElement(
|
|
5455
|
+
"p",
|
|
5456
|
+
{
|
|
5457
|
+
style: {
|
|
5458
|
+
margin: "0 0 20px 0",
|
|
5459
|
+
fontSize: "14px",
|
|
5460
|
+
color: "#6b7280"
|
|
5461
|
+
}
|
|
5462
|
+
},
|
|
5463
|
+
description
|
|
5464
|
+
),
|
|
5465
|
+
/* @__PURE__ */ React2.createElement(
|
|
5466
|
+
"div",
|
|
5467
|
+
{
|
|
5468
|
+
style: {
|
|
5469
|
+
display: "flex",
|
|
5470
|
+
justifyContent: "center",
|
|
5471
|
+
marginBottom: "20px"
|
|
5472
|
+
}
|
|
5473
|
+
},
|
|
5474
|
+
qrCodeDataUrl ? /* @__PURE__ */ React2.createElement(
|
|
5475
|
+
"img",
|
|
5476
|
+
{
|
|
5477
|
+
src: qrCodeDataUrl,
|
|
5478
|
+
alt: "QR Code",
|
|
5479
|
+
style: {
|
|
5480
|
+
width: "100%",
|
|
5481
|
+
maxWidth: "300px",
|
|
5482
|
+
height: "auto",
|
|
5483
|
+
border: "1px solid #e5e7eb",
|
|
5484
|
+
borderRadius: "8px"
|
|
5485
|
+
}
|
|
5486
|
+
}
|
|
5487
|
+
) : /* @__PURE__ */ React2.createElement(
|
|
5488
|
+
"div",
|
|
5489
|
+
{
|
|
5490
|
+
style: {
|
|
5491
|
+
width: "300px",
|
|
5492
|
+
height: "300px",
|
|
5493
|
+
backgroundColor: "#f3f4f6",
|
|
5494
|
+
display: "flex",
|
|
5495
|
+
alignItems: "center",
|
|
5496
|
+
justifyContent: "center",
|
|
5497
|
+
borderRadius: "8px",
|
|
5498
|
+
color: "#6b7280"
|
|
5499
|
+
}
|
|
5500
|
+
},
|
|
5501
|
+
"\u751F\u6210\u4E8C\u7EF4\u7801\u4E2D..."
|
|
5502
|
+
)
|
|
5503
|
+
),
|
|
5504
|
+
/* @__PURE__ */ React2.createElement(
|
|
5505
|
+
"div",
|
|
5506
|
+
{
|
|
5507
|
+
style: {
|
|
5508
|
+
textAlign: "center",
|
|
5509
|
+
marginBottom: "20px"
|
|
5510
|
+
}
|
|
5511
|
+
},
|
|
5512
|
+
/* @__PURE__ */ React2.createElement(
|
|
5513
|
+
"div",
|
|
5514
|
+
{
|
|
5515
|
+
style: {
|
|
5516
|
+
display: "inline-flex",
|
|
5517
|
+
alignItems: "center",
|
|
5518
|
+
gap: "8px",
|
|
5519
|
+
padding: "8px 16px",
|
|
5520
|
+
borderRadius: "6px",
|
|
5521
|
+
backgroundColor: `${getStatusColor()}15`,
|
|
5522
|
+
color: getStatusColor(),
|
|
5523
|
+
fontSize: "14px",
|
|
5524
|
+
fontWeight: 500
|
|
5525
|
+
}
|
|
5526
|
+
},
|
|
5527
|
+
status === "waiting" /* WAITING */ && /* @__PURE__ */ React2.createElement(
|
|
5528
|
+
"div",
|
|
5529
|
+
{
|
|
5530
|
+
style: {
|
|
5531
|
+
width: "8px",
|
|
5532
|
+
height: "8px",
|
|
5533
|
+
borderRadius: "50%",
|
|
5534
|
+
backgroundColor: getStatusColor(),
|
|
5535
|
+
animation: "pulse 2s cubic-bezier(0.4, 0, 0.6, 1) infinite"
|
|
5536
|
+
}
|
|
5537
|
+
}
|
|
5538
|
+
),
|
|
5539
|
+
status === "pending" /* PENDING */ && /* @__PURE__ */ React2.createElement(
|
|
5540
|
+
"div",
|
|
5541
|
+
{
|
|
5542
|
+
style: {
|
|
5543
|
+
width: "8px",
|
|
5544
|
+
height: "8px",
|
|
5545
|
+
borderRadius: "50%",
|
|
5546
|
+
backgroundColor: getStatusColor()
|
|
5547
|
+
}
|
|
5548
|
+
}
|
|
5549
|
+
),
|
|
5550
|
+
status === "success" /* SUCCESS */ && "\u2713",
|
|
5551
|
+
status === "failed" /* FAILED */ && "\u2715",
|
|
5552
|
+
getStatusText()
|
|
5553
|
+
)
|
|
5554
|
+
),
|
|
5555
|
+
error && /* @__PURE__ */ React2.createElement(
|
|
5556
|
+
"div",
|
|
5557
|
+
{
|
|
5558
|
+
style: {
|
|
5559
|
+
padding: "12px",
|
|
5560
|
+
backgroundColor: "#fef2f2",
|
|
5561
|
+
border: "1px solid #fecaca",
|
|
5562
|
+
borderRadius: "6px",
|
|
5563
|
+
marginBottom: "20px"
|
|
5564
|
+
}
|
|
5565
|
+
},
|
|
5566
|
+
/* @__PURE__ */ React2.createElement(
|
|
5567
|
+
"p",
|
|
5568
|
+
{
|
|
5569
|
+
style: {
|
|
5570
|
+
margin: 0,
|
|
5571
|
+
fontSize: "14px",
|
|
5572
|
+
color: "#dc2626"
|
|
5573
|
+
}
|
|
5574
|
+
},
|
|
5575
|
+
error.message
|
|
5576
|
+
)
|
|
5577
|
+
),
|
|
5578
|
+
/* @__PURE__ */ React2.createElement(
|
|
5579
|
+
"div",
|
|
5580
|
+
{
|
|
5581
|
+
style: {
|
|
5582
|
+
display: "flex",
|
|
5583
|
+
gap: "12px"
|
|
5584
|
+
}
|
|
5585
|
+
},
|
|
5586
|
+
/* @__PURE__ */ React2.createElement(
|
|
5587
|
+
"button",
|
|
5588
|
+
{
|
|
5589
|
+
onClick: onClose,
|
|
5590
|
+
style: {
|
|
5591
|
+
flex: 1,
|
|
5592
|
+
padding: "10px 20px",
|
|
5593
|
+
border: "1px solid #d1d5db",
|
|
5594
|
+
borderRadius: "6px",
|
|
5595
|
+
backgroundColor: "#ffffff",
|
|
5596
|
+
color: "#374151",
|
|
5597
|
+
fontSize: "14px",
|
|
5598
|
+
fontWeight: 500,
|
|
5599
|
+
cursor: "pointer"
|
|
5600
|
+
}
|
|
5601
|
+
},
|
|
5602
|
+
status === "success" /* SUCCESS */ ? "\u5173\u95ED" : "\u53D6\u6D88"
|
|
5603
|
+
)
|
|
5604
|
+
)
|
|
5605
|
+
),
|
|
5606
|
+
/* @__PURE__ */ React2.createElement("style", null, `
|
|
5607
|
+
@keyframes pulse {
|
|
5608
|
+
0%, 100% {
|
|
5609
|
+
opacity: 1;
|
|
5610
|
+
}
|
|
5611
|
+
50% {
|
|
5612
|
+
opacity: 0.5;
|
|
5613
|
+
}
|
|
5614
|
+
}
|
|
5615
|
+
`)
|
|
5616
|
+
);
|
|
5617
|
+
}
|
|
2360
5618
|
|
|
2361
|
-
export { WalletProvider, useAccount, useConnect, useDisconnect, useSignMessage, useSignTransaction, useWallet };
|
|
5619
|
+
export { QRCodeModal, WalletProvider, useAccount, useConnect, useDisconnect, useQRCodeSigner, useSignMessage, useSignTransaction, useWallet };
|
|
2362
5620
|
//# sourceMappingURL=index.mjs.map
|
|
2363
5621
|
//# sourceMappingURL=index.mjs.map
|