@tomo-inc/wallet-adaptor-base 0.0.17 → 0.0.19

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/dist/index.js CHANGED
@@ -1,3 +1,5 @@
1
+ import { ProviderProtocol, ProviderStandard, ChainTypeEnum } from '@tomo-inc/wallet-utils';
2
+ import { mainnet } from 'viem/chains';
1
3
  import { getWallets } from '@wallet-standard/core';
2
4
  import { WalletConnectClient } from '@tomo-inc/wallet-connect-protocol';
3
5
  import { toHex, isHex, fromHex } from 'viem';
@@ -70,23 +72,49 @@ function uint8arrayToHex(uint8array) {
70
72
  return Array.from(uint8array, (byte) => byte.toString(16).padStart(2, "0")).join("");
71
73
  }
72
74
 
73
- // src/type.ts
74
- var ProviderProtocol = /* @__PURE__ */ ((ProviderProtocol2) => {
75
- ProviderProtocol2["EIP6963"] = "eip6963";
76
- ProviderProtocol2["WALLET_STANDARD"] = "wallet-standard";
77
- ProviderProtocol2["WALLET_CONNECT"] = "wallet-connect";
78
- ProviderProtocol2["INJECT"] = "inject";
79
- return ProviderProtocol2;
80
- })(ProviderProtocol || {});
81
- var ProviderChainType = /* @__PURE__ */ ((ProviderChainType2) => {
82
- ProviderChainType2["inject"] = "inject";
83
- ProviderChainType2["deeplink"] = "deeplink";
84
- ProviderChainType2["walletConnect"] = "walletConnect";
85
- return ProviderChainType2;
86
- })(ProviderChainType || {});
87
- var SupportedChainTypes = ["evm", "solana", "aptos", "dogecoin"];
75
+ // src/utils/wallet-config.ts
76
+ function hasWalletConfigProperties(config) {
77
+ if (!config || typeof config !== "object") {
78
+ return false;
79
+ }
80
+ return typeof config.id === "string" && typeof config.name === "string" && typeof config.icon === "string" && typeof config.iconBackground === "string";
81
+ }
82
+ function hasWagmiConnectorProperties(config) {
83
+ if (!config || typeof config !== "object") {
84
+ return false;
85
+ }
86
+ if (typeof config.createConnector === "function") {
87
+ return true;
88
+ }
89
+ const hasConnectorMethods = typeof config.connect === "function" || typeof config.disconnect === "function" || typeof config.getAccounts === "function" || typeof config.getChainId === "function";
90
+ const hasConnectorId = typeof config.id === "string" || typeof config.name === "string";
91
+ return hasConnectorMethods && hasConnectorId;
92
+ }
93
+ function isWagmiConnector(config) {
94
+ if (typeof config === "function") {
95
+ return true;
96
+ }
97
+ if (config && typeof config === "object") {
98
+ const hasWalletConfigProps = hasWalletConfigProperties(config);
99
+ if (!hasWalletConfigProps) {
100
+ return hasWagmiConnectorProperties(config);
101
+ }
102
+ }
103
+ return false;
104
+ }
88
105
 
89
- // src/wallets/detector.ts
106
+ // src/wallets/index.ts
107
+ var walletBaseUrl = "https://web3-assets.tomo.inc";
108
+ var allWallets = [];
109
+ var getAllWallets = async (baseUrl) => {
110
+ if (allWallets.length > 0) {
111
+ return allWallets;
112
+ }
113
+ const walletListResponse = await fetch((baseUrl || walletBaseUrl) + "/api/wallets");
114
+ const walletListData = await walletListResponse.json();
115
+ const walletList = allWallets = walletListData == null ? void 0 : walletListData.data;
116
+ return walletList;
117
+ };
90
118
  function getEvmExplicitInjectedProvider(flag) {
91
119
  const _window = typeof window !== "undefined" ? window : void 0;
92
120
  if (typeof _window === "undefined" || typeof _window.ethereum === "undefined") return;
@@ -131,17 +159,67 @@ var supportedWalletConfigTypes = {
131
159
  tomo: true,
132
160
  wagmi: true
133
161
  };
134
- function walletConfigAdapter(walletConfig, connectorType) {
162
+ function findWalletInDefaultConnectors(id, name, defaultWallets) {
163
+ let found = defaultWallets.find(
164
+ (wallet) => {
165
+ var _a, _b;
166
+ return ((_a = wallet.id) == null ? void 0 : _a.toLowerCase()) === id.toLowerCase() || ((_b = wallet.id) == null ? void 0 : _b.toLowerCase()) === id.toLowerCase().replace(/\s+/g, "-");
167
+ }
168
+ );
169
+ if (!found) {
170
+ const normalizedName = name.toLowerCase();
171
+ found = defaultWallets.find((wallet) => {
172
+ const walletName = wallet.name.toLowerCase();
173
+ return walletName === normalizedName || walletName.includes(normalizedName) || normalizedName.includes(walletName) || // support common wallet name variations matching
174
+ normalizedName.includes("metamask") && walletName.includes("metamask") || normalizedName.includes("coinbase") && walletName.includes("coinbase") || normalizedName.includes("safe") && walletName.includes("safe") || normalizedName.includes("walletconnect") && walletName.includes("walletconnect") || normalizedName.includes("injected") && walletName.includes("injected");
175
+ });
176
+ }
177
+ return found || null;
178
+ }
179
+ function adaptWagmiConnectorToWalletConfig(wagmiConnector, allWallets2, baseUrl) {
180
+ var _a, _b, _c, _d;
181
+ const tempConfig = { chains: [mainnet] };
182
+ const connector = wagmiConnector(tempConfig);
183
+ const id = connector.id || "unknown";
184
+ const name = connector.name || "Unknown Wallet";
185
+ const defaultWallet = findWalletInDefaultConnectors(id, name, allWallets2);
186
+ const walletConfig = {
187
+ id,
188
+ name,
189
+ namespace: (defaultWallet == null ? void 0 : defaultWallet.namespace) || "eip155",
190
+ // default EVM namespace
191
+ icon: baseUrl + (defaultWallet == null ? void 0 : defaultWallet.icon) || "",
192
+ iconBackground: (defaultWallet == null ? void 0 : defaultWallet.iconBackground) || "#666666",
193
+ rdns: defaultWallet == null ? void 0 : defaultWallet.rdns,
194
+ downloadUrls: (defaultWallet == null ? void 0 : defaultWallet.downloadUrls) || {},
195
+ installed: (defaultWallet == null ? void 0 : defaultWallet.installed) || false,
196
+ flag: (defaultWallet == null ? void 0 : defaultWallet.flag) || "",
197
+ solana: {
198
+ namespace: ((_a = defaultWallet == null ? void 0 : defaultWallet.solana) == null ? void 0 : _a.namespace) || "",
199
+ flag: ((_b = defaultWallet == null ? void 0 : defaultWallet.solana) == null ? void 0 : _b.flag) || ""
200
+ },
201
+ aptos: {
202
+ namespace: ((_c = defaultWallet == null ? void 0 : defaultWallet.aptos) == null ? void 0 : _c.namespace) || "",
203
+ flag: ((_d = defaultWallet == null ? void 0 : defaultWallet.aptos) == null ? void 0 : _d.flag) || ""
204
+ },
205
+ createConnector: wagmiConnector
206
+ };
207
+ return walletConfig;
208
+ }
209
+ function walletConfigAdapter(walletConfig, connectorType, allWallets2, baseUrl) {
135
210
  switch (connectorType) {
136
211
  case "tomo":
137
212
  return walletConfig;
138
213
  case "wagmi":
139
- return null;
214
+ if (typeof walletConfig === "function") {
215
+ return adaptWagmiConnectorToWalletConfig(walletConfig, allWallets2, baseUrl);
216
+ }
217
+ return walletConfig;
140
218
  default:
141
219
  return walletConfig;
142
220
  }
143
221
  }
144
- function connectorDector(wallet) {
222
+ function tomoConnectorDector(wallet) {
145
223
  var _a, _b, _c, _d, _e, _f, _g;
146
224
  const evmNS = {
147
225
  namespace: (wallet == null ? void 0 : wallet.namespace) || "",
@@ -161,42 +239,39 @@ function connectorDector(wallet) {
161
239
  const isAptosExists = hasInjectedProvider(aptosNS);
162
240
  const isDogecoinExists = hasInjectedProvider(dogecoinNS);
163
241
  const isInstalled = isEvmExists || isSolanaExists || isAptosExists || isDogecoinExists;
242
+ const DEFAULT_PROVIDER = {
243
+ provider: void 0,
244
+ protocol: void 0,
245
+ standard: ProviderStandard.NORMAL
246
+ };
164
247
  const providers = {};
165
248
  if (evmNS.namespace || evmNS.flag) {
166
249
  providers.evm = isEvmExists ? {
167
250
  provider: getInjectedProvider(evmNS),
168
- protocol: "eip6963" /* EIP6963 */
169
- } : {
170
- provider: void 0,
171
- protocol: void 0
172
- };
251
+ protocol: ProviderProtocol.INJECT,
252
+ standard: ProviderStandard.EIP1193
253
+ } : DEFAULT_PROVIDER;
173
254
  }
174
255
  if (solanaNS.namespace) {
175
256
  providers.solana = isSolanaExists ? {
176
257
  provider: getInjectedProvider(solanaNS),
177
- protocol: "inject" /* INJECT */
178
- } : {
179
- provider: void 0,
180
- protocol: void 0
181
- };
258
+ protocol: ProviderProtocol.INJECT,
259
+ standard: ProviderStandard.NORMAL
260
+ } : DEFAULT_PROVIDER;
182
261
  }
183
262
  if (aptosNS.namespace) {
184
263
  providers.aptos = isAptosExists ? {
185
264
  provider: getInjectedProvider(aptosNS),
186
- protocol: "inject" /* INJECT */
187
- } : {
188
- provider: void 0,
189
- protocol: void 0
190
- };
265
+ protocol: ProviderProtocol.INJECT,
266
+ standard: ProviderStandard.NORMAL
267
+ } : DEFAULT_PROVIDER;
191
268
  }
192
269
  if (dogecoinNS.namespace) {
193
270
  providers.dogecoin = isDogecoinExists ? {
194
271
  provider: getInjectedProvider(dogecoinNS),
195
- protocol: "inject" /* INJECT */
196
- } : {
197
- provider: void 0,
198
- protocol: void 0
199
- };
272
+ protocol: ProviderProtocol.INJECT,
273
+ standard: ProviderStandard.NORMAL
274
+ } : DEFAULT_PROVIDER;
200
275
  }
201
276
  return {
202
277
  info: {
@@ -215,25 +290,53 @@ function connectorDector(wallet) {
215
290
  }
216
291
  },
217
292
  isInstalled,
218
- providers
293
+ connectors: providers
219
294
  };
220
295
  }
296
+ function wagmiConnectorDector(wallet) {
297
+ var _a, _b, _c, _d;
298
+ const tempConfig = {};
299
+ const connector = wallet.createConnector(tempConfig);
300
+ const providers = {
301
+ evm: {
302
+ provider: connector,
303
+ protocol: ProviderProtocol.INJECT,
304
+ standard: ProviderStandard.EIP1193
305
+ }
306
+ };
307
+ return {
308
+ info: {
309
+ uuid: wallet == null ? void 0 : wallet.id,
310
+ name: wallet == null ? void 0 : wallet.name,
311
+ icon: wallet == null ? void 0 : wallet.icon,
312
+ iconBackground: wallet == null ? void 0 : wallet.iconBackground,
313
+ rdns: wallet == null ? void 0 : wallet.rdns,
314
+ links: {
315
+ homepage: ((_a = wallet == null ? void 0 : wallet.downloadUrls) == null ? void 0 : _a.qrCode) || "",
316
+ ios_install: ((_b = wallet == null ? void 0 : wallet.downloadUrls) == null ? void 0 : _b.ios) || "",
317
+ android_install: ((_c = wallet == null ? void 0 : wallet.downloadUrls) == null ? void 0 : _c.android) || "",
318
+ chrome_install: ((_d = wallet == null ? void 0 : wallet.downloadUrls) == null ? void 0 : _d.chrome) || ""
319
+ }
320
+ },
321
+ isInstalled: true,
322
+ connectors: providers
323
+ };
324
+ }
325
+ function connectorDector(wallet, connectorType) {
326
+ if (connectorType === "wagmi") {
327
+ return wagmiConnectorDector(wallet);
328
+ }
329
+ return tomoConnectorDector(wallet);
330
+ }
221
331
 
222
332
  // src/wallets/defaultConnectors.ts
223
- var defaultWallets = [];
224
333
  var getDefaultConnectors = async (baseUrl) => {
225
- if (defaultWallets.length > 0) {
226
- return defaultWallets.map((wallet) => connectorDector(wallet));
227
- }
228
- const walletListResponse = await fetch((baseUrl || "https://web3-assets.tomo.inc") + "/api/wallets");
229
- const walletList = await walletListResponse.json();
230
- defaultWallets = walletList == null ? void 0 : walletList.data.map((wallet) => __spreadProps(__spreadValues({}, wallet), {
231
- icon: (baseUrl || "https://web3-assets.tomo.inc") + wallet.icon
334
+ const walletList = await getAllWallets(baseUrl);
335
+ const defaultWallets = walletList == null ? void 0 : walletList.map((wallet) => __spreadProps(__spreadValues({}, wallet), {
336
+ icon: (baseUrl || walletBaseUrl) + wallet.icon
232
337
  }));
233
- return defaultWallets == null ? void 0 : defaultWallets.map((wallet) => connectorDector(wallet));
338
+ return defaultWallets == null ? void 0 : defaultWallets.map((wallet) => connectorDector(wallet, "tomo"));
234
339
  };
235
-
236
- // src/wallets/wallet-eip6963.ts
237
340
  var announceEvent = "eip6963:announceProvider";
238
341
  var requestEvent = "eip6963:requestProvider";
239
342
  var walletType = "evm";
@@ -264,17 +367,18 @@ async function eip6963Wallets() {
264
367
  }
265
368
  },
266
369
  isInstalled: true,
267
- providers: {
370
+ connectors: {
268
371
  [walletType]: {
269
372
  provider,
270
- protocol: "eip6963" /* EIP6963 */
373
+ standard: ProviderStandard.EIP1193,
374
+ protocol: ProviderProtocol.INJECT
271
375
  }
272
376
  }
273
377
  });
274
378
  }
275
379
  }
276
380
  } catch (error) {
277
- console.warn(`${"eip6963" /* EIP6963 */} detect error:`, error);
381
+ console.warn(`${ProviderProtocol.INJECT} detect error:`, error);
278
382
  }
279
383
  }
280
384
  window.addEventListener(announceEvent, handleProviderAnnouncement);
@@ -333,10 +437,11 @@ async function walletStandardWallets() {
333
437
  }
334
438
  },
335
439
  isInstalled: true,
336
- providers: {
440
+ connectors: {
337
441
  solana: {
338
442
  provider: wallet.features,
339
- protocol: "wallet-standard" /* WALLET_STANDARD */
443
+ standard: ProviderStandard.WALLET_STANDARD,
444
+ protocol: ProviderProtocol.INJECT
340
445
  }
341
446
  }
342
447
  });
@@ -359,10 +464,11 @@ async function walletStandardWallets() {
359
464
  }
360
465
  },
361
466
  isInstalled: true,
362
- providers: {
467
+ connectors: {
363
468
  aptos: {
364
469
  provider: wallet.features,
365
- protocol: "wallet-standard" /* WALLET_STANDARD */
470
+ standard: ProviderStandard.WALLET_STANDARD,
471
+ protocol: ProviderProtocol.INJECT
366
472
  }
367
473
  }
368
474
  });
@@ -385,10 +491,11 @@ async function walletStandardWallets() {
385
491
  }
386
492
  },
387
493
  isInstalled: true,
388
- providers: {
494
+ connectors: {
389
495
  sui: {
390
496
  provider: wallet.features,
391
- protocol: "wallet-standard" /* WALLET_STANDARD */
497
+ standard: ProviderStandard.WALLET_STANDARD,
498
+ protocol: ProviderProtocol.INJECT
392
499
  }
393
500
  }
394
501
  });
@@ -1004,9 +1111,7 @@ async function walletConnectWallets(baseUrl) {
1004
1111
  if (wcWallets.length > 0) {
1005
1112
  return wcWallets.map((wallet) => convertWalletToConnector(wallet));
1006
1113
  }
1007
- const walletListResponse = await fetch(
1008
- (baseUrl || "https://web3-assets.tomo.inc") + "/api/wallets?walletId=walletConnect"
1009
- );
1114
+ const walletListResponse = await fetch((baseUrl || walletBaseUrl) + "/api/wallets?walletId=walletConnect");
1010
1115
  const walletList = await walletListResponse.json();
1011
1116
  if (((_a = walletList == null ? void 0 : walletList.data) == null ? void 0 : _a.length) === 0) {
1012
1117
  return [];
@@ -1030,15 +1135,24 @@ function convertWalletToConnector(wallet) {
1030
1135
  },
1031
1136
  isInstalled: true,
1032
1137
  // Always available
1033
- providers: {
1034
- evm: {
1035
- provider: createWalletConnectEVMProvider(walletConnectConfig == null ? void 0 : walletConnectConfig.evmNamespaces),
1036
- protocol: "wallet-connect" /* WALLET_CONNECT */
1138
+ connectors: {
1139
+ [ChainTypeEnum.EVM]: {
1140
+ standard: ProviderStandard.NORMAL,
1141
+ protocol: ProviderProtocol.WALLET_CONNECT,
1142
+ provider: createWalletConnectEVMProvider(walletConnectConfig == null ? void 0 : walletConnectConfig.evmNamespaces)
1037
1143
  },
1038
- solana: {
1039
- provider: createWalletConnectSolanaProvider(walletConnectConfig == null ? void 0 : walletConnectConfig.solanaNamespaces),
1040
- protocol: "wallet-connect" /* WALLET_CONNECT */
1144
+ [ChainTypeEnum.SOLANA]: {
1145
+ standard: ProviderStandard.NORMAL,
1146
+ protocol: ProviderProtocol.WALLET_CONNECT,
1147
+ provider: createWalletConnectSolanaProvider(walletConnectConfig == null ? void 0 : walletConnectConfig.solanaNamespaces)
1041
1148
  }
1149
+ // [ChainTypeEnum.BITCOIN]: null,
1150
+ // [ChainTypeEnum.DOGECOIN]: null,
1151
+ // [ChainTypeEnum.APTOS]: null,
1152
+ // [ChainTypeEnum.COSMOS]: null,
1153
+ // [ChainTypeEnum.TON]: null,
1154
+ // [ChainTypeEnum.TRON]: null,
1155
+ // [ChainTypeEnum.SUI]: null,
1042
1156
  }
1043
1157
  };
1044
1158
  }
@@ -1180,9 +1294,16 @@ var connect = async (connectParams, walletOptions) => {
1180
1294
  let address = "";
1181
1295
  let network = "";
1182
1296
  if (chainType === "evm") {
1183
- const res = await provider.request({ method: "eth_requestAccounts" });
1184
- address = (res == null ? void 0 : res[0]) || "";
1185
- chainId = await provider.request({ method: "eth_chainId" });
1297
+ if (provider == null ? void 0 : provider.request) {
1298
+ const res = await provider.request({ method: "eth_requestAccounts" });
1299
+ address = (res == null ? void 0 : res[0]) || "";
1300
+ chainId = await provider.request({ method: "eth_chainId" });
1301
+ } else {
1302
+ const wagmiProvider = await provider.getProvider();
1303
+ const res = await wagmiProvider.request({ method: "eth_requestAccounts" });
1304
+ address = (res == null ? void 0 : res[0]) || "";
1305
+ chainId = await provider.request({ method: "eth_chainId" });
1306
+ }
1186
1307
  }
1187
1308
  if (chainType === "solana") {
1188
1309
  let res = null;
@@ -1692,53 +1813,51 @@ var getBalance = async (token, walletOptions) => {
1692
1813
  throw new Error(`getBalance not supported in ${chainType}`);
1693
1814
  };
1694
1815
 
1695
- // src/wallets/index.ts
1696
- var getAllWallets = async (baseUrl) => {
1697
- const walletListResponse = await fetch((baseUrl || "https://web3-assets.tomo.inc") + "/api/wallets");
1698
- const walletList = await walletListResponse.json();
1699
- return walletList;
1700
- };
1701
-
1702
1816
  // src/index.ts
1703
- var walletBaseUrl = "https://embedded-wallet.tomo.inc";
1704
1817
  async function loadConnectors({
1705
1818
  chainType = "all",
1706
- recommonedConnectors,
1819
+ recommendedConnectors,
1707
1820
  connectorTypes = [],
1708
1821
  options = {
1709
1822
  baseUrl: walletBaseUrl
1710
1823
  }
1711
1824
  }) {
1712
- const recommonedWalletConfigs = recommonedConnectors || [];
1825
+ const recommonedWalletConfigs = recommendedConnectors || [];
1713
1826
  const evmWallets = await eip6963Wallets();
1714
1827
  const { solanaWallets, aptosWallets } = await walletStandardWallets();
1715
1828
  const wcWallets2 = await walletConnectWallets(options.baseUrl);
1716
- let recommonedConnectorsDetected = [];
1829
+ const allWallets2 = await getAllWallets(options.baseUrl);
1830
+ let recommendedConnectorsDetected = [];
1717
1831
  if (recommonedWalletConfigs.length > 0) {
1718
- recommonedConnectorsDetected = recommonedWalletConfigs.map((walletConfig, index) => {
1832
+ recommendedConnectorsDetected = recommonedWalletConfigs.map((walletConfig, index) => {
1719
1833
  const connectorType = connectorTypes[index];
1720
1834
  if (!supportedWalletConfigTypes[connectorType]) {
1721
1835
  throw new Error(`Unsupported wallet config type: ${connectorType}`);
1722
1836
  }
1723
- const _walletConfig = walletConfigAdapter(walletConfig, connectorType);
1724
- return __spreadProps(__spreadValues({}, connectorDector(_walletConfig)), {
1837
+ const _walletConfig = walletConfigAdapter(
1838
+ walletConfig,
1839
+ connectorType,
1840
+ allWallets2,
1841
+ options.baseUrl || walletBaseUrl
1842
+ );
1843
+ return __spreadProps(__spreadValues({}, connectorDector(_walletConfig, connectorType)), {
1725
1844
  recommoned: true
1726
1845
  });
1727
1846
  });
1728
1847
  }
1729
1848
  const defaultConnectors = await getDefaultConnectors(options.baseUrl);
1730
- const connectorsFromConfig = uniqueConnectors([...recommonedConnectorsDetected, ...defaultConnectors]);
1849
+ const connectorsFromConfig = uniqueConnectors([...recommendedConnectorsDetected, ...defaultConnectors]);
1731
1850
  const connectorsDetecteds = [...evmWallets, ...solanaWallets, ...aptosWallets, ...wcWallets2];
1732
1851
  for (const connector of connectorsDetecteds) {
1733
1852
  const _connector = connectorsFromConfig.find((c) => c.info.name === connector.info.name);
1734
1853
  if (_connector) {
1735
1854
  _connector.isInstalled = true;
1736
- _connector.providers = __spreadValues(__spreadValues({}, _connector.providers), connector.providers);
1855
+ _connector.connectors = __spreadValues(__spreadValues({}, _connector.connectors), connector.connectors);
1737
1856
  } else {
1738
1857
  connectorsFromConfig.push(connector);
1739
1858
  }
1740
1859
  }
1741
- const allConnectors = connectorsFromConfig.filter((wallet) => chainType !== "all" ? !!wallet.providers[chainType] : true).sort((a, b) => {
1860
+ const allConnectors = connectorsFromConfig.filter((wallet) => chainType !== "all" ? !!wallet.connectors[chainType] : true).sort((a, b) => {
1742
1861
  if (a.recommoned && !b.recommoned) return -1;
1743
1862
  if (!a.recommoned && b.recommoned) return 1;
1744
1863
  if (a.isInstalled && !b.isInstalled) return -1;
@@ -1747,8 +1866,8 @@ async function loadConnectors({
1747
1866
  });
1748
1867
  return {
1749
1868
  all: allConnectors,
1750
- recommoned: recommonedConnectorsDetected
1869
+ recommoned: recommendedConnectorsDetected
1751
1870
  };
1752
1871
  }
1753
1872
 
1754
- export { ProviderChainType, ProviderProtocol, SupportedChainTypes, addChain, connect, connectMobile, disconnect, getAllWallets, getBalance, isMobile, loadConnectors, setWalletConnectConfig, signInWithWallet, signMessage, switchChain };
1873
+ export { addChain, connect, connectMobile, disconnect, getAllWallets, getBalance, isMobile, isWagmiConnector, loadConnectors, setWalletConnectConfig, signInWithWallet, signMessage, switchChain, walletBaseUrl };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tomo-inc/wallet-adaptor-base",
3
- "version": "0.0.17",
3
+ "version": "0.0.19",
4
4
  "author": "tomo.inc",
5
5
  "license": "MIT",
6
6
  "private": false,
@@ -22,7 +22,8 @@
22
22
  "@solana/web3.js": "^1.98.0",
23
23
  "@wallet-standard/core": "^1.1.0",
24
24
  "viem": "2.21.54",
25
- "@tomo-inc/wallet-connect-protocol": "0.0.16"
25
+ "@tomo-inc/wallet-utils": "0.0.17",
26
+ "@tomo-inc/wallet-connect-protocol": "0.0.17"
26
27
  },
27
28
  "devDependencies": {
28
29
  "@types/node": "^20.0.0",
@@ -0,0 +1,110 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
2
+
3
+ describe("defaultConnectors", () => {
4
+ let originalFetch: typeof fetch;
5
+ let mockConnectorDector: ReturnType<typeof vi.fn>;
6
+
7
+ beforeEach(async () => {
8
+ originalFetch = global.fetch;
9
+ // Reset module cache before each test
10
+ vi.resetModules();
11
+
12
+ // Re-import after reset
13
+ const detectorModule = await import("../wallets/detector");
14
+ mockConnectorDector = vi.fn((wallet: any) => ({
15
+ info: { name: wallet.name, uuid: wallet.id },
16
+ providers: {},
17
+ }));
18
+ vi.spyOn(detectorModule, "connectorDector").mockImplementation(mockConnectorDector);
19
+ });
20
+
21
+ afterEach(() => {
22
+ global.fetch = originalFetch;
23
+ vi.restoreAllMocks();
24
+ });
25
+
26
+ describe("getDefaultConnectors", () => {
27
+ it("should fetch and return default connectors", async () => {
28
+ const { getDefaultConnectors } = await import("../wallets/defaultConnectors");
29
+ const mockWallets = [
30
+ { id: "wallet1", name: "Wallet 1", icon: "/icon1.png" },
31
+ { id: "wallet2", name: "Wallet 2", icon: "/icon2.png" },
32
+ ];
33
+
34
+ global.fetch = vi.fn().mockResolvedValue({
35
+ json: vi.fn().mockResolvedValue({ data: mockWallets }),
36
+ });
37
+
38
+ const result = await getDefaultConnectors();
39
+
40
+ expect(global.fetch).toHaveBeenCalledWith("https://web3-assets.tomo.inc/api/wallets");
41
+ expect(result).toHaveLength(2);
42
+ expect(mockConnectorDector).toHaveBeenCalledTimes(2);
43
+ });
44
+
45
+ it("should use custom baseUrl", async () => {
46
+ const { getDefaultConnectors } = await import("../wallets/defaultConnectors");
47
+ const mockWallets = [{ id: "wallet1", name: "Wallet 1", icon: "/icon1.png" }];
48
+
49
+ global.fetch = vi.fn().mockResolvedValue({
50
+ json: vi.fn().mockResolvedValue({ data: mockWallets }),
51
+ });
52
+
53
+ const customBaseUrl = "https://custom-api.example.com";
54
+ await getDefaultConnectors(customBaseUrl);
55
+
56
+ expect(global.fetch).toHaveBeenCalledWith(`${customBaseUrl}/api/wallets`);
57
+ });
58
+
59
+ it("should prepend baseUrl to icon paths", async () => {
60
+ const { getDefaultConnectors } = await import("../wallets/defaultConnectors");
61
+ const mockWallets = [{ id: "wallet1", name: "Wallet 1", icon: "/icon1.png" }];
62
+
63
+ global.fetch = vi.fn().mockResolvedValue({
64
+ json: vi.fn().mockResolvedValue({ data: mockWallets }),
65
+ });
66
+
67
+ const customBaseUrl = "https://custom-api.example.com";
68
+ await getDefaultConnectors(customBaseUrl);
69
+
70
+ expect(mockConnectorDector).toHaveBeenCalledWith({
71
+ id: "wallet1",
72
+ name: "Wallet 1",
73
+ icon: "https://custom-api.example.com/icon1.png",
74
+ });
75
+ });
76
+
77
+ it("should return cached connectors on second call", async () => {
78
+ const { getDefaultConnectors } = await import("../wallets/defaultConnectors");
79
+ const mockWallets = [{ id: "wallet1", name: "Wallet 1", icon: "/icon1.png" }];
80
+
81
+ global.fetch = vi.fn().mockResolvedValue({
82
+ json: vi.fn().mockResolvedValue({ data: mockWallets }),
83
+ });
84
+
85
+ const result1 = await getDefaultConnectors();
86
+ const result2 = await getDefaultConnectors();
87
+
88
+ expect(global.fetch).toHaveBeenCalledTimes(1);
89
+ expect(result1).toEqual(result2);
90
+ });
91
+
92
+ it("should handle empty wallet list", async () => {
93
+ const { getDefaultConnectors } = await import("../wallets/defaultConnectors");
94
+ global.fetch = vi.fn().mockResolvedValue({
95
+ json: vi.fn().mockResolvedValue({ data: [] }),
96
+ });
97
+
98
+ const result = await getDefaultConnectors();
99
+
100
+ expect(result).toHaveLength(0);
101
+ });
102
+
103
+ it("should handle fetch errors gracefully", async () => {
104
+ const { getDefaultConnectors } = await import("../wallets/defaultConnectors");
105
+ global.fetch = vi.fn().mockRejectedValue(new Error("Network error"));
106
+
107
+ await expect(getDefaultConnectors()).rejects.toThrow("Network error");
108
+ });
109
+ });
110
+ });