@tronweb3/tronwallet-adapter-trust 1.0.0 → 1.0.2
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/lib/cjs/adapter.js +15 -6
- package/lib/cjs/adapter.js.map +1 -1
- package/lib/cjs/utils.js +1 -1
- package/lib/cjs/utils.js.map +1 -1
- package/lib/esm/adapter.js +15 -6
- package/lib/esm/adapter.js.map +1 -1
- package/lib/esm/utils.js +1 -1
- package/lib/esm/utils.js.map +1 -1
- package/lib/types/adapter.d.ts.map +1 -1
- package/lib/umd/index.js +1738 -1635
- package/lib/umd/index.min.js +1 -1
- package/package.json +1 -1
- package/src/adapter.ts +12 -6
- package/src/utils.ts +1 -1
package/lib/umd/index.js
CHANGED
|
@@ -4,8 +4,6 @@
|
|
|
4
4
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global["@tronweb3/tronwallet-adapter-trust"] = factory());
|
|
5
5
|
})(this, (function () { 'use strict';
|
|
6
6
|
|
|
7
|
-
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
8
|
-
|
|
9
7
|
function getDefaultExportFromCjs (x) {
|
|
10
8
|
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
11
9
|
}
|
|
@@ -20,639 +18,683 @@
|
|
|
20
18
|
|
|
21
19
|
var eventemitter3 = {exports: {}};
|
|
22
20
|
|
|
23
|
-
|
|
21
|
+
var hasRequiredEventemitter3;
|
|
22
|
+
|
|
23
|
+
function requireEventemitter3 () {
|
|
24
|
+
if (hasRequiredEventemitter3) return eventemitter3.exports;
|
|
25
|
+
hasRequiredEventemitter3 = 1;
|
|
26
|
+
(function (module) {
|
|
27
|
+
|
|
28
|
+
var has = Object.prototype.hasOwnProperty
|
|
29
|
+
, prefix = '~';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Constructor to create a storage for our `EE` objects.
|
|
33
|
+
* An `Events` instance is a plain object whose properties are event names.
|
|
34
|
+
*
|
|
35
|
+
* @constructor
|
|
36
|
+
* @private
|
|
37
|
+
*/
|
|
38
|
+
function Events() {}
|
|
39
|
+
|
|
40
|
+
//
|
|
41
|
+
// We try to not inherit from `Object.prototype`. In some engines creating an
|
|
42
|
+
// instance in this way is faster than calling `Object.create(null)` directly.
|
|
43
|
+
// If `Object.create(null)` is not supported we prefix the event names with a
|
|
44
|
+
// character to make sure that the built-in object properties are not
|
|
45
|
+
// overridden or used as an attack vector.
|
|
46
|
+
//
|
|
47
|
+
if (Object.create) {
|
|
48
|
+
Events.prototype = Object.create(null);
|
|
49
|
+
|
|
50
|
+
//
|
|
51
|
+
// This hack is needed because the `__proto__` property is still inherited in
|
|
52
|
+
// some old browsers like Android 4, iPhone 5.1, Opera 11 and Safari 5.
|
|
53
|
+
//
|
|
54
|
+
if (!new Events().__proto__) prefix = false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Representation of a single event listener.
|
|
59
|
+
*
|
|
60
|
+
* @param {Function} fn The listener function.
|
|
61
|
+
* @param {*} context The context to invoke the listener with.
|
|
62
|
+
* @param {Boolean} [once=false] Specify if the listener is a one-time listener.
|
|
63
|
+
* @constructor
|
|
64
|
+
* @private
|
|
65
|
+
*/
|
|
66
|
+
function EE(fn, context, once) {
|
|
67
|
+
this.fn = fn;
|
|
68
|
+
this.context = context;
|
|
69
|
+
this.once = once || false;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Add a listener for a given event.
|
|
74
|
+
*
|
|
75
|
+
* @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
|
|
76
|
+
* @param {(String|Symbol)} event The event name.
|
|
77
|
+
* @param {Function} fn The listener function.
|
|
78
|
+
* @param {*} context The context to invoke the listener with.
|
|
79
|
+
* @param {Boolean} once Specify if the listener is a one-time listener.
|
|
80
|
+
* @returns {EventEmitter}
|
|
81
|
+
* @private
|
|
82
|
+
*/
|
|
83
|
+
function addListener(emitter, event, fn, context, once) {
|
|
84
|
+
if (typeof fn !== 'function') {
|
|
85
|
+
throw new TypeError('The listener must be a function');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
var listener = new EE(fn, context || emitter, once)
|
|
89
|
+
, evt = prefix ? prefix + event : event;
|
|
90
|
+
|
|
91
|
+
if (!emitter._events[evt]) emitter._events[evt] = listener, emitter._eventsCount++;
|
|
92
|
+
else if (!emitter._events[evt].fn) emitter._events[evt].push(listener);
|
|
93
|
+
else emitter._events[evt] = [emitter._events[evt], listener];
|
|
94
|
+
|
|
95
|
+
return emitter;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Clear event by name.
|
|
100
|
+
*
|
|
101
|
+
* @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
|
|
102
|
+
* @param {(String|Symbol)} evt The Event name.
|
|
103
|
+
* @private
|
|
104
|
+
*/
|
|
105
|
+
function clearEvent(emitter, evt) {
|
|
106
|
+
if (--emitter._eventsCount === 0) emitter._events = new Events();
|
|
107
|
+
else delete emitter._events[evt];
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Minimal `EventEmitter` interface that is molded against the Node.js
|
|
112
|
+
* `EventEmitter` interface.
|
|
113
|
+
*
|
|
114
|
+
* @constructor
|
|
115
|
+
* @public
|
|
116
|
+
*/
|
|
117
|
+
function EventEmitter() {
|
|
118
|
+
this._events = new Events();
|
|
119
|
+
this._eventsCount = 0;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Return an array listing the events for which the emitter has registered
|
|
124
|
+
* listeners.
|
|
125
|
+
*
|
|
126
|
+
* @returns {Array}
|
|
127
|
+
* @public
|
|
128
|
+
*/
|
|
129
|
+
EventEmitter.prototype.eventNames = function eventNames() {
|
|
130
|
+
var names = []
|
|
131
|
+
, events
|
|
132
|
+
, name;
|
|
133
|
+
|
|
134
|
+
if (this._eventsCount === 0) return names;
|
|
135
|
+
|
|
136
|
+
for (name in (events = this._events)) {
|
|
137
|
+
if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (Object.getOwnPropertySymbols) {
|
|
141
|
+
return names.concat(Object.getOwnPropertySymbols(events));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return names;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Return the listeners registered for a given event.
|
|
149
|
+
*
|
|
150
|
+
* @param {(String|Symbol)} event The event name.
|
|
151
|
+
* @returns {Array} The registered listeners.
|
|
152
|
+
* @public
|
|
153
|
+
*/
|
|
154
|
+
EventEmitter.prototype.listeners = function listeners(event) {
|
|
155
|
+
var evt = prefix ? prefix + event : event
|
|
156
|
+
, handlers = this._events[evt];
|
|
157
|
+
|
|
158
|
+
if (!handlers) return [];
|
|
159
|
+
if (handlers.fn) return [handlers.fn];
|
|
160
|
+
|
|
161
|
+
for (var i = 0, l = handlers.length, ee = new Array(l); i < l; i++) {
|
|
162
|
+
ee[i] = handlers[i].fn;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return ee;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Return the number of listeners listening to a given event.
|
|
170
|
+
*
|
|
171
|
+
* @param {(String|Symbol)} event The event name.
|
|
172
|
+
* @returns {Number} The number of listeners.
|
|
173
|
+
* @public
|
|
174
|
+
*/
|
|
175
|
+
EventEmitter.prototype.listenerCount = function listenerCount(event) {
|
|
176
|
+
var evt = prefix ? prefix + event : event
|
|
177
|
+
, listeners = this._events[evt];
|
|
178
|
+
|
|
179
|
+
if (!listeners) return 0;
|
|
180
|
+
if (listeners.fn) return 1;
|
|
181
|
+
return listeners.length;
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Calls each of the listeners registered for a given event.
|
|
186
|
+
*
|
|
187
|
+
* @param {(String|Symbol)} event The event name.
|
|
188
|
+
* @returns {Boolean} `true` if the event had listeners, else `false`.
|
|
189
|
+
* @public
|
|
190
|
+
*/
|
|
191
|
+
EventEmitter.prototype.emit = function emit(event, a1, a2, a3, a4, a5) {
|
|
192
|
+
var evt = prefix ? prefix + event : event;
|
|
193
|
+
|
|
194
|
+
if (!this._events[evt]) return false;
|
|
195
|
+
|
|
196
|
+
var listeners = this._events[evt]
|
|
197
|
+
, len = arguments.length
|
|
198
|
+
, args
|
|
199
|
+
, i;
|
|
200
|
+
|
|
201
|
+
if (listeners.fn) {
|
|
202
|
+
if (listeners.once) this.removeListener(event, listeners.fn, undefined, true);
|
|
203
|
+
|
|
204
|
+
switch (len) {
|
|
205
|
+
case 1: return listeners.fn.call(listeners.context), true;
|
|
206
|
+
case 2: return listeners.fn.call(listeners.context, a1), true;
|
|
207
|
+
case 3: return listeners.fn.call(listeners.context, a1, a2), true;
|
|
208
|
+
case 4: return listeners.fn.call(listeners.context, a1, a2, a3), true;
|
|
209
|
+
case 5: return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
|
|
210
|
+
case 6: return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
for (i = 1, args = new Array(len -1); i < len; i++) {
|
|
214
|
+
args[i - 1] = arguments[i];
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
listeners.fn.apply(listeners.context, args);
|
|
218
|
+
} else {
|
|
219
|
+
var length = listeners.length
|
|
220
|
+
, j;
|
|
221
|
+
|
|
222
|
+
for (i = 0; i < length; i++) {
|
|
223
|
+
if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
|
|
224
|
+
|
|
225
|
+
switch (len) {
|
|
226
|
+
case 1: listeners[i].fn.call(listeners[i].context); break;
|
|
227
|
+
case 2: listeners[i].fn.call(listeners[i].context, a1); break;
|
|
228
|
+
case 3: listeners[i].fn.call(listeners[i].context, a1, a2); break;
|
|
229
|
+
case 4: listeners[i].fn.call(listeners[i].context, a1, a2, a3); break;
|
|
230
|
+
default:
|
|
231
|
+
if (!args) for (j = 1, args = new Array(len -1); j < len; j++) {
|
|
232
|
+
args[j - 1] = arguments[j];
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
listeners[i].fn.apply(listeners[i].context, args);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return true;
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Add a listener for a given event.
|
|
245
|
+
*
|
|
246
|
+
* @param {(String|Symbol)} event The event name.
|
|
247
|
+
* @param {Function} fn The listener function.
|
|
248
|
+
* @param {*} [context=this] The context to invoke the listener with.
|
|
249
|
+
* @returns {EventEmitter} `this`.
|
|
250
|
+
* @public
|
|
251
|
+
*/
|
|
252
|
+
EventEmitter.prototype.on = function on(event, fn, context) {
|
|
253
|
+
return addListener(this, event, fn, context, false);
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Add a one-time listener for a given event.
|
|
258
|
+
*
|
|
259
|
+
* @param {(String|Symbol)} event The event name.
|
|
260
|
+
* @param {Function} fn The listener function.
|
|
261
|
+
* @param {*} [context=this] The context to invoke the listener with.
|
|
262
|
+
* @returns {EventEmitter} `this`.
|
|
263
|
+
* @public
|
|
264
|
+
*/
|
|
265
|
+
EventEmitter.prototype.once = function once(event, fn, context) {
|
|
266
|
+
return addListener(this, event, fn, context, true);
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* Remove the listeners of a given event.
|
|
271
|
+
*
|
|
272
|
+
* @param {(String|Symbol)} event The event name.
|
|
273
|
+
* @param {Function} fn Only remove the listeners that match this function.
|
|
274
|
+
* @param {*} context Only remove the listeners that have this context.
|
|
275
|
+
* @param {Boolean} once Only remove one-time listeners.
|
|
276
|
+
* @returns {EventEmitter} `this`.
|
|
277
|
+
* @public
|
|
278
|
+
*/
|
|
279
|
+
EventEmitter.prototype.removeListener = function removeListener(event, fn, context, once) {
|
|
280
|
+
var evt = prefix ? prefix + event : event;
|
|
281
|
+
|
|
282
|
+
if (!this._events[evt]) return this;
|
|
283
|
+
if (!fn) {
|
|
284
|
+
clearEvent(this, evt);
|
|
285
|
+
return this;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
var listeners = this._events[evt];
|
|
289
|
+
|
|
290
|
+
if (listeners.fn) {
|
|
291
|
+
if (
|
|
292
|
+
listeners.fn === fn &&
|
|
293
|
+
(!once || listeners.once) &&
|
|
294
|
+
(!context || listeners.context === context)
|
|
295
|
+
) {
|
|
296
|
+
clearEvent(this, evt);
|
|
297
|
+
}
|
|
298
|
+
} else {
|
|
299
|
+
for (var i = 0, events = [], length = listeners.length; i < length; i++) {
|
|
300
|
+
if (
|
|
301
|
+
listeners[i].fn !== fn ||
|
|
302
|
+
(once && !listeners[i].once) ||
|
|
303
|
+
(context && listeners[i].context !== context)
|
|
304
|
+
) {
|
|
305
|
+
events.push(listeners[i]);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
//
|
|
310
|
+
// Reset the array, or remove it completely if we have no more listeners.
|
|
311
|
+
//
|
|
312
|
+
if (events.length) this._events[evt] = events.length === 1 ? events[0] : events;
|
|
313
|
+
else clearEvent(this, evt);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return this;
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Remove all listeners, or those of the specified event.
|
|
321
|
+
*
|
|
322
|
+
* @param {(String|Symbol)} [event] The event name.
|
|
323
|
+
* @returns {EventEmitter} `this`.
|
|
324
|
+
* @public
|
|
325
|
+
*/
|
|
326
|
+
EventEmitter.prototype.removeAllListeners = function removeAllListeners(event) {
|
|
327
|
+
var evt;
|
|
328
|
+
|
|
329
|
+
if (event) {
|
|
330
|
+
evt = prefix ? prefix + event : event;
|
|
331
|
+
if (this._events[evt]) clearEvent(this, evt);
|
|
332
|
+
} else {
|
|
333
|
+
this._events = new Events();
|
|
334
|
+
this._eventsCount = 0;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
return this;
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
//
|
|
341
|
+
// Alias methods names because people roll like that.
|
|
342
|
+
//
|
|
343
|
+
EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
|
|
344
|
+
EventEmitter.prototype.addListener = EventEmitter.prototype.on;
|
|
345
|
+
|
|
346
|
+
//
|
|
347
|
+
// Expose the prefix.
|
|
348
|
+
//
|
|
349
|
+
EventEmitter.prefixed = prefix;
|
|
350
|
+
|
|
351
|
+
//
|
|
352
|
+
// Allow `EventEmitter` to be imported as module namespace.
|
|
353
|
+
//
|
|
354
|
+
EventEmitter.EventEmitter = EventEmitter;
|
|
355
|
+
|
|
356
|
+
//
|
|
357
|
+
// Expose the module.
|
|
358
|
+
//
|
|
359
|
+
{
|
|
360
|
+
module.exports = EventEmitter;
|
|
361
|
+
}
|
|
362
|
+
} (eventemitter3));
|
|
363
|
+
return eventemitter3.exports;
|
|
364
|
+
}
|
|
24
365
|
|
|
25
|
-
|
|
26
|
-
, prefix = '~';
|
|
366
|
+
var hasRequiredAdapter$2;
|
|
27
367
|
|
|
368
|
+
function requireAdapter$2 () {
|
|
369
|
+
if (hasRequiredAdapter$2) return adapter$1;
|
|
370
|
+
hasRequiredAdapter$2 = 1;
|
|
371
|
+
var __importDefault = (adapter$1 && adapter$1.__importDefault) || function (mod) {
|
|
372
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
373
|
+
};
|
|
374
|
+
Object.defineProperty(adapter$1, "__esModule", { value: true });
|
|
375
|
+
adapter$1.Adapter = adapter$1.AdapterState = adapter$1.WalletReadyState = adapter$1.EventEmitter = void 0;
|
|
376
|
+
const eventemitter3_1 = __importDefault(requireEventemitter3());
|
|
377
|
+
adapter$1.EventEmitter = eventemitter3_1.default;
|
|
28
378
|
/**
|
|
29
|
-
*
|
|
30
|
-
* An `Events` instance is a plain object whose properties are event names.
|
|
31
|
-
*
|
|
32
|
-
* @constructor
|
|
33
|
-
* @private
|
|
379
|
+
* Wallet ready state.
|
|
34
380
|
*/
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
//
|
|
51
|
-
if (!new Events().__proto__) prefix = false;
|
|
52
|
-
}
|
|
53
|
-
|
|
381
|
+
var WalletReadyState;
|
|
382
|
+
(function (WalletReadyState) {
|
|
383
|
+
/**
|
|
384
|
+
* Adapter will start to check if wallet exists after adapter instance is created.
|
|
385
|
+
*/
|
|
386
|
+
WalletReadyState["Loading"] = "Loading";
|
|
387
|
+
/**
|
|
388
|
+
* When checking ends and wallet is not found, readyState will be NotFound.
|
|
389
|
+
*/
|
|
390
|
+
WalletReadyState["NotFound"] = "NotFound";
|
|
391
|
+
/**
|
|
392
|
+
* When checking ends and wallet is found, readyState will be Found.
|
|
393
|
+
*/
|
|
394
|
+
WalletReadyState["Found"] = "Found";
|
|
395
|
+
})(WalletReadyState || (adapter$1.WalletReadyState = WalletReadyState = {}));
|
|
54
396
|
/**
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
* @param {Function} fn The listener function.
|
|
58
|
-
* @param {*} context The context to invoke the listener with.
|
|
59
|
-
* @param {Boolean} [once=false] Specify if the listener is a one-time listener.
|
|
60
|
-
* @constructor
|
|
61
|
-
* @private
|
|
397
|
+
* Adapter state
|
|
62
398
|
*/
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
399
|
+
var AdapterState;
|
|
400
|
+
(function (AdapterState) {
|
|
401
|
+
/**
|
|
402
|
+
* If adapter is checking the wallet, the state is Loading.
|
|
403
|
+
*/
|
|
404
|
+
AdapterState["Loading"] = "Loading";
|
|
405
|
+
/**
|
|
406
|
+
* If wallet is not installed, the state is NotFound.
|
|
407
|
+
*/
|
|
408
|
+
AdapterState["NotFound"] = "NotFound";
|
|
409
|
+
/**
|
|
410
|
+
* If wallet is installed but is not connected to current Dapp, the state is Disconnected.
|
|
411
|
+
*/
|
|
412
|
+
AdapterState["Disconnect"] = "Disconnected";
|
|
413
|
+
/**
|
|
414
|
+
* Wallet is connected to current Dapp.
|
|
415
|
+
*/
|
|
416
|
+
AdapterState["Connected"] = "Connected";
|
|
417
|
+
})(AdapterState || (adapter$1.AdapterState = AdapterState = {}));
|
|
418
|
+
class Adapter extends eventemitter3_1.default {
|
|
419
|
+
get connected() {
|
|
420
|
+
return this.state === AdapterState.Connected;
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Some wallets such as TronLink don't support disconnect() method.
|
|
424
|
+
*/
|
|
425
|
+
disconnect() {
|
|
426
|
+
console.info("The current adapter doesn't support disconnect by DApp.");
|
|
427
|
+
return Promise.resolve();
|
|
428
|
+
}
|
|
429
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
430
|
+
multiSign(...args) {
|
|
431
|
+
return Promise.reject("The current wallet doesn't support multiSign.");
|
|
432
|
+
}
|
|
433
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
434
|
+
switchChain(_chainId) {
|
|
435
|
+
return Promise.reject("The current wallet doesn't support switch chain.");
|
|
436
|
+
}
|
|
67
437
|
}
|
|
438
|
+
adapter$1.Adapter = Adapter;
|
|
439
|
+
|
|
440
|
+
return adapter$1;
|
|
441
|
+
}
|
|
68
442
|
|
|
69
|
-
|
|
70
|
-
* Add a listener for a given event.
|
|
71
|
-
*
|
|
72
|
-
* @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
|
|
73
|
-
* @param {(String|Symbol)} event The event name.
|
|
74
|
-
* @param {Function} fn The listener function.
|
|
75
|
-
* @param {*} context The context to invoke the listener with.
|
|
76
|
-
* @param {Boolean} once Specify if the listener is a one-time listener.
|
|
77
|
-
* @returns {EventEmitter}
|
|
78
|
-
* @private
|
|
79
|
-
*/
|
|
80
|
-
function addListener(emitter, event, fn, context, once) {
|
|
81
|
-
if (typeof fn !== 'function') {
|
|
82
|
-
throw new TypeError('The listener must be a function');
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
var listener = new EE(fn, context || emitter, once)
|
|
86
|
-
, evt = prefix ? prefix + event : event;
|
|
87
|
-
|
|
88
|
-
if (!emitter._events[evt]) emitter._events[evt] = listener, emitter._eventsCount++;
|
|
89
|
-
else if (!emitter._events[evt].fn) emitter._events[evt].push(listener);
|
|
90
|
-
else emitter._events[evt] = [emitter._events[evt], listener];
|
|
443
|
+
var errors = {};
|
|
91
444
|
|
|
92
|
-
|
|
445
|
+
var hasRequiredErrors;
|
|
446
|
+
|
|
447
|
+
function requireErrors () {
|
|
448
|
+
if (hasRequiredErrors) return errors;
|
|
449
|
+
hasRequiredErrors = 1;
|
|
450
|
+
Object.defineProperty(errors, "__esModule", { value: true });
|
|
451
|
+
errors.WalletGetNetworkError = errors.WalletSwitchChainError = errors.WalletWindowClosedError = errors.WalletWalletLoadError = errors.WalletSignTransactionError = errors.WalletSignMessageError = errors.WalletDisconnectionError = errors.WalletConnectionError = errors.WalletDisconnectedError = errors.WalletNotSelectedError = errors.WalletNotFoundError = errors.WalletError = void 0;
|
|
452
|
+
class WalletError extends Error {
|
|
453
|
+
constructor(message, error) {
|
|
454
|
+
super(message);
|
|
455
|
+
this.error = error;
|
|
456
|
+
}
|
|
93
457
|
}
|
|
94
|
-
|
|
458
|
+
errors.WalletError = WalletError;
|
|
95
459
|
/**
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
* @param {EventEmitter} emitter Reference to the `EventEmitter` instance.
|
|
99
|
-
* @param {(String|Symbol)} evt The Event name.
|
|
100
|
-
* @private
|
|
460
|
+
* Occurs when wallet is not installed.
|
|
101
461
|
*/
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
462
|
+
class WalletNotFoundError extends WalletError {
|
|
463
|
+
constructor() {
|
|
464
|
+
super(...arguments);
|
|
465
|
+
this.name = 'WalletNotFoundError';
|
|
466
|
+
this.message = 'The wallet is not found.';
|
|
467
|
+
}
|
|
105
468
|
}
|
|
106
|
-
|
|
469
|
+
errors.WalletNotFoundError = WalletNotFoundError;
|
|
107
470
|
/**
|
|
108
|
-
*
|
|
109
|
-
* `EventEmitter` interface.
|
|
110
|
-
*
|
|
111
|
-
* @constructor
|
|
112
|
-
* @public
|
|
471
|
+
* Occurs when connect to a wallet but there is no wallet selected.
|
|
113
472
|
*/
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
473
|
+
class WalletNotSelectedError extends WalletError {
|
|
474
|
+
constructor() {
|
|
475
|
+
super(...arguments);
|
|
476
|
+
this.name = 'WalletNotSelectedError';
|
|
477
|
+
this.message = 'No wallet is selected. Please select a wallet.';
|
|
478
|
+
}
|
|
117
479
|
}
|
|
118
|
-
|
|
480
|
+
errors.WalletNotSelectedError = WalletNotSelectedError;
|
|
119
481
|
/**
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
* @returns {Array}
|
|
124
|
-
* @public
|
|
482
|
+
* Occurs when wallet is disconnected.
|
|
483
|
+
* Used by some wallets which won't connect automatically when call `signMessage()` or `signTransaction()`.
|
|
125
484
|
*/
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
if (has.call(events, name)) names.push(prefix ? name.slice(1) : name);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if (Object.getOwnPropertySymbols) {
|
|
138
|
-
return names.concat(Object.getOwnPropertySymbols(events));
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return names;
|
|
142
|
-
};
|
|
143
|
-
|
|
485
|
+
class WalletDisconnectedError extends WalletError {
|
|
486
|
+
constructor() {
|
|
487
|
+
super(...arguments);
|
|
488
|
+
this.name = 'WalletDisconnectedError';
|
|
489
|
+
this.message = 'The wallet is disconnected. Please connect first.';
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
errors.WalletDisconnectedError = WalletDisconnectedError;
|
|
144
493
|
/**
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
* @param {(String|Symbol)} event The event name.
|
|
148
|
-
* @returns {Array} The registered listeners.
|
|
149
|
-
* @public
|
|
494
|
+
* Occurs when try to connect a wallet.
|
|
150
495
|
*/
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
for (var i = 0, l = handlers.length, ee = new Array(l); i < l; i++) {
|
|
159
|
-
ee[i] = handlers[i].fn;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return ee;
|
|
163
|
-
};
|
|
164
|
-
|
|
496
|
+
class WalletConnectionError extends WalletError {
|
|
497
|
+
constructor() {
|
|
498
|
+
super(...arguments);
|
|
499
|
+
this.name = 'WalletConnectionError';
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
errors.WalletConnectionError = WalletConnectionError;
|
|
165
503
|
/**
|
|
166
|
-
*
|
|
167
|
-
*
|
|
168
|
-
* @param {(String|Symbol)} event The event name.
|
|
169
|
-
* @returns {Number} The number of listeners.
|
|
170
|
-
* @public
|
|
504
|
+
* Occurs when try to disconnect a wallet.
|
|
171
505
|
*/
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
};
|
|
180
|
-
|
|
506
|
+
class WalletDisconnectionError extends WalletError {
|
|
507
|
+
constructor() {
|
|
508
|
+
super(...arguments);
|
|
509
|
+
this.name = 'WalletDisconnectionError';
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
errors.WalletDisconnectionError = WalletDisconnectionError;
|
|
181
513
|
/**
|
|
182
|
-
*
|
|
183
|
-
*
|
|
184
|
-
* @param {(String|Symbol)} event The event name.
|
|
185
|
-
* @returns {Boolean} `true` if the event had listeners, else `false`.
|
|
186
|
-
* @public
|
|
514
|
+
* Occurs when call `signMessage()`.
|
|
187
515
|
*/
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
var listeners = this._events[evt]
|
|
194
|
-
, len = arguments.length
|
|
195
|
-
, args
|
|
196
|
-
, i;
|
|
197
|
-
|
|
198
|
-
if (listeners.fn) {
|
|
199
|
-
if (listeners.once) this.removeListener(event, listeners.fn, undefined, true);
|
|
200
|
-
|
|
201
|
-
switch (len) {
|
|
202
|
-
case 1: return listeners.fn.call(listeners.context), true;
|
|
203
|
-
case 2: return listeners.fn.call(listeners.context, a1), true;
|
|
204
|
-
case 3: return listeners.fn.call(listeners.context, a1, a2), true;
|
|
205
|
-
case 4: return listeners.fn.call(listeners.context, a1, a2, a3), true;
|
|
206
|
-
case 5: return listeners.fn.call(listeners.context, a1, a2, a3, a4), true;
|
|
207
|
-
case 6: return listeners.fn.call(listeners.context, a1, a2, a3, a4, a5), true;
|
|
516
|
+
class WalletSignMessageError extends WalletError {
|
|
517
|
+
constructor() {
|
|
518
|
+
super(...arguments);
|
|
519
|
+
this.name = 'WalletSignMessageError';
|
|
208
520
|
}
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
args[i - 1] = arguments[i];
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
listeners.fn.apply(listeners.context, args);
|
|
215
|
-
} else {
|
|
216
|
-
var length = listeners.length
|
|
217
|
-
, j;
|
|
218
|
-
|
|
219
|
-
for (i = 0; i < length; i++) {
|
|
220
|
-
if (listeners[i].once) this.removeListener(event, listeners[i].fn, undefined, true);
|
|
221
|
-
|
|
222
|
-
switch (len) {
|
|
223
|
-
case 1: listeners[i].fn.call(listeners[i].context); break;
|
|
224
|
-
case 2: listeners[i].fn.call(listeners[i].context, a1); break;
|
|
225
|
-
case 3: listeners[i].fn.call(listeners[i].context, a1, a2); break;
|
|
226
|
-
case 4: listeners[i].fn.call(listeners[i].context, a1, a2, a3); break;
|
|
227
|
-
default:
|
|
228
|
-
if (!args) for (j = 1, args = new Array(len -1); j < len; j++) {
|
|
229
|
-
args[j - 1] = arguments[j];
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
listeners[i].fn.apply(listeners[i].context, args);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
return true;
|
|
238
|
-
};
|
|
239
|
-
|
|
521
|
+
}
|
|
522
|
+
errors.WalletSignMessageError = WalletSignMessageError;
|
|
240
523
|
/**
|
|
241
|
-
*
|
|
242
|
-
*
|
|
243
|
-
* @param {(String|Symbol)} event The event name.
|
|
244
|
-
* @param {Function} fn The listener function.
|
|
245
|
-
* @param {*} [context=this] The context to invoke the listener with.
|
|
246
|
-
* @returns {EventEmitter} `this`.
|
|
247
|
-
* @public
|
|
524
|
+
* Occurs when call `signTransaction()`.
|
|
248
525
|
*/
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
526
|
+
class WalletSignTransactionError extends WalletError {
|
|
527
|
+
constructor() {
|
|
528
|
+
super(...arguments);
|
|
529
|
+
this.name = 'WalletSignTransactionError';
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
errors.WalletSignTransactionError = WalletSignTransactionError;
|
|
253
533
|
/**
|
|
254
|
-
*
|
|
255
|
-
*
|
|
256
|
-
* @param {(String|Symbol)} event The event name.
|
|
257
|
-
* @param {Function} fn The listener function.
|
|
258
|
-
* @param {*} [context=this] The context to invoke the listener with.
|
|
259
|
-
* @returns {EventEmitter} `this`.
|
|
260
|
-
* @public
|
|
534
|
+
* Occurs when load wallet
|
|
261
535
|
*/
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
536
|
+
class WalletWalletLoadError extends WalletError {
|
|
537
|
+
constructor() {
|
|
538
|
+
super(...arguments);
|
|
539
|
+
this.name = 'WalletWalletLoadError';
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
errors.WalletWalletLoadError = WalletWalletLoadError;
|
|
266
543
|
/**
|
|
267
|
-
*
|
|
268
|
-
*
|
|
269
|
-
* @param {(String|Symbol)} event The event name.
|
|
270
|
-
* @param {Function} fn Only remove the listeners that match this function.
|
|
271
|
-
* @param {*} context Only remove the listeners that have this context.
|
|
272
|
-
* @param {Boolean} once Only remove one-time listeners.
|
|
273
|
-
* @returns {EventEmitter} `this`.
|
|
274
|
-
* @public
|
|
544
|
+
* Occurs when walletconnect QR window is closed.
|
|
275
545
|
*/
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
clearEvent(this, evt);
|
|
282
|
-
return this;
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
var listeners = this._events[evt];
|
|
286
|
-
|
|
287
|
-
if (listeners.fn) {
|
|
288
|
-
if (
|
|
289
|
-
listeners.fn === fn &&
|
|
290
|
-
(!once || listeners.once) &&
|
|
291
|
-
(!context || listeners.context === context)
|
|
292
|
-
) {
|
|
293
|
-
clearEvent(this, evt);
|
|
546
|
+
class WalletWindowClosedError extends WalletError {
|
|
547
|
+
constructor() {
|
|
548
|
+
super(...arguments);
|
|
549
|
+
this.name = 'WalletWindowClosedError';
|
|
550
|
+
this.message = 'The QR window is closed.';
|
|
294
551
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
552
|
+
}
|
|
553
|
+
errors.WalletWindowClosedError = WalletWindowClosedError;
|
|
554
|
+
/**
|
|
555
|
+
* Occurs when request wallet to switch chain.
|
|
556
|
+
*/
|
|
557
|
+
class WalletSwitchChainError extends WalletError {
|
|
558
|
+
constructor() {
|
|
559
|
+
super(...arguments);
|
|
560
|
+
this.name = 'WalletSwitchChainError';
|
|
304
561
|
}
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
// Reset the array, or remove it completely if we have no more listeners.
|
|
308
|
-
//
|
|
309
|
-
if (events.length) this._events[evt] = events.length === 1 ? events[0] : events;
|
|
310
|
-
else clearEvent(this, evt);
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
return this;
|
|
314
|
-
};
|
|
315
|
-
|
|
562
|
+
}
|
|
563
|
+
errors.WalletSwitchChainError = WalletSwitchChainError;
|
|
316
564
|
/**
|
|
317
|
-
*
|
|
318
|
-
*
|
|
319
|
-
* @param {(String|Symbol)} [event] The event name.
|
|
320
|
-
* @returns {EventEmitter} `this`.
|
|
321
|
-
* @public
|
|
565
|
+
* Occurs when get network infomation.
|
|
322
566
|
*/
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
return this;
|
|
335
|
-
};
|
|
336
|
-
|
|
337
|
-
//
|
|
338
|
-
// Alias methods names because people roll like that.
|
|
339
|
-
//
|
|
340
|
-
EventEmitter.prototype.off = EventEmitter.prototype.removeListener;
|
|
341
|
-
EventEmitter.prototype.addListener = EventEmitter.prototype.on;
|
|
342
|
-
|
|
343
|
-
//
|
|
344
|
-
// Expose the prefix.
|
|
345
|
-
//
|
|
346
|
-
EventEmitter.prefixed = prefix;
|
|
347
|
-
|
|
348
|
-
//
|
|
349
|
-
// Allow `EventEmitter` to be imported as module namespace.
|
|
350
|
-
//
|
|
351
|
-
EventEmitter.EventEmitter = EventEmitter;
|
|
352
|
-
|
|
353
|
-
//
|
|
354
|
-
// Expose the module.
|
|
355
|
-
//
|
|
356
|
-
{
|
|
357
|
-
module.exports = EventEmitter;
|
|
358
|
-
}
|
|
359
|
-
} (eventemitter3));
|
|
360
|
-
|
|
361
|
-
var eventemitter3Exports = eventemitter3.exports;
|
|
362
|
-
|
|
363
|
-
var __importDefault = (commonjsGlobal && commonjsGlobal.__importDefault) || function (mod) {
|
|
364
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
365
|
-
};
|
|
366
|
-
Object.defineProperty(adapter$1, "__esModule", { value: true });
|
|
367
|
-
adapter$1.Adapter = adapter$1.AdapterState = adapter$1.WalletReadyState = adapter$1.EventEmitter = void 0;
|
|
368
|
-
const eventemitter3_1 = __importDefault(eventemitter3Exports);
|
|
369
|
-
adapter$1.EventEmitter = eventemitter3_1.default;
|
|
370
|
-
/**
|
|
371
|
-
* Wallet ready state.
|
|
372
|
-
*/
|
|
373
|
-
var WalletReadyState;
|
|
374
|
-
(function (WalletReadyState) {
|
|
375
|
-
/**
|
|
376
|
-
* Adapter will start to check if wallet exists after adapter instance is created.
|
|
377
|
-
*/
|
|
378
|
-
WalletReadyState["Loading"] = "Loading";
|
|
379
|
-
/**
|
|
380
|
-
* When checking ends and wallet is not found, readyState will be NotFound.
|
|
381
|
-
*/
|
|
382
|
-
WalletReadyState["NotFound"] = "NotFound";
|
|
383
|
-
/**
|
|
384
|
-
* When checking ends and wallet is found, readyState will be Found.
|
|
385
|
-
*/
|
|
386
|
-
WalletReadyState["Found"] = "Found";
|
|
387
|
-
})(WalletReadyState || (adapter$1.WalletReadyState = WalletReadyState = {}));
|
|
388
|
-
/**
|
|
389
|
-
* Adapter state
|
|
390
|
-
*/
|
|
391
|
-
var AdapterState;
|
|
392
|
-
(function (AdapterState) {
|
|
393
|
-
/**
|
|
394
|
-
* If adapter is checking the wallet, the state is Loading.
|
|
395
|
-
*/
|
|
396
|
-
AdapterState["Loading"] = "Loading";
|
|
397
|
-
/**
|
|
398
|
-
* If wallet is not installed, the state is NotFound.
|
|
399
|
-
*/
|
|
400
|
-
AdapterState["NotFound"] = "NotFound";
|
|
401
|
-
/**
|
|
402
|
-
* If wallet is installed but is not connected to current Dapp, the state is Disconnected.
|
|
403
|
-
*/
|
|
404
|
-
AdapterState["Disconnect"] = "Disconnected";
|
|
405
|
-
/**
|
|
406
|
-
* Wallet is connected to current Dapp.
|
|
407
|
-
*/
|
|
408
|
-
AdapterState["Connected"] = "Connected";
|
|
409
|
-
})(AdapterState || (adapter$1.AdapterState = AdapterState = {}));
|
|
410
|
-
class Adapter extends eventemitter3_1.default {
|
|
411
|
-
get connected() {
|
|
412
|
-
return this.state === AdapterState.Connected;
|
|
413
|
-
}
|
|
414
|
-
/**
|
|
415
|
-
* Some wallets such as TronLink don't support disconnect() method.
|
|
416
|
-
*/
|
|
417
|
-
disconnect() {
|
|
418
|
-
console.info("The current adapter doesn't support disconnect by DApp.");
|
|
419
|
-
return Promise.resolve();
|
|
420
|
-
}
|
|
421
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
422
|
-
multiSign(...args) {
|
|
423
|
-
return Promise.reject("The current wallet doesn't support multiSign.");
|
|
424
|
-
}
|
|
425
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
426
|
-
switchChain(_chainId) {
|
|
427
|
-
return Promise.reject("The current wallet doesn't support switch chain.");
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
adapter$1.Adapter = Adapter;
|
|
431
|
-
|
|
432
|
-
var errors = {};
|
|
433
|
-
|
|
434
|
-
Object.defineProperty(errors, "__esModule", { value: true });
|
|
435
|
-
errors.WalletGetNetworkError = errors.WalletSwitchChainError = errors.WalletWindowClosedError = errors.WalletWalletLoadError = errors.WalletSignTransactionError = errors.WalletSignMessageError = errors.WalletDisconnectionError = errors.WalletConnectionError = errors.WalletDisconnectedError = errors.WalletNotSelectedError = errors.WalletNotFoundError = errors.WalletError = void 0;
|
|
436
|
-
class WalletError extends Error {
|
|
437
|
-
constructor(message, error) {
|
|
438
|
-
super(message);
|
|
439
|
-
this.error = error;
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
errors.WalletError = WalletError;
|
|
443
|
-
/**
|
|
444
|
-
* Occurs when wallet is not installed.
|
|
445
|
-
*/
|
|
446
|
-
class WalletNotFoundError extends WalletError {
|
|
447
|
-
constructor() {
|
|
448
|
-
super(...arguments);
|
|
449
|
-
this.name = 'WalletNotFoundError';
|
|
450
|
-
this.message = 'The wallet is not found.';
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
errors.WalletNotFoundError = WalletNotFoundError;
|
|
454
|
-
/**
|
|
455
|
-
* Occurs when connect to a wallet but there is no wallet selected.
|
|
456
|
-
*/
|
|
457
|
-
class WalletNotSelectedError extends WalletError {
|
|
458
|
-
constructor() {
|
|
459
|
-
super(...arguments);
|
|
460
|
-
this.name = 'WalletNotSelectedError';
|
|
461
|
-
this.message = 'No wallet is selected. Please select a wallet.';
|
|
462
|
-
}
|
|
463
|
-
}
|
|
464
|
-
errors.WalletNotSelectedError = WalletNotSelectedError;
|
|
465
|
-
/**
|
|
466
|
-
* Occurs when wallet is disconnected.
|
|
467
|
-
* Used by some wallets which won't connect automatically when call `signMessage()` or `signTransaction()`.
|
|
468
|
-
*/
|
|
469
|
-
class WalletDisconnectedError extends WalletError {
|
|
470
|
-
constructor() {
|
|
471
|
-
super(...arguments);
|
|
472
|
-
this.name = 'WalletDisconnectedError';
|
|
473
|
-
this.message = 'The wallet is disconnected. Please connect first.';
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
errors.WalletDisconnectedError = WalletDisconnectedError;
|
|
477
|
-
/**
|
|
478
|
-
* Occurs when try to connect a wallet.
|
|
479
|
-
*/
|
|
480
|
-
class WalletConnectionError extends WalletError {
|
|
481
|
-
constructor() {
|
|
482
|
-
super(...arguments);
|
|
483
|
-
this.name = 'WalletConnectionError';
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
errors.WalletConnectionError = WalletConnectionError;
|
|
487
|
-
/**
|
|
488
|
-
* Occurs when try to disconnect a wallet.
|
|
489
|
-
*/
|
|
490
|
-
class WalletDisconnectionError extends WalletError {
|
|
491
|
-
constructor() {
|
|
492
|
-
super(...arguments);
|
|
493
|
-
this.name = 'WalletDisconnectionError';
|
|
494
|
-
}
|
|
495
|
-
}
|
|
496
|
-
errors.WalletDisconnectionError = WalletDisconnectionError;
|
|
497
|
-
/**
|
|
498
|
-
* Occurs when call `signMessage()`.
|
|
499
|
-
*/
|
|
500
|
-
class WalletSignMessageError extends WalletError {
|
|
501
|
-
constructor() {
|
|
502
|
-
super(...arguments);
|
|
503
|
-
this.name = 'WalletSignMessageError';
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
errors.WalletSignMessageError = WalletSignMessageError;
|
|
507
|
-
/**
|
|
508
|
-
* Occurs when call `signTransaction()`.
|
|
509
|
-
*/
|
|
510
|
-
class WalletSignTransactionError extends WalletError {
|
|
511
|
-
constructor() {
|
|
512
|
-
super(...arguments);
|
|
513
|
-
this.name = 'WalletSignTransactionError';
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
errors.WalletSignTransactionError = WalletSignTransactionError;
|
|
517
|
-
/**
|
|
518
|
-
* Occurs when load wallet
|
|
519
|
-
*/
|
|
520
|
-
class WalletWalletLoadError extends WalletError {
|
|
521
|
-
constructor() {
|
|
522
|
-
super(...arguments);
|
|
523
|
-
this.name = 'WalletWalletLoadError';
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
errors.WalletWalletLoadError = WalletWalletLoadError;
|
|
527
|
-
/**
|
|
528
|
-
* Occurs when walletconnect QR window is closed.
|
|
529
|
-
*/
|
|
530
|
-
class WalletWindowClosedError extends WalletError {
|
|
531
|
-
constructor() {
|
|
532
|
-
super(...arguments);
|
|
533
|
-
this.name = 'WalletWindowClosedError';
|
|
534
|
-
this.message = 'The QR window is closed.';
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
errors.WalletWindowClosedError = WalletWindowClosedError;
|
|
538
|
-
/**
|
|
539
|
-
* Occurs when request wallet to switch chain.
|
|
540
|
-
*/
|
|
541
|
-
class WalletSwitchChainError extends WalletError {
|
|
542
|
-
constructor() {
|
|
543
|
-
super(...arguments);
|
|
544
|
-
this.name = 'WalletSwitchChainError';
|
|
545
|
-
}
|
|
546
|
-
}
|
|
547
|
-
errors.WalletSwitchChainError = WalletSwitchChainError;
|
|
548
|
-
/**
|
|
549
|
-
* Occurs when get network infomation.
|
|
550
|
-
*/
|
|
551
|
-
class WalletGetNetworkError extends WalletError {
|
|
552
|
-
constructor() {
|
|
553
|
-
super(...arguments);
|
|
554
|
-
this.name = 'WalletGetNetworkError';
|
|
555
|
-
}
|
|
567
|
+
class WalletGetNetworkError extends WalletError {
|
|
568
|
+
constructor() {
|
|
569
|
+
super(...arguments);
|
|
570
|
+
this.name = 'WalletGetNetworkError';
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
errors.WalletGetNetworkError = WalletGetNetworkError;
|
|
574
|
+
|
|
575
|
+
return errors;
|
|
556
576
|
}
|
|
557
|
-
errors.WalletGetNetworkError = WalletGetNetworkError;
|
|
558
577
|
|
|
559
578
|
var types$1 = {};
|
|
560
579
|
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
580
|
+
var hasRequiredTypes$1;
|
|
581
|
+
|
|
582
|
+
function requireTypes$1 () {
|
|
583
|
+
if (hasRequiredTypes$1) return types$1;
|
|
584
|
+
hasRequiredTypes$1 = 1;
|
|
585
|
+
Object.defineProperty(types$1, "__esModule", { value: true });
|
|
586
|
+
types$1.ChainNetwork = types$1.NetworkType = void 0;
|
|
587
|
+
var NetworkType;
|
|
588
|
+
(function (NetworkType) {
|
|
589
|
+
NetworkType["Mainnet"] = "Mainnet";
|
|
590
|
+
NetworkType["Shasta"] = "Shasta";
|
|
591
|
+
NetworkType["Nile"] = "Nile";
|
|
592
|
+
/**
|
|
593
|
+
* When use custom node
|
|
594
|
+
*/
|
|
595
|
+
NetworkType["Unknown"] = "Unknown";
|
|
596
|
+
})(NetworkType || (types$1.NetworkType = NetworkType = {}));
|
|
597
|
+
var ChainNetwork;
|
|
598
|
+
(function (ChainNetwork) {
|
|
599
|
+
ChainNetwork["Mainnet"] = "Mainnet";
|
|
600
|
+
ChainNetwork["Shasta"] = "Shasta";
|
|
601
|
+
ChainNetwork["Nile"] = "Nile";
|
|
602
|
+
})(ChainNetwork || (types$1.ChainNetwork = ChainNetwork = {}));
|
|
603
|
+
|
|
604
|
+
return types$1;
|
|
605
|
+
}
|
|
579
606
|
|
|
580
607
|
var utils$2 = {};
|
|
581
608
|
|
|
582
|
-
|
|
583
|
-
utils$2.isInBrowser = isInBrowser;
|
|
584
|
-
utils$2.checkAdapterState = checkAdapterState;
|
|
585
|
-
utils$2.isInMobileBrowser = isInMobileBrowser;
|
|
586
|
-
/**
|
|
587
|
-
* check simply if current environment is browser or not
|
|
588
|
-
* @returns boolean
|
|
589
|
-
*/
|
|
590
|
-
function isInBrowser() {
|
|
591
|
-
return typeof window !== 'undefined' && typeof document !== 'undefined' && typeof navigator !== 'undefined';
|
|
592
|
-
}
|
|
593
|
-
/**
|
|
594
|
-
*
|
|
595
|
-
* @param {Function} check funcion to check if wallet is installed. return true if wallet is detected.
|
|
596
|
-
* @returns
|
|
597
|
-
*/
|
|
598
|
-
function checkAdapterState(check) {
|
|
599
|
-
if (!isInBrowser())
|
|
600
|
-
return;
|
|
601
|
-
const disposers = [];
|
|
602
|
-
function dispose() {
|
|
603
|
-
for (const dispose of disposers) {
|
|
604
|
-
dispose();
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
function checkAndDispose() {
|
|
608
|
-
if (check()) {
|
|
609
|
-
dispose();
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
const interval = setInterval(checkAndDispose, 500);
|
|
613
|
-
disposers.push(() => clearInterval(interval));
|
|
614
|
-
if (document.readyState === 'loading') {
|
|
615
|
-
document.addEventListener('DOMContentLoaded', checkAndDispose, { once: true });
|
|
616
|
-
disposers.push(() => document.removeEventListener('DOMContentLoaded', checkAndDispose));
|
|
617
|
-
}
|
|
618
|
-
if (document.readyState !== 'complete') {
|
|
619
|
-
window.addEventListener('load', checkAndDispose, { once: true });
|
|
620
|
-
disposers.push(() => window.removeEventListener('load', checkAndDispose));
|
|
621
|
-
}
|
|
622
|
-
checkAndDispose();
|
|
623
|
-
// stop all task after 1min
|
|
624
|
-
setTimeout(dispose, 60 * 1000);
|
|
625
|
-
}
|
|
626
|
-
/**
|
|
627
|
-
* Simplily detect mobile device
|
|
628
|
-
*/
|
|
629
|
-
function isInMobileBrowser() {
|
|
630
|
-
return (typeof navigator !== 'undefined' &&
|
|
631
|
-
navigator.userAgent.match(/Android|webOS|iPhone|iPad|iPod|BlackBerry|Windows Phone/i));
|
|
632
|
-
}
|
|
609
|
+
var hasRequiredUtils$2;
|
|
633
610
|
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
611
|
+
function requireUtils$2 () {
|
|
612
|
+
if (hasRequiredUtils$2) return utils$2;
|
|
613
|
+
hasRequiredUtils$2 = 1;
|
|
614
|
+
Object.defineProperty(utils$2, "__esModule", { value: true });
|
|
615
|
+
utils$2.isInBrowser = isInBrowser;
|
|
616
|
+
utils$2.checkAdapterState = checkAdapterState;
|
|
617
|
+
utils$2.isInMobileBrowser = isInMobileBrowser;
|
|
618
|
+
/**
|
|
619
|
+
* check simply if current environment is browser or not
|
|
620
|
+
* @returns boolean
|
|
621
|
+
*/
|
|
622
|
+
function isInBrowser() {
|
|
623
|
+
return typeof window !== 'undefined' && typeof document !== 'undefined' && typeof navigator !== 'undefined';
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
*
|
|
627
|
+
* @param {Function} check funcion to check if wallet is installed. return true if wallet is detected.
|
|
628
|
+
* @returns
|
|
629
|
+
*/
|
|
630
|
+
function checkAdapterState(check) {
|
|
631
|
+
if (!isInBrowser())
|
|
632
|
+
return;
|
|
633
|
+
const disposers = [];
|
|
634
|
+
function dispose() {
|
|
635
|
+
for (const dispose of disposers) {
|
|
636
|
+
dispose();
|
|
637
|
+
}
|
|
640
638
|
}
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
639
|
+
function checkAndDispose() {
|
|
640
|
+
if (check()) {
|
|
641
|
+
dispose();
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
const interval = setInterval(checkAndDispose, 500);
|
|
645
|
+
disposers.push(() => clearInterval(interval));
|
|
646
|
+
if (document.readyState === 'loading') {
|
|
647
|
+
document.addEventListener('DOMContentLoaded', checkAndDispose, { once: true });
|
|
648
|
+
disposers.push(() => document.removeEventListener('DOMContentLoaded', checkAndDispose));
|
|
649
|
+
}
|
|
650
|
+
if (document.readyState !== 'complete') {
|
|
651
|
+
window.addEventListener('load', checkAndDispose, { once: true });
|
|
652
|
+
disposers.push(() => window.removeEventListener('load', checkAndDispose));
|
|
653
|
+
}
|
|
654
|
+
checkAndDispose();
|
|
655
|
+
// stop all task after 1min
|
|
656
|
+
setTimeout(dispose, 60 * 1000);
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* Simplily detect mobile device
|
|
660
|
+
*/
|
|
661
|
+
function isInMobileBrowser() {
|
|
662
|
+
return (typeof navigator !== 'undefined' &&
|
|
663
|
+
navigator.userAgent.match(/Android|webOS|iPhone|iPad|iPod|BlackBerry|Windows Phone/i));
|
|
664
|
+
}
|
|
654
665
|
|
|
655
|
-
|
|
666
|
+
return utils$2;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
var hasRequiredCjs$2;
|
|
670
|
+
|
|
671
|
+
function requireCjs$2 () {
|
|
672
|
+
if (hasRequiredCjs$2) return cjs$1;
|
|
673
|
+
hasRequiredCjs$2 = 1;
|
|
674
|
+
(function (exports) {
|
|
675
|
+
var __createBinding = (cjs$1 && cjs$1.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
676
|
+
if (k2 === undefined) k2 = k;
|
|
677
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
678
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
679
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
680
|
+
}
|
|
681
|
+
Object.defineProperty(o, k2, desc);
|
|
682
|
+
}) : (function(o, m, k, k2) {
|
|
683
|
+
if (k2 === undefined) k2 = k;
|
|
684
|
+
o[k2] = m[k];
|
|
685
|
+
}));
|
|
686
|
+
var __exportStar = (cjs$1 && cjs$1.__exportStar) || function(m, exports) {
|
|
687
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
688
|
+
};
|
|
689
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
690
|
+
__exportStar(/*@__PURE__*/ requireAdapter$2(), exports);
|
|
691
|
+
__exportStar(/*@__PURE__*/ requireErrors(), exports);
|
|
692
|
+
__exportStar(/*@__PURE__*/ requireTypes$1(), exports);
|
|
693
|
+
__exportStar(/*@__PURE__*/ requireUtils$2(), exports);
|
|
694
|
+
|
|
695
|
+
} (cjs$1));
|
|
696
|
+
return cjs$1;
|
|
697
|
+
}
|
|
656
698
|
|
|
657
699
|
var cjs = {};
|
|
658
700
|
|
|
@@ -660,87 +702,12 @@
|
|
|
660
702
|
|
|
661
703
|
var utils$1 = {};
|
|
662
704
|
|
|
663
|
-
var
|
|
664
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
665
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
666
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
667
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
668
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
669
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
670
|
-
});
|
|
671
|
-
};
|
|
672
|
-
Object.defineProperty(utils$1, "__esModule", { value: true });
|
|
673
|
-
utils$1.supportTron = supportTron;
|
|
674
|
-
utils$1.supportTronLink = supportTronLink;
|
|
675
|
-
utils$1.isInTronLinkApp = isInTronLinkApp;
|
|
676
|
-
utils$1.openTronLink = openTronLink;
|
|
677
|
-
utils$1.waitTronwebReady = waitTronwebReady;
|
|
678
|
-
const tronwallet_abstract_adapter_1 = cjs$1;
|
|
679
|
-
function supportTron() {
|
|
680
|
-
return !!(window.tron && window.tron.isTronLink);
|
|
681
|
-
}
|
|
682
|
-
function supportTronLink() {
|
|
683
|
-
return !!(supportTron() || window.tronLink || window.tronWeb);
|
|
684
|
-
}
|
|
685
|
-
/**
|
|
686
|
-
* Detect if in TronLinkApp
|
|
687
|
-
* Tron DApp running in the DApp Explorer injects iTron objects automatically to offer customized App service.
|
|
688
|
-
* See [here](https://docs.tronlink.org/tronlink-app/dapp-support/dapp-explorer)
|
|
689
|
-
*/
|
|
690
|
-
function isInTronLinkApp() {
|
|
691
|
-
return (0, tronwallet_abstract_adapter_1.isInBrowser)() && typeof window.iTron !== 'undefined';
|
|
692
|
-
}
|
|
693
|
-
function openTronLink({ dappIcon, dappName } = { dappIcon: '', dappName: '' }) {
|
|
694
|
-
if (!supportTronLink() && (0, tronwallet_abstract_adapter_1.isInMobileBrowser)() && !isInTronLinkApp()) {
|
|
695
|
-
let defaultDappName = '', defaultDappIcon = '';
|
|
696
|
-
try {
|
|
697
|
-
defaultDappName = document.title;
|
|
698
|
-
const link = document.querySelector('link[rel*="icon"]');
|
|
699
|
-
if (link) {
|
|
700
|
-
defaultDappIcon = new URL(link.getAttribute('href') || '', location.href).toString();
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
catch (e) {
|
|
704
|
-
// console.error(e);
|
|
705
|
-
}
|
|
706
|
-
const { origin, pathname, search, hash } = window.location;
|
|
707
|
-
const url = origin + pathname + search + (hash.includes('?') ? hash : `${hash}?_=1`);
|
|
708
|
-
const params = {
|
|
709
|
-
action: 'open',
|
|
710
|
-
actionId: Date.now() + '',
|
|
711
|
-
callbackUrl: 'http://someurl.com', // no need callback
|
|
712
|
-
dappIcon: dappIcon || defaultDappIcon,
|
|
713
|
-
dappName: dappName || defaultDappName,
|
|
714
|
-
url,
|
|
715
|
-
protocol: 'TronLink',
|
|
716
|
-
version: '1.0',
|
|
717
|
-
chainId: '0x2b6653dc',
|
|
718
|
-
};
|
|
719
|
-
window.location.href = `tronlinkoutside://pull.activity?param=${encodeURIComponent(JSON.stringify(params))}`;
|
|
720
|
-
return true;
|
|
721
|
-
}
|
|
722
|
-
return false;
|
|
723
|
-
}
|
|
724
|
-
function waitTronwebReady(tronObj) {
|
|
725
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
726
|
-
return new Promise((resolve, reject) => {
|
|
727
|
-
const interval = setInterval(() => {
|
|
728
|
-
if (tronObj.tronWeb) {
|
|
729
|
-
clearInterval(interval);
|
|
730
|
-
clearTimeout(timeout);
|
|
731
|
-
resolve();
|
|
732
|
-
}
|
|
733
|
-
}, 50);
|
|
734
|
-
const timeout = setTimeout(() => {
|
|
735
|
-
clearInterval(interval);
|
|
736
|
-
reject('`window.tron.tronweb` is not ready.');
|
|
737
|
-
}, 2000);
|
|
738
|
-
});
|
|
739
|
-
});
|
|
740
|
-
}
|
|
705
|
+
var hasRequiredUtils$1;
|
|
741
706
|
|
|
742
|
-
|
|
743
|
-
|
|
707
|
+
function requireUtils$1 () {
|
|
708
|
+
if (hasRequiredUtils$1) return utils$1;
|
|
709
|
+
hasRequiredUtils$1 = 1;
|
|
710
|
+
var __awaiter = (utils$1 && utils$1.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
744
711
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
745
712
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
746
713
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
@@ -749,990 +716,1126 @@
|
|
|
749
716
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
750
717
|
});
|
|
751
718
|
};
|
|
752
|
-
Object.defineProperty(
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
};
|
|
762
|
-
function getNetworkInfoByTronWeb(tronWeb) {
|
|
763
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
764
|
-
var _a, _b, _c;
|
|
765
|
-
const { blockID = '' } = yield tronWeb.trx.getBlockByNumber(0);
|
|
766
|
-
const chainId = `0x${blockID.slice(-8)}`;
|
|
767
|
-
return {
|
|
768
|
-
networkType: exports.chainIdNetworkMap[chainId] || tronwallet_abstract_adapter_1.NetworkType.Unknown,
|
|
769
|
-
chainId,
|
|
770
|
-
fullNode: ((_a = tronWeb.fullNode) === null || _a === void 0 ? void 0 : _a.host) || '',
|
|
771
|
-
solidityNode: ((_b = tronWeb.solidityNode) === null || _b === void 0 ? void 0 : _b.host) || '',
|
|
772
|
-
eventServer: ((_c = tronWeb.eventServer) === null || _c === void 0 ? void 0 : _c.host) || '',
|
|
773
|
-
};
|
|
774
|
-
});
|
|
719
|
+
Object.defineProperty(utils$1, "__esModule", { value: true });
|
|
720
|
+
utils$1.supportTron = supportTron;
|
|
721
|
+
utils$1.supportTronLink = supportTronLink;
|
|
722
|
+
utils$1.isInTronLinkApp = isInTronLinkApp;
|
|
723
|
+
utils$1.openTronLink = openTronLink;
|
|
724
|
+
utils$1.waitTronwebReady = waitTronwebReady;
|
|
725
|
+
const tronwallet_abstract_adapter_1 = /*@__PURE__*/ requireCjs$2();
|
|
726
|
+
function supportTron() {
|
|
727
|
+
return !!(window.tron && window.tron.isTronLink);
|
|
775
728
|
}
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
setTimeout(() => {
|
|
796
|
-
var _a;
|
|
797
|
-
const preAddr = this.address || '';
|
|
798
|
-
if ((_a = this._wallet) === null || _a === void 0 ? void 0 : _a.ready) {
|
|
799
|
-
const address = message.data.address;
|
|
800
|
-
this.setAddress(address);
|
|
801
|
-
this.setState(tronwallet_abstract_adapter_1.AdapterState.Connected);
|
|
802
|
-
}
|
|
803
|
-
else {
|
|
804
|
-
this.setAddress(null);
|
|
805
|
-
this.setState(tronwallet_abstract_adapter_1.AdapterState.Disconnect);
|
|
806
|
-
}
|
|
807
|
-
this.emit('accountsChanged', this.address || '', preAddr);
|
|
808
|
-
if (!preAddr && this.address) {
|
|
809
|
-
this.emit('connect', this.address);
|
|
810
|
-
}
|
|
811
|
-
else if (preAddr && !this.address) {
|
|
812
|
-
this.emit('disconnect');
|
|
813
|
-
}
|
|
814
|
-
}, 200);
|
|
815
|
-
}
|
|
816
|
-
else if (message.action === 'setNode') {
|
|
817
|
-
this.emit('chainChanged', { chainId: ((_c = (_b = message.data) === null || _b === void 0 ? void 0 : _b.node) === null || _c === void 0 ? void 0 : _c.chainId) || '' });
|
|
818
|
-
}
|
|
819
|
-
else if (message.action === 'connect') {
|
|
820
|
-
const address = ((_e = (_d = this._wallet.tronWeb) === null || _d === void 0 ? void 0 : _d.defaultAddress) === null || _e === void 0 ? void 0 : _e.base58) || '';
|
|
821
|
-
this.setAddress(address);
|
|
822
|
-
this.setState(tronwallet_abstract_adapter_1.AdapterState.Connected);
|
|
823
|
-
this.emit('connect', address);
|
|
824
|
-
}
|
|
825
|
-
else if (message.action === 'disconnect') {
|
|
826
|
-
this.setAddress(null);
|
|
827
|
-
this.setState(tronwallet_abstract_adapter_1.AdapterState.Disconnect);
|
|
828
|
-
this.emit('disconnect');
|
|
829
|
-
}
|
|
830
|
-
};
|
|
831
|
-
this._onChainChanged = (data) => {
|
|
832
|
-
this.emit('chainChanged', data);
|
|
833
|
-
};
|
|
834
|
-
this._onAccountsChanged = () => {
|
|
835
|
-
var _a, _b, _c;
|
|
836
|
-
const preAddr = this.address || '';
|
|
837
|
-
const curAddr = (((_a = this._wallet) === null || _a === void 0 ? void 0 : _a.tronWeb) && ((_c = (_b = this._wallet) === null || _b === void 0 ? void 0 : _b.tronWeb.defaultAddress) === null || _c === void 0 ? void 0 : _c.base58)) || '';
|
|
838
|
-
if (!curAddr) {
|
|
839
|
-
// change to a new address and if it's disconnected, data will be empty
|
|
840
|
-
// tronlink will emit accountsChanged many times, only process when connected
|
|
841
|
-
this.setAddress(null);
|
|
842
|
-
this.setState(tronwallet_abstract_adapter_1.AdapterState.Disconnect);
|
|
843
|
-
}
|
|
844
|
-
else {
|
|
845
|
-
const address = curAddr;
|
|
846
|
-
this.setAddress(address);
|
|
847
|
-
this.setState(tronwallet_abstract_adapter_1.AdapterState.Connected);
|
|
848
|
-
}
|
|
849
|
-
this.emit('accountsChanged', this.address || '', preAddr);
|
|
850
|
-
if (!preAddr && this.address) {
|
|
851
|
-
this.emit('connect', this.address);
|
|
852
|
-
}
|
|
853
|
-
else if (preAddr && !this.address) {
|
|
854
|
-
this.emit('disconnect');
|
|
855
|
-
}
|
|
856
|
-
};
|
|
857
|
-
this._checkPromise = null;
|
|
858
|
-
this._updateWallet = () => {
|
|
859
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
860
|
-
let state = this.state;
|
|
861
|
-
let address = this.address;
|
|
862
|
-
if ((0, tronwallet_abstract_adapter_1.isInMobileBrowser)()) {
|
|
863
|
-
if (window.tronLink) {
|
|
864
|
-
this._wallet = window.tronLink;
|
|
865
|
-
}
|
|
866
|
-
else {
|
|
867
|
-
this._wallet = {
|
|
868
|
-
ready: !!((_a = window.tronWeb) === null || _a === void 0 ? void 0 : _a.defaultAddress),
|
|
869
|
-
tronWeb: window.tronWeb,
|
|
870
|
-
request: () => Promise.resolve(true),
|
|
871
|
-
};
|
|
872
|
-
}
|
|
873
|
-
address = ((_c = (_b = this._wallet.tronWeb) === null || _b === void 0 ? void 0 : _b.defaultAddress) === null || _c === void 0 ? void 0 : _c.base58) || null;
|
|
874
|
-
state = address ? tronwallet_abstract_adapter_1.AdapterState.Connected : tronwallet_abstract_adapter_1.AdapterState.Disconnect;
|
|
875
|
-
}
|
|
876
|
-
else if (window.tron && window.tron.isTronLink) {
|
|
877
|
-
this._supportNewTronProtocol = true;
|
|
878
|
-
this._wallet = window.tron;
|
|
879
|
-
this._listenTronEvent();
|
|
880
|
-
address = (this._wallet.tronWeb && ((_e = (_d = this._wallet.tronWeb) === null || _d === void 0 ? void 0 : _d.defaultAddress) === null || _e === void 0 ? void 0 : _e.base58)) || null;
|
|
881
|
-
state = address ? tronwallet_abstract_adapter_1.AdapterState.Connected : tronwallet_abstract_adapter_1.AdapterState.Disconnect;
|
|
882
|
-
}
|
|
883
|
-
else if (window.tronLink) {
|
|
884
|
-
this._wallet = window.tronLink;
|
|
885
|
-
this._listenTronLinkEvent();
|
|
886
|
-
address = ((_g = (_f = this._wallet.tronWeb) === null || _f === void 0 ? void 0 : _f.defaultAddress) === null || _g === void 0 ? void 0 : _g.base58) || null;
|
|
887
|
-
state = this._wallet.ready ? tronwallet_abstract_adapter_1.AdapterState.Connected : tronwallet_abstract_adapter_1.AdapterState.Disconnect;
|
|
888
|
-
}
|
|
889
|
-
else if (window.tronWeb) {
|
|
890
|
-
// fake tronLink
|
|
891
|
-
this._wallet = {
|
|
892
|
-
ready: window.tronWeb.ready,
|
|
893
|
-
tronWeb: window.tronWeb,
|
|
894
|
-
request: () => Promise.resolve(true),
|
|
895
|
-
};
|
|
896
|
-
address = ((_h = this._wallet.tronWeb.defaultAddress) === null || _h === void 0 ? void 0 : _h.base58) || null;
|
|
897
|
-
state = this._wallet.ready ? tronwallet_abstract_adapter_1.AdapterState.Connected : tronwallet_abstract_adapter_1.AdapterState.Disconnect;
|
|
898
|
-
}
|
|
899
|
-
else {
|
|
900
|
-
// no tronlink support
|
|
901
|
-
this._wallet = null;
|
|
902
|
-
address = null;
|
|
903
|
-
state = tronwallet_abstract_adapter_1.AdapterState.NotFound;
|
|
904
|
-
}
|
|
905
|
-
// In TronLink App, account should be connected
|
|
906
|
-
if ((0, tronwallet_abstract_adapter_1.isInMobileBrowser)() && state === tronwallet_abstract_adapter_1.AdapterState.Disconnect) {
|
|
907
|
-
this.checkForWalletReadyForApp();
|
|
908
|
-
}
|
|
909
|
-
this.setAddress(address);
|
|
910
|
-
this.setState(state);
|
|
911
|
-
};
|
|
912
|
-
this.checkReadyInterval = null;
|
|
913
|
-
const { checkTimeout = 30 * 1000, dappIcon = '', dappName = '', openUrlWhenWalletNotFound = true, openTronLinkAppOnMobile = true, } = config;
|
|
914
|
-
if (typeof checkTimeout !== 'number') {
|
|
915
|
-
throw new Error('[TronLinkAdapter] config.checkTimeout should be a number');
|
|
916
|
-
}
|
|
917
|
-
this.config = {
|
|
918
|
-
checkTimeout,
|
|
919
|
-
openTronLinkAppOnMobile,
|
|
920
|
-
openUrlWhenWalletNotFound,
|
|
921
|
-
dappIcon,
|
|
922
|
-
dappName,
|
|
923
|
-
};
|
|
924
|
-
this._connecting = false;
|
|
925
|
-
this._wallet = null;
|
|
926
|
-
this._address = null;
|
|
927
|
-
if (!(0, tronwallet_abstract_adapter_1.isInBrowser)()) {
|
|
928
|
-
this._readyState = tronwallet_abstract_adapter_1.WalletReadyState.NotFound;
|
|
929
|
-
this.setState(tronwallet_abstract_adapter_1.AdapterState.NotFound);
|
|
930
|
-
return;
|
|
931
|
-
}
|
|
932
|
-
if ((0, utils_js_1.supportTron)() || ((0, tronwallet_abstract_adapter_1.isInMobileBrowser)() && (window.tronLink || window.tronWeb))) {
|
|
933
|
-
this._readyState = tronwallet_abstract_adapter_1.WalletReadyState.Found;
|
|
934
|
-
this._updateWallet();
|
|
935
|
-
}
|
|
936
|
-
else {
|
|
937
|
-
this._checkWallet().then(() => {
|
|
938
|
-
if (this.connected) {
|
|
939
|
-
this.emit('connect', this.address || '');
|
|
940
|
-
}
|
|
941
|
-
});
|
|
942
|
-
}
|
|
943
|
-
}
|
|
944
|
-
get address() {
|
|
945
|
-
return this._address;
|
|
946
|
-
}
|
|
947
|
-
get state() {
|
|
948
|
-
return this._state;
|
|
949
|
-
}
|
|
950
|
-
get readyState() {
|
|
951
|
-
return this._readyState;
|
|
952
|
-
}
|
|
953
|
-
get connecting() {
|
|
954
|
-
return this._connecting;
|
|
955
|
-
}
|
|
956
|
-
/**
|
|
957
|
-
* Get network information used by TronLink.
|
|
958
|
-
* @returns {Network} Current network information.
|
|
959
|
-
*/
|
|
960
|
-
network() {
|
|
961
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
962
|
-
var _a;
|
|
963
|
-
try {
|
|
964
|
-
yield this._checkWallet();
|
|
965
|
-
if (this.state !== tronwallet_abstract_adapter_1.AdapterState.Connected)
|
|
966
|
-
throw new tronwallet_abstract_adapter_1.WalletDisconnectedError();
|
|
967
|
-
const tronWeb = ((_a = this._wallet) === null || _a === void 0 ? void 0 : _a.tronWeb) || window.tronWeb;
|
|
968
|
-
if (!tronWeb)
|
|
969
|
-
throw new tronwallet_abstract_adapter_1.WalletDisconnectedError();
|
|
970
|
-
try {
|
|
971
|
-
return yield getNetworkInfoByTronWeb(tronWeb);
|
|
972
|
-
}
|
|
973
|
-
catch (e) {
|
|
974
|
-
throw new tronwallet_abstract_adapter_1.WalletGetNetworkError(e === null || e === void 0 ? void 0 : e.message, e);
|
|
975
|
-
}
|
|
976
|
-
}
|
|
977
|
-
catch (e) {
|
|
978
|
-
this.emit('error', e);
|
|
979
|
-
throw e;
|
|
980
|
-
}
|
|
981
|
-
});
|
|
982
|
-
}
|
|
983
|
-
connect() {
|
|
984
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
985
|
-
var _a, _b;
|
|
986
|
-
try {
|
|
987
|
-
this.checkIfOpenTronLink();
|
|
988
|
-
if (this.connected || this.connecting)
|
|
989
|
-
return;
|
|
990
|
-
yield this._checkWallet();
|
|
991
|
-
if (this.state === tronwallet_abstract_adapter_1.AdapterState.NotFound) {
|
|
992
|
-
if (this.config.openUrlWhenWalletNotFound !== false && (0, tronwallet_abstract_adapter_1.isInBrowser)()) {
|
|
993
|
-
window.open(this.url, '_blank');
|
|
994
|
-
}
|
|
995
|
-
throw new tronwallet_abstract_adapter_1.WalletNotFoundError();
|
|
996
|
-
}
|
|
997
|
-
// lower version only support window.tronWeb, no window.tronLink
|
|
998
|
-
if (!this._wallet)
|
|
999
|
-
return;
|
|
1000
|
-
this._connecting = true;
|
|
1001
|
-
if (this._supportNewTronProtocol) {
|
|
1002
|
-
const wallet = this._wallet;
|
|
1003
|
-
try {
|
|
1004
|
-
const res = yield wallet.request({ method: 'eth_requestAccounts' });
|
|
1005
|
-
const address = res[0];
|
|
1006
|
-
this.setAddress(address);
|
|
1007
|
-
this.setState(tronwallet_abstract_adapter_1.AdapterState.Connected);
|
|
1008
|
-
this._listenTronEvent();
|
|
1009
|
-
if (!this._wallet.tronWeb) {
|
|
1010
|
-
yield (0, utils_js_1.waitTronwebReady)(this._wallet);
|
|
1011
|
-
}
|
|
1012
|
-
}
|
|
1013
|
-
catch (error) {
|
|
1014
|
-
let message = (error === null || error === void 0 ? void 0 : error.message) || error || 'Connect TronLink wallet failed.';
|
|
1015
|
-
if (error.code === -32002) {
|
|
1016
|
-
message =
|
|
1017
|
-
'The same DApp has already initiated a request to connect to TronLink wallet, and the pop-up window has not been closed.';
|
|
1018
|
-
}
|
|
1019
|
-
if (error.code === 4001) {
|
|
1020
|
-
message = 'The user rejected connection.';
|
|
1021
|
-
}
|
|
1022
|
-
throw new tronwallet_abstract_adapter_1.WalletConnectionError(message, error);
|
|
1023
|
-
}
|
|
1024
|
-
}
|
|
1025
|
-
else if (window.tronLink) {
|
|
1026
|
-
const wallet = this._wallet;
|
|
1027
|
-
try {
|
|
1028
|
-
const res = yield wallet.request({ method: 'tron_requestAccounts' });
|
|
1029
|
-
if (!res) {
|
|
1030
|
-
// 1. wallet is locked
|
|
1031
|
-
// 2. tronlink is first installed and there is no wallet account
|
|
1032
|
-
throw new tronwallet_abstract_adapter_1.WalletConnectionError('TronLink wallet is locked or no wallet account is avaliable.');
|
|
1033
|
-
}
|
|
1034
|
-
if (res.code === 4000) {
|
|
1035
|
-
throw new tronwallet_abstract_adapter_1.WalletConnectionError('The same DApp has already initiated a request to connect to TronLink wallet, and the pop-up window has not been closed.');
|
|
1036
|
-
}
|
|
1037
|
-
if (res.code === 4001) {
|
|
1038
|
-
throw new tronwallet_abstract_adapter_1.WalletConnectionError('The user rejected connection.');
|
|
1039
|
-
}
|
|
1040
|
-
}
|
|
1041
|
-
catch (error) {
|
|
1042
|
-
throw new tronwallet_abstract_adapter_1.WalletConnectionError(error === null || error === void 0 ? void 0 : error.message, error);
|
|
1043
|
-
}
|
|
1044
|
-
const address = ((_a = wallet.tronWeb.defaultAddress) === null || _a === void 0 ? void 0 : _a.base58) || '';
|
|
1045
|
-
this.setAddress(address);
|
|
1046
|
-
this.setState(tronwallet_abstract_adapter_1.AdapterState.Connected);
|
|
1047
|
-
this._listenTronLinkEvent();
|
|
1048
|
-
}
|
|
1049
|
-
else if (window.tronWeb) {
|
|
1050
|
-
const wallet = this._wallet;
|
|
1051
|
-
const address = ((_b = wallet.tronWeb.defaultAddress) === null || _b === void 0 ? void 0 : _b.base58) || '';
|
|
1052
|
-
this.setAddress(address);
|
|
1053
|
-
this.setState(tronwallet_abstract_adapter_1.AdapterState.Connected);
|
|
1054
|
-
}
|
|
1055
|
-
else {
|
|
1056
|
-
throw new tronwallet_abstract_adapter_1.WalletConnectionError('Cannot connect wallet.');
|
|
1057
|
-
}
|
|
1058
|
-
this.connected && this.emit('connect', this.address || '');
|
|
1059
|
-
}
|
|
1060
|
-
catch (error) {
|
|
1061
|
-
this.emit('error', error);
|
|
1062
|
-
throw error;
|
|
1063
|
-
}
|
|
1064
|
-
finally {
|
|
1065
|
-
this._connecting = false;
|
|
1066
|
-
}
|
|
1067
|
-
});
|
|
1068
|
-
}
|
|
1069
|
-
disconnect() {
|
|
1070
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1071
|
-
if (this._supportNewTronProtocol) {
|
|
1072
|
-
this._stopListenTronEvent();
|
|
1073
|
-
}
|
|
1074
|
-
else {
|
|
1075
|
-
this._stopListenTronLinkEvent();
|
|
1076
|
-
}
|
|
1077
|
-
if (this.state !== tronwallet_abstract_adapter_1.AdapterState.Connected) {
|
|
1078
|
-
return;
|
|
1079
|
-
}
|
|
1080
|
-
this.setAddress(null);
|
|
1081
|
-
this.setState(tronwallet_abstract_adapter_1.AdapterState.Disconnect);
|
|
1082
|
-
this.emit('disconnect');
|
|
1083
|
-
});
|
|
1084
|
-
}
|
|
1085
|
-
signTransaction(transaction, privateKey) {
|
|
1086
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1087
|
-
try {
|
|
1088
|
-
const wallet = yield this.checkAndGetWallet();
|
|
1089
|
-
try {
|
|
1090
|
-
return yield wallet.tronWeb.trx.sign(transaction, privateKey);
|
|
1091
|
-
}
|
|
1092
|
-
catch (error) {
|
|
1093
|
-
if (error instanceof Error) {
|
|
1094
|
-
throw new tronwallet_abstract_adapter_1.WalletSignTransactionError(error.message, error);
|
|
1095
|
-
}
|
|
1096
|
-
else {
|
|
1097
|
-
throw new tronwallet_abstract_adapter_1.WalletSignTransactionError(error, new Error(error));
|
|
1098
|
-
}
|
|
1099
|
-
}
|
|
1100
|
-
}
|
|
1101
|
-
catch (error) {
|
|
1102
|
-
this.emit('error', error);
|
|
1103
|
-
throw error;
|
|
1104
|
-
}
|
|
1105
|
-
});
|
|
1106
|
-
}
|
|
1107
|
-
multiSign(transaction, privateKey, permissionId) {
|
|
1108
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1109
|
-
try {
|
|
1110
|
-
const wallet = yield this.checkAndGetWallet();
|
|
1111
|
-
try {
|
|
1112
|
-
return yield wallet.tronWeb.trx.multiSign(transaction, privateKey, permissionId);
|
|
1113
|
-
}
|
|
1114
|
-
catch (error) {
|
|
1115
|
-
if (error instanceof Error) {
|
|
1116
|
-
throw new tronwallet_abstract_adapter_1.WalletSignTransactionError(error.message, error);
|
|
1117
|
-
}
|
|
1118
|
-
else {
|
|
1119
|
-
throw new tronwallet_abstract_adapter_1.WalletSignTransactionError(error, new Error(error));
|
|
1120
|
-
}
|
|
1121
|
-
}
|
|
1122
|
-
}
|
|
1123
|
-
catch (error) {
|
|
1124
|
-
this.emit('error', error);
|
|
1125
|
-
throw error;
|
|
1126
|
-
}
|
|
1127
|
-
});
|
|
1128
|
-
}
|
|
1129
|
-
signMessage(message, privateKey) {
|
|
1130
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1131
|
-
try {
|
|
1132
|
-
const wallet = yield this.checkAndGetWallet();
|
|
1133
|
-
try {
|
|
1134
|
-
return yield wallet.tronWeb.trx.signMessageV2(message, privateKey);
|
|
1135
|
-
}
|
|
1136
|
-
catch (error) {
|
|
1137
|
-
if (error instanceof Error) {
|
|
1138
|
-
throw new tronwallet_abstract_adapter_1.WalletSignMessageError(error.message, error);
|
|
1139
|
-
}
|
|
1140
|
-
else {
|
|
1141
|
-
throw new tronwallet_abstract_adapter_1.WalletSignMessageError(error, new Error(error));
|
|
1142
|
-
}
|
|
1143
|
-
}
|
|
1144
|
-
}
|
|
1145
|
-
catch (error) {
|
|
1146
|
-
this.emit('error', error);
|
|
1147
|
-
throw error;
|
|
1148
|
-
}
|
|
1149
|
-
});
|
|
1150
|
-
}
|
|
1151
|
-
/**
|
|
1152
|
-
* Switch to target chain. If current chain is the same as target chain, the call will success immediately.
|
|
1153
|
-
* Available chainIds:
|
|
1154
|
-
* - Mainnet: 0x2b6653dc
|
|
1155
|
-
* - Shasta: 0x94a9059e
|
|
1156
|
-
* - Nile: 0xcd8690dc
|
|
1157
|
-
* @param chainId chainId
|
|
1158
|
-
*/
|
|
1159
|
-
switchChain(chainId) {
|
|
1160
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1161
|
-
try {
|
|
1162
|
-
yield this._checkWallet();
|
|
1163
|
-
if (this.state === tronwallet_abstract_adapter_1.AdapterState.NotFound) {
|
|
1164
|
-
if (this.config.openUrlWhenWalletNotFound !== false && (0, tronwallet_abstract_adapter_1.isInBrowser)()) {
|
|
1165
|
-
window.open(this.url, '_blank');
|
|
1166
|
-
}
|
|
1167
|
-
throw new tronwallet_abstract_adapter_1.WalletNotFoundError();
|
|
1168
|
-
}
|
|
1169
|
-
if (!this._supportNewTronProtocol) {
|
|
1170
|
-
throw new tronwallet_abstract_adapter_1.WalletSwitchChainError("Current version of TronLink doesn't support switch chain operation.");
|
|
1171
|
-
}
|
|
1172
|
-
const wallet = this._wallet;
|
|
1173
|
-
try {
|
|
1174
|
-
yield wallet.request({
|
|
1175
|
-
method: 'wallet_switchEthereumChain',
|
|
1176
|
-
params: [{ chainId }],
|
|
1177
|
-
});
|
|
1178
|
-
}
|
|
1179
|
-
catch (e) {
|
|
1180
|
-
throw new tronwallet_abstract_adapter_1.WalletSwitchChainError((e === null || e === void 0 ? void 0 : e.message) || e, e instanceof Error ? e : new Error(e));
|
|
1181
|
-
}
|
|
1182
|
-
}
|
|
1183
|
-
catch (error) {
|
|
1184
|
-
this.emit('error', error);
|
|
1185
|
-
throw error;
|
|
729
|
+
function supportTronLink() {
|
|
730
|
+
return !!(supportTron() || window.tronLink || window.tronWeb);
|
|
731
|
+
}
|
|
732
|
+
/**
|
|
733
|
+
* Detect if in TronLinkApp
|
|
734
|
+
* Tron DApp running in the DApp Explorer injects iTron objects automatically to offer customized App service.
|
|
735
|
+
* See [here](https://docs.tronlink.org/tronlink-app/dapp-support/dapp-explorer)
|
|
736
|
+
*/
|
|
737
|
+
function isInTronLinkApp() {
|
|
738
|
+
return (0, tronwallet_abstract_adapter_1.isInBrowser)() && typeof window.iTron !== 'undefined';
|
|
739
|
+
}
|
|
740
|
+
function openTronLink({ dappIcon, dappName } = { dappIcon: '', dappName: '' }) {
|
|
741
|
+
if (!supportTronLink() && (0, tronwallet_abstract_adapter_1.isInMobileBrowser)() && !isInTronLinkApp()) {
|
|
742
|
+
let defaultDappName = '', defaultDappIcon = '';
|
|
743
|
+
try {
|
|
744
|
+
defaultDappName = document.title;
|
|
745
|
+
const link = document.querySelector('link[rel*="icon"]');
|
|
746
|
+
if (link) {
|
|
747
|
+
defaultDappIcon = new URL(link.getAttribute('href') || '', location.href).toString();
|
|
1186
748
|
}
|
|
1187
|
-
});
|
|
1188
|
-
}
|
|
1189
|
-
checkAndGetWallet() {
|
|
1190
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
1191
|
-
this.checkIfOpenTronLink();
|
|
1192
|
-
yield this._checkWallet();
|
|
1193
|
-
if (this.state !== tronwallet_abstract_adapter_1.AdapterState.Connected)
|
|
1194
|
-
throw new tronwallet_abstract_adapter_1.WalletDisconnectedError();
|
|
1195
|
-
const wallet = this._wallet;
|
|
1196
|
-
if (!wallet || !wallet.tronWeb)
|
|
1197
|
-
throw new tronwallet_abstract_adapter_1.WalletDisconnectedError();
|
|
1198
|
-
return wallet;
|
|
1199
|
-
});
|
|
1200
|
-
}
|
|
1201
|
-
_listenTronLinkEvent() {
|
|
1202
|
-
this._stopListenTronLinkEvent();
|
|
1203
|
-
window.addEventListener('message', this._tronLinkMessageHandler);
|
|
1204
|
-
}
|
|
1205
|
-
_stopListenTronLinkEvent() {
|
|
1206
|
-
window.removeEventListener('message', this._tronLinkMessageHandler);
|
|
1207
|
-
}
|
|
1208
|
-
checkIfOpenTronLink() {
|
|
1209
|
-
const { dappName = '', dappIcon = '' } = this.config;
|
|
1210
|
-
if (this.config.openTronLinkAppOnMobile === false) {
|
|
1211
|
-
return;
|
|
1212
749
|
}
|
|
1213
|
-
|
|
1214
|
-
|
|
750
|
+
catch (e) {
|
|
751
|
+
// console.error(e);
|
|
1215
752
|
}
|
|
753
|
+
const { origin, pathname, search, hash } = window.location;
|
|
754
|
+
const url = origin + pathname + search + (hash.includes('?') ? hash : `${hash}?_=1`);
|
|
755
|
+
const params = {
|
|
756
|
+
action: 'open',
|
|
757
|
+
actionId: Date.now() + '',
|
|
758
|
+
callbackUrl: 'http://someurl.com', // no need callback
|
|
759
|
+
dappIcon: dappIcon || defaultDappIcon,
|
|
760
|
+
dappName: dappName || defaultDappName,
|
|
761
|
+
url,
|
|
762
|
+
protocol: 'TronLink',
|
|
763
|
+
version: '1.0',
|
|
764
|
+
chainId: '0x2b6653dc',
|
|
765
|
+
};
|
|
766
|
+
window.location.href = `tronlinkoutside://pull.activity?param=${encodeURIComponent(JSON.stringify(params))}`;
|
|
767
|
+
return true;
|
|
1216
768
|
}
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
wallet.removeListener('chainChanged', this._onChainChanged);
|
|
1228
|
-
wallet.removeListener('accountsChanged', this._onAccountsChanged);
|
|
1229
|
-
}
|
|
1230
|
-
/**
|
|
1231
|
-
* check if wallet exists by interval, the promise only resolve when wallet detected or timeout
|
|
1232
|
-
* @returns if wallet exists
|
|
1233
|
-
*/
|
|
1234
|
-
_checkWallet() {
|
|
1235
|
-
if (this.readyState === tronwallet_abstract_adapter_1.WalletReadyState.Found) {
|
|
1236
|
-
return Promise.resolve(true);
|
|
1237
|
-
}
|
|
1238
|
-
if (this._checkPromise) {
|
|
1239
|
-
return this._checkPromise;
|
|
1240
|
-
}
|
|
1241
|
-
const interval = 100;
|
|
1242
|
-
const checkTronTimes = Math.floor(2000 / interval);
|
|
1243
|
-
const maxTimes = Math.floor(this.config.checkTimeout / interval);
|
|
1244
|
-
let times = 0, timer;
|
|
1245
|
-
this._checkPromise = new Promise((resolve) => {
|
|
1246
|
-
const check = () => {
|
|
1247
|
-
times++;
|
|
1248
|
-
const isSupport = times < checkTronTimes && !(0, tronwallet_abstract_adapter_1.isInMobileBrowser)() ? (0, utils_js_1.supportTron)() : (0, utils_js_1.supportTronLink)();
|
|
1249
|
-
if (isSupport || times > maxTimes) {
|
|
1250
|
-
timer && clearInterval(timer);
|
|
1251
|
-
this._readyState = isSupport ? tronwallet_abstract_adapter_1.WalletReadyState.Found : tronwallet_abstract_adapter_1.WalletReadyState.NotFound;
|
|
1252
|
-
this._updateWallet();
|
|
1253
|
-
this.emit('readyStateChanged', this.readyState);
|
|
1254
|
-
resolve(isSupport);
|
|
769
|
+
return false;
|
|
770
|
+
}
|
|
771
|
+
function waitTronwebReady(tronObj) {
|
|
772
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
773
|
+
return new Promise((resolve, reject) => {
|
|
774
|
+
const interval = setInterval(() => {
|
|
775
|
+
if (tronObj.tronWeb) {
|
|
776
|
+
clearInterval(interval);
|
|
777
|
+
clearTimeout(timeout);
|
|
778
|
+
resolve();
|
|
1255
779
|
}
|
|
1256
|
-
};
|
|
1257
|
-
|
|
1258
|
-
|
|
780
|
+
}, 50);
|
|
781
|
+
const timeout = setTimeout(() => {
|
|
782
|
+
clearInterval(interval);
|
|
783
|
+
reject('`window.tron.tronweb` is not ready.');
|
|
784
|
+
}, 2000);
|
|
1259
785
|
});
|
|
1260
|
-
|
|
1261
|
-
}
|
|
1262
|
-
checkForWalletReadyForApp() {
|
|
1263
|
-
if (this.checkReadyInterval) {
|
|
1264
|
-
return;
|
|
1265
|
-
}
|
|
1266
|
-
let times = 0;
|
|
1267
|
-
const maxTimes = Math.floor(this.config.checkTimeout / 200);
|
|
1268
|
-
const check = () => {
|
|
1269
|
-
var _a, _b;
|
|
1270
|
-
if (window.tronLink ? (_a = window.tronLink.tronWeb) === null || _a === void 0 ? void 0 : _a.defaultAddress : (_b = window.tronWeb) === null || _b === void 0 ? void 0 : _b.defaultAddress) {
|
|
1271
|
-
this.checkReadyInterval && clearInterval(this.checkReadyInterval);
|
|
1272
|
-
this.checkReadyInterval = null;
|
|
1273
|
-
this._updateWallet();
|
|
1274
|
-
this.emit('connect', this.address || '');
|
|
1275
|
-
}
|
|
1276
|
-
else if (times > maxTimes) {
|
|
1277
|
-
this.checkReadyInterval && clearInterval(this.checkReadyInterval);
|
|
1278
|
-
this.checkReadyInterval = null;
|
|
1279
|
-
}
|
|
1280
|
-
else {
|
|
1281
|
-
times++;
|
|
1282
|
-
}
|
|
1283
|
-
};
|
|
1284
|
-
this.checkReadyInterval = setInterval(check, 200);
|
|
1285
|
-
}
|
|
1286
|
-
setAddress(address) {
|
|
1287
|
-
this._address = address;
|
|
1288
|
-
}
|
|
1289
|
-
setState(state) {
|
|
1290
|
-
const preState = this.state;
|
|
1291
|
-
if (state !== preState) {
|
|
1292
|
-
this._state = state;
|
|
1293
|
-
this.emit('stateChanged', state);
|
|
1294
|
-
}
|
|
1295
|
-
}
|
|
786
|
+
});
|
|
1296
787
|
}
|
|
1297
|
-
exports.TronLinkAdapter = TronLinkAdapter;
|
|
1298
788
|
|
|
1299
|
-
|
|
789
|
+
return utils$1;
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
var hasRequiredAdapter$1;
|
|
793
|
+
|
|
794
|
+
function requireAdapter$1 () {
|
|
795
|
+
if (hasRequiredAdapter$1) return adapter;
|
|
796
|
+
hasRequiredAdapter$1 = 1;
|
|
797
|
+
(function (exports) {
|
|
798
|
+
var __awaiter = (adapter && adapter.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
799
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
800
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
801
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
802
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
803
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
804
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
805
|
+
});
|
|
806
|
+
};
|
|
807
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
808
|
+
exports.TronLinkAdapter = exports.TronLinkAdapterName = exports.chainIdNetworkMap = void 0;
|
|
809
|
+
exports.getNetworkInfoByTronWeb = getNetworkInfoByTronWeb;
|
|
810
|
+
const tronwallet_abstract_adapter_1 = /*@__PURE__*/ requireCjs$2();
|
|
811
|
+
const utils_js_1 = /*@__PURE__*/ requireUtils$1();
|
|
812
|
+
exports.chainIdNetworkMap = {
|
|
813
|
+
'0x2b6653dc': tronwallet_abstract_adapter_1.NetworkType.Mainnet,
|
|
814
|
+
'0x94a9059e': tronwallet_abstract_adapter_1.NetworkType.Shasta,
|
|
815
|
+
'0xcd8690dc': tronwallet_abstract_adapter_1.NetworkType.Nile,
|
|
816
|
+
};
|
|
817
|
+
function getNetworkInfoByTronWeb(tronWeb) {
|
|
818
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
819
|
+
var _a, _b, _c;
|
|
820
|
+
const { blockID = '' } = yield tronWeb.trx.getBlockByNumber(0);
|
|
821
|
+
const chainId = `0x${blockID.slice(-8)}`;
|
|
822
|
+
return {
|
|
823
|
+
networkType: exports.chainIdNetworkMap[chainId] || tronwallet_abstract_adapter_1.NetworkType.Unknown,
|
|
824
|
+
chainId,
|
|
825
|
+
fullNode: ((_a = tronWeb.fullNode) === null || _a === void 0 ? void 0 : _a.host) || '',
|
|
826
|
+
solidityNode: ((_b = tronWeb.solidityNode) === null || _b === void 0 ? void 0 : _b.host) || '',
|
|
827
|
+
eventServer: ((_c = tronWeb.eventServer) === null || _c === void 0 ? void 0 : _c.host) || '',
|
|
828
|
+
};
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
exports.TronLinkAdapterName = 'TronLink';
|
|
832
|
+
class TronLinkAdapter extends tronwallet_abstract_adapter_1.Adapter {
|
|
833
|
+
// record if first connect event has emitted or not
|
|
834
|
+
constructor(config = {}) {
|
|
835
|
+
super();
|
|
836
|
+
this.name = exports.TronLinkAdapterName;
|
|
837
|
+
this.url = 'https://www.tronlink.org/';
|
|
838
|
+
this.icon = 'data:image/png;base64,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';
|
|
839
|
+
this._readyState = (0, tronwallet_abstract_adapter_1.isInBrowser)() ? tronwallet_abstract_adapter_1.WalletReadyState.Loading : tronwallet_abstract_adapter_1.WalletReadyState.NotFound;
|
|
840
|
+
this._state = tronwallet_abstract_adapter_1.AdapterState.Loading;
|
|
841
|
+
// https://github.com/tronprotocol/tips/blob/master/tip-1193.md
|
|
842
|
+
this._supportNewTronProtocol = false;
|
|
843
|
+
this._tronLinkMessageHandler = (e) => {
|
|
844
|
+
var _a, _b, _c, _d, _e;
|
|
845
|
+
const message = (_a = e.data) === null || _a === void 0 ? void 0 : _a.message;
|
|
846
|
+
if (!message) {
|
|
847
|
+
return;
|
|
848
|
+
}
|
|
849
|
+
if (message.action === 'accountsChanged') {
|
|
850
|
+
setTimeout(() => {
|
|
851
|
+
var _a;
|
|
852
|
+
const preAddr = this.address || '';
|
|
853
|
+
if ((_a = this._wallet) === null || _a === void 0 ? void 0 : _a.ready) {
|
|
854
|
+
const address = message.data.address;
|
|
855
|
+
this.setAddress(address);
|
|
856
|
+
this.setState(tronwallet_abstract_adapter_1.AdapterState.Connected);
|
|
857
|
+
}
|
|
858
|
+
else {
|
|
859
|
+
this.setAddress(null);
|
|
860
|
+
this.setState(tronwallet_abstract_adapter_1.AdapterState.Disconnect);
|
|
861
|
+
}
|
|
862
|
+
this.emit('accountsChanged', this.address || '', preAddr);
|
|
863
|
+
if (!preAddr && this.address) {
|
|
864
|
+
this.emit('connect', this.address);
|
|
865
|
+
}
|
|
866
|
+
else if (preAddr && !this.address) {
|
|
867
|
+
this.emit('disconnect');
|
|
868
|
+
}
|
|
869
|
+
}, 200);
|
|
870
|
+
}
|
|
871
|
+
else if (message.action === 'setNode') {
|
|
872
|
+
this.emit('chainChanged', { chainId: ((_c = (_b = message.data) === null || _b === void 0 ? void 0 : _b.node) === null || _c === void 0 ? void 0 : _c.chainId) || '' });
|
|
873
|
+
}
|
|
874
|
+
else if (message.action === 'connect') {
|
|
875
|
+
const address = ((_e = (_d = this._wallet.tronWeb) === null || _d === void 0 ? void 0 : _d.defaultAddress) === null || _e === void 0 ? void 0 : _e.base58) || '';
|
|
876
|
+
this.setAddress(address);
|
|
877
|
+
this.setState(tronwallet_abstract_adapter_1.AdapterState.Connected);
|
|
878
|
+
this.emit('connect', address);
|
|
879
|
+
}
|
|
880
|
+
else if (message.action === 'disconnect') {
|
|
881
|
+
this.setAddress(null);
|
|
882
|
+
this.setState(tronwallet_abstract_adapter_1.AdapterState.Disconnect);
|
|
883
|
+
this.emit('disconnect');
|
|
884
|
+
}
|
|
885
|
+
};
|
|
886
|
+
this._onChainChanged = (data) => {
|
|
887
|
+
this.emit('chainChanged', data);
|
|
888
|
+
};
|
|
889
|
+
this._onAccountsChanged = () => {
|
|
890
|
+
var _a, _b, _c;
|
|
891
|
+
const preAddr = this.address || '';
|
|
892
|
+
const curAddr = (((_a = this._wallet) === null || _a === void 0 ? void 0 : _a.tronWeb) && ((_c = (_b = this._wallet) === null || _b === void 0 ? void 0 : _b.tronWeb.defaultAddress) === null || _c === void 0 ? void 0 : _c.base58)) || '';
|
|
893
|
+
if (!curAddr) {
|
|
894
|
+
// change to a new address and if it's disconnected, data will be empty
|
|
895
|
+
// tronlink will emit accountsChanged many times, only process when connected
|
|
896
|
+
this.setAddress(null);
|
|
897
|
+
this.setState(tronwallet_abstract_adapter_1.AdapterState.Disconnect);
|
|
898
|
+
}
|
|
899
|
+
else {
|
|
900
|
+
const address = curAddr;
|
|
901
|
+
this.setAddress(address);
|
|
902
|
+
this.setState(tronwallet_abstract_adapter_1.AdapterState.Connected);
|
|
903
|
+
}
|
|
904
|
+
this.emit('accountsChanged', this.address || '', preAddr);
|
|
905
|
+
if (!preAddr && this.address) {
|
|
906
|
+
this.emit('connect', this.address);
|
|
907
|
+
}
|
|
908
|
+
else if (preAddr && !this.address) {
|
|
909
|
+
this.emit('disconnect');
|
|
910
|
+
}
|
|
911
|
+
};
|
|
912
|
+
this._checkPromise = null;
|
|
913
|
+
this._updateWallet = () => {
|
|
914
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
915
|
+
let state = this.state;
|
|
916
|
+
let address = this.address;
|
|
917
|
+
if ((0, tronwallet_abstract_adapter_1.isInMobileBrowser)()) {
|
|
918
|
+
if (window.tronLink) {
|
|
919
|
+
this._wallet = window.tronLink;
|
|
920
|
+
}
|
|
921
|
+
else {
|
|
922
|
+
this._wallet = {
|
|
923
|
+
ready: !!((_a = window.tronWeb) === null || _a === void 0 ? void 0 : _a.defaultAddress),
|
|
924
|
+
tronWeb: window.tronWeb,
|
|
925
|
+
request: () => Promise.resolve(true),
|
|
926
|
+
};
|
|
927
|
+
}
|
|
928
|
+
address = ((_c = (_b = this._wallet.tronWeb) === null || _b === void 0 ? void 0 : _b.defaultAddress) === null || _c === void 0 ? void 0 : _c.base58) || null;
|
|
929
|
+
state = address ? tronwallet_abstract_adapter_1.AdapterState.Connected : tronwallet_abstract_adapter_1.AdapterState.Disconnect;
|
|
930
|
+
}
|
|
931
|
+
else if (window.tron && window.tron.isTronLink) {
|
|
932
|
+
this._supportNewTronProtocol = true;
|
|
933
|
+
this._wallet = window.tron;
|
|
934
|
+
this._listenTronEvent();
|
|
935
|
+
address = (this._wallet.tronWeb && ((_e = (_d = this._wallet.tronWeb) === null || _d === void 0 ? void 0 : _d.defaultAddress) === null || _e === void 0 ? void 0 : _e.base58)) || null;
|
|
936
|
+
state = address ? tronwallet_abstract_adapter_1.AdapterState.Connected : tronwallet_abstract_adapter_1.AdapterState.Disconnect;
|
|
937
|
+
}
|
|
938
|
+
else if (window.tronLink) {
|
|
939
|
+
this._wallet = window.tronLink;
|
|
940
|
+
this._listenTronLinkEvent();
|
|
941
|
+
address = ((_g = (_f = this._wallet.tronWeb) === null || _f === void 0 ? void 0 : _f.defaultAddress) === null || _g === void 0 ? void 0 : _g.base58) || null;
|
|
942
|
+
state = this._wallet.ready ? tronwallet_abstract_adapter_1.AdapterState.Connected : tronwallet_abstract_adapter_1.AdapterState.Disconnect;
|
|
943
|
+
}
|
|
944
|
+
else if (window.tronWeb) {
|
|
945
|
+
// fake tronLink
|
|
946
|
+
this._wallet = {
|
|
947
|
+
ready: window.tronWeb.ready,
|
|
948
|
+
tronWeb: window.tronWeb,
|
|
949
|
+
request: () => Promise.resolve(true),
|
|
950
|
+
};
|
|
951
|
+
address = ((_h = this._wallet.tronWeb.defaultAddress) === null || _h === void 0 ? void 0 : _h.base58) || null;
|
|
952
|
+
state = this._wallet.ready ? tronwallet_abstract_adapter_1.AdapterState.Connected : tronwallet_abstract_adapter_1.AdapterState.Disconnect;
|
|
953
|
+
}
|
|
954
|
+
else {
|
|
955
|
+
// no tronlink support
|
|
956
|
+
this._wallet = null;
|
|
957
|
+
address = null;
|
|
958
|
+
state = tronwallet_abstract_adapter_1.AdapterState.NotFound;
|
|
959
|
+
}
|
|
960
|
+
// In TronLink App, account should be connected
|
|
961
|
+
if ((0, tronwallet_abstract_adapter_1.isInMobileBrowser)() && state === tronwallet_abstract_adapter_1.AdapterState.Disconnect) {
|
|
962
|
+
this.checkForWalletReadyForApp();
|
|
963
|
+
}
|
|
964
|
+
this.setAddress(address);
|
|
965
|
+
this.setState(state);
|
|
966
|
+
};
|
|
967
|
+
this.checkReadyInterval = null;
|
|
968
|
+
const { checkTimeout = 30 * 1000, dappIcon = '', dappName = '', openUrlWhenWalletNotFound = true, openTronLinkAppOnMobile = true, } = config;
|
|
969
|
+
if (typeof checkTimeout !== 'number') {
|
|
970
|
+
throw new Error('[TronLinkAdapter] config.checkTimeout should be a number');
|
|
971
|
+
}
|
|
972
|
+
this.config = {
|
|
973
|
+
checkTimeout,
|
|
974
|
+
openTronLinkAppOnMobile,
|
|
975
|
+
openUrlWhenWalletNotFound,
|
|
976
|
+
dappIcon,
|
|
977
|
+
dappName,
|
|
978
|
+
};
|
|
979
|
+
this._connecting = false;
|
|
980
|
+
this._wallet = null;
|
|
981
|
+
this._address = null;
|
|
982
|
+
if (!(0, tronwallet_abstract_adapter_1.isInBrowser)()) {
|
|
983
|
+
this._readyState = tronwallet_abstract_adapter_1.WalletReadyState.NotFound;
|
|
984
|
+
this.setState(tronwallet_abstract_adapter_1.AdapterState.NotFound);
|
|
985
|
+
return;
|
|
986
|
+
}
|
|
987
|
+
if ((0, utils_js_1.supportTron)() || ((0, tronwallet_abstract_adapter_1.isInMobileBrowser)() && (window.tronLink || window.tronWeb))) {
|
|
988
|
+
this._readyState = tronwallet_abstract_adapter_1.WalletReadyState.Found;
|
|
989
|
+
this._updateWallet();
|
|
990
|
+
}
|
|
991
|
+
else {
|
|
992
|
+
this._checkWallet().then(() => {
|
|
993
|
+
if (this.connected) {
|
|
994
|
+
this.emit('connect', this.address || '');
|
|
995
|
+
}
|
|
996
|
+
});
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
get address() {
|
|
1000
|
+
return this._address;
|
|
1001
|
+
}
|
|
1002
|
+
get state() {
|
|
1003
|
+
return this._state;
|
|
1004
|
+
}
|
|
1005
|
+
get readyState() {
|
|
1006
|
+
return this._readyState;
|
|
1007
|
+
}
|
|
1008
|
+
get connecting() {
|
|
1009
|
+
return this._connecting;
|
|
1010
|
+
}
|
|
1011
|
+
/**
|
|
1012
|
+
* Get network information used by TronLink.
|
|
1013
|
+
* @returns {Network} Current network information.
|
|
1014
|
+
*/
|
|
1015
|
+
network() {
|
|
1016
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1017
|
+
var _a;
|
|
1018
|
+
try {
|
|
1019
|
+
yield this._checkWallet();
|
|
1020
|
+
if (this.state !== tronwallet_abstract_adapter_1.AdapterState.Connected)
|
|
1021
|
+
throw new tronwallet_abstract_adapter_1.WalletDisconnectedError();
|
|
1022
|
+
const tronWeb = ((_a = this._wallet) === null || _a === void 0 ? void 0 : _a.tronWeb) || window.tronWeb;
|
|
1023
|
+
if (!tronWeb)
|
|
1024
|
+
throw new tronwallet_abstract_adapter_1.WalletDisconnectedError();
|
|
1025
|
+
try {
|
|
1026
|
+
return yield getNetworkInfoByTronWeb(tronWeb);
|
|
1027
|
+
}
|
|
1028
|
+
catch (e) {
|
|
1029
|
+
throw new tronwallet_abstract_adapter_1.WalletGetNetworkError(e === null || e === void 0 ? void 0 : e.message, e);
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
catch (e) {
|
|
1033
|
+
this.emit('error', e);
|
|
1034
|
+
throw e;
|
|
1035
|
+
}
|
|
1036
|
+
});
|
|
1037
|
+
}
|
|
1038
|
+
connect() {
|
|
1039
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1040
|
+
var _a, _b;
|
|
1041
|
+
try {
|
|
1042
|
+
this.checkIfOpenTronLink();
|
|
1043
|
+
if (this.connected || this.connecting)
|
|
1044
|
+
return;
|
|
1045
|
+
yield this._checkWallet();
|
|
1046
|
+
if (this.state === tronwallet_abstract_adapter_1.AdapterState.NotFound) {
|
|
1047
|
+
if (this.config.openUrlWhenWalletNotFound !== false && (0, tronwallet_abstract_adapter_1.isInBrowser)()) {
|
|
1048
|
+
window.open(this.url, '_blank');
|
|
1049
|
+
}
|
|
1050
|
+
throw new tronwallet_abstract_adapter_1.WalletNotFoundError();
|
|
1051
|
+
}
|
|
1052
|
+
// lower version only support window.tronWeb, no window.tronLink
|
|
1053
|
+
if (!this._wallet)
|
|
1054
|
+
return;
|
|
1055
|
+
this._connecting = true;
|
|
1056
|
+
if (this._supportNewTronProtocol) {
|
|
1057
|
+
const wallet = this._wallet;
|
|
1058
|
+
try {
|
|
1059
|
+
const res = yield wallet.request({ method: 'eth_requestAccounts' });
|
|
1060
|
+
const address = res[0];
|
|
1061
|
+
this.setAddress(address);
|
|
1062
|
+
this.setState(tronwallet_abstract_adapter_1.AdapterState.Connected);
|
|
1063
|
+
this._listenTronEvent();
|
|
1064
|
+
if (!this._wallet.tronWeb) {
|
|
1065
|
+
yield (0, utils_js_1.waitTronwebReady)(this._wallet);
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
catch (error) {
|
|
1069
|
+
let message = (error === null || error === void 0 ? void 0 : error.message) || error || 'Connect TronLink wallet failed.';
|
|
1070
|
+
if (error.code === -32002) {
|
|
1071
|
+
message =
|
|
1072
|
+
'The same DApp has already initiated a request to connect to TronLink wallet, and the pop-up window has not been closed.';
|
|
1073
|
+
}
|
|
1074
|
+
if (error.code === 4001) {
|
|
1075
|
+
message = 'The user rejected connection.';
|
|
1076
|
+
}
|
|
1077
|
+
throw new tronwallet_abstract_adapter_1.WalletConnectionError(message, error);
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
else if (window.tronLink) {
|
|
1081
|
+
const wallet = this._wallet;
|
|
1082
|
+
try {
|
|
1083
|
+
const res = yield wallet.request({ method: 'tron_requestAccounts' });
|
|
1084
|
+
if (!res) {
|
|
1085
|
+
// 1. wallet is locked
|
|
1086
|
+
// 2. tronlink is first installed and there is no wallet account
|
|
1087
|
+
throw new tronwallet_abstract_adapter_1.WalletConnectionError('TronLink wallet is locked or no wallet account is avaliable.');
|
|
1088
|
+
}
|
|
1089
|
+
if (res.code === 4000) {
|
|
1090
|
+
throw new tronwallet_abstract_adapter_1.WalletConnectionError('The same DApp has already initiated a request to connect to TronLink wallet, and the pop-up window has not been closed.');
|
|
1091
|
+
}
|
|
1092
|
+
if (res.code === 4001) {
|
|
1093
|
+
throw new tronwallet_abstract_adapter_1.WalletConnectionError('The user rejected connection.');
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
catch (error) {
|
|
1097
|
+
throw new tronwallet_abstract_adapter_1.WalletConnectionError(error === null || error === void 0 ? void 0 : error.message, error);
|
|
1098
|
+
}
|
|
1099
|
+
const address = ((_a = wallet.tronWeb.defaultAddress) === null || _a === void 0 ? void 0 : _a.base58) || '';
|
|
1100
|
+
this.setAddress(address);
|
|
1101
|
+
this.setState(tronwallet_abstract_adapter_1.AdapterState.Connected);
|
|
1102
|
+
this._listenTronLinkEvent();
|
|
1103
|
+
}
|
|
1104
|
+
else if (window.tronWeb) {
|
|
1105
|
+
const wallet = this._wallet;
|
|
1106
|
+
const address = ((_b = wallet.tronWeb.defaultAddress) === null || _b === void 0 ? void 0 : _b.base58) || '';
|
|
1107
|
+
this.setAddress(address);
|
|
1108
|
+
this.setState(tronwallet_abstract_adapter_1.AdapterState.Connected);
|
|
1109
|
+
}
|
|
1110
|
+
else {
|
|
1111
|
+
throw new tronwallet_abstract_adapter_1.WalletConnectionError('Cannot connect wallet.');
|
|
1112
|
+
}
|
|
1113
|
+
this.connected && this.emit('connect', this.address || '');
|
|
1114
|
+
}
|
|
1115
|
+
catch (error) {
|
|
1116
|
+
this.emit('error', error);
|
|
1117
|
+
throw error;
|
|
1118
|
+
}
|
|
1119
|
+
finally {
|
|
1120
|
+
this._connecting = false;
|
|
1121
|
+
}
|
|
1122
|
+
});
|
|
1123
|
+
}
|
|
1124
|
+
disconnect() {
|
|
1125
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1126
|
+
if (this._supportNewTronProtocol) {
|
|
1127
|
+
this._stopListenTronEvent();
|
|
1128
|
+
}
|
|
1129
|
+
else {
|
|
1130
|
+
this._stopListenTronLinkEvent();
|
|
1131
|
+
}
|
|
1132
|
+
if (this.state !== tronwallet_abstract_adapter_1.AdapterState.Connected) {
|
|
1133
|
+
return;
|
|
1134
|
+
}
|
|
1135
|
+
this.setAddress(null);
|
|
1136
|
+
this.setState(tronwallet_abstract_adapter_1.AdapterState.Disconnect);
|
|
1137
|
+
this.emit('disconnect');
|
|
1138
|
+
});
|
|
1139
|
+
}
|
|
1140
|
+
signTransaction(transaction, privateKey) {
|
|
1141
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1142
|
+
try {
|
|
1143
|
+
const wallet = yield this.checkAndGetWallet();
|
|
1144
|
+
try {
|
|
1145
|
+
return yield wallet.tronWeb.trx.sign(transaction, privateKey);
|
|
1146
|
+
}
|
|
1147
|
+
catch (error) {
|
|
1148
|
+
if (error instanceof Error) {
|
|
1149
|
+
throw new tronwallet_abstract_adapter_1.WalletSignTransactionError(error.message, error);
|
|
1150
|
+
}
|
|
1151
|
+
else {
|
|
1152
|
+
throw new tronwallet_abstract_adapter_1.WalletSignTransactionError(error, new Error(error));
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
catch (error) {
|
|
1157
|
+
this.emit('error', error);
|
|
1158
|
+
throw error;
|
|
1159
|
+
}
|
|
1160
|
+
});
|
|
1161
|
+
}
|
|
1162
|
+
multiSign(transaction, privateKey, permissionId) {
|
|
1163
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1164
|
+
try {
|
|
1165
|
+
const wallet = yield this.checkAndGetWallet();
|
|
1166
|
+
try {
|
|
1167
|
+
return yield wallet.tronWeb.trx.multiSign(transaction, privateKey, permissionId);
|
|
1168
|
+
}
|
|
1169
|
+
catch (error) {
|
|
1170
|
+
if (error instanceof Error) {
|
|
1171
|
+
throw new tronwallet_abstract_adapter_1.WalletSignTransactionError(error.message, error);
|
|
1172
|
+
}
|
|
1173
|
+
else {
|
|
1174
|
+
throw new tronwallet_abstract_adapter_1.WalletSignTransactionError(error, new Error(error));
|
|
1175
|
+
}
|
|
1176
|
+
}
|
|
1177
|
+
}
|
|
1178
|
+
catch (error) {
|
|
1179
|
+
this.emit('error', error);
|
|
1180
|
+
throw error;
|
|
1181
|
+
}
|
|
1182
|
+
});
|
|
1183
|
+
}
|
|
1184
|
+
signMessage(message, privateKey) {
|
|
1185
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1186
|
+
try {
|
|
1187
|
+
const wallet = yield this.checkAndGetWallet();
|
|
1188
|
+
try {
|
|
1189
|
+
return yield wallet.tronWeb.trx.signMessageV2(message, privateKey);
|
|
1190
|
+
}
|
|
1191
|
+
catch (error) {
|
|
1192
|
+
if (error instanceof Error) {
|
|
1193
|
+
throw new tronwallet_abstract_adapter_1.WalletSignMessageError(error.message, error);
|
|
1194
|
+
}
|
|
1195
|
+
else {
|
|
1196
|
+
throw new tronwallet_abstract_adapter_1.WalletSignMessageError(error, new Error(error));
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
catch (error) {
|
|
1201
|
+
this.emit('error', error);
|
|
1202
|
+
throw error;
|
|
1203
|
+
}
|
|
1204
|
+
});
|
|
1205
|
+
}
|
|
1206
|
+
/**
|
|
1207
|
+
* Switch to target chain. If current chain is the same as target chain, the call will success immediately.
|
|
1208
|
+
* Available chainIds:
|
|
1209
|
+
* - Mainnet: 0x2b6653dc
|
|
1210
|
+
* - Shasta: 0x94a9059e
|
|
1211
|
+
* - Nile: 0xcd8690dc
|
|
1212
|
+
* @param chainId chainId
|
|
1213
|
+
*/
|
|
1214
|
+
switchChain(chainId) {
|
|
1215
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1216
|
+
try {
|
|
1217
|
+
yield this._checkWallet();
|
|
1218
|
+
if (this.state === tronwallet_abstract_adapter_1.AdapterState.NotFound) {
|
|
1219
|
+
if (this.config.openUrlWhenWalletNotFound !== false && (0, tronwallet_abstract_adapter_1.isInBrowser)()) {
|
|
1220
|
+
window.open(this.url, '_blank');
|
|
1221
|
+
}
|
|
1222
|
+
throw new tronwallet_abstract_adapter_1.WalletNotFoundError();
|
|
1223
|
+
}
|
|
1224
|
+
if (!this._supportNewTronProtocol) {
|
|
1225
|
+
throw new tronwallet_abstract_adapter_1.WalletSwitchChainError("Current version of TronLink doesn't support switch chain operation.");
|
|
1226
|
+
}
|
|
1227
|
+
const wallet = this._wallet;
|
|
1228
|
+
try {
|
|
1229
|
+
yield wallet.request({
|
|
1230
|
+
method: 'wallet_switchEthereumChain',
|
|
1231
|
+
params: [{ chainId }],
|
|
1232
|
+
});
|
|
1233
|
+
}
|
|
1234
|
+
catch (e) {
|
|
1235
|
+
throw new tronwallet_abstract_adapter_1.WalletSwitchChainError((e === null || e === void 0 ? void 0 : e.message) || e, e instanceof Error ? e : new Error(e));
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
catch (error) {
|
|
1239
|
+
this.emit('error', error);
|
|
1240
|
+
throw error;
|
|
1241
|
+
}
|
|
1242
|
+
});
|
|
1243
|
+
}
|
|
1244
|
+
checkAndGetWallet() {
|
|
1245
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1246
|
+
this.checkIfOpenTronLink();
|
|
1247
|
+
yield this._checkWallet();
|
|
1248
|
+
if (this.state !== tronwallet_abstract_adapter_1.AdapterState.Connected)
|
|
1249
|
+
throw new tronwallet_abstract_adapter_1.WalletDisconnectedError();
|
|
1250
|
+
const wallet = this._wallet;
|
|
1251
|
+
if (!wallet || !wallet.tronWeb)
|
|
1252
|
+
throw new tronwallet_abstract_adapter_1.WalletDisconnectedError();
|
|
1253
|
+
return wallet;
|
|
1254
|
+
});
|
|
1255
|
+
}
|
|
1256
|
+
_listenTronLinkEvent() {
|
|
1257
|
+
this._stopListenTronLinkEvent();
|
|
1258
|
+
window.addEventListener('message', this._tronLinkMessageHandler);
|
|
1259
|
+
}
|
|
1260
|
+
_stopListenTronLinkEvent() {
|
|
1261
|
+
window.removeEventListener('message', this._tronLinkMessageHandler);
|
|
1262
|
+
}
|
|
1263
|
+
checkIfOpenTronLink() {
|
|
1264
|
+
const { dappName = '', dappIcon = '' } = this.config;
|
|
1265
|
+
if (this.config.openTronLinkAppOnMobile === false) {
|
|
1266
|
+
return;
|
|
1267
|
+
}
|
|
1268
|
+
if ((0, utils_js_1.openTronLink)({ dappIcon, dappName })) {
|
|
1269
|
+
throw new tronwallet_abstract_adapter_1.WalletNotFoundError();
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
// following code is for TIP-1193
|
|
1273
|
+
_listenTronEvent() {
|
|
1274
|
+
this._stopListenTronEvent();
|
|
1275
|
+
this._stopListenTronLinkEvent();
|
|
1276
|
+
const wallet = this._wallet;
|
|
1277
|
+
wallet.on('chainChanged', this._onChainChanged);
|
|
1278
|
+
wallet.on('accountsChanged', this._onAccountsChanged);
|
|
1279
|
+
}
|
|
1280
|
+
_stopListenTronEvent() {
|
|
1281
|
+
const wallet = this._wallet;
|
|
1282
|
+
wallet.removeListener('chainChanged', this._onChainChanged);
|
|
1283
|
+
wallet.removeListener('accountsChanged', this._onAccountsChanged);
|
|
1284
|
+
}
|
|
1285
|
+
/**
|
|
1286
|
+
* check if wallet exists by interval, the promise only resolve when wallet detected or timeout
|
|
1287
|
+
* @returns if wallet exists
|
|
1288
|
+
*/
|
|
1289
|
+
_checkWallet() {
|
|
1290
|
+
if (this.readyState === tronwallet_abstract_adapter_1.WalletReadyState.Found) {
|
|
1291
|
+
return Promise.resolve(true);
|
|
1292
|
+
}
|
|
1293
|
+
if (this._checkPromise) {
|
|
1294
|
+
return this._checkPromise;
|
|
1295
|
+
}
|
|
1296
|
+
const interval = 100;
|
|
1297
|
+
const checkTronTimes = Math.floor(2000 / interval);
|
|
1298
|
+
const maxTimes = Math.floor(this.config.checkTimeout / interval);
|
|
1299
|
+
let times = 0, timer;
|
|
1300
|
+
this._checkPromise = new Promise((resolve) => {
|
|
1301
|
+
const check = () => {
|
|
1302
|
+
times++;
|
|
1303
|
+
const isSupport = times < checkTronTimes && !(0, tronwallet_abstract_adapter_1.isInMobileBrowser)() ? (0, utils_js_1.supportTron)() : (0, utils_js_1.supportTronLink)();
|
|
1304
|
+
if (isSupport || times > maxTimes) {
|
|
1305
|
+
timer && clearInterval(timer);
|
|
1306
|
+
this._readyState = isSupport ? tronwallet_abstract_adapter_1.WalletReadyState.Found : tronwallet_abstract_adapter_1.WalletReadyState.NotFound;
|
|
1307
|
+
this._updateWallet();
|
|
1308
|
+
this.emit('readyStateChanged', this.readyState);
|
|
1309
|
+
resolve(isSupport);
|
|
1310
|
+
}
|
|
1311
|
+
};
|
|
1312
|
+
timer = setInterval(check, interval);
|
|
1313
|
+
check();
|
|
1314
|
+
});
|
|
1315
|
+
return this._checkPromise;
|
|
1316
|
+
}
|
|
1317
|
+
checkForWalletReadyForApp() {
|
|
1318
|
+
if (this.checkReadyInterval) {
|
|
1319
|
+
return;
|
|
1320
|
+
}
|
|
1321
|
+
let times = 0;
|
|
1322
|
+
const maxTimes = Math.floor(this.config.checkTimeout / 200);
|
|
1323
|
+
const check = () => {
|
|
1324
|
+
var _a, _b;
|
|
1325
|
+
if (window.tronLink ? (_a = window.tronLink.tronWeb) === null || _a === void 0 ? void 0 : _a.defaultAddress : (_b = window.tronWeb) === null || _b === void 0 ? void 0 : _b.defaultAddress) {
|
|
1326
|
+
this.checkReadyInterval && clearInterval(this.checkReadyInterval);
|
|
1327
|
+
this.checkReadyInterval = null;
|
|
1328
|
+
this._updateWallet();
|
|
1329
|
+
this.emit('connect', this.address || '');
|
|
1330
|
+
}
|
|
1331
|
+
else if (times > maxTimes) {
|
|
1332
|
+
this.checkReadyInterval && clearInterval(this.checkReadyInterval);
|
|
1333
|
+
this.checkReadyInterval = null;
|
|
1334
|
+
}
|
|
1335
|
+
else {
|
|
1336
|
+
times++;
|
|
1337
|
+
}
|
|
1338
|
+
};
|
|
1339
|
+
this.checkReadyInterval = setInterval(check, 200);
|
|
1340
|
+
}
|
|
1341
|
+
setAddress(address) {
|
|
1342
|
+
this._address = address;
|
|
1343
|
+
}
|
|
1344
|
+
setState(state) {
|
|
1345
|
+
const preState = this.state;
|
|
1346
|
+
if (state !== preState) {
|
|
1347
|
+
this._state = state;
|
|
1348
|
+
this.emit('stateChanged', state);
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
exports.TronLinkAdapter = TronLinkAdapter;
|
|
1353
|
+
|
|
1354
|
+
} (adapter));
|
|
1355
|
+
return adapter;
|
|
1356
|
+
}
|
|
1300
1357
|
|
|
1301
1358
|
var types = {};
|
|
1302
1359
|
|
|
1303
|
-
|
|
1360
|
+
var hasRequiredTypes;
|
|
1304
1361
|
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
1310
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
1311
|
-
}
|
|
1312
|
-
Object.defineProperty(o, k2, desc);
|
|
1313
|
-
}) : (function(o, m, k, k2) {
|
|
1314
|
-
if (k2 === undefined) k2 = k;
|
|
1315
|
-
o[k2] = m[k];
|
|
1316
|
-
}));
|
|
1317
|
-
var __exportStar = (commonjsGlobal && commonjsGlobal.__exportStar) || function(m, exports) {
|
|
1318
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
1319
|
-
};
|
|
1320
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1321
|
-
__exportStar(adapter, exports);
|
|
1322
|
-
__exportStar(types, exports);
|
|
1323
|
-
__exportStar(utils$1, exports);
|
|
1362
|
+
function requireTypes () {
|
|
1363
|
+
if (hasRequiredTypes) return types;
|
|
1364
|
+
hasRequiredTypes = 1;
|
|
1365
|
+
Object.defineProperty(types, "__esModule", { value: true });
|
|
1324
1366
|
|
|
1325
|
-
|
|
1367
|
+
return types;
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
var hasRequiredCjs$1;
|
|
1371
|
+
|
|
1372
|
+
function requireCjs$1 () {
|
|
1373
|
+
if (hasRequiredCjs$1) return cjs;
|
|
1374
|
+
hasRequiredCjs$1 = 1;
|
|
1375
|
+
(function (exports) {
|
|
1376
|
+
var __createBinding = (cjs && cjs.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
1377
|
+
if (k2 === undefined) k2 = k;
|
|
1378
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
1379
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
1380
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
1381
|
+
}
|
|
1382
|
+
Object.defineProperty(o, k2, desc);
|
|
1383
|
+
}) : (function(o, m, k, k2) {
|
|
1384
|
+
if (k2 === undefined) k2 = k;
|
|
1385
|
+
o[k2] = m[k];
|
|
1386
|
+
}));
|
|
1387
|
+
var __exportStar = (cjs && cjs.__exportStar) || function(m, exports) {
|
|
1388
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
1389
|
+
};
|
|
1390
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1391
|
+
__exportStar(/*@__PURE__*/ requireAdapter$1(), exports);
|
|
1392
|
+
__exportStar(/*@__PURE__*/ requireTypes(), exports);
|
|
1393
|
+
__exportStar(/*@__PURE__*/ requireUtils$1(), exports);
|
|
1394
|
+
|
|
1395
|
+
} (cjs));
|
|
1396
|
+
return cjs;
|
|
1397
|
+
}
|
|
1326
1398
|
|
|
1327
1399
|
var utils = {};
|
|
1328
1400
|
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1401
|
+
var hasRequiredUtils;
|
|
1402
|
+
|
|
1403
|
+
function requireUtils () {
|
|
1404
|
+
if (hasRequiredUtils) return utils;
|
|
1405
|
+
hasRequiredUtils = 1;
|
|
1406
|
+
(function (exports) {
|
|
1407
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1408
|
+
exports.isTrustApp = void 0;
|
|
1409
|
+
exports.supportTrust = supportTrust;
|
|
1410
|
+
exports.openTrustWallet = openTrustWallet;
|
|
1411
|
+
const tronwallet_abstract_adapter_1 = /*@__PURE__*/ requireCjs$2();
|
|
1412
|
+
function supportTrust() {
|
|
1413
|
+
return typeof window !== 'undefined' && !!(window.trustwallet && window.trustwallet.tronLink);
|
|
1414
|
+
}
|
|
1415
|
+
const isTrustApp = function () {
|
|
1416
|
+
if (typeof window !== 'undefined' && typeof window.navigator !== 'undefined') {
|
|
1417
|
+
return /Trust/i.test(window.navigator.userAgent);
|
|
1418
|
+
}
|
|
1419
|
+
return false;
|
|
1420
|
+
};
|
|
1421
|
+
exports.isTrustApp = isTrustApp;
|
|
1422
|
+
function openTrustWallet() {
|
|
1423
|
+
if (!(0, exports.isTrustApp)() && (0, tronwallet_abstract_adapter_1.isInMobileBrowser)()) {
|
|
1424
|
+
window.location.href = 'https://link.trustwallet.com/open_url?url=' + encodeURIComponent(window.location.href);
|
|
1425
|
+
return true;
|
|
1426
|
+
}
|
|
1427
|
+
return false;
|
|
1428
|
+
}
|
|
1429
|
+
|
|
1430
|
+
} (utils));
|
|
1431
|
+
return utils;
|
|
1432
|
+
}
|
|
1354
1433
|
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
|
|
1733
|
-
|
|
1434
|
+
var hasRequiredAdapter;
|
|
1435
|
+
|
|
1436
|
+
function requireAdapter () {
|
|
1437
|
+
if (hasRequiredAdapter) return adapter$2;
|
|
1438
|
+
hasRequiredAdapter = 1;
|
|
1439
|
+
(function (exports) {
|
|
1440
|
+
var __awaiter = (adapter$2 && adapter$2.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
1441
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
1442
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
1443
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
1444
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
1445
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
1446
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
1447
|
+
});
|
|
1448
|
+
};
|
|
1449
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1450
|
+
exports.TrustAdapter = exports.TrustAdapterName = void 0;
|
|
1451
|
+
const tronwallet_abstract_adapter_1 = /*@__PURE__*/ requireCjs$2();
|
|
1452
|
+
const tronwallet_adapter_tronlink_1 = /*@__PURE__*/ requireCjs$1();
|
|
1453
|
+
const utils_js_1 = /*@__PURE__*/ requireUtils();
|
|
1454
|
+
exports.TrustAdapterName = 'Trust';
|
|
1455
|
+
class TrustAdapter extends tronwallet_abstract_adapter_1.Adapter {
|
|
1456
|
+
constructor(config = {}) {
|
|
1457
|
+
super();
|
|
1458
|
+
this.name = exports.TrustAdapterName;
|
|
1459
|
+
this.url = 'https://trustwallet.com';
|
|
1460
|
+
this.icon = 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNTgiIGhlaWdodD0iNjUiIHZpZXdCb3g9IjAgMCA1OCA2NSIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTAgOS4zODk0OUwyOC44OTA3IDBWNjUuMDA0MkM4LjI1NDUgNTYuMzM2OSAwIDM5LjcyNDggMCAzMC4zMzUzVjkuMzg5NDlaIiBmaWxsPSIjMDUwMEZGIi8+CjxwYXRoIGQ9Ik01Ny43ODIyIDkuMzg5NDlMMjguODkxNSAwVjY1LjAwNDJDNDkuNTI3NyA1Ni4zMzY5IDU3Ljc4MjIgMzkuNzI0OCA1Ny43ODIyIDMwLjMzNTNWOS4zODk0OVoiIGZpbGw9InVybCgjcGFpbnQwX2xpbmVhcl8yMjAxXzY5NDIpIi8+CjxkZWZzPgo8bGluZWFyR3JhZGllbnQgaWQ9InBhaW50MF9saW5lYXJfMjIwMV82OTQyIiB4MT0iNTEuMzYxNSIgeTE9Ii00LjE1MjkzIiB4Mj0iMjkuNTM4NCIgeTI9IjY0LjUxNDciIGdyYWRpZW50VW5pdHM9InVzZXJTcGFjZU9uVXNlIj4KPHN0b3Agb2Zmc2V0PSIwLjAyMTEyIiBzdG9wLWNvbG9yPSIjMDAwMEZGIi8+CjxzdG9wIG9mZnNldD0iMC4wNzYyNDIzIiBzdG9wLWNvbG9yPSIjMDA5NEZGIi8+CjxzdG9wIG9mZnNldD0iMC4xNjMwODkiIHN0b3AtY29sb3I9IiM0OEZGOTEiLz4KPHN0b3Agb2Zmc2V0PSIwLjQyMDA0OSIgc3RvcC1jb2xvcj0iIzAwOTRGRiIvPgo8c3RvcCBvZmZzZXQ9IjAuNjgyODg2IiBzdG9wLWNvbG9yPSIjMDAzOEZGIi8+CjxzdG9wIG9mZnNldD0iMC45MDI0NjUiIHN0b3AtY29sb3I9IiMwNTAwRkYiLz4KPC9saW5lYXJHcmFkaWVudD4KPC9kZWZzPgo8L3N2Zz4K';
|
|
1461
|
+
this._readyState = (0, tronwallet_abstract_adapter_1.isInBrowser)() ? tronwallet_abstract_adapter_1.WalletReadyState.Loading : tronwallet_abstract_adapter_1.WalletReadyState.NotFound;
|
|
1462
|
+
this._state = tronwallet_abstract_adapter_1.AdapterState.Loading;
|
|
1463
|
+
this.messageHandler = (e) => {
|
|
1464
|
+
var _a, _b, _c;
|
|
1465
|
+
const message = (_a = e.data) === null || _a === void 0 ? void 0 : _a.message;
|
|
1466
|
+
if (!message) {
|
|
1467
|
+
return;
|
|
1468
|
+
}
|
|
1469
|
+
if (message.action === 'accountsChanged') {
|
|
1470
|
+
setTimeout(() => {
|
|
1471
|
+
var _a;
|
|
1472
|
+
const preAddr = this.address || '';
|
|
1473
|
+
if ((_a = this._wallet) === null || _a === void 0 ? void 0 : _a.ready) {
|
|
1474
|
+
const address = message.data.address;
|
|
1475
|
+
this.setAddress(address);
|
|
1476
|
+
this.setState(tronwallet_abstract_adapter_1.AdapterState.Connected);
|
|
1477
|
+
}
|
|
1478
|
+
else {
|
|
1479
|
+
this.setAddress(null);
|
|
1480
|
+
this.setState(tronwallet_abstract_adapter_1.AdapterState.Disconnect);
|
|
1481
|
+
}
|
|
1482
|
+
const address = this.address || '';
|
|
1483
|
+
if (address !== preAddr) {
|
|
1484
|
+
this.emit('accountsChanged', this.address || '', preAddr);
|
|
1485
|
+
}
|
|
1486
|
+
if (!preAddr && this.address) {
|
|
1487
|
+
this.emit('connect', this.address);
|
|
1488
|
+
}
|
|
1489
|
+
else if (preAddr && !this.address) {
|
|
1490
|
+
this.emit('disconnect');
|
|
1491
|
+
}
|
|
1492
|
+
}, 200);
|
|
1493
|
+
}
|
|
1494
|
+
else if (message.action === 'connect') {
|
|
1495
|
+
const isCurConnected = this.connected;
|
|
1496
|
+
const preAddress = this.address || '';
|
|
1497
|
+
const address = ((_c = (_b = this._wallet.tronWeb) === null || _b === void 0 ? void 0 : _b.defaultAddress) === null || _c === void 0 ? void 0 : _c.base58) || '';
|
|
1498
|
+
this.setAddress(address);
|
|
1499
|
+
this.setState(tronwallet_abstract_adapter_1.AdapterState.Connected);
|
|
1500
|
+
if (!isCurConnected) {
|
|
1501
|
+
this.emit('connect', address);
|
|
1502
|
+
}
|
|
1503
|
+
else if (address !== preAddress) {
|
|
1504
|
+
this.emit('accountsChanged', this.address || '', preAddress);
|
|
1505
|
+
}
|
|
1506
|
+
}
|
|
1507
|
+
else if (message.action === 'disconnect') {
|
|
1508
|
+
this.setAddress(null);
|
|
1509
|
+
this.setState(tronwallet_abstract_adapter_1.AdapterState.Disconnect);
|
|
1510
|
+
this.emit('disconnect');
|
|
1511
|
+
}
|
|
1512
|
+
};
|
|
1513
|
+
this._checkPromise = null;
|
|
1514
|
+
this._updateWallet = () => {
|
|
1515
|
+
var _a, _b;
|
|
1516
|
+
let state = this.state;
|
|
1517
|
+
let address = this.address;
|
|
1518
|
+
if ((0, utils_js_1.supportTrust)()) {
|
|
1519
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
1520
|
+
this._wallet = window.trustwallet.tronLink;
|
|
1521
|
+
this._listenEvent();
|
|
1522
|
+
address = ((_b = (_a = this._wallet.tronWeb) === null || _a === void 0 ? void 0 : _a.defaultAddress) === null || _b === void 0 ? void 0 : _b.base58) || null;
|
|
1523
|
+
state = this._wallet.ready ? tronwallet_abstract_adapter_1.AdapterState.Connected : tronwallet_abstract_adapter_1.AdapterState.Disconnect;
|
|
1524
|
+
}
|
|
1525
|
+
else {
|
|
1526
|
+
this._wallet = null;
|
|
1527
|
+
address = null;
|
|
1528
|
+
state = tronwallet_abstract_adapter_1.AdapterState.NotFound;
|
|
1529
|
+
}
|
|
1530
|
+
this.setAddress(address);
|
|
1531
|
+
this.setState(state);
|
|
1532
|
+
};
|
|
1533
|
+
const { checkTimeout = 2 * 1000, openUrlWhenWalletNotFound = true, openAppWithDeeplink = true } = config;
|
|
1534
|
+
if (typeof checkTimeout !== 'number') {
|
|
1535
|
+
throw new Error('[TrustAdapter] config.checkTimeout should be a number');
|
|
1536
|
+
}
|
|
1537
|
+
this.config = {
|
|
1538
|
+
checkTimeout,
|
|
1539
|
+
openAppWithDeeplink,
|
|
1540
|
+
openUrlWhenWalletNotFound,
|
|
1541
|
+
};
|
|
1542
|
+
this._connecting = false;
|
|
1543
|
+
this._wallet = null;
|
|
1544
|
+
this._address = null;
|
|
1545
|
+
if (!(0, tronwallet_abstract_adapter_1.isInBrowser)()) {
|
|
1546
|
+
this._readyState = tronwallet_abstract_adapter_1.WalletReadyState.NotFound;
|
|
1547
|
+
this.setState(tronwallet_abstract_adapter_1.AdapterState.NotFound);
|
|
1548
|
+
return;
|
|
1549
|
+
}
|
|
1550
|
+
if ((0, utils_js_1.supportTrust)()) {
|
|
1551
|
+
this._readyState = tronwallet_abstract_adapter_1.WalletReadyState.Found;
|
|
1552
|
+
this._updateWallet();
|
|
1553
|
+
}
|
|
1554
|
+
else {
|
|
1555
|
+
this._checkWallet().then(() => {
|
|
1556
|
+
if (this.connected) {
|
|
1557
|
+
this.emit('connect', this.address || '');
|
|
1558
|
+
}
|
|
1559
|
+
});
|
|
1560
|
+
}
|
|
1561
|
+
}
|
|
1562
|
+
get address() {
|
|
1563
|
+
return this._address;
|
|
1564
|
+
}
|
|
1565
|
+
get state() {
|
|
1566
|
+
return this._state;
|
|
1567
|
+
}
|
|
1568
|
+
get readyState() {
|
|
1569
|
+
return this._readyState;
|
|
1570
|
+
}
|
|
1571
|
+
get connecting() {
|
|
1572
|
+
return this._connecting;
|
|
1573
|
+
}
|
|
1574
|
+
/**
|
|
1575
|
+
* Get network information used by Trust.
|
|
1576
|
+
* @returns {Network} Current network information.
|
|
1577
|
+
*/
|
|
1578
|
+
network() {
|
|
1579
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1580
|
+
try {
|
|
1581
|
+
yield this._checkWallet();
|
|
1582
|
+
if (this.state !== tronwallet_abstract_adapter_1.AdapterState.Connected)
|
|
1583
|
+
throw new tronwallet_abstract_adapter_1.WalletDisconnectedError();
|
|
1584
|
+
const wallet = this._wallet;
|
|
1585
|
+
if (!wallet || !wallet.tronWeb)
|
|
1586
|
+
throw new tronwallet_abstract_adapter_1.WalletDisconnectedError();
|
|
1587
|
+
try {
|
|
1588
|
+
return yield (0, tronwallet_adapter_tronlink_1.getNetworkInfoByTronWeb)(wallet.tronWeb);
|
|
1589
|
+
}
|
|
1590
|
+
catch (e) {
|
|
1591
|
+
throw new tronwallet_abstract_adapter_1.WalletGetNetworkError(e === null || e === void 0 ? void 0 : e.message, e);
|
|
1592
|
+
}
|
|
1593
|
+
}
|
|
1594
|
+
catch (e) {
|
|
1595
|
+
this.emit('error', e);
|
|
1596
|
+
throw e;
|
|
1597
|
+
}
|
|
1598
|
+
});
|
|
1599
|
+
}
|
|
1600
|
+
connect() {
|
|
1601
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1602
|
+
var _a;
|
|
1603
|
+
try {
|
|
1604
|
+
this.checkIfopenTrustWallet();
|
|
1605
|
+
if (this.connected || this.connecting)
|
|
1606
|
+
return;
|
|
1607
|
+
yield this._checkWallet();
|
|
1608
|
+
if (this.state === tronwallet_abstract_adapter_1.AdapterState.NotFound) {
|
|
1609
|
+
if (this.config.openUrlWhenWalletNotFound !== false && (0, tronwallet_abstract_adapter_1.isInBrowser)()) {
|
|
1610
|
+
window.open(this.url, '_blank');
|
|
1611
|
+
}
|
|
1612
|
+
throw new tronwallet_abstract_adapter_1.WalletNotFoundError();
|
|
1613
|
+
}
|
|
1614
|
+
if (!this._wallet)
|
|
1615
|
+
return;
|
|
1616
|
+
this._connecting = true;
|
|
1617
|
+
const wallet = this._wallet;
|
|
1618
|
+
try {
|
|
1619
|
+
const res = yield wallet.request({ method: 'tron_requestAccounts' });
|
|
1620
|
+
if (!res) {
|
|
1621
|
+
throw new tronwallet_abstract_adapter_1.WalletConnectionError('Request connect error.');
|
|
1622
|
+
}
|
|
1623
|
+
if (res.code === 4000) {
|
|
1624
|
+
throw new tronwallet_abstract_adapter_1.WalletConnectionError('The same DApp has already initiated a request to connect to trustwallet, and the pop-up window has not been closed.');
|
|
1625
|
+
}
|
|
1626
|
+
if (res.code === 4001) {
|
|
1627
|
+
throw new tronwallet_abstract_adapter_1.WalletConnectionError('The user rejected connection.');
|
|
1628
|
+
}
|
|
1629
|
+
}
|
|
1630
|
+
catch (error) {
|
|
1631
|
+
throw new tronwallet_abstract_adapter_1.WalletConnectionError(error === null || error === void 0 ? void 0 : error.message, error);
|
|
1632
|
+
}
|
|
1633
|
+
const address = ((_a = wallet.tronWeb.defaultAddress) === null || _a === void 0 ? void 0 : _a.base58) || '';
|
|
1634
|
+
this.setAddress(address);
|
|
1635
|
+
this.setState(tronwallet_abstract_adapter_1.AdapterState.Connected);
|
|
1636
|
+
this._listenEvent();
|
|
1637
|
+
this.connected && this.emit('connect', this.address || '');
|
|
1638
|
+
}
|
|
1639
|
+
catch (error) {
|
|
1640
|
+
this.emit('error', error);
|
|
1641
|
+
throw error;
|
|
1642
|
+
}
|
|
1643
|
+
finally {
|
|
1644
|
+
this._connecting = false;
|
|
1645
|
+
}
|
|
1646
|
+
});
|
|
1647
|
+
}
|
|
1648
|
+
disconnect() {
|
|
1649
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1650
|
+
this._stopListenEvent();
|
|
1651
|
+
if (this.state !== tronwallet_abstract_adapter_1.AdapterState.Connected) {
|
|
1652
|
+
return;
|
|
1653
|
+
}
|
|
1654
|
+
this.setAddress(null);
|
|
1655
|
+
this.setState(tronwallet_abstract_adapter_1.AdapterState.Disconnect);
|
|
1656
|
+
this.emit('disconnect');
|
|
1657
|
+
});
|
|
1658
|
+
}
|
|
1659
|
+
signTransaction(transaction, privateKey) {
|
|
1660
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1661
|
+
try {
|
|
1662
|
+
const wallet = yield this.checkAndGetWallet();
|
|
1663
|
+
try {
|
|
1664
|
+
return yield wallet.tronWeb.trx.sign(transaction, privateKey);
|
|
1665
|
+
}
|
|
1666
|
+
catch (error) {
|
|
1667
|
+
if (error instanceof Error || (typeof error === 'object' && error.message)) {
|
|
1668
|
+
throw new tronwallet_abstract_adapter_1.WalletSignTransactionError(error.message, error);
|
|
1669
|
+
}
|
|
1670
|
+
else if (typeof error === 'string') {
|
|
1671
|
+
throw new tronwallet_abstract_adapter_1.WalletSignTransactionError(error, new Error(error));
|
|
1672
|
+
}
|
|
1673
|
+
else {
|
|
1674
|
+
throw new tronwallet_abstract_adapter_1.WalletSignTransactionError('Unknown error', error);
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1678
|
+
catch (error) {
|
|
1679
|
+
this.emit('error', error);
|
|
1680
|
+
throw error;
|
|
1681
|
+
}
|
|
1682
|
+
});
|
|
1683
|
+
}
|
|
1684
|
+
multiSign(transaction, privateKey, permissionId) {
|
|
1685
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1686
|
+
try {
|
|
1687
|
+
const wallet = yield this.checkAndGetWallet();
|
|
1688
|
+
try {
|
|
1689
|
+
return yield wallet.tronWeb.trx.multiSign(transaction, privateKey, permissionId);
|
|
1690
|
+
}
|
|
1691
|
+
catch (error) {
|
|
1692
|
+
if (error instanceof Error || (typeof error === 'object' && error.message)) {
|
|
1693
|
+
throw new tronwallet_abstract_adapter_1.WalletSignTransactionError(error.message, error);
|
|
1694
|
+
}
|
|
1695
|
+
else if (typeof error === 'string') {
|
|
1696
|
+
throw new tronwallet_abstract_adapter_1.WalletSignTransactionError(error, new Error(error));
|
|
1697
|
+
}
|
|
1698
|
+
else {
|
|
1699
|
+
throw new tronwallet_abstract_adapter_1.WalletSignTransactionError('Unknown error', error);
|
|
1700
|
+
}
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
catch (error) {
|
|
1704
|
+
this.emit('error', error);
|
|
1705
|
+
throw error;
|
|
1706
|
+
}
|
|
1707
|
+
});
|
|
1708
|
+
}
|
|
1709
|
+
signMessage(message, privateKey) {
|
|
1710
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1711
|
+
try {
|
|
1712
|
+
const wallet = yield this.checkAndGetWallet();
|
|
1713
|
+
try {
|
|
1714
|
+
return yield wallet.tronWeb.trx.signMessageV2(message, privateKey);
|
|
1715
|
+
}
|
|
1716
|
+
catch (error) {
|
|
1717
|
+
if (error instanceof Error || (typeof error === 'object' && error.message)) {
|
|
1718
|
+
throw new tronwallet_abstract_adapter_1.WalletSignMessageError(error.message, error);
|
|
1719
|
+
}
|
|
1720
|
+
else if (typeof error === 'string') {
|
|
1721
|
+
throw new tronwallet_abstract_adapter_1.WalletSignMessageError(error, new Error(error));
|
|
1722
|
+
}
|
|
1723
|
+
else {
|
|
1724
|
+
throw new tronwallet_abstract_adapter_1.WalletSignMessageError('Unknown error', error);
|
|
1725
|
+
}
|
|
1726
|
+
}
|
|
1727
|
+
}
|
|
1728
|
+
catch (error) {
|
|
1729
|
+
this.emit('error', error);
|
|
1730
|
+
throw error;
|
|
1731
|
+
}
|
|
1732
|
+
});
|
|
1733
|
+
}
|
|
1734
|
+
checkAndGetWallet() {
|
|
1735
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1736
|
+
this.checkIfopenTrustWallet();
|
|
1737
|
+
yield this._checkWallet();
|
|
1738
|
+
if (this.state !== tronwallet_abstract_adapter_1.AdapterState.Connected)
|
|
1739
|
+
throw new tronwallet_abstract_adapter_1.WalletDisconnectedError();
|
|
1740
|
+
const wallet = this._wallet;
|
|
1741
|
+
if (!wallet || !wallet.tronWeb)
|
|
1742
|
+
throw new tronwallet_abstract_adapter_1.WalletDisconnectedError();
|
|
1743
|
+
return wallet;
|
|
1744
|
+
});
|
|
1745
|
+
}
|
|
1746
|
+
_listenEvent() {
|
|
1747
|
+
this._stopListenEvent();
|
|
1748
|
+
window.addEventListener('message', this.messageHandler);
|
|
1749
|
+
}
|
|
1750
|
+
_stopListenEvent() {
|
|
1751
|
+
window.removeEventListener('message', this.messageHandler);
|
|
1752
|
+
}
|
|
1753
|
+
checkIfopenTrustWallet() {
|
|
1754
|
+
if (this.config.openAppWithDeeplink === false) {
|
|
1755
|
+
return;
|
|
1756
|
+
}
|
|
1757
|
+
if ((0, utils_js_1.openTrustWallet)()) {
|
|
1758
|
+
throw new tronwallet_abstract_adapter_1.WalletNotFoundError();
|
|
1759
|
+
}
|
|
1760
|
+
}
|
|
1761
|
+
/**
|
|
1762
|
+
* check if wallet exists by interval, the promise only resolve when wallet detected or timeout
|
|
1763
|
+
* @returns if trustwallet exists
|
|
1764
|
+
*/
|
|
1765
|
+
_checkWallet() {
|
|
1766
|
+
if (this.readyState === tronwallet_abstract_adapter_1.WalletReadyState.Found) {
|
|
1767
|
+
return Promise.resolve(true);
|
|
1768
|
+
}
|
|
1769
|
+
if (this._checkPromise) {
|
|
1770
|
+
return this._checkPromise;
|
|
1771
|
+
}
|
|
1772
|
+
const interval = 100;
|
|
1773
|
+
const maxTimes = Math.floor(this.config.checkTimeout / interval);
|
|
1774
|
+
let times = 0, timer;
|
|
1775
|
+
this._checkPromise = new Promise((resolve) => {
|
|
1776
|
+
const check = () => {
|
|
1777
|
+
times++;
|
|
1778
|
+
const isSupport = (0, utils_js_1.supportTrust)();
|
|
1779
|
+
if (isSupport || times > maxTimes) {
|
|
1780
|
+
timer && clearInterval(timer);
|
|
1781
|
+
this._readyState = isSupport ? tronwallet_abstract_adapter_1.WalletReadyState.Found : tronwallet_abstract_adapter_1.WalletReadyState.NotFound;
|
|
1782
|
+
this._updateWallet();
|
|
1783
|
+
this.emit('readyStateChanged', this.readyState);
|
|
1784
|
+
resolve(isSupport);
|
|
1785
|
+
}
|
|
1786
|
+
};
|
|
1787
|
+
timer = setInterval(check, interval);
|
|
1788
|
+
check();
|
|
1789
|
+
});
|
|
1790
|
+
return this._checkPromise;
|
|
1791
|
+
}
|
|
1792
|
+
setAddress(address) {
|
|
1793
|
+
this._address = address;
|
|
1794
|
+
}
|
|
1795
|
+
setState(state) {
|
|
1796
|
+
const preState = this.state;
|
|
1797
|
+
if (state !== preState) {
|
|
1798
|
+
this._state = state;
|
|
1799
|
+
this.emit('stateChanged', state);
|
|
1800
|
+
}
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
exports.TrustAdapter = TrustAdapter;
|
|
1804
|
+
|
|
1805
|
+
} (adapter$2));
|
|
1806
|
+
return adapter$2;
|
|
1807
|
+
}
|
|
1808
|
+
|
|
1809
|
+
var hasRequiredCjs;
|
|
1810
|
+
|
|
1811
|
+
function requireCjs () {
|
|
1812
|
+
if (hasRequiredCjs) return cjs$2;
|
|
1813
|
+
hasRequiredCjs = 1;
|
|
1814
|
+
(function (exports) {
|
|
1815
|
+
var __createBinding = (cjs$2 && cjs$2.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
1816
|
+
if (k2 === undefined) k2 = k;
|
|
1817
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
1818
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
1819
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
1820
|
+
}
|
|
1821
|
+
Object.defineProperty(o, k2, desc);
|
|
1822
|
+
}) : (function(o, m, k, k2) {
|
|
1823
|
+
if (k2 === undefined) k2 = k;
|
|
1824
|
+
o[k2] = m[k];
|
|
1825
|
+
}));
|
|
1826
|
+
var __exportStar = (cjs$2 && cjs$2.__exportStar) || function(m, exports) {
|
|
1827
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
1828
|
+
};
|
|
1829
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1830
|
+
__exportStar(/*@__PURE__*/ requireAdapter(), exports);
|
|
1831
|
+
__exportStar(/*@__PURE__*/ requireUtils(), exports);
|
|
1832
|
+
|
|
1833
|
+
} (cjs$2));
|
|
1834
|
+
return cjs$2;
|
|
1835
|
+
}
|
|
1734
1836
|
|
|
1735
|
-
var
|
|
1837
|
+
var cjsExports = requireCjs();
|
|
1838
|
+
var index = /*@__PURE__*/getDefaultExportFromCjs(cjsExports);
|
|
1736
1839
|
|
|
1737
1840
|
return index;
|
|
1738
1841
|
|