@sky-mavis/tanto-connect 0.0.1-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/README.md +0 -0
  2. package/dist/index.js +799 -0
  3. package/dist/types/common/chain.d.ts +20 -0
  4. package/dist/types/common/chain.d.ts.map +1 -0
  5. package/dist/types/common/connectors.d.ts +17 -0
  6. package/dist/types/common/connectors.d.ts.map +1 -0
  7. package/dist/types/common/constant.d.ts +14 -0
  8. package/dist/types/common/constant.d.ts.map +1 -0
  9. package/dist/types/common/storage.d.ts +13 -0
  10. package/dist/types/common/storage.d.ts.map +1 -0
  11. package/dist/types/connectors/base/BaseConnector.d.ts +28 -0
  12. package/dist/types/connectors/base/BaseConnector.d.ts.map +1 -0
  13. package/dist/types/connectors/index.d.ts +11 -0
  14. package/dist/types/connectors/index.d.ts.map +1 -0
  15. package/dist/types/connectors/injected/InjectedConnector.d.ts +23 -0
  16. package/dist/types/connectors/injected/InjectedConnector.d.ts.map +1 -0
  17. package/dist/types/connectors/ronin-wallet/RoninWalletConnector.d.ts +22 -0
  18. package/dist/types/connectors/ronin-wallet/RoninWalletConnector.d.ts.map +1 -0
  19. package/dist/types/connectors/ronin-wallet/RoninWalletConnector.test.d.ts +2 -0
  20. package/dist/types/connectors/ronin-wallet/RoninWalletConnector.test.d.ts.map +1 -0
  21. package/dist/types/connectors/ronin-wallet-connect/RoninWalletConnectConnector.d.ts +34 -0
  22. package/dist/types/connectors/ronin-wallet-connect/RoninWalletConnectConnector.d.ts.map +1 -0
  23. package/dist/types/connectors/safe/SafeConnector.d.ts +21 -0
  24. package/dist/types/connectors/safe/SafeConnector.d.ts.map +1 -0
  25. package/dist/types/connectors/waypoint/WaypointConnector.d.ts +21 -0
  26. package/dist/types/connectors/waypoint/WaypointConnector.d.ts.map +1 -0
  27. package/dist/types/index.d.ts +18 -0
  28. package/dist/types/index.d.ts.map +1 -0
  29. package/dist/types/providers/eip6963.d.ts +3 -0
  30. package/dist/types/providers/eip6963.d.ts.map +1 -0
  31. package/dist/types/providers/index.d.ts +6 -0
  32. package/dist/types/providers/index.d.ts.map +1 -0
  33. package/dist/types/providers/ronin-wallet-connect.d.ts +3 -0
  34. package/dist/types/providers/ronin-wallet-connect.d.ts.map +1 -0
  35. package/dist/types/providers/ronin.d.ts +4 -0
  36. package/dist/types/providers/ronin.d.ts.map +1 -0
  37. package/dist/types/providers/safe-provider.d.ts +3 -0
  38. package/dist/types/providers/safe-provider.d.ts.map +1 -0
  39. package/dist/types/providers/waypoint-provider.d.ts +3 -0
  40. package/dist/types/providers/waypoint-provider.d.ts.map +1 -0
  41. package/dist/types/types/connector-error.d.ts +16 -0
  42. package/dist/types/types/connector-error.d.ts.map +1 -0
  43. package/dist/types/types/connector-event.d.ts +49 -0
  44. package/dist/types/types/connector-event.d.ts.map +1 -0
  45. package/dist/types/types/connector.d.ts +34 -0
  46. package/dist/types/types/connector.d.ts.map +1 -0
  47. package/dist/types/types/eip1193.d.ts +40 -0
  48. package/dist/types/types/eip1193.d.ts.map +1 -0
  49. package/dist/types/types/eip6963.d.ts +19 -0
  50. package/dist/types/types/eip6963.d.ts.map +1 -0
  51. package/dist/types/utils/index.d.ts +3 -0
  52. package/dist/types/utils/index.d.ts.map +1 -0
  53. package/package.json +53 -0
package/dist/index.js ADDED
@@ -0,0 +1,799 @@
1
+ import {EthereumProvider}from'@walletconnect/ethereum-provider';import {SafeAppProvider}from'@safe-global/safe-apps-provider';import SafeAppsSDK from'@safe-global/safe-apps-sdk';import {WaypointProvider}from'@sky-mavis/waypoint';import {EventEmitter}from'events';var ChainIds;
2
+ (function (ChainIds) {
3
+ ChainIds[ChainIds["Ethereum"] = 1] = "Ethereum";
4
+ ChainIds[ChainIds["Goerli"] = 5] = "Goerli";
5
+ ChainIds[ChainIds["RoninMainet"] = 2020] = "RoninMainet";
6
+ ChainIds[ChainIds["RoninTestnet"] = 2021] = "RoninTestnet";
7
+ })(ChainIds || (ChainIds = {}));
8
+ const CHAINS_CONFIG = {
9
+ [ChainIds.RoninMainet]: {
10
+ chainId: ChainIds.RoninMainet,
11
+ blockExplorerUrl: 'https://app.roninchain.com',
12
+ chainName: 'Ronin Mainnet',
13
+ iconUrl: 'https://cdn.skymavis.com/explorer-cdn/asset/favicon/apple-touch-icon.png',
14
+ nativeCurrency: {
15
+ name: 'Ronin',
16
+ symbol: 'RON',
17
+ decimals: 18,
18
+ },
19
+ },
20
+ [ChainIds.RoninTestnet]: {
21
+ chainId: ChainIds.RoninTestnet,
22
+ blockExplorerUrl: 'https://saigon-app.roninchain.com',
23
+ chainName: 'Saigon Testnet',
24
+ iconUrl: 'https://cdn.skymavis.com/explorer-cdn/asset/favicon/apple-touch-icon.png',
25
+ nativeCurrency: {
26
+ name: 'tRonin',
27
+ symbol: 'tRON',
28
+ decimals: 18,
29
+ },
30
+ },
31
+ [ChainIds.Ethereum]: {
32
+ chainId: ChainIds.Ethereum,
33
+ blockExplorerUrl: 'https://etherscan.io',
34
+ chainName: 'Ethereum',
35
+ nativeCurrency: {
36
+ name: 'Ethereum',
37
+ symbol: 'ETH',
38
+ decimals: 18,
39
+ },
40
+ },
41
+ [ChainIds.Goerli]: {
42
+ chainId: ChainIds.Goerli,
43
+ blockExplorerUrl: 'https://goerli.etherscan.io/',
44
+ chainName: 'Goerli',
45
+ nativeCurrency: {
46
+ name: 'GoerliETH',
47
+ symbol: 'GTH',
48
+ decimals: 18,
49
+ },
50
+ },
51
+ };var SupportedConnectors;
52
+ (function (SupportedConnectors) {
53
+ SupportedConnectors["RONIN_WALLET"] = "RONIN_WALLET";
54
+ SupportedConnectors["RONIN_WC"] = "RONIN_WC";
55
+ SupportedConnectors["INJECTED"] = "INJECTED";
56
+ SupportedConnectors["WAYPOINT"] = "WAYPOINT";
57
+ SupportedConnectors["SAFE"] = "SAFE";
58
+ SupportedConnectors["COINBASE_WALLET"] = "COINBASE_WALLET";
59
+ })(SupportedConnectors || (SupportedConnectors = {}));
60
+ var ConnectorType;
61
+ (function (ConnectorType) {
62
+ ConnectorType["WALLET"] = "WALLET";
63
+ ConnectorType["MULTISIG"] = "MULTISIG";
64
+ ConnectorType["MPC"] = "MPC";
65
+ ConnectorType["WC"] = "WC";
66
+ })(ConnectorType || (ConnectorType = {}));
67
+ const DEFAULT_CONNECTORS_CONFIG = {
68
+ [SupportedConnectors.RONIN_WALLET]: {
69
+ name: 'Ronin Wallet',
70
+ id: SupportedConnectors.RONIN_WALLET,
71
+ type: ConnectorType.WALLET,
72
+ },
73
+ [SupportedConnectors.RONIN_WC]: {
74
+ name: 'Ronin Mobile',
75
+ id: SupportedConnectors.RONIN_WC,
76
+ type: ConnectorType.WC,
77
+ },
78
+ [SupportedConnectors.WAYPOINT]: {
79
+ name: 'Ronin Waypoint',
80
+ id: SupportedConnectors.WAYPOINT,
81
+ type: ConnectorType.MPC,
82
+ },
83
+ [SupportedConnectors.INJECTED]: {
84
+ name: 'Injected Providers',
85
+ id: SupportedConnectors.INJECTED,
86
+ type: ConnectorType.WALLET,
87
+ },
88
+ [SupportedConnectors.SAFE]: {
89
+ name: 'Gnosis Safe',
90
+ id: SupportedConnectors.SAFE,
91
+ type: ConnectorType.MULTISIG,
92
+ },
93
+ [SupportedConnectors.COINBASE_WALLET]: {
94
+ name: 'Coinbase Wallet',
95
+ id: SupportedConnectors.COINBASE_WALLET,
96
+ type: ConnectorType.WALLET,
97
+ },
98
+ };const RONIN_WALLET_RDNS = 'com.roninchain.wallet';
99
+ const DEFAULT_DELAY_TIME = 700;
100
+ const WAYPOINT_CLIENT_ID = 'ced25363-9cab-4e50-b1bc-0e583b93c3a2';
101
+ const WC_SUPPORTED_CHAIN_IDS = [ChainIds.RoninMainet, ChainIds.RoninTestnet];
102
+ const WC_RPC_MAP = {
103
+ [ChainIds.RoninMainet]: 'https://api.roninchain.com/rpc',
104
+ [ChainIds.RoninTestnet]: 'https://saigon-testnet.roninchain.com/rpc',
105
+ };
106
+ const WC_CAIP_CHAIN = 'eip155';
107
+ const WC_SUPPORTED_METHODS = ['eth_sendTransaction', 'eth_sign', 'personal_sign', 'eth_signTypedData'];
108
+ const WC_SUPPORTED_OPTIONAL_METHODS = [
109
+ 'eth_accounts',
110
+ 'eth_requestAccounts',
111
+ 'eth_signTypedData_v4',
112
+ 'eth_getFreeGasRequests',
113
+ 'wallet_initialData',
114
+ ];const STORAGE_PREFIX = 'tanto';
115
+ const LocalStorage = {
116
+ isAccessible: () => typeof localStorage !== 'undefined',
117
+ set: (key, data) => {
118
+ if (!LocalStorage.isAccessible()) {
119
+ return false;
120
+ }
121
+ localStorage.setItem(key, JSON.stringify(data));
122
+ return true;
123
+ },
124
+ get: (key) => {
125
+ if (!LocalStorage.isAccessible()) {
126
+ return undefined;
127
+ }
128
+ const data = localStorage.getItem(key);
129
+ if (data) {
130
+ return JSON.parse(data);
131
+ }
132
+ return undefined;
133
+ },
134
+ remove: (key) => {
135
+ if (!LocalStorage.isAccessible()) {
136
+ return false;
137
+ }
138
+ localStorage.removeItem(key);
139
+ return true;
140
+ },
141
+ };
142
+ const ReconnectStorage = {
143
+ get: (connectorId) => LocalStorage.get(`${STORAGE_PREFIX}.RECONNECT.${connectorId}`) ?? false,
144
+ add: (connectorId) => LocalStorage.set(`${STORAGE_PREFIX}.RECONNECT.${connectorId}`, true),
145
+ remove: (connectorId) => LocalStorage.remove(`${STORAGE_PREFIX}.RECONNECT.${connectorId}`),
146
+ };var EIP6963EventNames;
147
+ (function (EIP6963EventNames) {
148
+ EIP6963EventNames["AnnounceProvider"] = "eip6963:announceProvider";
149
+ EIP6963EventNames["RequestProvider"] = "eip6963:requestProvider";
150
+ })(EIP6963EventNames || (EIP6963EventNames = {}));const injectedProviders = [];
151
+ const requestProviders = async (delay = DEFAULT_DELAY_TIME) => {
152
+ if (typeof window === 'undefined') {
153
+ return [];
154
+ }
155
+ const handlerAnnouncement = (event) => {
156
+ const isAnnounced = injectedProviders.some(detail => detail.info.uuid === event.detail.info.uuid);
157
+ if (!isAnnounced) {
158
+ injectedProviders.push(event.detail);
159
+ }
160
+ };
161
+ window.addEventListener(EIP6963EventNames.AnnounceProvider, handlerAnnouncement);
162
+ window.dispatchEvent(new CustomEvent(EIP6963EventNames.RequestProvider));
163
+ await new Promise(resolve => setTimeout(resolve, delay));
164
+ window.removeEventListener(EIP6963EventNames.AnnounceProvider, handlerAnnouncement);
165
+ return injectedProviders;
166
+ };var ConnectorErrorType;
167
+ (function (ConnectorErrorType) {
168
+ ConnectorErrorType["PROVIDER_NOT_FOUND"] = "ProviderNotFound";
169
+ ConnectorErrorType["WALLET_IS_LOCKED"] = "WalletIsLocked";
170
+ ConnectorErrorType["CONNECT_FAILED"] = "ConnectFailed";
171
+ ConnectorErrorType["USER_REJECTED_SESSION_REQUEST"] = "UserRejectedSessionRequest";
172
+ ConnectorErrorType["INVALID_PARAMS"] = "InvalidParams";
173
+ ConnectorErrorType["NOT_INSTALLED"] = "NotInstalled";
174
+ ConnectorErrorType["UNKNOWN"] = "Unknown";
175
+ ConnectorErrorType["SWITCH_CHAIN_NOT_SUPPORTED"] = "SwitchChainNotSupported";
176
+ })(ConnectorErrorType || (ConnectorErrorType = {}));
177
+ class ConnectorError extends Error {
178
+ name;
179
+ error;
180
+ constructor(name, error) {
181
+ super();
182
+ this.name = name;
183
+ this.error = error;
184
+ }
185
+ }const checkRoninInstalled = () => {
186
+ if (typeof window !== 'undefined') {
187
+ return window.ronin !== undefined;
188
+ }
189
+ else {
190
+ return false;
191
+ }
192
+ };
193
+ function numberToHex(value) {
194
+ return `0x${value.toString(16)}`;
195
+ }const requestRoninProvider = async (delay = DEFAULT_DELAY_TIME) => {
196
+ const providersDetail = await requestProviders(delay);
197
+ const roninProvider = providersDetail.find(({ info }) => info.rdns === RONIN_WALLET_RDNS)?.provider;
198
+ if (roninProvider) {
199
+ return roninProvider;
200
+ }
201
+ else {
202
+ throw new ConnectorError(ConnectorErrorType.PROVIDER_NOT_FOUND);
203
+ }
204
+ };
205
+ const requestLegacyRoninProvider = async (delay = DEFAULT_DELAY_TIME) => {
206
+ if (checkRoninInstalled()) {
207
+ return window.ronin?.provider;
208
+ }
209
+ // Wait for the provider to be injected if it's not available yet
210
+ await new Promise(resolve => setTimeout(resolve, delay));
211
+ if (checkRoninInstalled()) {
212
+ return window.ronin?.provider;
213
+ }
214
+ throw new ConnectorError(ConnectorErrorType.NOT_INSTALLED);
215
+ };const requestRoninWalletConnectProvider = (options) => {
216
+ return EthereumProvider.init({
217
+ chains: WC_SUPPORTED_CHAIN_IDS,
218
+ methods: WC_SUPPORTED_METHODS,
219
+ optionalMethods: WC_SUPPORTED_OPTIONAL_METHODS,
220
+ rpcMap: WC_RPC_MAP,
221
+ ...options,
222
+ });
223
+ };const requestSafeProvider = async (delay = DEFAULT_DELAY_TIME) => {
224
+ const safeSDK = new SafeAppsSDK();
225
+ const safeInfo = await Promise.race([
226
+ safeSDK.safe.getInfo(),
227
+ new Promise(resolve => setTimeout(resolve, delay)),
228
+ ]);
229
+ if (!safeInfo) {
230
+ throw new ConnectorError(ConnectorErrorType.PROVIDER_NOT_FOUND);
231
+ }
232
+ const safeProvider = new SafeAppProvider(safeInfo, safeSDK);
233
+ return safeProvider;
234
+ };const requestWaypointProvider = (chainId) => {
235
+ const waypointProvider = WaypointProvider.create({
236
+ clientId: WAYPOINT_CLIENT_ID,
237
+ chainId: chainId || ChainIds.RoninMainet,
238
+ });
239
+ if (!waypointProvider) {
240
+ throw new ConnectorError(ConnectorErrorType.PROVIDER_NOT_FOUND);
241
+ }
242
+ return waypointProvider;
243
+ };var EIP1193Event;
244
+ (function (EIP1193Event) {
245
+ EIP1193Event["CONNECT"] = "connect";
246
+ EIP1193Event["DISCONNECT"] = "disconnect";
247
+ EIP1193Event["ACCOUNTS_CHANGED"] = "accountsChanged";
248
+ EIP1193Event["CHAIN_CHANGED"] = "chainChanged";
249
+ EIP1193Event["MESSAGE"] = "message";
250
+ })(EIP1193Event || (EIP1193Event = {}));var WCEvent;
251
+ (function (WCEvent) {
252
+ WCEvent["DISPLAY_URI"] = "display_uri";
253
+ WCEvent["SESSION_PING"] = "session_ping";
254
+ WCEvent["SESSION_UPDATE"] = "session_update";
255
+ WCEvent["SESSION_EVENT"] = "session_event";
256
+ WCEvent["SESSION_DELETE"] = "session_delete";
257
+ })(WCEvent || (WCEvent = {}));
258
+ var ConnectorEvent;
259
+ (function (ConnectorEvent) {
260
+ ConnectorEvent["CONNECT"] = "connect";
261
+ ConnectorEvent["DISCONNECT"] = "disconnect";
262
+ ConnectorEvent["ACCOUNTS_CHANGED"] = "accountsChanged";
263
+ ConnectorEvent["CHAIN_CHANGED"] = "chainChanged";
264
+ ConnectorEvent["MESSAGE"] = "message";
265
+ ConnectorEvent["DISPLAY_URI"] = "display_uri";
266
+ ConnectorEvent["SESSION_DELETE"] = "session_delete";
267
+ })(ConnectorEvent || (ConnectorEvent = {}));
268
+ class ConnectorEventEmitter extends EventEmitter {
269
+ emit(event, payload) {
270
+ return super.emit(event, payload);
271
+ }
272
+ on(event, listener) {
273
+ return super.on(event, listener);
274
+ }
275
+ once(event, listener) {
276
+ return super.once(event, listener);
277
+ }
278
+ off(event, listener) {
279
+ return super.off(event, listener);
280
+ }
281
+ }class BaseConnector extends ConnectorEventEmitter {
282
+ id;
283
+ name;
284
+ type;
285
+ isRonin;
286
+ icon;
287
+ provider;
288
+ constructor(configs, provider) {
289
+ const { id, name, icon, type } = configs;
290
+ super();
291
+ this.id = id;
292
+ this.name = name;
293
+ this.icon = icon;
294
+ this.type = type;
295
+ this.isRonin = false;
296
+ this.provider = provider;
297
+ }
298
+ getProvider = async () => {
299
+ if (!this.provider) {
300
+ this.provider = await this.requestProvider();
301
+ }
302
+ return this.provider;
303
+ };
304
+ autoConnect = async () => {
305
+ try {
306
+ const shouldConnect = await this.shouldAutoReconnect();
307
+ if (shouldConnect)
308
+ await this.connect();
309
+ }
310
+ catch (e) {
311
+ console.error(e);
312
+ }
313
+ };
314
+ shouldAutoReconnect = async () => {
315
+ const isReconnect = ReconnectStorage.get(this.id);
316
+ const isAuthorized = await this.isAuthorized();
317
+ return isReconnect && isAuthorized;
318
+ };
319
+ onChainChanged = (chainId) => {
320
+ this.emit(ConnectorEvent.CHAIN_CHANGED, Number(chainId));
321
+ };
322
+ onAccountsChanged = (accounts) => {
323
+ if (accounts.length === 0) {
324
+ this.onDisconnect();
325
+ }
326
+ this.emit(ConnectorEvent.ACCOUNTS_CHANGED, accounts);
327
+ };
328
+ onConnect = (results) => {
329
+ this.emit(ConnectorEvent.CONNECT, results);
330
+ };
331
+ onDisconnect = () => {
332
+ this.emit(ConnectorEvent.DISCONNECT);
333
+ };
334
+ }class InjectedConnector extends BaseConnector {
335
+ isRonin;
336
+ provider;
337
+ constructor(configs, provider) {
338
+ super(configs);
339
+ this.isRonin = !!provider.isRonin;
340
+ this.provider = provider;
341
+ }
342
+ async connect(chainId) {
343
+ const provider = await this.getProvider();
344
+ if (!provider) {
345
+ throw new ConnectorError(ConnectorErrorType.PROVIDER_NOT_FOUND);
346
+ }
347
+ try {
348
+ const accounts = await this.requestAccounts();
349
+ const currentChainId = await this.getChainId();
350
+ if (chainId && currentChainId !== chainId) {
351
+ await this.switchChain(chainId);
352
+ }
353
+ const connectResults = {
354
+ provider,
355
+ chainId: chainId || currentChainId,
356
+ account: accounts[0],
357
+ };
358
+ this.setupProviderListeners();
359
+ this.onConnect(connectResults);
360
+ ReconnectStorage.add(this.id);
361
+ return connectResults;
362
+ }
363
+ catch (err) {
364
+ throw new ConnectorError(ConnectorErrorType.CONNECT_FAILED, err);
365
+ }
366
+ }
367
+ async disconnect() {
368
+ this.onDisconnect();
369
+ this.removeAllListeners();
370
+ this.removeProviderListeners();
371
+ }
372
+ async isAuthorized() {
373
+ const accounts = await this.getAccounts();
374
+ return accounts.length > 0;
375
+ }
376
+ async getAccounts() {
377
+ const provider = await this.getProvider();
378
+ return provider.request({
379
+ method: 'eth_accounts',
380
+ });
381
+ }
382
+ async switchChain(chain) {
383
+ const provider = await this.getProvider();
384
+ const chainId = provider?.request({
385
+ method: 'wallet_switchEthereumChain',
386
+ params: [{ chainId: numberToHex(chain) }],
387
+ });
388
+ return !!chainId;
389
+ }
390
+ async getChainId() {
391
+ const provider = await this.getProvider();
392
+ const chainId = await provider?.request({
393
+ method: 'eth_chainId',
394
+ });
395
+ return Number(chainId);
396
+ }
397
+ async requestAccounts() {
398
+ const provider = await this.getProvider();
399
+ return provider?.request({
400
+ method: 'eth_requestAccounts',
401
+ });
402
+ }
403
+ async requestProvider() {
404
+ return this.provider;
405
+ }
406
+ setupProviderListeners() {
407
+ if (this.provider) {
408
+ this.provider.on(EIP1193Event.DISCONNECT, this.onDisconnect);
409
+ this.provider.on(EIP1193Event.ACCOUNTS_CHANGED, this.onAccountsChanged);
410
+ this.provider.on(EIP1193Event.CHAIN_CHANGED, this.onChainChanged);
411
+ }
412
+ }
413
+ removeProviderListeners() {
414
+ if (this.provider) {
415
+ this.provider.removeListener(EIP1193Event.DISCONNECT, this.onDisconnect);
416
+ this.provider.removeListener(EIP1193Event.ACCOUNTS_CHANGED, this.onAccountsChanged);
417
+ this.provider.removeListener(EIP1193Event.CHAIN_CHANGED, this.onChainChanged);
418
+ }
419
+ }
420
+ }class RoninWalletConnector extends BaseConnector {
421
+ isRonin;
422
+ constructor(configs, provider) {
423
+ super({ ...DEFAULT_CONNECTORS_CONFIG.RONIN_WALLET, ...configs }, provider);
424
+ this.isRonin = true;
425
+ }
426
+ async connect(chainId) {
427
+ const provider = await this.getProvider();
428
+ if (!provider) {
429
+ throw new ConnectorError(ConnectorErrorType.PROVIDER_NOT_FOUND);
430
+ }
431
+ const accounts = await this.requestAccounts();
432
+ const currentChainId = await this.getChainId();
433
+ if (chainId && currentChainId !== chainId) {
434
+ await this.switchChain(chainId);
435
+ }
436
+ const connectResults = {
437
+ provider,
438
+ chainId: chainId || currentChainId,
439
+ account: accounts[0],
440
+ };
441
+ this.setupProviderListeners();
442
+ this.onConnect(connectResults);
443
+ ReconnectStorage.add(this.id);
444
+ return connectResults;
445
+ }
446
+ async disconnect() {
447
+ ReconnectStorage.remove(this.id);
448
+ this.onDisconnect();
449
+ this.removeAllListeners();
450
+ this.removeProviderListeners();
451
+ }
452
+ async isAuthorized() {
453
+ const accounts = await this.getAccounts();
454
+ return accounts.length > 0;
455
+ }
456
+ async getAccounts() {
457
+ const provider = await this.getProvider();
458
+ return provider.request({
459
+ method: 'eth_accounts',
460
+ });
461
+ }
462
+ async switchChain(chain) {
463
+ const provider = await this.getProvider();
464
+ const chainId = provider?.request({
465
+ method: 'wallet_switchEthereumChain',
466
+ params: [{ chainId: numberToHex(chain) }],
467
+ });
468
+ return !!chainId;
469
+ }
470
+ async getChainId() {
471
+ const provider = await this.getProvider();
472
+ const chainId = await provider?.request({
473
+ method: 'eth_chainId',
474
+ });
475
+ return Number(chainId);
476
+ }
477
+ async requestAccounts() {
478
+ const provider = await this.getProvider();
479
+ return provider?.request({
480
+ method: 'eth_requestAccounts',
481
+ });
482
+ }
483
+ async requestProvider() {
484
+ return requestRoninProvider();
485
+ }
486
+ setupProviderListeners() {
487
+ this.removeProviderListeners();
488
+ if (this.provider) {
489
+ this.provider.on(EIP1193Event.DISCONNECT, this.onDisconnect);
490
+ this.provider.on(EIP1193Event.ACCOUNTS_CHANGED, this.onAccountsChanged);
491
+ this.provider.on(EIP1193Event.CHAIN_CHANGED, this.onChainChanged);
492
+ }
493
+ }
494
+ removeProviderListeners() {
495
+ if (this.provider) {
496
+ this.provider.removeListener(EIP1193Event.DISCONNECT, this.onDisconnect);
497
+ this.provider.removeListener(EIP1193Event.ACCOUNTS_CHANGED, this.onAccountsChanged);
498
+ this.provider.removeListener(EIP1193Event.CHAIN_CHANGED, this.onChainChanged);
499
+ }
500
+ }
501
+ }class RoninWalletConnectConnector extends BaseConnector {
502
+ projectId;
503
+ metadata;
504
+ showQrModal;
505
+ isRonin;
506
+ constructor(configs, provider) {
507
+ const { projectId, metadata, showQrModal, ...restConfigs } = configs;
508
+ super({ ...DEFAULT_CONNECTORS_CONFIG.RONIN_WC, ...restConfigs }, provider);
509
+ this.metadata = metadata;
510
+ this.projectId = projectId;
511
+ this.showQrModal = showQrModal;
512
+ this.isRonin = true;
513
+ this.setupProviderListeners();
514
+ }
515
+ getProvider = async () => {
516
+ if (!this.provider) {
517
+ this.provider = await this.requestProvider();
518
+ }
519
+ return this.provider;
520
+ };
521
+ async connect(chainId) {
522
+ const provider = await this.getProvider();
523
+ const targetChainId = chainId ?? WC_SUPPORTED_CHAIN_IDS[0];
524
+ const isChainSupported = WC_SUPPORTED_CHAIN_IDS.includes(targetChainId);
525
+ if (!isChainSupported) {
526
+ throw new ConnectorError(ConnectorErrorType.SWITCH_CHAIN_NOT_SUPPORTED);
527
+ }
528
+ const isAuthorized = await this.isAuthorized();
529
+ if (!isAuthorized) {
530
+ await provider.connect({ chains: [targetChainId] });
531
+ }
532
+ else {
533
+ await provider.enable();
534
+ }
535
+ const connectResults = {
536
+ provider,
537
+ chainId: targetChainId,
538
+ account: provider.accounts[0],
539
+ };
540
+ ReconnectStorage.add(this.id);
541
+ this.onConnect(connectResults);
542
+ return connectResults;
543
+ }
544
+ async disconnect() {
545
+ const provider = await this.getProvider();
546
+ await provider.disconnect();
547
+ this.onDisconnect();
548
+ ReconnectStorage.remove(this.id);
549
+ this.removeAllListeners();
550
+ this.removeProviderListeners();
551
+ }
552
+ async isAuthorized() {
553
+ const accounts = await this.getAccounts();
554
+ return accounts.length > 0;
555
+ }
556
+ async getAccounts() {
557
+ const provider = await this.getProvider();
558
+ return provider.accounts;
559
+ }
560
+ async switchChain(chain) {
561
+ const provider = await this.getProvider();
562
+ const chainId = provider?.request({
563
+ method: 'wallet_switchEthereumChain',
564
+ params: [{ chainId: numberToHex(chain) }],
565
+ });
566
+ return !!chainId;
567
+ }
568
+ async getChainId() {
569
+ const provider = await this.getProvider();
570
+ const chainId = await provider?.request({
571
+ method: 'eth_chainId',
572
+ });
573
+ return Number(chainId);
574
+ }
575
+ async requestAccounts() {
576
+ const provider = await this.getProvider();
577
+ return provider?.request({
578
+ method: 'eth_requestAccounts',
579
+ });
580
+ }
581
+ async requestProvider() {
582
+ return requestRoninWalletConnectProvider({
583
+ projectId: this.projectId,
584
+ metadata: this.metadata,
585
+ showQrModal: this.isRonin,
586
+ chains: WC_SUPPORTED_CHAIN_IDS,
587
+ });
588
+ }
589
+ onDisplayUri = (uri) => {
590
+ this.emit(ConnectorEvent.DISPLAY_URI, uri);
591
+ };
592
+ setupWalletConnectEvent() {
593
+ this.getProvider().then(provider => {
594
+ provider.on(WCEvent.SESSION_DELETE, this.onDisconnect);
595
+ provider.on(WCEvent.DISPLAY_URI, this.onDisplayUri);
596
+ });
597
+ }
598
+ setupProviderListeners() {
599
+ if (this.provider) {
600
+ this.provider.on(EIP1193Event.DISCONNECT, this.onDisconnect);
601
+ this.provider.on(EIP1193Event.ACCOUNTS_CHANGED, this.onAccountsChanged);
602
+ this.provider.on(EIP1193Event.CHAIN_CHANGED, this.onChainChanged);
603
+ this.setupWalletConnectEvent();
604
+ }
605
+ }
606
+ removeProviderListeners() {
607
+ if (this.provider) {
608
+ this.provider.removeListener(EIP1193Event.DISCONNECT, this.onDisconnect);
609
+ this.provider.removeListener(EIP1193Event.ACCOUNTS_CHANGED, this.onAccountsChanged);
610
+ this.provider.removeListener(EIP1193Event.CHAIN_CHANGED, this.onChainChanged);
611
+ this.provider.removeListener(WCEvent.DISPLAY_URI, this.onDisplayUri);
612
+ this.provider.removeListener(WCEvent.SESSION_DELETE, this.onDisconnect);
613
+ }
614
+ }
615
+ }class SafeConnector extends BaseConnector {
616
+ constructor(configs, provider) {
617
+ super({ ...DEFAULT_CONNECTORS_CONFIG.SAFE, ...configs }, provider);
618
+ }
619
+ async connect(chainId) {
620
+ const provider = await this.getProvider();
621
+ if (!provider) {
622
+ throw new ConnectorError(ConnectorErrorType.PROVIDER_NOT_FOUND);
623
+ }
624
+ const accounts = await this.requestAccounts();
625
+ const currentChainId = await this.getChainId();
626
+ if (chainId && chainId !== currentChainId) {
627
+ await this.switchChain(chainId);
628
+ }
629
+ const connectResults = {
630
+ provider,
631
+ chainId: chainId || currentChainId,
632
+ account: accounts[0],
633
+ };
634
+ this.setupProviderListeners();
635
+ this.onConnect(connectResults);
636
+ return {
637
+ provider,
638
+ chainId: chainId ?? currentChainId,
639
+ account: accounts[0],
640
+ };
641
+ }
642
+ async disconnect() {
643
+ this.onDisconnect();
644
+ this.removeAllListeners();
645
+ this.removeProviderListeners();
646
+ }
647
+ async isAuthorized() {
648
+ const accounts = await this.getAccounts();
649
+ return accounts.length > 0;
650
+ }
651
+ async getAccounts() {
652
+ const provider = await this.getProvider();
653
+ return provider?.request({
654
+ method: 'eth_accounts',
655
+ });
656
+ }
657
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
658
+ async switchChain(chain) {
659
+ // TODO: Add a Connector Error for not allowing switch chain
660
+ throw new ConnectorError(ConnectorErrorType.SWITCH_CHAIN_NOT_SUPPORTED);
661
+ }
662
+ async getChainId() {
663
+ const provider = await this.getProvider();
664
+ const chainId = await provider?.request({
665
+ method: 'eth_chainId',
666
+ });
667
+ return Number(chainId);
668
+ }
669
+ async requestAccounts() {
670
+ const provider = await this.getProvider();
671
+ return provider?.request({
672
+ method: 'eth_requestAccounts',
673
+ });
674
+ }
675
+ async requestProvider() {
676
+ return await requestSafeProvider();
677
+ }
678
+ setupProviderListeners() {
679
+ this.removeProviderListeners();
680
+ if (this.provider) {
681
+ this.provider.on(EIP1193Event.DISCONNECT, this.onDisconnect);
682
+ this.provider.on(EIP1193Event.ACCOUNTS_CHANGED, this.onAccountsChanged);
683
+ }
684
+ }
685
+ removeProviderListeners() {
686
+ if (this.provider) {
687
+ this.provider.removeListener(EIP1193Event.DISCONNECT, this.onDisconnect);
688
+ this.provider.removeListener(EIP1193Event.ACCOUNTS_CHANGED, this.onAccountsChanged);
689
+ }
690
+ }
691
+ }class WaypointConnector extends BaseConnector {
692
+ constructor(configs, provider) {
693
+ super({ ...DEFAULT_CONNECTORS_CONFIG.WAYPOINT, ...configs }, provider);
694
+ }
695
+ async isAuthorized() {
696
+ const accounts = await this.getAccounts();
697
+ return accounts.length > 0;
698
+ }
699
+ async getAccounts() {
700
+ const provider = await this.getProvider();
701
+ return provider.request({
702
+ method: 'eth_accounts',
703
+ });
704
+ }
705
+ async switchChain() {
706
+ console.error(new ConnectorError(ConnectorErrorType.SWITCH_CHAIN_NOT_SUPPORTED));
707
+ return false;
708
+ }
709
+ async getChainId() {
710
+ const provider = await this.getProvider();
711
+ const chainId = await provider?.request({
712
+ method: 'eth_chainId',
713
+ });
714
+ return Number(chainId);
715
+ }
716
+ async requestAccounts() {
717
+ const provider = await this.getProvider();
718
+ return provider?.request({
719
+ method: 'eth_requestAccounts',
720
+ });
721
+ }
722
+ async connect(chainId) {
723
+ const provider = await this.getProvider();
724
+ if (!provider) {
725
+ throw new ConnectorError(ConnectorErrorType.PROVIDER_NOT_FOUND);
726
+ }
727
+ const accounts = await this.requestAccounts();
728
+ const currentChainId = await this.getChainId();
729
+ const connectResults = {
730
+ provider,
731
+ chainId: chainId || currentChainId,
732
+ account: accounts[0],
733
+ };
734
+ this.setupProviderListeners();
735
+ this.onConnect(connectResults);
736
+ ReconnectStorage.add(this.id);
737
+ return {
738
+ provider,
739
+ chainId: chainId ?? currentChainId,
740
+ account: accounts[0],
741
+ };
742
+ }
743
+ async disconnect() {
744
+ this.onDisconnect();
745
+ this.removeAllListeners();
746
+ this.removeProviderListeners();
747
+ }
748
+ async requestProvider() {
749
+ return requestWaypointProvider();
750
+ }
751
+ setupProviderListeners() {
752
+ this.removeProviderListeners();
753
+ if (this.provider) {
754
+ this.provider.on(EIP1193Event.DISCONNECT, this.onDisconnect);
755
+ }
756
+ }
757
+ removeProviderListeners() {
758
+ if (this.provider) {
759
+ this.provider.removeListener(EIP1193Event.DISCONNECT, this.onDisconnect);
760
+ }
761
+ }
762
+ }const requestInjectedConnectors = async () => {
763
+ const providerDetails = await requestProviders();
764
+ return providerDetails.map(detail => {
765
+ if (detail.info.rdns === RONIN_WALLET_RDNS) {
766
+ return new RoninWalletConnector({ icon: detail.info.icon });
767
+ }
768
+ else {
769
+ const configs = {
770
+ name: detail.info.name,
771
+ id: detail.info.rdns,
772
+ icon: detail.info.icon,
773
+ type: 'injected',
774
+ };
775
+ return new InjectedConnector(configs, detail.provider);
776
+ }
777
+ });
778
+ };
779
+ const requestRoninWalletConnector = async () => {
780
+ const provider = await requestLegacyRoninProvider();
781
+ return new RoninWalletConnector({}, provider);
782
+ };
783
+ const requestRoninWalletConnectConnector = async (configs) => {
784
+ const provider = await requestRoninWalletConnectProvider({
785
+ projectId: configs.projectId,
786
+ metadata: configs.metadata,
787
+ chains: WC_SUPPORTED_CHAIN_IDS,
788
+ showQrModal: false,
789
+ });
790
+ return new RoninWalletConnectConnector(configs, provider);
791
+ };
792
+ const requestSafeConnector = async (configs, delay = DEFAULT_DELAY_TIME) => {
793
+ const provider = await requestSafeProvider(delay);
794
+ return new SafeConnector(configs, provider);
795
+ };
796
+ const requestWaypointConnector = (config, chainId) => {
797
+ const waypointProvider = requestWaypointProvider(chainId);
798
+ return new WaypointConnector(config, waypointProvider);
799
+ };export{BaseConnector,CHAINS_CONFIG,ChainIds,ConnectorError,ConnectorErrorType,ConnectorEvent,ConnectorEventEmitter,ConnectorType,DEFAULT_CONNECTORS_CONFIG,DEFAULT_DELAY_TIME,EIP1193Event,EIP6963EventNames,InjectedConnector,LocalStorage,RONIN_WALLET_RDNS,ReconnectStorage,RoninWalletConnectConnector,RoninWalletConnector,STORAGE_PREFIX,SafeConnector,SupportedConnectors,WAYPOINT_CLIENT_ID,WCEvent,WC_CAIP_CHAIN,WC_RPC_MAP,WC_SUPPORTED_CHAIN_IDS,WC_SUPPORTED_METHODS,WC_SUPPORTED_OPTIONAL_METHODS,WaypointConnector,requestInjectedConnectors,requestLegacyRoninProvider,requestProviders,requestRoninProvider,requestRoninWalletConnectConnector,requestRoninWalletConnectProvider,requestRoninWalletConnector,requestSafeConnector,requestSafeProvider,requestWaypointConnector,requestWaypointProvider};