@ab-org/sdk-core 0.1.0 → 0.1.2-beta.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.
- package/README.md +29 -0
- package/dist/chunk-5HURLKIK.js +1005 -0
- package/dist/chunk-MVZIAM4H.js +363 -0
- package/dist/cubeSignerAuth-DrPc9FeB.d.ts +426 -0
- package/dist/index.d.ts +237 -16
- package/dist/index.js +1119 -16
- package/dist/social-auth.d.ts +11 -0
- package/dist/social-auth.js +1 -0
- package/dist/social-provider-Bq58TBRt.d.ts +88 -0
- package/dist/social-provider.d.ts +2 -0
- package/dist/social-provider.js +2 -0
- package/dist/social.d.ts +3 -0
- package/dist/social.js +2 -0
- package/package.json +24 -6
- package/dist/core/capabilities.d.ts +0 -32
- package/dist/core/capabilities.js +0 -88
- package/dist/core/chains.d.ts +0 -23
- package/dist/core/chains.js +0 -83
- package/dist/core/errors.d.ts +0 -9
- package/dist/core/errors.js +0 -51
- package/dist/core/sessionStore.d.ts +0 -26
- package/dist/core/sessionStore.js +0 -129
- package/dist/core/types.d.ts +0 -75
- package/dist/core/types.js +0 -1
- package/dist/core/walletConnector.d.ts +0 -29
- package/dist/core/walletConnector.js +0 -153
- package/dist/core/walletExecution.d.ts +0 -22
- package/dist/core/walletExecution.js +0 -89
- package/dist/hooks/useAccount.d.ts +0 -11
- package/dist/hooks/useAccount.js +0 -32
- package/dist/hooks/useWalletConnect.d.ts +0 -16
- package/dist/hooks/useWalletConnect.js +0 -24
- package/dist/providers/base.d.ts +0 -15
- package/dist/providers/base.js +0 -30
- package/dist/providers/plugin/injectedEvmProvider.d.ts +0 -112
- package/dist/providers/plugin/injectedEvmProvider.js +0 -352
- package/dist/providers/plugin/injectedWalletRegistry.d.ts +0 -9
- package/dist/providers/plugin/injectedWalletRegistry.js +0 -34
- package/dist/providers/plugin/metamaskProvider.d.ts +0 -1
- package/dist/providers/plugin/metamaskProvider.js +0 -1
- package/dist/providers/social/baseSocialProvider.d.ts +0 -21
- package/dist/providers/social/baseSocialProvider.js +0 -11
- package/dist/providers/social/cubeSignerAuth.d.ts +0 -89
- package/dist/providers/social/cubeSignerAuth.js +0 -128
- package/dist/providers/social/cubistEvmWalletProvider.d.ts +0 -9
- package/dist/providers/social/cubistEvmWalletProvider.js +0 -175
- package/dist/providers/social/cubistProvider.d.ts +0 -52
- package/dist/providers/social/cubistProvider.js +0 -160
package/dist/index.js
CHANGED
|
@@ -1,16 +1,1119 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
import { AbstractProvider, getSupportedChainFromEvmChainId, createChainContext, assertSessionCapability } from './chunk-MVZIAM4H.js';
|
|
2
|
+
export { AbstractProvider, AbstractSocialProvider, CubistSocialProvider, SessionCapabilityError, assertSessionCapability, createChainContext, createSessionCapabilityPolicy, describeSessionCapabilityPolicy, getAllChainDescriptors, getChainDescriptor, getSupportedChainFromEvmChainId, isCapabilityPolicyExpired, isSessionExpired, sessionSupportsCapability } from './chunk-MVZIAM4H.js';
|
|
3
|
+
export { CubeSignerAuth } from './chunk-5HURLKIK.js';
|
|
4
|
+
import EventEmitter from 'eventemitter3';
|
|
5
|
+
|
|
6
|
+
// src/core/errors.ts
|
|
7
|
+
var EVM_TRANSACTION_SIGNING_UNSUPPORTED = "EVM_TRANSACTION_SIGNING_UNSUPPORTED";
|
|
8
|
+
var EvmTransactionSigningUnsupportedError = class extends Error {
|
|
9
|
+
constructor(providerName, cause) {
|
|
10
|
+
super(`${providerName} does not support EVM transaction signing`);
|
|
11
|
+
this.providerName = providerName;
|
|
12
|
+
this.cause = cause;
|
|
13
|
+
this.code = EVM_TRANSACTION_SIGNING_UNSUPPORTED;
|
|
14
|
+
this.name = "EvmTransactionSigningUnsupportedError";
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var getErrorCode = (error) => {
|
|
18
|
+
if (typeof error !== "object" || error === null || !("code" in error)) return void 0;
|
|
19
|
+
const { code } = error;
|
|
20
|
+
return typeof code === "number" || typeof code === "string" ? code : void 0;
|
|
21
|
+
};
|
|
22
|
+
var getErrorMessage = (error) => {
|
|
23
|
+
if (error instanceof Error) return error.message;
|
|
24
|
+
if (typeof error === "object" && error !== null && "message" in error) {
|
|
25
|
+
const { message } = error;
|
|
26
|
+
if (typeof message === "string") return message;
|
|
27
|
+
}
|
|
28
|
+
return String(error);
|
|
29
|
+
};
|
|
30
|
+
var isUnsupportedEvmTransactionSigningError = (error) => {
|
|
31
|
+
if (error instanceof EvmTransactionSigningUnsupportedError) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
const code = getErrorCode(error);
|
|
35
|
+
if (code === 4200 || code === -32601) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
const message = getErrorMessage(error).toLowerCase();
|
|
39
|
+
return message.includes("eth_signtransaction") && (message.includes("unsupported") || message.includes("not support") || message.includes("not found") || message.includes("does not exist") || message.includes("unrecognized"));
|
|
40
|
+
};
|
|
41
|
+
var normalizeEvmTransactionSigningError = (providerName, error) => {
|
|
42
|
+
if (error instanceof Error && !isUnsupportedEvmTransactionSigningError(error)) {
|
|
43
|
+
return error;
|
|
44
|
+
}
|
|
45
|
+
if (isUnsupportedEvmTransactionSigningError(error)) {
|
|
46
|
+
return new EvmTransactionSigningUnsupportedError(providerName, error);
|
|
47
|
+
}
|
|
48
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
49
|
+
};
|
|
50
|
+
var STORAGE_KEY = "ab:wallet:session";
|
|
51
|
+
function getStorage() {
|
|
52
|
+
try {
|
|
53
|
+
return typeof localStorage !== "undefined" ? localStorage : null;
|
|
54
|
+
} catch {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function persistSession(session) {
|
|
59
|
+
const storage = getStorage();
|
|
60
|
+
if (!storage) return;
|
|
61
|
+
if (!session) {
|
|
62
|
+
storage.removeItem(STORAGE_KEY);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const serializable = {
|
|
66
|
+
address: session.address,
|
|
67
|
+
chain: session.chain,
|
|
68
|
+
walletType: session.walletType,
|
|
69
|
+
authSource: session.authSource,
|
|
70
|
+
sessionId: session.sessionId,
|
|
71
|
+
expiresAt: session.expiresAt
|
|
72
|
+
};
|
|
73
|
+
if (session.capabilities?.length) {
|
|
74
|
+
serializable.capabilities = session.capabilities;
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
storage.setItem(STORAGE_KEY, JSON.stringify(serializable));
|
|
78
|
+
} catch {
|
|
79
|
+
storage.removeItem(STORAGE_KEY);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function readPersistedSession() {
|
|
83
|
+
const storage = getStorage();
|
|
84
|
+
if (!storage) return null;
|
|
85
|
+
const raw = storage.getItem(STORAGE_KEY);
|
|
86
|
+
if (!raw) return null;
|
|
87
|
+
try {
|
|
88
|
+
return JSON.parse(raw);
|
|
89
|
+
} catch {
|
|
90
|
+
storage.removeItem(STORAGE_KEY);
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
var staleProvider = {
|
|
95
|
+
request() {
|
|
96
|
+
return Promise.reject(
|
|
97
|
+
new Error("Session restored from cache. Reconnect your wallet for transactions.")
|
|
98
|
+
);
|
|
99
|
+
},
|
|
100
|
+
disconnect() {
|
|
101
|
+
return Promise.resolve();
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
var SessionStore = class {
|
|
105
|
+
constructor() {
|
|
106
|
+
this.state = {
|
|
107
|
+
session: null,
|
|
108
|
+
balance: null,
|
|
109
|
+
isConnecting: false
|
|
110
|
+
};
|
|
111
|
+
this.emitter = new EventEmitter();
|
|
112
|
+
this.tryRehydrate();
|
|
113
|
+
}
|
|
114
|
+
getState() {
|
|
115
|
+
return { ...this.state };
|
|
116
|
+
}
|
|
117
|
+
setConnecting(flag) {
|
|
118
|
+
this.state.isConnecting = flag;
|
|
119
|
+
this.emitter.emit("connecting:changed", flag);
|
|
120
|
+
}
|
|
121
|
+
setSession(session) {
|
|
122
|
+
this.state.session = session;
|
|
123
|
+
persistSession(session);
|
|
124
|
+
this.emitter.emit("session:changed", session);
|
|
125
|
+
}
|
|
126
|
+
rehydrateSession(session) {
|
|
127
|
+
this.state.session = session;
|
|
128
|
+
persistSession(session);
|
|
129
|
+
this.emitter.emit("session:changed", session);
|
|
130
|
+
this.emitter.emit("session:rehydrated", session);
|
|
131
|
+
}
|
|
132
|
+
clearSession() {
|
|
133
|
+
this.state.session = null;
|
|
134
|
+
this.state.balance = null;
|
|
135
|
+
persistSession(null);
|
|
136
|
+
this.emitter.emit("session:changed", null);
|
|
137
|
+
this.emitter.emit("balance:changed", null);
|
|
138
|
+
}
|
|
139
|
+
expireSession() {
|
|
140
|
+
const currentSession = this.state.session;
|
|
141
|
+
if (!currentSession) return;
|
|
142
|
+
this.clearSession();
|
|
143
|
+
this.emitter.emit("session:expired", currentSession);
|
|
144
|
+
}
|
|
145
|
+
revokeSession() {
|
|
146
|
+
const currentSession = this.state.session;
|
|
147
|
+
if (!currentSession) return;
|
|
148
|
+
this.clearSession();
|
|
149
|
+
this.emitter.emit("session:revoked", currentSession);
|
|
150
|
+
}
|
|
151
|
+
setBalance(balance) {
|
|
152
|
+
this.state.balance = balance;
|
|
153
|
+
this.emitter.emit("balance:changed", balance);
|
|
154
|
+
}
|
|
155
|
+
on(event, listener) {
|
|
156
|
+
this.emitter.on(event, listener);
|
|
157
|
+
return () => this.emitter.off(event, listener);
|
|
158
|
+
}
|
|
159
|
+
tryRehydrate() {
|
|
160
|
+
const persisted = readPersistedSession();
|
|
161
|
+
if (!persisted) return;
|
|
162
|
+
if (persisted.expiresAt && persisted.expiresAt <= Date.now()) {
|
|
163
|
+
persistSession(null);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
this.state.session = { ...persisted, provider: staleProvider };
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
var sessionStore = new SessionStore();
|
|
170
|
+
|
|
171
|
+
// src/core/walletExecution.ts
|
|
172
|
+
var requireSession = (session) => {
|
|
173
|
+
const resolvedSession = session ?? sessionStore.getState().session;
|
|
174
|
+
if (!resolvedSession) {
|
|
175
|
+
throw new Error("Wallet session missing");
|
|
176
|
+
}
|
|
177
|
+
return resolvedSession;
|
|
178
|
+
};
|
|
179
|
+
var performCapabilityCheck = (session, capability, check) => {
|
|
180
|
+
assertSessionCapability(session, {
|
|
181
|
+
capability,
|
|
182
|
+
chain: check?.chain ?? session.chainContext?.walletChain ?? session.chain,
|
|
183
|
+
token: check?.token,
|
|
184
|
+
amount: check?.amount,
|
|
185
|
+
appId: check?.appId,
|
|
186
|
+
origin: check?.origin,
|
|
187
|
+
now: check?.now
|
|
188
|
+
});
|
|
189
|
+
};
|
|
190
|
+
var getWalletExecutionCapabilities = (session) => requireSession(session).capabilities ?? [];
|
|
191
|
+
var createWalletExecutionClient = (session) => {
|
|
192
|
+
const resolvedSession = requireSession(session);
|
|
193
|
+
return {
|
|
194
|
+
session: resolvedSession,
|
|
195
|
+
capabilities: resolvedSession.capabilities ?? [],
|
|
196
|
+
supports(capability) {
|
|
197
|
+
return this.capabilities.length === 0 || this.capabilities.includes(capability);
|
|
198
|
+
},
|
|
199
|
+
async sendTransaction(transaction, check) {
|
|
200
|
+
performCapabilityCheck(resolvedSession, "eth_sendTransaction", {
|
|
201
|
+
...check,
|
|
202
|
+
amount: check?.amount ?? transaction.value
|
|
203
|
+
});
|
|
204
|
+
return resolvedSession.provider.request({
|
|
205
|
+
method: "eth_sendTransaction",
|
|
206
|
+
params: [{ ...transaction, from: transaction.from ?? resolvedSession.address }]
|
|
207
|
+
});
|
|
208
|
+
},
|
|
209
|
+
async signTransaction(transaction, check) {
|
|
210
|
+
performCapabilityCheck(resolvedSession, "eth_signTransaction", {
|
|
211
|
+
...check,
|
|
212
|
+
amount: check?.amount ?? transaction.value
|
|
213
|
+
});
|
|
214
|
+
return resolvedSession.provider.request({
|
|
215
|
+
method: "eth_signTransaction",
|
|
216
|
+
params: [{ ...transaction, from: transaction.from ?? resolvedSession.address }]
|
|
217
|
+
});
|
|
218
|
+
},
|
|
219
|
+
async signMessage(message, check) {
|
|
220
|
+
performCapabilityCheck(resolvedSession, "personal_sign", check);
|
|
221
|
+
return resolvedSession.provider.request({
|
|
222
|
+
method: "personal_sign",
|
|
223
|
+
params: [message, resolvedSession.address]
|
|
224
|
+
});
|
|
225
|
+
},
|
|
226
|
+
async signTypedData(typedData, check) {
|
|
227
|
+
performCapabilityCheck(resolvedSession, "eth_signTypedData_v4", check);
|
|
228
|
+
return resolvedSession.provider.request({
|
|
229
|
+
method: "eth_signTypedData_v4",
|
|
230
|
+
params: [resolvedSession.address, JSON.stringify(typedData)]
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
};
|
|
235
|
+
var createWalletExecutionController = () => ({
|
|
236
|
+
get session() {
|
|
237
|
+
return sessionStore.getState().session;
|
|
238
|
+
},
|
|
239
|
+
get capabilities() {
|
|
240
|
+
return sessionStore.getState().session?.capabilities ?? [];
|
|
241
|
+
},
|
|
242
|
+
supports(capability) {
|
|
243
|
+
const session = requireSession();
|
|
244
|
+
return createWalletExecutionClient(session).supports(capability);
|
|
245
|
+
},
|
|
246
|
+
sendTransaction(transaction, check) {
|
|
247
|
+
return createWalletExecutionClient().sendTransaction(transaction, check);
|
|
248
|
+
},
|
|
249
|
+
signTransaction(transaction, check) {
|
|
250
|
+
return createWalletExecutionClient().signTransaction(transaction, check);
|
|
251
|
+
},
|
|
252
|
+
signMessage(message, check) {
|
|
253
|
+
return createWalletExecutionClient().signMessage(message, check);
|
|
254
|
+
},
|
|
255
|
+
signTypedData(typedData, check) {
|
|
256
|
+
return createWalletExecutionClient().signTypedData(typedData, check);
|
|
257
|
+
}
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
// src/core/walletConnector.ts
|
|
261
|
+
var ADAPTER_STORAGE_KEY = "ab:wallet:adapterId";
|
|
262
|
+
function getStorage2() {
|
|
263
|
+
try {
|
|
264
|
+
return typeof localStorage !== "undefined" ? localStorage : null;
|
|
265
|
+
} catch {
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
function persistAdapterId(adapterId) {
|
|
270
|
+
const storage = getStorage2();
|
|
271
|
+
if (!storage) return;
|
|
272
|
+
if (!adapterId) {
|
|
273
|
+
storage.removeItem(ADAPTER_STORAGE_KEY);
|
|
274
|
+
} else {
|
|
275
|
+
storage.setItem(ADAPTER_STORAGE_KEY, adapterId);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
function readPersistedAdapterId() {
|
|
279
|
+
const storage = getStorage2();
|
|
280
|
+
if (!storage) return null;
|
|
281
|
+
return storage.getItem(ADAPTER_STORAGE_KEY);
|
|
282
|
+
}
|
|
283
|
+
var WalletConnector = class {
|
|
284
|
+
constructor(adapters) {
|
|
285
|
+
this.adapters = adapters;
|
|
286
|
+
this.currentAdapterId = null;
|
|
287
|
+
this.currentAdapterId = readPersistedAdapterId();
|
|
288
|
+
}
|
|
289
|
+
getAdapterById(adapterId) {
|
|
290
|
+
const adapter = this.adapters.find((item) => item.id === adapterId);
|
|
291
|
+
if (!adapter) throw new Error(`Wallet adapter ${adapterId} not registered`);
|
|
292
|
+
return adapter;
|
|
293
|
+
}
|
|
294
|
+
getCurrentAdapter(session = sessionStore.getState().session) {
|
|
295
|
+
return (this.currentAdapterId ? this.adapters.find((item) => item.id === this.currentAdapterId) : void 0) ?? this.adapters.find((item) => item.getProvider() === session?.provider);
|
|
296
|
+
}
|
|
297
|
+
clearActiveSession() {
|
|
298
|
+
this.currentAdapterId = null;
|
|
299
|
+
persistAdapterId(null);
|
|
300
|
+
sessionStore.clearSession();
|
|
301
|
+
}
|
|
302
|
+
requireActiveSession() {
|
|
303
|
+
const session = sessionStore.getState().session;
|
|
304
|
+
if (!session) {
|
|
305
|
+
throw new Error("Wallet session missing");
|
|
306
|
+
}
|
|
307
|
+
return session;
|
|
308
|
+
}
|
|
309
|
+
async connect(adapterId, args) {
|
|
310
|
+
const adapter = this.getAdapterById(adapterId);
|
|
311
|
+
const currentAdapter = this.getCurrentAdapter();
|
|
312
|
+
if (currentAdapter && currentAdapter.id !== adapter.id) {
|
|
313
|
+
await currentAdapter.disconnect();
|
|
314
|
+
this.clearActiveSession();
|
|
315
|
+
}
|
|
316
|
+
sessionStore.setConnecting(true);
|
|
317
|
+
try {
|
|
318
|
+
const session = await adapter.connect(args);
|
|
319
|
+
this.currentAdapterId = adapter.id;
|
|
320
|
+
persistAdapterId(adapter.id);
|
|
321
|
+
sessionStore.setSession(session);
|
|
322
|
+
sessionStore.setBalance(null);
|
|
323
|
+
return session;
|
|
324
|
+
} finally {
|
|
325
|
+
sessionStore.setConnecting(false);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
async disconnect() {
|
|
329
|
+
const adapter = this.getCurrentAdapter();
|
|
330
|
+
if (!adapter) return;
|
|
331
|
+
await adapter?.disconnect();
|
|
332
|
+
this.clearActiveSession();
|
|
333
|
+
}
|
|
334
|
+
rehydrateSession(adapterId, session) {
|
|
335
|
+
this.currentAdapterId = adapterId;
|
|
336
|
+
persistAdapterId(adapterId);
|
|
337
|
+
sessionStore.rehydrateSession(session);
|
|
338
|
+
sessionStore.setBalance(null);
|
|
339
|
+
return session;
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Attempt to silently reconnect a wallet whose session was rehydrated
|
|
343
|
+
* from localStorage. Uses the adapter's `reconnect()` if available,
|
|
344
|
+
* falling back to `connect()` for backward compatibility.
|
|
345
|
+
*
|
|
346
|
+
* Returns the restored session on success, or `null` if reconnection
|
|
347
|
+
* is not possible (e.g. wallet extension removed, social session expired).
|
|
348
|
+
*/
|
|
349
|
+
async tryAutoReconnect() {
|
|
350
|
+
const state = sessionStore.getState();
|
|
351
|
+
if (!state.session) return null;
|
|
352
|
+
const adapterId = this.currentAdapterId ?? readPersistedAdapterId();
|
|
353
|
+
if (!adapterId) return null;
|
|
354
|
+
const adapter = this.adapters.find((a) => a.id === adapterId);
|
|
355
|
+
if (!adapter) return null;
|
|
356
|
+
sessionStore.setConnecting(true);
|
|
357
|
+
try {
|
|
358
|
+
const session = adapter.reconnect ? await adapter.reconnect(state.session) : null;
|
|
359
|
+
if (!session) return null;
|
|
360
|
+
this.currentAdapterId = adapterId;
|
|
361
|
+
persistAdapterId(adapterId);
|
|
362
|
+
sessionStore.rehydrateSession(session);
|
|
363
|
+
return session;
|
|
364
|
+
} catch {
|
|
365
|
+
return null;
|
|
366
|
+
} finally {
|
|
367
|
+
sessionStore.setConnecting(false);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
supportsCapability(capability) {
|
|
371
|
+
const session = this.requireActiveSession();
|
|
372
|
+
return createWalletExecutionClient(session).supports(capability);
|
|
373
|
+
}
|
|
374
|
+
async requestCurrentProvider(payload) {
|
|
375
|
+
return this.requireActiveSession().provider.request(payload);
|
|
376
|
+
}
|
|
377
|
+
async sendEvmTransaction(transaction) {
|
|
378
|
+
const session = this.requireActiveSession();
|
|
379
|
+
return createWalletExecutionClient(session).sendTransaction(transaction);
|
|
380
|
+
}
|
|
381
|
+
async signEvmTransaction(transaction) {
|
|
382
|
+
const session = this.requireActiveSession();
|
|
383
|
+
return createWalletExecutionClient(session).signTransaction(transaction);
|
|
384
|
+
}
|
|
385
|
+
async signMessage(message) {
|
|
386
|
+
const session = this.requireActiveSession();
|
|
387
|
+
return createWalletExecutionClient(session).signMessage(message);
|
|
388
|
+
}
|
|
389
|
+
async signTypedData(typedData) {
|
|
390
|
+
const session = this.requireActiveSession();
|
|
391
|
+
return createWalletExecutionClient(session).signTypedData(typedData);
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
// src/hooks/useWalletConnect.ts
|
|
396
|
+
var createWalletConnectController = ({
|
|
397
|
+
connector,
|
|
398
|
+
defaultAdapterId
|
|
399
|
+
}) => {
|
|
400
|
+
return {
|
|
401
|
+
openModal: async (adapterId = defaultAdapterId, args) => {
|
|
402
|
+
if (!adapterId) throw new Error("No adapter id provided");
|
|
403
|
+
await connector.connect(adapterId, args);
|
|
404
|
+
},
|
|
405
|
+
disconnect: () => connector.disconnect(),
|
|
406
|
+
rehydrate: (adapterId, session) => connector.rehydrateSession(adapterId, session),
|
|
407
|
+
tryAutoReconnect: () => connector.tryAutoReconnect(),
|
|
408
|
+
supports(capability) {
|
|
409
|
+
if (!sessionStore.getState().session) return false;
|
|
410
|
+
return connector.supportsCapability(capability);
|
|
411
|
+
},
|
|
412
|
+
get isConnected() {
|
|
413
|
+
return Boolean(sessionStore.getState().session);
|
|
414
|
+
},
|
|
415
|
+
get isConnecting() {
|
|
416
|
+
return sessionStore.getState().isConnecting;
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
};
|
|
420
|
+
|
|
421
|
+
// src/hooks/useAccount.ts
|
|
422
|
+
var createAccountController = () => {
|
|
423
|
+
return {
|
|
424
|
+
get session() {
|
|
425
|
+
return sessionStore.getState().session;
|
|
426
|
+
},
|
|
427
|
+
get address() {
|
|
428
|
+
return sessionStore.getState().session?.address ?? null;
|
|
429
|
+
},
|
|
430
|
+
get chain() {
|
|
431
|
+
return sessionStore.getState().session?.chain ?? null;
|
|
432
|
+
},
|
|
433
|
+
get balance() {
|
|
434
|
+
return sessionStore.getState().balance;
|
|
435
|
+
},
|
|
436
|
+
get isConnected() {
|
|
437
|
+
return Boolean(sessionStore.getState().session);
|
|
438
|
+
},
|
|
439
|
+
get currentProvider() {
|
|
440
|
+
return sessionStore.getState().session?.provider ?? null;
|
|
441
|
+
},
|
|
442
|
+
get chainContext() {
|
|
443
|
+
return sessionStore.getState().session?.chainContext ?? null;
|
|
444
|
+
},
|
|
445
|
+
get capabilities() {
|
|
446
|
+
return sessionStore.getState().session?.capabilities ?? [];
|
|
447
|
+
},
|
|
448
|
+
get capabilityPolicy() {
|
|
449
|
+
return sessionStore.getState().session?.capabilityPolicy ?? null;
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
// ../wallet-utils/dist/index.js
|
|
455
|
+
var __defProp = Object.defineProperty;
|
|
456
|
+
var __export = (target, all) => {
|
|
457
|
+
for (var name in all)
|
|
458
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
459
|
+
};
|
|
460
|
+
var cache_exports = {};
|
|
461
|
+
__export(cache_exports, {
|
|
462
|
+
clear: () => clear,
|
|
463
|
+
get: () => get,
|
|
464
|
+
remove: () => remove,
|
|
465
|
+
set: () => set,
|
|
466
|
+
setKeyNS: () => setKeyNS
|
|
467
|
+
});
|
|
468
|
+
var keyNS = "tomo-";
|
|
469
|
+
function get(key) {
|
|
470
|
+
if (typeof window === "undefined") {
|
|
471
|
+
return null;
|
|
472
|
+
}
|
|
473
|
+
const tempKey = keyNS + key;
|
|
474
|
+
if (!isKeyExist(tempKey)) {
|
|
475
|
+
return null;
|
|
476
|
+
}
|
|
477
|
+
let val = null;
|
|
478
|
+
try {
|
|
479
|
+
const data = window.localStorage.getItem(tempKey) || window.sessionStorage.getItem(tempKey);
|
|
480
|
+
val = JSON.parse(data);
|
|
481
|
+
} catch (err) {
|
|
482
|
+
console.error(err);
|
|
483
|
+
}
|
|
484
|
+
if (val !== null && Object.prototype.hasOwnProperty.call(val, "type") && Object.prototype.hasOwnProperty.call(val, "data")) {
|
|
485
|
+
return val["data"];
|
|
486
|
+
}
|
|
487
|
+
return null;
|
|
488
|
+
}
|
|
489
|
+
function set(key, val, isTemp) {
|
|
490
|
+
if (typeof window === "undefined") {
|
|
491
|
+
return false;
|
|
492
|
+
}
|
|
493
|
+
let store;
|
|
494
|
+
if (isTemp) {
|
|
495
|
+
store = window.sessionStorage;
|
|
496
|
+
} else {
|
|
497
|
+
store = window.localStorage;
|
|
498
|
+
}
|
|
499
|
+
const data = JSON.stringify({
|
|
500
|
+
data: val,
|
|
501
|
+
time: (/* @__PURE__ */ new Date()).getTime(),
|
|
502
|
+
//for manage by time limit
|
|
503
|
+
type: typeof val
|
|
504
|
+
});
|
|
505
|
+
try {
|
|
506
|
+
store.setItem(keyNS + key, data);
|
|
507
|
+
return true;
|
|
508
|
+
} catch (err) {
|
|
509
|
+
if (err?.name?.toUpperCase().indexOf("QUOTA") >= 0) {
|
|
510
|
+
window.localStorage.clear();
|
|
511
|
+
store.setItem(keyNS + key, data);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
return false;
|
|
515
|
+
}
|
|
516
|
+
function remove(key) {
|
|
517
|
+
if (typeof window === "undefined") {
|
|
518
|
+
return;
|
|
519
|
+
}
|
|
520
|
+
const tempKey = keyNS + key;
|
|
521
|
+
window.localStorage.removeItem(tempKey);
|
|
522
|
+
window.sessionStorage.removeItem(tempKey);
|
|
523
|
+
}
|
|
524
|
+
function clear() {
|
|
525
|
+
if (typeof window === "undefined") {
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
window?.localStorage?.clear();
|
|
529
|
+
window?.sessionStorage?.clear();
|
|
530
|
+
}
|
|
531
|
+
function isKeyExist(key) {
|
|
532
|
+
if (typeof window === "undefined") {
|
|
533
|
+
return false;
|
|
534
|
+
}
|
|
535
|
+
return Object.prototype.hasOwnProperty.call(window.localStorage, key) || Object.prototype.hasOwnProperty.call(window.sessionStorage, key);
|
|
536
|
+
}
|
|
537
|
+
function setKeyNS(NS) {
|
|
538
|
+
const isString = typeof NS === "string";
|
|
539
|
+
if (isString && NS !== "") {
|
|
540
|
+
keyNS = NS;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
function pick(...candidates) {
|
|
544
|
+
for (const c of candidates) {
|
|
545
|
+
if (c != null && c !== "") return c;
|
|
546
|
+
}
|
|
547
|
+
return void 0;
|
|
548
|
+
}
|
|
549
|
+
function nextPublicProcessEnvKey(logicalKey) {
|
|
550
|
+
return `NEXT_PUBLIC_${logicalKey}`;
|
|
551
|
+
}
|
|
552
|
+
function readProcessEnvStatic(key) {
|
|
553
|
+
if (typeof process === "undefined" || !process.env) return void 0;
|
|
554
|
+
switch (key) {
|
|
555
|
+
case "STAGE":
|
|
556
|
+
return pick(
|
|
557
|
+
process.env.NEXT_PUBLIC_STAGE,
|
|
558
|
+
process.env.STAGE
|
|
559
|
+
);
|
|
560
|
+
case "FUNDING_CHAIN_ID":
|
|
561
|
+
return pick(
|
|
562
|
+
process.env.NEXT_PUBLIC_FUNDING_CHAIN_ID,
|
|
563
|
+
process.env.FUNDING_CHAIN_ID
|
|
564
|
+
);
|
|
565
|
+
/** Next.js 仅内联「静态」`process.env.NEXT_PUBLIC_*`;动态键名在客户端会为 undefined,必须逐键写出。 */
|
|
566
|
+
case "GOOGLE_CLIENT_ID":
|
|
567
|
+
return pick(
|
|
568
|
+
process.env.NEXT_PUBLIC_GOOGLE_CLIENT_ID,
|
|
569
|
+
process.env.GOOGLE_CLIENT_ID
|
|
570
|
+
);
|
|
571
|
+
case "X_CLIENT_ID":
|
|
572
|
+
return pick(
|
|
573
|
+
process.env.NEXT_PUBLIC_X_CLIENT_ID,
|
|
574
|
+
process.env.X_CLIENT_ID
|
|
575
|
+
);
|
|
576
|
+
case "MERCHANT_BASE_URL":
|
|
577
|
+
return pick(
|
|
578
|
+
process.env.NEXT_PUBLIC_MERCHANT_BASE_URL,
|
|
579
|
+
process.env.MERCHANT_BASE_URL
|
|
580
|
+
);
|
|
581
|
+
case "RELAY_ORIGIN":
|
|
582
|
+
return pick(
|
|
583
|
+
process.env.NEXT_PUBLIC_RELAY_ORIGIN,
|
|
584
|
+
process.env.RELAY_ORIGIN
|
|
585
|
+
);
|
|
586
|
+
case "CUBE_SIGNER_ENV":
|
|
587
|
+
return pick(
|
|
588
|
+
process.env.NEXT_PUBLIC_CUBE_SIGNER_ENV,
|
|
589
|
+
process.env.CUBE_SIGNER_ENV
|
|
590
|
+
);
|
|
591
|
+
case "CUBE_SIGNER_ORG_ID":
|
|
592
|
+
return pick(
|
|
593
|
+
process.env.NEXT_PUBLIC_CUBE_SIGNER_ORG_ID,
|
|
594
|
+
process.env.CUBE_SIGNER_ORG_ID
|
|
595
|
+
);
|
|
596
|
+
case "CUBE_REG":
|
|
597
|
+
return pick(
|
|
598
|
+
process.env.NEXT_PUBLIC_CUBE_REG,
|
|
599
|
+
process.env.CUBE_REG
|
|
600
|
+
);
|
|
601
|
+
case "FUNDING_TOKEN_SYMBOL":
|
|
602
|
+
return pick(
|
|
603
|
+
process.env.NEXT_PUBLIC_FUNDING_TOKEN_SYMBOL,
|
|
604
|
+
process.env.FUNDING_TOKEN_SYMBOL
|
|
605
|
+
);
|
|
606
|
+
case "FUNDING_TOKEN_ADDRESS":
|
|
607
|
+
return pick(
|
|
608
|
+
process.env.NEXT_PUBLIC_FUNDING_TOKEN_ADDRESS,
|
|
609
|
+
process.env.FUNDING_TOKEN_ADDRESS
|
|
610
|
+
);
|
|
611
|
+
default:
|
|
612
|
+
return pick(
|
|
613
|
+
process.env[`NEXT_PUBLIC_${key}`],
|
|
614
|
+
process.env[key]
|
|
615
|
+
);
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
function getEnv(key) {
|
|
619
|
+
const staticVal = readProcessEnvStatic(key);
|
|
620
|
+
if (staticVal != null && staticVal !== "") return staticVal;
|
|
621
|
+
const g = globalThis;
|
|
622
|
+
const env = g.process?.env;
|
|
623
|
+
if (env) {
|
|
624
|
+
const pub = nextPublicProcessEnvKey(key);
|
|
625
|
+
const v = env[key] ?? env[pub];
|
|
626
|
+
if (v != null && v !== "") return v;
|
|
627
|
+
}
|
|
628
|
+
try {
|
|
629
|
+
const m = import.meta.env;
|
|
630
|
+
if (m) {
|
|
631
|
+
const pub = nextPublicProcessEnvKey(key);
|
|
632
|
+
const v = m[key] ?? m[pub];
|
|
633
|
+
if (v != null && v !== "") return v;
|
|
634
|
+
}
|
|
635
|
+
} catch {
|
|
636
|
+
}
|
|
637
|
+
return "";
|
|
638
|
+
}
|
|
639
|
+
var TENDERLY_BSC_3131 = {
|
|
640
|
+
chainId: "3131",
|
|
641
|
+
name: "BSC_TENDERLY",
|
|
642
|
+
chainName: "BSC_TENDERLY",
|
|
643
|
+
nativeCurrencyName: "BSC",
|
|
644
|
+
nativeCurrencySymbol: "BSC",
|
|
645
|
+
nativeCurrencyDecimals: 18,
|
|
646
|
+
rpcUrls: [
|
|
647
|
+
"https://virtual.binance.eu.rpc.tenderly.co/e643ea28-32eb-4fb9-8116-90be24f7defa"
|
|
648
|
+
],
|
|
649
|
+
blockExplorerUrl: "https://dashboard.tenderly.co/explorer/vnet/6593bc72-f548-497d-bff9-5be061436a48",
|
|
650
|
+
platformType: "EVM",
|
|
651
|
+
icon: "https://static.tomo.inc/token/bsc_new.svg",
|
|
652
|
+
// Tenderly 默认资金侧合约地址(测试环境镜像):
|
|
653
|
+
defaultFundingTokenAddress: "0x55d398326f99059fF775485246999027B3197955"
|
|
654
|
+
};
|
|
655
|
+
var BSC_MAINNET_56 = {
|
|
656
|
+
chainId: "56",
|
|
657
|
+
name: "BSC",
|
|
658
|
+
chainName: "BNB Smart Chain",
|
|
659
|
+
nativeCurrencyName: "BNB",
|
|
660
|
+
nativeCurrencySymbol: "BNB",
|
|
661
|
+
nativeCurrencyDecimals: 18,
|
|
662
|
+
rpcUrls: ["https://bsc-dataseed.binance.org", "https://bsc-dataseed1.defibit.io"],
|
|
663
|
+
blockExplorerUrl: "https://bscscan.com",
|
|
664
|
+
platformType: "EVM",
|
|
665
|
+
icon: "https://static.tomo.inc/token/bsc_new.svg",
|
|
666
|
+
// 主网默认资金侧合约地址(USDT);如需覆盖请使用 FUNDING_TOKEN_ADDRESS 环境变量
|
|
667
|
+
defaultFundingTokenAddress: "0x55d398326f99059fF775485246999027B3197955"
|
|
668
|
+
};
|
|
669
|
+
var CHAIN_REGISTRY = {
|
|
670
|
+
[TENDERLY_BSC_3131.chainId]: TENDERLY_BSC_3131,
|
|
671
|
+
[BSC_MAINNET_56.chainId]: BSC_MAINNET_56
|
|
672
|
+
};
|
|
673
|
+
var DEFAULT_FUNDING_CHAIN_ID = getEnv("FUNDING_CHAIN_ID") || "3131";
|
|
674
|
+
function normalizeFundingChainId(chainId) {
|
|
675
|
+
if (chainId === void 0 || chainId === null) return DEFAULT_FUNDING_CHAIN_ID;
|
|
676
|
+
const s = String(chainId).trim();
|
|
677
|
+
return s === "" ? DEFAULT_FUNDING_CHAIN_ID : s;
|
|
678
|
+
}
|
|
679
|
+
function getChainInfo(chainId) {
|
|
680
|
+
const id = normalizeFundingChainId(chainId);
|
|
681
|
+
const info = CHAIN_REGISTRY[id];
|
|
682
|
+
if (!info) {
|
|
683
|
+
throw new Error(
|
|
684
|
+
`Unsupported funding chainId "${id}". Supported: ${Object.keys(CHAIN_REGISTRY).sort().join(", ")}`
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
return info;
|
|
688
|
+
}
|
|
689
|
+
function pickEnvFundingTokenAddress() {
|
|
690
|
+
const tryPairs = [
|
|
691
|
+
["NEXT_PUBLIC_FUNDING_TOKEN_ADDRESS", "FUNDING_TOKEN_ADDRESS"]
|
|
692
|
+
];
|
|
693
|
+
if (typeof process !== "undefined" && process.env) {
|
|
694
|
+
for (const [pub, priv] of tryPairs) {
|
|
695
|
+
const a = process.env[pub];
|
|
696
|
+
const b = process.env[priv];
|
|
697
|
+
if (a != null && a !== "") return a;
|
|
698
|
+
if (b != null && b !== "") return b;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
try {
|
|
702
|
+
const m = import.meta.env;
|
|
703
|
+
if (m) {
|
|
704
|
+
for (const [pub, priv] of tryPairs) {
|
|
705
|
+
const a = m[pub];
|
|
706
|
+
const b = m[priv];
|
|
707
|
+
if (a != null && a !== "") return a;
|
|
708
|
+
if (b != null && b !== "") return b;
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
} catch {
|
|
712
|
+
}
|
|
713
|
+
return void 0;
|
|
714
|
+
}
|
|
715
|
+
function getFundingTokenAddress(chainId) {
|
|
716
|
+
const v = pickEnvFundingTokenAddress();
|
|
717
|
+
if (v && /^0x[0-9a-fA-F]{40}$/.test(v)) return v;
|
|
718
|
+
return getChainInfo(chainId).defaultFundingTokenAddress;
|
|
719
|
+
}
|
|
720
|
+
getFundingTokenAddress(DEFAULT_FUNDING_CHAIN_ID);
|
|
721
|
+
({
|
|
722
|
+
google: getEnv("GOOGLE_CLIENT_ID"),
|
|
723
|
+
x: getEnv("X_CLIENT_ID")
|
|
724
|
+
});
|
|
725
|
+
new TextEncoder();
|
|
726
|
+
|
|
727
|
+
// src/providers/plugin/injectedEvmProvider.ts
|
|
728
|
+
var injectedWalletCapabilities = [
|
|
729
|
+
"eth_accounts",
|
|
730
|
+
"eth_requestAccounts",
|
|
731
|
+
"eth_chainId",
|
|
732
|
+
"eth_signTransaction",
|
|
733
|
+
"eth_sendTransaction",
|
|
734
|
+
"personal_sign",
|
|
735
|
+
"eth_signTypedData_v4",
|
|
736
|
+
"wallet_disconnect",
|
|
737
|
+
"wallet_rehydrate"
|
|
738
|
+
];
|
|
739
|
+
function unwrapProvider(candidate) {
|
|
740
|
+
if (!candidate) return void 0;
|
|
741
|
+
if ("request" in candidate) return candidate;
|
|
742
|
+
return candidate.ethereum;
|
|
743
|
+
}
|
|
744
|
+
function collectCandidates() {
|
|
745
|
+
if (typeof window === "undefined") return [];
|
|
746
|
+
const providers = [];
|
|
747
|
+
const seen = /* @__PURE__ */ new Set();
|
|
748
|
+
const push = (candidate) => {
|
|
749
|
+
const provider = unwrapProvider(candidate);
|
|
750
|
+
if (!provider || seen.has(provider)) return;
|
|
751
|
+
seen.add(provider);
|
|
752
|
+
providers.push(provider);
|
|
753
|
+
};
|
|
754
|
+
push(window.ethereum);
|
|
755
|
+
window.ethereum?.providers?.forEach(push);
|
|
756
|
+
push(window.okxwallet);
|
|
757
|
+
push(window.coinbaseWalletExtension);
|
|
758
|
+
push(window.trustwallet);
|
|
759
|
+
push(window.phantom?.ethereum);
|
|
760
|
+
push(window.rabby);
|
|
761
|
+
push(window.bitkeep);
|
|
762
|
+
push(window.rainbow?.ethereum);
|
|
763
|
+
push(window.zerionWallet);
|
|
764
|
+
push(unwrapProvider(window.bitget));
|
|
765
|
+
return providers;
|
|
766
|
+
}
|
|
767
|
+
function discoverEIP6963Providers() {
|
|
768
|
+
if (typeof window === "undefined") return [];
|
|
769
|
+
const details = [];
|
|
770
|
+
const seen = /* @__PURE__ */ new Set();
|
|
771
|
+
const onAnnounce = (event) => {
|
|
772
|
+
const detail = event.detail;
|
|
773
|
+
if (!detail?.info?.uuid || !detail.provider) return;
|
|
774
|
+
if (seen.has(detail.info.uuid)) return;
|
|
775
|
+
seen.add(detail.info.uuid);
|
|
776
|
+
details.push(detail);
|
|
777
|
+
};
|
|
778
|
+
window.addEventListener("eip6963:announceProvider", onAnnounce);
|
|
779
|
+
window.dispatchEvent(new Event("eip6963:requestProviderInfo"));
|
|
780
|
+
window.removeEventListener("eip6963:announceProvider", onAnnounce);
|
|
781
|
+
return details;
|
|
782
|
+
}
|
|
783
|
+
function getWindow() {
|
|
784
|
+
return typeof window === "undefined" ? void 0 : window;
|
|
785
|
+
}
|
|
786
|
+
function getKnownNonMetaMaskProviders() {
|
|
787
|
+
const currentWindow = getWindow();
|
|
788
|
+
if (!currentWindow) return [];
|
|
789
|
+
const providers = [
|
|
790
|
+
unwrapProvider(currentWindow.okxwallet),
|
|
791
|
+
unwrapProvider(currentWindow.coinbaseWalletExtension),
|
|
792
|
+
unwrapProvider(currentWindow.trustwallet),
|
|
793
|
+
unwrapProvider(currentWindow.phantom?.ethereum),
|
|
794
|
+
unwrapProvider(currentWindow.rabby),
|
|
795
|
+
unwrapProvider(currentWindow.bitkeep),
|
|
796
|
+
unwrapProvider(currentWindow.rainbow?.ethereum),
|
|
797
|
+
currentWindow.zerionWallet,
|
|
798
|
+
unwrapProvider(currentWindow.bitget)
|
|
799
|
+
];
|
|
800
|
+
return providers.filter((provider) => Boolean(provider));
|
|
801
|
+
}
|
|
802
|
+
function findInjectedProvider(predicate) {
|
|
803
|
+
return collectCandidates().find(predicate);
|
|
804
|
+
}
|
|
805
|
+
function findEIP6963Provider(predicate) {
|
|
806
|
+
return discoverEIP6963Providers().find(predicate)?.provider;
|
|
807
|
+
}
|
|
808
|
+
function isMetaMaskOnly(provider) {
|
|
809
|
+
const knownNonMetaMaskProviders = getKnownNonMetaMaskProviders();
|
|
810
|
+
return provider.isMetaMask === true && !knownNonMetaMaskProviders.includes(provider) && provider.isCoinbaseWallet !== true && provider.isPhantom !== true && provider.isRabby !== true && provider.isOkxWallet !== true && provider.isTrust !== true && provider.isTrustWallet !== true && provider.isBitKeep !== true && provider.isRainbow !== true && provider.isZerion !== true && provider.isBraveWallet !== true && provider.isBitget !== true;
|
|
811
|
+
}
|
|
812
|
+
function rdnsIncludes(info, value) {
|
|
813
|
+
return info.rdns.toLowerCase().includes(value.toLowerCase());
|
|
814
|
+
}
|
|
815
|
+
function nameIncludes(info, value) {
|
|
816
|
+
return info.name.toLowerCase().includes(value.toLowerCase());
|
|
817
|
+
}
|
|
818
|
+
var FALLBACK_FUNDING_CHAIN = getChainInfo();
|
|
819
|
+
function evmChainInfoToWalletAddEthereumChainParams(info) {
|
|
820
|
+
const chainIdHex = `0x${BigInt(info.chainId).toString(16)}`;
|
|
821
|
+
return {
|
|
822
|
+
chainId: chainIdHex,
|
|
823
|
+
chainName: info.chainName,
|
|
824
|
+
nativeCurrency: {
|
|
825
|
+
name: info.nativeCurrencyName,
|
|
826
|
+
symbol: info.nativeCurrencySymbol,
|
|
827
|
+
decimals: info.nativeCurrencyDecimals
|
|
828
|
+
},
|
|
829
|
+
rpcUrls: [...info.rpcUrls],
|
|
830
|
+
blockExplorerUrls: [info.blockExplorerUrl]
|
|
831
|
+
};
|
|
832
|
+
}
|
|
833
|
+
var FALLBACK_FUNDING_CHAIN_ID_HEX = `0x${BigInt(FALLBACK_FUNDING_CHAIN.chainId).toString(16)}`;
|
|
834
|
+
var FALLBACK_FUNDING_CHAIN_PARAMS = evmChainInfoToWalletAddEthereumChainParams(FALLBACK_FUNDING_CHAIN);
|
|
835
|
+
async function switchToFallbackFundingChain(provider) {
|
|
836
|
+
const switchChain = () => provider.request({
|
|
837
|
+
method: "wallet_switchEthereumChain",
|
|
838
|
+
params: [{ chainId: FALLBACK_FUNDING_CHAIN_ID_HEX }]
|
|
839
|
+
});
|
|
840
|
+
try {
|
|
841
|
+
await switchChain();
|
|
842
|
+
} catch (err) {
|
|
843
|
+
const code = err?.code;
|
|
844
|
+
if (code === 4902) {
|
|
845
|
+
await provider.request({
|
|
846
|
+
method: "wallet_addEthereumChain",
|
|
847
|
+
params: [FALLBACK_FUNDING_CHAIN_PARAMS]
|
|
848
|
+
});
|
|
849
|
+
await switchChain();
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
852
|
+
throw err;
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
function createInjectedWalletProvider(title, provider) {
|
|
856
|
+
return {
|
|
857
|
+
async request(payload) {
|
|
858
|
+
try {
|
|
859
|
+
return await provider.request(payload);
|
|
860
|
+
} catch (error) {
|
|
861
|
+
if (payload.method === "eth_signTransaction") {
|
|
862
|
+
throw normalizeEvmTransactionSigningError(title, error);
|
|
863
|
+
}
|
|
864
|
+
throw error;
|
|
865
|
+
}
|
|
866
|
+
},
|
|
867
|
+
async disconnect() {
|
|
868
|
+
await provider.disconnect();
|
|
869
|
+
}
|
|
870
|
+
};
|
|
871
|
+
}
|
|
872
|
+
var InjectedEvmProvider = class extends AbstractProvider {
|
|
873
|
+
constructor(config, injected) {
|
|
874
|
+
super();
|
|
875
|
+
this.config = config;
|
|
876
|
+
this.injected = injected;
|
|
877
|
+
this.category = "plugin";
|
|
878
|
+
this.id = config.id;
|
|
879
|
+
this.title = config.title;
|
|
880
|
+
}
|
|
881
|
+
isAvailable() {
|
|
882
|
+
return Boolean(this.injected ?? this.config.resolveProvider());
|
|
883
|
+
}
|
|
884
|
+
get client() {
|
|
885
|
+
const provider = this.injected ?? this.config.resolveProvider();
|
|
886
|
+
if (provider) return provider;
|
|
887
|
+
throw new Error(`${this.title} provider not available`);
|
|
888
|
+
}
|
|
889
|
+
async connect(_args) {
|
|
890
|
+
const provider = this.client;
|
|
891
|
+
const walletProvider = createInjectedWalletProvider(this.title, provider);
|
|
892
|
+
const [address] = await walletProvider.request({ method: "eth_requestAccounts" }) ?? [];
|
|
893
|
+
if (!address) {
|
|
894
|
+
throw new Error(`${this.title} did not return an account`);
|
|
895
|
+
}
|
|
896
|
+
await switchToFallbackFundingChain(provider);
|
|
897
|
+
const chainId = await walletProvider.request({ method: "eth_chainId" });
|
|
898
|
+
const chain = getSupportedChainFromEvmChainId(chainId);
|
|
899
|
+
const session = {
|
|
900
|
+
address,
|
|
901
|
+
chain,
|
|
902
|
+
provider: walletProvider,
|
|
903
|
+
walletType: "injected",
|
|
904
|
+
authSource: "wallet",
|
|
905
|
+
sessionId: `${this.id}:${address.toLowerCase()}`,
|
|
906
|
+
capabilities: injectedWalletCapabilities,
|
|
907
|
+
chainContext: createChainContext(chain)
|
|
908
|
+
};
|
|
909
|
+
return this.setSession(session);
|
|
910
|
+
}
|
|
911
|
+
async reconnect(_persistedSession) {
|
|
912
|
+
const provider = this.injected ?? this.config.resolveProvider();
|
|
913
|
+
if (!provider) return null;
|
|
914
|
+
const walletProvider = createInjectedWalletProvider(this.title, provider);
|
|
915
|
+
const accounts = await provider.request({
|
|
916
|
+
method: "eth_accounts",
|
|
917
|
+
params: []
|
|
918
|
+
});
|
|
919
|
+
const [address] = accounts ?? [];
|
|
920
|
+
if (!address) return null;
|
|
921
|
+
let chain;
|
|
922
|
+
try {
|
|
923
|
+
const chainId = await walletProvider.request({ method: "eth_chainId" });
|
|
924
|
+
chain = getSupportedChainFromEvmChainId(chainId);
|
|
925
|
+
} catch {
|
|
926
|
+
return null;
|
|
927
|
+
}
|
|
928
|
+
const session = {
|
|
929
|
+
address,
|
|
930
|
+
chain,
|
|
931
|
+
provider: walletProvider,
|
|
932
|
+
walletType: "injected",
|
|
933
|
+
authSource: "wallet",
|
|
934
|
+
sessionId: `${this.id}:${address.toLowerCase()}`,
|
|
935
|
+
capabilities: injectedWalletCapabilities,
|
|
936
|
+
chainContext: createChainContext(chain)
|
|
937
|
+
};
|
|
938
|
+
return this.setSession(session);
|
|
939
|
+
}
|
|
940
|
+
};
|
|
941
|
+
var MetaMaskProvider = class extends InjectedEvmProvider {
|
|
942
|
+
constructor(injected) {
|
|
943
|
+
super(
|
|
944
|
+
{
|
|
945
|
+
id: "metamask",
|
|
946
|
+
title: "MetaMask",
|
|
947
|
+
resolveProvider: () => findEIP6963Provider(
|
|
948
|
+
({ info }) => rdnsIncludes(info, "io.metamask") || nameIncludes(info, "metamask")
|
|
949
|
+
) ?? findInjectedProvider(isMetaMaskOnly)
|
|
950
|
+
},
|
|
951
|
+
injected
|
|
952
|
+
);
|
|
953
|
+
}
|
|
954
|
+
};
|
|
955
|
+
var OKXProvider = class extends InjectedEvmProvider {
|
|
956
|
+
constructor(injected) {
|
|
957
|
+
super(
|
|
958
|
+
{
|
|
959
|
+
id: "okx",
|
|
960
|
+
title: "OKX Wallet",
|
|
961
|
+
resolveProvider: () => findEIP6963Provider(
|
|
962
|
+
({ info }) => rdnsIncludes(info, "okx") || nameIncludes(info, "okx")
|
|
963
|
+
) ?? unwrapProvider(getWindow()?.okxwallet) ?? findInjectedProvider((provider) => provider.isOkxWallet === true)
|
|
964
|
+
},
|
|
965
|
+
injected
|
|
966
|
+
);
|
|
967
|
+
}
|
|
968
|
+
};
|
|
969
|
+
var CoinbaseWalletProvider = class extends InjectedEvmProvider {
|
|
970
|
+
constructor(injected) {
|
|
971
|
+
super(
|
|
972
|
+
{
|
|
973
|
+
id: "coinbase",
|
|
974
|
+
title: "Coinbase Wallet",
|
|
975
|
+
resolveProvider: () => findEIP6963Provider(
|
|
976
|
+
({ info }) => rdnsIncludes(info, "com.coinbase.wallet") || nameIncludes(info, "coinbase")
|
|
977
|
+
) ?? unwrapProvider(getWindow()?.coinbaseWalletExtension) ?? findInjectedProvider((provider) => provider.isCoinbaseWallet === true)
|
|
978
|
+
},
|
|
979
|
+
injected
|
|
980
|
+
);
|
|
981
|
+
}
|
|
982
|
+
};
|
|
983
|
+
var TrustWalletProvider = class extends InjectedEvmProvider {
|
|
984
|
+
constructor(injected) {
|
|
985
|
+
super(
|
|
986
|
+
{
|
|
987
|
+
id: "trust",
|
|
988
|
+
title: "Trust Wallet",
|
|
989
|
+
resolveProvider: () => findEIP6963Provider(
|
|
990
|
+
({ info }) => rdnsIncludes(info, "trust") || nameIncludes(info, "trust")
|
|
991
|
+
) ?? unwrapProvider(getWindow()?.trustwallet) ?? findInjectedProvider(
|
|
992
|
+
(provider) => provider.isTrust === true || provider.isTrustWallet === true
|
|
993
|
+
)
|
|
994
|
+
},
|
|
995
|
+
injected
|
|
996
|
+
);
|
|
997
|
+
}
|
|
998
|
+
};
|
|
999
|
+
var PhantomProvider = class extends InjectedEvmProvider {
|
|
1000
|
+
constructor(injected) {
|
|
1001
|
+
super(
|
|
1002
|
+
{
|
|
1003
|
+
id: "phantom",
|
|
1004
|
+
title: "Phantom",
|
|
1005
|
+
resolveProvider: () => findEIP6963Provider(
|
|
1006
|
+
({ info }) => rdnsIncludes(info, "phantom") || nameIncludes(info, "phantom")
|
|
1007
|
+
) ?? unwrapProvider(getWindow()?.phantom?.ethereum) ?? findInjectedProvider((provider) => provider.isPhantom === true)
|
|
1008
|
+
},
|
|
1009
|
+
injected
|
|
1010
|
+
);
|
|
1011
|
+
}
|
|
1012
|
+
};
|
|
1013
|
+
var RabbyProvider = class extends InjectedEvmProvider {
|
|
1014
|
+
constructor(injected) {
|
|
1015
|
+
super(
|
|
1016
|
+
{
|
|
1017
|
+
id: "rabby",
|
|
1018
|
+
title: "Rabby",
|
|
1019
|
+
resolveProvider: () => findEIP6963Provider(
|
|
1020
|
+
({ info }) => rdnsIncludes(info, "rabby") || nameIncludes(info, "rabby")
|
|
1021
|
+
) ?? unwrapProvider(getWindow()?.rabby) ?? findInjectedProvider((provider) => provider.isRabby === true)
|
|
1022
|
+
},
|
|
1023
|
+
injected
|
|
1024
|
+
);
|
|
1025
|
+
}
|
|
1026
|
+
};
|
|
1027
|
+
var RainbowProvider = class extends InjectedEvmProvider {
|
|
1028
|
+
constructor(injected) {
|
|
1029
|
+
super(
|
|
1030
|
+
{
|
|
1031
|
+
id: "rainbow",
|
|
1032
|
+
title: "Rainbow",
|
|
1033
|
+
resolveProvider: () => findEIP6963Provider(
|
|
1034
|
+
({ info }) => rdnsIncludes(info, "rainbow") || nameIncludes(info, "rainbow")
|
|
1035
|
+
) ?? unwrapProvider(getWindow()?.rainbow?.ethereum) ?? findInjectedProvider((provider) => provider.isRainbow === true)
|
|
1036
|
+
},
|
|
1037
|
+
injected
|
|
1038
|
+
);
|
|
1039
|
+
}
|
|
1040
|
+
};
|
|
1041
|
+
var ZerionProvider = class extends InjectedEvmProvider {
|
|
1042
|
+
constructor(injected) {
|
|
1043
|
+
super(
|
|
1044
|
+
{
|
|
1045
|
+
id: "zerion",
|
|
1046
|
+
title: "Zerion",
|
|
1047
|
+
resolveProvider: () => findEIP6963Provider(
|
|
1048
|
+
({ info }) => rdnsIncludes(info, "zerion") || nameIncludes(info, "zerion")
|
|
1049
|
+
) ?? getWindow()?.zerionWallet ?? findInjectedProvider((provider) => provider.isZerion === true)
|
|
1050
|
+
},
|
|
1051
|
+
injected
|
|
1052
|
+
);
|
|
1053
|
+
}
|
|
1054
|
+
};
|
|
1055
|
+
var BraveWalletProvider = class extends InjectedEvmProvider {
|
|
1056
|
+
constructor(injected) {
|
|
1057
|
+
super(
|
|
1058
|
+
{
|
|
1059
|
+
id: "brave",
|
|
1060
|
+
title: "Brave Wallet",
|
|
1061
|
+
resolveProvider: () => findEIP6963Provider(
|
|
1062
|
+
({ info }) => rdnsIncludes(info, "brave") || nameIncludes(info, "brave")
|
|
1063
|
+
) ?? findInjectedProvider((provider) => provider.isBraveWallet === true)
|
|
1064
|
+
},
|
|
1065
|
+
injected
|
|
1066
|
+
);
|
|
1067
|
+
}
|
|
1068
|
+
};
|
|
1069
|
+
var BitgetProvider = class extends InjectedEvmProvider {
|
|
1070
|
+
constructor(injected) {
|
|
1071
|
+
super(
|
|
1072
|
+
{
|
|
1073
|
+
id: "bitget",
|
|
1074
|
+
title: "Bitget",
|
|
1075
|
+
resolveProvider: () => findEIP6963Provider(
|
|
1076
|
+
({ info }) => rdnsIncludes(info, "bitget") || nameIncludes(info, "bitget")
|
|
1077
|
+
) ?? unwrapProvider(getWindow()?.bitget) ?? findInjectedProvider((provider) => provider.isBitget === true)
|
|
1078
|
+
},
|
|
1079
|
+
injected
|
|
1080
|
+
);
|
|
1081
|
+
}
|
|
1082
|
+
};
|
|
1083
|
+
|
|
1084
|
+
// src/providers/plugin/injectedWalletRegistry.ts
|
|
1085
|
+
var installUrls = {
|
|
1086
|
+
metamask: "https://metamask.io/download/",
|
|
1087
|
+
okx: "https://www.okx.com/web3",
|
|
1088
|
+
coinbase: "https://www.coinbase.com/wallet/downloads",
|
|
1089
|
+
trust: "https://trustwallet.com/browser-extension",
|
|
1090
|
+
phantom: "https://phantom.com/download",
|
|
1091
|
+
rabby: "https://rabby.io/",
|
|
1092
|
+
rainbow: "https://rainbow.me/extension",
|
|
1093
|
+
zerion: "https://zerion.io/extension",
|
|
1094
|
+
brave: "https://brave.com/wallet/",
|
|
1095
|
+
bitget: "https://web3.bitget.com/en/wallet-download"
|
|
1096
|
+
};
|
|
1097
|
+
function createDefaultInjectedWalletRegistry() {
|
|
1098
|
+
const providers = [
|
|
1099
|
+
new MetaMaskProvider(),
|
|
1100
|
+
new OKXProvider(),
|
|
1101
|
+
new CoinbaseWalletProvider(),
|
|
1102
|
+
new TrustWalletProvider(),
|
|
1103
|
+
new PhantomProvider(),
|
|
1104
|
+
new RabbyProvider(),
|
|
1105
|
+
new RainbowProvider(),
|
|
1106
|
+
new ZerionProvider(),
|
|
1107
|
+
new BraveWalletProvider(),
|
|
1108
|
+
new BitgetProvider()
|
|
1109
|
+
];
|
|
1110
|
+
return providers.map((provider) => ({
|
|
1111
|
+
id: provider.id,
|
|
1112
|
+
title: provider.title,
|
|
1113
|
+
installUrl: installUrls[provider.id],
|
|
1114
|
+
provider,
|
|
1115
|
+
installed: provider.isAvailable()
|
|
1116
|
+
}));
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
export { BitgetProvider, BraveWalletProvider, CoinbaseWalletProvider, EVM_TRANSACTION_SIGNING_UNSUPPORTED, EvmTransactionSigningUnsupportedError, InjectedEvmProvider, MetaMaskProvider, OKXProvider, PhantomProvider, RabbyProvider, RainbowProvider, SessionStore, TrustWalletProvider, WalletConnector, ZerionProvider, createAccountController, createDefaultInjectedWalletRegistry, createWalletConnectController, createWalletExecutionClient, createWalletExecutionController, discoverEIP6963Providers, findEIP6963Provider, findInjectedProvider, getWalletExecutionCapabilities, isUnsupportedEvmTransactionSigningError, normalizeEvmTransactionSigningError, sessionStore, switchToFallbackFundingChain };
|