@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.cjs +212 -94
- package/dist/index.d.cts +30 -23
- package/dist/index.d.ts +30 -23
- package/dist/index.js +211 -92
- package/package.json +3 -2
- package/src/__tests__/defaultConnectors.test.ts +110 -0
- package/src/__tests__/wallet-standard.test.ts +302 -0
- package/src/index.ts +24 -15
- package/src/type.ts +6 -25
- package/src/utils/wallet-config.ts +85 -0
- package/src/wallet-api/connect.ts +10 -3
- package/src/wallets/defaultConnectors.ts +5 -10
- package/src/wallets/detector.ts +159 -24
- package/src/wallets/index.ts +11 -2
- package/src/wallets/wallet-eip6963.ts +6 -4
- package/src/wallets/wallet-standard.ts +12 -8
- package/src/wallets/wallet-walletconnect.ts +18 -9
- /package/src/wallets/{Wallet.ts → types.ts} +0 -0
package/src/wallets/detector.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ProviderProtocol, ProviderStandard } from "@tomo-inc/wallet-utils";
|
|
2
|
+
import { mainnet } from "viem/chains";
|
|
3
|
+
import { Connector, WalletConnectorType } from "../type";
|
|
4
|
+
import { WagmiWalletConfig, WalletConfig } from "./types";
|
|
3
5
|
|
|
4
6
|
type WalletProviderFlags = string;
|
|
5
7
|
type WindowProvider = any;
|
|
@@ -66,21 +68,113 @@ export const supportedWalletConfigTypes = {
|
|
|
66
68
|
wagmi: true,
|
|
67
69
|
};
|
|
68
70
|
|
|
71
|
+
/**
|
|
72
|
+
* from TomoDefaultConnectors find wallet config
|
|
73
|
+
* @param id - wallet id
|
|
74
|
+
* @param name - wallet name
|
|
75
|
+
* @param defaultWallets - default wallets list
|
|
76
|
+
* @returns found wallet config or null
|
|
77
|
+
*/
|
|
78
|
+
function findWalletInDefaultConnectors(id: string, name: string, defaultWallets: WalletConfig[]): WalletConfig | null {
|
|
79
|
+
// first find by id/uuid (case insensitive)
|
|
80
|
+
let found = defaultWallets.find(
|
|
81
|
+
(wallet) =>
|
|
82
|
+
wallet.id?.toLowerCase() === id.toLowerCase() ||
|
|
83
|
+
wallet.id?.toLowerCase() === id.toLowerCase().replace(/\s+/g, "-"),
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
// if not found, find by name (case insensitive, support partial matching)
|
|
87
|
+
if (!found) {
|
|
88
|
+
const normalizedName = name.toLowerCase();
|
|
89
|
+
found = defaultWallets.find((wallet) => {
|
|
90
|
+
const walletName = wallet.name.toLowerCase();
|
|
91
|
+
return (
|
|
92
|
+
walletName === normalizedName ||
|
|
93
|
+
walletName.includes(normalizedName) ||
|
|
94
|
+
normalizedName.includes(walletName) ||
|
|
95
|
+
// support common wallet name variations matching
|
|
96
|
+
(normalizedName.includes("metamask") && walletName.includes("metamask")) ||
|
|
97
|
+
(normalizedName.includes("coinbase") && walletName.includes("coinbase")) ||
|
|
98
|
+
(normalizedName.includes("safe") && walletName.includes("safe")) ||
|
|
99
|
+
(normalizedName.includes("walletconnect") && walletName.includes("walletconnect")) ||
|
|
100
|
+
(normalizedName.includes("injected") && walletName.includes("injected"))
|
|
101
|
+
);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return found || null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* adapt wagmi CreateConnectorFn to WalletConfig
|
|
110
|
+
* @param wagmiConnector - wagmi connector create function
|
|
111
|
+
* @param defaultConnectors - default connectors list, for finding missing properties
|
|
112
|
+
* @returns WalletConfig
|
|
113
|
+
*/
|
|
114
|
+
function adaptWagmiConnectorToWalletConfig(
|
|
115
|
+
wagmiConnector: WagmiWalletConfig,
|
|
116
|
+
allWallets: WalletConfig[],
|
|
117
|
+
baseUrl: string,
|
|
118
|
+
): WalletConfig {
|
|
119
|
+
// create a temporary configuration to get connector instance information
|
|
120
|
+
// note: here we use a mock configuration, because the actual configuration needs a complete wagmi config
|
|
121
|
+
const tempConfig = { chains: [mainnet] } as Parameters<typeof wagmiConnector>[0] | Record<string, never>;
|
|
122
|
+
const connector = wagmiConnector(tempConfig);
|
|
123
|
+
|
|
124
|
+
// extract information from connector instance
|
|
125
|
+
const id = connector.id || "unknown";
|
|
126
|
+
const name = connector.name || "Unknown Wallet";
|
|
127
|
+
|
|
128
|
+
// find corresponding wallet from defaultConnectors
|
|
129
|
+
const defaultWallet = findWalletInDefaultConnectors(id, name, allWallets);
|
|
130
|
+
|
|
131
|
+
// build WalletConfig
|
|
132
|
+
const walletConfig: WalletConfig & { createConnector: WagmiWalletConfig } = {
|
|
133
|
+
id,
|
|
134
|
+
name,
|
|
135
|
+
namespace: defaultWallet?.namespace || "eip155", // default EVM namespace
|
|
136
|
+
icon: baseUrl + defaultWallet?.icon || "",
|
|
137
|
+
iconBackground: defaultWallet?.iconBackground || "#666666",
|
|
138
|
+
rdns: defaultWallet?.rdns,
|
|
139
|
+
downloadUrls: defaultWallet?.downloadUrls || {},
|
|
140
|
+
installed: defaultWallet?.installed || false,
|
|
141
|
+
flag: defaultWallet?.flag || "",
|
|
142
|
+
solana: {
|
|
143
|
+
namespace: defaultWallet?.solana?.namespace || "",
|
|
144
|
+
flag: defaultWallet?.solana?.flag || "",
|
|
145
|
+
},
|
|
146
|
+
aptos: {
|
|
147
|
+
namespace: defaultWallet?.aptos?.namespace || "",
|
|
148
|
+
flag: defaultWallet?.aptos?.flag || "",
|
|
149
|
+
},
|
|
150
|
+
createConnector: wagmiConnector,
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
return walletConfig;
|
|
154
|
+
}
|
|
155
|
+
|
|
69
156
|
export function walletConfigAdapter(
|
|
70
157
|
walletConfig: WalletConfig | WagmiWalletConfig,
|
|
71
158
|
connectorType: WalletConnectorType,
|
|
159
|
+
allWallets: WalletConfig[],
|
|
160
|
+
baseUrl: string,
|
|
72
161
|
): WalletConfig {
|
|
73
162
|
switch (connectorType) {
|
|
74
163
|
case "tomo":
|
|
75
|
-
return walletConfig;
|
|
164
|
+
return walletConfig as WalletConfig;
|
|
76
165
|
case "wagmi":
|
|
77
|
-
|
|
166
|
+
// check if it is a function (CreateConnectorFn)
|
|
167
|
+
if (typeof walletConfig === "function") {
|
|
168
|
+
return adaptWagmiConnectorToWalletConfig(walletConfig as WagmiWalletConfig, allWallets, baseUrl);
|
|
169
|
+
}
|
|
170
|
+
// if not a function, it may be other format wagmi config, return directly
|
|
171
|
+
return walletConfig as WalletConfig;
|
|
78
172
|
default:
|
|
79
|
-
return walletConfig;
|
|
173
|
+
return walletConfig as WalletConfig;
|
|
80
174
|
}
|
|
81
175
|
}
|
|
82
176
|
|
|
83
|
-
|
|
177
|
+
function tomoConnectorDector(wallet: WalletConfig): Connector {
|
|
84
178
|
const evmNS = {
|
|
85
179
|
namespace: wallet?.namespace || "",
|
|
86
180
|
flag: wallet?.flag || "",
|
|
@@ -101,50 +195,48 @@ export function connectorDector(wallet: WalletConfig): Connector {
|
|
|
101
195
|
const isDogecoinExists = hasInjectedProvider(dogecoinNS);
|
|
102
196
|
const isInstalled = isEvmExists || isSolanaExists || isAptosExists || isDogecoinExists;
|
|
103
197
|
|
|
198
|
+
const DEFAULT_PROVIDER = {
|
|
199
|
+
provider: undefined,
|
|
200
|
+
protocol: undefined,
|
|
201
|
+
standard: ProviderStandard.NORMAL,
|
|
202
|
+
};
|
|
203
|
+
|
|
104
204
|
const providers: any = {};
|
|
105
205
|
if (evmNS.namespace || evmNS.flag) {
|
|
106
206
|
providers.evm = isEvmExists
|
|
107
207
|
? {
|
|
108
208
|
provider: getInjectedProvider(evmNS),
|
|
109
|
-
protocol: ProviderProtocol.
|
|
209
|
+
protocol: ProviderProtocol.INJECT,
|
|
210
|
+
standard: ProviderStandard.EIP1193,
|
|
110
211
|
}
|
|
111
|
-
:
|
|
112
|
-
provider: undefined,
|
|
113
|
-
protocol: undefined,
|
|
114
|
-
};
|
|
212
|
+
: DEFAULT_PROVIDER;
|
|
115
213
|
}
|
|
116
214
|
if (solanaNS.namespace) {
|
|
117
215
|
providers.solana = isSolanaExists
|
|
118
216
|
? {
|
|
119
217
|
provider: getInjectedProvider(solanaNS),
|
|
120
218
|
protocol: ProviderProtocol.INJECT,
|
|
219
|
+
standard: ProviderStandard.NORMAL,
|
|
121
220
|
}
|
|
122
|
-
:
|
|
123
|
-
provider: undefined,
|
|
124
|
-
protocol: undefined,
|
|
125
|
-
};
|
|
221
|
+
: DEFAULT_PROVIDER;
|
|
126
222
|
}
|
|
127
223
|
if (aptosNS.namespace) {
|
|
128
224
|
providers.aptos = isAptosExists
|
|
129
225
|
? {
|
|
130
226
|
provider: getInjectedProvider(aptosNS),
|
|
131
227
|
protocol: ProviderProtocol.INJECT,
|
|
228
|
+
standard: ProviderStandard.NORMAL,
|
|
132
229
|
}
|
|
133
|
-
:
|
|
134
|
-
provider: undefined,
|
|
135
|
-
protocol: undefined,
|
|
136
|
-
};
|
|
230
|
+
: DEFAULT_PROVIDER;
|
|
137
231
|
}
|
|
138
232
|
if (dogecoinNS.namespace) {
|
|
139
233
|
providers.dogecoin = isDogecoinExists
|
|
140
234
|
? {
|
|
141
235
|
provider: getInjectedProvider(dogecoinNS),
|
|
142
236
|
protocol: ProviderProtocol.INJECT,
|
|
237
|
+
standard: ProviderStandard.NORMAL,
|
|
143
238
|
}
|
|
144
|
-
:
|
|
145
|
-
provider: undefined,
|
|
146
|
-
protocol: undefined,
|
|
147
|
-
};
|
|
239
|
+
: DEFAULT_PROVIDER;
|
|
148
240
|
}
|
|
149
241
|
return {
|
|
150
242
|
info: {
|
|
@@ -163,6 +255,49 @@ export function connectorDector(wallet: WalletConfig): Connector {
|
|
|
163
255
|
},
|
|
164
256
|
},
|
|
165
257
|
isInstalled,
|
|
166
|
-
providers,
|
|
258
|
+
connectors: providers,
|
|
167
259
|
};
|
|
168
260
|
}
|
|
261
|
+
|
|
262
|
+
function wagmiConnectorDector(wallet: WagmiWalletConfig): Connector {
|
|
263
|
+
const tempConfig = {} as Record<string, any>;
|
|
264
|
+
const connector = wallet.createConnector(tempConfig);
|
|
265
|
+
// const evmNS = {
|
|
266
|
+
// namespace: wallet?.namespace || "",
|
|
267
|
+
// flag: wallet?.flag || "",
|
|
268
|
+
// };
|
|
269
|
+
|
|
270
|
+
// const isEvm = hasInjectedProvider(evmNS);
|
|
271
|
+
// const isInstalled = isEvm || wallet.installed || false;
|
|
272
|
+
const providers = {
|
|
273
|
+
evm: {
|
|
274
|
+
provider: connector,
|
|
275
|
+
protocol: ProviderProtocol.INJECT,
|
|
276
|
+
standard: ProviderStandard.EIP1193,
|
|
277
|
+
},
|
|
278
|
+
};
|
|
279
|
+
return {
|
|
280
|
+
info: {
|
|
281
|
+
uuid: wallet?.id,
|
|
282
|
+
name: wallet?.name,
|
|
283
|
+
icon: wallet?.icon,
|
|
284
|
+
iconBackground: wallet?.iconBackground,
|
|
285
|
+
rdns: wallet?.rdns,
|
|
286
|
+
links: {
|
|
287
|
+
homepage: wallet?.downloadUrls?.qrCode || "",
|
|
288
|
+
ios_install: wallet?.downloadUrls?.ios || "",
|
|
289
|
+
android_install: wallet?.downloadUrls?.android || "",
|
|
290
|
+
chrome_install: wallet?.downloadUrls?.chrome || "",
|
|
291
|
+
},
|
|
292
|
+
},
|
|
293
|
+
isInstalled: true,
|
|
294
|
+
connectors: providers,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export function connectorDector(wallet: WalletConfig, connectorType: WalletConnectorType): Connector {
|
|
299
|
+
if (connectorType === "wagmi") {
|
|
300
|
+
return wagmiConnectorDector(wallet as WagmiWalletConfig);
|
|
301
|
+
}
|
|
302
|
+
return tomoConnectorDector(wallet);
|
|
303
|
+
}
|
package/src/wallets/index.ts
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
|
+
import { WalletConfig } from "./types";
|
|
2
|
+
|
|
3
|
+
export const walletBaseUrl = "https://web3-assets.tomo.inc";
|
|
4
|
+
|
|
5
|
+
let allWallets: WalletConfig[] = [];
|
|
1
6
|
export const getAllWallets = async (baseUrl?: string) => {
|
|
2
|
-
|
|
3
|
-
|
|
7
|
+
if (allWallets.length > 0) {
|
|
8
|
+
return allWallets;
|
|
9
|
+
}
|
|
10
|
+
const walletListResponse = await fetch((baseUrl || walletBaseUrl) + "/api/wallets");
|
|
11
|
+
const walletListData = await walletListResponse.json();
|
|
12
|
+
const walletList = (allWallets = walletListData?.data);
|
|
4
13
|
return walletList;
|
|
5
14
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ProviderProtocol, ProviderStandard } from "@tomo-inc/wallet-utils";
|
|
2
|
+
import { Connector, WalletInfo } from "../type";
|
|
2
3
|
import { uniqueConnectors } from "../utils/utils";
|
|
3
4
|
|
|
4
5
|
const announceEvent = "eip6963:announceProvider";
|
|
@@ -41,17 +42,18 @@ export async function eip6963Wallets(): Promise<Connector[]> {
|
|
|
41
42
|
},
|
|
42
43
|
},
|
|
43
44
|
isInstalled: true,
|
|
44
|
-
|
|
45
|
+
connectors: {
|
|
45
46
|
[walletType]: {
|
|
46
47
|
provider,
|
|
47
|
-
|
|
48
|
+
standard: ProviderStandard.EIP1193,
|
|
49
|
+
protocol: ProviderProtocol.INJECT,
|
|
48
50
|
},
|
|
49
51
|
},
|
|
50
52
|
});
|
|
51
53
|
}
|
|
52
54
|
}
|
|
53
55
|
} catch (error) {
|
|
54
|
-
console.warn(`${ProviderProtocol.
|
|
56
|
+
console.warn(`${ProviderProtocol.INJECT} detect error:`, error);
|
|
55
57
|
}
|
|
56
58
|
}
|
|
57
59
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Connector, ProviderProtocol } from "../type";
|
|
1
|
+
import { ProviderProtocol, ProviderStandard } from "@tomo-inc/wallet-utils";
|
|
3
2
|
import { getWallets } from "@wallet-standard/core";
|
|
3
|
+
import { Connector } from "../type";
|
|
4
|
+
import { uniqueConnectors } from "../utils/utils";
|
|
4
5
|
|
|
5
6
|
const disabledWallets = {
|
|
6
7
|
"Bitget Wallet": true,
|
|
@@ -71,10 +72,11 @@ export async function walletStandardWallets(): Promise<{
|
|
|
71
72
|
},
|
|
72
73
|
},
|
|
73
74
|
isInstalled: true,
|
|
74
|
-
|
|
75
|
+
connectors: {
|
|
75
76
|
solana: {
|
|
76
77
|
provider: wallet.features,
|
|
77
|
-
|
|
78
|
+
standard: ProviderStandard.WALLET_STANDARD,
|
|
79
|
+
protocol: ProviderProtocol.INJECT,
|
|
78
80
|
},
|
|
79
81
|
},
|
|
80
82
|
});
|
|
@@ -101,10 +103,11 @@ export async function walletStandardWallets(): Promise<{
|
|
|
101
103
|
},
|
|
102
104
|
},
|
|
103
105
|
isInstalled: true,
|
|
104
|
-
|
|
106
|
+
connectors: {
|
|
105
107
|
aptos: {
|
|
106
108
|
provider: wallet.features,
|
|
107
|
-
|
|
109
|
+
standard: ProviderStandard.WALLET_STANDARD,
|
|
110
|
+
protocol: ProviderProtocol.INJECT,
|
|
108
111
|
},
|
|
109
112
|
},
|
|
110
113
|
});
|
|
@@ -131,10 +134,11 @@ export async function walletStandardWallets(): Promise<{
|
|
|
131
134
|
},
|
|
132
135
|
},
|
|
133
136
|
isInstalled: true,
|
|
134
|
-
|
|
137
|
+
connectors: {
|
|
135
138
|
sui: {
|
|
136
139
|
provider: wallet.features,
|
|
137
|
-
|
|
140
|
+
standard: ProviderStandard.WALLET_STANDARD,
|
|
141
|
+
protocol: ProviderProtocol.INJECT,
|
|
138
142
|
},
|
|
139
143
|
} as any,
|
|
140
144
|
});
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { NamespaceConfig } from "@tomo-inc/wallet-connect-protocol";
|
|
2
|
-
import {
|
|
2
|
+
import { ChainTypeEnum, ProviderProtocol, ProviderStandard } from "@tomo-inc/wallet-utils";
|
|
3
|
+
import { Connector } from "../type";
|
|
4
|
+
import { walletBaseUrl } from "./index";
|
|
3
5
|
import { WalletConnectProvider } from "./providers/WalletConnectProvider";
|
|
4
6
|
import { WalletConnectSolanaProvider } from "./providers/WalletConnectSolanaProvider";
|
|
5
7
|
|
|
@@ -48,9 +50,7 @@ export async function walletConnectWallets(baseUrl?: string): Promise<Connector[
|
|
|
48
50
|
if (wcWallets.length > 0) {
|
|
49
51
|
return wcWallets.map((wallet: any) => convertWalletToConnector(wallet));
|
|
50
52
|
}
|
|
51
|
-
const walletListResponse = await fetch(
|
|
52
|
-
(baseUrl || "https://web3-assets.tomo.inc") + "/api/wallets?walletId=walletConnect",
|
|
53
|
-
);
|
|
53
|
+
const walletListResponse = await fetch((baseUrl || walletBaseUrl) + "/api/wallets?walletId=walletConnect");
|
|
54
54
|
const walletList = await walletListResponse.json();
|
|
55
55
|
if (walletList?.data?.length === 0) {
|
|
56
56
|
return [];
|
|
@@ -77,15 +77,24 @@ function convertWalletToConnector(wallet: Record<string, any>): Connector {
|
|
|
77
77
|
},
|
|
78
78
|
},
|
|
79
79
|
isInstalled: true, // Always available
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
connectors: {
|
|
81
|
+
[ChainTypeEnum.EVM]: {
|
|
82
|
+
standard: ProviderStandard.NORMAL,
|
|
83
83
|
protocol: ProviderProtocol.WALLET_CONNECT,
|
|
84
|
+
provider: createWalletConnectEVMProvider(walletConnectConfig?.evmNamespaces),
|
|
84
85
|
},
|
|
85
|
-
|
|
86
|
-
|
|
86
|
+
[ChainTypeEnum.SOLANA]: {
|
|
87
|
+
standard: ProviderStandard.NORMAL,
|
|
87
88
|
protocol: ProviderProtocol.WALLET_CONNECT,
|
|
89
|
+
provider: createWalletConnectSolanaProvider(walletConnectConfig?.solanaNamespaces),
|
|
88
90
|
},
|
|
91
|
+
// [ChainTypeEnum.BITCOIN]: null,
|
|
92
|
+
// [ChainTypeEnum.DOGECOIN]: null,
|
|
93
|
+
// [ChainTypeEnum.APTOS]: null,
|
|
94
|
+
// [ChainTypeEnum.COSMOS]: null,
|
|
95
|
+
// [ChainTypeEnum.TON]: null,
|
|
96
|
+
// [ChainTypeEnum.TRON]: null,
|
|
97
|
+
// [ChainTypeEnum.SUI]: null,
|
|
89
98
|
},
|
|
90
99
|
};
|
|
91
100
|
}
|
|
File without changes
|